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

Commit 9c44a14a authored by Ruslan Tkhakokhov's avatar Ruslan Tkhakokhov
Browse files

Pass operation type to BackupAgent

Pass @OperationType in BackupAgent#onCreate and use it to instantiate
BackupRestoreEventLogger.

Bug: 255376040
Test: Manual: 1. Log operationType to logcat in BackupAgent#onCreate
              2. adb shell bmgr backupnow android; verify
		 operationType=0 via logcat
	      3. adb shell bmgr restore 1 android; verify
		 operationType=1 via logcat
Change-Id: I4b2e337209f31aade91baa6900abced35591a0af
parent 0828847a
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import android.app.assist.AssistContent;
import android.app.assist.AssistStructure;
import android.app.backup.BackupAgent;
import android.app.backup.BackupAnnotations.BackupDestination;
import android.app.backup.BackupAnnotations.OperationType;
import android.app.servertransaction.ActivityLifecycleItem;
import android.app.servertransaction.ActivityLifecycleItem.LifecycleState;
import android.app.servertransaction.ActivityRelaunchItem;
@@ -4403,7 +4404,8 @@ public final class ActivityThread extends ClientTransactionHandler
                    context.setOuterContext(agent);
                    agent.attach(context);

                    agent.onCreate(UserHandle.of(data.userId), data.backupDestination);
                    agent.onCreate(UserHandle.of(data.userId), data.backupDestination,
                            getOperationTypeFromBackupMode(data.backupMode));
                    binder = agent.onBind();
                    backupAgents.put(packageName, agent);
                } catch (Exception e) {
@@ -4431,6 +4433,22 @@ public final class ActivityThread extends ClientTransactionHandler
        }
    }

    @OperationType
    private static int getOperationTypeFromBackupMode(int backupMode) {
        switch (backupMode) {
            case ApplicationThreadConstants.BACKUP_MODE_RESTORE:
            case ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL:
                return OperationType.RESTORE;
            case ApplicationThreadConstants.BACKUP_MODE_FULL:
            case ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL:
                return OperationType.BACKUP;
            default:
                Slog.w(TAG, "Invalid backup mode when initialising BackupAgent: "
                        + backupMode);
                return OperationType.UNKNOWN;
        }
    }

    private String getBackupAgentName(CreateBackupAgentData data) {
        String agentName = data.appInfo.backupAgentName;
        // full backup operation but no app-supplied agent?  use the default implementation
+22 −8
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.Nullable;
import android.app.IBackupAgent;
import android.app.QueuedWork;
import android.app.backup.BackupAnnotations.BackupDestination;
import android.app.backup.BackupAnnotations.OperationType;
import android.app.backup.FullBackup.BackupScheme.PathWithRequiredFlags;
import android.content.Context;
import android.content.ContextWrapper;
@@ -265,27 +266,40 @@ public abstract class BackupAgent extends ContextWrapper {
    }

    /**
     * Provided as a convenience for agent implementations that need an opportunity
     * to do one-time initialization before the actual backup or restore operation
     * is begun with information about the calling user.
     * <p>
     *
     * @hide
     */
    public void onCreate(UserHandle user) {
        onCreate(user, DEFAULT_BACKUP_DESTINATION);
        onCreate();
    }

    /**
     * Provided as a convenience for agent implementations that need an opportunity
     * to do one-time initialization before the actual backup or restore operation
     * is begun with information about the calling user.
     * <p>
     * @deprecated Use {@link BackupAgent#onCreate(UserHandle, int, int)} instead.
     *
     * @hide
     */
    @Deprecated
    public void onCreate(UserHandle user, @BackupDestination int backupDestination) {
        // TODO: Instantiate with the correct type using a parameter.
        mLogger = new BackupRestoreEventLogger(BackupRestoreEventLogger.OperationType.BACKUP);
        onCreate();
        mUser = user;
        mBackupDestination = backupDestination;

        onCreate(user);
    }

    /**
    * @hide
    */
    public void onCreate(UserHandle user, @BackupDestination int backupDestination,
            @OperationType int operationType) {
        mUser = user;
        mBackupDestination = backupDestination;
        mLogger = new BackupRestoreEventLogger(operationType);

        onCreate(user, backupDestination);
    }

    /**
+2 −0
Original line number Diff line number Diff line
@@ -30,10 +30,12 @@ public class BackupAnnotations {
    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            OperationType.UNKNOWN,
            OperationType.BACKUP,
            OperationType.RESTORE,
    })
    public @interface OperationType {
        int UNKNOWN = -1;
        int BACKUP = 0;
        int RESTORE = 1;
    }
+1 −16
Original line number Diff line number Diff line
@@ -16,13 +16,13 @@

package android.app.backup;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.app.backup.BackupAnnotations.OperationType;
import android.util.Slog;

import java.lang.annotation.Retention;
@@ -55,21 +55,6 @@ public class BackupRestoreEventLogger {
     */
    public static final int DATA_TYPES_ALLOWED = 15;

    /**
     * Operation types for which this logger can be used.
     *
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            OperationType.BACKUP,
            OperationType.RESTORE
    })
    @interface OperationType {
        int BACKUP = 1;
        int RESTORE = 2;
    }

    /**
     * Denotes that the annotated element identifies a data type as required by the logging methods
     * of {@code BackupRestoreEventLogger}
+7 −5
Original line number Diff line number Diff line
@@ -85,24 +85,26 @@ public class BackupAgentTest {
    @Test
    public void getBackupRestoreEventLogger_afterOnCreateForBackup_initializedForBackup() {
        BackupAgent agent = new TestFullBackupAgent();
        agent.onCreate(USER_HANDLE, OperationType.BACKUP); // TODO: pass in new operation type
        agent.onCreate(USER_HANDLE, BackupDestination.CLOUD, OperationType.BACKUP);

        assertThat(agent.getBackupRestoreEventLogger().getOperationType()).isEqualTo(1);
        assertThat(agent.getBackupRestoreEventLogger().getOperationType()).isEqualTo(
                OperationType.BACKUP);
    }

    @Test
    public void getBackupRestoreEventLogger_afterOnCreateForRestore_initializedForRestore() {
        BackupAgent agent = new TestFullBackupAgent();
        agent.onCreate(USER_HANDLE, OperationType.BACKUP); // TODO: pass in new operation type
        agent.onCreate(USER_HANDLE, BackupDestination.CLOUD, OperationType.RESTORE);

        assertThat(agent.getBackupRestoreEventLogger().getOperationType()).isEqualTo(1);
        assertThat(agent.getBackupRestoreEventLogger().getOperationType()).isEqualTo(
                OperationType.RESTORE);
    }

    @Test
    public void getBackupRestoreEventLogger_afterBackup_containsLogsLoggedByAgent()
            throws Exception {
        BackupAgent agent = new TestFullBackupAgent();
        agent.onCreate(USER_HANDLE, OperationType.BACKUP); // TODO: pass in new operation type
        agent.onCreate(USER_HANDLE, BackupDestination.CLOUD, OperationType.BACKUP);

        // TestFullBackupAgent logs DATA_TYPE_BACKED_UP when onFullBackup is called.
        agent.onFullBackup(new FullBackupDataOutput(/* quota = */ 0));
Loading