summaryrefslogtreecommitdiff
path: root/services/backup/java
diff options
context:
space:
mode:
authornathch <nathch@google.com>2020-01-15 15:58:36 +0000
committerChandan Nath <nathch@google.com>2020-01-16 10:58:38 +0000
commitf203e2a6740d7d98d864bf34a814725d9c21908e (patch)
tree99318a48e058770721badd4845685a3c04a3f41c /services/backup/java
parent3746de821efdca333f01bcd170658831d2f884c5 (diff)
Remove RunBackupReceiver
Currently, the logic for backupnow is split between UserBackupManagerService, RunBackupReceiver and BackupHandler. This makes it very confusing as the pre checks are split over three classes and there's no obvious reason why it is like that. On the other hand, requestBackup is split nicely into checks in UserBackupManagerService and starting the work from BackupHandler. This change removes RunBackupReceiver for backupNow and splits the logic only between UserBackupManager and BackupHandler - in the direction of making it consistent with requestBackup by moving the checks to UseBackupManagerService. (except the check for isBackupRunning which needs to be in the same synchronised block as setBackupRunning). Also, this CL moves the wakelock acquire from RunBackupReceiver (removed in this CL) to BackupHandler. Previously, RunBackupReceiver would acquire the wakelock and then send MSG_RUN_BACKUP. BackupHandler would then have to release that wakelock when it finds it cant run the backup because there's no transport. Now, the acquire is in BackupHandler after checking the transport. Therefore we dont need that release (when there's no transport) anymore A side benefit is we get rid of an extra hop to the broadcast receiver so potentially backups should be scheduled (albeit very slightly) faster. Test: atest -v RunBackupFrameworksServicesRoboTests Test: atest -v $(find frameworks/base/services/tests/servicestests/src/com/android/server/backup -name '\''*Test.java'\'') Test: atest -v CtsBackupTestCases Test: atest -v CtsBackupHostTestCases Bug: 147741497 Change-Id: I40051540c7e8531ef05076eab7ccc5b44b0c08d2
Diffstat (limited to 'services/backup/java')
-rw-r--r--services/backup/java/com/android/server/backup/UserBackupManagerService.java69
-rw-r--r--services/backup/java/com/android/server/backup/internal/BackupHandler.java19
-rw-r--r--services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java110
3 files changed, 49 insertions, 149 deletions
diff --git a/services/backup/java/com/android/server/backup/UserBackupManagerService.java b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
index 2c229b443f91..f0fa99a4eec7 100644
--- a/services/backup/java/com/android/server/backup/UserBackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/UserBackupManagerService.java
@@ -31,6 +31,7 @@ import static com.android.server.backup.internal.BackupHandler.MSG_RESTORE_SESSI
import static com.android.server.backup.internal.BackupHandler.MSG_RETRY_CLEAR;
import static com.android.server.backup.internal.BackupHandler.MSG_RUN_ADB_BACKUP;
import static com.android.server.backup.internal.BackupHandler.MSG_RUN_ADB_RESTORE;
+import static com.android.server.backup.internal.BackupHandler.MSG_RUN_BACKUP;
import static com.android.server.backup.internal.BackupHandler.MSG_RUN_CLEAR;
import static com.android.server.backup.internal.BackupHandler.MSG_RUN_RESTORE;
import static com.android.server.backup.internal.BackupHandler.MSG_SCHEDULE_BACKUP_PACKAGE;
@@ -111,7 +112,6 @@ import com.android.server.backup.internal.ClearDataObserver;
import com.android.server.backup.internal.OnTaskFinishedListener;
import com.android.server.backup.internal.Operation;
import com.android.server.backup.internal.PerformInitializeTask;
-import com.android.server.backup.internal.RunBackupReceiver;
import com.android.server.backup.internal.RunInitializeReceiver;
import com.android.server.backup.internal.SetupObserver;
import com.android.server.backup.keyvalue.BackupRequest;
@@ -257,7 +257,6 @@ public class UserBackupManagerService {
// Retry interval for clear/init when the transport is unavailable
private static final long TRANSPORT_RETRY_INTERVAL = 1 * AlarmManager.INTERVAL_HOUR;
- public static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN";
public static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT";
private static final String BACKUP_FINISHED_ACTION = "android.intent.action.BACKUP_FINISHED";
private static final String BACKUP_FINISHED_PACKAGE_EXTRA = "packageName";
@@ -319,7 +318,6 @@ public class UserBackupManagerService {
private boolean mSetupComplete;
private boolean mAutoRestore;
- private final PendingIntent mRunBackupIntent;
private final PendingIntent mRunInitIntent;
private final ArraySet<String> mPendingInits = new ArraySet<>(); // transport names
@@ -417,7 +415,6 @@ public class UserBackupManagerService {
@Nullable private File mAncestralSerialNumberFile;
private final ContentObserver mSetupObserver;
- private final BroadcastReceiver mRunBackupReceiver;
private final BroadcastReceiver mRunInitReceiver;
/**
@@ -566,19 +563,9 @@ public class UserBackupManagerService {
mDataDir = Objects.requireNonNull(dataDir, "dataDir cannot be null");
mBackupPasswordManager = new BackupPasswordManager(mContext, mBaseStateDir, mRng);
- // Receivers for scheduled backups and transport initialization operations.
- mRunBackupReceiver = new RunBackupReceiver(this);
- IntentFilter filter = new IntentFilter();
- filter.addAction(RUN_BACKUP_ACTION);
- context.registerReceiverAsUser(
- mRunBackupReceiver,
- UserHandle.of(userId),
- filter,
- android.Manifest.permission.BACKUP,
- /* scheduler */ null);
-
+ // Receiver for transport initialization.
mRunInitReceiver = new RunInitializeReceiver(this);
- filter = new IntentFilter();
+ IntentFilter filter = new IntentFilter();
filter.addAction(RUN_INITIALIZE_ACTION);
context.registerReceiverAsUser(
mRunInitReceiver,
@@ -587,16 +574,6 @@ public class UserBackupManagerService {
android.Manifest.permission.BACKUP,
/* scheduler */ null);
- Intent backupIntent = new Intent(RUN_BACKUP_ACTION);
- backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
- mRunBackupIntent =
- PendingIntent.getBroadcastAsUser(
- context,
- /* requestCode */ 0,
- backupIntent,
- /* flags */ 0,
- UserHandle.of(userId));
-
Intent initIntent = new Intent(RUN_INITIALIZE_ACTION);
initIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mRunInitIntent =
@@ -659,7 +636,6 @@ public class UserBackupManagerService {
mAgentTimeoutParameters.stop();
mConstants.stop();
mContext.getContentResolver().unregisterContentObserver(mSetupObserver);
- mContext.unregisterReceiver(mRunBackupReceiver);
mContext.unregisterReceiver(mRunInitReceiver);
mContext.unregisterReceiver(mPackageTrackingReceiver);
mBackupHandler.stop();
@@ -2538,15 +2514,38 @@ public class UserBackupManagerService {
KeyValueBackupJob.schedule(mUserId, mContext, mConstants);
} else {
if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass");
- // Fire the intent that kicks off the whole shebang...
- try {
- mRunBackupIntent.send();
- } catch (PendingIntent.CanceledException e) {
- // should never happen
- Slog.e(TAG, "run-backup intent cancelled!");
+
+ synchronized (getQueueLock()) {
+ if (getPendingInits().size() > 0) {
+ // If there are pending init operations, we process those and then settle
+ // into the usual periodic backup schedule.
+ if (MORE_DEBUG) {
+ Slog.v(TAG, "Init pending at scheduled backup");
+ }
+ try {
+ getAlarmManager().cancel(mRunInitIntent);
+ mRunInitIntent.send();
+ } catch (PendingIntent.CanceledException ce) {
+ Slog.w(TAG, "Run init intent cancelled");
+ }
+ return;
}
- // ...and cancel any pending scheduled job, because we've just superseded it
- KeyValueBackupJob.cancel(mUserId, mContext);
+ }
+
+ // Don't run backups if we're disabled or not yet set up.
+ if (!isEnabled() || !isSetupComplete()) {
+ Slog.w(
+ TAG,
+ "Backup pass but enabled=" + isEnabled()
+ + " setupComplete=" + isSetupComplete());
+ return;
+ }
+
+ // Fire the msg that kicks off the whole shebang...
+ Message message = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
+ mBackupHandler.sendMessage(message);
+ // ...and cancel any pending scheduled job, because we've just superseded it
+ KeyValueBackupJob.cancel(mUserId, mContext);
}
} finally {
Binder.restoreCallingIdentity(oldId);
diff --git a/services/backup/java/com/android/server/backup/internal/BackupHandler.java b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
index b06fc52a24c2..05396f36b364 100644
--- a/services/backup/java/com/android/server/backup/internal/BackupHandler.java
+++ b/services/backup/java/com/android/server/backup/internal/BackupHandler.java
@@ -158,10 +158,6 @@ public class BackupHandler extends Handler {
.disposeOfTransportClient(transportClient, callerLogString);
}
Slog.v(TAG, "Backup requested but no transport available");
- synchronized (backupManagerService.getQueueLock()) {
- backupManagerService.setBackupRunning(false);
- }
- backupManagerService.getWakelock().release();
break;
}
@@ -169,6 +165,21 @@ public class BackupHandler extends Handler {
List<String> queue = new ArrayList<>();
DataChangedJournal oldJournal = backupManagerService.getJournal();
synchronized (backupManagerService.getQueueLock()) {
+ // Don't run backups if one is already running.
+ if (backupManagerService.isBackupRunning()) {
+ Slog.i(TAG, "Backup time but one already running");
+ return;
+ }
+
+ if (DEBUG) {
+ Slog.v(TAG, "Running a backup pass");
+ }
+
+ // Acquire the wakelock and pass it to the backup thread. It will be released
+ // once backup concludes.
+ backupManagerService.setBackupRunning(true);
+ backupManagerService.getWakelock().acquire();
+
// Do we have any work to do? Construct the work queue
// then release the synchronization lock to actually run
// the backup.
diff --git a/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java b/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java
deleted file mode 100644
index d37b106c2b26..000000000000
--- a/services/backup/java/com/android/server/backup/internal/RunBackupReceiver.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-package com.android.server.backup.internal;
-
-import static com.android.server.backup.BackupManagerService.DEBUG;
-import static com.android.server.backup.BackupManagerService.MORE_DEBUG;
-import static com.android.server.backup.BackupManagerService.TAG;
-import static com.android.server.backup.UserBackupManagerService.RUN_BACKUP_ACTION;
-import static com.android.server.backup.internal.BackupHandler.MSG_RUN_BACKUP;
-
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Handler;
-import android.os.Message;
-import android.util.Slog;
-
-import com.android.server.backup.UserBackupManagerService;
-
-/**
- * A {@link BroadcastReceiver} for the action {@link UserBackupManagerService#RUN_BACKUP_ACTION}
- * that runs an immediate backup operation if eligible.
- */
-public class RunBackupReceiver extends BroadcastReceiver {
- private final UserBackupManagerService mUserBackupManagerService;
-
- public RunBackupReceiver(UserBackupManagerService userBackupManagerService) {
- mUserBackupManagerService = userBackupManagerService;
- }
-
- /**
- * Run a backup pass if we're eligible. We're eligible if the following conditions are met:
- *
- * <ul>
- * <li>No transports are pending initialization (otherwise we kick off an initialization
- * operation instead).
- * <li>Backup is enabled for the user.
- * <li>The user has completed setup.
- * <li>No backup operation is currently running for the user.
- * </ul>
- */
- public void onReceive(Context context, Intent intent) {
- if (!RUN_BACKUP_ACTION.equals(intent.getAction())) {
- return;
- }
-
- synchronized (mUserBackupManagerService.getQueueLock()) {
- if (mUserBackupManagerService.getPendingInits().size() > 0) {
- // If there are pending init operations, we process those and then settle into the
- // usual periodic backup schedule.
- if (MORE_DEBUG) {
- Slog.v(TAG, "Init pending at scheduled backup");
- }
- try {
- PendingIntent runInitIntent = mUserBackupManagerService.getRunInitIntent();
- mUserBackupManagerService.getAlarmManager().cancel(runInitIntent);
- runInitIntent.send();
- } catch (PendingIntent.CanceledException ce) {
- Slog.w(TAG, "Run init intent cancelled");
- }
- } else {
- // Don't run backups if we're disabled or not yet set up.
- if (!mUserBackupManagerService.isEnabled()
- || !mUserBackupManagerService.isSetupComplete()) {
- Slog.w(
- TAG,
- "Backup pass but enabled="
- + mUserBackupManagerService.isEnabled()
- + " setupComplete="
- + mUserBackupManagerService.isSetupComplete());
- return;
- }
-
- // Don't run backups if one is already running.
- if (mUserBackupManagerService.isBackupRunning()) {
- Slog.i(TAG, "Backup time but one already running");
- return;
- }
-
- if (DEBUG) {
- Slog.v(TAG, "Running a backup pass");
- }
-
- // Acquire the wakelock and pass it to the backup thread. It will be released once
- // backup concludes.
- mUserBackupManagerService.setBackupRunning(true);
- mUserBackupManagerService.getWakelock().acquire();
-
- Handler backupHandler = mUserBackupManagerService.getBackupHandler();
- Message message = backupHandler.obtainMessage(MSG_RUN_BACKUP);
- backupHandler.sendMessage(message);
- }
- }
- }
-}