Skip to content

Commit 85c22cc

Browse files
committed
Added Shell command to disabled keyguard.
Test: adb shell dumpsys activity service --user 0 com.afwsamples.testdpc set-keyguard-disabled true Test: adb shell dumpsys activity service --user 0 com.afwsamples.testdpc set-keyguard-disabled-features 510 Test: adb shell dumpsys activity service --user 0 com.afwsamples.testdpc get-keyguard-disabled-features Bug: 185509921 Bug: 171350084 Change-Id: I48167e94da6c04e84576b6601523e6a52c9ad2d7
1 parent d1c0750 commit 85c22cc

File tree

8 files changed

+122
-10
lines changed

8 files changed

+122
-10
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,23 @@ void setDeviceOwnerLockScreenInfo(CharSequence info, @NonNull Consumer<Void> onS
424424
*/
425425
CharSequence getDeviceOwnerLockScreenInfo();
426426

427+
/**
428+
* See {@link android.app.admin.DevicePolicyManager#setKeyguardDisabled(ComponentName, boolean)}.
429+
*/
430+
void setKeyguardDisabled(boolean disabled, @NonNull Consumer<Void> onSuccess,
431+
@NonNull Consumer<Exception> onError);
432+
433+
/**
434+
* See {@link android.app.admin.DevicePolicyManager#setKeyguardDisabledFeatures(ComponentName, int)}.
435+
*/
436+
void setKeyguardDisabledFeatures(int which, @NonNull Consumer<Void> onSuccess,
437+
@NonNull Consumer<Exception> onError);
438+
439+
/**
440+
* See {@link android.app.admin.DevicePolicyManager#getKeyguardDisabledFeatures(ComponentName)}.
441+
*/
442+
int getKeyguardDisabledFeatures();
443+
427444
/**
428445
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
429446
* call failed.

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ public void setLockTaskFeatures(int flags, Consumer<Void> onSuccess,
742742
public int getLockTaskFeatures() {
743743
int flags = mDevicePolicyManager.getLockTaskFeatures(mAdminComponentName);
744744
Log.d(TAG, "getLockTaskFeatures(): " + Util.lockTaskFeaturesToString(flags)
745-
+ " (" + flags + ")");
745+
+ " (" + flags + ")");
746746
return flags;
747747
}
748748

@@ -826,11 +826,46 @@ public void setDeviceOwnerLockScreenInfo(CharSequence info, Consumer<Void> onSuc
826826
}
827827
}
828828

829+
public void setKeyguardDisabled(boolean disabled, Consumer<Void> onSuccess,
830+
Consumer<Exception> onError) {
831+
Log.d(TAG, "KeyguardDisabled(" + disabled + ")");
832+
try {
833+
if (mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, disabled)) {
834+
onSuccess.accept(null);
835+
} else {
836+
onError.accept(new InvalidResultException("false", "setKeyguardDisabled(%b)",
837+
disabled));
838+
}
839+
} catch (Exception e) {
840+
onError.accept(e);
841+
}
842+
}
843+
844+
@Override
845+
public void setKeyguardDisabledFeatures(int which, Consumer<Void> onSuccess,
846+
Consumer<Exception> onError) {
847+
String features = Util.keyguardDisabledFeaturesToString(which);
848+
Log.d(TAG, "setKeyguardDisabledFeatures(" + features + ")");
849+
try {
850+
mDevicePolicyManager.setKeyguardDisabledFeatures(mAdminComponentName, which);
851+
onSuccess.accept(null);
852+
} catch (Exception e) {
853+
onError.accept(e);
854+
}
855+
}
856+
829857
@Override
830858
public CharSequence getDeviceOwnerLockScreenInfo() {
831859
return mDevicePolicyManager.getDeviceOwnerLockScreenInfo();
832860
}
833861

862+
public int getKeyguardDisabledFeatures() {
863+
int which = mDevicePolicyManager.getKeyguardDisabledFeatures(mAdminComponentName);
864+
Log.d(TAG, "getKeyguardDisabledFeatures(): " + Util.keyguardDisabledFeaturesToString(which)
865+
+ " (" + which + ")");
866+
return which;
867+
}
868+
834869
@Override
835870
public String toString() {
836871
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ final class ShellCommand {
103103
= "set-device-owner-lockscreen-info";
104104
private static final String CMD_GET_DEVICE_OWNER_LOCKSCREEN_INFO
105105
= "get-device-owner-lockscreen-info";
106+
private static final String CMD_SET_KEYGUARD_DISABLED = "set-keyguard-disabled";
107+
private static final String CMD_SET_KEYGUARD_DISABLED_FEATURES =
108+
"set-keyguard-disabled-features";
109+
private static final String CMD_GET_KEYGUARD_DISABLED_FEATURES =
110+
"get-keyguard-disabled-features";
106111

107112
// Commands for APIs added on Android S
108113
private static final String CMD_SET_PERMITTED_INPUT_METHODS_PARENT =
@@ -295,6 +300,15 @@ public void run() {
295300
case CMD_GET_DEVICE_OWNER_LOCKSCREEN_INFO:
296301
execute(() -> getDeviceOwnerLockScreenInfo());
297302
break;
303+
case CMD_SET_KEYGUARD_DISABLED:
304+
execute(() -> setKeyguardDisabled());
305+
break;
306+
case CMD_SET_KEYGUARD_DISABLED_FEATURES:
307+
execute(() -> setKeyguardDisabledFeatures());
308+
break;
309+
case CMD_GET_KEYGUARD_DISABLED_FEATURES:
310+
execute(() -> getKeyguardDisabledFeatures());
311+
break;
298312
default:
299313
mWriter.printf("Invalid command: %s\n\n", cmd);
300314
showUsage();
@@ -405,10 +419,14 @@ private void showUsage() {
405419
mWriter.printf("\t%s - get whether location is enabled for the user\n",
406420
CMD_IS_LOCATION_ENABLED);
407421
mWriter.printf("\t%s [INFO] - set the device owner lock screen info (or reset when no INFO "
408-
+ "is passed)\n",
409-
CMD_SET_DEVICE_OWNER_LOCKSCREEN_INFO);
410-
mWriter.printf("\t%s - get the device owner lock screen info",
422+
+ "is passed)\n", CMD_SET_DEVICE_OWNER_LOCKSCREEN_INFO);
423+
mWriter.printf("\t%s - get the device owner lock screen info\n",
411424
CMD_GET_DEVICE_OWNER_LOCKSCREEN_INFO);
425+
mWriter.printf("\t%s <true|false> - set keyguard disabled\n", CMD_SET_KEYGUARD_DISABLED);
426+
mWriter.printf("\t%s <FLAGS> - set the keyguard disabled features\n",
427+
CMD_SET_KEYGUARD_DISABLED_FEATURES);
428+
mWriter.printf("\t%s - get the keyguard disabled features\n",
429+
CMD_GET_KEYGUARD_DISABLED_FEATURES);
412430

413431
// Separator for S / pre-S commands - do NOT remove line to avoid cherry-pick conflicts
414432

@@ -943,6 +961,33 @@ private void getDeviceOwnerLockScreenInfo() {
943961
CharSequence info = mDevicePolicyManagerGateway.getDeviceOwnerLockScreenInfo();
944962
mWriter.printf("Lock screen info: %s\n", info);
945963
}
964+
965+
private void setKeyguardDisabled() {
966+
// TODO(b/171350084): check args
967+
boolean disabled = Boolean.parseBoolean(mArgs[1]);
968+
969+
mDevicePolicyManagerGateway.setKeyguardDisabled(disabled,
970+
(v) -> onSuccess("Set keyguard disabled to %b", disabled),
971+
(e) -> onError(e, "Error setting keyguard disabled to %b", disabled));
972+
}
973+
974+
private void setKeyguardDisabledFeatures() {
975+
int which = getIntArg(/* index= */ 1);
976+
977+
String features = Util.keyguardDisabledFeaturesToString(which);
978+
Log.i(TAG, "setKeyguardDisabledFeatures(" + which + "): setting to " + features);
979+
980+
mDevicePolicyManagerGateway.setKeyguardDisabledFeatures(which,
981+
(v) -> onSuccess("Set keyguard features to %s", features),
982+
(e) -> onError(e, "Error settings keyguard features to %s", features));
983+
}
984+
985+
private void getKeyguardDisabledFeatures() {
986+
int flags = mDevicePolicyManagerGateway.getKeyguardDisabledFeatures();
987+
String features = Util.keyguardDisabledFeaturesToString(flags);
988+
989+
mWriter.printf("%s (%d)\n", features, flags);
990+
}
946991

