|
55 | 55 | import android.app.admin.DevicePolicyManager;
|
56 | 56 | import android.content.ComponentName;
|
57 | 57 | import android.content.Context;
|
| 58 | +import android.os.Build; |
58 | 59 | import android.os.Bundle;
|
59 | 60 | import android.os.UserManager;
|
60 | 61 | import android.support.v14.preference.PreferenceFragment;
|
61 |
| -import android.support.v14.preference.SwitchPreference; |
62 | 62 | import android.support.v7.preference.Preference;
|
63 | 63 | import android.support.v7.preference.PreferenceScreen;
|
64 | 64 | import android.util.Log;
|
65 | 65 | import android.widget.Toast;
|
66 | 66 |
|
67 | 67 | import com.afwsamples.testdpc.DeviceAdminReceiver;
|
68 | 68 | import com.afwsamples.testdpc.R;
|
69 |
| -import com.afwsamples.testdpc.common.Util; |
| 69 | +import com.afwsamples.testdpc.common.preference.DpcPreferenceBase; |
| 70 | +import com.afwsamples.testdpc.common.preference.DpcPreferenceHelper; |
| 71 | +import com.afwsamples.testdpc.common.preference.DpcSwitchPreference; |
70 | 72 |
|
71 | 73 | public class UserRestrictionsDisplayFragment extends PreferenceFragment
|
72 | 74 | implements Preference.OnPreferenceChangeListener {
|
@@ -195,17 +197,17 @@ public void onCreatePreferences(Bundle bundle, String rootkey) {
|
195 | 197 | getPreferenceManager().getContext());
|
196 | 198 | setPreferenceScreen(preferenceScreen);
|
197 | 199 |
|
| 200 | + final Context preferenceContext = getPreferenceManager().getContext(); |
198 | 201 | for (UserRestriction restriction : ALL_USER_RESTRICTIONS) {
|
199 |
| - SwitchPreference preference = new SwitchPreference(getPreferenceManager().getContext()); |
| 202 | + DpcSwitchPreference preference = new DpcSwitchPreference(preferenceContext); |
200 | 203 | preference.setTitle(restriction.titleResId);
|
201 | 204 | preference.setKey(restriction.key);
|
202 | 205 | preference.setOnPreferenceChangeListener(this);
|
203 | 206 | preferenceScreen.addPreference(preference);
|
204 | 207 | }
|
205 | 208 |
|
206 | 209 | updateAllUserRestrictions();
|
207 |
| - disableIncompatibleRestrictionsByApiLevel(); |
208 |
| - disableIncompatibleRestrictionsByUserType(); |
| 210 | + constrainPerferences(); |
209 | 211 | }
|
210 | 212 |
|
211 | 213 | @Override
|
@@ -246,43 +248,31 @@ private void updateAllUserRestrictions() {
|
246 | 248 | }
|
247 | 249 |
|
248 | 250 | private void updateUserRestriction(String userRestriction) {
|
249 |
| - SwitchPreference preference = (SwitchPreference) findPreference(userRestriction); |
| 251 | + DpcSwitchPreference preference = (DpcSwitchPreference) findPreference(userRestriction); |
250 | 252 | boolean disallowed = mUserManager.hasUserRestriction(userRestriction);
|
251 | 253 | preference.setChecked(disallowed);
|
252 | 254 | }
|
253 | 255 |
|
254 |
| - private void disableIncompatibleRestrictionsByApiLevel() { |
255 |
| - if (Util.isBeforeM()) { |
256 |
| - for (String restriction : MNC_PLUS_RESTRICTIONS) { |
257 |
| - Util.disablePreference(findPreference(restriction), R.string.requires_android_m); |
258 |
| - } |
| 256 | + private void constrainPerferences() { |
| 257 | + for (String restriction : MNC_PLUS_RESTRICTIONS) { |
| 258 | + DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction); |
| 259 | + pref.setMinSdkVersion(Build.VERSION_CODES.M); |
259 | 260 | }
|
260 |
| - if (Util.isBeforeN()) { |
261 |
| - for (String restriction : NYC_PLUS_RESTRICTIONS) { |
262 |
| - Util.disablePreference(findPreference(restriction), R.string.requires_android_n); |
263 |
| - } |
| 261 | + for (String restriction : NYC_PLUS_RESTRICTIONS) { |
| 262 | + DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction); |
| 263 | + pref.setMinSdkVersion(Build.VERSION_CODES.N); |
264 | 264 | }
|
265 |
| - } |
266 |
| - |
267 |
| - private void disableIncompatibleRestrictionsByUserType() { |
268 |
| - String pkgName = getActivity().getPackageName(); |
269 |
| - boolean isProfileOwner = mDevicePolicyManager.isProfileOwnerApp(pkgName); |
270 |
| - boolean isDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(pkgName); |
271 |
| - if (isProfileOwner) { |
272 |
| - for (String restriction : PRIMARY_USER_ONLY_RESTRICTIONS) { |
273 |
| - Util.disablePreference(findPreference(restriction), R.string.primary_user_only); |
274 |
| - } |
275 |
| - } else if (isDeviceOwner) { |
276 |
| - for (String restriction : MANAGED_PROFILE_ONLY_RESTRICTIONS) { |
277 |
| - Util.disablePreference(findPreference(restriction), R.string.managed_profile_only); |
278 |
| - } |
| 265 | + for (String restriction : PRIMARY_USER_ONLY_RESTRICTIONS) { |
| 266 | + DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction); |
| 267 | + pref.setUserConstraint(DpcPreferenceHelper.USER_PRIMARY_USER); |
279 | 268 | }
|
280 |
| - |
281 |
| - if (Util.isManagedProfile(getActivity(), mAdminComponentName)) { |
282 |
| - for (String restriction : NON_MANAGED_PROFILE_RESTRICTIONS) { |
283 |
| - Util.disablePreference(findPreference(restriction), |
284 |
| - R.string.not_for_managed_profile); |
285 |
| - } |
| 269 | + for (String restriction : MANAGED_PROFILE_ONLY_RESTRICTIONS) { |
| 270 | + DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction); |
| 271 | + pref.setUserConstraint(DpcPreferenceHelper.USER_MANAGED_PROFILE); |
| 272 | + } |
| 273 | + for (String restriction : NON_MANAGED_PROFILE_RESTRICTIONS) { |
| 274 | + DpcPreferenceBase pref = (DpcPreferenceBase) findPreference(restriction); |
| 275 | + pref.setUserConstraint(DpcPreferenceHelper.USER_NOT_MANAGED_PROFILE); |
286 | 276 | }
|
287 | 277 | }
|
288 | 278 |
|
|
0 commit comments