Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b99b38c authored by Joanne Chung's avatar Joanne Chung Committed by Android (Google) Code Review
Browse files

Merge "Fix PackagesSuspened/Unsuspended should not be called if no access" into main

parents 9f3dc638 1ea4b4fb
Loading
Loading
Loading
Loading
+21 −12
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import java.util.function.BiFunction;
class PackageMonitorCallbackHelper {

    private static final boolean DEBUG = false;
    private static final String TAG = "PackageMonitorCallbackHelper";

    @NonNull
    private final Object mLock = new Object();
@@ -243,25 +244,33 @@ class PackageMonitorCallbackHelper {
                return;
            }
            int registerUid = registerUser.getUid();
            if (allowUids != null && registerUid != Process.SYSTEM_UID
                    && !ArrayUtils.contains(allowUids, registerUid)) {
                if (DEBUG) {
                    Slog.w(TAG, "Skip invoke PackageMonitorCallback for " + intent.getAction()
                            + ", uid " + registerUid);
                }
                return;
            }
            Intent newIntent = intent;
            if (filterExtrasFunction != null) {
                final Bundle extras = intent.getExtras();
                if (extras != null) {
                    final Bundle filteredExtras = filterExtrasFunction.apply(registerUid, extras);
                    if (filteredExtras != null) {
                        intent.replaceExtras(filteredExtras);
                    }
                }
            }
            if (allowUids != null && registerUid != Process.SYSTEM_UID
                    && !ArrayUtils.contains(allowUids, registerUid)) {
                    if (filteredExtras == null) {
                        // caller is unable to access this intent
                        if (DEBUG) {
                    Slog.w("PackageMonitorCallbackHelper",
                            Slog.w(TAG,
                                    "Skip invoke PackageMonitorCallback for " + intent.getAction()
                                    + ", uid " + registerUid);
                                            + " because null filteredExtras");
                        }
                        return;
                    }
            invokeCallback(callback, intent);
                    newIntent = new Intent(newIntent);
                    newIntent.replaceExtras(filteredExtras);
                }
            }
            invokeCallback(callback, newIntent);
        }));
    }

+14 −0
Original line number Diff line number Diff line
@@ -107,6 +107,20 @@ public class PackageMonitorCallbackHelperTest {
        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).never()).sendResult(any());
    }

    @Test
    public void testPackageMonitorCallback_SuspendNoAccessCallbackNotCalled() throws Exception {
        BiFunction<Integer, Bundle, Bundle> filterExtras = (callingUid, intentExtras) -> null;

        IRemoteCallback callback = createMockPackageMonitorCallback();
        mPackageMonitorCallbackHelper.registerPackageMonitorCallback(callback, 0 /* userId */,
                Binder.getCallingUid());
        mPackageMonitorCallbackHelper.notifyPackageMonitor(Intent.ACTION_PACKAGES_SUSPENDED,
                FAKE_PACKAGE_NAME, createFakeBundle(), new int[]{0}, null /* instantUserIds */,
                null /* broadcastAllowList */, mHandler, filterExtras);

        verify(callback, after(WAIT_CALLBACK_CALLED_IN_MS).never()).sendResult(any());
    }

    @Test
    public void testPackageMonitorCallback_SuspendCallbackCalled() throws Exception {
        Bundle result = new Bundle();