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
This commit is contained in:
Ruslan Tkhakokhov
2022-11-23 23:52:08 +00:00
parent 0828847a7e
commit 9c44a14afe
6 changed files with 55 additions and 34 deletions

View File

@@ -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

View File

@@ -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;
@@ -264,13 +265,6 @@ public abstract class BackupAgent extends ContextWrapper {
public void onCreate() {
}
/**
* @hide
*/
public void onCreate(UserHandle user) {
onCreate(user, DEFAULT_BACKUP_DESTINATION);
}
/**
* Provided as a convenience for agent implementations that need an opportunity
* to do one-time initialization before the actual backup or restore operation
@@ -279,13 +273,33 @@ public abstract class BackupAgent extends ContextWrapper {
*
* @hide
*/
public void onCreate(UserHandle user, @BackupDestination int backupDestination) {
// TODO: Instantiate with the correct type using a parameter.
mLogger = new BackupRestoreEventLogger(BackupRestoreEventLogger.OperationType.BACKUP);
public void onCreate(UserHandle user) {
onCreate();
}
/**
* @deprecated Use {@link BackupAgent#onCreate(UserHandle, int, int)} instead.
*
* @hide
*/
@Deprecated
public void onCreate(UserHandle user, @BackupDestination int backupDestination) {
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);
}
/**

View File

@@ -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;
}

View File

@@ -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}

View File

@@ -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));

View File

@@ -16,8 +16,8 @@
package android.app.backup;
import static android.app.backup.BackupRestoreEventLogger.OperationType.BACKUP;
import static android.app.backup.BackupRestoreEventLogger.OperationType.RESTORE;
import static android.app.backup.BackupAnnotations.OperationType.BACKUP;
import static android.app.backup.BackupAnnotations.OperationType.RESTORE;
import static com.google.common.truth.Truth.assertThat;