Skip to content

Commit 46586d0

Browse files
committed
Added clear-profile-owner Shell command.
Test: adb shell dumpsys activity service --user 10 com.afwsamples.testdpc clear-profile-owner Bug: 171350084 Fixes: 177564920 Change-Id: I2946cfa044e3608644a67834395386c5b643b07d (cherry picked from commit 9b9ce9de3aa9f59670bddd1cf367a8676da8efc3)
1 parent b6a6a3a commit 46586d0

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ void wipeData(int flags, @NonNull Consumer<Void> onSuccess,
185185
*/
186186
void clearDeviceOwnerApp(@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
187187

188+
/**
189+
* See {@link android.app.admin.DevicePolicyManager#clearProfileOwner(android.content.ComponentName)}.
190+
*/
191+
void clearProfileOwner(@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
192+
188193
/**
189194
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
190195
* call failed.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ public void clearDeviceOwnerApp(Consumer<Void> onSuccess, Consumer<Exception> on
380380
}
381381
}
382382

383+
@Override
384+
public void clearProfileOwner(Consumer<Void> onSuccess, Consumer<Exception> onError) {
385+
Log.d(TAG, "clearProfileOwner()");
386+
387+
try {
388+
mDevicePolicyManager.clearProfileOwner(mAdminComponentName);
389+
onSuccess.accept(null);
390+
} catch (Exception e) {
391+
onError.accept(e);
392+
}
393+
}
394+
383395
@Override
384396
public String toString() {
385397
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ final class ShellCommand {
6969
"get-user-control-disabled-packages";
7070
private static final String CMD_REMOVE_ACTIVE_ADMIN = "remove-active-admin";
7171
private static final String CMD_CLEAR_DEVICE_OWNER = "clear-device-owner";
72+
private static final String CMD_CLEAR_PROFILE_OWNER = "clear-profile-owner";
7273

7374
private static final String ARG_FLAGS = "--flags";
7475

@@ -163,6 +164,9 @@ public void run() {
163164
case CMD_CLEAR_DEVICE_OWNER:
164165
execute(() -> clearDeviceOwner());
165166
break;
167+
case CMD_CLEAR_PROFILE_OWNER:
168+
execute(() -> clearProfileOwner());
169+
break;
166170
default:
167171
mWriter.printf("Invalid command: %s\n\n", cmd);
168172
showUsage();
@@ -208,13 +212,14 @@ private void showUsage() {
208212
mWriter.printf("\t%s [NAME] - set the organization name; use it without a name to reset\n",
209213
CMD_SET_ORGANIZATION_NAME);
210214
mWriter.printf("\t%s - get the organization name\n", CMD_GET_ORGANIZATION_NAME);
211-
mWriter.printf("\t%s [PKG1] [PKG2] [PKGN] - sets the packages that the user cannot force\n"
212-
+ "\tstop or clear data. Use no args to reset it.\n",
215+
mWriter.printf("\t%s [PKG1] [PKG2] [PKGN] - sets the packages that the user cannot\n"
216+
+ "\t\tforce stop or clear data. Use no args to reset it.\n",
213217
CMD_SET_USER_CONTROL_DISABLED_PACKAGES);
214218
mWriter.printf("\t%s - gets the packages that the user cannot force stop or "
215219
+ "clear data\n", CMD_GET_USER_CONTROL_DISABLED_PACKAGES);
216220
mWriter.printf("\t%s - remove itself as an admin\n", CMD_REMOVE_ACTIVE_ADMIN);
217221
mWriter.printf("\t%s - clear itself as device owner \n", CMD_CLEAR_DEVICE_OWNER);
222+
mWriter.printf("\t%s - clear itself as profile owner \n", CMD_CLEAR_PROFILE_OWNER);
218223
}
219224

220225
private void createUser() {
@@ -447,6 +452,15 @@ private void clearDeviceOwner() {
447452
(e) -> onError(e, "Error removing %s as device owner", pkg));
448453
}
449454

455+
private void clearProfileOwner() {
456+
Log.i(TAG, "clearProfileOwner()");
457+
458+
String pkg = mDevicePolicyManagerGateway.getAdmin().getPackageName();
459+
mDevicePolicyManagerGateway.clearProfileOwner(
460+
(v) -> onSuccess("Removed %s as profile owner", pkg),
461+
(e) -> onError(e, "Error removing %s as profile owner", pkg));
462+
}
463+
450464
private void execute(@NonNull Runnable r) {
451465
try {
452466
r.run();

0 commit comments

Comments
 (0)