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

Commit b188863f authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Move more transport operations to Trampoline

From BMS. Namely:
* selectBackupTransport()
* selectBackupTransportAsync()
* getConfigurationIntent()
* getDestinationString()
* getDataManagementIntent()
* getDataManagementLabel()

Test: atest BackupManagerServiceTest TrampolineRoboTest TrampolineTest
Bug: 135661048
Change-Id: I9dbc0c4dfa73bd9738ae658ff24f986000ffc54b
parent dd1fbf2b
Loading
Loading
Loading
Loading
+0 −93
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
import android.app.backup.IFullBackupRestoreObserver;
import android.app.backup.IRestoreSession;
import android.app.backup.ISelectBackupTransportCallback;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
@@ -152,54 +151,6 @@ public class BackupManagerService {
        }
    }

    /**
     * Selects transport {@code transportName} and returns the previously selected transport.
     *
     * @deprecated Use {@link #selectBackupTransportAsync(ComponentName,
     *     ISelectBackupTransportCallback)} instead.
     */
    @Deprecated
    @Nullable
    public String selectBackupTransport(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "selectBackupTransport()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.selectBackupTransport(transportName);
    }

    /**
     * Selects transport {@code transportComponent} asynchronously and notifies {@code listener}
     * with the result upon completion.
     */
    public void selectBackupTransportAsync(
            @UserIdInt int userId,
            ComponentName transportComponent,
            ISelectBackupTransportCallback listener) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "selectBackupTransportAsync()");

        if (userBackupManagerService != null) {
            userBackupManagerService.selectBackupTransportAsync(transportComponent, listener);
        }
    }

    /**
     * Supply the configuration intent for the given transport. If the name is not one of the
     * available transports, or if the transport does not supply any configuration UI, the method
     * returns {@code null}.
     */
    @Nullable
    public Intent getConfigurationIntent(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getConfigurationIntent()");

        return userBackupManagerService == null
            ? null
            : userBackupManagerService.getConfigurationIntent(transportName);
    }

    /**
     * Sets the ancestral work profile for the calling user.
     *
@@ -251,50 +202,6 @@ public class BackupManagerService {
        return null;
    }

    /**
     * Supply the current destination string for the given transport. If the name is not one of the
     * registered transports the method will return null.
     *
     * <p>This string is used VERBATIM as the summary text of the relevant Settings item.
     *
     * @param transportName The name of the registered transport.
     * @return The current destination string or null if the transport is not registered.
     */
    @Nullable
    public String getDestinationString(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDestinationString()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDestinationString(transportName);
    }

    /** Supply the manage-data intent for the given transport. */
    @Nullable
    public Intent getDataManagementIntent(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDataManagementIntent()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDataManagementIntent(transportName);
    }

    /**
     * Supply the menu label for affordances that fire the manage-data intent for the given
     * transport.
     */
    @Nullable
    public CharSequence getDataManagementLabel(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDataManagementLabel()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDataManagementLabel(transportName);
    }

    // ---------------------------------------------
    // SETTINGS OPERATIONS
    // ---------------------------------------------
