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

Commit bf7f342b authored by Bernardo Rufino's avatar Bernardo Rufino Committed by Android (Google) Code Review
Browse files

Merge changes I7f839a37,I11d8912b,Ia44b7b83,I47e80728

* changes:
  Move adb backup/restore operations to Trampoline
  Move restore operations to Trampoline
  Move backup operations to Trampoline
  Move settings operations to Trampoline
parents 9dfafd6a f410de02
Loading
Loading
Loading
Loading
+0 −290
Original line number Diff line number Diff line
@@ -20,18 +20,8 @@ import static com.android.internal.util.Preconditions.checkNotNull;

import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.backup.BackupManager;
import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
import android.app.backup.IFullBackupRestoreObserver;
import android.app.backup.IRestoreSession;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.util.SparseArray;

@@ -98,286 +88,6 @@ public class BackupManagerService {
     */
    // TODO (b/118520567): Stop hardcoding system user when we pass in user id as a parameter

    // ---------------------------------------------
    // SETTINGS OPERATIONS
    // ---------------------------------------------

    /** Enable/disable the backup service. This is user-configurable via backup settings. */
    public void setBackupEnabled(@UserIdInt int userId, boolean enable) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setBackupEnabled()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setBackupEnabled(enable);
        }
    }

    /** Enable/disable automatic restore of app data at install time. */
    public void setAutoRestore(@UserIdInt int userId, boolean autoRestore) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setAutoRestore()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setAutoRestore(autoRestore);
        }
    }

    /**
     * Return {@code true} if the backup mechanism is currently enabled, else returns {@code false}.
     */
    public boolean isBackupEnabled(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "isBackupEnabled()");

        return userBackupManagerService != null && userBackupManagerService.isBackupEnabled();
    }

    // ---------------------------------------------
    // BACKUP OPERATIONS
    // ---------------------------------------------

    /** Checks if the given package {@code packageName} is eligible for backup. */
    public boolean isAppEligibleForBackup(@UserIdInt int userId, String packageName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "isAppEligibleForBackup()");

        return userBackupManagerService != null
                && userBackupManagerService.isAppEligibleForBackup(packageName);
    }

    /**
     * Returns from the inputted packages {@code packages}, the ones that are eligible for backup.
     */
    @Nullable
    public String[] filterAppsEligibleForBackup(@UserIdInt int userId, String[] packages) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "filterAppsEligibleForBackup()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.filterAppsEligibleForBackup(packages);
    }

    /**
     * Run a backup pass immediately for any key-value backup applications that have declared that
     * they have pending updates.
     */
    public void backupNow(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "backupNow()");

        if (userBackupManagerService != null) {
            userBackupManagerService.backupNow();
        }
    }

    /**
     * Requests a backup for the inputted {@code packages} with a specified callback {@link
     * IBackupManagerMonitor} for receiving events during the operation.
     */
    public int requestBackup(
            @UserIdInt int userId,
            String[] packages,
            IBackupObserver observer,
            IBackupManagerMonitor monitor,
            int flags) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "requestBackup()");

        return userBackupManagerService == null
                ? BackupManager.ERROR_BACKUP_NOT_ALLOWED
                : userBackupManagerService.requestBackup(packages, observer, monitor, flags);
    }

    /** Cancel all running backup operations. */
    public void cancelBackups(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "cancelBackups()");

        if (userBackupManagerService != null) {
            userBackupManagerService.cancelBackups();
        }
    }

    /**
     * Used by the {@link JobScheduler} to run a full backup when conditions are right. The model we
     * use is to perform one app backup per scheduled job execution, and to reschedule the job with
     * zero latency as long as conditions remain right and we still have work to do.
     *
     * @return Whether ongoing work will continue. The return value here will be passed along as the
     *     return value to the callback {@link JobService#onStartJob(JobParameters)}.
     */
    public boolean beginFullBackup(@UserIdInt int userId, FullBackupJob scheduledJob) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "beginFullBackup()");

        return userBackupManagerService != null
                && userBackupManagerService.beginFullBackup(scheduledJob);
    }

    /**
     * Used by the {@link JobScheduler} to end the current full backup task when conditions are no
     * longer met for running the full backup job.
     */
    public void endFullBackup(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "endFullBackup()");

        if (userBackupManagerService != null) {
            userBackupManagerService.endFullBackup();
        }
    }

    /**
     * Run a full backup pass for the given packages {@code packageNames}. Used by 'adb shell bmgr'.
     */
    public void fullTransportBackup(@UserIdInt int userId, String[] packageNames) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "fullTransportBackup()");

        if (userBackupManagerService != null) {
            userBackupManagerService.fullTransportBackup(packageNames);
        }
    }

    // ---------------------------------------------
    // RESTORE OPERATIONS
    // ---------------------------------------------

    /**
     * Used to run a restore pass for an application that is being installed. This should only be
     * called from the {@link PackageManager}.
     */
    public void restoreAtInstall(@UserIdInt int userId, String packageName, int token) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "restoreAtInstall()");

        if (userBackupManagerService != null) {
            userBackupManagerService.restoreAtInstall(packageName, token);
        }
    }

    /**
     * Begin a restore for the specified package {@code packageName} using the specified transport
     * {@code transportName}.
     */
    @Nullable
    public IRestoreSession beginRestoreSession(
            @UserIdInt int userId, String packageName, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "beginRestoreSession()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.beginRestoreSession(packageName, transportName);
    }

    /**
     * Get the restore-set token for the best-available restore set for this {@code packageName}:
     * the active set if possible, else the ancestral one. Returns zero if none available.
     */
    public long getAvailableRestoreToken(@UserIdInt int userId, String packageName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getAvailableRestoreToken()");

        return userBackupManagerService == null
                ? 0
                : userBackupManagerService.getAvailableRestoreToken(packageName);
    }

    // ---------------------------------------------
    // ADB BACKUP/RESTORE OPERATIONS
    // ---------------------------------------------

    /** Sets the backup password used when running adb backup. */
    public boolean setBackupPassword(String currentPassword, String newPassword) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(
                        UserHandle.USER_SYSTEM, "setBackupPassword()");

        return userBackupManagerService != null
                && userBackupManagerService.setBackupPassword(currentPassword, newPassword);
    }

    /** Returns {@code true} if adb backup was run with a password, else returns {@code false}. */
    public boolean hasBackupPassword() {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(
                        UserHandle.USER_SYSTEM, "hasBackupPassword()");

        return userBackupManagerService != null && userBackupManagerService.hasBackupPassword();
    }

    /**
     * Used by 'adb backup' to run a backup pass for packages {@code packageNames} supplied via the
     * command line, writing the resulting data stream to the supplied {@code fd}. This method is
     * synchronous and does not return to the caller until the backup has been completed. It
     * requires on-screen confirmation by the user.
     */
    public void adbBackup(
            @UserIdInt int userId,
            ParcelFileDescriptor fd,
            boolean includeApks,
            boolean includeObbs,
            boolean includeShared,
            boolean doWidgets,
            boolean doAllApps,
            boolean includeSystem,
            boolean doCompress,
            boolean doKeyValue,
            String[] packageNames) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "adbBackup()");

        if (userBackupManagerService != null) {
            userBackupManagerService.adbBackup(
                    fd,
                    includeApks,
                    includeObbs,
                    includeShared,
                    doWidgets,
                    doAllApps,
                    includeSystem,
                    doCompress,
                    doKeyValue,
                    packageNames);
        }
    }

    /**
     * Used by 'adb restore' to run a restore pass reading from the supplied {@code fd}. This method
     * is synchronous and does not return to the caller until the restore has been completed. It
     * requires on-screen confirmation by the user.
     */
    public void adbRestore(@UserIdInt int userId, ParcelFileDescriptor fd) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "adbRestore()");

        if (userBackupManagerService != null) {
            userBackupManagerService.adbRestore(fd);
        }
    }

    /**
     * Confirm that the previously requested adb backup/restore operation can proceed. This is used
     * to require a user-facing disclosure about the operation.
     */
    public void acknowledgeAdbBackupOrRestore(
            @UserIdInt int userId,
            int token,
            boolean allow,
            String currentPassword,
            String encryptionPassword,
            IFullBackupRestoreObserver observer) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "acknowledgeAdbBackupOrRestore()");

        if (userBackupManagerService != null) {
            userBackupManagerService.acknowledgeAdbBackupOrRestore(
                    token, allow, currentPassword, encryptionPassword, observer);
        }
    }

    // ---------------------------------------------
    //  SERVICE OPERATIONS
    // ---------------------------------------------
