Skip to content

Commit d449b75

Browse files
committed
New Shell commands to manage lock task features and packages.
Examples: $ alias testdpc='adb shell dumpsys activity service --user 0 com.afwsamples.testdpc' $ testdpc is-lock-task-permitted com.afwsamples.testdpc com.afwsamples.testdpc: NOT PERMITTED $ testdpc set-lock-task-packages com.afwsamples.testdpc Set lock tasks packages to [com.afwsamples.testdpc] $ testdpc is-lock-task-permitted com.afwsamples.testdpc com.afwsamples.testdpc: PERMITTED $ testdpc get-lock-task-packages [com.afwsamples.testdpc] $ testdpc set-lock-task-features 4 Set lock tasks features to HOME $ testdpc get-lock-task-features HOME (4) // launch TestDpc and select "Self-start lock task mode" Test: see above Bug: 171350084 Bug: 181154143 Change-Id: Ifaa25d680dc3f4190919469775ee7139ff945953 (cherry picked from commit 38e54fc66649f56cee77f6d0247779a39b3597c4)
1 parent 3c1dc13 commit d449b75

File tree

8 files changed

+233
-37
lines changed

8 files changed

+233
-37
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
@@ -244,6 +244,34 @@ void setPackagesSuspended(String[] packageNames, boolean suspended, @NonNull Con
244244
*/
245245
boolean isPackageSuspended(String packageName) throws NameNotFoundException;
246246

247+
// TODO(b/171350084): use in other places
248+
/**
249+
* See {@link android.app.admin.DevicePolicyManager#setLockTaskPackages(ComponentName, String[])}.
250+
*/
251+
void setLockTaskPackages(String[] packages, @NonNull Consumer<Void> onSuccess,
252+
@NonNull Consumer<Exception> onError);
253+
254+
/**
255+
* See {@link android.app.admin.DevicePolicyManager#getLockTaskPackages(ComponentName)}.
256+
*/
257+
String[] getLockTaskPackages();
258+
259+
/**
260+
* See {@link android.app.admin.DevicePolicyManager#setLockTaskFeatures(ComponentName, int)}.
261+
*/
262+
void setLockTaskFeatures(int flags, @NonNull Consumer<Void> onSuccess,
263+
@NonNull Consumer<Exception> onError);
264+
265+
/**
266+
* See {@link android.app.admin.DevicePolicyManager#getLockTaskFeatures(ComponentName)}.
267+
*/
268+
int getLockTaskFeatures();
269+
270+
/**
271+
* See {@link android.app.admin.DevicePolicyManager#isLockTaskPermitted(ComponentName, String)}.
272+
*/
273+
boolean isLockTaskPermitted(String packageName);
274+
247275
/**
248276
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
249277
* call failed.

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

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import androidx.annotation.NonNull;
3131

32+
import com.afwsamples.testdpc.common.Util;
33+
3234
import java.util.Arrays;
3335
import java.util.List;
3436
import java.util.Set;
@@ -246,7 +248,7 @@ public void setUserRestriction(String userRestriction, boolean enabled) {
246248
String message = String.format("setUserRestriction(%s, %b)", userRestriction, enabled);
247249
setUserRestriction(userRestriction, enabled,
248250
(v) -> onSuccessLog(message),
249-
(e) -> onErrorLog(message));
251+
(e) -> onErrorLog(e, message));
250252
}
251253

252254
@Override
@@ -339,7 +341,7 @@ public void setNetworkLogging(boolean enabled) {
339341
String message = String.format("setNetworkLogging(%b)", enabled);
340342
setNetworkLogging(enabled,
341343
(v) -> onSuccessLog(message),
342-
(e) -> onErrorLog(message));
344+
(e) -> onErrorLog(e, message));
343345
}
344346

345347
@Override
@@ -466,17 +468,59 @@ public boolean isPackageSuspended(String packageName) throws NameNotFoundExcepti
466468
return mDevicePolicyManager.isPackageSuspended(mAdminComponentName, packageName);
467469
}
468470

471+
@Override
472+
public void setLockTaskPackages(String[] packages, Consumer<Void> onSuccess,
473+
Consumer<Exception> onError) {
474+
Log.d(TAG, "setLockTaskPackages(" + Arrays.toString(packages) + ")");
475+
try {
476+
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName, packages);
477+
onSuccess.accept(null);
478+
} catch (Exception e) {
479+
onError.accept(e);
480+
}
481+
}
482+
483+
@Override
484+
public String[] getLockTaskPackages() {
485+
return mDevicePolicyManager.getLockTaskPackages(mAdminComponentName);
486+
}
487+
488+
@Override
489+
public void setLockTaskFeatures(int flags, Consumer<Void> onSuccess,
490+
Consumer<Exception> onError) {
491+
String features = Util.lockTaskFeaturesToString(flags);
492+
Log.d(TAG, "setLockTaskFeatures(" + features + ")");
493+
try {
494+
mDevicePolicyManager.setLockTaskFeatures(mAdminComponentName, flags);
495+
onSuccess.accept(null);
496+
} catch (Exception e) {
497+
onError.accept(e);
498+
}
499+
}
500+
501+
@Override
502+
public int getLockTaskFeatures() {
503+
int flags = mDevicePolicyManager.getLockTaskFeatures(mAdminComponentName);
504+
Log.d(TAG, "getLockTaskFeatures(): " + Util.lockTaskFeaturesToString(flags)
505+
+ " (" + flags + ")");
506+
return flags;
507+
}
508+
509+
@Override
510+
public boolean isLockTaskPermitted(String packageName) {
511+
return mDevicePolicyManager.isLockTaskPermitted(packageName);
512+
}
513+
469514
@Override
470515
public String toString() {
471516
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";
472517
}
473518

474-
private void onSuccessLog(String template, Object... args) {
475-
Log.d(TAG, String.format(template, args) + " succeeded");
519+
private static void onSuccessLog(String template, Object... args) {
520+
Util.onSuccessLog(TAG, template, args);
476521
}
477522

478-
private void onErrorLog(String template, Object... args) {
479-
Log.d(TAG, String.format(template, args) + " failed");
523+
private static void onErrorLog(Exception e, String template, Object... args) {
524+
Util.onErrorLog(TAG, e, template, args);
480525
}
481-
482526
}

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
2929

30+
import com.afwsamples.testdpc.common.Util;
31+
3032
import java.io.File;
3133
import java.io.PrintWriter;
3234
import java.util.ArrayList;
@@ -77,6 +79,11 @@ final class ShellCommand {
7779
private static final String CMD_TRANSFER_OWNERSHIP = "transfer-ownership";
7880
private static final String CMD_SET_SUSPENDED_PACKAGES = "set-suspended-packages";
7981
private static final String CMD_IS_PACKAGE_SUSPENDED = "is-package-suspended";
82+
private static final String CMD_SET_LOCK_TASK_PACKAGES = "set-lock-task-packages";
83+
private static final String CMD_GET_LOCK_TASK_PACKAGES = "get-lock-task-packages";
84+
private static final String CMD_IS_LOCK_TASK_PERMITTED = "is-lock-task-permitted";
85+
private static final String CMD_SET_LOCK_TASK_FEATURES = "set-lock-task-features";
86+
private static final String CMD_GET_LOCK_TASK_FEATURES = "get-lock-task-features";
8087

8188
private static final String ARG_FLAGS = "--flags";
8289

@@ -192,6 +199,21 @@ public void run() {
192199
case CMD_IS_PACKAGE_SUSPENDED:
193200
execute(() -> isPackageSuspended());
194201
break;
202+
case CMD_SET_LOCK_TASK_PACKAGES:
203+
execute(() -> setLockTaskPackages());
204+
break;
205+
case CMD_GET_LOCK_TASK_PACKAGES:
206+
execute(() -> getLockTaskPackages());
207+
break;
208+
case CMD_IS_LOCK_TASK_PERMITTED:
209+
execute(() -> isLockTaskPermitted());
210+
break;
211+
case CMD_SET_LOCK_TASK_FEATURES:
212+
execute(() -> setLockTaskFeatures());
213+
break;
214+
case CMD_GET_LOCK_TASK_FEATURES:
215+
execute(() -> getLockTaskFeatures());
216+
break;
195217
default:
196218
mWriter.printf("Invalid command: %s\n\n", cmd);
197219
showUsage();
@@ -260,6 +282,14 @@ private void showUsage() {
260282
+ "packages\n", CMD_SET_SUSPENDED_PACKAGES);
261283
mWriter.printf("\t%s <PKG1> [PKG2] [PKGN] - checks if the given packages are suspended\n",
262284
CMD_IS_PACKAGE_SUSPENDED);
285+
mWriter.printf("\t%s <PKG1> [PKG2] [PGKN] - set the packages allowed to have tasks locked"
286+
+ "\n", CMD_SET_LOCK_TASK_PACKAGES);
287+
mWriter.printf("\t%s - get the packages allowed to have tasks locked\n",
288+
CMD_GET_LOCK_TASK_PACKAGES);
289+
mWriter.printf("\t%s <PKG1> [PKG2] [PKGN] - checks if the given packages are allowed to "
290+
+ "have tasks locked\n", CMD_IS_LOCK_TASK_PERMITTED);
291+
mWriter.printf("\t%s <FLAGS> - set the lock task features\n", CMD_SET_LOCK_TASK_FEATURES);
292+
mWriter.printf("\t%s - get the lock task features\n", CMD_GET_LOCK_TASK_FEATURES);
263293
}
264294

265295
private void createUser() {
@@ -532,7 +562,7 @@ private static String suspendedToString(boolean suspended) {
532562

533563
private void setPackagesSuspended() {
534564
boolean suspended = Boolean.parseBoolean(mArgs[1]);
535-
String[] packageNames = getArrayFromArgs(2);
565+
String[] packageNames = getArrayFromArgs(/* index= */ 2);
536566

