blob: ba153bf90ebea1c871c6dd78737d740306743ab5 [file] [log] [blame]
Artem Iglikovf251e352017-04-07 12:48:54 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.backup.internal;
18
Annie Meng384230f2018-11-28 10:58:08 +000019import static com.android.server.backup.BackupManagerService.DEBUG;
20import static com.android.server.backup.BackupManagerService.MORE_DEBUG;
21import static com.android.server.backup.BackupManagerService.TAG;
Artem Iglikovdbe68322017-05-09 11:00:03 +010022
Artem Iglikovf251e352017-04-07 12:48:54 +010023import android.app.backup.RestoreSet;
24import android.content.Intent;
25import android.os.Handler;
26import android.os.Looper;
27import android.os.Message;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.util.EventLog;
31import android.util.Pair;
32import android.util.Slog;
Artem Iglikov21510f02017-04-18 14:53:06 +010033
Artem Iglikovf251e352017-04-07 12:48:54 +010034import com.android.internal.backup.IBackupTransport;
Annie Menga1e8fd62018-03-15 14:45:46 +000035import com.android.internal.util.Preconditions;
Artem Iglikovf251e352017-04-07 12:48:54 +010036import com.android.server.EventLogTags;
Annie Menga1e8fd62018-03-15 14:45:46 +000037import com.android.server.backup.BackupAgentTimeoutParameters;
Artem Iglikovf251e352017-04-07 12:48:54 +010038import com.android.server.backup.BackupRestoreTask;
Robert Berryc31a8392017-07-14 14:19:21 +010039import com.android.server.backup.DataChangedJournal;
Bernardo Rufinoaf547f42017-11-13 15:49:41 +000040import com.android.server.backup.TransportManager;
Annie Meng813716b2018-11-20 14:08:21 +000041import com.android.server.backup.UserBackupManagerService;
Artem Iglikovf251e352017-04-07 12:48:54 +010042import com.android.server.backup.fullbackup.PerformAdbBackupTask;
43import com.android.server.backup.fullbackup.PerformFullTransportBackupTask;
Bernardo Rufino75f73f02018-08-02 09:39:39 +010044import com.android.server.backup.keyvalue.BackupRequest;
45import com.android.server.backup.keyvalue.KeyValueBackupTask;
Artem Iglikovf251e352017-04-07 12:48:54 +010046import com.android.server.backup.params.AdbBackupParams;
47import com.android.server.backup.params.AdbParams;
48import com.android.server.backup.params.AdbRestoreParams;
49import com.android.server.backup.params.BackupParams;
50import com.android.server.backup.params.ClearParams;
51import com.android.server.backup.params.ClearRetryParams;
52import com.android.server.backup.params.RestoreGetSetsParams;
53import com.android.server.backup.params.RestoreParams;
54import com.android.server.backup.restore.PerformAdbRestoreTask;
55import com.android.server.backup.restore.PerformUnifiedRestoreTask;
Bernardo Rufino2d87f452018-06-22 11:47:49 +010056import com.android.server.backup.transport.TransportClient;
Artem Iglikov21510f02017-04-18 14:53:06 +010057
Artem Iglikovf251e352017-04-07 12:48:54 +010058import java.util.ArrayList;
59import java.util.Collections;
Bernardo Rufinoe80f2702018-08-10 17:06:15 +010060import java.util.List;
Artem Iglikovf251e352017-04-07 12:48:54 +010061
62/**
63 * Asynchronous backup/restore handler thread.
64 */
65public class BackupHandler extends Handler {
66
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +010067 public static final int MSG_RUN_BACKUP = 1;
68 public static final int MSG_RUN_ADB_BACKUP = 2;
69 public static final int MSG_RUN_RESTORE = 3;
70 public static final int MSG_RUN_CLEAR = 4;
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +010071 public static final int MSG_RUN_GET_RESTORE_SETS = 6;
72 public static final int MSG_RESTORE_SESSION_TIMEOUT = 8;
73 public static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9;
74 public static final int MSG_RUN_ADB_RESTORE = 10;
75 public static final int MSG_RETRY_INIT = 11;
76 public static final int MSG_RETRY_CLEAR = 12;
77 public static final int MSG_WIDGET_BROADCAST = 13;
78 public static final int MSG_RUN_FULL_TRANSPORT_BACKUP = 14;
79 public static final int MSG_REQUEST_BACKUP = 15;
80 public static final int MSG_SCHEDULE_BACKUP_PACKAGE = 16;
81 public static final int MSG_BACKUP_OPERATION_TIMEOUT = 17;
82 public static final int MSG_RESTORE_OPERATION_TIMEOUT = 18;
83 // backup task state machine tick
84 public static final int MSG_BACKUP_RESTORE_STEP = 20;
85 public static final int MSG_OP_COMPLETE = 21;
86
Annie Meng813716b2018-11-20 14:08:21 +000087 private final UserBackupManagerService backupManagerService;
Annie Menga1e8fd62018-03-15 14:45:46 +000088 private final BackupAgentTimeoutParameters mAgentTimeoutParameters;
Artem Iglikovf251e352017-04-07 12:48:54 +010089
Annie Meng813716b2018-11-20 14:08:21 +000090 public BackupHandler(UserBackupManagerService backupManagerService, Looper looper) {
Artem Iglikovf251e352017-04-07 12:48:54 +010091 super(looper);
92 this.backupManagerService = backupManagerService;
Annie Menga1e8fd62018-03-15 14:45:46 +000093 mAgentTimeoutParameters = Preconditions.checkNotNull(
94 backupManagerService.getAgentTimeoutParameters(),
95 "Timeout parameters cannot be null");
Artem Iglikovf251e352017-04-07 12:48:54 +010096 }
97
98 public void handleMessage(Message msg) {
99
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000100 TransportManager transportManager = backupManagerService.getTransportManager();
Artem Iglikovf251e352017-04-07 12:48:54 +0100101 switch (msg.what) {
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100102 case MSG_RUN_BACKUP: {
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100103 backupManagerService.setLastBackupPass(System.currentTimeMillis());
Artem Iglikovf251e352017-04-07 12:48:54 +0100104
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000105 String callerLogString = "BH/MSG_RUN_BACKUP";
106 TransportClient transportClient =
107 transportManager.getCurrentTransportClient(callerLogString);
Artem Iglikov21510f02017-04-18 14:53:06 +0100108 IBackupTransport transport =
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000109 transportClient != null
110 ? transportClient.connect(callerLogString)
111 : null;
Artem Iglikov21510f02017-04-18 14:53:06 +0100112 if (transport == null) {
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000113 if (transportClient != null) {
114 transportManager
115 .disposeOfTransportClient(transportClient, callerLogString);
116 }
Artem Iglikovdbe68322017-05-09 11:00:03 +0100117 Slog.v(TAG, "Backup requested but no transport available");
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100118 synchronized (backupManagerService.getQueueLock()) {
119 backupManagerService.setBackupRunning(false);
Artem Iglikov21510f02017-04-18 14:53:06 +0100120 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100121 backupManagerService.getWakelock().release();
Artem Iglikov21510f02017-04-18 14:53:06 +0100122 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100123 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100124
Bernardo Rufinoe80f2702018-08-10 17:06:15 +0100125 // Snapshot the pending-backup set and work on that.
126 List<String> queue = new ArrayList<>();
Robert Berryc31a8392017-07-14 14:19:21 +0100127 DataChangedJournal oldJournal = backupManagerService.getJournal();
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100128 synchronized (backupManagerService.getQueueLock()) {
Artem Iglikov21510f02017-04-18 14:53:06 +0100129 // Do we have any work to do? Construct the work queue
130 // then release the synchronization lock to actually run
131 // the backup.
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100132 if (backupManagerService.getPendingBackups().size() > 0) {
133 for (BackupRequest b : backupManagerService.getPendingBackups().values()) {
Bernardo Rufinoe80f2702018-08-10 17:06:15 +0100134 queue.add(b.packageName);
Artem Iglikov21510f02017-04-18 14:53:06 +0100135 }
Artem Iglikovdbe68322017-05-09 11:00:03 +0100136 if (DEBUG) {
137 Slog.v(TAG, "clearing pending backups");
Artem Iglikov21510f02017-04-18 14:53:06 +0100138 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100139 backupManagerService.getPendingBackups().clear();
Artem Iglikov21510f02017-04-18 14:53:06 +0100140
141 // Start a new backup-queue journal file too
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100142 backupManagerService.setJournal(null);
Artem Iglikov21510f02017-04-18 14:53:06 +0100143
144 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100145 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100146
Artem Iglikov21510f02017-04-18 14:53:06 +0100147 // At this point, we have started a new journal file, and the old
148 // file identity is being passed to the backup processing task.
149 // When it completes successfully, that old journal file will be
150 // deleted. If we crash prior to that, the old journal is parsed
151 // at next boot and the journaled requests fulfilled.
152 boolean staged = true;
153 if (queue.size() > 0) {
154 // Spin up a backup state sequence and set it running
155 try {
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000156 OnTaskFinishedListener listener =
157 caller ->
158 transportManager
159 .disposeOfTransportClient(transportClient, caller);
Bernardo Rufinocc714c12018-08-02 08:39:45 +0100160 KeyValueBackupTask.start(
Bernardo Rufino2d87f452018-06-22 11:47:49 +0100161 backupManagerService,
162 transportClient,
163 transport.transportDirName(),
164 queue,
165 oldJournal,
166 /* observer */ null,
167 /* monitor */ null,
168 listener,
169 Collections.emptyList(),
170 /* userInitiated */ false,
171 /* nonIncremental */ false);
Artem Iglikov21510f02017-04-18 14:53:06 +0100172 } catch (Exception e) {
173 // unable to ask the transport its dir name -- transient failure, since
174 // the above check succeeded. Try again next time.
Artem Iglikovdbe68322017-05-09 11:00:03 +0100175 Slog.e(TAG, "Transport became unavailable attempting backup"
176 + " or error initializing backup task", e);
Artem Iglikov21510f02017-04-18 14:53:06 +0100177 staged = false;
178 }
179 } else {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100180 Slog.v(TAG, "Backup requested but nothing pending");
Artem Iglikov21510f02017-04-18 14:53:06 +0100181 staged = false;
Artem Iglikovf251e352017-04-07 12:48:54 +0100182 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100183
184 if (!staged) {
Bernardo Rufinoaf547f42017-11-13 15:49:41 +0000185 transportManager.disposeOfTransportClient(transportClient, callerLogString);
Artem Iglikov21510f02017-04-18 14:53:06 +0100186 // if we didn't actually hand off the wakelock, rewind until next time
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100187 synchronized (backupManagerService.getQueueLock()) {
188 backupManagerService.setBackupRunning(false);
Artem Iglikov21510f02017-04-18 14:53:06 +0100189 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100190 backupManagerService.getWakelock().release();
Artem Iglikovf251e352017-04-07 12:48:54 +0100191 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100192 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100193 }
194
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100195 case MSG_BACKUP_RESTORE_STEP: {
Artem Iglikovf251e352017-04-07 12:48:54 +0100196 try {
Artem Iglikov21510f02017-04-18 14:53:06 +0100197 BackupRestoreTask task = (BackupRestoreTask) msg.obj;
Artem Iglikovdbe68322017-05-09 11:00:03 +0100198 if (MORE_DEBUG) {
199 Slog.v(TAG, "Got next step for " + task + ", executing");
Artem Iglikov21510f02017-04-18 14:53:06 +0100200 }
201 task.execute();
202 } catch (ClassCastException e) {
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000203 Slog.e(TAG, "Invalid backup/restore task in flight, obj=" + msg.obj);
Artem Iglikov21510f02017-04-18 14:53:06 +0100204 }
205 break;
206 }
207
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100208 case MSG_OP_COMPLETE: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100209 try {
210 Pair<BackupRestoreTask, Long> taskWithResult =
211 (Pair<BackupRestoreTask, Long>) msg.obj;
212 taskWithResult.first.operationComplete(taskWithResult.second);
213 } catch (ClassCastException e) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100214 Slog.e(TAG, "Invalid completion in flight, obj=" + msg.obj);
Artem Iglikov21510f02017-04-18 14:53:06 +0100215 }
216 break;
217 }
218
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100219 case MSG_RUN_ADB_BACKUP: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100220 // TODO: refactor full backup to be a looper-based state machine
221 // similar to normal backup/restore.
222 AdbBackupParams params = (AdbBackupParams) msg.obj;
223 PerformAdbBackupTask task = new PerformAdbBackupTask(backupManagerService,
224 params.fd,
225 params.observer, params.includeApks, params.includeObbs,
226 params.includeShared, params.doWidgets, params.curPassword,
227 params.encryptPassword, params.allApps, params.includeSystem,
228 params.doCompress, params.includeKeyValue, params.packages, params.latch);
229 (new Thread(task, "adb-backup")).start();
230 break;
231 }
232
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100233 case MSG_RUN_FULL_TRANSPORT_BACKUP: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100234 PerformFullTransportBackupTask task = (PerformFullTransportBackupTask) msg.obj;
235 (new Thread(task, "transport-backup")).start();
236 break;
237 }
238
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100239 case MSG_RUN_RESTORE: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100240 RestoreParams params = (RestoreParams) msg.obj;
Artem Iglikovdbe68322017-05-09 11:00:03 +0100241 Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
Artem Iglikov21510f02017-04-18 14:53:06 +0100242
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000243 PerformUnifiedRestoreTask task =
244 new PerformUnifiedRestoreTask(
245 backupManagerService,
246 params.transportClient,
247 params.observer,
248 params.monitor,
249 params.token,
250 params.packageInfo,
251 params.pmToken,
252 params.isSystemRestore,
253 params.filterSet,
254 params.listener);
Artem Iglikov21510f02017-04-18 14:53:06 +0100255
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100256 synchronized (backupManagerService.getPendingRestores()) {
257 if (backupManagerService.isRestoreInProgress()) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100258 if (DEBUG) {
259 Slog.d(TAG, "Restore in progress, queueing.");
Artem Iglikov21510f02017-04-18 14:53:06 +0100260 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100261 backupManagerService.getPendingRestores().add(task);
Artem Iglikov21510f02017-04-18 14:53:06 +0100262 // This task will be picked up and executed when the the currently running
263 // restore task finishes.
264 } else {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100265 if (DEBUG) {
266 Slog.d(TAG, "Starting restore.");
Artem Iglikov21510f02017-04-18 14:53:06 +0100267 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100268 backupManagerService.setRestoreInProgress(true);
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100269 Message restoreMsg = obtainMessage(MSG_BACKUP_RESTORE_STEP, task);
Artem Iglikov21510f02017-04-18 14:53:06 +0100270 sendMessage(restoreMsg);
271 }
272 }
273 break;
274 }
275
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100276 case MSG_RUN_ADB_RESTORE: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100277 // TODO: refactor full restore to be a looper-based state machine
278 // similar to normal backup/restore.
279 AdbRestoreParams params = (AdbRestoreParams) msg.obj;
280 PerformAdbRestoreTask task = new PerformAdbRestoreTask(backupManagerService,
281 params.fd,
282 params.curPassword, params.encryptPassword,
283 params.observer, params.latch);
284 (new Thread(task, "adb-restore")).start();
285 break;
286 }
287
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100288 case MSG_RUN_CLEAR: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100289 ClearParams params = (ClearParams) msg.obj;
Bernardo Rufinof93a0912017-12-04 14:11:24 +0000290 Runnable task =
291 new PerformClearTask(
292 backupManagerService,
293 params.transportClient,
294 params.packageInfo,
295 params.listener);
296 task.run();
Artem Iglikov21510f02017-04-18 14:53:06 +0100297 break;
298 }
299
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100300 case MSG_RETRY_CLEAR: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100301 // reenqueues if the transport remains unavailable
302 ClearRetryParams params = (ClearRetryParams) msg.obj;
303 backupManagerService.clearBackupData(params.transportName, params.packageName);
304 break;
305 }
306
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100307 case MSG_RUN_GET_RESTORE_SETS: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100308 // Like other async operations, this is entered with the wakelock held
309 RestoreSet[] sets = null;
310 RestoreGetSetsParams params = (RestoreGetSetsParams) msg.obj;
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000311 String callerLogString = "BH/MSG_RUN_GET_RESTORE_SETS";
Artem Iglikov21510f02017-04-18 14:53:06 +0100312 try {
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000313 IBackupTransport transport =
314 params.transportClient.connectOrThrow(callerLogString);
315 sets = transport.getAvailableRestoreSets();
Artem Iglikov21510f02017-04-18 14:53:06 +0100316 // cache the result in the active session
317 synchronized (params.session) {
Bernardo Rufino12b6baf2018-02-26 14:07:01 +0000318 params.session.setRestoreSets(sets);
Artem Iglikov21510f02017-04-18 14:53:06 +0100319 }
320 if (sets == null) {
321 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
322 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100323 } catch (Exception e) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100324 Slog.e(TAG, "Error from transport getting set list: " + e.getMessage());
Artem Iglikov21510f02017-04-18 14:53:06 +0100325 } finally {
326 if (params.observer != null) {
327 try {
328 params.observer.restoreSetsAvailable(sets);
329 } catch (RemoteException re) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100330 Slog.e(TAG, "Unable to report listing to observer");
Artem Iglikov21510f02017-04-18 14:53:06 +0100331 } catch (Exception e) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100332 Slog.e(TAG, "Restore observer threw: " + e.getMessage());
Artem Iglikov21510f02017-04-18 14:53:06 +0100333 }
334 }
335
336 // Done: reset the session timeout clock
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100337 removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
Annie Menga1e8fd62018-03-15 14:45:46 +0000338 sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT,
339 mAgentTimeoutParameters.getRestoreAgentTimeoutMillis());
Artem Iglikov21510f02017-04-18 14:53:06 +0100340
Bernardo Rufino998fdaa2017-12-05 17:27:45 +0000341 params.listener.onFinished(callerLogString);
Artem Iglikovf251e352017-04-07 12:48:54 +0100342 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100343 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100344 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100345
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100346 case MSG_BACKUP_OPERATION_TIMEOUT:
347 case MSG_RESTORE_OPERATION_TIMEOUT: {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100348 Slog.d(TAG, "Timeout message received for token=" + Integer.toHexString(msg.arg1));
Artem Iglikov21510f02017-04-18 14:53:06 +0100349 backupManagerService.handleCancel(msg.arg1, false);
350 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100351 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100352
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100353 case MSG_RESTORE_SESSION_TIMEOUT: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100354 synchronized (backupManagerService) {
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100355 if (backupManagerService.getActiveRestoreSession() != null) {
Artem Iglikov21510f02017-04-18 14:53:06 +0100356 // Client app left the restore session dangling. We know that it
357 // can't be in the middle of an actual restore operation because
358 // the timeout is suspended while a restore is in progress. Clean
359 // up now.
Artem Iglikovdbe68322017-05-09 11:00:03 +0100360 Slog.w(TAG, "Restore session timed out; aborting");
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100361 backupManagerService.getActiveRestoreSession().markTimedOut();
362 post(backupManagerService.getActiveRestoreSession().new EndRestoreRunnable(
363 backupManagerService,
364 backupManagerService.getActiveRestoreSession()));
Artem Iglikov21510f02017-04-18 14:53:06 +0100365 }
366 }
367 break;
368 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100369
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100370 case MSG_FULL_CONFIRMATION_TIMEOUT: {
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100371 synchronized (backupManagerService.getAdbBackupRestoreConfirmations()) {
372 AdbParams params = backupManagerService.getAdbBackupRestoreConfirmations().get(
Artem Iglikov21510f02017-04-18 14:53:06 +0100373 msg.arg1);
374 if (params != null) {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100375 Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation");
Artem Iglikovf251e352017-04-07 12:48:54 +0100376
Artem Iglikov21510f02017-04-18 14:53:06 +0100377 // Release the waiter; timeout == completion
378 backupManagerService.signalAdbBackupRestoreCompletion(params);
Artem Iglikovf251e352017-04-07 12:48:54 +0100379
Artem Iglikov21510f02017-04-18 14:53:06 +0100380 // Remove the token from the set
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100381 backupManagerService.getAdbBackupRestoreConfirmations().delete(msg.arg1);
Artem Iglikov21510f02017-04-18 14:53:06 +0100382
383 // Report a timeout to the observer, if any
384 if (params.observer != null) {
385 try {
386 params.observer.onTimeout();
387 } catch (RemoteException e) {
Artem Iglikovf251e352017-04-07 12:48:54 +0100388 /* don't care if the app has gone away */
Artem Iglikov21510f02017-04-18 14:53:06 +0100389 }
390 }
391 } else {
Artem Iglikovdbe68322017-05-09 11:00:03 +0100392 Slog.d(TAG, "couldn't find params for token " + msg.arg1);
Artem Iglikov21510f02017-04-18 14:53:06 +0100393 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100394 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100395 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100396 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100397
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100398 case MSG_WIDGET_BROADCAST: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100399 final Intent intent = (Intent) msg.obj;
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100400 backupManagerService.getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Artem Iglikov21510f02017-04-18 14:53:06 +0100401 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100402 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100403
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100404 case MSG_REQUEST_BACKUP: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100405 BackupParams params = (BackupParams) msg.obj;
Artem Iglikovdbe68322017-05-09 11:00:03 +0100406 if (MORE_DEBUG) {
407 Slog.d(TAG, "MSG_REQUEST_BACKUP observer=" + params.observer);
Artem Iglikov21510f02017-04-18 14:53:06 +0100408 }
Artem Iglikovd6c00c72017-04-24 12:03:42 +0100409 backupManagerService.setBackupRunning(true);
410 backupManagerService.getWakelock().acquire();
Artem Iglikovf251e352017-04-07 12:48:54 +0100411
Bernardo Rufinocc714c12018-08-02 08:39:45 +0100412 KeyValueBackupTask.start(
Artem Iglikov21510f02017-04-18 14:53:06 +0100413 backupManagerService,
Bernardo Rufino2d87f452018-06-22 11:47:49 +0100414 params.transportClient,
415 params.dirName,
Bernardo Rufinoe80f2702018-08-10 17:06:15 +0100416 params.kvPackages,
Bernardo Rufino2d87f452018-06-22 11:47:49 +0100417 /* dataChangedJournal */ null,
418 params.observer,
419 params.monitor,
420 params.listener,
421 params.fullPackages,
422 /* userInitiated */ true,
423 params.nonIncrementalBackup);
Artem Iglikov21510f02017-04-18 14:53:06 +0100424 break;
Artem Iglikovf251e352017-04-07 12:48:54 +0100425 }
Artem Iglikov21510f02017-04-18 14:53:06 +0100426
Artem Iglikovc2a3d0f2017-04-28 13:16:47 +0100427 case MSG_SCHEDULE_BACKUP_PACKAGE: {
Artem Iglikov21510f02017-04-18 14:53:06 +0100428 String pkgName = (String) msg.obj;
Artem Iglikovdbe68322017-05-09 11:00:03 +0100429 if (MORE_DEBUG) {
430 Slog.d(TAG, "MSG_SCHEDULE_BACKUP_PACKAGE " + pkgName);
Artem Iglikov21510f02017-04-18 14:53:06 +0100431 }
432 backupManagerService.dataChangedImpl(pkgName);
433 break;
434 }
Artem Iglikovf251e352017-04-07 12:48:54 +0100435 }
436 }
437}