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

Commit 0b55cb0d authored by George Chan's avatar George Chan Committed by Xiaozhen Lin
Browse files

Update BTS Service to use push API instead of pull API in BIC

There will follow up CL to separate the handling of install and
uninstall events in BTS before we turn the flag on.

Host test and unit test will be added in a follow-up CL
(Bug: 374120984)

Flag: android.app.background_install_control_callback_api
Bug: 369382811
Test: manually testing
Change-Id: I91a7b45b027e8108888811f5be2b187b620513dd
parent b8e3b939
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -9,3 +9,10 @@ flag {
     is_fixed_read_only: true
     bug: "287507984"
}

flag {
    name: "background_install_control_callback_api"
    namespace: "preload_safety"
    description: "Feature flag to enable the use of push API in background install control service"
    bug: "369382811"
}
 No newline at end of file
+63 −1
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.IRemoteCallback;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
@@ -347,7 +348,8 @@ public class BinaryTransparencyService extends SystemService {
                        + " packages after considering preloads");
            }

            if (CompatChanges.isChangeEnabled(LOG_MBA_INFO)) {
            if (!android.app.Flags.backgroundInstallControlCallbackApi()
                    && CompatChanges.isChangeEnabled(LOG_MBA_INFO)) {
                // lastly measure all newly installed MBAs
                List<IBinaryTransparencyService.AppInfo> allMbaInfo =
                        collectAllSilentInstalledMbaInfo(packagesMeasured);
@@ -1157,6 +1159,49 @@ public class BinaryTransparencyService extends SystemService {
        mBiometricLogger = biometricLogger;
    }

    /**
     * Receive callbacks from BIC to write silently installed apps to log
     *
     * TODO: Add a host test for testing registration and callback of BicCallbackHandler
     *  b/380002484
     */
    static class BicCallbackHandler extends IRemoteCallback.Stub {
        private static final String BIC_CALLBACK_HANDLER_TAG =
                "BTS.BicCallbackHandler";
        private final BinaryTransparencyServiceImpl mServiceImpl;
        static final String FLAGGED_PACKAGE_NAME_KEY = "packageName";

        BicCallbackHandler(BinaryTransparencyServiceImpl impl) {
            mServiceImpl = impl;
        }

        @Override
        public void sendResult(Bundle data) {
            String packageName = data.getString(FLAGGED_PACKAGE_NAME_KEY);
            if (packageName == null) return;
            if (DEBUG) {
                Slog.d(BIC_CALLBACK_HANDLER_TAG, "background install event detected for "
                        + packageName);
            }

            PackageState packageState = LocalServices.getService(PackageManagerInternal.class)
                    .getPackageStateInternal(packageName);
            if (packageState == null) {
                Slog.w(TAG, "Package state is unavailable, ignoring the package "
                        + packageName);
                return;
            }
            if (packageState.isUpdatedSystemApp()) {
                return;
            }
            List<IBinaryTransparencyService.AppInfo> mbaInfo = mServiceImpl.collectAppInfo(
                    packageState, MBA_STATUS_NEW_INSTALL);
            for (IBinaryTransparencyService.AppInfo appInfo : mbaInfo) {
                mServiceImpl.writeAppInfoToLog(appInfo);
            }
        }
    };

    /**
     * Called when the system service should publish a binder service using
     * {@link #publishBinderService(String, IBinder).}
@@ -1534,6 +1579,19 @@ public class BinaryTransparencyService extends SystemService {
        }
    }

    private void registerBicCallback() {
        IBackgroundInstallControlService iBics =
                IBackgroundInstallControlService.Stub.asInterface(
                        ServiceManager.getService(
                                Context.BACKGROUND_INSTALL_CONTROL_SERVICE));
        try {
            iBics.registerBackgroundInstallCallback(
                    new BicCallbackHandler(mServiceImpl));
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to register BackgroundInstallControl callback.");
        }
    }

    private boolean isPackagePreloaded(String packageName) {
        PackageManager pm = mContext.getPackageManager();
        try {
@@ -1596,6 +1654,10 @@ public class BinaryTransparencyService extends SystemService {
    private void registerAllPackageUpdateObservers() {
        registerApkAndNonStagedApexUpdateListener();
        registerStagedApexUpdateObserver();
        if (android.app.Flags.backgroundInstallControlCallbackApi()
                && CompatChanges.isChangeEnabled(LOG_MBA_INFO)) {
            registerBicCallback();
        }
    }

    private String translateContentDigestAlgorithmIdToString(int algorithmId) {