537567
String printableNames = Arrays.toString(packageNames);
538568
String printableStatus = suspendedToString(suspended);
@@ -556,6 +586,56 @@ private void isPackageSuspended() {
556586
});
557587
}
558588

589+
private void setLockTaskPackages() {
590+
String[] packages = getArrayFromArgs(/* index= */ 1);
591+
592+
String printableNames = Arrays.toString(packages);
593+
594+
Log.i(TAG, "setLockTaskPackages(): " + printableNames);
595+
596+
mDevicePolicyManagerGateway.setLockTaskPackages(packages,
597+
(v) -> onSuccess("Set lock tasks packages to %s", printableNames),
598+
(e) -> onError(e, "Error settings lock task packages to %s", printableNames));
599+
}
600+
601+
private void getLockTaskPackages() {
602+
String[] packages = mDevicePolicyManagerGateway.getLockTaskPackages();
603+
if (packages.length == 0) {
604+
mWriter.println("no lock task packages");
605+
return;
606+
}
607+
mWriter.println(Arrays.toString(packages));
608+
}
609+
610+
private void setLockTaskFeatures() {
611+
int flags = getIntArg(/* index= */ 1);
612+
613+
String features = Util.lockTaskFeaturesToString(flags);
614+
Log.i(TAG, "setLockTaskFeatures(" + flags + "): setting to " + features);
615+
616+
mDevicePolicyManagerGateway.setLockTaskFeatures(flags,
617+
(v) -> onSuccess("Set lock tasks features to %s", features),
618+
(e) -> onError(e, "Error settings lock task features to %s", features));
619+
}
620+
621+
private void getLockTaskFeatures() {
622+
int flags = mDevicePolicyManagerGateway.getLockTaskFeatures();
623+
String features= Util.lockTaskFeaturesToString(flags);
624+
625+
mWriter.printf("%s (%d)\n", features, flags);
626+
}
627+
628+
private static String permittedToString(boolean permitted) {
629+
return permitted ? "PERMITTED" : "NOT PERMITTED";
630+
}
631+
632+
private void isLockTaskPermitted() {
633+
getListFromAllArgs().forEach((packageName) -> {
634+
boolean permitted = mDevicePolicyManagerGateway.isLockTaskPermitted(packageName);
635+
mWriter.printf("%s: %s\n", packageName, permittedToString(permitted));
636+
});
637+
}
638+
559639
private void execute(@NonNull Runnable r) {
560640
try {
561641
r.run();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import android.widget.ImageView;
3333
import android.widget.TextView;
3434

35+
import com.afwsamples.testdpc.DevicePolicyManagerGateway;
36+
import com.afwsamples.testdpc.DevicePolicyManagerGatewayImpl;
3537
import com.afwsamples.testdpc.R;
3638

3739
import java.util.ArrayList;
@@ -49,13 +51,14 @@ public abstract class ToggleComponentsArrayAdapter extends ArrayAdapter<ResolveI
4951

5052
protected PackageManager mPackageManager;
5153
protected DevicePolicyManager mDevicePolicyManager;
54+
protected DevicePolicyManagerGateway mDevicePolicyManagerGateway;
5255
protected List<Boolean> mIsComponentCheckedList;
5356

5457
public ToggleComponentsArrayAdapter(Context context, int resource, List<ResolveInfo> objects) {
5558
super(context, resource, objects);
5659
mPackageManager = context.getPackageManager();
57-
mDevicePolicyManager = (DevicePolicyManager) context.getSystemService(
58-
Context.DEVICE_POLICY_SERVICE);
60+
mDevicePolicyManagerGateway = new DevicePolicyManagerGatewayImpl(context);
61+
mDevicePolicyManager = mDevicePolicyManagerGateway.getDevicePolicyManager();
5962
// Init mIsComponentCheckedList
6063
mIsComponentCheckedList = new ArrayList<>(Arrays.asList(new Boolean[objects.size()]));
6164
Collections.fill(mIsComponentCheckedList, Boolean.FALSE);

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.io.FileNotFoundException;
4545
import java.io.IOException;
4646
import java.io.InputStream;
47+
import java.lang.reflect.Field;
48+
import java.lang.reflect.Modifier;
4749
import java.util.Collections;
4850
import java.util.List;
4951

@@ -247,4 +249,51 @@ public static boolean isRunningOnTvDevice(Context context) {
247249
public static boolean isRunningOnAutomotiveDevice(Context context) {
248250
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
249251
}
252+
253+
public static String lockTaskFeaturesToString(int flags) {
254+
return flagsToString(DevicePolicyManager.class, "LOCK_TASK_FEATURE_", flags);
255+
}
256+
257+
public static void onSuccessLog(String tag, String template, Object... args) {
258+
Log.d(tag, String.format(template, args) + " succeeded");
259+
}
260+
261+
public static void onErrorLog(String tag, Exception e, String template, Object... args) {
262+
Log.d(tag, String.format(template, args) + " failed", e);
263+
}
264+
265+
// Copied from DebugUtils
266+
public static String flagsToString(Class<?> clazz, String prefix, int flags) {
267+
final StringBuilder res = new StringBuilder();
268+
boolean flagsWasZero = flags == 0;
269+
270+
for (Field field : clazz.getDeclaredFields()) {
271+
final int modifiers = field.getModifiers();
272+
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
273+
&& field.getType().equals(int.class) && field.getName().startsWith(prefix)) {
274+
try {
275+
final int value = field.getInt(null);
276+
if (value == 0 && flagsWasZero) {
277+
return constNameWithoutPrefix(prefix, field);
278+
}
279+
if (value != 0 && (flags & value) == value) {
280+
flags &= ~value;
281+
res.append(constNameWithoutPrefix(prefix, field)).append('|');
282+
}
283+
} catch (IllegalAccessException ignored) {
284+
}
285+
}
286+
}
287+
if (flags != 0 || res.length() == 0) {
288+
res.append(Integer.toHexString(flags));
289+
} else {
290+
res.deleteCharAt(res.length() - 1);
291+
}
292+
return res.toString();
293+
}
294+
295+
// Copied from DebugUtils
296+
private static String constNameWithoutPrefix(String prefix, Field field) {
297+
return field.getName().substring(prefix.length());
298+
}
250299
}

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -914,20 +914,10 @@ public boolean onPreferenceClick(Preference preference) {
914914
switch (key) {
915915
case MANAGE_LOCK_TASK_LIST_KEY:
916916
showManageLockTaskListPrompt(R.string.lock_task_title,
917-
new ManageLockTaskListCallback() {
918-
@Override
919-
public void onPositiveButtonClicked(String[] lockTaskArray) {
920-
try {
921-
mDevicePolicyManager.setLockTaskPackages(
922-
DeviceAdminReceiver.getComponentName(getActivity()),
923-
lockTaskArray);
924-
} catch (SecurityException e) {
925-
Log.d(TAG, "Exception when setting lock task packages", e);
926-
showToast(R.string.lock_task_unavailable);
927-
}
928-
}
929-
}
930-
);
917+
(packages) -> mDevicePolicyManagerGateway.setLockTaskPackages(packages,
918+
(v) -> onSuccessLog("setLockTaskPackages()"),
919+
(e) -> onErrorShowToast("setLockTaskPackages()", e,
920+
R.string.lock_task_unavailable)));
931921
return true;
932922
case CHECK_LOCK_TASK_PERMITTED_KEY:
933923
showCheckLockTaskPermittedPrompt();
@@ -1775,7 +1765,7 @@ private void showCheckLockTaskPermittedPrompt() {
17751765
@Override
17761766
public void onClick(DialogInterface dialog, int which) {
17771767
String packageName = input.getText().toString();
1778-
boolean isLockTaskPermitted = mDevicePolicyManager
1768+
boolean isLockTaskPermitted = mDevicePolicyManagerGateway
17791769
.isLockTaskPermitted(packageName);
17801770
showToast(isLockTaskPermitted
17811771
? R.string.check_lock_task_permitted_result_permitted
@@ -3713,7 +3703,6 @@ private void relaunchInLockTaskMode() {
37133703
if (!activityManager.isInLockTaskMode()){
37143704
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
37153705
}
3716-
37173706
final ActivityOptions options = ActivityOptions.makeBasic();
37183707
options.setLockTaskEnabled(true);
37193708

@@ -4044,8 +4033,8 @@ private int validateDeviceOwnerBeforeQ() {
40444033
return NO_CUSTOM_CONSTRIANT;
40454034
}
40464035

4047-
abstract static class ManageLockTaskListCallback {
4048-
public abstract void onPositiveButtonClicked(String[] lockTaskArray);
4036+
interface ManageLockTaskListCallback {
4037+
void onPositiveButtonClicked(String[] lockTaskArray);
40494038
}
40504039

40514040
@RequiresApi(Util.R_VERSION_CODE)

0 commit comments

Comments
 (0)