947992
private static String permittedToString(boolean permitted) {
948993
return permitted ? "PERMITTED" : "NOT PERMITTED";

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ protected void onSuccessLog(String method) {
222222
Log.d(mTag, method + "() succeeded");
223223
}
224224

225+
protected void onSuccessLog(String format, Object...args) {
226+
Log.d(mTag, String.format(format, args) + "() succeeded");
227+
}
228+
225229
protected void onErrorLog(String method, Exception e) {
226230
Log.e(mTag, method + "() failed: ", e);
227231
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ public static String grantStateToString(int grantState) {
272272
return constantToString(DevicePolicyManager.class, "PERMISSION_GRANT_STATE_", grantState);
273273
}
274274

275+
public static String keyguardDisabledFeaturesToString(int which) {
276+
return flagsToString(DevicePolicyManager.class, "KEYGUARD_DISABLE_", which);
277+
}
278+
275279
public static void onSuccessLog(String tag, String template, Object... args) {
276280
Log.d(tag, String.format(template, args) + " succeeded");
277281
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,11 @@ private void setUsbDataSignalingEnabled(boolean enabled) {
17091709

17101710
@TargetApi(VERSION_CODES.M)
17111711
private void setKeyGuardDisabled(boolean disabled) {
1712+
mDevicePolicyManagerGateway.setKeyguardDisabled(disabled,
1713+
(v) -> onSuccessLog("setKeyGuardDisabled(%b)", disabled),
1714+
(e) -> showToast(disabled ? R.string.unable_disable_keyguard
1715+
: R.string.unable_enable_keyguard));
1716+
17121717
if (!mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, disabled)) {
17131718
// this should not happen
17141719
if (disabled) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void onCreatePreferences(Bundle bundle, String rootKey) {
122122
addPreferencesFromResource(R.xml.lock_screen_preferences);
123123
setupAll();
124124
disableIncompatibleManagementOptionsInCurrentProfile();
125-
final int disabledFeatures = getDpm().getKeyguardDisabledFeatures(getAdmin());
125+
final int disabledFeatures = getDpmGateway().getKeyguardDisabledFeatures();
126126
for (Map.Entry<String, Integer> flag : KEYGUARD_FEATURES.entrySet()) {
127127
setup(flag.getKey(), (disabledFeatures & flag.getValue()) != 0 ? true : false);
128128
}
@@ -229,16 +229,18 @@ private void setLockScreenMessage(Preference preference, String newValue) {
229229
}
230230

231231
private boolean updateKeyguardFeatures(int flag, boolean newValue) {
232-
int disabledFeatures = getDpm().getKeyguardDisabledFeatures(getAdmin());
232+
int disabledFeatures = getDpmGateway().getKeyguardDisabledFeatures();
233233
if (newValue) {
234234
disabledFeatures |= flag;
235235
} else {
236236
disabledFeatures &= ~flag;
237237
}
238-
getDpm().setKeyguardDisabledFeatures(getAdmin(), disabledFeatures);
238+
getDpmGateway().setKeyguardDisabledFeatures(disabledFeatures,
239+
(v) -> onSuccessLog("setKeyguardDisabledFeatures"),
240+
(e) -> onErrorLog("setKeyguardDisabledFeatures", e));
239241

240242
// Verify that the new setting stuck.
241-
int newDisabledFeatures = getDpm().getKeyguardDisabledFeatures(getAdmin());
243+
int newDisabledFeatures = getDpmGateway().getKeyguardDisabledFeatures();
242244
return disabledFeatures == newDisabledFeatures;
243245
}
244246

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
224224
list.setValue((String) newValue);
225225
summary = list.getEntry();
226226
getDpmGateway().setPasswordQuality(value,
227-
(v) -> onSuccessLog("set password quality"),
228-
(e) -> onErrorLog("set password quality", e));
227+
(v) -> onSuccessLog("setPasswordQuality"),
228+
(e) -> onErrorLog("setPasswordQuality", e));
229229
refreshPreferences();
230230
break;
231231
}

0 commit comments

Comments
 (0)