Skip to content

Commit 41239dd

Browse files
committed
Fixed TestDPC so it doesn't crash when its user is not the device owner.
Bug: 171350084 Bug: 181154143 Test: adb shell dpm set-device-owner --user 0 com.afwsamples.testdpc/.DeviceAdminReceiver && adb shell am start com.afwsamples.testdpc/.PolicyManagementActivity # on automotive Change-Id: Ifd9e9a5f17e1064aa68c2a457b0fea93158d68c9 (cherry picked from commit 6c63e72a173ea74e1f2c1781576e24032ea50412)
1 parent f811656 commit 41239dd

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

app/src/main/java/com/afwsamples/testdpc/common/preference/DpcPreferenceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class DpcPreferenceHelper {
6262
ADMIN_NONE | ADMIN_DEVICE_OWNER | ADMIN_PROFILE_OWNER | ADMIN_ORG_OWNED_PROFILE_OWNER;
6363
public static final int ADMIN_NOT_NONE = ADMIN_ANY & ~ADMIN_NONE;
6464
public static final @AdminKind int ADMIN_DEFAULT = ADMIN_NOT_NONE;
65-
public static final int NO_CUSTOM_CONSTRIANT = 0;
65+
public static final int NO_CUSTOM_CONSTRAINT = 0;
6666

6767
@Retention(RetentionPolicy.SOURCE)
6868
@IntDef(flag = true, value = {USER_PRIMARY_USER, USER_SECONDARY_USER, USER_MANAGED_PROFILE})
@@ -242,7 +242,7 @@ private CharSequence findConstraintViolation() {
242242

243243
for (CustomConstraint customConstraint : mCustomConstraints) {
244244
@StringRes int strRes = customConstraint.validateConstraint();
245-
if (strRes != NO_CUSTOM_CONSTRIANT) {
245+
if (strRes != NO_CUSTOM_CONSTRAINT) {
246246
return mContext.getString(strRes);
247247
}
248248
}

app/src/main/java/com/afwsamples/testdpc/comp/BindDeviceAdminFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.afwsamples.testdpc.comp;
1818

19-
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRIANT;
19+
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRAINT;
2020

2121
import android.annotation.TargetApi;
2222
import android.app.Activity;
@@ -97,7 +97,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
9797

9898
// Installing certificates makes sense for managed profile and secondary users.
9999
mInstallCaCertificatePreference.setCustomConstraint(
100-
getCustomConstraint(NO_CUSTOM_CONSTRIANT));
100+
getCustomConstraint(NO_CUSTOM_CONSTRAINT));
101101
}
102102

103103
@Override
@@ -116,7 +116,7 @@ private CustomConstraint getCustomConstraint(int secondaryUserConstraint) {
116116
} else if (!isManagedProfileRunning() || !isManagedProfileUnlocked()) {
117117
return R.string.managed_profile_not_running_or_unlocked;
118118
} else {
119-
return NO_CUSTOM_CONSTRIANT;
119+
return NO_CUSTOM_CONSTRAINT;
120120
}
121121
};
122122
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.afwsamples.testdpc.policy;
1818

1919
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
20-
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRIANT;
20+
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRAINT;
2121

2222
import android.accessibilityservice.AccessibilityServiceInfo;
2323
import android.accounts.Account;
@@ -631,7 +631,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
631631
mRequestSecurityLogsPreference = (DpcPreference) findPreference(REQUEST_SECURITY_LOGS);
632632
mRequestSecurityLogsPreference.setOnPreferenceClickListener(this);
633633
final CustomConstraint securityLoggingChecker = () -> isSecurityLoggingEnabled()
634-
? NO_CUSTOM_CONSTRIANT
634+
? NO_CUSTOM_CONSTRAINT
635635
: R.string.requires_security_logs;
636636
mRequestSecurityLogsPreference.setCustomConstraint(securityLoggingChecker);
637637
mRequestPreRebootSecurityLogsPreference =
@@ -644,7 +644,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
644644
mRequestNetworkLogsPreference.setOnPreferenceClickListener(this);
645645
mRequestNetworkLogsPreference.setCustomConstraint(
646646
() -> isNetworkLoggingEnabled()
647-
? NO_CUSTOM_CONSTRIANT
647+
? NO_CUSTOM_CONSTRAINT
648648
: R.string.requires_network_logs);
649649
findPreference(SET_ACCESSIBILITY_SERVICES_KEY).setOnPreferenceClickListener(this);
650650
findPreference(SET_INPUT_METHODS_KEY).setOnPreferenceClickListener(this);
@@ -707,7 +707,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
707707
mUserManager.hasUserRestriction(
708708
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY))
709709
? R.string.user_restricted
710-
: NO_CUSTOM_CONSTRIANT);
710+
: NO_CUSTOM_CONSTRAINT);
711711
mInstallNonMarketAppsPreference.setOnPreferenceChangeListener(this);
712712
findPreference(SET_USER_RESTRICTIONS_KEY).setOnPreferenceClickListener(this);
713713
mUserRestrictionsParentPreference = (DpcPreference) findPreference(SET_USER_RESTRICTIONS_PARENT_KEY);
@@ -742,7 +742,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
742742
(DpcPreference) findPreference(BIND_DEVICE_ADMIN_POLICIES);
743743
bindDeviceAdminPreference.setCustomConstraint(
744744
() -> (Util.getBindDeviceAdminTargetUsers(getActivity()).size() == 1)
745-
? NO_CUSTOM_CONSTRIANT
745+
? NO_CUSTOM_CONSTRAINT
746746
: R.string.require_one_po_to_bind);
747747
bindDeviceAdminPreference.setOnPreferenceClickListener(this);
748748

