Skip to content

Commit 6ac1eb2

Browse files
AndrewScullAndroid (Google) Code Review
authored andcommitted
Merge "Explain why policy management items are disabled." into ub-testdpc-nyc
2 parents 3cdc9a8 + 345e8c8 commit 6ac1eb2

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.os.Build;
2828
import android.os.Build.VERSION_CODES;
2929
import android.os.UserManager;
30+
import android.support.v7.preference.Preference;
3031
import android.text.format.DateUtils;
3132
import android.widget.ImageView;
3233
import android.widget.Toast;
@@ -121,4 +122,9 @@ public static boolean isManagedProfile(Context context, ComponentName admin) {
121122
return devicePolicyManager.isManagedProfile(admin);
122123
}
123124
}
125+
126+
public static void disablePreference(Preference preference, int whyResId) {
127+
preference.setEnabled(false);
128+
preference.setSummary(whyResId);
129+
}
124130
}

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@
7070
import com.afwsamples.testdpc.DeviceAdminReceiver;
7171
import com.afwsamples.testdpc.R;
7272
import com.afwsamples.testdpc.common.AppInfoArrayAdapter;
73+
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
7374
import com.afwsamples.testdpc.common.CertificateUtil;
7475
import com.afwsamples.testdpc.common.MediaDisplayFragment;
75-
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
7676
import com.afwsamples.testdpc.common.Util;
7777
import com.afwsamples.testdpc.policy.blockuninstallation.BlockUninstallationInfoArrayAdapter;
7878
import com.afwsamples.testdpc.policy.certificate.DelegatedCertInstallerFragment;
@@ -1097,8 +1097,12 @@ private void updateStayOnWhilePluggedInPreference() {
10971097
* </p>
10981098
*/
10991099
public void updateInstallNonMarketAppsPreference() {
1100-
mInstallNonMarketAppsPreference.setEnabled(
1101-
mUserManager.hasUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES) ? false : true);
1100+
if (mUserManager.hasUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
1101+
Util.disablePreference(mInstallNonMarketAppsPreference, R.string.user_restricted);
1102+
} else {
1103+
mInstallNonMarketAppsPreference.setEnabled(true);
1104+
mInstallNonMarketAppsPreference.setSummary(null);
1105+
}
11021106
int isInstallNonMarketAppsAllowed = Settings.Secure.getInt(
11031107
getActivity().getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
11041108
mInstallNonMarketAppsPreference.setChecked(
@@ -1116,11 +1120,11 @@ private void disableIncompatibleManagementOptionsInCurrentProfile() {
11161120
if (isProfileOwner) {
11171121
// Some of the management options can only be applied in a primary profile.
11181122
for (String preference : PRIMARY_USER_ONLY_PREFERENCES) {
1119-
findPreference(preference).setEnabled(false);
1123+
Util.disablePreference(findPreference(preference), R.string.primary_user_only);
11201124
}
11211125
if (Util.isBeforeN()) {
11221126
for (String preference : PROFILE_OWNER_NYC_PLUS_PREFERENCES) {
1123-
findPreference(preference).setEnabled(false);
1127+
Util.disablePreference(findPreference(preference), R.string.requires_android_n);
11241128
}
11251129
}
11261130
deviceOwnerStatusStringId = R.string.this_is_a_profile_owner;
@@ -1130,26 +1134,27 @@ private void disableIncompatibleManagementOptionsInCurrentProfile() {
11301134
}
11311135
findPreference(DEVICE_OWNER_STATUS_KEY).setSummary(deviceOwnerStatusStringId);
11321136
if (!isDeviceOwner) {
1133-
findPreference(WIFI_CONFIG_LOCKDOWN_ENABLE_KEY).setEnabled(false);
1137+
Util.disablePreference(findPreference(WIFI_CONFIG_LOCKDOWN_ENABLE_KEY),
1138+
R.string.device_owner_only);
11341139
}
11351140
// Disable managed profile specific options if we are not running in managed profile.
11361141
if (!Util.isManagedProfile(getActivity(), mAdminComponentName)) {
11371142
for (String managedProfileSpecificOption : MANAGED_PROFILE_SPECIFIC_OPTIONS) {
1138-
findPreference(managedProfileSpecificOption).setEnabled(false);
1143+
Util.disablePreference(findPreference(managedProfileSpecificOption),
1144+
R.string.managed_profile_only);
11391145
}
11401146
}
11411147
}
11421148

11431149
private void disableIncompatibleManagementOptionsByApiLevel() {
11441150
if (Util.isBeforeM()) {
1145-
// The following options depend on MNC APIs.
11461151
for (String preference : MNC_PLUS_PREFERENCES) {
1147-
findPreference(preference).setEnabled(false);
1152+
Util.disablePreference(findPreference(preference), R.string.requires_android_m);
11481153
}
11491154
}
11501155
if (Util.isBeforeN()) {
11511156
for (String preference : NYC_PLUS_PREFERENCES) {
1152-
findPreference(preference).setEnabled(false);
1157+
Util.disablePreference(findPreference(preference), R.string.requires_android_n);
11531158
}
11541159
}
11551160
}
@@ -1441,7 +1446,13 @@ private void reloadEnableProcessLoggingUi() {
14411446
boolean isProcessLoggingEnabled = mDevicePolicyManager.isSecurityLoggingEnabled(
14421447
mAdminComponentName);
14431448
mEnableProcessLoggingPreference.setChecked(isProcessLoggingEnabled);
1444-
findPreference(REQUEST_PROCESS_LOGS).setEnabled(isProcessLoggingEnabled);
1449+
Preference requestLogsPreference = findPreference((REQUEST_PROCESS_LOGS));
1450+
if (isProcessLoggingEnabled) {
1451+
requestLogsPreference.setEnabled(true);
1452+
requestLogsPreference.setSummary(null);
1453+
} else {
1454+
Util.disablePreference(requestLogsPreference, R.string.requires_process_logs);
1455+
}
14451456
}
14461457
}
14471458

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ private void updateUserRestriction(String userRestriction) {
254254
private void disableIncompatibleRestrictionsByApiLevel() {
255255
if (Util.isBeforeM()) {
256256
for (String restriction : MNC_PLUS_RESTRICTIONS) {
257-
findPreference(restriction).setEnabled(false);
257+
Util.disablePreference(findPreference(restriction), R.string.requires_android_m);
258258
}
259259
}
260260
if (Util.isBeforeN()) {
261261
for (String restriction : NYC_PLUS_RESTRICTIONS) {
262-
findPreference(restriction).setEnabled(false);
262+
Util.disablePreference(findPreference(restriction), R.string.requires_android_n);
263263
}
264264
}
265265
}
@@ -270,17 +270,18 @@ private void disableIncompatibleRestrictionsByUserType() {
270270
boolean isDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(pkgName);
271271
if (isProfileOwner) {
272272
for (String restriction : PRIMARY_USER_ONLY_RESTRICTIONS) {
273-
findPreference(restriction).setEnabled(false);
273+
Util.disablePreference(findPreference(restriction), R.string.primary_user_only);
274274
}
275275
} else if (isDeviceOwner) {
276276
for (String restriction : MANAGED_PROFILE_ONLY_RESTRICTIONS) {
277-
findPreference(restriction).setEnabled(false);
277+
Util.disablePreference(findPreference(restriction), R.string.managed_profile_only);
278278
}
279279
}
280280

281281
if (Util.isManagedProfile(getActivity(), mAdminComponentName)) {
282282
for (String restriction : NON_MANAGED_PROFILE_RESTRICTIONS) {
283-
findPreference(restriction).setEnabled(false);
283+
Util.disablePreference(findPreference(restriction),
284+
R.string.non_managed_profile_only);
284285
}
285286
}
286287
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,20 @@ private void setup(String key, Object adminSetting) {
288288
}
289289

290290
// If the preference is not applicable, just hide it instead.
291-
if ((Keys.NOT_APPLICABLE_TO_PARENT.contains(key) && isParentProfileInstance())
292-
|| (Keys.NOT_APPLICABLE_TO_PROFILE.contains(key) && isManagedProfileInstance())
293-
|| (Keys.DEVICE_OWNER_ONLY.contains(key) && !isDeviceOwner())
294-
|| (Keys.NYC_PLUS.contains(key) && Util.isBeforeN())) {
295-
pref.setEnabled(false);
291+
if (Keys.NOT_APPLICABLE_TO_PARENT.contains(key) && isParentProfileInstance()) {
292+
Util.disablePreference(pref, R.string.not_for_parent_profile);
293+
return;
294+
}
295+
if (Keys.NOT_APPLICABLE_TO_PROFILE.contains(key) && isManagedProfileInstance()) {
296+
Util.disablePreference(pref, R.string.non_managed_profile_only);
297+
return;
298+
}
299+
if (Keys.DEVICE_OWNER_ONLY.contains(key) && !isDeviceOwner()) {
300+
Util.disablePreference(pref, R.string.device_owner_only);
301+
return;
302+
}
303+
if (Keys.NYC_PLUS.contains(key) && Util.isBeforeN()) {
304+
Util.disablePreference(pref, R.string.requires_android_n);
296305
return;
297306
}
298307

@@ -316,7 +325,7 @@ private void setup(String key, Object adminSetting) {
316325
private void disableIncompatibleManagementOptionsInCurrentProfile() {
317326
if (isProfileOwner() && Util.isBeforeM()) {
318327
for (String preference : Keys.PROFILE_OWNER_ONLY_MNC_PLUS) {
319-
findPreference(preference).setEnabled(false);
328+
Util.disablePreference(findPreference(preference), R.string.profile_owner_only);
320329
}
321330
}
322331
}

app/src/main/res/values/strings.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676
<string name="invalid_launch_intent_no_account">No account found on incoming intent.</string>
7777
<string name="device_admin_receiver_failure">Failed to become device/profile owner.</string>
7878
<string name="menu_search_title">Search</string>
79+
80+
<!-- Reasons for disabling preferences -->
81+
<string name="requires_android_m">Requires API level 23</string>
82+
<string name="requires_android_n">Requires API level 24</string>
83+
<string name="primary_user_only">Primary user only</string>
84+
<string name="not_for_parent_profile">Not applicable to parent profile</string>
85+
<string name="managed_profile_only">Managed profile only</string>
86+
<string name="non_managed_profile_only">Non-managed profile only</string>
87+
<string name="device_owner_only">Device owner only</string>
88+
<string name="profile_owner_only">Profile owner only</string>
89+
<string name="user_restricted">Disallowed by user restriction</string>
90+
<string name="requires_process_logs">Requires process logging to be enabled</string>
91+
7992
<!-- Strings for device owner management -->
8093
<string name="this_is_not_a_device_owner">This app is currently not the device owner.</string>
8194
<string name="this_is_a_device_owner">This app is the device owner.</string>

0 commit comments

Comments
 (0)