20
20
import android .content .Context ;
21
21
import android .content .res .TypedArray ;
22
22
import android .os .Build ;
23
- import android .os .UserManager ;
24
23
import android .support .v7 .preference .Preference ;
25
24
import android .support .v7 .preference .PreferenceViewHolder ;
26
25
import android .text .TextUtils ;
32
31
import com .afwsamples .testdpc .R ;
33
32
import com .afwsamples .testdpc .common .Util ;
34
33
34
+ import java .util .ArrayList ;
35
+ import java .util .List ;
36
+
35
37
/**
36
38
* Helper class to check preference constraints declared in the XML file and disable the preference
37
39
* with an informative message if the constraint does not hold. The API level, admin type (device
@@ -56,8 +58,13 @@ public class DpcPreferenceHelper {
56
58
public static final int ADMIN_ANY = ADMIN_DEVICE_OWNER | ADMIN_PROFILE_OWNER ;
57
59
58
60
public static final int USER_PRIMARY_USER = 0x1 ;
59
- public static final int USER_MANAGED_PROFILE = 0x2 ;
60
- public static final int USER_ANY = USER_PRIMARY_USER | USER_MANAGED_PROFILE ;
61
+ public static final int USER_SECONDARY_USER = 0x2 ;
62
+ public static final int USER_MANAGED_PROFILE = 0x4 ;
63
+ public static final int USER_ANY =
64
+ USER_PRIMARY_USER | USER_SECONDARY_USER | USER_MANAGED_PROFILE ;
65
+ public static final int NOT_USER_PRIMARY_USER = USER_ANY & ~USER_PRIMARY_USER ;
66
+ public static final int NOT_USER_SECONDARY_USER = USER_ANY & ~USER_SECONDARY_USER ;
67
+ public static final int NOT_USER_MANAGED_PROFILE = USER_ANY & ~USER_MANAGED_PROFILE ;
61
68
62
69
public DpcPreferenceHelper (Context context , Preference preference , AttributeSet attrs ) {
63
70
mContext = context ;
@@ -163,20 +170,17 @@ public void clearCustomConstraint() {
163
170
}
164
171
165
172
private void disableIfConstraintsNotMet () {
166
- mConstraintViolationSummary = findContraintViolation ();
173
+ mConstraintViolationSummary = findConstraintViolation ();
167
174
mPreference .setEnabled (constraintsMet ());
168
175
}
169
176
170
177
/**
171
178
* Check for constraint violations.
172
179
*
173
- * TODO(ascull): change message to say when the preference will be enabled rather than explain
174
- * why it is currently disabled.
175
- *
176
180
* @return A string describing the constraint violation or {@code null} if no violations were
177
181
* found.
178
182
*/
179
- private CharSequence findContraintViolation () {
183
+ private CharSequence findConstraintViolation () {
180
184
if (Build .VERSION .SDK_INT < mMinSdkVersion ) {
181
185
return mContext .getString (R .string .requires_android_api_level , mMinSdkVersion );
182
186
}
@@ -186,38 +190,91 @@ private CharSequence findContraintViolation() {
186
190
return mCustomConstraintSummary ;
187
191
}
188
192
189
- // Admin constraints
193
+ if (!isEnabledForAdmin (getCurrentAdmin ())) {
194
+ return getAdminConstraintSummary ();
195
+ }
196
+
197
+ if (!isEnabledForUser (getCurrentUser ())) {
198
+ return getUserConstraintSummary ();
199
+ }
200
+
201
+ return null ;
202
+ }
203
+
204
+ private int getCurrentAdmin () {
190
205
final DevicePolicyManager dpm =
191
206
(DevicePolicyManager ) mContext .getSystemService (Context .DEVICE_POLICY_SERVICE );
192
207
final String packageName = mContext .getPackageName ();
193
208
194
- if (isDisabledForAdmin ( ADMIN_DEVICE_OWNER ) && dpm .isDeviceOwnerApp (packageName )) {
195
- return mContext . getString ( R . string . not_for_device_owner ) ;
209
+ if (dpm .isDeviceOwnerApp (packageName )) {
210
+ return ADMIN_DEVICE_OWNER ;
196
211
}
197
-
198
- if (isDisabledForAdmin (ADMIN_PROFILE_OWNER ) && dpm .isProfileOwnerApp (packageName )) {
199
- return mContext .getString (R .string .not_for_profile_owner );
212
+ if (dpm .isProfileOwnerApp (packageName )) {
213
+ return ADMIN_PROFILE_OWNER ;
200
214
}
201
215
202
- // User constraints
203
- if (isDisabledForUser (USER_PRIMARY_USER ) && Util .isPrimaryUser (mContext )) {
204
- return mContext .getString (R .string .not_for_primary_user );
216
+ throw new RuntimeException ("Invalid admin for TestDPC" );
217
+ }
218
+
219
+ private int getCurrentUser () {
220
+ if (Util .isPrimaryUser (mContext )) {
221
+ return USER_PRIMARY_USER ;
205
222
}
206
223
207
- if (isDisabledForUser ( USER_MANAGED_PROFILE ) && Util .isManagedProfile (
224
+ if (Util .isManagedProfile (
208
225
mContext , DeviceAdminReceiver .getComponentName (mContext ))) {
209
- return mContext . getString ( R . string . not_for_managed_profile ) ;
226
+ return USER_MANAGED_PROFILE ;
210
227
}
211
228
212
- return null ;
229
+ return USER_SECONDARY_USER ;
230
+ }
231
+
232
+ private boolean isEnabledForAdmin (int admin ) {
233
+ return (mAdminConstraint & admin ) == admin ;
213
234
}
214
235
215
- private boolean isDisabledForAdmin (int admin ) {
216
- return (mAdminConstraint & admin ) != admin ;
236
+ private boolean isEnabledForUser (int user ) {
237
+ return (mUserConstraint & user ) == user ;
217
238
}
218
239
219
- private boolean isDisabledForUser (int user ) {
220
- return (mUserConstraint & user ) != user ;
240
+ private String getAdminConstraintSummary () {
241
+ final List <String > admins = new ArrayList <>(3 );
242
+
243
+ if (isEnabledForAdmin (ADMIN_DEVICE_OWNER )) {
244
+ admins .add (mContext .getString (R .string .device_owner ));
245
+ }
246
+ if (isEnabledForAdmin (ADMIN_PROFILE_OWNER )) {
247
+ admins .add (mContext .getString (R .string .profile_owner ));
248
+ }
249
+
250
+ return joinRequirementList (admins );
251
+ }
252
+
253
+ private String getUserConstraintSummary () {
254
+ final List <String > users = new ArrayList <>(3 );
255
+
256
+ if (isEnabledForUser (USER_PRIMARY_USER )) {
257
+ users .add (mContext .getString (R .string .primary_user ));
258
+ }
259
+ if (isEnabledForUser (USER_SECONDARY_USER )) {
260
+ users .add (mContext .getString (R .string .secondary_user ));
261
+ }
262
+ if (isEnabledForUser (USER_MANAGED_PROFILE )) {
263
+ users .add (mContext .getString (R .string .managed_profile ));
264
+ }
265
+
266
+ return joinRequirementList (users );
267
+ }
268
+
269
+ private String joinRequirementList (List <String > items ) {
270
+ final StringBuilder sb = new StringBuilder (mContext .getString (R .string .requires ));
271
+ final String lastItem = items .remove (items .size () - 1 );
272
+ sb .append (TextUtils .join (mContext .getString (R .string .requires_delimiter ), items ));
273
+ if (!items .isEmpty ()) {
274
+ sb .append (mContext .getString (R .string .requires_or ));
275
+ }
276
+ sb .append (lastItem );
277
+ return sb .toString ();
221
278
}
222
279
223
280
/**
0 commit comments