@@ -1619,9 +1619,13 @@ private void setScreenCaptureDisabledOnParent(boolean disabled) {
16191619
parentDpm.setScreenCaptureDisabled(mAdminComponentName, disabled);
16201620
}
16211621

1622+
private boolean isDeviceOwner() {
1623+
return mDevicePolicyManager.isDeviceOwnerApp(mPackageName);
1624+
}
1625+
16221626
@TargetApi(VERSION_CODES.O)
16231627
private boolean isNetworkLoggingEnabled() {
1624-
return mDevicePolicyManager.isNetworkLoggingEnabled(mAdminComponentName);
1628+
return isDeviceOwner() && mDevicePolicyManager.isNetworkLoggingEnabled(mAdminComponentName);
16251629
}
16261630

16271631
@TargetApi(VERSION_CODES.O)
@@ -2506,7 +2510,7 @@ private void reloadCameraDisableOnParentUi() {
25062510
@TargetApi(VERSION_CODES.O)
25072511
private void reloadEnableNetworkLoggingUi() {
25082512
if (mEnableNetworkLoggingPreference.isEnabled()) {
2509-
boolean isNetworkLoggingEnabled = mDevicePolicyManager.isNetworkLoggingEnabled(mAdminComponentName);
2513+
boolean isNetworkLoggingEnabled = isNetworkLoggingEnabled();
25102514
mEnableNetworkLoggingPreference.setChecked(isNetworkLoggingEnabled);
25112515
mRequestNetworkLogsPreference.refreshEnabledState();
25122516
}
@@ -3998,7 +4002,7 @@ private int validateAffiliatedUserAfterP() {
39984002
return R.string.require_affiliated_user;
39994003
}
40004004
}
4001-
return NO_CUSTOM_CONSTRIANT;
4005+
return NO_CUSTOM_CONSTRAINT;
40024006
}
40034007

40044008
@TargetApi(30)
@@ -4014,7 +4018,7 @@ private int validateDeviceOwnerBeforeO() {
40144018
return R.string.requires_device_owner;
40154019
}
40164020
}
4017-
return NO_CUSTOM_CONSTRIANT;
4021+
return NO_CUSTOM_CONSTRAINT;
40184022
}
40194023

40204024
private int validateDeviceOwnerBeforeP() {
@@ -4023,7 +4027,7 @@ private int validateDeviceOwnerBeforeP() {
40234027
return R.string.requires_device_owner;
40244028
}
40254029
}
4026-
return NO_CUSTOM_CONSTRIANT;
4030+
return NO_CUSTOM_CONSTRAINT;
40274031
}
40284032

40294033
private int validateDeviceOwnerBeforeQ() {
@@ -4032,7 +4036,7 @@ private int validateDeviceOwnerBeforeQ() {
40324036
return R.string.requires_device_owner;
40334037
}
40344038
}
4035-
return NO_CUSTOM_CONSTRIANT;
4039+
return NO_CUSTOM_CONSTRAINT;
40364040
}
40374041

40384042
abstract static class ManageLockTaskListCallback {

app/src/main/java/com/afwsamples/testdpc/policy/keyguard/LockScreenPolicyFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.afwsamples.testdpc.policy.keyguard;
1818

19-
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRIANT;
19+
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRAINT;
2020
import static com.afwsamples.testdpc.policy.keyguard.SetTrustAgentConfigFragment.Type;
2121

2222
import android.annotation.TargetApi;
@@ -282,7 +282,7 @@ private void setup(String key, Object adminSetting) {
282282
dpcPref.setCustomConstraint(
283283
() -> Keys.NOT_APPLICABLE_TO_PARENT.contains(key) && isParentProfileInstance()
284284
? R.string.not_for_parent_profile
285-
: NO_CUSTOM_CONSTRIANT
285+
: NO_CUSTOM_CONSTRAINT
286286
);
287287

288288
// We do not allow user to add trust agent config in pre-N devices in managed profile.

app/src/main/java/com/afwsamples/testdpc/policy/keyguard/PasswordConstraintsFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
2525
import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
2626

27-
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRIANT;
27+
import static com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.NO_CUSTOM_CONSTRAINT;
2828

2929
import android.app.admin.DevicePolicyManager;
3030
import android.content.ComponentName;
@@ -266,13 +266,13 @@ private void setPreferencesConstraint() {
266266
// Minimum length can be set for most qualities
267267
mMinLength.setCustomConstraint(
268268
() -> getDpmGateway().getPasswordQuality() >= PASSWORD_QUALITY_NUMERIC
269-
? NO_CUSTOM_CONSTRIANT
269+
? NO_CUSTOM_CONSTRAINT
270270
: R.string.not_for_password_quality);
271271

272272
// Other minimums are only active for the highest quality
273273
CustomConstraint constraint =
274274
() -> getDpmGateway().getPasswordQuality() == PASSWORD_QUALITY_COMPLEX
275-
? NO_CUSTOM_CONSTRIANT
275+
? NO_CUSTOM_CONSTRAINT
276276
: R.string.not_for_password_quality;
277277
mMinLetters.setCustomConstraint(constraint);
278278
mMinNumeric.setCustomConstraint(constraint);

0 commit comments

Comments
 (0)