Add AM#forceStopPackageAsUserEvenWhenStopping

This is needed in scenarios that clients want to force stop the
application package even when the user is in a stopping state but not
completely stopped yet.

Bug: 276961198
Test: atest FrameworksServicesTests:ActivityManagerServiceTest
Test: perform passenger logout and login manual on the MD emulator
Change-Id: Ie08c87d717e5207ac4a0d84b28ecff65a75c882c
This commit is contained in:
Xiang Wang
2023-04-19 16:32:17 -07:00
parent be2d9c4580
commit 1412974671
3 changed files with 27 additions and 2 deletions

View File

@@ -4431,6 +4431,21 @@ public class ActivityManager {
forceStopPackageAsUser(packageName, mContext.getUserId());
}
/**
* Similar to {@link #forceStopPackageAsUser(String, int)} but will also stop the package even
* when the user is in the stopping state.
*
* @hide
*/
@RequiresPermission(Manifest.permission.FORCE_STOP_PACKAGES)
public void forceStopPackageAsUserEvenWhenStopping(String packageName, @UserIdInt int userId) {
try {
getService().forceStopPackageEvenWhenStopping(packageName, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Sets the current locales of the device. Calling app must have the permission
* {@code android.permission.CHANGE_CONFIGURATION} and

View File

@@ -299,6 +299,7 @@ interface IActivityManager {
boolean registerForegroundServiceObserver(in IForegroundServiceObserver callback);
@UnsupportedAppUsage
void forceStopPackage(in String packageName, int userId);
void forceStopPackageEvenWhenStopping(in String packageName, int userId);
boolean killPids(in int[] pids, in String reason, boolean secure);
@UnsupportedAppUsage
List<ActivityManager.RunningServiceInfo> getServices(int maxNum, int flags);

View File

@@ -3740,6 +3740,15 @@ public class ActivityManagerService extends IActivityManager.Stub
@Override
public void forceStopPackage(final String packageName, int userId) {
forceStopPackage(packageName, userId, /*flags=*/ 0);
}
@Override
public void forceStopPackageEvenWhenStopping(final String packageName, int userId) {
forceStopPackage(packageName, userId, ActivityManager.FLAG_OR_STOPPED);
}
private void forceStopPackage(final String packageName, int userId, int userRunningFlags) {
if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
!= PackageManager.PERMISSION_GRANTED) {
String msg = "Permission Denial: forceStopPackage() from pid="
@@ -3755,7 +3764,7 @@ public class ActivityManagerService extends IActivityManager.Stub
final long callingId = Binder.clearCallingIdentity();
try {
IPackageManager pm = AppGlobals.getPackageManager();
synchronized(this) {
synchronized (this) {
int[] users = userId == UserHandle.USER_ALL
? mUserController.getUsers() : new int[] { userId };
for (int user : users) {
@@ -3783,7 +3792,7 @@ public class ActivityManagerService extends IActivityManager.Stub
Slog.w(TAG, "Failed trying to unstop package "
+ packageName + ": " + e);
}
if (mUserController.isUserRunning(user, 0)) {
if (mUserController.isUserRunning(user, userRunningFlags)) {
forceStopPackageLocked(packageName, pkgUid, "from pid " + callingPid);
finishForceStopPackageLocked(packageName, pkgUid);
}