Skip to content

Commit 8755bed

Browse files
committed
Change validateAffiliatedUserAfterP to not require DO before P.
This was added as the isAffiliatedUser method is only available from P onwards. However support for affiliated PO calling these methods exists from N. This change will mean that on unaffiliated PO, the buttons will still be enabled but will show a toast when pressed indicating that they are not available. In other cases functionality will be as expected. Also adds validateDeviceOwnerBeforeP for those functions where the current use of validateAffiliatedUserAfterP is correct. This means DpcPreference now supports multiple CustomConstraints at a time. Bug: 110926860 Test: Manual. Tested on P that it is disabled for unaffiliated PO. Enabled in other cases. Tested on O that it is enabled for PO and DO, shows error toast when clicking while unaffiliated. Change-Id: Icf84ba1eb1ad1bccd0ae627f3e23598e9860d2a5 (cherry picked from commit b10764d92004bb771e89b26f9bd445db926ec5d4)
1 parent 13d31da commit 8755bed

File tree

7 files changed

+63
-17
lines changed

7 files changed

+63
-17
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void setCustomConstraint(@Nullable CustomConstraint customConstraint) {
103103
mHelper.setCustomConstraint(customConstraint);
104104
}
105105

106+
@Override
107+
public void addCustomConstraint(@Nullable CustomConstraint customConstraint) {
108+
mHelper.addCustomConstraint(customConstraint);
109+
}
110+
106111
@Override
107112
public void refreshEnabledState() {
108113
mHelper.disableIfConstraintsNotMet();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void setCustomConstraint(@Nullable CustomConstraint customConstraint) {
103103
mHelper.setCustomConstraint(customConstraint);
104104
}
105105

106+
@Override
107+
public void addCustomConstraint(@Nullable CustomConstraint customConstraint) {
108+
mHelper.addCustomConstraint(customConstraint);
109+
}
110+
106111
@Override
107112
public void refreshEnabledState() {
108113
mHelper.disableIfConstraintsNotMet();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void setCustomConstraint(@Nullable CustomConstraint customConstraint) {
103103
mHelper.setCustomConstraint(customConstraint);
104104
}
105105

106+
@Override
107+
public void addCustomConstraint(@Nullable CustomConstraint customConstraint) {
108+
mHelper.addCustomConstraint(customConstraint);
109+
}
110+
106111
@Override
107112
public void refreshEnabledState() {
108113
mHelper.disableIfConstraintsNotMet();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public interface DpcPreferenceBase {
2929
void clearUserConstraint();
3030
void clearNonCustomConstraints();
3131
void setCustomConstraint(@Nullable CustomConstraint customConstraint);
32+
void addCustomConstraint(@Nullable CustomConstraint customConstraint);
3233
/**
3334
* To re-check is the constraint met and enable/disable the preference accordingly.
3435
*/

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class DpcPreferenceHelper {
8181
private Preference mPreference;
8282

8383
private CharSequence mConstraintViolationSummary = null;
84-
private CustomConstraint mCustomConstraint = null;
84+
private List<CustomConstraint> mCustomConstraints = new ArrayList<>();
8585
private int mMinSdkVersion;
8686
private @AdminKind int mAdminConstraint;
8787
private @UserKind int mUserConstraint;
@@ -194,19 +194,37 @@ public void clearNonCustomConstraints() {
194194
/**
195195
* Specify a custom constraint by setting a {{@link CustomConstraint}} object. The enabled
196196
* state of the preference will be updated accordingly.
197+
*
198+
* <p>This will remove all previously set custom constraints. If you do not want to do this, use
199+
* {@link #addCustomConstraint(CustomConstraint)}.
197200
*/
198201
public void setCustomConstraint(CustomConstraint customConstraint) {
199-
mCustomConstraint = customConstraint;
202+
clearCustomConstraints();
203+
addCustomConstraint(customConstraint);
204+
}
205+
206+
/**
207+
* Add a {@link CustomConstraint} to be evaluated in addition to existing custom constraints.
208+
*
209+
* <p>The enabled state of the preference will be updated accordingly.
210+
*
211+
* <p>The new constraint will be enforced in addition to any previously set custom constraints.
212+
* If you'd prefer that this constraint replaces previous constraints,
213+
* use {@link #setCustomConstraint(CustomConstraint)}.
214+
*/
215+
public void addCustomConstraint(CustomConstraint customConstraint) {
216+
mCustomConstraints.add(customConstraint);
200217
disableIfConstraintsNotMet();
201218
}
202219

203220
/**
204-
* Remove any custom constraints set by {@link #setCustomConstraint(CustomConstraint)}.
205-
* <p/>
206-
* This method is safe to call if there is no current custom constraint.
221+
* Remove any custom constraints set by {@link #setCustomConstraint(CustomConstraint)} and
222+
* {@link #addCustomConstraint(CustomConstraint)}.
223+
*
224+
* <p>This method is safe to call if there is no current custom constraint.
207225
*/
208-
public void clearCustomConstraint() {
209-
setCustomConstraint(null);
226+
public void clearCustomConstraints() {
227+
mCustomConstraints.clear();
210228
}
211229

212230
public void disableIfConstraintsNotMet() {
@@ -233,8 +251,8 @@ private CharSequence findConstraintViolation() {
233251
return getUserConstraintSummary();
234252
}
235253

236-
if (mCustomConstraint != null) {
237-
@StringRes int strRes = mCustomConstraint.validateConstraint();
254+
for (CustomConstraint customConstraint : mCustomConstraints) {
255+
@StringRes int strRes = customConstraint.validateConstraint();
238256
if (strRes != NO_CUSTOM_CONSTRIANT) {
239257
return mContext.getString(strRes);
240258
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void setCustomConstraint(@Nullable CustomConstraint customConstraint) {
103103
mHelper.setCustomConstraint(customConstraint);
104104
}
105105

106+
@Override
107+
public void addCustomConstraint(@Nullable CustomConstraint customConstraint) {
108+
mHelper.addCustomConstraint(customConstraint);
109+
}
110+
106111
@Override
107112
public void refreshEnabledState() {
108113
mHelper.disableIfConstraintsNotMet();

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import android.os.AsyncTask;
5050
import android.os.BatteryManager;
5151
import android.os.Build;
52+
import android.os.Build.VERSION_CODES;
5253
import android.os.Bundle;
5354
import android.os.UserHandle;
5455
import android.os.UserManager;
@@ -545,15 +546,19 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
545546
mDisableStatusBarPreference = (DpcPreference) findPreference(DISABLE_STATUS_BAR);
546547
mDisableStatusBarPreference.setOnPreferenceClickListener(this);
547548
mDisableStatusBarPreference.setCustomConstraint(this::validateAffiliatedUserAfterP);
549+
mDisableStatusBarPreference.addCustomConstraint(this::validateDeviceOwnerBeforeP);
548550
mReenableStatusBarPreference = (DpcPreference) findPreference(REENABLE_STATUS_BAR);
549551
mReenableStatusBarPreference.setOnPreferenceClickListener(this);
550552
mReenableStatusBarPreference.setCustomConstraint(this::validateAffiliatedUserAfterP);
553+
mReenableStatusBarPreference.addCustomConstraint(this::validateDeviceOwnerBeforeP);
551554
mDisableKeyguardPreference = (DpcPreference) findPreference(DISABLE_KEYGUARD);
552555
mDisableKeyguardPreference.setOnPreferenceClickListener(this);
553556
mDisableKeyguardPreference.setCustomConstraint(this::validateAffiliatedUserAfterP);
557+
mDisableKeyguardPreference.addCustomConstraint(this::validateDeviceOwnerBeforeP);
554558
mReenableKeyguardPreference = (DpcPreference) findPreference(REENABLE_KEYGUARD);
555559
mReenableKeyguardPreference.setOnPreferenceClickListener(this);
556560
mReenableKeyguardPreference.setCustomConstraint(this::validateAffiliatedUserAfterP);
561+
mReenableKeyguardPreference.addCustomConstraint(this::validateDeviceOwnerBeforeP);
557562
findPreference(START_KIOSK_MODE).setOnPreferenceClickListener(this);
558563
mStayOnWhilePluggedInSwitchPreference = (SwitchPreference) findPreference(
559564
STAY_ON_WHILE_PLUGGED_IN);
@@ -3575,19 +3580,21 @@ private void removeAccount(Account account) {
35753580

35763581
@TargetApi(28)
35773582
private int validateAffiliatedUserAfterP() {
3578-
if (BuildCompat.isAtLeastP()) {
3579-
if (mDevicePolicyManager.isAffiliatedUser()) {
3580-
return NO_CUSTOM_CONSTRIANT;
3581-
} else {
3583+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
3584+
if (!mDevicePolicyManager.isAffiliatedUser()) {
35823585
return R.string.require_affiliated_user;
35833586
}
3584-
} else {
3585-
if (mDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
3586-
return NO_CUSTOM_CONSTRIANT;
3587-
} else {
3587+
}
3588+
return NO_CUSTOM_CONSTRIANT;
3589+
}
3590+
3591+
private int validateDeviceOwnerBeforeP() {
3592+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
3593+
if (!mDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
35883594
return R.string.requires_device_owner;
35893595
}
35903596
}
3597+
return NO_CUSTOM_CONSTRIANT;
35913598
}
35923599

35933600
abstract class ManageLockTaskListCallback {

0 commit comments

Comments
 (0)