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

Commit 10460467 authored by Sarp Misoglu's avatar Sarp Misoglu
Browse files

Pass agent logs to monitor after package B&R

In this change B&R tasks ask for logs from each BackupAgent after it is
backed up or restored. These logs are then immediately passed on to the
BackupManagerMonitor.

Bug: 252760247
Change-Id: Iaf43ce61e98bb4b42c05c18a46031a06d41abb12
Test: $ atest BackupFrameworksServicesRoboTests:KeyValueBackupTaskTest
  $ atest BackupManagerMonitorUtilsTest
  Unfortunately, the other classes are not unit testable.
  For manual testing, I implemented some fake loggers for various
  BackupAgents, passed in a fake BackupManagerMonitor, ran B&R with the
  following commands, and observed that the monitor received the logs.
  1. $ adb shell bmgr backupnow --all
  2. $ adb shell bmgr restore ... (for one KV and one FullBackup package)
parent f9ca7a2f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1470,6 +1470,7 @@ package android.app.backup {
  public class BackupManagerMonitor {
    ctor public BackupManagerMonitor();
    method public void onEvent(android.os.Bundle);
    field public static final String EXTRA_LOG_AGENT_LOGGING_RESULTS = "android.app.backup.extra.LOG_AGENT_LOGGING_RESULTS";
    field public static final String EXTRA_LOG_CANCEL_ALL = "android.app.backup.extra.LOG_CANCEL_ALL";
    field public static final String EXTRA_LOG_EVENT_CATEGORY = "android.app.backup.extra.LOG_EVENT_CATEGORY";
    field public static final String EXTRA_LOG_EVENT_ID = "android.app.backup.extra.LOG_EVENT_ID";
@@ -1488,6 +1489,7 @@ package android.app.backup {
    field public static final int LOG_EVENT_CATEGORY_AGENT = 2; // 0x2
    field public static final int LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY = 3; // 0x3
    field public static final int LOG_EVENT_CATEGORY_TRANSPORT = 1; // 0x1
    field public static final int LOG_EVENT_ID_AGENT_LOGGING_RESULTS = 52; // 0x34
    field public static final int LOG_EVENT_ID_APK_NOT_INSTALLED = 40; // 0x28
    field public static final int LOG_EVENT_ID_APP_HAS_NO_AGENT = 28; // 0x1c
    field public static final int LOG_EVENT_ID_BACKUP_DISABLED = 13; // 0xd
+10 −8
Original line number Diff line number Diff line
@@ -129,6 +129,13 @@ public class BackupManagerMonitor {
   */
  public static final String EXTRA_LOG_OLD_VERSION = "android.app.backup.extra.LOG_OLD_VERSION";

  /**
   * ParcelableList: when we have an event of id LOG_EVENT_ID_AGENT_LOGGING_RESULTS we send a list
   * of {@link android.app.backup.BackupRestoreEventLogger.DataTypeResult}.
   */
  public static final String EXTRA_LOG_AGENT_LOGGING_RESULTS =
          "android.app.backup.extra.LOG_AGENT_LOGGING_RESULTS";

  // TODO complete this list with all log messages. And document properly.
  public static final int LOG_EVENT_ID_FULL_BACKUP_CANCEL = 4;
  public static final int LOG_EVENT_ID_ILLEGAL_KEY = 5;
@@ -171,15 +178,10 @@ public class BackupManagerMonitor {
  public static final int LOG_EVENT_ID_WIDGET_UNKNOWN_VERSION = 48;
  public static final int LOG_EVENT_ID_NO_PACKAGES = 49;
  public static final int LOG_EVENT_ID_TRANSPORT_IS_NULL = 50;

    /**
     * The transport returned {@link BackupTransport#TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED}.
     */
  /** The transport returned {@link BackupTransport#TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED}. */
  public static final int LOG_EVENT_ID_TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED = 51;




  public static final int LOG_EVENT_ID_AGENT_LOGGING_RESULTS = 52;

  /**
   * This method will be called each time something important happens on BackupManager.
+10 −2
Original line number Diff line number Diff line
@@ -23,11 +23,13 @@ import static com.android.server.backup.UserBackupManagerService.BACKUP_MANIFEST
import static com.android.server.backup.UserBackupManagerService.BACKUP_METADATA_FILENAME;
import static com.android.server.backup.UserBackupManagerService.SHARED_BACKUP_AGENT_PACKAGE;

import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ApplicationThreadConstants;
import android.app.IBackupAgent;
import android.app.backup.BackupTransport;
import android.app.backup.FullBackupDataOutput;
import android.app.backup.IBackupManagerMonitor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -42,6 +44,7 @@ import com.android.server.backup.OperationStorage.OpType;
import com.android.server.backup.UserBackupManagerService;
import com.android.server.backup.remote.RemoteCall;
import com.android.server.backup.utils.BackupEligibilityRules;
import com.android.server.backup.utils.BackupManagerMonitorUtils;
import com.android.server.backup.utils.FullBackupUtils;

import java.io.File;
@@ -60,12 +63,13 @@ public class FullBackupEngine {
    private BackupRestoreTask mTimeoutMonitor;
    private IBackupAgent mAgent;
    private boolean mIncludeApks;
    private PackageInfo mPkg;
    private final PackageInfo mPkg;
    private final long mQuota;
    private final int mOpToken;
    private final int mTransportFlags;
    private final BackupAgentTimeoutParameters mAgentTimeoutParameters;
    private final BackupEligibilityRules mBackupEligibilityRules;
    @Nullable private final IBackupManagerMonitor mMonitor;

    class FullBackupRunner implements Runnable {
        private final @UserIdInt int mUserId;
@@ -193,7 +197,8 @@ public class FullBackupEngine {
            long quota,
            int opToken,
            int transportFlags,
            BackupEligibilityRules backupEligibilityRules) {
            BackupEligibilityRules backupEligibilityRules,
            IBackupManagerMonitor monitor) {
        this.backupManagerService = backupManagerService;
        mOutput = output;
        mPreflightHook = preflightHook;
@@ -208,6 +213,7 @@ public class FullBackupEngine {
                        backupManagerService.getAgentTimeoutParameters(),
                        "Timeout parameters cannot be null");
        mBackupEligibilityRules = backupEligibilityRules;
        mMonitor = monitor;
    }

    public int preflightCheck() throws RemoteException {
@@ -260,6 +266,8 @@ public class FullBackupEngine {
                    }
                    result = BackupTransport.TRANSPORT_OK;
                }

                BackupManagerMonitorUtils.monitorAgentLoggingResults(mMonitor, mPkg, mAgent);
            } catch (IOException e) {
                Slog.e(TAG, "Error backing up " + mPkg.packageName + ": " + e.getMessage());
                result = BackupTransport.AGENT_ERROR;
+2 −1
Original line number Diff line number Diff line
@@ -420,7 +420,8 @@ public class PerformAdbBackupTask extends FullBackupTask implements BackupRestor
                                Long.MAX_VALUE,
                                mCurrentOpToken,
                                /*transportFlags=*/ 0,
                                mBackupEligibilityRules);
                                mBackupEligibilityRules,
                                /* monitor= */ null);
                sendOnBackupPackage(isSharedStorage ? "Shared storage" : pkg.packageName);

                // Don't need to check preflight result as there is no preflight hook.
+2 −1
Original line number Diff line number Diff line
@@ -882,7 +882,8 @@ public class PerformFullTransportBackupTask extends FullBackupTask implements Ba
                            mQuota,
                            mCurrentOpToken,
                            mTransportFlags,
                            mBackupEligibilityRules);
                            mBackupEligibilityRules,
                            mMonitor);
            try {
                try {
                    if (!mIsCancelled) {
Loading