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

Commit eb85d520 authored by Max Loh's avatar Max Loh
Browse files

BICS: Add InstallEventType as a type of extras and send it during...

BICS: Add InstallEventType as a type of extras and send it during notifyAllCallbacks based on whether it was install or uninstall
Bug: 375000910
Flag: NONE (API has no consumers yet)
Test: Unit test

Change-Id: I8b02fdcdff6d68aff556ca0eda32e0b4b017ce58
parent 5c32317a
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ public class BackgroundInstallControlCallbackHelper {

    @VisibleForTesting static final String FLAGGED_PACKAGE_NAME_KEY = "packageName";
    @VisibleForTesting static final String FLAGGED_USER_ID_KEY = "userId";
    @VisibleForTesting static final String INSTALL_EVENT_TYPE_KEY = "installEventType";
    private static final String TAG = "BackgroundInstallControlCallbackHelper";

    private final Handler mHandler;
@@ -74,10 +75,14 @@ public class BackgroundInstallControlCallbackHelper {
     * Invokes all registered callbacks Callbacks are processed through user provided-threads and
     * parameters are passed in via {@link BackgroundInstallControlManager} InstallEvent
     */
    public void notifyAllCallbacks(int userId, String packageName) {
    public void notifyAllCallbacks(
            int userId,
            String packageName,
            @BackgroundInstallControlService.InstallEventType int installEventType) {
        Bundle extras = new Bundle();
        extras.putCharSequence(FLAGGED_PACKAGE_NAME_KEY, packageName);
        extras.putInt(FLAGGED_USER_ID_KEY, userId);
        extras.putInt(INSTALL_EVENT_TYPE_KEY, installEventType);
        synchronized (mCallbacks) {
            mHandler.post(
                    () ->
+18 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.pm;
import static android.Manifest.permission.GET_BACKGROUND_INSTALLED_PACKAGES;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.app.Flags;
@@ -64,6 +65,8 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -76,11 +79,23 @@ import java.util.TreeSet;
 * @hide
 */
public class BackgroundInstallControlService extends SystemService {
    public static final int INSTALL_EVENT_TYPE_UNKNOWN = 0;
    public static final int INSTALL_EVENT_TYPE_INSTALL = 1;
    public static final int INSTALL_EVENT_TYPE_UNINSTALL = 2;

    @IntDef(
            value = {
                INSTALL_EVENT_TYPE_UNKNOWN,
                INSTALL_EVENT_TYPE_INSTALL,
                INSTALL_EVENT_TYPE_UNINSTALL,
            })
    @Retention(RetentionPolicy.SOURCE)
    public @interface InstallEventType {}

    private static final String TAG = "BackgroundInstallControlService";

    private static final String DISK_FILE_NAME = "states";
    private static final String DISK_DIR_NAME = "bic";

    private static final String ENFORCE_PERMISSION_ERROR_MSG =
            "User is not permitted to call service: ";

@@ -313,7 +328,7 @@ public class BackgroundInstallControlService extends SystemService {

        initBackgroundInstalledPackages();
        mBackgroundInstalledPackages.add(userId, packageName);
        mCallbackHelper.notifyAllCallbacks(userId, packageName);
        mCallbackHelper.notifyAllCallbacks(userId, packageName, INSTALL_EVENT_TYPE_INSTALL);
        writeBackgroundInstalledPackagesToDisk();
    }

@@ -391,6 +406,7 @@ public class BackgroundInstallControlService extends SystemService {
        initBackgroundInstalledPackages();
        mBackgroundInstalledPackages.remove(userId, packageName);
        writeBackgroundInstalledPackagesToDisk();
        mCallbackHelper.notifyAllCallbacks(userId, packageName, INSTALL_EVENT_TYPE_UNINSTALL);
    }

    void handleUsageEvent(UsageEvents.Event event, int userId) {
+8 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.pm;

import static com.android.server.pm.BackgroundInstallControlCallbackHelper.FLAGGED_PACKAGE_NAME_KEY;
import static com.android.server.pm.BackgroundInstallControlCallbackHelper.FLAGGED_USER_ID_KEY;
import static com.android.server.pm.BackgroundInstallControlCallbackHelper.INSTALL_EVENT_TYPE_KEY;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.after;
@@ -84,12 +85,18 @@ public class BackgroundInstallControlCallbackHelperTest {
        int testUserId = 1;
        mCallbackHelper.registerBackgroundInstallCallback(mCallback);

        mCallbackHelper.notifyAllCallbacks(testUserId, testPackageName);
        mCallbackHelper.notifyAllCallbacks(
                testUserId,
                testPackageName,
                BackgroundInstallControlService.INSTALL_EVENT_TYPE_INSTALL);

        ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
        verify(mCallback, after(1000).times(1)).sendResult(bundleCaptor.capture());
        Bundle receivedBundle = bundleCaptor.getValue();
        assertEquals(testPackageName, receivedBundle.getString(FLAGGED_PACKAGE_NAME_KEY));
        assertEquals(testUserId, receivedBundle.getInt(FLAGGED_USER_ID_KEY));
        assertEquals(
                BackgroundInstallControlService.INSTALL_EVENT_TYPE_INSTALL,
                receivedBundle.getInt(INSTALL_EVENT_TYPE_KEY));
    }
}