Loading core/java/android/app/ApplicationPackageManager.java +15 −0 Original line number Diff line number Diff line Loading @@ -179,6 +179,10 @@ public class ApplicationPackageManager extends PackageManager { @GuardedBy("mDelegates") private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>(); @NonNull @GuardedBy("mPackageMonitorCallbacks") private final ArraySet<IRemoteCallback> mPackageMonitorCallbacks = new ArraySet<>(); UserManager getUserManager() { if (mUserManager == null) { mUserManager = UserManager.get(mContext); Loading Loading @@ -3926,6 +3930,14 @@ public class ApplicationPackageManager extends PackageManager { Objects.requireNonNull(callback); try { mPM.registerPackageMonitorCallback(callback, userId); synchronized (mPackageMonitorCallbacks) { if (mPackageMonitorCallbacks.contains(callback)) { throw new IllegalStateException( "registerPackageMonitorCallback: callback already registered: " + callback); } mPackageMonitorCallbacks.add(callback); } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } Loading @@ -3936,6 +3948,9 @@ public class ApplicationPackageManager extends PackageManager { Objects.requireNonNull(callback); try { mPM.unregisterPackageMonitorCallback(callback); synchronized (mPackageMonitorCallbacks) { mPackageMonitorCallbacks.remove(callback); } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } Loading core/tests/packagemonitortests/src/com/android/internal/content/PackageMonitorVisibilityTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -20,9 +20,12 @@ import static com.android.compatibility.common.util.ShellUtils.runShellCommand; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.content.Context; import android.content.pm.PackageManager; import android.os.Handler; import android.os.IRemoteCallback; import android.os.Looper; import android.os.UserHandle; Loading @@ -45,6 +48,25 @@ public class PackageMonitorVisibilityTest { TEST_DATA_PATH + "TestVisibilityApp.apk"; private static final String TEAT_APK_PACKAGE_NAME = "com.example.android.testvisibilityapp"; private static final int WAIT_CALLBACK_CALLED_IN_SECONDS = 1; @Test public void testPackageMonitorCallbackMultipleRegisterThrowsException() throws Exception { Context context = InstrumentationRegistry.getInstrumentation().getContext(); final IRemoteCallback callback = new IRemoteCallback.Stub() { @Override public void sendResult(android.os.Bundle bundle) { // do-nothing } }; try { context.getPackageManager().registerPackageMonitorCallback(callback, 0); assertThrows(IllegalStateException.class, () -> context.getPackageManager().registerPackageMonitorCallback(callback, 0)); } finally { context.getPackageManager().unregisterPackageMonitorCallback(callback); } } @Test public void testPackageMonitorPackageVisible() throws Exception { TestVisibilityPackageMonitor testPackageMonitor = new TestVisibilityPackageMonitor(); Loading Loading
core/java/android/app/ApplicationPackageManager.java +15 −0 Original line number Diff line number Diff line Loading @@ -179,6 +179,10 @@ public class ApplicationPackageManager extends PackageManager { @GuardedBy("mDelegates") private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>(); @NonNull @GuardedBy("mPackageMonitorCallbacks") private final ArraySet<IRemoteCallback> mPackageMonitorCallbacks = new ArraySet<>(); UserManager getUserManager() { if (mUserManager == null) { mUserManager = UserManager.get(mContext); Loading Loading @@ -3926,6 +3930,14 @@ public class ApplicationPackageManager extends PackageManager { Objects.requireNonNull(callback); try { mPM.registerPackageMonitorCallback(callback, userId); synchronized (mPackageMonitorCallbacks) { if (mPackageMonitorCallbacks.contains(callback)) { throw new IllegalStateException( "registerPackageMonitorCallback: callback already registered: " + callback); } mPackageMonitorCallbacks.add(callback); } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } Loading @@ -3936,6 +3948,9 @@ public class ApplicationPackageManager extends PackageManager { Objects.requireNonNull(callback); try { mPM.unregisterPackageMonitorCallback(callback); synchronized (mPackageMonitorCallbacks) { mPackageMonitorCallbacks.remove(callback); } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } Loading
core/tests/packagemonitortests/src/com/android/internal/content/PackageMonitorVisibilityTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -20,9 +20,12 @@ import static com.android.compatibility.common.util.ShellUtils.runShellCommand; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.content.Context; import android.content.pm.PackageManager; import android.os.Handler; import android.os.IRemoteCallback; import android.os.Looper; import android.os.UserHandle; Loading @@ -45,6 +48,25 @@ public class PackageMonitorVisibilityTest { TEST_DATA_PATH + "TestVisibilityApp.apk"; private static final String TEAT_APK_PACKAGE_NAME = "com.example.android.testvisibilityapp"; private static final int WAIT_CALLBACK_CALLED_IN_SECONDS = 1; @Test public void testPackageMonitorCallbackMultipleRegisterThrowsException() throws Exception { Context context = InstrumentationRegistry.getInstrumentation().getContext(); final IRemoteCallback callback = new IRemoteCallback.Stub() { @Override public void sendResult(android.os.Bundle bundle) { // do-nothing } }; try { context.getPackageManager().registerPackageMonitorCallback(callback, 0); assertThrows(IllegalStateException.class, () -> context.getPackageManager().registerPackageMonitorCallback(callback, 0)); } finally { context.getPackageManager().unregisterPackageMonitorCallback(callback); } } @Test public void testPackageMonitorPackageVisible() throws Exception { TestVisibilityPackageMonitor testPackageMonitor = new TestVisibilityPackageMonitor(); Loading