Skip to content

Commit 44655ee

Browse files
committed
Added transfer-ownership command.
Test: adb shell dumpsys activity service --user 10 com.afwsamples.testdpc transfer-ownership com.android.cts.verifier/.managedprovisioning.DeviceAdminTestReceive Bug: 171350084 Bug: 179100903 Test: adb shell dumpsys activity service --user 10 com.afwsamples.testdpc Change-Id: If06f13113e4cc6cf174790191ea3a5ac0026c3c9 (cherry picked from commit 257a793666382a6348ac856ea298e31f84c5c517)
1 parent 4a96680 commit 44655ee

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.app.admin.DevicePolicyManager;
1919
import android.content.ComponentName;
2020
import android.graphics.Bitmap;
21+
import android.os.PersistableBundle;
2122
import android.os.UserHandle;
2223
import android.os.UserManager;
2324
import androidx.annotation.NonNull;
@@ -218,6 +219,12 @@ void setPasswordQuality(int quality, @NonNull Consumer<Void> onSuccess,
218219
*/
219220
int getPasswordQuality();
220221

222+
/**
223+
* See {@link android.app.admin.DevicePolicyManager#transferOwnership(ComponentName, ComponentName, android.os.PersistableBundle)}.
224+
*/
225+
void transferOwnership(@NonNull ComponentName target, @Nullable PersistableBundle bundle,
226+
@NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
227+
221228
/**
222229
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
223230
* call failed.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.Context;
2222
import android.graphics.Bitmap;
2323
import android.os.Bundle;
24+
import android.os.PersistableBundle;
2425
import android.os.UserHandle;
2526
import android.os.UserManager;
2627
import android.util.Log;
@@ -427,6 +428,19 @@ public int getPasswordQuality() {
427428
return quality;
428429
}
429430

431+
@Override
432+
public void transferOwnership(ComponentName target, PersistableBundle bundle,
433+
Consumer<Void> onSuccess, Consumer<Exception> onError) {
434+
Log.d(TAG, "transferOwnership(" + target + ", " + bundle + ")");
435+
436+
try {
437+
mDevicePolicyManager.transferOwnership(mAdminComponentName, target, bundle);
438+
onSuccess.accept(null);
439+
} catch (Exception e) {
440+
onError.accept(e);
441+
}
442+
}
443+
430444
@Override
431445
public String toString() {
432446
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ final class ShellCommand {
7373
private static final String CMD_CLEAR_PROFILE_OWNER = "clear-profile-owner";
7474
private static final String CMD_SET_PASSWORD_QUALITY = "set-password-quality";
7575
private static final String CMD_GET_PASSWORD_QUALITY = "get-password-quality";
76+
private static final String CMD_TRANSFER_OWNERSHIP = "transfer-ownership";
7677

7778
private static final String ARG_FLAGS = "--flags";
7879

@@ -179,6 +180,9 @@ public void run() {
179180
case CMD_GET_PASSWORD_QUALITY:
180181
execute(() -> getPasswordQuality());
181182
break;
183+
case CMD_TRANSFER_OWNERSHIP:
184+
execute(() -> transferOwnership());
185+
break;
182186
default:
183187
mWriter.printf("Invalid command: %s\n\n", cmd);
184188
showUsage();
@@ -241,6 +245,8 @@ private void showUsage() {
241245
mWriter.printf("\t%s <QUALITY> - set password quality\n",
242246
CMD_SET_PASSWORD_QUALITY);
243247
mWriter.printf("\t%s - get password quality\n", CMD_GET_PASSWORD_QUALITY);
248+
mWriter.printf("\t%s [ADMIN]- transfer ownership to the given admin\n",
249+
CMD_TRANSFER_OWNERSHIP);
244250
}
245251

246252
private void createUser() {
@@ -495,6 +501,18 @@ private void getPasswordQuality() {
495501
mWriter.printf("password quality: %d\n", mDevicePolicyManagerGateway.getPasswordQuality());
496502
}
497503

504+
private void transferOwnership() {
505+
// TODO(b/171350084): check args
506+
String flatTarget = mArgs[1];
507+
ComponentName target = ComponentName.unflattenFromString(flatTarget);
508+
509+
Log.i(TAG, "transferOwnership(" + target + ")");
510+
511+
mDevicePolicyManagerGateway.transferOwnership(target, /* bundle= */ null,
512+
(v) -> onSuccess("Ownership transferred to %s", flatTarget),
513+
(e) -> onError(e, "Error transferring ownership to %s", flatTarget));
514+
}
515+
498516
private void execute(@NonNull Runnable r) {
499517
try {
500518
r.run();

app/src/main/java/com/afwsamples/testdpc/transferownership/PickTransferComponentFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private String performTransfer(ComponentName target) {
107107
try {
108108
PersistableBundle persistableBundle = new PersistableBundle();
109109
persistableBundle.putString("random_key", "random_value");
110+
// TODO: use DevicePolicyManagerGateway instead
110111
mDevicePolicyManager.transferOwnership(source, target, persistableBundle);
111112
return "Success!";
112113
} catch (Exception e) {

0 commit comments

Comments
 (0)