+98 −6
Original line number Diff line number Diff line
@@ -872,7 +872,7 @@ public class Trampoline extends IBackupManager.Stub {
    public String selectBackupTransportForUser(int userId, String transport)
            throws RemoteException {
        return (isUserReadyForBackup(userId))
                ? mService.selectBackupTransport(userId, transport) : null;
                ? selectBackupTransport(userId, transport) : null;
    }

    @Override
@@ -880,11 +880,28 @@ public class Trampoline extends IBackupManager.Stub {
        return selectBackupTransportForUser(binderGetCallingUserId(), transport);
    }

    /**
     * Selects transport {@code transportName} and returns the previously selected transport.
     *
     * @deprecated Use {@link #selectBackupTransportAsync(ComponentName,
     *     ISelectBackupTransportCallback)} instead.
     */
    @Deprecated
    @Nullable
    public String selectBackupTransport(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "selectBackupTransport()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.selectBackupTransport(transportName);
    }

    @Override
    public void selectBackupTransportAsyncForUser(int userId, ComponentName transport,
            ISelectBackupTransportCallback listener) throws RemoteException {
        if (isUserReadyForBackup(userId)) {
            mService.selectBackupTransportAsync(userId, transport, listener);
            selectBackupTransportAsync(userId, transport, listener);
        } else {
            if (listener != null) {
                try {
@@ -896,10 +913,26 @@ public class Trampoline extends IBackupManager.Stub {
        }
    }

    /**
     * Selects transport {@code transportComponent} asynchronously and notifies {@code listener}
     * with the result upon completion.
     */
    public void selectBackupTransportAsync(
            @UserIdInt int userId,
            ComponentName transportComponent,
            ISelectBackupTransportCallback listener) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "selectBackupTransportAsync()");

        if (userBackupManagerService != null) {
            userBackupManagerService.selectBackupTransportAsync(transportComponent, listener);
        }
    }

    @Override
    public Intent getConfigurationIntentForUser(int userId, String transport)
            throws RemoteException {
        return isUserReadyForBackup(userId) ? mService.getConfigurationIntent(userId, transport)
        return isUserReadyForBackup(userId) ? getConfigurationIntent(userId, transport)
                : null;
    }

@@ -909,9 +942,24 @@ public class Trampoline extends IBackupManager.Stub {
        return getConfigurationIntentForUser(binderGetCallingUserId(), transport);
    }

    /**
     * Supply the configuration intent for the given transport. If the name is not one of the
     * available transports, or if the transport does not supply any configuration UI, the method
     * returns {@code null}.
     */
    @Nullable
    public Intent getConfigurationIntent(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getConfigurationIntent()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getConfigurationIntent(transportName);
    }

    @Override
    public String getDestinationStringForUser(int userId, String transport) throws RemoteException {
        return isUserReadyForBackup(userId) ? mService.getDestinationString(userId, transport)
        return isUserReadyForBackup(userId) ? getDestinationString(userId, transport)
                : null;
    }

@@ -920,11 +968,30 @@ public class Trampoline extends IBackupManager.Stub {
        return getDestinationStringForUser(binderGetCallingUserId(), transport);
    }

    /**
     * Supply the current destination string for the given transport. If the name is not one of the
     * registered transports the method will return null.
     *
     * <p>This string is used VERBATIM as the summary text of the relevant Settings item.
     *
     * @param transportName The name of the registered transport.
     * @return The current destination string or null if the transport is not registered.
     */
    @Nullable
    public String getDestinationString(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDestinationString()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDestinationString(transportName);
    }

    @Override
    public Intent getDataManagementIntentForUser(int userId, String transport)
            throws RemoteException {
        return isUserReadyForBackup(userId)
                ? mService.getDataManagementIntent(userId, transport) : null;
                ? getDataManagementIntent(userId, transport) : null;
    }

    @Override
@@ -933,13 +1000,38 @@ public class Trampoline extends IBackupManager.Stub {
        return getDataManagementIntentForUser(binderGetCallingUserId(), transport);
    }

    /** Supply the manage-data intent for the given transport. */
    @Nullable
    public Intent getDataManagementIntent(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDataManagementIntent()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDataManagementIntent(transportName);
    }

    @Override
    public CharSequence getDataManagementLabelForUser(int userId, String transport)
            throws RemoteException {
        return isUserReadyForBackup(userId) ? mService.getDataManagementLabel(userId, transport)
        return isUserReadyForBackup(userId) ? getDataManagementLabel(userId, transport)
                : null;
    }

    /**
     * Supply the menu label for affordances that fire the manage-data intent for the given
     * transport.
     */
    @Nullable
    public CharSequence getDataManagementLabel(@UserIdInt int userId, String transportName) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "getDataManagementLabel()");

        return userBackupManagerService == null
                ? null
                : userBackupManagerService.getDataManagementLabel(transportName);
    }

    @Override
    public IRestoreSession beginRestoreSessionForUser(
            int userId, String packageName, String transportID) throws RemoteException {
+0 −154
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ import android.app.Application;
import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
import android.app.backup.IFullBackupRestoreObserver;
import android.app.backup.ISelectBackupTransportCallback;
import android.content.Context;
import android.content.Intent;
import android.os.ParcelFileDescriptor;
@@ -284,159 +283,6 @@ public class BackupManagerServiceTest {
                        "dataManagementLabel");
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSelectBackupTransport_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.selectBackupTransport(mUserOneId, TEST_TRANSPORT);

        verify(mUserOneService).selectBackupTransport(TEST_TRANSPORT);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSelectBackupTransport_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.selectBackupTransport(mUserTwoId, TEST_TRANSPORT);

        verify(mUserOneService, never()).selectBackupTransport(TEST_TRANSPORT);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSelectTransportAsync_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);
        TransportData transport = backupTransport();
        ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class);

        backupManagerService.selectBackupTransportAsync(
                mUserOneId, transport.getTransportComponent(), callback);

        verify(mUserOneService)
                .selectBackupTransportAsync(transport.getTransportComponent(), callback);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSelectBackupTransportAsync_onUnknownUser_doesNotPropagateCall()
            throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);
        TransportData transport = backupTransport();
        ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class);

        backupManagerService.selectBackupTransportAsync(
                mUserTwoId, transport.getTransportComponent(), callback);

        verify(mUserOneService, never())
                .selectBackupTransportAsync(transport.getTransportComponent(), callback);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testGetConfigurationIntent_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.getConfigurationIntent(mUserOneId, TEST_TRANSPORT);

        verify(mUserOneService).getConfigurationIntent(TEST_TRANSPORT);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testGetConfigurationIntent_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.getConfigurationIntent(mUserTwoId, TEST_TRANSPORT);

        verify(mUserOneService, never()).getConfigurationIntent(TEST_TRANSPORT);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testGetDestinationString_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.getDestinationString(mUserOneId, TEST_TRANSPORT);

        verify(mUserOneService).getDestinationString(TEST_TRANSPORT);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testGetDestinationString_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.getDestinationString(mUserTwoId, TEST_TRANSPORT);

        verify(mUserOneService, never()).getDestinationString(TEST_TRANSPORT);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testGetDataManagementIntent_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.getDataManagementIntent(mUserOneId, TEST_TRANSPORT);

        verify(mUserOneService).getDataManagementIntent(TEST_TRANSPORT);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testGetDataManagementIntent_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.getDataManagementIntent(mUserTwoId, TEST_TRANSPORT);

        verify(mUserOneService, never()).getDataManagementIntent(TEST_TRANSPORT);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testGetDataManagementLabel_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.getDataManagementLabel(mUserOneId, TEST_TRANSPORT);

        verify(mUserOneService).getDataManagementLabel(TEST_TRANSPORT);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testGetDataManagementLabel_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.getDataManagementLabel(mUserTwoId, TEST_TRANSPORT);

        verify(mUserOneService, never()).getDataManagementLabel(TEST_TRANSPORT);
    }

    // ---------------------------------------------
    // Settings tests
    // ---------------------------------------------
