Skip to content

Commit 5a4ed3e

Browse files
committed
Inform when preference will be active.
Gives options for more user types and changes the messages to explain when a feature can be used rather than what means it cannot be used. Change-Id: I788cd8c87ec33148a8ee0afa622e494a0ee30047
1 parent b1d4d4c commit 5a4ed3e

File tree

3 files changed

+96
-25
lines changed

3 files changed

+96
-25
lines changed

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

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.content.Context;
2121
import android.content.res.TypedArray;
2222
import android.os.Build;
23-
import android.os.UserManager;
2423
import android.support.v7.preference.Preference;
2524
import android.support.v7.preference.PreferenceViewHolder;
2625
import android.text.TextUtils;
@@ -32,6 +31,9 @@
3231
import com.afwsamples.testdpc.R;
3332
import com.afwsamples.testdpc.common.Util;
3433

34+
import java.util.ArrayList;
35+
import java.util.List;
36+
3537
/**
3638
* Helper class to check preference constraints declared in the XML file and disable the preference
3739
* with an informative message if the constraint does not hold. The API level, admin type (device
@@ -56,8 +58,13 @@ public class DpcPreferenceHelper {
5658
public static final int ADMIN_ANY = ADMIN_DEVICE_OWNER | ADMIN_PROFILE_OWNER;
5759

5860
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;
6168

6269
public DpcPreferenceHelper(Context context, Preference preference, AttributeSet attrs) {
6370
mContext = context;
@@ -163,20 +170,17 @@ public void clearCustomConstraint() {
163170
}
164171

165172
private void disableIfConstraintsNotMet() {
166-
mConstraintViolationSummary = findContraintViolation();
173+
mConstraintViolationSummary = findConstraintViolation();
167174
mPreference.setEnabled(constraintsMet());
168175
}
169176

170177
/**
171178
* Check for constraint violations.
172179
*
173-
* TODO(ascull): change message to say when the preference will be enabled rather than explain
174-
* why it is currently disabled.
175-
*
176180
* @return A string describing the constraint violation or {@code null} if no violations were
177181
* found.
178182
*/
179-
private CharSequence findContraintViolation() {
183+
private CharSequence findConstraintViolation() {
180184
if (Build.VERSION.SDK_INT < mMinSdkVersion) {
181185
return mContext.getString(R.string.requires_android_api_level, mMinSdkVersion);
182186
}
@@ -186,38 +190,91 @@ private CharSequence findContraintViolation() {
186190
return mCustomConstraintSummary;
187191
}
188192

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() {
190205
final DevicePolicyManager dpm =
191206
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
192207
final String packageName = mContext.getPackageName();
193208

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;
196211
}
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;
200214
}
201215

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;
205222
}
206223

207-
if (isDisabledForUser(USER_MANAGED_PROFILE) && Util.isManagedProfile(
224+
if (Util.isManagedProfile(
208225
mContext, DeviceAdminReceiver.getComponentName(mContext))) {
209-
return mContext.getString(R.string.not_for_managed_profile);
226+
return USER_MANAGED_PROFILE;
210227
}
211228

212-
return null;
229+
return USER_SECONDARY_USER;
230+
}
231+
232+
private boolean isEnabledForAdmin(int admin) {
233+
return (mAdminConstraint & admin) == admin;
213234
}
214235

215-
private boolean isDisabledForAdmin(int admin) {
216-
return (mAdminConstraint & admin) != admin;
236+
private boolean isEnabledForUser(int user) {
237+
return (mUserConstraint & user) == user;
217238
}
218239

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();
221278
}
222279

223280
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
<!-- Constrain a preference to certain users. -->
3636
<attr name="user">
3737
<flag name="primaryUser" value="0x1" />
38-
<flag name="managedProfile" value="0x2" />
38+
<flag name="secondaryUser" value="0x2" />
39+
<flag name="managedProfile" value="0x4" />
40+
41+
<flag name="notPrimaryUser" value="0x6" />
42+
<flag name="notSecondaryUser" value="0x5" />
43+
<flag name="notManagedProfile" value="0x3" />
3944
</attr>
4045
</declare-styleable>
4146
</resources>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080
<!-- Reasons for disabling preferences -->
8181
<string name="requires_android_api_level">Requires API level
8282
<xliff:g id="api level">%d</xliff:g></string>
83+
<string name="requires">Requires\u0020</string>
84+
<string name="requires_delimiter">,\u0020</string>
85+
<string name="requires_or">\u0020or\u0020</string>
86+
<string name="device_owner">device owner</string>
87+
<string name="profile_owner">profile owner</string>
88+
<string name="primary_user">primary user</string>
89+
<string name="secondary_user">secondary user</string>
90+
<string name="managed_profile">managed profile</string>
91+
8392
<string name="not_for_device_owner">Not applicable to device owner</string>
8493
<string name="not_for_primary_user">Not applicable to primary user</string>
8594
<string name="not_for_profile_owner">Not applicable to profile owner</string>

0 commit comments

Comments
 (0)