Skip to content

Commit 4a96680

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Added Shell commands for password quality." into ub-testdpc-rvc
2 parents b47acb5 + c6880c6 commit 4a96680

File tree

7 files changed

+142
-25
lines changed

7 files changed

+142
-25
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGateway.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.afwsamples.testdpc;
1717

18+
import android.app.admin.DevicePolicyManager;
1819
import android.content.ComponentName;
1920
import android.graphics.Bitmap;
2021
import android.os.UserHandle;
@@ -40,6 +41,22 @@ public interface DevicePolicyManagerGateway {
4041
@NonNull
4142
ComponentName getAdmin();
4243

44+
/**
45+
* Gets the {@link android.app.admin.DevicePolicyManager}.
46+
*/
47+
@NonNull
48+
DevicePolicyManager getDevicePolicyManager();
49+
50+
/**
51+
* See {@link android.app.admin.DevicePolicyManager#isDeviceOwnerApp(String)}.
52+
*/
53+
boolean isDeviceOwnerApp();
54+
55+
/**
56+
* See {@link android.app.admin.DevicePolicyManager#isProfileOwnerApp(String)}.
57+
*/
58+
boolean isProfileOwnerApp();
59+
4360
/**
4461
* See {@link android.app.admin.DevicePolicyManager#createAndManageUser(android.content.ComponentName, String, android.content.ComponentName, android.os.PersistableBundle, int)}.
4562
*/
@@ -190,6 +207,17 @@ void wipeData(int flags, @NonNull Consumer<Void> onSuccess,
190207
*/
191208
void clearProfileOwner(@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
192209

210+
/**
211+
* See {@link android.app.admin.DevicePolicyManager#setPasswordQuality(ComponentName, int)
212+
*/
213+
void setPasswordQuality(int quality, @NonNull Consumer<Void> onSuccess,
214+
@NonNull Consumer<Exception> onError);
215+
216+
/**
217+
* See {@link android.app.admin.DevicePolicyManager#getPasswordQuality(ComponentName)
218+
*/
219+
int getPasswordQuality();
220+
193221
/**
194222
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
195223
* call failed.

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ public ComponentName getAdmin() {
6969
return mAdminComponentName;
7070
}
7171

72+
@Override
73+
public DevicePolicyManager getDevicePolicyManager() {
74+
return mDevicePolicyManager;
75+
}
76+
77+
@Override
78+
public boolean isProfileOwnerApp() {
79+
return mDevicePolicyManager.isProfileOwnerApp(mAdminComponentName.getPackageName());
80+
}
81+
82+
@Override
83+
public boolean isDeviceOwnerApp() {
84+
return mDevicePolicyManager.isDeviceOwnerApp(mAdminComponentName.getPackageName());
85+
}
86+
7287
@Override
7388
public void createAndManageUser(String name, int flags, Consumer<UserHandle> onSuccess,
7489
Consumer<Exception> onError) {
@@ -392,6 +407,26 @@ public void clearProfileOwner(Consumer<Void> onSuccess, Consumer<Exception> onEr
392407
}
393408
}
394409

410+
@Override
411+
public void setPasswordQuality(int quality, Consumer<Void> onSuccess,
412+
Consumer<Exception> onError) {
413+
Log.d(TAG, "setPasswordQuality(" + quality + ")");
414+
415+
try {
416+
mDevicePolicyManager.setPasswordQuality(mAdminComponentName, quality);
417+
onSuccess.accept(null);
418+
} catch (Exception e) {
419+
onError.accept(e);
420+
}
421+
}
422+
423+
@Override
424+
public int getPasswordQuality() {
425+
int quality = mDevicePolicyManager.getPasswordQuality(mAdminComponentName);
426+
Log.d(TAG, "getPasswordQuality(): " + quality);
427+
return quality;
428+
}
429+
395430
@Override
396431
public String toString() {
397432
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

app/src/main/java/com/afwsamples/testdpc/ShellCommand.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
final class ShellCommand {
4545
private static final String TAG = "TestDPCShellCommand";
4646

47+
private static final String CMD_DUMP = "dump";
4748
private static final String CMD_CREATE_USER = "create-user";
4849
private static final String CMD_SET_USER_ICON = "set-user-icon";
4950
private static final String CMD_REMOVE_USER = "remove-user";
@@ -70,6 +71,8 @@ final class ShellCommand {
7071
private static final String CMD_REMOVE_ACTIVE_ADMIN = "remove-active-admin";
7172
private static final String CMD_CLEAR_DEVICE_OWNER = "clear-device-owner";
7273
private static final String CMD_CLEAR_PROFILE_OWNER = "clear-profile-owner";
74+
private static final String CMD_SET_PASSWORD_QUALITY = "set-password-quality";
75+
private static final String CMD_GET_PASSWORD_QUALITY = "get-password-quality";
7376

7477
private static final String ARG_FLAGS = "--flags";
7578

@@ -95,6 +98,9 @@ public void run() {
9598
}
9699
String cmd = mArgs[0];
97100
switch (cmd) {
101+
case CMD_DUMP:
102+
dumpState();
103+
break;
98104
case CMD_HELP:
99105
showUsage();
100106
break;
@@ -167,15 +173,27 @@ public void run() {
167173
case CMD_CLEAR_PROFILE_OWNER:
168174
execute(() -> clearProfileOwner());
169175
break;
176+
case CMD_SET_PASSWORD_QUALITY:
177+
execute(() -> setPasswordQuality());
178+
break;
179+
case CMD_GET_PASSWORD_QUALITY:
180+
execute(() -> getPasswordQuality());
181+
break;
170182
default:
171183
mWriter.printf("Invalid command: %s\n\n", cmd);
172184
showUsage();
173185
}
174186
}
175187

188+
private void dumpState() {
189+
mWriter.printf("isDeviceOwner: %b\n", mDevicePolicyManagerGateway.isDeviceOwnerApp());
190+
mWriter.printf("isProfileOwner: %b\n", mDevicePolicyManagerGateway.isProfileOwnerApp());
191+
}
192+
176193
private void showUsage() {
177194
mWriter.printf("Usage:\n\n");
178195
mWriter.printf("\t%s - show this help\n", CMD_HELP);
196+
mWriter.printf("\t%s - dump internal state\n", CMD_DUMP);
179197
mWriter.printf("\t%s [%s FLAGS] [NAME] - create a user with the optional flags and name\n",
180198
CMD_CREATE_USER, ARG_FLAGS);
181199
File setIconRootDir = UserIconContentProvider.getStorageDirectory(mContext);
@@ -220,6 +238,9 @@ private void showUsage() {
220238
mWriter.printf("\t%s - remove itself as an admin\n", CMD_REMOVE_ACTIVE_ADMIN);
221239
mWriter.printf("\t%s - clear itself as device owner \n", CMD_CLEAR_DEVICE_OWNER);
222240
mWriter.printf("\t%s - clear itself as profile owner \n", CMD_CLEAR_PROFILE_OWNER);
241+
mWriter.printf("\t%s <QUALITY> - set password quality\n",
242+
CMD_SET_PASSWORD_QUALITY);
243+
mWriter.printf("\t%s - get password quality\n", CMD_GET_PASSWORD_QUALITY);
223244
}
224245

225246
private void createUser() {
@@ -461,6 +482,19 @@ private void clearProfileOwner() {
461482
(e) -> onError(e, "Error removing %s as profile owner", pkg));
462483
}
463484

485+
private void setPasswordQuality() {
486+
int quality = getIntArg(/* index= */ 1);
487+
Log.i(TAG, "setPasswordQuality(" + quality + ")");
488+
489+
mDevicePolicyManagerGateway.setPasswordQuality(quality,
490+
(v) -> onSuccess("Set password quality to %d", quality),
491+
(e) -> onError(e, "Error setting password quality to %d", quality));
492+
}
493+
494+
private void getPasswordQuality() {
495+
mWriter.printf("password quality: %d\n", mDevicePolicyManagerGateway.getPasswordQuality());
496+
}
497+
464498
private void execute(@NonNull Runnable r) {
465499
try {
466500
r.run();

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import androidx.recyclerview.widget.LinearLayoutManager;
1212
import androidx.recyclerview.widget.RecyclerView;
1313
import android.text.TextUtils;
14+
import android.util.Log;
1415
import android.view.Menu;
1516
import android.view.MenuInflater;
1617
import android.view.MenuItem;
@@ -32,11 +33,21 @@ public abstract class BaseSearchablePolicyPreferenceFragment extends PreferenceF
3233
protected LinearLayoutManager mLayoutManager;
3334
private HighlightablePreferenceGroupAdapter mAdapter;
3435
private String mPreferenceKey;
35-
private boolean mPreferenceHighlighted = false;
36+
private boolean mPreferenceHighlighted;
37+
private final String mTag;
38+
3639
public static final String EXTRA_PREFERENCE_KEY = "preference_key";
3740
private static final String SAVE_HIGHLIGHTED_KEY = "preference_highlighted";
3841
private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 500;
3942

43+
protected BaseSearchablePolicyPreferenceFragment() {
44+
mTag = getClass().getSimpleName();
45+
}
46+
47+
protected BaseSearchablePolicyPreferenceFragment(String tag) {
48+
mTag = tag;
49+
}
50+
4051
@Override
4152
public void onCreate(Bundle savedInstanceState) {
4253
super.onCreate(savedInstanceState);
@@ -206,4 +217,12 @@ public void onBindViewHolder(PreferenceViewHolder holder, int position) {
206217
* @return whether the preference fragment is available.
207218
*/
208219
public abstract boolean isAvailable(Context context);
220+
221+
protected void onSuccessLog(String method) {
222+
Log.d(mTag, method + "() succeeded");
223+
}
224+
225+
protected void onErrorLog(String method, Exception e) {
226+
Log.e(mTag, method + "() failed: ", e);
227+
}
209228
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import android.view.View;
3030
import android.view.ViewGroup;
3131
import com.afwsamples.testdpc.DeviceAdminReceiver;
32+
import com.afwsamples.testdpc.DevicePolicyManagerGateway;
33+
import com.afwsamples.testdpc.DevicePolicyManagerGatewayImpl;
3234
import com.afwsamples.testdpc.R;
3335

3436
/**
@@ -98,21 +100,28 @@ public View onCreateView(
98100
public abstract Class<? extends ProfileOrParentFragment> getContentClass();
99101
}
100102

101-
private DevicePolicyManager mDevicePolicyManager;
102-
private ComponentName mAdminComponent;
103+
private DevicePolicyManagerGateway mDevicePolicyGateway;
103104
private boolean mParentInstance;
104105
private boolean mProfileOwner;
105106
private boolean mDeviceOwner;
106107

108+
public ProfileOrParentFragment() {
109+
super(LOG_TAG);
110+
}
111+
107112
/**
108113
* @return a {@link DevicePolicyManager} instance for the profile this tab should affect.
109114
*/
110115
protected final DevicePolicyManager getDpm() {
111-
return mDevicePolicyManager;
116+
return mDevicePolicyGateway.getDevicePolicyManager();
117+
}
118+
119+
protected final DevicePolicyManagerGateway getDpmGateway() {
120+
return mDevicePolicyGateway;
112121
}
113122

114123
protected final ComponentName getAdmin() {
115-
return mAdminComponent;
124+
return mDevicePolicyGateway.getAdmin();
116125
}
117126

118127
/**
@@ -146,18 +155,15 @@ public void onCreate(Bundle savedInstanceState) {
146155
mParentInstance = arguments.getBoolean(EXTRA_PARENT_PROFILE, false);
147156
}
148157

149-
mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity());
150-
151-
// Get a device policy manager for the current user.
152-
mDevicePolicyManager = (DevicePolicyManager)
153-
getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
158+
Context context = getActivity();
159+
mDevicePolicyGateway = new DevicePolicyManagerGatewayImpl(context);
154160

155161
// Store whether we are the profile owner for faster lookup.
156-
mProfileOwner = mDevicePolicyManager.isProfileOwnerApp(getActivity().getPackageName());
157-
mDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(getActivity().getPackageName());
162+
mProfileOwner = mDevicePolicyGateway.isProfileOwnerApp();
163+
mDeviceOwner = mDevicePolicyGateway.isDeviceOwnerApp();
158164

159165
if (mParentInstance) {
160-
mDevicePolicyManager = mDevicePolicyManager.getParentProfileInstance(mAdminComponent);
166+
mDevicePolicyGateway = DevicePolicyManagerGatewayImpl.forParentProfile(context);
161167
}
162168

163169
// Put at last to make sure all initializations above are done before subclass's

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
12321232
SetSupportMessageFragment.TYPE_LONG));
12331233
return true;
12341234
case SET_NEW_PASSWORD:
1235+
Log.d(TAG, "starting " + DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
12351236
startActivity(new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD));
12361237
return true;
12371238
case SET_PROFILE_PARENT_NEW_PASSWORD:
@@ -4048,14 +4049,6 @@ private void setAutoTimeZoneEnabled(boolean enabled) {
40484049
mDevicePolicyManager.setAutoTimeZoneEnabled(mAdminComponentName, enabled);
40494050
}
40504051

4051-
private void onSuccessLog(String method) {
4052-
Log.d(TAG, method + "() succeeded");
4053-
}
4054-
4055-
private void onErrorLog(String method, Exception e) {
4056-
Log.e(TAG, method + "() failed: ", e);
4057-
}
4058-
40594052
private void onSuccessShowToast(String method, int msgId) {
40604053
Log.d(TAG, method + "() succeeded");
40614054
showToast(msgId);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
170170
setup(Keys.HISTORY_LENGTH, getDpm().getPasswordHistoryLength(getAdmin()));
171171

172172
// Minimum quality requirement.
173-
setup(Keys.QUALITY, PASSWORD_QUALITIES.floorKey(getDpm().getPasswordQuality(getAdmin())));
173+
setup(Keys.QUALITY, PASSWORD_QUALITIES.floorKey(getDpmGateway().getPasswordQuality()));
174174

175175
// Minimum length requirements.
176176
setup(Keys.MIN_LENGTH, getDpm().getPasswordMinimumLength(getAdmin()));
@@ -223,7 +223,9 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
223223
// Store newValue now so getEntry() can return the new setting
224224
list.setValue((String) newValue);
225225
summary = list.getEntry();
226-
getDpm().setPasswordQuality(getAdmin(), value);
226+
getDpmGateway().setPasswordQuality(value,
227+
(v) -> onSuccessLog("set password quality"),
228+
(e) -> onErrorLog("set password quality", e));
227229
refreshPreferences();
228230
break;
229231
}
@@ -263,13 +265,13 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
263265
private void setPreferencesConstraint() {
264266
// Minimum length can be set for most qualities
265267
mMinLength.setCustomConstraint(
266-
() -> getDpm().getPasswordQuality(getAdmin()) >= PASSWORD_QUALITY_NUMERIC
268+
() -> getDpmGateway().getPasswordQuality() >= PASSWORD_QUALITY_NUMERIC
267269
? NO_CUSTOM_CONSTRIANT
268270
: R.string.not_for_password_quality);
269271

270272
// Other minimums are only active for the highest quality
271273
CustomConstraint constraint =
272-
() -> getDpm().getPasswordQuality(getAdmin()) == PASSWORD_QUALITY_COMPLEX
274+
() -> getDpmGateway().getPasswordQuality() == PASSWORD_QUALITY_COMPLEX
273275
? NO_CUSTOM_CONSTRIANT
274276
: R.string.not_for_password_quality;
275277
mMinLetters.setCustomConstraint(constraint);

0 commit comments

Comments
 (0)