Merge "Remove RunBackupReceiver"

This commit is contained in:
TreeHugger Robot
2020-01-17 12:45:45 +00:00
committed by Android (Google) Code Review
4 changed files with 49 additions and 150 deletions

View File

@@ -131,7 +131,6 @@
<protected-broadcast android:name="android.os.action.SETTING_RESTORED" />
<protected-broadcast android:name="android.app.backup.intent.RUN" />
<protected-broadcast android:name="android.app.backup.intent.CLEAR" />
<protected-broadcast android:name="android.app.backup.intent.INIT" />

View File

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

View File

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

View File

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