+286 −37

File changed.

Preview size limit exceeded, changes collapsed.

+0 −783

File changed.

Preview size limit exceeded, changes collapsed.

+804 −0

File changed.

Preview size limit exceeded, changes collapsed.

+0 −231
Original line number Diff line number Diff line
@@ -481,164 +481,6 @@ public class TrampolineTest {
                TrampolineTestable.sRememberActivatedFiles.get(NON_USER_SYSTEM), true));
    }

    @Test
    public void restoreAtInstallForUser_forwarded() throws Exception {

        mTrampoline.restoreAtInstallForUser(mUserId, PACKAGE_NAME, 123);

        verify(mBackupManagerServiceMock).restoreAtInstall(mUserId, PACKAGE_NAME, 123);
    }

    @Test
    public void restoreAtInstall_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.restoreAtInstall(PACKAGE_NAME, 123);

        verify(mBackupManagerServiceMock).restoreAtInstall(mUserId, PACKAGE_NAME, 123);
    }

    @Test
    public void setBackupEnabledForUser_forwarded() throws Exception {

        mTrampoline.setBackupEnabledForUser(mUserId, true);

        verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
    }

    @Test
    public void setBackupEnabled_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.setBackupEnabled(true);

        verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
    }

    @Test
    public void setAutoRestoreForUser_forwarded() throws Exception {

        mTrampoline.setAutoRestoreForUser(mUserId, true);

        verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
    }

    @Test
    public void setAutoRestore_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.setAutoRestore(true);

        verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
    }

    @Test
    public void isBackupEnabledForUser_forwarded() throws Exception {

        mTrampoline.isBackupEnabledForUser(mUserId);

        verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
    }

    @Test
    public void isBackupEnabled_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.isBackupEnabled();

        verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
    }

    @Test
    public void setBackupPassword_forwarded() throws Exception {
        mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
        verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
    }

    @Test
    public void hasBackupPassword_forwarded() throws Exception {
        mTrampoline.hasBackupPassword();
        verify(mBackupManagerServiceMock).hasBackupPassword();
    }

    @Test
    public void backupNowForUser_forwarded() throws Exception {

        mTrampoline.backupNowForUser(mUserId);

        verify(mBackupManagerServiceMock).backupNow(mUserId);
    }

    @Test
    public void backupNow_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.backupNow();

        verify(mBackupManagerServiceMock).backupNow(mUserId);
    }

    @Test
    public void adbBackup_forwarded() throws Exception {
        mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true,
                true, true, true, true, true, true,
                PACKAGE_NAMES);
        verify(mBackupManagerServiceMock).adbBackup(mUserId, mParcelFileDescriptorMock, true,
                true, true, true, true, true, true, true, PACKAGE_NAMES);
    }

    @Test
    public void fullTransportBackupForUser_forwarded() throws Exception {

        mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES);

        verify(mBackupManagerServiceMock).fullTransportBackup(mUserId, PACKAGE_NAMES);
    }

    @Test
    public void adbRestore_forwarded() throws Exception {
        mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock);
        verify(mBackupManagerServiceMock).adbRestore(mUserId, mParcelFileDescriptorMock);
    }

    @Test
    public void acknowledgeFullBackupOrRestoreForUser_forwarded() throws Exception {

        mTrampoline.acknowledgeFullBackupOrRestoreForUser(
                mUserId,
                123,
                true,
                CURRENT_PASSWORD,
                ENCRYPTION_PASSWORD,
                mFullBackupRestoreObserverMock);

        verify(mBackupManagerServiceMock)
                .acknowledgeAdbBackupOrRestore(
                        mUserId,
                        123,
                        true,
                        CURRENT_PASSWORD,
                        ENCRYPTION_PASSWORD,
                        mFullBackupRestoreObserverMock);
    }

    @Test
    public void acknowledgeFullBackupOrRestore_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD,
                mFullBackupRestoreObserverMock);

        verify(mBackupManagerServiceMock)
                .acknowledgeAdbBackupOrRestore(
                        mUserId,
                        123,
                        true,
                        CURRENT_PASSWORD,
                        ENCRYPTION_PASSWORD,
                        mFullBackupRestoreObserverMock);
    }

    @Test
    public void selectBackupTransportAsyncForUser_beforeUserUnlocked_notifiesBackupNotAllowed()
            throws Exception {
@@ -688,79 +530,6 @@ public class TrampolineTest {
        // No crash.
    }

    @Test
    public void getAvailableRestoreTokenForUser_forwarded() {
        when(mBackupManagerServiceMock.getAvailableRestoreToken(mUserId, PACKAGE_NAME))
                .thenReturn(123L);

        assertEquals(123, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME));
        verify(mBackupManagerServiceMock).getAvailableRestoreToken(mUserId, PACKAGE_NAME);
    }

    @Test
    public void isAppEligibleForBackupForUser_forwarded() {
        when(mBackupManagerServiceMock.isAppEligibleForBackup(mUserId, PACKAGE_NAME))
                .thenReturn(true);

        assertTrue(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME));
        verify(mBackupManagerServiceMock).isAppEligibleForBackup(mUserId, PACKAGE_NAME);
    }

    @Test
    public void requestBackupForUser_forwarded() throws Exception {
        when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456);

        assertEquals(456, mTrampoline.requestBackupForUser(mUserId, PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123));
        verify(mBackupManagerServiceMock).requestBackup(mUserId, PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123);
    }

    @Test
    public void requestBackup_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;
        when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456);

        assertEquals(456, mTrampoline.requestBackup(PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123));
        verify(mBackupManagerServiceMock).requestBackup(mUserId, PACKAGE_NAMES,
                mBackupObserverMock, mBackupManagerMonitorMock, 123);
    }

    @Test
    public void cancelBackupsForUser_forwarded() throws Exception {

        mTrampoline.cancelBackupsForUser(mUserId);

        verify(mBackupManagerServiceMock).cancelBackups(mUserId);
    }

    @Test
    public void cancelBackups_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.cancelBackups();

        verify(mBackupManagerServiceMock).cancelBackups(mUserId);
    }

    @Test
    public void beginFullBackup_forwarded() throws Exception {
        FullBackupJob fullBackupJob = new FullBackupJob();
        when(mBackupManagerServiceMock.beginFullBackup(mUserId, fullBackupJob)).thenReturn(true);

        assertTrue(mTrampoline.beginFullBackup(mUserId, fullBackupJob));
        verify(mBackupManagerServiceMock).beginFullBackup(mUserId, fullBackupJob);
    }

    @Test
    public void endFullBackup_forwarded() {
        mTrampoline.endFullBackup(mUserId);
        verify(mBackupManagerServiceMock).endFullBackup(mUserId);
    }

    @Test
    public void dump_callerDoesNotHavePermission_ignored() {
        when(mContextMock.checkCallingOrSelfPermission(