From 2b5a601f15e4709eb9e79c7fd831242e3d449531 Mon Sep 17 00:00:00 2001 From: Bookatz Date: Tue, 16 Apr 2019 19:41:28 -0700 Subject: pm set-home-activity waits until completion PackageManagerShellActivity.runSetHomeActivity used to be synchronous, but in Q the underlying calls became asynchronous. This cl: 1. changes the shell command to use RoleManager's addRoleHolderAsUser, instead of calling into PackageManagerService. 2. uses a RemoteCallback to restore the wait-until-complete aspect of the shell command. 3. consequently, the shell command now accepts either a package name or a component, but either way, the component will be determined automatically from the package. (This is an alternative to ag/6989721; see comments there for motivation) Fixes: 128686703 Fixes: 130167856 Test: atest CtsShortcutHostTestCases CtsShortcutManagerTestCases (.ShortcutManagerThrottlingTest#testActivityUnthrottled fails, but that is presumably unrelated) Test: atest android.content.pm.cts.shortcutmanager.ShortcutManagerLauncherApiTest Test: atest android.content.pm.cts.shortcuthost.ShortcutManagerMultiuserTest Change-Id: Ibce6282bf401ca471469448262ea87cfc504cf4f --- .../server/pm/PackageManagerShellCommand.java | 44 +++++++++++++++++----- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java index 6b804df2e068..33dd48a1ac6a 100644 --- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java +++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java @@ -25,6 +25,8 @@ import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATIO import android.accounts.IAccountManager; import android.app.ActivityManager; import android.app.ActivityManagerInternal; +import android.app.role.IRoleManager; +import android.app.role.RoleManager; import android.content.ComponentName; import android.content.Context; import android.content.IIntentReceiver; @@ -75,6 +77,7 @@ import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor.AutoCloseInputStream; import android.os.PersistableBundle; import android.os.Process; +import android.os.RemoteCallback; import android.os.RemoteException; import android.os.ServiceManager; import android.os.ShellCommand; @@ -115,6 +118,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.WeakHashMap; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -2460,19 +2464,37 @@ class PackageManagerShellCommand extends ShellCommand { } } + String pkgName; String component = getNextArg(); - ComponentName componentName = - component != null ? ComponentName.unflattenFromString(component) : null; - - if (componentName == null) { - pw.println("Error: component name not specified or invalid"); - return 1; + if (component.indexOf('/') < 0) { + // No component specified, so assume it's just a package name. + pkgName = component; + } else { + ComponentName componentName = + component != null ? ComponentName.unflattenFromString(component) : null; + if (componentName == null) { + pw.println("Error: invalid component name"); + return 1; + } + pkgName = componentName.getPackageName(); } + + final CompletableFuture future = new CompletableFuture<>(); + final RemoteCallback callback = new RemoteCallback(res -> future.complete(res != null)); try { - mInterface.setHomeActivity(componentName, userId); - pw.println("Success"); - return 0; + IRoleManager roleManager = android.app.role.IRoleManager.Stub.asInterface( + ServiceManager.getServiceOrThrow(Context.ROLE_SERVICE)); + roleManager.addRoleHolderAsUser(RoleManager.ROLE_HOME, pkgName, + 0, userId, callback); + boolean success = future.get(); + if (success) { + pw.println("Success"); + return 0; + } else { + pw.println("Error: Failed to set default home."); + return 1; + } } catch (Exception e) { pw.println(e.toString()); return 1; @@ -3161,6 +3183,10 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(""); pw.println(" set-home-activity [--user USER_ID] TARGET-COMPONENT"); pw.println(" Set the default home activity (aka launcher)."); + pw.println(" TARGET-COMPONENT can be a package name (com.package.my) or a full"); + pw.println(" component (com.package.my/component.name). However, only the package name"); + pw.println(" matters: the actual component used will be determined automatically from"); + pw.println(" the package."); pw.println(""); pw.println(" set-installer PACKAGE INSTALLER"); pw.println(" Set installer package name"); -- cgit v1.2.3-59-g8ed1b