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

Commit 6cf1da7d authored by Ruslan Tkhakokhov's avatar Ruslan Tkhakokhov
Browse files

[FSD2D] Adjust backup agent creation for device-to-device migrations

1. Pass @OperationType to backup agent on creation so that it can adjust
   its behavior if needed.
2. For device-to-device migrations, always instantiate FullBackupAgent
   regardless of the app's preferences.

Bug: 160407842
Test: atest FrameworksServicesTests:ActivityManagerServiceTest
Change-Id: I89bf2b074e7a1039b9af70660a3f19a9bb7dab23
parent 1304c4aa
Loading
Loading
Loading
Loading
+25 −8
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import android.annotation.Nullable;
import android.app.assist.AssistContent;
import android.app.assist.AssistStructure;
import android.app.backup.BackupAgent;
import android.app.backup.BackupManager;
import android.app.servertransaction.ActivityLifecycleItem;
import android.app.servertransaction.ActivityLifecycleItem.LifecycleState;
import android.app.servertransaction.ActivityRelaunchItem;
@@ -289,6 +290,8 @@ public final class ActivityThread extends ClientTransactionHandler {

    private final Object mNetworkPolicyLock = new Object();

    private static final String DEFAULT_FULL_BACKUP_AGENT = "android.app.backup.FullBackupAgent";

    /**
     * Denotes the sequence number of the process state change for which the main thread needs
     * to block until the network rules are updated for it.
@@ -737,6 +740,7 @@ public final class ActivityThread extends ClientTransactionHandler {
        CompatibilityInfo compatInfo;
        int backupMode;
        int userId;
        int operationType;
        public String toString() {
            return "CreateBackupAgentData{appInfo=" + appInfo
                    + " backupAgent=" + appInfo.backupAgentName
@@ -957,12 +961,13 @@ public final class ActivityThread extends ClientTransactionHandler {
        }

        public final void scheduleCreateBackupAgent(ApplicationInfo app,
                CompatibilityInfo compatInfo, int backupMode, int userId) {
                CompatibilityInfo compatInfo, int backupMode, int userId, int operationType) {
            CreateBackupAgentData d = new CreateBackupAgentData();
            d.appInfo = app;
            d.compatInfo = compatInfo;
            d.backupMode = backupMode;
            d.userId = userId;
            d.operationType = operationType;

            sendMessage(H.CREATE_BACKUP_AGENT, d);
        }
@@ -4075,12 +4080,7 @@ public final class ActivityThread extends ClientTransactionHandler {
            return;
        }

        String classname = data.appInfo.backupAgentName;
        // full backup operation but no app-supplied agent?  use the default implementation
        if (classname == null && (data.backupMode == ApplicationThreadConstants.BACKUP_MODE_FULL
                || data.backupMode == ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL)) {
            classname = "android.app.backup.FullBackupAgent";
        }
        String classname = getBackupAgentName(data);

        try {
            IBinder binder = null;
@@ -4104,7 +4104,7 @@ public final class ActivityThread extends ClientTransactionHandler {
                    context.setOuterContext(agent);
                    agent.attach(context);

                    agent.onCreate(UserHandle.of(data.userId));
                    agent.onCreate(UserHandle.of(data.userId), data.operationType);
                    binder = agent.onBind();
                    backupAgents.put(packageName, agent);
                } catch (Exception e) {
@@ -4132,6 +4132,23 @@ public final class ActivityThread extends ClientTransactionHandler {
        }
    }

    private String getBackupAgentName(CreateBackupAgentData data) {
        String agentName = data.appInfo.backupAgentName;
        if (!UserHandle.isCore(data.appInfo.uid)
                && data.operationType == BackupManager.OperationType.MIGRATION) {
            // If this is a migration, use the default backup agent regardless of the app's
            // preferences.
            agentName = DEFAULT_FULL_BACKUP_AGENT;
        } else {
            // full backup operation but no app-supplied agent?  use the default implementation
            if (agentName == null && (data.backupMode == ApplicationThreadConstants.BACKUP_MODE_FULL
                    || data.backupMode == ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL)) {
                agentName = DEFAULT_FULL_BACKUP_AGENT;
            }
        }
        return agentName;
    }

    // Tear down a BackupAgent
    private void handleDestroyBackupAgent(CreateBackupAgentData data) {
        if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
+2 −1
Original line number Diff line number Diff line
@@ -288,7 +288,8 @@ interface IActivityManager {
    void stopAppSwitches();
    @UnsupportedAppUsage
    void resumeAppSwitches();
    boolean bindBackupAgent(in String packageName, int backupRestoreMode, int targetUserId);
    boolean bindBackupAgent(in String packageName, int backupRestoreMode, int targetUserId,
            int operationType);
    void backupAgentCreated(in String packageName, in IBinder agent, int userId);
    void unbindBackupAgent(in ApplicationInfo appInfo);
    int getUidForIntentSender(in IIntentSender sender);
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ oneway interface IApplicationThread {
    void profilerControl(boolean start, in ProfilerInfo profilerInfo, int profileType);
    void setSchedulingGroup(int group);
    void scheduleCreateBackupAgent(in ApplicationInfo app, in CompatibilityInfo compatInfo,
            int backupMode, int userId);
            int backupMode, int userId, int operationType);
    void scheduleDestroyBackupAgent(in ApplicationInfo app,
            in CompatibilityInfo compatInfo, int userId);
    void scheduleOnNewActivityOptions(IBinder token, in Bundle options);
+2 −1
Original line number Diff line number Diff line
@@ -494,7 +494,8 @@ public class TransactionParcelTests {

        @Override
        public void scheduleCreateBackupAgent(ApplicationInfo applicationInfo,
                CompatibilityInfo compatibilityInfo, int i, int userId) throws RemoteException {
                CompatibilityInfo compatibilityInfo, int i, int userId, int operatioType)
                throws RemoteException {
        }

        @Override
+8 −4
Original line number Diff line number Diff line
@@ -184,6 +184,8 @@ import android.app.PendingIntent;
import android.app.ProcessMemoryState;
import android.app.ProfilerInfo;
import android.app.WaitResult;
import android.app.backup.BackupManager;
import android.app.backup.BackupManager.OperationType;
import android.app.backup.IBackupManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageEvents.Event;
@@ -5415,7 +5417,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            try {
                thread.scheduleCreateBackupAgent(backupTarget.appInfo,
                        compatibilityInfoForPackage(backupTarget.appInfo),
                        backupTarget.backupMode, backupTarget.userId);
                        backupTarget.backupMode, backupTarget.userId, backupTarget.operationType);
            } catch (Exception e) {
                Slog.wtf(TAG, "Exception thrown creating backup agent in " + app, e);
                badApp = true;
@@ -15248,7 +15250,8 @@ public class ActivityManagerService extends IActivityManager.Stub
    // Cause the target app to be launched if necessary and its backup agent
    // instantiated.  The backup agent will invoke backupAgentCreated() on the
    // activity manager to announce its creation.
    public boolean bindBackupAgent(String packageName, int backupMode, int targetUserId) {
    public boolean bindBackupAgent(String packageName, int backupMode, int targetUserId,
            @OperationType int operationType) {
        if (DEBUG_BACKUP) {
            Slog.v(TAG, "bindBackupAgent: app=" + packageName + " mode=" + backupMode
                    + " targetUserId=" + targetUserId + " callingUid = " + Binder.getCallingUid()
@@ -15291,7 +15294,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                        + app.packageName + ": " + e);
            }
            BackupRecord r = new BackupRecord(app, backupMode, targetUserId);
            BackupRecord r = new BackupRecord(app, backupMode, targetUserId, operationType);
            ComponentName hostingName =
                    (backupMode == ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL)
                            ? new ComponentName(app.packageName, app.backupAgentName)
@@ -15330,7 +15333,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                if (DEBUG_BACKUP) Slog.v(TAG_BACKUP, "Agent proc already running: " + proc);
                try {
                    proc.thread.scheduleCreateBackupAgent(app,
                            compatibilityInfoForPackage(app), backupMode, targetUserId);
                            compatibilityInfoForPackage(app), backupMode, targetUserId,
                            operationType);
                } catch (RemoteException e) {
                    // Will time out on the backup manager side
                }
Loading