blob: 7607c741d76271c7894e81898498c70abd5ce6d4 [file] [log] [blame]
Christopher Tate487529a2009-04-29 14:03:25 -07001/*
2 * Copyright (C) 2009 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;
18
Christopher Tate181fafa2009-05-14 11:12:14 -070019import android.app.ActivityManagerNative;
Christopher Tate1bb69062010-02-19 17:02:12 -080020import android.app.ActivityThread;
Christopher Tateb6787f22009-07-02 17:40:45 -070021import android.app.AlarmManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070022import android.app.IActivityManager;
23import android.app.IApplicationThread;
24import android.app.IBackupAgent;
Christopher Tateb6787f22009-07-02 17:40:45 -070025import android.app.PendingIntent;
Christopher Tate45281862010-03-05 15:46:30 -080026import android.app.backup.RestoreSet;
27import android.app.backup.IBackupManager;
28import android.app.backup.IRestoreObserver;
29import android.app.backup.IRestoreSession;
Christopher Tate3799bc22009-05-06 16:13:56 -070030import android.content.BroadcastReceiver;
Dan Egnor87a02bc2009-06-17 02:30:10 -070031import android.content.ComponentName;
Christopher Tate487529a2009-04-29 14:03:25 -070032import android.content.Context;
33import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070034import android.content.IntentFilter;
Dan Egnor87a02bc2009-06-17 02:30:10 -070035import android.content.ServiceConnection;
Christopher Tate181fafa2009-05-14 11:12:14 -070036import android.content.pm.ApplicationInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -070037import android.content.pm.IPackageDataObserver;
Christopher Tate1bb69062010-02-19 17:02:12 -080038import android.content.pm.IPackageManager;
Christopher Tate7b881282009-06-07 13:52:37 -070039import android.content.pm.PackageInfo;
Dan Egnor87a02bc2009-06-17 02:30:10 -070040import android.content.pm.PackageManager;
Christopher Tateabce4e82009-06-18 18:35:32 -070041import android.content.pm.Signature;
Oscar Montemayora8529f62009-11-18 10:14:20 -080042import android.content.pm.PackageManager.NameNotFoundException;
Christopher Tate3799bc22009-05-06 16:13:56 -070043import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070044import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070045import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070046import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070047import android.os.Handler;
Christopher Tate44a27902010-01-27 17:15:49 -080048import android.os.HandlerThread;
Christopher Tate487529a2009-04-29 14:03:25 -070049import android.os.IBinder;
Christopher Tate44a27902010-01-27 17:15:49 -080050import android.os.Looper;
Christopher Tate487529a2009-04-29 14:03:25 -070051import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070052import android.os.ParcelFileDescriptor;
Christopher Tateb6787f22009-07-02 17:40:45 -070053import android.os.PowerManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070054import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070055import android.os.RemoteException;
Dan Egnorbb9001c2009-07-27 12:20:13 -070056import android.os.SystemClock;
Oscar Montemayora8529f62009-11-18 10:14:20 -080057import android.provider.Settings;
Dan Egnorbb9001c2009-07-27 12:20:13 -070058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
Christopher Tate487529a2009-04-29 14:03:25 -070060import android.util.SparseArray;
Christopher Tate44a27902010-01-27 17:15:49 -080061import android.util.SparseIntArray;
Christopher Tate487529a2009-04-29 14:03:25 -070062
Christopher Tated55e18a2009-09-21 10:12:59 -070063import com.android.internal.backup.BackupConstants;
Christopher Tate043dadc2009-06-02 16:11:00 -070064import com.android.internal.backup.IBackupTransport;
Oscar Montemayora8529f62009-11-18 10:14:20 -080065import com.android.internal.backup.LocalTransport;
Christopher Tate6aa41f42009-06-19 14:14:22 -070066import com.android.server.PackageManagerBackupAgent.Metadata;
Christopher Tate6785dd82009-06-18 15:58:25 -070067
Christopher Tatecde87f42009-06-12 12:55:53 -070068import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070069import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070070import java.io.FileDescriptor;
Christopher Tate1168baa2010-02-17 13:03:40 -080071import java.io.FileNotFoundException;
Christopher Tate4cc86e12009-09-21 19:36:51 -070072import java.io.FileOutputStream;
Christopher Tatec7b31e32009-06-10 15:49:30 -070073import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070074import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070075import java.io.RandomAccessFile;
Joe Onorato8ad02812009-05-13 01:41:44 -040076import java.util.ArrayList;
77import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070078import java.util.HashSet;
79import java.util.List;
Christopher Tate91717492009-06-26 21:07:13 -070080import java.util.Map;
Dan Egnorc1c49c02009-10-30 17:35:39 -070081import java.util.Random;
Christopher Tateb49ceb32010-02-08 16:22:24 -080082import java.util.Set;
Christopher Tate487529a2009-04-29 14:03:25 -070083
84class BackupManagerService extends IBackupManager.Stub {
85 private static final String TAG = "BackupManagerService";
Christopher Tate13f4a642009-09-30 20:06:45 -070086 private static final boolean DEBUG = false;
Christopher Tateaa088442009-06-16 18:25:46 -070087
Christopher Tate49401dd2009-07-01 12:34:29 -070088 // How often we perform a backup pass. Privileged external callers can
89 // trigger an immediate pass.
Christopher Tateb6787f22009-07-02 17:40:45 -070090 private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR;
Christopher Tate487529a2009-04-29 14:03:25 -070091
Dan Egnorc1c49c02009-10-30 17:35:39 -070092 // Random variation in backup scheduling time to avoid server load spikes
93 private static final int FUZZ_MILLIS = 5 * 60 * 1000;
94
Christopher Tate8031a3d2009-07-06 16:36:05 -070095 // The amount of time between the initial provisioning of the device and
96 // the first backup pass.
97 private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR;
98
Christopher Tate45281862010-03-05 15:46:30 -080099 private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN";
100 private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT";
101 private static final String RUN_CLEAR_ACTION = "android.app.backup.intent.CLEAR";
Christopher Tate487529a2009-04-29 14:03:25 -0700102 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -0700103 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700104 private static final int MSG_RUN_RESTORE = 3;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700105 private static final int MSG_RUN_CLEAR = 4;
Christopher Tate4cc86e12009-09-21 19:36:51 -0700106 private static final int MSG_RUN_INITIALIZE = 5;
Christopher Tate44a27902010-01-27 17:15:49 -0800107 private static final int MSG_TIMEOUT = 6;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700108
109 // Timeout interval for deciding that a bind or clear-data has taken too long
110 static final long TIMEOUT_INTERVAL = 10 * 1000;
111
Christopher Tate44a27902010-01-27 17:15:49 -0800112 // Timeout intervals for agent backup & restore operations
113 static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000;
114 static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000;
115
Christopher Tate487529a2009-04-29 14:03:25 -0700116 private Context mContext;
117 private PackageManager mPackageManager;
Christopher Tate1bb69062010-02-19 17:02:12 -0800118 IPackageManager mPackageManagerBinder;
Christopher Tate6ef58a12009-06-29 14:56:28 -0700119 private IActivityManager mActivityManager;
Christopher Tateb6787f22009-07-02 17:40:45 -0700120 private PowerManager mPowerManager;
121 private AlarmManager mAlarmManager;
Christopher Tate44a27902010-01-27 17:15:49 -0800122 IBackupManager mBackupManagerBinder;
Christopher Tateb6787f22009-07-02 17:40:45 -0700123
Christopher Tate73e02522009-07-15 14:18:26 -0700124 boolean mEnabled; // access to this is synchronized on 'this'
125 boolean mProvisioned;
Christopher Tatecce9da52010-02-03 15:11:15 -0800126 boolean mAutoRestore;
Christopher Tate73e02522009-07-15 14:18:26 -0700127 PowerManager.WakeLock mWakelock;
Christopher Tate44a27902010-01-27 17:15:49 -0800128 HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND);
129 BackupHandler mBackupHandler;
Christopher Tate4cc86e12009-09-21 19:36:51 -0700130 PendingIntent mRunBackupIntent, mRunInitIntent;
131 BroadcastReceiver mRunBackupReceiver, mRunInitReceiver;
Christopher Tate487529a2009-04-29 14:03:25 -0700132 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate73e02522009-07-15 14:18:26 -0700133 final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
Christopher Tate181fafa2009-05-14 11:12:14 -0700134 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -0700135 // set of backup services that have pending changes
Christopher Tate73e02522009-07-15 14:18:26 -0700136 class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -0700137 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -0700138 public boolean fullBackup;
Christopher Tateaa088442009-06-16 18:25:46 -0700139
Christopher Tate181fafa2009-05-14 11:12:14 -0700140 BackupRequest(ApplicationInfo app, boolean isFull) {
141 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -0700142 fullBackup = isFull;
143 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700144
145 public String toString() {
146 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
147 }
Christopher Tate46758122009-05-06 11:22:00 -0700148 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400149 // Backups that we haven't started yet.
Christopher Tate73e02522009-07-15 14:18:26 -0700150 HashMap<ApplicationInfo,BackupRequest> mPendingBackups
Christopher Tate181fafa2009-05-14 11:12:14 -0700151 = new HashMap<ApplicationInfo,BackupRequest>();
Christopher Tate5cb400b2009-06-25 16:03:14 -0700152
153 // Pseudoname that we use for the Package Manager metadata "package"
Christopher Tate73e02522009-07-15 14:18:26 -0700154 static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
Christopher Tate6aa41f42009-06-19 14:14:22 -0700155
156 // locking around the pending-backup management
Christopher Tate73e02522009-07-15 14:18:26 -0700157 final Object mQueueLock = new Object();
Christopher Tate487529a2009-04-29 14:03:25 -0700158
Christopher Tate043dadc2009-06-02 16:11:00 -0700159 // The thread performing the sequence of queued backups binds to each app's agent
160 // in succession. Bind notifications are asynchronously delivered through the
161 // Activity Manager; use this lock object to signal when a requested binding has
162 // completed.
Christopher Tate73e02522009-07-15 14:18:26 -0700163 final Object mAgentConnectLock = new Object();
164 IBackupAgent mConnectedAgent;
165 volatile boolean mConnecting;
Christopher Tate55f931a2009-09-29 17:17:34 -0700166 volatile long mLastBackupPass;
167 volatile long mNextBackupPass;
Christopher Tate043dadc2009-06-02 16:11:00 -0700168
Christopher Tate55f931a2009-09-29 17:17:34 -0700169 // A similar synchronization mechanism around clearing apps' data for restore
Christopher Tate73e02522009-07-15 14:18:26 -0700170 final Object mClearDataLock = new Object();
171 volatile boolean mClearingData;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700172
Christopher Tate91717492009-06-26 21:07:13 -0700173 // Transport bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700174 final HashMap<String,IBackupTransport> mTransports
Christopher Tate91717492009-06-26 21:07:13 -0700175 = new HashMap<String,IBackupTransport>();
Christopher Tate73e02522009-07-15 14:18:26 -0700176 String mCurrentTransport;
177 IBackupTransport mLocalTransport, mGoogleTransport;
Christopher Tate80202c82010-01-25 19:37:47 -0800178 ActiveRestoreSession mActiveRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -0700179
Christopher Tate73e02522009-07-15 14:18:26 -0700180 class RestoreParams {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700181 public IBackupTransport transport;
182 public IRestoreObserver observer;
Dan Egnor156411d2009-06-26 13:20:02 -0700183 public long token;
Christopher Tate84725812010-02-04 15:52:40 -0800184 public PackageInfo pkgInfo;
Christopher Tate1bb69062010-02-19 17:02:12 -0800185 public int pmToken; // in post-install restore, the PM's token for this transaction
Christopher Tate84725812010-02-04 15:52:40 -0800186
187 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs,
Christopher Tate1bb69062010-02-19 17:02:12 -0800188 long _token, PackageInfo _pkg, int _pmToken) {
Christopher Tate84725812010-02-04 15:52:40 -0800189 transport = _transport;
190 observer = _obs;
191 token = _token;
192 pkgInfo = _pkg;
Christopher Tate1bb69062010-02-19 17:02:12 -0800193 pmToken = _pmToken;
Christopher Tate84725812010-02-04 15:52:40 -0800194 }
Christopher Tate7d562ec2009-06-25 18:03:43 -0700195
Dan Egnor156411d2009-06-26 13:20:02 -0700196 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700197 transport = _transport;
198 observer = _obs;
Dan Egnor156411d2009-06-26 13:20:02 -0700199 token = _token;
Christopher Tate84725812010-02-04 15:52:40 -0800200 pkgInfo = null;
Christopher Tate1bb69062010-02-19 17:02:12 -0800201 pmToken = 0;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700202 }
203 }
204
Christopher Tate73e02522009-07-15 14:18:26 -0700205 class ClearParams {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700206 public IBackupTransport transport;
207 public PackageInfo packageInfo;
208
209 ClearParams(IBackupTransport _transport, PackageInfo _info) {
210 transport = _transport;
211 packageInfo = _info;
212 }
213 }
214
Christopher Tate44a27902010-01-27 17:15:49 -0800215 // Bookkeeping of in-flight operations for timeout etc. purposes. The operation
216 // token is the index of the entry in the pending-operations list.
217 static final int OP_PENDING = 0;
218 static final int OP_ACKNOWLEDGED = 1;
219 static final int OP_TIMEOUT = -1;
220
221 final SparseIntArray mCurrentOperations = new SparseIntArray();
222 final Object mCurrentOpLock = new Object();
223 final Random mTokenGenerator = new Random();
224
Christopher Tate5cb400b2009-06-25 16:03:14 -0700225 // Where we keep our journal files and other bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700226 File mBaseStateDir;
227 File mDataDir;
228 File mJournalDir;
229 File mJournal;
Christopher Tate73e02522009-07-15 14:18:26 -0700230
Christopher Tate84725812010-02-04 15:52:40 -0800231 // Keep a log of all the apps we've ever backed up, and what the
232 // dataset tokens are for both the current backup dataset and
233 // the ancestral dataset.
Christopher Tate73e02522009-07-15 14:18:26 -0700234 private File mEverStored;
Christopher Tate73e02522009-07-15 14:18:26 -0700235 HashSet<String> mEverStoredApps = new HashSet<String>();
236
Christopher Tateb49ceb32010-02-08 16:22:24 -0800237 static final int CURRENT_ANCESTRAL_RECORD_VERSION = 1; // increment when the schema changes
Christopher Tate84725812010-02-04 15:52:40 -0800238 File mTokenFile;
Christopher Tateb49ceb32010-02-08 16:22:24 -0800239 Set<String> mAncestralPackages = null;
Christopher Tate84725812010-02-04 15:52:40 -0800240 long mAncestralToken = 0;
241 long mCurrentToken = 0;
242
Christopher Tate4cc86e12009-09-21 19:36:51 -0700243 // Persistently track the need to do a full init
244 static final String INIT_SENTINEL_FILE_NAME = "_need_init_";
245 HashSet<String> mPendingInits = new HashSet<String>(); // transport names
Christopher Tateaa088442009-06-16 18:25:46 -0700246
Christopher Tate44a27902010-01-27 17:15:49 -0800247 // ----- Asynchronous backup/restore handler thread -----
248
249 private class BackupHandler extends Handler {
250 public BackupHandler(Looper looper) {
251 super(looper);
252 }
253
254 public void handleMessage(Message msg) {
255
256 switch (msg.what) {
257 case MSG_RUN_BACKUP:
258 {
259 mLastBackupPass = System.currentTimeMillis();
260 mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL;
261
262 IBackupTransport transport = getTransport(mCurrentTransport);
263 if (transport == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800264 Slog.v(TAG, "Backup requested but no transport available");
Christopher Tate44a27902010-01-27 17:15:49 -0800265 mWakelock.release();
266 break;
267 }
268
269 // snapshot the pending-backup set and work on that
270 ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
Christopher Tatec61da312010-02-05 10:41:27 -0800271 File oldJournal = mJournal;
Christopher Tate44a27902010-01-27 17:15:49 -0800272 synchronized (mQueueLock) {
Christopher Tatec61da312010-02-05 10:41:27 -0800273 // Do we have any work to do? Construct the work queue
274 // then release the synchronization lock to actually run
275 // the backup.
Christopher Tate44a27902010-01-27 17:15:49 -0800276 if (mPendingBackups.size() > 0) {
277 for (BackupRequest b: mPendingBackups.values()) {
278 queue.add(b);
279 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800280 if (DEBUG) Slog.v(TAG, "clearing pending backups");
Christopher Tate44a27902010-01-27 17:15:49 -0800281 mPendingBackups.clear();
282
283 // Start a new backup-queue journal file too
Christopher Tate44a27902010-01-27 17:15:49 -0800284 mJournal = null;
285
Christopher Tate44a27902010-01-27 17:15:49 -0800286 }
287 }
Christopher Tatec61da312010-02-05 10:41:27 -0800288
289 if (queue.size() > 0) {
290 // At this point, we have started a new journal file, and the old
291 // file identity is being passed to the backup processing thread.
292 // When it completes successfully, that old journal file will be
293 // deleted. If we crash prior to that, the old journal is parsed
294 // at next boot and the journaled requests fulfilled.
295 (new PerformBackupTask(transport, queue, oldJournal)).run();
296 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800297 Slog.v(TAG, "Backup requested but nothing pending");
Christopher Tatec61da312010-02-05 10:41:27 -0800298 mWakelock.release();
299 }
Christopher Tate44a27902010-01-27 17:15:49 -0800300 break;
301 }
302
303 case MSG_RUN_FULL_BACKUP:
304 break;
305
306 case MSG_RUN_RESTORE:
307 {
308 RestoreParams params = (RestoreParams)msg.obj;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800309 Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
Christopher Tate44a27902010-01-27 17:15:49 -0800310 (new PerformRestoreTask(params.transport, params.observer,
Christopher Tate1bb69062010-02-19 17:02:12 -0800311 params.token, params.pkgInfo, params.pmToken)).run();
Christopher Tate44a27902010-01-27 17:15:49 -0800312 break;
313 }
314
315 case MSG_RUN_CLEAR:
316 {
317 ClearParams params = (ClearParams)msg.obj;
318 (new PerformClearTask(params.transport, params.packageInfo)).run();
319 break;
320 }
321
322 case MSG_RUN_INITIALIZE:
323 {
324 HashSet<String> queue;
325
326 // Snapshot the pending-init queue and work on that
327 synchronized (mQueueLock) {
328 queue = new HashSet<String>(mPendingInits);
329 mPendingInits.clear();
330 }
331
332 (new PerformInitializeTask(queue)).run();
333 break;
334 }
335
336 case MSG_TIMEOUT:
337 {
338 synchronized (mCurrentOpLock) {
339 final int token = msg.arg1;
340 int state = mCurrentOperations.get(token, OP_TIMEOUT);
341 if (state == OP_PENDING) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800342 if (DEBUG) Slog.v(TAG, "TIMEOUT: token=" + token);
Christopher Tate44a27902010-01-27 17:15:49 -0800343 mCurrentOperations.put(token, OP_TIMEOUT);
344 }
345 mCurrentOpLock.notifyAll();
346 }
347 break;
348 }
349 }
350 }
351 }
352
353 // ----- Main service implementation -----
354
Christopher Tate487529a2009-04-29 14:03:25 -0700355 public BackupManagerService(Context context) {
356 mContext = context;
357 mPackageManager = context.getPackageManager();
Christopher Tate1bb69062010-02-19 17:02:12 -0800358 mPackageManagerBinder = ActivityThread.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700359 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700360
Christopher Tateb6787f22009-07-02 17:40:45 -0700361 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
362 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
363
Christopher Tate44a27902010-01-27 17:15:49 -0800364 mBackupManagerBinder = asInterface(asBinder());
365
366 // spin up the backup/restore handler thread
367 mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND);
368 mHandlerThread.start();
369 mBackupHandler = new BackupHandler(mHandlerThread.getLooper());
370
Christopher Tate22b87872009-05-04 16:41:53 -0700371 // Set up our bookkeeping
Christopher Tateb6787f22009-07-02 17:40:45 -0700372 boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborncf098292009-07-01 19:55:20 -0700373 Settings.Secure.BACKUP_ENABLED, 0) != 0;
Christopher Tate8031a3d2009-07-06 16:36:05 -0700374 mProvisioned = Settings.Secure.getInt(context.getContentResolver(),
Joe Onoratoab9a2a52009-07-27 08:56:39 -0700375 Settings.Secure.BACKUP_PROVISIONED, 0) != 0;
Christopher Tatecce9da52010-02-03 15:11:15 -0800376 mAutoRestore = Settings.Secure.getInt(context.getContentResolver(),
Christopher Tate5035fda2010-02-25 18:01:14 -0800377 Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0;
Oscar Montemayora8529f62009-11-18 10:14:20 -0800378 // If Encrypted file systems is enabled or disabled, this call will return the
379 // correct directory.
380 mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup");
381 mBaseStateDir.mkdirs();
Christopher Tatef4172472009-05-05 15:50:03 -0700382 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700383
Christopher Tate4cc86e12009-09-21 19:36:51 -0700384 // Alarm receivers for scheduled backups & initialization operations
Christopher Tateb6787f22009-07-02 17:40:45 -0700385 mRunBackupReceiver = new RunBackupReceiver();
Christopher Tate4cc86e12009-09-21 19:36:51 -0700386 IntentFilter filter = new IntentFilter();
387 filter.addAction(RUN_BACKUP_ACTION);
388 context.registerReceiver(mRunBackupReceiver, filter,
389 android.Manifest.permission.BACKUP, null);
390
391 mRunInitReceiver = new RunInitializeReceiver();
392 filter = new IntentFilter();
393 filter.addAction(RUN_INITIALIZE_ACTION);
394 context.registerReceiver(mRunInitReceiver, filter,
395 android.Manifest.permission.BACKUP, null);
Christopher Tateb6787f22009-07-02 17:40:45 -0700396
397 Intent backupIntent = new Intent(RUN_BACKUP_ACTION);
Christopher Tateb6787f22009-07-02 17:40:45 -0700398 backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
399 mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0);
400
Christopher Tate4cc86e12009-09-21 19:36:51 -0700401 Intent initIntent = new Intent(RUN_INITIALIZE_ACTION);
402 backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
403 mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0);
404
Christopher Tatecde87f42009-06-12 12:55:53 -0700405 // Set up the backup-request journaling
Christopher Tate5cb400b2009-06-25 16:03:14 -0700406 mJournalDir = new File(mBaseStateDir, "pending");
407 mJournalDir.mkdirs(); // creates mBaseStateDir along the way
Dan Egnor852f8e42009-09-30 11:20:45 -0700408 mJournal = null; // will be created on first use
Christopher Tatecde87f42009-06-12 12:55:53 -0700409
Christopher Tate73e02522009-07-15 14:18:26 -0700410 // Set up the various sorts of package tracking we do
411 initPackageTracking();
412
Christopher Tateabce4e82009-06-18 18:35:32 -0700413 // Build our mapping of uid to backup client services. This implicitly
414 // schedules a backup pass on the Package Manager metadata the first
415 // time anything needs to be backed up.
Christopher Tate3799bc22009-05-06 16:13:56 -0700416 synchronized (mBackupParticipants) {
417 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700418 }
419
Dan Egnor87a02bc2009-06-17 02:30:10 -0700420 // Set up our transport options and initialize the default transport
421 // TODO: Have transports register themselves somehow?
422 // TODO: Don't create transports that we don't need to?
Dan Egnor87a02bc2009-06-17 02:30:10 -0700423 mLocalTransport = new LocalTransport(context); // This is actually pretty cheap
Christopher Tate91717492009-06-26 21:07:13 -0700424 ComponentName localName = new ComponentName(context, LocalTransport.class);
425 registerTransport(localName.flattenToShortString(), mLocalTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700426
Christopher Tate91717492009-06-26 21:07:13 -0700427 mGoogleTransport = null;
Dianne Hackborncf098292009-07-01 19:55:20 -0700428 mCurrentTransport = Settings.Secure.getString(context.getContentResolver(),
429 Settings.Secure.BACKUP_TRANSPORT);
430 if ("".equals(mCurrentTransport)) {
431 mCurrentTransport = null;
Christopher Tatece0bf062009-07-01 11:43:53 -0700432 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800433 if (DEBUG) Slog.v(TAG, "Starting with transport " + mCurrentTransport);
Christopher Tate91717492009-06-26 21:07:13 -0700434
435 // Attach to the Google backup transport. When this comes up, it will set
436 // itself as the current transport because we explicitly reset mCurrentTransport
437 // to null.
Dan Egnor87a02bc2009-06-17 02:30:10 -0700438 Intent intent = new Intent().setComponent(new ComponentName(
439 "com.google.android.backup",
440 "com.google.android.backup.BackupTransportService"));
441 context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE);
Christopher Tateaa088442009-06-16 18:25:46 -0700442
Christopher Tatecde87f42009-06-12 12:55:53 -0700443 // Now that we know about valid backup participants, parse any
Christopher Tate49401dd2009-07-01 12:34:29 -0700444 // leftover journal files into the pending backup set
Christopher Tatecde87f42009-06-12 12:55:53 -0700445 parseLeftoverJournals();
446
Christopher Tateb6787f22009-07-02 17:40:45 -0700447 // Power management
448 mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backup");
449
450 // Start the backup passes going
451 setBackupEnabled(areEnabled);
452 }
453
454 private class RunBackupReceiver extends BroadcastReceiver {
455 public void onReceive(Context context, Intent intent) {
456 if (RUN_BACKUP_ACTION.equals(intent.getAction())) {
Christopher Tateb6787f22009-07-02 17:40:45 -0700457 synchronized (mQueueLock) {
Christopher Tate4cc86e12009-09-21 19:36:51 -0700458 if (mPendingInits.size() > 0) {
459 // If there are pending init operations, we process those
460 // and then settle into the usual periodic backup schedule.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800461 if (DEBUG) Slog.v(TAG, "Init pending at scheduled backup");
Christopher Tate4cc86e12009-09-21 19:36:51 -0700462 try {
463 mAlarmManager.cancel(mRunInitIntent);
464 mRunInitIntent.send();
465 } catch (PendingIntent.CanceledException ce) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800466 Slog.e(TAG, "Run init intent cancelled");
Christopher Tate4cc86e12009-09-21 19:36:51 -0700467 // can't really do more than bail here
468 }
469 } else {
Christopher Tatec2af5d32010-02-02 15:18:58 -0800470 // Don't run backups now if we're disabled or not yet
471 // fully set up.
472 if (mEnabled && mProvisioned) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 if (DEBUG) Slog.v(TAG, "Running a backup pass");
Christopher Tate4cc86e12009-09-21 19:36:51 -0700474
475 // Acquire the wakelock and pass it to the backup thread. it will
476 // be released once backup concludes.
477 mWakelock.acquire();
478
479 Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
480 mBackupHandler.sendMessage(msg);
481 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800482 Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned);
Christopher Tate4cc86e12009-09-21 19:36:51 -0700483 }
484 }
485 }
486 }
487 }
488 }
489
490 private class RunInitializeReceiver extends BroadcastReceiver {
491 public void onReceive(Context context, Intent intent) {
492 if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) {
493 synchronized (mQueueLock) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800494 if (DEBUG) Slog.v(TAG, "Running a device init");
Christopher Tate4cc86e12009-09-21 19:36:51 -0700495
496 // Acquire the wakelock and pass it to the init thread. it will
497 // be released once init concludes.
Christopher Tateb6787f22009-07-02 17:40:45 -0700498 mWakelock.acquire();
499
Christopher Tate4cc86e12009-09-21 19:36:51 -0700500 Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE);
Christopher Tateb6787f22009-07-02 17:40:45 -0700501 mBackupHandler.sendMessage(msg);
502 }
503 }
Christopher Tate49401dd2009-07-01 12:34:29 -0700504 }
Christopher Tateb6787f22009-07-02 17:40:45 -0700505 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700506
Christopher Tate73e02522009-07-15 14:18:26 -0700507 private void initPackageTracking() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 if (DEBUG) Slog.v(TAG, "Initializing package tracking");
Christopher Tate73e02522009-07-15 14:18:26 -0700509
Christopher Tate84725812010-02-04 15:52:40 -0800510 // Remember our ancestral dataset
511 mTokenFile = new File(mBaseStateDir, "ancestral");
512 try {
513 RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r");
Christopher Tateb49ceb32010-02-08 16:22:24 -0800514 int version = tf.readInt();
515 if (version == CURRENT_ANCESTRAL_RECORD_VERSION) {
516 mAncestralToken = tf.readLong();
517 mCurrentToken = tf.readLong();
518
519 int numPackages = tf.readInt();
520 if (numPackages >= 0) {
521 mAncestralPackages = new HashSet<String>();
522 for (int i = 0; i < numPackages; i++) {
523 String pkgName = tf.readUTF();
524 mAncestralPackages.add(pkgName);
525 }
526 }
527 }
Christopher Tate1168baa2010-02-17 13:03:40 -0800528 } catch (FileNotFoundException fnf) {
529 // Probably innocuous
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.v(TAG, "No ancestral data");
Christopher Tate84725812010-02-04 15:52:40 -0800531 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.w(TAG, "Unable to read token file", e);
Christopher Tate84725812010-02-04 15:52:40 -0800533 }
534
Christopher Tatee97e8072009-07-15 16:45:50 -0700535 // Keep a log of what apps we've ever backed up. Because we might have
536 // rebooted in the middle of an operation that was removing something from
537 // this log, we sanity-check its contents here and reconstruct it.
Christopher Tate73e02522009-07-15 14:18:26 -0700538 mEverStored = new File(mBaseStateDir, "processed");
Christopher Tatee97e8072009-07-15 16:45:50 -0700539 File tempProcessedFile = new File(mBaseStateDir, "processed.new");
Christopher Tate73e02522009-07-15 14:18:26 -0700540
Christopher Tatee97e8072009-07-15 16:45:50 -0700541 // If we were in the middle of removing something from the ever-backed-up
542 // file, there might be a transient "processed.new" file still present.
Dan Egnor852f8e42009-09-30 11:20:45 -0700543 // Ignore it -- we'll validate "processed" against the current package set.
Christopher Tatee97e8072009-07-15 16:45:50 -0700544 if (tempProcessedFile.exists()) {
545 tempProcessedFile.delete();
546 }
547
Dan Egnor852f8e42009-09-30 11:20:45 -0700548 // If there are previous contents, parse them out then start a new
549 // file to continue the recordkeeping.
550 if (mEverStored.exists()) {
551 RandomAccessFile temp = null;
552 RandomAccessFile in = null;
553
554 try {
555 temp = new RandomAccessFile(tempProcessedFile, "rws");
556 in = new RandomAccessFile(mEverStored, "r");
557
558 while (true) {
559 PackageInfo info;
560 String pkg = in.readUTF();
561 try {
562 info = mPackageManager.getPackageInfo(pkg, 0);
563 mEverStoredApps.add(pkg);
564 temp.writeUTF(pkg);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 if (DEBUG) Slog.v(TAG, " + " + pkg);
Dan Egnor852f8e42009-09-30 11:20:45 -0700566 } catch (NameNotFoundException e) {
567 // nope, this package was uninstalled; don't include it
Joe Onorato8a9b2202010-02-26 18:56:32 -0800568 if (DEBUG) Slog.v(TAG, " - " + pkg);
Dan Egnor852f8e42009-09-30 11:20:45 -0700569 }
570 }
571 } catch (EOFException e) {
572 // Once we've rewritten the backup history log, atomically replace the
573 // old one with the new one then reopen the file for continuing use.
574 if (!tempProcessedFile.renameTo(mEverStored)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800575 Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored);
Dan Egnor852f8e42009-09-30 11:20:45 -0700576 }
577 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800578 Slog.e(TAG, "Error in processed file", e);
Dan Egnor852f8e42009-09-30 11:20:45 -0700579 } finally {
580 try { if (temp != null) temp.close(); } catch (IOException e) {}
581 try { if (in != null) in.close(); } catch (IOException e) {}
582 }
583 }
584
Christopher Tate73e02522009-07-15 14:18:26 -0700585 // Register for broadcasts about package install, etc., so we can
586 // update the provider list.
587 IntentFilter filter = new IntentFilter();
588 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
589 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
590 filter.addDataScheme("package");
591 mContext.registerReceiver(mBroadcastReceiver, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800592 // Register for events related to sdcard installation.
593 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800594 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
595 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800596 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
Christopher Tate73e02522009-07-15 14:18:26 -0700597 }
598
Christopher Tatecde87f42009-06-12 12:55:53 -0700599 private void parseLeftoverJournals() {
Dan Egnor852f8e42009-09-30 11:20:45 -0700600 for (File f : mJournalDir.listFiles()) {
601 if (mJournal == null || f.compareTo(mJournal) != 0) {
602 // This isn't the current journal, so it must be a leftover. Read
603 // out the package names mentioned there and schedule them for
604 // backup.
605 RandomAccessFile in = null;
606 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800607 Slog.i(TAG, "Found stale backup journal, scheduling:");
Dan Egnor852f8e42009-09-30 11:20:45 -0700608 in = new RandomAccessFile(f, "r");
609 while (true) {
610 String packageName = in.readUTF();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800611 Slog.i(TAG, " + " + packageName);
Dan Egnor852f8e42009-09-30 11:20:45 -0700612 dataChanged(packageName);
Christopher Tatecde87f42009-06-12 12:55:53 -0700613 }
Dan Egnor852f8e42009-09-30 11:20:45 -0700614 } catch (EOFException e) {
615 // no more data; we're done
616 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 Slog.e(TAG, "Can't read " + f, e);
Dan Egnor852f8e42009-09-30 11:20:45 -0700618 } finally {
619 // close/delete the file
620 try { if (in != null) in.close(); } catch (IOException e) {}
621 f.delete();
Christopher Tatecde87f42009-06-12 12:55:53 -0700622 }
623 }
624 }
625 }
626
Christopher Tate4cc86e12009-09-21 19:36:51 -0700627 // Maintain persistent state around whether need to do an initialize operation.
628 // Must be called with the queue lock held.
629 void recordInitPendingLocked(boolean isPending, String transportName) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800630 if (DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending
Christopher Tate4cc86e12009-09-21 19:36:51 -0700631 + " on transport " + transportName);
632 try {
633 IBackupTransport transport = getTransport(transportName);
634 String transportDirName = transport.transportDirName();
635 File stateDir = new File(mBaseStateDir, transportDirName);
636 File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME);
637
638 if (isPending) {
639 // We need an init before we can proceed with sending backup data.
640 // Record that with an entry in our set of pending inits, as well as
641 // journaling it via creation of a sentinel file.
642 mPendingInits.add(transportName);
643 try {
644 (new FileOutputStream(initPendingFile)).close();
645 } catch (IOException ioe) {
646 // Something is badly wrong with our permissions; just try to move on
647 }
648 } else {
649 // No more initialization needed; wipe the journal and reset our state.
650 initPendingFile.delete();
651 mPendingInits.remove(transportName);
652 }
653 } catch (RemoteException e) {
654 // can't happen; the transport is local
655 }
656 }
657
Christopher Tated55e18a2009-09-21 10:12:59 -0700658 // Reset all of our bookkeeping, in response to having been told that
659 // the backend data has been wiped [due to idle expiry, for example],
660 // so we must re-upload all saved settings.
661 void resetBackupState(File stateFileDir) {
662 synchronized (mQueueLock) {
663 // Wipe the "what we've ever backed up" tracking
Christopher Tated55e18a2009-09-21 10:12:59 -0700664 mEverStoredApps.clear();
Dan Egnor852f8e42009-09-30 11:20:45 -0700665 mEverStored.delete();
Christopher Tated55e18a2009-09-21 10:12:59 -0700666
Christopher Tate84725812010-02-04 15:52:40 -0800667 mCurrentToken = 0;
668 writeRestoreTokens();
669
Christopher Tated55e18a2009-09-21 10:12:59 -0700670 // Remove all the state files
671 for (File sf : stateFileDir.listFiles()) {
Christopher Tate4cc86e12009-09-21 19:36:51 -0700672 // ... but don't touch the needs-init sentinel
673 if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) {
674 sf.delete();
675 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700676 }
677
678 // Enqueue a new backup of every participant
679 int N = mBackupParticipants.size();
680 for (int i=0; i<N; i++) {
681 int uid = mBackupParticipants.keyAt(i);
682 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
683 for (ApplicationInfo app: participants) {
Dan Egnor852f8e42009-09-30 11:20:45 -0700684 dataChanged(app.packageName);
Christopher Tated55e18a2009-09-21 10:12:59 -0700685 }
686 }
687 }
688 }
689
Christopher Tatedfa47b56e2009-12-22 16:01:32 -0800690 // Add a transport to our set of available backends. If 'transport' is null, this
691 // is an unregistration, and the transport's entry is removed from our bookkeeping.
Christopher Tate91717492009-06-26 21:07:13 -0700692 private void registerTransport(String name, IBackupTransport transport) {
693 synchronized (mTransports) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800694 if (DEBUG) Slog.v(TAG, "Registering transport " + name + " = " + transport);
Christopher Tatedfa47b56e2009-12-22 16:01:32 -0800695 if (transport != null) {
696 mTransports.put(name, transport);
697 } else {
698 mTransports.remove(name);
Christopher Tateb0dcaaf2010-01-29 16:27:04 -0800699 if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) {
Christopher Tatedfa47b56e2009-12-22 16:01:32 -0800700 mCurrentTransport = null;
701 }
702 // Nothing further to do in the unregistration case
703 return;
704 }
Christopher Tate91717492009-06-26 21:07:13 -0700705 }
Christopher Tate4cc86e12009-09-21 19:36:51 -0700706
707 // If the init sentinel file exists, we need to be sure to perform the init
708 // as soon as practical. We also create the state directory at registration
709 // time to ensure it's present from the outset.
710 try {
711 String transportName = transport.transportDirName();
712 File stateDir = new File(mBaseStateDir, transportName);
713 stateDir.mkdirs();
714
715 File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME);
716 if (initSentinel.exists()) {
717 synchronized (mQueueLock) {
718 mPendingInits.add(transportName);
719
720 // TODO: pick a better starting time than now + 1 minute
721 long delay = 1000 * 60; // one minute, in milliseconds
722 mAlarmManager.set(AlarmManager.RTC_WAKEUP,
723 System.currentTimeMillis() + delay, mRunInitIntent);
724 }
725 }
726 } catch (RemoteException e) {
727 // can't happen, the transport is local
728 }
Christopher Tate91717492009-06-26 21:07:13 -0700729 }
730
Christopher Tate3799bc22009-05-06 16:13:56 -0700731 // ----- Track installation/removal of packages -----
732 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
733 public void onReceive(Context context, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 if (DEBUG) Slog.d(TAG, "Received broadcast " + intent);
Christopher Tate3799bc22009-05-06 16:13:56 -0700735
Christopher Tate3799bc22009-05-06 16:13:56 -0700736 String action = intent.getAction();
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800737 boolean replacing = false;
738 boolean added = false;
739 Bundle extras = intent.getExtras();
740 String pkgList[] = null;
741 if (Intent.ACTION_PACKAGE_ADDED.equals(action) ||
742 Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
743 Uri uri = intent.getData();
744 if (uri == null) {
745 return;
746 }
747 String pkgName = uri.getSchemeSpecificPart();
748 if (pkgName != null) {
749 pkgList = new String[] { pkgName };
750 }
751 added = Intent.ACTION_PACKAGE_ADDED.equals(action);
752 replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800753 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800754 added = true;
755 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800756 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800757 added = false;
758 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
759 }
760 if (pkgList == null || pkgList.length == 0) {
761 return;
762 }
763 if (added) {
Christopher Tate3799bc22009-05-06 16:13:56 -0700764 synchronized (mBackupParticipants) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800765 for (String pkgName : pkgList) {
766 if (replacing) {
767 // The package was just upgraded
768 updatePackageParticipantsLocked(pkgName);
769 } else {
770 // The package was just added
771 addPackageParticipantsLocked(pkgName);
772 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700773 }
774 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800775 } else {
776 if (replacing) {
Christopher Tate3799bc22009-05-06 16:13:56 -0700777 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
778 } else {
779 synchronized (mBackupParticipants) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800780 for (String pkgName : pkgList) {
781 removePackageParticipantsLocked(pkgName);
782 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700783 }
784 }
785 }
786 }
787 };
788
Dan Egnor87a02bc2009-06-17 02:30:10 -0700789 // ----- Track connection to GoogleBackupTransport service -----
790 ServiceConnection mGoogleConnection = new ServiceConnection() {
791 public void onServiceConnected(ComponentName name, IBinder service) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 if (DEBUG) Slog.v(TAG, "Connected to Google transport");
Dan Egnor87a02bc2009-06-17 02:30:10 -0700793 mGoogleTransport = IBackupTransport.Stub.asInterface(service);
Christopher Tate91717492009-06-26 21:07:13 -0700794 registerTransport(name.flattenToShortString(), mGoogleTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700795 }
796
797 public void onServiceDisconnected(ComponentName name) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800798 if (DEBUG) Slog.v(TAG, "Disconnected from Google transport");
Dan Egnor87a02bc2009-06-17 02:30:10 -0700799 mGoogleTransport = null;
Christopher Tate91717492009-06-26 21:07:13 -0700800 registerTransport(name.flattenToShortString(), null);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700801 }
802 };
803
Christopher Tate181fafa2009-05-14 11:12:14 -0700804 // Add the backup agents in the given package to our set of known backup participants.
805 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700806 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700807 // Look for apps that define the android:backupAgent attribute
Joe Onorato8a9b2202010-02-26 18:56:32 -0800808 if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700809 List<PackageInfo> targetApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700810 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700811 }
812
Christopher Tate181fafa2009-05-14 11:12:14 -0700813 private void addPackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700814 List<PackageInfo> targetPkgs) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700815 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 Slog.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
Dan Egnorefe52642009-06-24 00:16:33 -0700817 for (PackageInfo p : targetPkgs) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 Slog.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName
Christopher Tate5e1ab332009-09-01 20:32:49 -0700819 + " uid=" + p.applicationInfo.uid
820 + " killAfterRestore="
821 + (((p.applicationInfo.flags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) ? "true" : "false")
Christopher Tate5e1ab332009-09-01 20:32:49 -0700822 );
Christopher Tate181fafa2009-05-14 11:12:14 -0700823 }
824 }
825
Dan Egnorefe52642009-06-24 00:16:33 -0700826 for (PackageInfo pkg : targetPkgs) {
827 if (packageName == null || pkg.packageName.equals(packageName)) {
828 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700829 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700830 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700831 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700832 mBackupParticipants.put(uid, set);
833 }
Dan Egnorefe52642009-06-24 00:16:33 -0700834 set.add(pkg.applicationInfo);
Christopher Tate73e02522009-07-15 14:18:26 -0700835
836 // If we've never seen this app before, schedule a backup for it
837 if (!mEverStoredApps.contains(pkg.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800838 if (DEBUG) Slog.i(TAG, "New app " + pkg.packageName
Christopher Tate73e02522009-07-15 14:18:26 -0700839 + " never backed up; scheduling");
Dan Egnor852f8e42009-09-30 11:20:45 -0700840 dataChanged(pkg.packageName);
Christopher Tate73e02522009-07-15 14:18:26 -0700841 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700842 }
Christopher Tate487529a2009-04-29 14:03:25 -0700843 }
844 }
845
Christopher Tate6785dd82009-06-18 15:58:25 -0700846 // Remove the given package's entry from our known active set. If
847 // 'packageName' is null, *all* participating apps will be removed.
Christopher Tate3799bc22009-05-06 16:13:56 -0700848 void removePackageParticipantsLocked(String packageName) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800849 if (DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700850 List<PackageInfo> allApps = null;
Christopher Tate181fafa2009-05-14 11:12:14 -0700851 if (packageName != null) {
Dan Egnorefe52642009-06-24 00:16:33 -0700852 allApps = new ArrayList<PackageInfo>();
Christopher Tate181fafa2009-05-14 11:12:14 -0700853 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700854 int flags = PackageManager.GET_SIGNATURES;
855 allApps.add(mPackageManager.getPackageInfo(packageName, flags));
Christopher Tate181fafa2009-05-14 11:12:14 -0700856 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700857 // just skip it (???)
Christopher Tate181fafa2009-05-14 11:12:14 -0700858 }
859 } else {
860 // all apps with agents
Dan Egnorefe52642009-06-24 00:16:33 -0700861 allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700862 }
863 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700864 }
865
Joe Onorato8ad02812009-05-13 01:41:44 -0400866 private void removePackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700867 List<PackageInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700868 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800869 Slog.v(TAG, "removePackageParticipantsLockedInner (" + packageName
Christopher Tate043dadc2009-06-02 16:11:00 -0700870 + ") removing " + agents.size() + " entries");
Dan Egnorefe52642009-06-24 00:16:33 -0700871 for (PackageInfo p : agents) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800872 Slog.v(TAG, " - " + p);
Christopher Tate043dadc2009-06-02 16:11:00 -0700873 }
874 }
Dan Egnorefe52642009-06-24 00:16:33 -0700875 for (PackageInfo pkg : agents) {
876 if (packageName == null || pkg.packageName.equals(packageName)) {
877 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700878 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700879 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700880 // Find the existing entry with the same package name, and remove it.
881 // We can't just remove(app) because the instances are different.
882 for (ApplicationInfo entry: set) {
Dan Egnorefe52642009-06-24 00:16:33 -0700883 if (entry.packageName.equals(pkg.packageName)) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700884 set.remove(entry);
Christopher Tatee97e8072009-07-15 16:45:50 -0700885 removeEverBackedUp(pkg.packageName);
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700886 break;
887 }
888 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700889 if (set.size() == 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700890 mBackupParticipants.delete(uid);
891 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700892 }
893 }
894 }
895 }
896
Christopher Tate181fafa2009-05-14 11:12:14 -0700897 // Returns the set of all applications that define an android:backupAgent attribute
Christopher Tate73e02522009-07-15 14:18:26 -0700898 List<PackageInfo> allAgentPackages() {
Christopher Tate6785dd82009-06-18 15:58:25 -0700899 // !!! TODO: cache this and regenerate only when necessary
Dan Egnorefe52642009-06-24 00:16:33 -0700900 int flags = PackageManager.GET_SIGNATURES;
901 List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
902 int N = packages.size();
903 for (int a = N-1; a >= 0; a--) {
Christopher Tate0749dcd2009-08-13 15:13:03 -0700904 PackageInfo pkg = packages.get(a);
Christopher Tateb8eb1cb2009-09-16 10:57:21 -0700905 try {
906 ApplicationInfo app = pkg.applicationInfo;
907 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
Christopher Tatea87240c2010-02-12 14:12:34 -0800908 || app.backupAgentName == null) {
Christopher Tateb8eb1cb2009-09-16 10:57:21 -0700909 packages.remove(a);
910 }
911 else {
912 // we will need the shared library path, so look that up and store it here
913 app = mPackageManager.getApplicationInfo(pkg.packageName,
914 PackageManager.GET_SHARED_LIBRARY_FILES);
915 pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles;
916 }
917 } catch (NameNotFoundException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700918 packages.remove(a);
Christopher Tate181fafa2009-05-14 11:12:14 -0700919 }
920 }
Dan Egnorefe52642009-06-24 00:16:33 -0700921 return packages;
Christopher Tate181fafa2009-05-14 11:12:14 -0700922 }
Christopher Tateaa088442009-06-16 18:25:46 -0700923
Christopher Tate3799bc22009-05-06 16:13:56 -0700924 // Reset the given package's known backup participants. Unlike add/remove, the update
925 // action cannot be passed a null package name.
926 void updatePackageParticipantsLocked(String packageName) {
927 if (packageName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800928 Slog.e(TAG, "updatePackageParticipants called with null package name");
Christopher Tate3799bc22009-05-06 16:13:56 -0700929 return;
930 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800931 if (DEBUG) Slog.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700932
933 // brute force but small code size
Dan Egnorefe52642009-06-24 00:16:33 -0700934 List<PackageInfo> allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700935 removePackageParticipantsLockedInner(packageName, allApps);
936 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700937 }
938
Christopher Tate84725812010-02-04 15:52:40 -0800939 // Called from the backup task: record that the given app has been successfully
Christopher Tate73e02522009-07-15 14:18:26 -0700940 // backed up at least once
941 void logBackupComplete(String packageName) {
Dan Egnor852f8e42009-09-30 11:20:45 -0700942 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return;
943
944 synchronized (mEverStoredApps) {
945 if (!mEverStoredApps.add(packageName)) return;
946
947 RandomAccessFile out = null;
948 try {
949 out = new RandomAccessFile(mEverStored, "rws");
950 out.seek(out.length());
951 out.writeUTF(packageName);
952 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800953 Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored);
Dan Egnor852f8e42009-09-30 11:20:45 -0700954 } finally {
955 try { if (out != null) out.close(); } catch (IOException e) {}
Christopher Tate73e02522009-07-15 14:18:26 -0700956 }
957 }
958 }
959
Christopher Tatee97e8072009-07-15 16:45:50 -0700960 // Remove our awareness of having ever backed up the given package
961 void removeEverBackedUp(String packageName) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800962 if (DEBUG) Slog.v(TAG, "Removing backed-up knowledge of " + packageName + ", new set:");
Christopher Tatee97e8072009-07-15 16:45:50 -0700963
Dan Egnor852f8e42009-09-30 11:20:45 -0700964 synchronized (mEverStoredApps) {
965 // Rewrite the file and rename to overwrite. If we reboot in the middle,
966 // we'll recognize on initialization time that the package no longer
967 // exists and fix it up then.
968 File tempKnownFile = new File(mBaseStateDir, "processed.new");
969 RandomAccessFile known = null;
970 try {
971 known = new RandomAccessFile(tempKnownFile, "rws");
972 mEverStoredApps.remove(packageName);
973 for (String s : mEverStoredApps) {
974 known.writeUTF(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800975 if (DEBUG) Slog.v(TAG, " " + s);
Christopher Tatee97e8072009-07-15 16:45:50 -0700976 }
Dan Egnor852f8e42009-09-30 11:20:45 -0700977 known.close();
978 known = null;
979 if (!tempKnownFile.renameTo(mEverStored)) {
980 throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored);
981 }
982 } catch (IOException e) {
983 // Bad: we couldn't create the new copy. For safety's sake we
984 // abandon the whole process and remove all what's-backed-up
985 // state entirely, meaning we'll force a backup pass for every
986 // participant on the next boot or [re]install.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800987 Slog.w(TAG, "Error rewriting " + mEverStored, e);
Dan Egnor852f8e42009-09-30 11:20:45 -0700988 mEverStoredApps.clear();
989 tempKnownFile.delete();
990 mEverStored.delete();
991 } finally {
992 try { if (known != null) known.close(); } catch (IOException e) {}
Christopher Tatee97e8072009-07-15 16:45:50 -0700993 }
994 }
995 }
996
Christopher Tateb49ceb32010-02-08 16:22:24 -0800997 // Persistently record the current and ancestral backup tokens as well
998 // as the set of packages with data [supposedly] available in the
999 // ancestral dataset.
Christopher Tate84725812010-02-04 15:52:40 -08001000 void writeRestoreTokens() {
1001 try {
1002 RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd");
Christopher Tateb49ceb32010-02-08 16:22:24 -08001003
1004 // First, the version number of this record, for futureproofing
1005 af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION);
1006
1007 // Write the ancestral and current tokens
Christopher Tate84725812010-02-04 15:52:40 -08001008 af.writeLong(mAncestralToken);
1009 af.writeLong(mCurrentToken);
Christopher Tateb49ceb32010-02-08 16:22:24 -08001010
1011 // Now write the set of ancestral packages
1012 if (mAncestralPackages == null) {
1013 af.writeInt(-1);
1014 } else {
1015 af.writeInt(mAncestralPackages.size());
Joe Onorato8a9b2202010-02-26 18:56:32 -08001016 if (DEBUG) Slog.v(TAG, "Ancestral packages: " + mAncestralPackages.size());
Christopher Tateb49ceb32010-02-08 16:22:24 -08001017 for (String pkgName : mAncestralPackages) {
1018 af.writeUTF(pkgName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001019 if (DEBUG) Slog.v(TAG, " " + pkgName);
Christopher Tateb49ceb32010-02-08 16:22:24 -08001020 }
1021 }
Christopher Tate84725812010-02-04 15:52:40 -08001022 af.close();
1023 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001024 Slog.w(TAG, "Unable to write token file:", e);
Christopher Tate84725812010-02-04 15:52:40 -08001025 }
1026 }
1027
Dan Egnor87a02bc2009-06-17 02:30:10 -07001028 // Return the given transport
Christopher Tate91717492009-06-26 21:07:13 -07001029 private IBackupTransport getTransport(String transportName) {
1030 synchronized (mTransports) {
1031 IBackupTransport transport = mTransports.get(transportName);
1032 if (transport == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 Slog.w(TAG, "Requested unavailable transport: " + transportName);
Christopher Tate91717492009-06-26 21:07:13 -07001034 }
1035 return transport;
Christopher Tate8c850b72009-06-07 19:33:20 -07001036 }
Christopher Tate8c850b72009-06-07 19:33:20 -07001037 }
1038
Christopher Tatedf01dea2009-06-09 20:45:02 -07001039 // fire off a backup agent, blocking until it attaches or times out
1040 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
1041 IBackupAgent agent = null;
1042 synchronized(mAgentConnectLock) {
1043 mConnecting = true;
1044 mConnectedAgent = null;
1045 try {
1046 if (mActivityManager.bindBackupAgent(app, mode)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001047 Slog.d(TAG, "awaiting agent for " + app);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001048
1049 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -07001050 // only wait 10 seconds for the clear data to happen
1051 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
1052 while (mConnecting && mConnectedAgent == null
1053 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001054 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001055 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001056 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001057 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -07001058 return null;
1059 }
1060 }
1061
1062 // if we timed out with no connect, abort and move on
1063 if (mConnecting == true) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.w(TAG, "Timeout waiting for agent " + app);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001065 return null;
1066 }
1067 agent = mConnectedAgent;
1068 }
1069 } catch (RemoteException e) {
1070 // can't happen
1071 }
1072 }
1073 return agent;
1074 }
1075
Christopher Tatec7b31e32009-06-10 15:49:30 -07001076 // clear an application's data, blocking until the operation completes or times out
1077 void clearApplicationDataSynchronous(String packageName) {
Christopher Tatef7c886b2009-06-26 15:34:09 -07001078 // Don't wipe packages marked allowClearUserData=false
1079 try {
1080 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
1081 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 if (DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping "
Christopher Tatef7c886b2009-06-26 15:34:09 -07001083 + packageName);
1084 return;
1085 }
1086 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001087 Slog.w(TAG, "Tried to clear data for " + packageName + " but not found");
Christopher Tatef7c886b2009-06-26 15:34:09 -07001088 return;
1089 }
1090
Christopher Tatec7b31e32009-06-10 15:49:30 -07001091 ClearDataObserver observer = new ClearDataObserver();
1092
1093 synchronized(mClearDataLock) {
1094 mClearingData = true;
Christopher Tate9dfdac52009-08-06 14:57:53 -07001095 try {
1096 mActivityManager.clearApplicationUserData(packageName, observer);
1097 } catch (RemoteException e) {
1098 // can't happen because the activity manager is in this process
1099 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001100
1101 // only wait 10 seconds for the clear data to happen
1102 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
1103 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
1104 try {
1105 mClearDataLock.wait(5000);
1106 } catch (InterruptedException e) {
1107 // won't happen, but still.
1108 mClearingData = false;
1109 }
1110 }
1111 }
1112 }
1113
1114 class ClearDataObserver extends IPackageDataObserver.Stub {
Dan Egnor852f8e42009-09-30 11:20:45 -07001115 public void onRemoveCompleted(String packageName, boolean succeeded) {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001116 synchronized(mClearDataLock) {
1117 mClearingData = false;
Christopher Tatef68eb502009-06-16 11:02:01 -07001118 mClearDataLock.notifyAll();
Christopher Tatec7b31e32009-06-10 15:49:30 -07001119 }
1120 }
1121 }
1122
Christopher Tate1bb69062010-02-19 17:02:12 -08001123 // Get the restore-set token for the best-available restore set for this package:
1124 // the active set if possible, else the ancestral one. Returns zero if none available.
1125 long getAvailableRestoreToken(String packageName) {
1126 long token = mAncestralToken;
1127 synchronized (mQueueLock) {
1128 if (mEverStoredApps.contains(packageName)) {
1129 token = mCurrentToken;
1130 }
1131 }
1132 return token;
1133 }
1134
Christopher Tate44a27902010-01-27 17:15:49 -08001135 // -----
1136 // Utility methods used by the asynchronous-with-timeout backup/restore operations
1137 boolean waitUntilOperationComplete(int token) {
1138 int finalState = OP_PENDING;
1139 synchronized (mCurrentOpLock) {
1140 try {
1141 while ((finalState = mCurrentOperations.get(token, OP_TIMEOUT)) == OP_PENDING) {
1142 try {
1143 mCurrentOpLock.wait();
1144 } catch (InterruptedException e) {}
1145 }
1146 } catch (IndexOutOfBoundsException e) {
1147 // the operation has been mysteriously cleared from our
1148 // bookkeeping -- consider this a success and ignore it.
1149 }
1150 }
1151 mBackupHandler.removeMessages(MSG_TIMEOUT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001152 if (DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token)
Christopher Tate1bb69062010-02-19 17:02:12 -08001153 + " complete: finalState=" + finalState);
Christopher Tate44a27902010-01-27 17:15:49 -08001154 return finalState == OP_ACKNOWLEDGED;
1155 }
1156
1157 void prepareOperationTimeout(int token, long interval) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001158 if (DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token)
Christopher Tate1bb69062010-02-19 17:02:12 -08001159 + " interval=" + interval);
Christopher Tate44a27902010-01-27 17:15:49 -08001160 mCurrentOperations.put(token, OP_PENDING);
1161 Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0);
1162 mBackupHandler.sendMessageDelayed(msg, interval);
1163 }
1164
Christopher Tate043dadc2009-06-02 16:11:00 -07001165 // ----- Back up a set of applications via a worker thread -----
1166
Christopher Tate44a27902010-01-27 17:15:49 -08001167 class PerformBackupTask implements Runnable {
Christopher Tate043dadc2009-06-02 16:11:00 -07001168 private static final String TAG = "PerformBackupThread";
Christopher Tateaa088442009-06-16 18:25:46 -07001169 IBackupTransport mTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -07001170 ArrayList<BackupRequest> mQueue;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001171 File mStateDir;
Christopher Tatecde87f42009-06-12 12:55:53 -07001172 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -07001173
Christopher Tate44a27902010-01-27 17:15:49 -08001174 public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue,
Christopher Tatecde87f42009-06-12 12:55:53 -07001175 File journal) {
Christopher Tateaa088442009-06-16 18:25:46 -07001176 mTransport = transport;
Christopher Tate043dadc2009-06-02 16:11:00 -07001177 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -07001178 mJournal = journal;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001179
1180 try {
1181 mStateDir = new File(mBaseStateDir, transport.transportDirName());
1182 } catch (RemoteException e) {
1183 // can't happen; the transport is local
1184 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001185 }
1186
Christopher Tate043dadc2009-06-02 16:11:00 -07001187 public void run() {
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001188 int status = BackupConstants.TRANSPORT_OK;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001189 long startRealtime = SystemClock.elapsedRealtime();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001190 if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
Christopher Tate043dadc2009-06-02 16:11:00 -07001191
Christopher Tate79588342009-06-30 16:11:49 -07001192 // Backups run at background priority
1193 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1194
Christopher Tate043dadc2009-06-02 16:11:00 -07001195 try {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196 EventLog.writeEvent(EventLogTags.BACKUP_START, mTransport.transportDirName());
Dan Egnor01445162009-09-21 17:04:05 -07001197
Dan Egnor852f8e42009-09-30 11:20:45 -07001198 // If we haven't stored package manager metadata yet, we must init the transport.
1199 File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL);
1200 if (status == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001201 Slog.i(TAG, "Initializing (wiping) backup state and transport storage");
Dan Egnor852f8e42009-09-30 11:20:45 -07001202 resetBackupState(mStateDir); // Just to make sure.
Dan Egnor01445162009-09-21 17:04:05 -07001203 status = mTransport.initializeDevice();
Dan Egnor726247c2009-09-29 19:12:31 -07001204 if (status == BackupConstants.TRANSPORT_OK) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205 EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
Dan Egnor726247c2009-09-29 19:12:31 -07001206 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 Slog.e(TAG, "Transport error in initializeDevice()");
Dan Egnor726247c2009-09-29 19:12:31 -07001209 }
Dan Egnor01445162009-09-21 17:04:05 -07001210 }
Dan Egnorbb9001c2009-07-27 12:20:13 -07001211
1212 // The package manager doesn't have a proper <application> etc, but since
1213 // it's running here in the system process we can just set up its agent
1214 // directly and use a synthetic BackupRequest. We always run this pass
1215 // because it's cheap and this way we guarantee that we don't get out of
1216 // step even if we're selecting among various transports at run time.
Dan Egnor01445162009-09-21 17:04:05 -07001217 if (status == BackupConstants.TRANSPORT_OK) {
1218 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
1219 mPackageManager, allAgentPackages());
1220 BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false);
1221 pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL;
1222 status = processOneBackup(pmRequest,
1223 IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport);
1224 }
Christopher Tate90967f42009-09-20 15:28:33 -07001225
Dan Egnor01445162009-09-21 17:04:05 -07001226 if (status == BackupConstants.TRANSPORT_OK) {
1227 // Now run all the backups in our queue
1228 status = doQueuedBackups(mTransport);
1229 }
1230
1231 if (status == BackupConstants.TRANSPORT_OK) {
1232 // Tell the transport to finish everything it has buffered
1233 status = mTransport.finishBackup();
1234 if (status == BackupConstants.TRANSPORT_OK) {
1235 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236 EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, mQueue.size(), millis);
Dan Egnor01445162009-09-21 17:04:05 -07001237 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(finish)");
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 Slog.e(TAG, "Transport error in finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -07001240 }
1241 }
1242
Dan Egnor01445162009-09-21 17:04:05 -07001243 if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) {
Christopher Tated55e18a2009-09-21 10:12:59 -07001244 // The backend reports that our dataset has been wiped. We need to
1245 // reset all of our bookkeeping and instead run a new backup pass for
Christopher Tatec2af5d32010-02-02 15:18:58 -08001246 // everything.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247 EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName());
Christopher Tated55e18a2009-09-21 10:12:59 -07001248 resetBackupState(mStateDir);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001249 }
1250 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 Slog.e(TAG, "Error in backup thread", e);
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001252 status = BackupConstants.TRANSPORT_ERROR;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001253 } finally {
Christopher Tate84725812010-02-04 15:52:40 -08001254 // If everything actually went through and this is the first time we've
1255 // done a backup, we can now record what the current backup dataset token
1256 // is.
1257 if ((mCurrentToken == 0) && (status != BackupConstants.TRANSPORT_OK)) {
1258 try {
1259 mCurrentToken = mTransport.getCurrentRestoreSet();
1260 } catch (RemoteException e) { /* cannot happen */ }
1261 writeRestoreTokens();
1262 }
1263
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001264 // If things went wrong, we need to re-stage the apps we had expected
1265 // to be backing up in this pass. This journals the package names in
1266 // the current active pending-backup file, not in the we are holding
1267 // here in mJournal.
1268 if (status != BackupConstants.TRANSPORT_OK) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 Slog.w(TAG, "Backup pass unsuccessful, restaging");
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001270 for (BackupRequest req : mQueue) {
Dan Egnor852f8e42009-09-30 11:20:45 -07001271 dataChanged(req.appInfo.packageName);
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001272 }
Christopher Tate21ab6a52009-09-24 18:01:46 -07001273
1274 // We also want to reset the backup schedule based on whatever
1275 // the transport suggests by way of retry/backoff time.
1276 try {
1277 startBackupAlarmsLocked(mTransport.requestBackupTime());
1278 } catch (RemoteException e) { /* cannot happen */ }
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001279 }
1280
1281 // Either backup was successful, in which case we of course do not need
1282 // this pass's journal any more; or it failed, in which case we just
1283 // re-enqueued all of these packages in the current active journal.
1284 // Either way, we no longer need this pass's journal.
Dan Egnor852f8e42009-09-30 11:20:45 -07001285 if (mJournal != null && !mJournal.delete()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 Slog.e(TAG, "Unable to remove backup journal file " + mJournal);
Christopher Tateb03b3bb2009-09-22 11:14:17 -07001287 }
1288
Christopher Tatec2af5d32010-02-02 15:18:58 -08001289 // Only once we're entirely finished do we release the wakelock
Dan Egnor852f8e42009-09-30 11:20:45 -07001290 if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) {
Dan Egnor852f8e42009-09-30 11:20:45 -07001291 backupNow();
1292 }
1293
Dan Egnorbb9001c2009-07-27 12:20:13 -07001294 mWakelock.release();
Christopher Tatecde87f42009-06-12 12:55:53 -07001295 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001296 }
1297
Dan Egnor01445162009-09-21 17:04:05 -07001298 private int doQueuedBackups(IBackupTransport transport) {
Christopher Tate043dadc2009-06-02 16:11:00 -07001299 for (BackupRequest request : mQueue) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001300 Slog.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -07001301
1302 IBackupAgent agent = null;
1303 int mode = (request.fullBackup)
1304 ? IApplicationThread.BACKUP_MODE_FULL
1305 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
1306 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001307 agent = bindToAgentSynchronous(request.appInfo, mode);
1308 if (agent != null) {
Dan Egnor01445162009-09-21 17:04:05 -07001309 int result = processOneBackup(request, agent, transport);
1310 if (result != BackupConstants.TRANSPORT_OK) return result;
Christopher Tate043dadc2009-06-02 16:11:00 -07001311 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001312 } catch (SecurityException ex) {
1313 // Try for the next one.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001314 Slog.d(TAG, "error in bind/backup", ex);
Dan Egnor01445162009-09-21 17:04:05 -07001315 } finally {
1316 try { // unbind even on timeout, just in case
1317 mActivityManager.unbindBackupAgent(request.appInfo);
1318 } catch (RemoteException e) {}
Christopher Tate043dadc2009-06-02 16:11:00 -07001319 }
1320 }
Dan Egnor01445162009-09-21 17:04:05 -07001321
1322 return BackupConstants.TRANSPORT_OK;
Christopher Tate043dadc2009-06-02 16:11:00 -07001323 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001324
Dan Egnor01445162009-09-21 17:04:05 -07001325 private int processOneBackup(BackupRequest request, IBackupAgent agent,
1326 IBackupTransport transport) {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001327 final String packageName = request.appInfo.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.d(TAG, "processOneBackup doBackup() on " + packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001329
Dan Egnorbb9001c2009-07-27 12:20:13 -07001330 File savedStateName = new File(mStateDir, packageName);
1331 File backupDataName = new File(mDataDir, packageName + ".data");
1332 File newStateName = new File(mStateDir, packageName + ".new");
1333
1334 ParcelFileDescriptor savedState = null;
1335 ParcelFileDescriptor backupData = null;
1336 ParcelFileDescriptor newState = null;
1337
1338 PackageInfo packInfo;
Christopher Tate44a27902010-01-27 17:15:49 -08001339 int token = mTokenGenerator.nextInt();
Christopher Tatec7b31e32009-06-10 15:49:30 -07001340 try {
1341 // Look up the package info & signatures. This is first so that if it
1342 // throws an exception, there's no file setup yet that would need to
1343 // be unraveled.
Christopher Tateabce4e82009-06-18 18:35:32 -07001344 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
1345 // The metadata 'package' is synthetic
1346 packInfo = new PackageInfo();
1347 packInfo.packageName = packageName;
1348 } else {
1349 packInfo = mPackageManager.getPackageInfo(packageName,
Christopher Tatec7b31e32009-06-10 15:49:30 -07001350 PackageManager.GET_SIGNATURES);
Christopher Tateabce4e82009-06-18 18:35:32 -07001351 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001352
Christopher Tatec7b31e32009-06-10 15:49:30 -07001353 // In a full backup, we pass a null ParcelFileDescriptor as
1354 // the saved-state "file"
Dan Egnorbb9001c2009-07-27 12:20:13 -07001355 if (!request.fullBackup) {
1356 savedState = ParcelFileDescriptor.open(savedStateName,
Christopher Tatec7b31e32009-06-10 15:49:30 -07001357 ParcelFileDescriptor.MODE_READ_ONLY |
Dan Egnorbb9001c2009-07-27 12:20:13 -07001358 ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary
1359 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001360
Dan Egnorbb9001c2009-07-27 12:20:13 -07001361 backupData = ParcelFileDescriptor.open(backupDataName,
1362 ParcelFileDescriptor.MODE_READ_WRITE |
1363 ParcelFileDescriptor.MODE_CREATE |
1364 ParcelFileDescriptor.MODE_TRUNCATE);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001365
Dan Egnorbb9001c2009-07-27 12:20:13 -07001366 newState = ParcelFileDescriptor.open(newStateName,
1367 ParcelFileDescriptor.MODE_READ_WRITE |
1368 ParcelFileDescriptor.MODE_CREATE |
1369 ParcelFileDescriptor.MODE_TRUNCATE);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001370
Christopher Tate44a27902010-01-27 17:15:49 -08001371 // Initiate the target's backup pass
1372 prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL);
1373 agent.doBackup(savedState, backupData, newState, token, mBackupManagerBinder);
1374 boolean success = waitUntilOperationComplete(token);
1375
1376 if (!success) {
1377 // timeout -- bail out into the failed-transaction logic
1378 throw new RuntimeException("Backup timeout");
1379 }
1380
Dan Egnorbb9001c2009-07-27 12:20:13 -07001381 logBackupComplete(packageName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG) Slog.v(TAG, "doBackup() success");
Christopher Tatec7b31e32009-06-10 15:49:30 -07001383 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001384 Slog.e(TAG, "Error backing up " + packageName, e);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385 EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, e.toString());
Dan Egnorbb9001c2009-07-27 12:20:13 -07001386 backupDataName.delete();
1387 newStateName.delete();
Christopher Tated55e18a2009-09-21 10:12:59 -07001388 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001389 } finally {
1390 try { if (savedState != null) savedState.close(); } catch (IOException e) {}
1391 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
1392 try { if (newState != null) newState.close(); } catch (IOException e) {}
1393 savedState = backupData = newState = null;
Christopher Tate44a27902010-01-27 17:15:49 -08001394 synchronized (mCurrentOpLock) {
1395 mCurrentOperations.clear();
1396 }
Dan Egnorbb9001c2009-07-27 12:20:13 -07001397 }
1398
1399 // Now propagate the newly-backed-up data to the transport
Dan Egnor01445162009-09-21 17:04:05 -07001400 int result = BackupConstants.TRANSPORT_OK;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001401 try {
1402 int size = (int) backupDataName.length();
1403 if (size > 0) {
Dan Egnor01445162009-09-21 17:04:05 -07001404 if (result == BackupConstants.TRANSPORT_OK) {
1405 backupData = ParcelFileDescriptor.open(backupDataName,
1406 ParcelFileDescriptor.MODE_READ_ONLY);
1407 result = transport.performBackup(packInfo, backupData);
1408 }
Dan Egnorbb9001c2009-07-27 12:20:13 -07001409
Dan Egnor83861e72009-09-17 16:17:55 -07001410 // TODO - We call finishBackup() for each application backed up, because
1411 // we need to know now whether it succeeded or failed. Instead, we should
1412 // hold off on finishBackup() until the end, which implies holding off on
1413 // renaming *all* the output state files (see below) until that happens.
1414
Dan Egnor01445162009-09-21 17:04:05 -07001415 if (result == BackupConstants.TRANSPORT_OK) {
1416 result = transport.finishBackup();
Dan Egnor83861e72009-09-17 16:17:55 -07001417 }
Dan Egnorbb9001c2009-07-27 12:20:13 -07001418 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 if (DEBUG) Slog.i(TAG, "no backup data written; not calling transport");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001420 }
1421
1422 // After successful transport, delete the now-stale data
1423 // and juggle the files so that next time we supply the agent
1424 // with the new state file it just created.
Dan Egnor01445162009-09-21 17:04:05 -07001425 if (result == BackupConstants.TRANSPORT_OK) {
1426 backupDataName.delete();
1427 newStateName.renameTo(savedStateName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428 EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, packageName, size);
Dan Egnor01445162009-09-21 17:04:05 -07001429 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName);
Dan Egnor01445162009-09-21 17:04:05 -07001431 }
Dan Egnorbb9001c2009-07-27 12:20:13 -07001432 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001433 Slog.e(TAG, "Transport error backing up " + packageName, e);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName);
Dan Egnor01445162009-09-21 17:04:05 -07001435 result = BackupConstants.TRANSPORT_ERROR;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001436 } finally {
1437 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
Christopher Tatec7b31e32009-06-10 15:49:30 -07001438 }
Christopher Tated55e18a2009-09-21 10:12:59 -07001439
Dan Egnor01445162009-09-21 17:04:05 -07001440 return result;
Christopher Tatec7b31e32009-06-10 15:49:30 -07001441 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001442 }
1443
Christopher Tatedf01dea2009-06-09 20:45:02 -07001444
1445 // ----- Restore handling -----
1446
Christopher Tate78dd4a72009-11-04 11:49:08 -08001447 private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) {
1448 // If the target resides on the system partition, we allow it to restore
1449 // data from the like-named package in a restore set even if the signatures
1450 // do not match. (Unlike general applications, those flashed to the system
1451 // partition will be signed with the device's platform certificate, so on
1452 // different phones the same system app will have different signatures.)
1453 if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001454 if (DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check");
Christopher Tate78dd4a72009-11-04 11:49:08 -08001455 return true;
1456 }
1457
Christopher Tate20efdf6b2009-06-18 19:41:36 -07001458 // Allow unsigned apps, but not signed on one device and unsigned on the other
1459 // !!! TODO: is this the right policy?
Christopher Tate78dd4a72009-11-04 11:49:08 -08001460 Signature[] deviceSigs = target.signatures;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001461 if (DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs
Christopher Tate6aa41f42009-06-19 14:14:22 -07001462 + " device=" + deviceSigs);
Christopher Tate20efdf6b2009-06-18 19:41:36 -07001463 if ((storedSigs == null || storedSigs.length == 0)
1464 && (deviceSigs == null || deviceSigs.length == 0)) {
1465 return true;
1466 }
1467 if (storedSigs == null || deviceSigs == null) {
1468 return false;
1469 }
1470
Christopher Tateabce4e82009-06-18 18:35:32 -07001471 // !!! TODO: this demands that every stored signature match one
1472 // that is present on device, and does not demand the converse.
1473 // Is this this right policy?
1474 int nStored = storedSigs.length;
1475 int nDevice = deviceSigs.length;
1476
1477 for (int i=0; i < nStored; i++) {
1478 boolean match = false;
1479 for (int j=0; j < nDevice; j++) {
1480 if (storedSigs[i].equals(deviceSigs[j])) {
1481 match = true;
1482 break;
1483 }
1484 }
1485 if (!match) {
1486 return false;
1487 }
1488 }
1489 return true;
1490 }
1491
Christopher Tate44a27902010-01-27 17:15:49 -08001492 class PerformRestoreTask implements Runnable {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001493 private IBackupTransport mTransport;
Christopher Tate7d562ec2009-06-25 18:03:43 -07001494 private IRestoreObserver mObserver;
Dan Egnor156411d2009-06-26 13:20:02 -07001495 private long mToken;
Christopher Tate84725812010-02-04 15:52:40 -08001496 private PackageInfo mTargetPackage;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001497 private File mStateDir;
Christopher Tate1bb69062010-02-19 17:02:12 -08001498 private int mPmToken;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001499
Christopher Tate5cbbf562009-06-22 16:44:51 -07001500 class RestoreRequest {
1501 public PackageInfo app;
1502 public int storedAppVersion;
1503
1504 RestoreRequest(PackageInfo _app, int _version) {
1505 app = _app;
1506 storedAppVersion = _version;
1507 }
1508 }
1509
Christopher Tate44a27902010-01-27 17:15:49 -08001510 PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer,
Christopher Tate1bb69062010-02-19 17:02:12 -08001511 long restoreSetToken, PackageInfo targetPackage, int pmToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001512 mTransport = transport;
Christopher Tate7d562ec2009-06-25 18:03:43 -07001513 mObserver = observer;
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001514 mToken = restoreSetToken;
Christopher Tate84725812010-02-04 15:52:40 -08001515 mTargetPackage = targetPackage;
Christopher Tate1bb69062010-02-19 17:02:12 -08001516 mPmToken = pmToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001517
1518 try {
1519 mStateDir = new File(mBaseStateDir, transport.transportDirName());
1520 } catch (RemoteException e) {
1521 // can't happen; the transport is local
1522 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001523 }
1524
Christopher Tatedf01dea2009-06-09 20:45:02 -07001525 public void run() {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001526 long startRealtime = SystemClock.elapsedRealtime();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 if (DEBUG) Slog.v(TAG, "Beginning restore process mTransport=" + mTransport
Christopher Tate84725812010-02-04 15:52:40 -08001528 + " mObserver=" + mObserver + " mToken=" + Long.toHexString(mToken)
Christopher Tate1bb69062010-02-19 17:02:12 -08001529 + " mTargetPackage=" + mTargetPackage + " mPmToken=" + mPmToken);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001530
Christopher Tateb49ceb32010-02-08 16:22:24 -08001531 PackageManagerBackupAgent pmAgent = null;
Christopher Tate7d562ec2009-06-25 18:03:43 -07001532 int error = -1; // assume error
1533
Dan Egnorefe52642009-06-24 00:16:33 -07001534 // build the set of apps to restore
Christopher Tatedf01dea2009-06-09 20:45:02 -07001535 try {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001536 // TODO: Log this before getAvailableRestoreSets, somehow
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537 EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken);
Christopher Tateabce4e82009-06-18 18:35:32 -07001538
Dan Egnorefe52642009-06-24 00:16:33 -07001539 // Get the list of all packages which have backup enabled.
1540 // (Include the Package Manager metadata pseudo-package first.)
1541 ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>();
1542 PackageInfo omPackage = new PackageInfo();
1543 omPackage.packageName = PACKAGE_MANAGER_SENTINEL;
1544 restorePackages.add(omPackage);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001545
Dan Egnorefe52642009-06-24 00:16:33 -07001546 List<PackageInfo> agentPackages = allAgentPackages();
Christopher Tate84725812010-02-04 15:52:40 -08001547 if (mTargetPackage == null) {
1548 restorePackages.addAll(agentPackages);
1549 } else {
1550 // Just one package to attempt restore of
1551 restorePackages.add(mTargetPackage);
1552 }
Dan Egnorefe52642009-06-24 00:16:33 -07001553
Christopher Tate7d562ec2009-06-25 18:03:43 -07001554 // let the observer know that we're running
1555 if (mObserver != null) {
1556 try {
1557 // !!! TODO: get an actual count from the transport after
1558 // its startRestore() runs?
1559 mObserver.restoreStarting(restorePackages.size());
1560 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 Slog.d(TAG, "Restore observer died at restoreStarting");
Christopher Tate7d562ec2009-06-25 18:03:43 -07001562 mObserver = null;
1563 }
1564 }
1565
Dan Egnor01445162009-09-21 17:04:05 -07001566 if (mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0])) !=
1567 BackupConstants.TRANSPORT_OK) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001568 Slog.e(TAG, "Error starting restore operation");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001569 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
Dan Egnorefe52642009-06-24 00:16:33 -07001570 return;
1571 }
1572
1573 String packageName = mTransport.nextRestorePackage();
1574 if (packageName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001575 Slog.e(TAG, "Error getting first restore package");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
Dan Egnorefe52642009-06-24 00:16:33 -07001577 return;
1578 } else if (packageName.equals("")) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001579 Slog.i(TAG, "No restore data available");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001580 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581 EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis);
Dan Egnorefe52642009-06-24 00:16:33 -07001582 return;
1583 } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 Slog.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL
Dan Egnorefe52642009-06-24 00:16:33 -07001585 + "\", found only \"" + packageName + "\"");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001587 "Package manager data missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001588 return;
1589 }
1590
1591 // Pull the Package Manager metadata from the restore set first
Christopher Tateb49ceb32010-02-08 16:22:24 -08001592 pmAgent = new PackageManagerBackupAgent(
Dan Egnorefe52642009-06-24 00:16:33 -07001593 mPackageManager, agentPackages);
1594 processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()));
1595
Christopher Tate8c032472009-07-02 14:28:47 -07001596 // Verify that the backup set includes metadata. If not, we can't do
1597 // signature/version verification etc, so we simply do not proceed with
1598 // the restore operation.
Christopher Tate3d7cd132009-07-07 14:23:07 -07001599 if (!pmAgent.hasMetadata()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 Slog.e(TAG, "No restore metadata available, so not restoring settings");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001601 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001602 "Package manager restore metadata missing");
Christopher Tate8c032472009-07-02 14:28:47 -07001603 return;
1604 }
1605
Christopher Tate7d562ec2009-06-25 18:03:43 -07001606 int count = 0;
Dan Egnorefe52642009-06-24 00:16:33 -07001607 for (;;) {
1608 packageName = mTransport.nextRestorePackage();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001609
Dan Egnorefe52642009-06-24 00:16:33 -07001610 if (packageName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001611 Slog.e(TAG, "Error getting next restore package");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001612 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
Dan Egnorefe52642009-06-24 00:16:33 -07001613 return;
1614 } else if (packageName.equals("")) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG) Slog.v(TAG, "No next package, finishing restore");
Dan Egnorefe52642009-06-24 00:16:33 -07001616 break;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001617 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001618
Christopher Tate7d562ec2009-06-25 18:03:43 -07001619 if (mObserver != null) {
Christopher Tate7d562ec2009-06-25 18:03:43 -07001620 try {
Christopher Tate9c3cee92010-03-25 16:06:43 -07001621 mObserver.onUpdate(count, packageName);
Christopher Tate7d562ec2009-06-25 18:03:43 -07001622 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001623 Slog.d(TAG, "Restore observer died in onUpdate");
Christopher Tate7d562ec2009-06-25 18:03:43 -07001624 mObserver = null;
1625 }
1626 }
1627
Dan Egnorefe52642009-06-24 00:16:33 -07001628 Metadata metaInfo = pmAgent.getRestoredMetadata(packageName);
1629 if (metaInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001630 Slog.e(TAG, "Missing metadata for " + packageName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001632 "Package metadata missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001633 continue;
1634 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001635
Dan Egnorbb9001c2009-07-27 12:20:13 -07001636 PackageInfo packageInfo;
1637 try {
1638 int flags = PackageManager.GET_SIGNATURES;
1639 packageInfo = mPackageManager.getPackageInfo(packageName, flags);
1640 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 Slog.e(TAG, "Invalid package restoring data", e);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001642 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001643 "Package missing on device");
1644 continue;
1645 }
1646
Dan Egnorefe52642009-06-24 00:16:33 -07001647 if (metaInfo.versionCode > packageInfo.versionCode) {
Christopher Tate3dda5182010-02-24 16:06:18 -08001648 // Data is from a "newer" version of the app than we have currently
1649 // installed. If the app has not declared that it is prepared to
1650 // handle this case, we do not attempt the restore.
1651 if ((packageInfo.applicationInfo.flags
1652 & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) {
1653 String message = "Version " + metaInfo.versionCode
1654 + " > installed version " + packageInfo.versionCode;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001655 Slog.w(TAG, "Package " + packageName + ": " + message);
Christopher Tate3dda5182010-02-24 16:06:18 -08001656 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
1657 packageName, message);
1658 continue;
1659 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001660 if (DEBUG) Slog.v(TAG, "Version " + metaInfo.versionCode
Christopher Tate3dda5182010-02-24 16:06:18 -08001661 + " > installed " + packageInfo.versionCode
1662 + " but restoreAnyVersion");
1663 }
Dan Egnorefe52642009-06-24 00:16:33 -07001664 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001665
Christopher Tate78dd4a72009-11-04 11:49:08 -08001666 if (!signaturesMatch(metaInfo.signatures, packageInfo)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001667 Slog.w(TAG, "Signature mismatch restoring " + packageName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001669 "Signature mismatch");
Dan Egnorefe52642009-06-24 00:16:33 -07001670 continue;
1671 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001672
Joe Onorato8a9b2202010-02-26 18:56:32 -08001673 if (DEBUG) Slog.v(TAG, "Package " + packageName
Dan Egnorefe52642009-06-24 00:16:33 -07001674 + " restore version [" + metaInfo.versionCode
1675 + "] is compatible with installed version ["
1676 + packageInfo.versionCode + "]");
Christopher Tatec7b31e32009-06-10 15:49:30 -07001677
Christopher Tate3de55bc2010-03-12 17:28:08 -08001678 // Then set up and bind the agent
Dan Egnorefe52642009-06-24 00:16:33 -07001679 IBackupAgent agent = bindToAgentSynchronous(
1680 packageInfo.applicationInfo,
Christopher Tate3de55bc2010-03-12 17:28:08 -08001681 IApplicationThread.BACKUP_MODE_INCREMENTAL);
Dan Egnorefe52642009-06-24 00:16:33 -07001682 if (agent == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001683 Slog.w(TAG, "Can't find backup agent for " + packageName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001684 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
Dan Egnorbb9001c2009-07-27 12:20:13 -07001685 "Restore agent missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001686 continue;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001687 }
1688
Christopher Tate5e1ab332009-09-01 20:32:49 -07001689 // And then finally run the restore on this agent
Dan Egnorefe52642009-06-24 00:16:33 -07001690 try {
1691 processOneRestore(packageInfo, metaInfo.versionCode, agent);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001692 ++count;
Dan Egnorefe52642009-06-24 00:16:33 -07001693 } finally {
Christopher Tate5e1ab332009-09-01 20:32:49 -07001694 // unbind and tidy up even on timeout or failure, just in case
Dan Egnorefe52642009-06-24 00:16:33 -07001695 mActivityManager.unbindBackupAgent(packageInfo.applicationInfo);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001696
1697 // The agent was probably running with a stub Application object,
1698 // which isn't a valid run mode for the main app logic. Shut
1699 // down the app so that next time it's launched, it gets the
Christopher Tate3dda5182010-02-24 16:06:18 -08001700 // usual full initialization. Note that this is only done for
1701 // full-system restores: when a single app has requested a restore,
1702 // it is explicitly not killed following that operation.
1703 if (mTargetPackage == null && (packageInfo.applicationInfo.flags
Christopher Tate5e1ab332009-09-01 20:32:49 -07001704 & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001705 if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07001706 + packageInfo.applicationInfo.processName);
1707 mActivityManager.killApplicationProcess(
1708 packageInfo.applicationInfo.processName,
1709 packageInfo.applicationInfo.uid);
1710 }
Dan Egnorefe52642009-06-24 00:16:33 -07001711 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001712 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001713
1714 // if we get this far, report success to the observer
1715 error = 0;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001716 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717 EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, count, millis);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001718 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001719 Slog.e(TAG, "Error in restore thread", e);
Dan Egnorefe52642009-06-24 00:16:33 -07001720 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001721 if (DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001722
Dan Egnorefe52642009-06-24 00:16:33 -07001723 try {
1724 mTransport.finishRestore();
1725 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001726 Slog.e(TAG, "Error finishing restore", e);
Dan Egnorefe52642009-06-24 00:16:33 -07001727 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001728
1729 if (mObserver != null) {
1730 try {
1731 mObserver.restoreFinished(error);
1732 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001733 Slog.d(TAG, "Restore observer died at restoreFinished");
Christopher Tate7d562ec2009-06-25 18:03:43 -07001734 }
1735 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001736
Christopher Tate84725812010-02-04 15:52:40 -08001737 // If this was a restoreAll operation, record that this was our
Christopher Tateb49ceb32010-02-08 16:22:24 -08001738 // ancestral dataset, as well as the set of apps that are possibly
1739 // restoreable from the dataset
1740 if (mTargetPackage == null && pmAgent != null) {
1741 mAncestralPackages = pmAgent.getRestoredPackages();
Christopher Tate84725812010-02-04 15:52:40 -08001742 mAncestralToken = mToken;
1743 writeRestoreTokens();
1744 }
1745
Christopher Tate1bb69062010-02-19 17:02:12 -08001746 // We must under all circumstances tell the Package Manager to
1747 // proceed with install notifications if it's waiting for us.
1748 if (mPmToken > 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001749 if (DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken);
Christopher Tate1bb69062010-02-19 17:02:12 -08001750 try {
1751 mPackageManagerBinder.finishPackageInstall(mPmToken);
1752 } catch (RemoteException e) { /* can't happen */ }
1753 }
1754
Christopher Tateb6787f22009-07-02 17:40:45 -07001755 // done; we can finally release the wakelock
1756 mWakelock.release();
Christopher Tatedf01dea2009-06-09 20:45:02 -07001757 }
1758 }
1759
Dan Egnorefe52642009-06-24 00:16:33 -07001760 // Do the guts of a restore of one application, using mTransport.getRestoreData().
1761 void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001762 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -07001763 final String packageName = app.packageName;
1764
Joe Onorato8a9b2202010-02-26 18:56:32 -08001765 if (DEBUG) Slog.d(TAG, "processOneRestore packageName=" + packageName);
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001766
Christopher Tatec7b31e32009-06-10 15:49:30 -07001767 // !!! TODO: get the dirs from the transport
1768 File backupDataName = new File(mDataDir, packageName + ".restore");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001769 File newStateName = new File(mStateDir, packageName + ".new");
1770 File savedStateName = new File(mStateDir, packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001771
Dan Egnorbb9001c2009-07-27 12:20:13 -07001772 ParcelFileDescriptor backupData = null;
1773 ParcelFileDescriptor newState = null;
1774
Christopher Tate44a27902010-01-27 17:15:49 -08001775 int token = mTokenGenerator.nextInt();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001776 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001777 // Run the transport's restore pass
Dan Egnorbb9001c2009-07-27 12:20:13 -07001778 backupData = ParcelFileDescriptor.open(backupDataName,
1779 ParcelFileDescriptor.MODE_READ_WRITE |
1780 ParcelFileDescriptor.MODE_CREATE |
1781 ParcelFileDescriptor.MODE_TRUNCATE);
1782
Dan Egnor01445162009-09-21 17:04:05 -07001783 if (mTransport.getRestoreData(backupData) != BackupConstants.TRANSPORT_OK) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001784 Slog.e(TAG, "Error getting restore data for " + packageName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001786 return;
Christopher Tatec7b31e32009-06-10 15:49:30 -07001787 }
1788
1789 // Okay, we have the data. Now have the agent do the restore.
Dan Egnorbb9001c2009-07-27 12:20:13 -07001790 backupData.close();
Christopher Tatec7b31e32009-06-10 15:49:30 -07001791 backupData = ParcelFileDescriptor.open(backupDataName,
1792 ParcelFileDescriptor.MODE_READ_ONLY);
1793
Dan Egnorbb9001c2009-07-27 12:20:13 -07001794 newState = ParcelFileDescriptor.open(newStateName,
1795 ParcelFileDescriptor.MODE_READ_WRITE |
1796 ParcelFileDescriptor.MODE_CREATE |
1797 ParcelFileDescriptor.MODE_TRUNCATE);
1798
Christopher Tate44a27902010-01-27 17:15:49 -08001799 // Kick off the restore, checking for hung agents
1800 prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL);
1801 agent.doRestore(backupData, appVersionCode, newState, token, mBackupManagerBinder);
1802 boolean success = waitUntilOperationComplete(token);
1803
1804 if (!success) {
1805 throw new RuntimeException("restore timeout");
1806 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001807
1808 // if everything went okay, remember the recorded state now
Christopher Tate90967f42009-09-20 15:28:33 -07001809 //
1810 // !!! TODO: the restored data should be migrated on the server
1811 // side into the current dataset. In that case the new state file
1812 // we just created would reflect the data already extant in the
1813 // backend, so there'd be nothing more to do. Until that happens,
1814 // however, we need to make sure that we record the data to the
1815 // current backend dataset. (Yes, this means shipping the data over
1816 // the wire in both directions. That's bad, but consistency comes
1817 // first, then efficiency.) Once we introduce server-side data
1818 // migration to the newly-restored device's dataset, we will change
1819 // the following from a discard of the newly-written state to the
1820 // "correct" operation of renaming into the canonical state blob.
1821 newStateName.delete(); // TODO: remove; see above comment
1822 //newStateName.renameTo(savedStateName); // TODO: replace with this
1823
Dan Egnorbb9001c2009-07-27 12:20:13 -07001824 int size = (int) backupDataName.length();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825 EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, packageName, size);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001826 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001827 Slog.e(TAG, "Error restoring data for " + packageName, e);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828 EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString());
Dan Egnorbb9001c2009-07-27 12:20:13 -07001829
Christopher Tate96733042009-07-20 14:49:13 -07001830 // If the agent fails restore, it might have put the app's data
1831 // into an incoherent state. For consistency we wipe its data
1832 // again in this case before propagating the exception
Christopher Tate96733042009-07-20 14:49:13 -07001833 clearApplicationDataSynchronous(packageName);
Christopher Tate1531dc82009-07-24 16:37:43 -07001834 } finally {
1835 backupDataName.delete();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001836 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
1837 try { if (newState != null) newState.close(); } catch (IOException e) {}
1838 backupData = newState = null;
Christopher Tate44a27902010-01-27 17:15:49 -08001839 mCurrentOperations.delete(token);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001840 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001841 }
1842 }
1843
Christopher Tate44a27902010-01-27 17:15:49 -08001844 class PerformClearTask implements Runnable {
Christopher Tateee0e78a2009-07-02 11:17:03 -07001845 IBackupTransport mTransport;
1846 PackageInfo mPackage;
1847
Christopher Tate44a27902010-01-27 17:15:49 -08001848 PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -07001849 mTransport = transport;
1850 mPackage = packageInfo;
1851 }
1852
Christopher Tateee0e78a2009-07-02 11:17:03 -07001853 public void run() {
1854 try {
1855 // Clear the on-device backup state to ensure a full backup next time
1856 File stateDir = new File(mBaseStateDir, mTransport.transportDirName());
1857 File stateFile = new File(stateDir, mPackage.packageName);
1858 stateFile.delete();
1859
1860 // Tell the transport to remove all the persistent storage for the app
Christopher Tate13f4a642009-09-30 20:06:45 -07001861 // TODO - need to handle failures
Christopher Tateee0e78a2009-07-02 11:17:03 -07001862 mTransport.clearBackupData(mPackage);
1863 } catch (RemoteException e) {
1864 // can't happen; the transport is local
1865 } finally {
1866 try {
Christopher Tate13f4a642009-09-30 20:06:45 -07001867 // TODO - need to handle failures
Christopher Tateee0e78a2009-07-02 11:17:03 -07001868 mTransport.finishBackup();
1869 } catch (RemoteException e) {
1870 // can't happen; the transport is local
1871 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001872
1873 // Last but not least, release the cpu
1874 mWakelock.release();
Christopher Tateee0e78a2009-07-02 11:17:03 -07001875 }
1876 }
1877 }
1878
Christopher Tate44a27902010-01-27 17:15:49 -08001879 class PerformInitializeTask implements Runnable {
Christopher Tate4cc86e12009-09-21 19:36:51 -07001880 HashSet<String> mQueue;
1881
Christopher Tate44a27902010-01-27 17:15:49 -08001882 PerformInitializeTask(HashSet<String> transportNames) {
Christopher Tate4cc86e12009-09-21 19:36:51 -07001883 mQueue = transportNames;
1884 }
1885
Christopher Tate4cc86e12009-09-21 19:36:51 -07001886 public void run() {
Christopher Tate4cc86e12009-09-21 19:36:51 -07001887 try {
1888 for (String transportName : mQueue) {
1889 IBackupTransport transport = getTransport(transportName);
1890 if (transport == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001891 Slog.e(TAG, "Requested init for " + transportName + " but not found");
Christopher Tate4cc86e12009-09-21 19:36:51 -07001892 continue;
1893 }
1894
Joe Onorato8a9b2202010-02-26 18:56:32 -08001895 Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001896 EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName());
Dan Egnor726247c2009-09-29 19:12:31 -07001897 long startRealtime = SystemClock.elapsedRealtime();
1898 int status = transport.initializeDevice();
Christopher Tate4cc86e12009-09-21 19:36:51 -07001899
Christopher Tate4cc86e12009-09-21 19:36:51 -07001900 if (status == BackupConstants.TRANSPORT_OK) {
1901 status = transport.finishBackup();
1902 }
1903
1904 // Okay, the wipe really happened. Clean up our local bookkeeping.
1905 if (status == BackupConstants.TRANSPORT_OK) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001906 Slog.i(TAG, "Device init successful");
Dan Egnor726247c2009-09-29 19:12:31 -07001907 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001908 EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
Dan Egnor726247c2009-09-29 19:12:31 -07001909 resetBackupState(new File(mBaseStateDir, transport.transportDirName()));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910 EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis);
Christopher Tate4cc86e12009-09-21 19:36:51 -07001911 synchronized (mQueueLock) {
1912 recordInitPendingLocked(false, transportName);
1913 }
Dan Egnor726247c2009-09-29 19:12:31 -07001914 } else {
1915 // If this didn't work, requeue this one and try again
1916 // after a suitable interval
Joe Onorato8a9b2202010-02-26 18:56:32 -08001917 Slog.e(TAG, "Transport error in initializeDevice()");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
Christopher Tate4cc86e12009-09-21 19:36:51 -07001919 synchronized (mQueueLock) {
1920 recordInitPendingLocked(true, transportName);
1921 }
1922 // do this via another alarm to make sure of the wakelock states
1923 long delay = transport.requestBackupTime();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001924 if (DEBUG) Slog.w(TAG, "init failed on "
Christopher Tate4cc86e12009-09-21 19:36:51 -07001925 + transportName + " resched in " + delay);
1926 mAlarmManager.set(AlarmManager.RTC_WAKEUP,
1927 System.currentTimeMillis() + delay, mRunInitIntent);
Christopher Tate4cc86e12009-09-21 19:36:51 -07001928 }
Christopher Tate4cc86e12009-09-21 19:36:51 -07001929 }
1930 } catch (RemoteException e) {
1931 // can't happen; the transports are local
1932 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001933 Slog.e(TAG, "Unexpected error performing init", e);
Christopher Tate4cc86e12009-09-21 19:36:51 -07001934 } finally {
Christopher Tatec2af5d32010-02-02 15:18:58 -08001935 // Done; release the wakelock
Christopher Tate4cc86e12009-09-21 19:36:51 -07001936 mWakelock.release();
1937 }
1938 }
1939 }
1940
Christopher Tatedf01dea2009-06-09 20:45:02 -07001941
Christopher Tate487529a2009-04-29 14:03:25 -07001942 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -07001943
Dan Egnor852f8e42009-09-30 11:20:45 -07001944 public void dataChanged(String packageName) {
Christopher Tate487529a2009-04-29 14:03:25 -07001945 // Record that we need a backup pass for the caller. Since multiple callers
1946 // may share a uid, we need to note all candidates within that uid and schedule
1947 // a backup pass for each of them.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948 EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001949
Christopher Tate63d27002009-06-16 17:16:42 -07001950 // If the caller does not hold the BACKUP permission, it can only request a
1951 // backup of its own data.
1952 HashSet<ApplicationInfo> targets;
Dianne Hackborncf098292009-07-01 19:55:20 -07001953 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tate63d27002009-06-16 17:16:42 -07001954 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1955 targets = mBackupParticipants.get(Binder.getCallingUid());
1956 } else {
1957 // a caller with full permission can ask to back up any participating app
1958 // !!! TODO: allow backup of ANY app?
Christopher Tate63d27002009-06-16 17:16:42 -07001959 targets = new HashSet<ApplicationInfo>();
1960 int N = mBackupParticipants.size();
1961 for (int i = 0; i < N; i++) {
1962 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1963 if (s != null) {
1964 targets.addAll(s);
1965 }
1966 }
1967 }
Christopher Tate487529a2009-04-29 14:03:25 -07001968 if (targets != null) {
1969 synchronized (mQueueLock) {
1970 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -07001971 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -07001972 // validate the caller-supplied package name against the known set of
1973 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -07001974 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -04001975 // Add the caller to the set of pending backups. If there is
1976 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -07001977 BackupRequest req = new BackupRequest(app, false);
Christopher Tatea7de3842009-07-07 14:50:26 -07001978 if (mPendingBackups.put(app, req) == null) {
1979 // Journal this request in case of crash. The put()
1980 // operation returned null when this package was not already
1981 // in the set; we want to avoid touching the disk redundantly.
1982 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001983
Christopher Tate22b60d82009-07-07 16:36:02 -07001984 if (DEBUG) {
1985 int numKeys = mPendingBackups.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001986 Slog.d(TAG, "Now awaiting backup for " + numKeys + " participants:");
Christopher Tate22b60d82009-07-07 16:36:02 -07001987 for (BackupRequest b : mPendingBackups.values()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 Slog.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
Christopher Tate22b60d82009-07-07 16:36:02 -07001989 }
1990 }
1991 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001992 }
1993 }
Christopher Tate487529a2009-04-29 14:03:25 -07001994 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001995 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001996 Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'"
Christopher Tate73e02522009-07-15 14:18:26 -07001997 + " uid=" + Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -07001998 }
1999 }
Christopher Tate46758122009-05-06 11:22:00 -07002000
Christopher Tatecde87f42009-06-12 12:55:53 -07002001 private void writeToJournalLocked(String str) {
Dan Egnor852f8e42009-09-30 11:20:45 -07002002 RandomAccessFile out = null;
2003 try {
2004 if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir);
2005 out = new RandomAccessFile(mJournal, "rws");
2006 out.seek(out.length());
2007 out.writeUTF(str);
2008 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002009 Slog.e(TAG, "Can't write " + str + " to backup journal", e);
Dan Egnor852f8e42009-09-30 11:20:45 -07002010 mJournal = null;
2011 } finally {
2012 try { if (out != null) out.close(); } catch (IOException e) {}
Christopher Tatecde87f42009-06-12 12:55:53 -07002013 }
2014 }
2015
Christopher Tateee0e78a2009-07-02 11:17:03 -07002016 // Clear the given package's backup data from the current transport
2017 public void clearBackupData(String packageName) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002018 if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName);
Christopher Tateee0e78a2009-07-02 11:17:03 -07002019 PackageInfo info;
2020 try {
2021 info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
2022 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002023 Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data");
Christopher Tateee0e78a2009-07-02 11:17:03 -07002024 return;
2025 }
2026
2027 // If the caller does not hold the BACKUP permission, it can only request a
2028 // wipe of its own backed-up data.
2029 HashSet<ApplicationInfo> apps;
Christopher Tate4e3e50c2009-07-02 12:14:05 -07002030 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tateee0e78a2009-07-02 11:17:03 -07002031 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
2032 apps = mBackupParticipants.get(Binder.getCallingUid());
2033 } else {
2034 // a caller with full permission can ask to back up any participating app
2035 // !!! TODO: allow data-clear of ANY app?
Joe Onorato8a9b2202010-02-26 18:56:32 -08002036 if (DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps");
Christopher Tateee0e78a2009-07-02 11:17:03 -07002037 apps = new HashSet<ApplicationInfo>();
2038 int N = mBackupParticipants.size();
2039 for (int i = 0; i < N; i++) {
2040 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
2041 if (s != null) {
2042 apps.addAll(s);
2043 }
2044 }
2045 }
2046
2047 // now find the given package in the set of candidate apps
2048 for (ApplicationInfo app : apps) {
2049 if (app.packageName.equals(packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002050 if (DEBUG) Slog.v(TAG, "Found the app - running clear process");
Christopher Tateee0e78a2009-07-02 11:17:03 -07002051 // found it; fire off the clear request
2052 synchronized (mQueueLock) {
Christopher Tateaa93b042009-08-05 18:21:40 -07002053 long oldId = Binder.clearCallingIdentity();
Christopher Tateb6787f22009-07-02 17:40:45 -07002054 mWakelock.acquire();
Christopher Tateee0e78a2009-07-02 11:17:03 -07002055 Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR,
2056 new ClearParams(getTransport(mCurrentTransport), info));
2057 mBackupHandler.sendMessage(msg);
Christopher Tateaa93b042009-08-05 18:21:40 -07002058 Binder.restoreCallingIdentity(oldId);
Christopher Tateee0e78a2009-07-02 11:17:03 -07002059 }
2060 break;
2061 }
2062 }
2063 }
2064
Christopher Tateace7f092009-06-15 18:07:25 -07002065 // Run a backup pass immediately for any applications that have declared
2066 // that they have pending updates.
Dan Egnor852f8e42009-09-30 11:20:45 -07002067 public void backupNow() {
Joe Onorato5933a492009-07-23 18:24:08 -04002068 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow");
Christopher Tate043dadc2009-06-02 16:11:00 -07002069
Joe Onorato8a9b2202010-02-26 18:56:32 -08002070 if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass");
Christopher Tate46758122009-05-06 11:22:00 -07002071 synchronized (mQueueLock) {
Christopher Tate21ab6a52009-09-24 18:01:46 -07002072 // Because the alarms we are using can jitter, and we want an *immediate*
2073 // backup pass to happen, we restart the timer beginning with "next time,"
2074 // then manually fire the backup trigger intent ourselves.
2075 startBackupAlarmsLocked(BACKUP_INTERVAL);
Christopher Tateb6787f22009-07-02 17:40:45 -07002076 try {
Christopher Tateb6787f22009-07-02 17:40:45 -07002077 mRunBackupIntent.send();
2078 } catch (PendingIntent.CanceledException e) {
2079 // should never happen
Joe Onorato8a9b2202010-02-26 18:56:32 -08002080 Slog.e(TAG, "run-backup intent cancelled!");
Christopher Tateb6787f22009-07-02 17:40:45 -07002081 }
Christopher Tate46758122009-05-06 11:22:00 -07002082 }
2083 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002084
Christopher Tate8031a3d2009-07-06 16:36:05 -07002085 // Enable/disable the backup service
Christopher Tate6ef58a12009-06-29 14:56:28 -07002086 public void setBackupEnabled(boolean enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07002087 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
2088 "setBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07002089
Joe Onorato8a9b2202010-02-26 18:56:32 -08002090 Slog.i(TAG, "Backup enabled => " + enable);
Christopher Tate4cc86e12009-09-21 19:36:51 -07002091
Christopher Tate6ef58a12009-06-29 14:56:28 -07002092 boolean wasEnabled = mEnabled;
2093 synchronized (this) {
Dianne Hackborncf098292009-07-01 19:55:20 -07002094 Settings.Secure.putInt(mContext.getContentResolver(),
2095 Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0);
Christopher Tate6ef58a12009-06-29 14:56:28 -07002096 mEnabled = enable;
2097 }
2098
Christopher Tate49401dd2009-07-01 12:34:29 -07002099 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07002100 if (enable && !wasEnabled && mProvisioned) {
Christopher Tate49401dd2009-07-01 12:34:29 -07002101 // if we've just been enabled, start scheduling backup passes
Christopher Tate8031a3d2009-07-06 16:36:05 -07002102 startBackupAlarmsLocked(BACKUP_INTERVAL);
Christopher Tate49401dd2009-07-01 12:34:29 -07002103 } else if (!enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07002104 // No longer enabled, so stop running backups
Joe Onorato8a9b2202010-02-26 18:56:32 -08002105 if (DEBUG) Slog.i(TAG, "Opting out of backup");
Christopher Tate4cc86e12009-09-21 19:36:51 -07002106
Christopher Tateb6787f22009-07-02 17:40:45 -07002107 mAlarmManager.cancel(mRunBackupIntent);
Christopher Tate4cc86e12009-09-21 19:36:51 -07002108
2109 // This also constitutes an opt-out, so we wipe any data for
2110 // this device from the backend. We start that process with
2111 // an alarm in order to guarantee wakelock states.
2112 if (wasEnabled && mProvisioned) {
2113 // NOTE: we currently flush every registered transport, not just
2114 // the currently-active one.
2115 HashSet<String> allTransports;
2116 synchronized (mTransports) {
2117 allTransports = new HashSet<String>(mTransports.keySet());
2118 }
2119 // build the set of transports for which we are posting an init
2120 for (String transport : allTransports) {
2121 recordInitPendingLocked(true, transport);
2122 }
2123 mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
2124 mRunInitIntent);
2125 }
Christopher Tate6ef58a12009-06-29 14:56:28 -07002126 }
2127 }
Christopher Tate49401dd2009-07-01 12:34:29 -07002128 }
Christopher Tate6ef58a12009-06-29 14:56:28 -07002129
Christopher Tatecce9da52010-02-03 15:11:15 -08002130 // Enable/disable automatic restore of app data at install time
2131 public void setAutoRestore(boolean doAutoRestore) {
2132 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
2133 "setBackupEnabled");
2134
Joe Onorato8a9b2202010-02-26 18:56:32 -08002135 Slog.i(TAG, "Auto restore => " + doAutoRestore);
Christopher Tatecce9da52010-02-03 15:11:15 -08002136
2137 synchronized (this) {
2138 Settings.Secure.putInt(mContext.getContentResolver(),
2139 Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0);
2140 mAutoRestore = doAutoRestore;
2141 }
2142 }
2143
Christopher Tate8031a3d2009-07-06 16:36:05 -07002144 // Mark the backup service as having been provisioned
2145 public void setBackupProvisioned(boolean available) {
2146 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
2147 "setBackupProvisioned");
2148
2149 boolean wasProvisioned = mProvisioned;
2150 synchronized (this) {
2151 Settings.Secure.putInt(mContext.getContentResolver(),
2152 Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0);
2153 mProvisioned = available;
2154 }
2155
2156 synchronized (mQueueLock) {
2157 if (available && !wasProvisioned && mEnabled) {
2158 // we're now good to go, so start the backup alarms
2159 startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL);
2160 } else if (!available) {
2161 // No longer enabled, so stop running backups
Joe Onorato8a9b2202010-02-26 18:56:32 -08002162 Slog.w(TAG, "Backup service no longer provisioned");
Christopher Tate8031a3d2009-07-06 16:36:05 -07002163 mAlarmManager.cancel(mRunBackupIntent);
2164 }
2165 }
2166 }
2167
2168 private void startBackupAlarmsLocked(long delayBeforeFirstBackup) {
Dan Egnorc1c49c02009-10-30 17:35:39 -07002169 // We used to use setInexactRepeating(), but that may be linked to
2170 // backups running at :00 more often than not, creating load spikes.
2171 // Schedule at an exact time for now, and also add a bit of "fuzz".
2172
2173 Random random = new Random();
2174 long when = System.currentTimeMillis() + delayBeforeFirstBackup +
2175 random.nextInt(FUZZ_MILLIS);
2176 mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when,
2177 BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent);
Christopher Tate55f931a2009-09-29 17:17:34 -07002178 mNextBackupPass = when;
Christopher Tate8031a3d2009-07-06 16:36:05 -07002179 }
2180
Christopher Tate6ef58a12009-06-29 14:56:28 -07002181 // Report whether the backup mechanism is currently enabled
2182 public boolean isBackupEnabled() {
Joe Onorato5933a492009-07-23 18:24:08 -04002183 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07002184 return mEnabled; // no need to synchronize just to read it
2185 }
2186
Christopher Tate91717492009-06-26 21:07:13 -07002187 // Report the name of the currently active transport
2188 public String getCurrentTransport() {
Joe Onorato5933a492009-07-23 18:24:08 -04002189 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate4e3e50c2009-07-02 12:14:05 -07002190 "getCurrentTransport");
Joe Onorato8a9b2202010-02-26 18:56:32 -08002191 if (DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport);
Christopher Tate91717492009-06-26 21:07:13 -07002192 return mCurrentTransport;
Christopher Tateace7f092009-06-15 18:07:25 -07002193 }
2194
Christopher Tate91717492009-06-26 21:07:13 -07002195 // Report all known, available backup transports
2196 public String[] listAllTransports() {
Christopher Tate34ebd0e2009-07-06 15:44:54 -07002197 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports");
Christopher Tate043dadc2009-06-02 16:11:00 -07002198
Christopher Tate91717492009-06-26 21:07:13 -07002199 String[] list = null;
2200 ArrayList<String> known = new ArrayList<String>();
2201 for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) {
2202 if (entry.getValue() != null) {
2203 known.add(entry.getKey());
2204 }
2205 }
2206
2207 if (known.size() > 0) {
2208 list = new String[known.size()];
2209 known.toArray(list);
2210 }
2211 return list;
2212 }
2213
2214 // Select which transport to use for the next backup operation. If the given
2215 // name is not one of the available transports, no action is taken and the method
2216 // returns null.
2217 public String selectBackupTransport(String transport) {
Joe Onorato5933a492009-07-23 18:24:08 -04002218 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport");
Christopher Tate91717492009-06-26 21:07:13 -07002219
2220 synchronized (mTransports) {
2221 String prevTransport = null;
2222 if (mTransports.get(transport) != null) {
2223 prevTransport = mCurrentTransport;
2224 mCurrentTransport = transport;
Dianne Hackborncf098292009-07-01 19:55:20 -07002225 Settings.Secure.putString(mContext.getContentResolver(),
2226 Settings.Secure.BACKUP_TRANSPORT, transport);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002227 Slog.v(TAG, "selectBackupTransport() set " + mCurrentTransport
Christopher Tate91717492009-06-26 21:07:13 -07002228 + " returning " + prevTransport);
2229 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002230 Slog.w(TAG, "Attempt to select unavailable transport " + transport);
Christopher Tate91717492009-06-26 21:07:13 -07002231 }
2232 return prevTransport;
2233 }
Christopher Tate043dadc2009-06-02 16:11:00 -07002234 }
2235
2236 // Callback: a requested backup agent has been instantiated. This should only
2237 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07002238 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -07002239 synchronized(mAgentConnectLock) {
2240 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002241 Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
Christopher Tate043dadc2009-06-02 16:11:00 -07002242 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
2243 mConnectedAgent = agent;
2244 mConnecting = false;
2245 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002246 Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
Christopher Tate043dadc2009-06-02 16:11:00 -07002247 + " claiming agent connected");
2248 }
2249 mAgentConnectLock.notifyAll();
2250 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002251 }
2252
2253 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
2254 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -07002255 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07002256 public void agentDisconnected(String packageName) {
2257 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -07002258 synchronized(mAgentConnectLock) {
2259 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
2260 mConnectedAgent = null;
2261 mConnecting = false;
2262 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002263 Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
Christopher Tate043dadc2009-06-02 16:11:00 -07002264 + " claiming agent disconnected");
2265 }
2266 mAgentConnectLock.notifyAll();
2267 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002268 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002269
Christopher Tate1bb69062010-02-19 17:02:12 -08002270 // An application being installed will need a restore pass, then the Package Manager
2271 // will need to be told when the restore is finished.
2272 public void restoreAtInstall(String packageName, int token) {
2273 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002274 Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
Christopher Tate1bb69062010-02-19 17:02:12 -08002275 + " attemping install-time restore");
2276 return;
2277 }
2278
2279 long restoreSet = getAvailableRestoreToken(packageName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002280 if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName
Christopher Tate1bb69062010-02-19 17:02:12 -08002281 + " token=" + Integer.toHexString(token));
2282
Christopher Tatef0872722010-02-25 15:22:48 -08002283 if (mAutoRestore && mProvisioned && restoreSet != 0) {
Christopher Tate1bb69062010-02-19 17:02:12 -08002284 // okay, we're going to attempt a restore of this package from this restore set.
2285 // The eventual message back into the Package Manager to run the post-install
2286 // steps for 'token' will be issued from the restore handling code.
2287
2288 // We can use a synthetic PackageInfo here because:
2289 // 1. We know it's valid, since the Package Manager supplied the name
2290 // 2. Only the packageName field will be used by the restore code
2291 PackageInfo pkg = new PackageInfo();
2292 pkg.packageName = packageName;
2293
2294 mWakelock.acquire();
2295 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
2296 msg.obj = new RestoreParams(getTransport(mCurrentTransport), null,
2297 restoreSet, pkg, token);
2298 mBackupHandler.sendMessage(msg);
2299 } else {
Christopher Tatef0872722010-02-25 15:22:48 -08002300 // Auto-restore disabled or no way to attempt a restore; just tell the Package
2301 // Manager to proceed with the post-install handling for this package.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002302 if (DEBUG) Slog.v(TAG, "No restore set -- skipping restore");
Christopher Tate1bb69062010-02-19 17:02:12 -08002303 try {
2304 mPackageManagerBinder.finishPackageInstall(token);
2305 } catch (RemoteException e) { /* can't happen */ }
2306 }
2307 }
2308
Christopher Tate8c850b72009-06-07 19:33:20 -07002309 // Hand off a restore session
Christopher Tate91717492009-06-26 21:07:13 -07002310 public IRestoreSession beginRestoreSession(String transport) {
Joe Onorato5933a492009-07-23 18:24:08 -04002311 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "beginRestoreSession");
Christopher Tatef68eb502009-06-16 11:02:01 -07002312
2313 synchronized(this) {
2314 if (mActiveRestoreSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002315 Slog.d(TAG, "Restore session requested but one already active");
Christopher Tatef68eb502009-06-16 11:02:01 -07002316 return null;
2317 }
Christopher Tate80202c82010-01-25 19:37:47 -08002318 mActiveRestoreSession = new ActiveRestoreSession(transport);
Christopher Tatef68eb502009-06-16 11:02:01 -07002319 }
2320 return mActiveRestoreSession;
Christopher Tate8c850b72009-06-07 19:33:20 -07002321 }
Christopher Tate043dadc2009-06-02 16:11:00 -07002322
Christopher Tate44a27902010-01-27 17:15:49 -08002323 // Note that a currently-active backup agent has notified us that it has
2324 // completed the given outstanding asynchronous backup/restore operation.
2325 public void opComplete(int token) {
2326 synchronized (mCurrentOpLock) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002327 if (DEBUG) Slog.v(TAG, "opComplete: " + Integer.toHexString(token));
Christopher Tate44a27902010-01-27 17:15:49 -08002328 mCurrentOperations.put(token, OP_ACKNOWLEDGED);
2329 mCurrentOpLock.notifyAll();
2330 }
2331 }
2332
Christopher Tate9b3905c2009-06-08 15:24:01 -07002333 // ----- Restore session -----
2334
Christopher Tate80202c82010-01-25 19:37:47 -08002335 class ActiveRestoreSession extends IRestoreSession.Stub {
Christopher Tatef68eb502009-06-16 11:02:01 -07002336 private static final String TAG = "RestoreSession";
2337
Christopher Tate9b3905c2009-06-08 15:24:01 -07002338 private IBackupTransport mRestoreTransport = null;
2339 RestoreSet[] mRestoreSets = null;
2340
Christopher Tate80202c82010-01-25 19:37:47 -08002341 ActiveRestoreSession(String transport) {
Christopher Tate91717492009-06-26 21:07:13 -07002342 mRestoreTransport = getTransport(transport);
Christopher Tate9b3905c2009-06-08 15:24:01 -07002343 }
2344
2345 // --- Binder interface ---
Dan Egnor0084da52009-07-29 12:57:16 -07002346 public synchronized RestoreSet[] getAvailableRestoreSets() {
Joe Onorato5933a492009-07-23 18:24:08 -04002347 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07002348 "getAvailableRestoreSets");
2349
Christopher Tate1bb69062010-02-19 17:02:12 -08002350 long oldId = Binder.clearCallingIdentity();
Christopher Tatef68eb502009-06-16 11:02:01 -07002351 try {
Christopher Tate43383042009-07-13 15:17:13 -07002352 if (mRestoreTransport == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002353 Slog.w(TAG, "Null transport getting restore sets");
Dan Egnor0084da52009-07-29 12:57:16 -07002354 return null;
2355 }
2356 if (mRestoreSets == null) { // valid transport; do the one-time fetch
Christopher Tate9b3905c2009-06-08 15:24:01 -07002357 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002358 if (mRestoreSets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
Christopher Tate9b3905c2009-06-08 15:24:01 -07002359 }
2360 return mRestoreSets;
Dan Egnor0084da52009-07-29 12:57:16 -07002361 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002362 Slog.e(TAG, "Error in getAvailableRestoreSets", e);
Dan Egnor0084da52009-07-29 12:57:16 -07002363 return null;
Christopher Tate1bb69062010-02-19 17:02:12 -08002364 } finally {
2365 Binder.restoreCallingIdentity(oldId);
Christopher Tatef68eb502009-06-16 11:02:01 -07002366 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07002367 }
2368
Christopher Tate84725812010-02-04 15:52:40 -08002369 public synchronized int restoreAll(long token, IRestoreObserver observer) {
Dan Egnor0084da52009-07-29 12:57:16 -07002370 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
2371 "performRestore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07002372
Joe Onorato8a9b2202010-02-26 18:56:32 -08002373 if (DEBUG) Slog.d(TAG, "performRestore token=" + Long.toHexString(token)
Christopher Tatef2c321a2009-08-10 15:43:36 -07002374 + " observer=" + observer);
Joe Onorato9a5e3e12009-07-01 21:04:03 -04002375
Dan Egnor0084da52009-07-29 12:57:16 -07002376 if (mRestoreTransport == null || mRestoreSets == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002377 Slog.e(TAG, "Ignoring performRestore() with no restore set");
Dan Egnor0084da52009-07-29 12:57:16 -07002378 return -1;
2379 }
2380
Christopher Tate21ab6a52009-09-24 18:01:46 -07002381 synchronized (mQueueLock) {
Christopher Tate21ab6a52009-09-24 18:01:46 -07002382 for (int i = 0; i < mRestoreSets.length; i++) {
2383 if (token == mRestoreSets[i].token) {
2384 long oldId = Binder.clearCallingIdentity();
Christopher Tate21ab6a52009-09-24 18:01:46 -07002385 mWakelock.acquire();
2386 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
2387 msg.obj = new RestoreParams(mRestoreTransport, observer, token);
2388 mBackupHandler.sendMessage(msg);
2389 Binder.restoreCallingIdentity(oldId);
2390 return 0;
2391 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -07002392 }
2393 }
Christopher Tate0e0b4ae2009-08-10 16:13:47 -07002394
Joe Onorato8a9b2202010-02-26 18:56:32 -08002395 Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
Christopher Tate9b3905c2009-06-08 15:24:01 -07002396 return -1;
2397 }
2398
Christopher Tate84725812010-02-04 15:52:40 -08002399 public synchronized int restorePackage(String packageName, IRestoreObserver observer) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002400 if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer);
Christopher Tate84725812010-02-04 15:52:40 -08002401
2402 PackageInfo app = null;
2403 try {
2404 app = mPackageManager.getPackageInfo(packageName, 0);
2405 } catch (NameNotFoundException nnf) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002406 Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName);
Christopher Tate84725812010-02-04 15:52:40 -08002407 return -1;
2408 }
2409
2410 // If the caller is not privileged and is not coming from the target
2411 // app's uid, throw a permission exception back to the caller.
2412 int perm = mContext.checkPermission(android.Manifest.permission.BACKUP,
2413 Binder.getCallingPid(), Binder.getCallingUid());
2414 if ((perm == PackageManager.PERMISSION_DENIED) &&
2415 (app.applicationInfo.uid != Binder.getCallingUid())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002416 Slog.w(TAG, "restorePackage: bad packageName=" + packageName
Christopher Tate84725812010-02-04 15:52:40 -08002417 + " or calling uid=" + Binder.getCallingUid());
2418 throw new SecurityException("No permission to restore other packages");
2419 }
2420
Christopher Tate7d411a32010-02-26 11:27:08 -08002421 // If the package has no backup agent, we obviously cannot proceed
2422 if (app.applicationInfo.backupAgentName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002423 Slog.w(TAG, "Asked to restore package " + packageName + " with no agent");
Christopher Tate7d411a32010-02-26 11:27:08 -08002424 return -1;
2425 }
2426
Christopher Tate84725812010-02-04 15:52:40 -08002427 // So far so good; we're allowed to try to restore this package. Now
2428 // check whether there is data for it in the current dataset, falling back
2429 // to the ancestral dataset if not.
Christopher Tate1bb69062010-02-19 17:02:12 -08002430 long token = getAvailableRestoreToken(packageName);
Christopher Tate84725812010-02-04 15:52:40 -08002431
2432 // If we didn't come up with a place to look -- no ancestral dataset and
2433 // the app has never been backed up from this device -- there's nothing
2434 // to do but return failure.
2435 if (token == 0) {
2436 return -1;
2437 }
2438
2439 // Ready to go: enqueue the restore request and claim success
2440 long oldId = Binder.clearCallingIdentity();
2441 mWakelock.acquire();
2442 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
Christopher Tate1bb69062010-02-19 17:02:12 -08002443 msg.obj = new RestoreParams(mRestoreTransport, observer, token, app, 0);
Christopher Tate84725812010-02-04 15:52:40 -08002444 mBackupHandler.sendMessage(msg);
2445 Binder.restoreCallingIdentity(oldId);
2446 return 0;
2447 }
2448
Dan Egnor0084da52009-07-29 12:57:16 -07002449 public synchronized void endRestoreSession() {
Joe Onorato5933a492009-07-23 18:24:08 -04002450 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07002451 "endRestoreSession");
2452
Joe Onorato8a9b2202010-02-26 18:56:32 -08002453 if (DEBUG) Slog.d(TAG, "endRestoreSession");
Joe Onorato9a5e3e12009-07-01 21:04:03 -04002454
Dan Egnor0084da52009-07-29 12:57:16 -07002455 synchronized (this) {
Christopher Tate1bb69062010-02-19 17:02:12 -08002456 long oldId = Binder.clearCallingIdentity();
Dan Egnor0084da52009-07-29 12:57:16 -07002457 try {
2458 if (mRestoreTransport != null) mRestoreTransport.finishRestore();
2459 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002460 Slog.e(TAG, "Error in finishRestore", e);
Dan Egnor0084da52009-07-29 12:57:16 -07002461 } finally {
2462 mRestoreTransport = null;
Christopher Tate1bb69062010-02-19 17:02:12 -08002463 Binder.restoreCallingIdentity(oldId);
Dan Egnor0084da52009-07-29 12:57:16 -07002464 }
2465 }
2466
2467 synchronized (BackupManagerService.this) {
Christopher Tatef68eb502009-06-16 11:02:01 -07002468 if (BackupManagerService.this.mActiveRestoreSession == this) {
2469 BackupManagerService.this.mActiveRestoreSession = null;
2470 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002471 Slog.e(TAG, "ending non-current restore session");
Christopher Tatef68eb502009-06-16 11:02:01 -07002472 }
2473 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07002474 }
2475 }
2476
Christopher Tate043dadc2009-06-02 16:11:00 -07002477
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002478 @Override
2479 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2480 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07002481 pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled")
Christopher Tate55f931a2009-09-29 17:17:34 -07002482 + " / " + (!mProvisioned ? "not " : "") + "provisioned / "
Christopher Tatec2af5d32010-02-02 15:18:58 -08002483 + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init");
Christopher Tateae06ed92010-02-25 17:13:28 -08002484 pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled"));
Christopher Tate55f931a2009-09-29 17:17:34 -07002485 pw.println("Last backup pass: " + mLastBackupPass
2486 + " (now = " + System.currentTimeMillis() + ')');
2487 pw.println(" next scheduled: " + mNextBackupPass);
2488
Christopher Tate91717492009-06-26 21:07:13 -07002489 pw.println("Available transports:");
2490 for (String t : listAllTransports()) {
Dan Egnor852f8e42009-09-30 11:20:45 -07002491 pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t);
2492 try {
2493 File dir = new File(mBaseStateDir, getTransport(t).transportDirName());
2494 for (File f : dir.listFiles()) {
2495 pw.println(" " + f.getName() + " - " + f.length() + " state bytes");
2496 }
2497 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002498 Slog.e(TAG, "Error in transportDirName()", e);
Dan Egnor852f8e42009-09-30 11:20:45 -07002499 pw.println(" Error: " + e);
2500 }
Christopher Tate91717492009-06-26 21:07:13 -07002501 }
Christopher Tate55f931a2009-09-29 17:17:34 -07002502
2503 pw.println("Pending init: " + mPendingInits.size());
2504 for (String s : mPendingInits) {
2505 pw.println(" " + s);
2506 }
2507
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002508 int N = mBackupParticipants.size();
Christopher Tate55f931a2009-09-29 17:17:34 -07002509 pw.println("Participants:");
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002510 for (int i=0; i<N; i++) {
2511 int uid = mBackupParticipants.keyAt(i);
2512 pw.print(" uid: ");
2513 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07002514 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
2515 for (ApplicationInfo app: participants) {
Christopher Tate55f931a2009-09-29 17:17:34 -07002516 pw.println(" " + app.packageName);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002517 }
2518 }
Christopher Tate55f931a2009-09-29 17:17:34 -07002519
Christopher Tateb49ceb32010-02-08 16:22:24 -08002520 pw.println("Ancestral packages: "
2521 + (mAncestralPackages == null ? "none" : mAncestralPackages.size()));
2522 for (String pkg : mAncestralPackages) {
2523 pw.println(" " + pkg);
2524 }
2525
Christopher Tate73e02522009-07-15 14:18:26 -07002526 pw.println("Ever backed up: " + mEverStoredApps.size());
2527 for (String pkg : mEverStoredApps) {
2528 pw.println(" " + pkg);
2529 }
Christopher Tate55f931a2009-09-29 17:17:34 -07002530
2531 pw.println("Pending backup: " + mPendingBackups.size());
Christopher Tate6aa41f42009-06-19 14:14:22 -07002532 for (BackupRequest req : mPendingBackups.values()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07002533 pw.println(" " + req);
Christopher Tate181fafa2009-05-14 11:12:14 -07002534 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07002535 }
2536 }
Christopher Tate487529a2009-04-29 14:03:25 -07002537}