Simplifying callback calls in PackageInstallerSession.
Test: atest PackageManagerShellCommandTest Change-Id: I6414d28ce66a925738615a448ed4fd8670ceec91 Merged-In: I6414d28ce66a925738615a448ed4fd8670ceec91
This commit is contained in:
committed by
Mohammad Samiul Islam
parent
f36f5590ac
commit
21a91cb702
@@ -26,7 +26,6 @@ import android.app.AppOpsManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PackageDeleteObserver;
|
||||
import android.app.PackageInstallObserver;
|
||||
import android.app.admin.DevicePolicyEventLogger;
|
||||
import android.app.admin.DevicePolicyManagerInternal;
|
||||
import android.content.Context;
|
||||
@@ -994,74 +993,57 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements
|
||||
}
|
||||
}
|
||||
|
||||
static class PackageInstallObserverAdapter extends PackageInstallObserver {
|
||||
private final Context mContext;
|
||||
private final IntentSender mTarget;
|
||||
private final int mSessionId;
|
||||
private final boolean mShowNotification;
|
||||
private final int mUserId;
|
||||
|
||||
public PackageInstallObserverAdapter(Context context, IntentSender target, int sessionId,
|
||||
boolean showNotification, int userId) {
|
||||
mContext = context;
|
||||
mTarget = target;
|
||||
mSessionId = sessionId;
|
||||
mShowNotification = showNotification;
|
||||
mUserId = userId;
|
||||
static void sendOnUserActionRequired(Context context, IntentSender target, int sessionId,
|
||||
Intent intent) {
|
||||
final Intent fillIn = new Intent();
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS,
|
||||
PackageInstaller.STATUS_PENDING_USER_ACTION);
|
||||
fillIn.putExtra(Intent.EXTRA_INTENT, intent);
|
||||
try {
|
||||
target.sendIntent(context, 0, fillIn, null, null);
|
||||
} catch (SendIntentException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUserActionRequired(Intent intent) {
|
||||
final Intent fillIn = new Intent();
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, mSessionId);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS,
|
||||
PackageInstaller.STATUS_PENDING_USER_ACTION);
|
||||
fillIn.putExtra(Intent.EXTRA_INTENT, intent);
|
||||
try {
|
||||
mTarget.sendIntent(mContext, 0, fillIn, null, null);
|
||||
} catch (SendIntentException ignored) {
|
||||
static void sendOnPackageInstalled(Context context, IntentSender target, int sessionId,
|
||||
boolean showNotification, int userId, String basePackageName, int returnCode,
|
||||
String msg, Bundle extras) {
|
||||
if (PackageManager.INSTALL_SUCCEEDED == returnCode && showNotification) {
|
||||
boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING);
|
||||
Notification notification = buildSuccessNotification(context,
|
||||
context.getResources()
|
||||
.getString(update ? R.string.package_updated_device_owner :
|
||||
R.string.package_installed_device_owner),
|
||||
basePackageName,
|
||||
userId);
|
||||
if (notification != null) {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(basePackageName,
|
||||
SystemMessage.NOTE_PACKAGE_STATE,
|
||||
notification);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPackageInstalled(String basePackageName, int returnCode, String msg,
|
||||
Bundle extras) {
|
||||
if (PackageManager.INSTALL_SUCCEEDED == returnCode && mShowNotification) {
|
||||
boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING);
|
||||
Notification notification = buildSuccessNotification(mContext,
|
||||
mContext.getResources()
|
||||
.getString(update ? R.string.package_updated_device_owner :
|
||||
R.string.package_installed_device_owner),
|
||||
basePackageName,
|
||||
mUserId);
|
||||
if (notification != null) {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(basePackageName,
|
||||
SystemMessage.NOTE_PACKAGE_STATE,
|
||||
notification);
|
||||
}
|
||||
}
|
||||
final Intent fillIn = new Intent();
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, mSessionId);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS,
|
||||
PackageManager.installStatusToPublicStatus(returnCode));
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE,
|
||||
PackageManager.installStatusToString(returnCode, msg));
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode);
|
||||
if (extras != null) {
|
||||
final String existing = extras.getString(
|
||||
PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE);
|
||||
if (!TextUtils.isEmpty(existing)) {
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing);
|
||||
}
|
||||
}
|
||||
try {
|
||||
mTarget.sendIntent(mContext, 0, fillIn, null, null);
|
||||
} catch (SendIntentException ignored) {
|
||||
final Intent fillIn = new Intent();
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId);
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS,
|
||||
PackageManager.installStatusToPublicStatus(returnCode));
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE,
|
||||
PackageManager.installStatusToString(returnCode, msg));
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode);
|
||||
if (extras != null) {
|
||||
final String existing = extras.getString(
|
||||
PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE);
|
||||
if (!TextUtils.isEmpty(existing)) {
|
||||
fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing);
|
||||
}
|
||||
}
|
||||
try {
|
||||
target.sendIntent(context, 0, fillIn, null, null);
|
||||
} catch (SendIntentException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -79,7 +79,6 @@ import android.os.Message;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.ParcelableException;
|
||||
import android.os.Process;
|
||||
import android.os.RemoteException;
|
||||
import android.os.RevocableFileDescriptor;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.UserHandle;
|
||||
@@ -107,7 +106,6 @@ import com.android.internal.util.IndentingPrintWriter;
|
||||
import com.android.internal.util.Preconditions;
|
||||
import com.android.server.LocalServices;
|
||||
import com.android.server.pm.Installer.InstallerException;
|
||||
import com.android.server.pm.PackageInstallerService.PackageInstallObserverAdapter;
|
||||
import com.android.server.pm.dex.DexManager;
|
||||
import com.android.server.security.VerityUtils;
|
||||
|
||||
@@ -257,7 +255,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
private final ArrayList<FileBridge> mBridges = new ArrayList<>();
|
||||
|
||||
@GuardedBy("mLock")
|
||||
private IPackageInstallObserver2 mRemoteObserver;
|
||||
private IntentSender mRemoteStatusReceiver;
|
||||
|
||||
/** Fields derived from commit parsing */
|
||||
@GuardedBy("mLock")
|
||||
@@ -342,14 +340,14 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
final String packageName = (String) args.arg1;
|
||||
final String message = (String) args.arg2;
|
||||
final Bundle extras = (Bundle) args.arg3;
|
||||
final IPackageInstallObserver2 observer = (IPackageInstallObserver2) args.arg4;
|
||||
final IntentSender statusReceiver = (IntentSender) args.arg4;
|
||||
final int returnCode = args.argi1;
|
||||
args.recycle();
|
||||
|
||||
try {
|
||||
observer.onPackageInstalled(packageName, returnCode, message, extras);
|
||||
} catch (RemoteException ignored) {
|
||||
}
|
||||
PackageInstallerService.sendOnPackageInstalled(mContext,
|
||||
statusReceiver, sessionId,
|
||||
isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId,
|
||||
packageName, returnCode, message, extras);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -959,10 +957,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
assertCallerIsOwnerOrRootLocked();
|
||||
assertPreparedAndNotDestroyedLocked("commit");
|
||||
|
||||
final PackageInstallObserverAdapter adapter = new PackageInstallObserverAdapter(
|
||||
mContext, statusReceiver, sessionId,
|
||||
isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId);
|
||||
mRemoteObserver = adapter.getBinder();
|
||||
mRemoteStatusReceiver = statusReceiver;
|
||||
|
||||
if (forTransfer) {
|
||||
mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, null);
|
||||
@@ -1299,11 +1294,10 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
}
|
||||
}
|
||||
if (!success) {
|
||||
try {
|
||||
mRemoteObserver.onPackageInstalled(
|
||||
null, failure.error, failure.getLocalizedMessage(), null);
|
||||
} catch (RemoteException ignored) {
|
||||
}
|
||||
PackageInstallerService.sendOnPackageInstalled(mContext,
|
||||
mRemoteStatusReceiver, sessionId,
|
||||
isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId, null,
|
||||
failure.error, failure.getLocalizedMessage(), null);
|
||||
return;
|
||||
}
|
||||
mPm.installStage(activeChildSessions);
|
||||
@@ -1347,10 +1341,9 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
final Intent intent = new Intent(PackageInstaller.ACTION_CONFIRM_INSTALL);
|
||||
intent.setPackage(mPm.getPackageInstallerPackageName());
|
||||
intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId);
|
||||
try {
|
||||
mRemoteObserver.onUserActionRequired(intent);
|
||||
} catch (RemoteException ignored) {
|
||||
}
|
||||
|
||||
PackageInstallerService.sendOnUserActionRequired(mContext,
|
||||
mRemoteStatusReceiver, sessionId, intent);
|
||||
|
||||
// Commit was keeping session marked as active until now; release
|
||||
// that extra refcount so session appears idle.
|
||||
@@ -2182,17 +2175,17 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
}
|
||||
|
||||
private void dispatchSessionFinished(int returnCode, String msg, Bundle extras) {
|
||||
final IPackageInstallObserver2 observer;
|
||||
final IntentSender statusReceiver;
|
||||
final String packageName;
|
||||
synchronized (mLock) {
|
||||
mFinalStatus = returnCode;
|
||||
mFinalMessage = msg;
|
||||
|
||||
observer = mRemoteObserver;
|
||||
statusReceiver = mRemoteStatusReceiver;
|
||||
packageName = mPackageName;
|
||||
}
|
||||
|
||||
if (observer != null) {
|
||||
if (statusReceiver != null) {
|
||||
// Execute observer.onPackageInstalled on different tread as we don't want callers
|
||||
// inside the system server have to worry about catching the callbacks while they are
|
||||
// calling into the session
|
||||
@@ -2200,7 +2193,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
|
||||
args.arg1 = packageName;
|
||||
args.arg2 = msg;
|
||||
args.arg3 = extras;
|
||||
args.arg4 = observer;
|
||||
args.arg4 = statusReceiver;
|
||||
args.argi1 = returnCode;
|
||||
|
||||
mHandler.obtainMessage(MSG_ON_PACKAGE_INSTALLED, args).sendToTarget();
|
||||
|
||||
Reference in New Issue
Block a user