+157 −0

File changed.

Preview size limit exceeded, changes collapsed.

+0 −114
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import android.app.backup.ISelectBackupTransportCallback;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.ConditionVariable;
@@ -662,23 +661,6 @@ public class TrampolineTest {
                        "Data Management");
    }

    @Test
    public void selectBackupTransportForUser_forwarded() throws Exception {

        mTrampoline.selectBackupTransportForUser(mUserId, TRANSPORT_NAME);

        verify(mBackupManagerServiceMock).selectBackupTransport(mUserId, TRANSPORT_NAME);
    }

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

        mTrampoline.selectBackupTransport(TRANSPORT_NAME);

        verify(mBackupManagerServiceMock).selectBackupTransport(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void selectBackupTransportAsyncForUser_beforeUserUnlocked_notifiesBackupNotAllowed()
            throws Exception {
@@ -728,102 +710,6 @@ public class TrampolineTest {
        // No crash.
    }

    @Test
    public void selectBackupTransportAsyncForUser_forwarded() throws Exception {

        mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null);

        verify(mBackupManagerServiceMock)
                .selectBackupTransportAsync(mUserId, TRANSPORT_COMPONENT_NAME, null);
    }

    @Test
    public void getConfigurationIntentForUser_forwarded() throws Exception {
        Intent configurationIntentStub = new Intent();
        when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn(
                configurationIntentStub);

        assertEquals(
                configurationIntentStub,
                mTrampoline.getConfigurationIntentForUser(mUserId, TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getConfigurationIntent(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getConfigurationIntent_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;
        Intent configurationIntentStub = new Intent();
        when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn(
                configurationIntentStub);

        assertEquals(configurationIntentStub, mTrampoline.getConfigurationIntent(TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getConfigurationIntent(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getDestinationStringForUser_forwarded() throws Exception {
        when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn(
                DESTINATION_STRING);

        assertEquals(
                DESTINATION_STRING,
                mTrampoline.getDestinationStringForUser(mUserId, TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getDestinationString(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getDestinationString_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;
        when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn(
                DESTINATION_STRING);

        assertEquals(DESTINATION_STRING, mTrampoline.getDestinationString(TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getDestinationString(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getDataManagementIntentForUser_forwarded() throws Exception {
        Intent dataManagementIntent = new Intent();
        when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn(
                dataManagementIntent);

        assertEquals(
                dataManagementIntent,
                mTrampoline.getDataManagementIntentForUser(mUserId, TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getDataManagementIntent(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getDataManagementIntent_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;
        Intent dataManagementIntent = new Intent();
        when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn(
                dataManagementIntent);

        assertEquals(dataManagementIntent, mTrampoline.getDataManagementIntent(TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getDataManagementIntent(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void getDataManagementLabelForUser_forwarded() throws Exception {
        when(mBackupManagerServiceMock.getDataManagementLabel(mUserId, TRANSPORT_NAME)).thenReturn(
                DATA_MANAGEMENT_LABEL);

        assertEquals(
                DATA_MANAGEMENT_LABEL,
                mTrampoline.getDataManagementLabelForUser(mUserId, TRANSPORT_NAME));
        verify(mBackupManagerServiceMock).getDataManagementLabel(mUserId, TRANSPORT_NAME);
    }

    @Test
    public void beginRestoreSessionForUser_forwarded() throws Exception {

        mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME);

        verify(mBackupManagerServiceMock)
                .beginRestoreSession(mUserId, PACKAGE_NAME, TRANSPORT_NAME);
    }

    @Test
    public void getAvailableRestoreTokenForUser_forwarded() {
        when(mBackupManagerServiceMock.getAvailableRestoreToken(mUserId, PACKAGE_NAME))