blob: fc1ec033d89dca2a547b5df2c13d51caf4148fff [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 Tateb6787f22009-07-02 17:40:45 -070020import android.app.AlarmManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070021import android.app.IActivityManager;
22import android.app.IApplicationThread;
23import android.app.IBackupAgent;
Christopher Tateb6787f22009-07-02 17:40:45 -070024import android.app.PendingIntent;
Christopher Tate3799bc22009-05-06 16:13:56 -070025import android.content.BroadcastReceiver;
Dan Egnor87a02bc2009-06-17 02:30:10 -070026import android.content.ComponentName;
Christopher Tate487529a2009-04-29 14:03:25 -070027import android.content.Context;
28import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070029import android.content.IntentFilter;
Dan Egnor87a02bc2009-06-17 02:30:10 -070030import android.content.ServiceConnection;
Christopher Tate181fafa2009-05-14 11:12:14 -070031import android.content.pm.ApplicationInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -070032import android.content.pm.IPackageDataObserver;
Christopher Tate7b881282009-06-07 13:52:37 -070033import android.content.pm.PackageInfo;
Christopher Tate043dadc2009-06-02 16:11:00 -070034import android.content.pm.PackageManager.NameNotFoundException;
Dan Egnor87a02bc2009-06-17 02:30:10 -070035import android.content.pm.PackageManager;
Christopher Tateabce4e82009-06-18 18:35:32 -070036import android.content.pm.Signature;
Christopher Tate3799bc22009-05-06 16:13:56 -070037import android.net.Uri;
Christopher Tatece0bf062009-07-01 11:43:53 -070038import android.provider.Settings;
Christopher Tate487529a2009-04-29 14:03:25 -070039import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070040import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070041import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070042import android.os.Handler;
43import android.os.IBinder;
44import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070045import android.os.ParcelFileDescriptor;
Christopher Tateb6787f22009-07-02 17:40:45 -070046import android.os.PowerManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070047import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070048import android.os.RemoteException;
49import android.util.Log;
50import android.util.SparseArray;
51
52import android.backup.IBackupManager;
Christopher Tate7d562ec2009-06-25 18:03:43 -070053import android.backup.IRestoreObserver;
Christopher Tate8c850b72009-06-07 19:33:20 -070054import android.backup.IRestoreSession;
Christopher Tate9b3905c2009-06-08 15:24:01 -070055import android.backup.RestoreSet;
Christopher Tate043dadc2009-06-02 16:11:00 -070056
Christopher Tate9bbc21a2009-06-10 20:23:25 -070057import com.android.internal.backup.LocalTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -070058import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070059
Christopher Tate6785dd82009-06-18 15:58:25 -070060import com.android.server.PackageManagerBackupAgent;
Christopher Tate6aa41f42009-06-19 14:14:22 -070061import com.android.server.PackageManagerBackupAgent.Metadata;
Christopher Tate6785dd82009-06-18 15:58:25 -070062
Christopher Tatecde87f42009-06-12 12:55:53 -070063import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070064import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070065import java.io.FileDescriptor;
Christopher Tatec7b31e32009-06-10 15:49:30 -070066import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070067import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070068import java.io.RandomAccessFile;
Christopher Tate487529a2009-04-29 14:03:25 -070069import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040070import java.util.ArrayList;
71import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070072import java.util.HashSet;
73import java.util.List;
Christopher Tate91717492009-06-26 21:07:13 -070074import java.util.Map;
Christopher Tate487529a2009-04-29 14:03:25 -070075
76class BackupManagerService extends IBackupManager.Stub {
77 private static final String TAG = "BackupManagerService";
78 private static final boolean DEBUG = true;
Christopher Tateaa088442009-06-16 18:25:46 -070079
Christopher Tate49401dd2009-07-01 12:34:29 -070080 // How often we perform a backup pass. Privileged external callers can
81 // trigger an immediate pass.
Christopher Tateb6787f22009-07-02 17:40:45 -070082 private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR;
Christopher Tate487529a2009-04-29 14:03:25 -070083
Christopher Tate8031a3d2009-07-06 16:36:05 -070084 // The amount of time between the initial provisioning of the device and
85 // the first backup pass.
86 private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR;
87
Christopher Tateb6787f22009-07-02 17:40:45 -070088 private static final String RUN_BACKUP_ACTION = "_backup_run_";
Christopher Tate487529a2009-04-29 14:03:25 -070089 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070090 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070091 private static final int MSG_RUN_RESTORE = 3;
Christopher Tateee0e78a2009-07-02 11:17:03 -070092 private static final int MSG_RUN_CLEAR = 4;
Christopher Tatec7b31e32009-06-10 15:49:30 -070093
94 // Timeout interval for deciding that a bind or clear-data has taken too long
95 static final long TIMEOUT_INTERVAL = 10 * 1000;
96
Christopher Tate487529a2009-04-29 14:03:25 -070097 private Context mContext;
98 private PackageManager mPackageManager;
Christopher Tate6ef58a12009-06-29 14:56:28 -070099 private IActivityManager mActivityManager;
Christopher Tateb6787f22009-07-02 17:40:45 -0700100 private PowerManager mPowerManager;
101 private AlarmManager mAlarmManager;
102
Christopher Tate73e02522009-07-15 14:18:26 -0700103 boolean mEnabled; // access to this is synchronized on 'this'
104 boolean mProvisioned;
105 PowerManager.WakeLock mWakelock;
106 final BackupHandler mBackupHandler = new BackupHandler();
107 PendingIntent mRunBackupIntent;
108 BroadcastReceiver mRunBackupReceiver;
109 IntentFilter mRunBackupFilter;
Christopher Tate487529a2009-04-29 14:03:25 -0700110 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate73e02522009-07-15 14:18:26 -0700111 final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
Christopher Tate181fafa2009-05-14 11:12:14 -0700112 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -0700113 // set of backup services that have pending changes
Christopher Tate73e02522009-07-15 14:18:26 -0700114 class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -0700115 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -0700116 public boolean fullBackup;
Christopher Tateaa088442009-06-16 18:25:46 -0700117
Christopher Tate181fafa2009-05-14 11:12:14 -0700118 BackupRequest(ApplicationInfo app, boolean isFull) {
119 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -0700120 fullBackup = isFull;
121 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700122
123 public String toString() {
124 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
125 }
Christopher Tate46758122009-05-06 11:22:00 -0700126 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400127 // Backups that we haven't started yet.
Christopher Tate73e02522009-07-15 14:18:26 -0700128 HashMap<ApplicationInfo,BackupRequest> mPendingBackups
Christopher Tate181fafa2009-05-14 11:12:14 -0700129 = new HashMap<ApplicationInfo,BackupRequest>();
Christopher Tate5cb400b2009-06-25 16:03:14 -0700130
131 // Pseudoname that we use for the Package Manager metadata "package"
Christopher Tate73e02522009-07-15 14:18:26 -0700132 static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
Christopher Tate6aa41f42009-06-19 14:14:22 -0700133
134 // locking around the pending-backup management
Christopher Tate73e02522009-07-15 14:18:26 -0700135 final Object mQueueLock = new Object();
Christopher Tate487529a2009-04-29 14:03:25 -0700136
Christopher Tate043dadc2009-06-02 16:11:00 -0700137 // The thread performing the sequence of queued backups binds to each app's agent
138 // in succession. Bind notifications are asynchronously delivered through the
139 // Activity Manager; use this lock object to signal when a requested binding has
140 // completed.
Christopher Tate73e02522009-07-15 14:18:26 -0700141 final Object mAgentConnectLock = new Object();
142 IBackupAgent mConnectedAgent;
143 volatile boolean mConnecting;
Christopher Tate043dadc2009-06-02 16:11:00 -0700144
Christopher Tatec7b31e32009-06-10 15:49:30 -0700145 // A similar synchronicity mechanism around clearing apps' data for restore
Christopher Tate73e02522009-07-15 14:18:26 -0700146 final Object mClearDataLock = new Object();
147 volatile boolean mClearingData;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700148
Christopher Tate91717492009-06-26 21:07:13 -0700149 // Transport bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700150 final HashMap<String,IBackupTransport> mTransports
Christopher Tate91717492009-06-26 21:07:13 -0700151 = new HashMap<String,IBackupTransport>();
Christopher Tate73e02522009-07-15 14:18:26 -0700152 String mCurrentTransport;
153 IBackupTransport mLocalTransport, mGoogleTransport;
154 RestoreSession mActiveRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -0700155
Christopher Tate73e02522009-07-15 14:18:26 -0700156 class RestoreParams {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700157 public IBackupTransport transport;
158 public IRestoreObserver observer;
Dan Egnor156411d2009-06-26 13:20:02 -0700159 public long token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700160
Dan Egnor156411d2009-06-26 13:20:02 -0700161 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700162 transport = _transport;
163 observer = _obs;
Dan Egnor156411d2009-06-26 13:20:02 -0700164 token = _token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700165 }
166 }
167
Christopher Tate73e02522009-07-15 14:18:26 -0700168 class ClearParams {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700169 public IBackupTransport transport;
170 public PackageInfo packageInfo;
171
172 ClearParams(IBackupTransport _transport, PackageInfo _info) {
173 transport = _transport;
174 packageInfo = _info;
175 }
176 }
177
Christopher Tate5cb400b2009-06-25 16:03:14 -0700178 // Where we keep our journal files and other bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700179 File mBaseStateDir;
180 File mDataDir;
181 File mJournalDir;
182 File mJournal;
183 RandomAccessFile mJournalStream;
184
185 // Keep a log of all the apps we've ever backed up
186 private File mEverStored;
187 private RandomAccessFile mEverStoredStream;
188 HashSet<String> mEverStoredApps = new HashSet<String>();
189
Christopher Tateaa088442009-06-16 18:25:46 -0700190
Christopher Tate487529a2009-04-29 14:03:25 -0700191 public BackupManagerService(Context context) {
192 mContext = context;
193 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700194 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700195
Christopher Tateb6787f22009-07-02 17:40:45 -0700196 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
197 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
198
Christopher Tate22b87872009-05-04 16:41:53 -0700199 // Set up our bookkeeping
Christopher Tateb6787f22009-07-02 17:40:45 -0700200 boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborncf098292009-07-01 19:55:20 -0700201 Settings.Secure.BACKUP_ENABLED, 0) != 0;
Christopher Tate8031a3d2009-07-06 16:36:05 -0700202 // !!! TODO: mProvisioned needs to default to 0, not 1.
203 mProvisioned = Settings.Secure.getInt(context.getContentResolver(),
204 Settings.Secure.BACKUP_PROVISIONED, 1) != 0;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700205 mBaseStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tatef4172472009-05-05 15:50:03 -0700206 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700207
Christopher Tateb6787f22009-07-02 17:40:45 -0700208 mRunBackupReceiver = new RunBackupReceiver();
209 mRunBackupFilter = new IntentFilter();
210 mRunBackupFilter.addAction(RUN_BACKUP_ACTION);
211 context.registerReceiver(mRunBackupReceiver, mRunBackupFilter);
212
213 Intent backupIntent = new Intent(RUN_BACKUP_ACTION);
214 // !!! TODO: restrict delivery to our receiver; the naive setClass() doesn't seem to work
215 //backupIntent.setClass(context, mRunBackupReceiver.getClass());
216 backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
217 mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0);
218
Christopher Tatecde87f42009-06-12 12:55:53 -0700219 // Set up the backup-request journaling
Christopher Tate5cb400b2009-06-25 16:03:14 -0700220 mJournalDir = new File(mBaseStateDir, "pending");
221 mJournalDir.mkdirs(); // creates mBaseStateDir along the way
Christopher Tatecde87f42009-06-12 12:55:53 -0700222 makeJournalLocked(); // okay because no other threads are running yet
223
Christopher Tate73e02522009-07-15 14:18:26 -0700224 // Set up the various sorts of package tracking we do
225 initPackageTracking();
226
Christopher Tateabce4e82009-06-18 18:35:32 -0700227 // Build our mapping of uid to backup client services. This implicitly
228 // schedules a backup pass on the Package Manager metadata the first
229 // time anything needs to be backed up.
Christopher Tate3799bc22009-05-06 16:13:56 -0700230 synchronized (mBackupParticipants) {
231 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700232 }
233
Dan Egnor87a02bc2009-06-17 02:30:10 -0700234 // Set up our transport options and initialize the default transport
235 // TODO: Have transports register themselves somehow?
236 // TODO: Don't create transports that we don't need to?
Dan Egnor87a02bc2009-06-17 02:30:10 -0700237 mLocalTransport = new LocalTransport(context); // This is actually pretty cheap
Christopher Tate91717492009-06-26 21:07:13 -0700238 ComponentName localName = new ComponentName(context, LocalTransport.class);
239 registerTransport(localName.flattenToShortString(), mLocalTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700240
Christopher Tate91717492009-06-26 21:07:13 -0700241 mGoogleTransport = null;
Dianne Hackborncf098292009-07-01 19:55:20 -0700242 mCurrentTransport = Settings.Secure.getString(context.getContentResolver(),
243 Settings.Secure.BACKUP_TRANSPORT);
244 if ("".equals(mCurrentTransport)) {
245 mCurrentTransport = null;
Christopher Tatece0bf062009-07-01 11:43:53 -0700246 }
Christopher Tate91717492009-06-26 21:07:13 -0700247 if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport);
248
249 // Attach to the Google backup transport. When this comes up, it will set
250 // itself as the current transport because we explicitly reset mCurrentTransport
251 // to null.
Dan Egnor87a02bc2009-06-17 02:30:10 -0700252 Intent intent = new Intent().setComponent(new ComponentName(
253 "com.google.android.backup",
254 "com.google.android.backup.BackupTransportService"));
255 context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE);
Christopher Tateaa088442009-06-16 18:25:46 -0700256
Christopher Tatecde87f42009-06-12 12:55:53 -0700257 // Now that we know about valid backup participants, parse any
Christopher Tate49401dd2009-07-01 12:34:29 -0700258 // leftover journal files into the pending backup set
Christopher Tatecde87f42009-06-12 12:55:53 -0700259 parseLeftoverJournals();
260
Christopher Tateb6787f22009-07-02 17:40:45 -0700261 // Power management
262 mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backup");
263
264 // Start the backup passes going
265 setBackupEnabled(areEnabled);
266 }
267
268 private class RunBackupReceiver extends BroadcastReceiver {
269 public void onReceive(Context context, Intent intent) {
270 if (RUN_BACKUP_ACTION.equals(intent.getAction())) {
271 if (DEBUG) Log.v(TAG, "Running a backup pass");
272
273 synchronized (mQueueLock) {
274 // acquire a wakelock and pass it to the backup thread. it will
275 // be released once backup concludes.
276 mWakelock.acquire();
277
278 Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
279 mBackupHandler.sendMessage(msg);
280 }
281 }
Christopher Tate49401dd2009-07-01 12:34:29 -0700282 }
Christopher Tateb6787f22009-07-02 17:40:45 -0700283 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700284
Christopher Tate73e02522009-07-15 14:18:26 -0700285 private void initPackageTracking() {
286 if (DEBUG) Log.v(TAG, "Initializing package tracking");
287
Christopher Tatee97e8072009-07-15 16:45:50 -0700288 // Keep a log of what apps we've ever backed up. Because we might have
289 // rebooted in the middle of an operation that was removing something from
290 // this log, we sanity-check its contents here and reconstruct it.
Christopher Tate73e02522009-07-15 14:18:26 -0700291 mEverStored = new File(mBaseStateDir, "processed");
Christopher Tatee97e8072009-07-15 16:45:50 -0700292 File tempProcessedFile = new File(mBaseStateDir, "processed.new");
Christopher Tate73e02522009-07-15 14:18:26 -0700293 try {
Christopher Tatee97e8072009-07-15 16:45:50 -0700294 RandomAccessFile temp = new RandomAccessFile(tempProcessedFile, "rw");
295 mEverStoredStream = new RandomAccessFile(mEverStored, "r");
Christopher Tate73e02522009-07-15 14:18:26 -0700296
297 // parse its existing contents
298 mEverStoredStream.seek(0);
Christopher Tatee97e8072009-07-15 16:45:50 -0700299 temp.seek(0);
Christopher Tate73e02522009-07-15 14:18:26 -0700300 try {
301 while (true) {
Christopher Tatee97e8072009-07-15 16:45:50 -0700302 PackageInfo info;
Christopher Tate73e02522009-07-15 14:18:26 -0700303 String pkg = mEverStoredStream.readUTF();
Christopher Tatee97e8072009-07-15 16:45:50 -0700304 try {
305 info = mPackageManager.getPackageInfo(pkg, 0);
306 mEverStoredApps.add(pkg);
307 temp.writeUTF(pkg);
308 if (DEBUG) Log.v(TAG, " + " + pkg);
309 } catch (NameNotFoundException e) {
310 // nope, this package was uninstalled; don't include it
311 if (DEBUG) Log.v(TAG, " - " + pkg);
312 }
Christopher Tate73e02522009-07-15 14:18:26 -0700313 }
314 } catch (EOFException e) {
315 // now we're at EOF
316 }
Christopher Tatee97e8072009-07-15 16:45:50 -0700317
318 // Once we've rewritten the backup history log, atomically replace the
319 // old one with the new one then reopen the file for continuing use.
320 temp.close();
321 mEverStoredStream.close();
322 tempProcessedFile.renameTo(mEverStored);
323 mEverStoredStream = new RandomAccessFile(mEverStored, "rwd");
Christopher Tate73e02522009-07-15 14:18:26 -0700324 } catch (IOException e) {
325 Log.e(TAG, "Unable to open known-stored file!");
326 mEverStoredStream = null;
327 }
328
Christopher Tatee97e8072009-07-15 16:45:50 -0700329 // If we were in the middle of removing something from the ever-backed-up
330 // file, there might be a transient "processed.new" file still present.
331 // We've reconstructed a coherent state at this point though, so we can
332 // safely discard that file now.
333 if (tempProcessedFile.exists()) {
334 tempProcessedFile.delete();
335 }
336
Christopher Tate73e02522009-07-15 14:18:26 -0700337 // Register for broadcasts about package install, etc., so we can
338 // update the provider list.
339 IntentFilter filter = new IntentFilter();
340 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
341 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
342 filter.addDataScheme("package");
343 mContext.registerReceiver(mBroadcastReceiver, filter);
344 }
345
Christopher Tatecde87f42009-06-12 12:55:53 -0700346 private void makeJournalLocked() {
347 try {
348 mJournal = File.createTempFile("journal", null, mJournalDir);
349 mJournalStream = new RandomAccessFile(mJournal, "rwd");
350 } catch (IOException e) {
351 Log.e(TAG, "Unable to write backup journals");
352 mJournal = null;
353 mJournalStream = null;
354 }
355 }
356
357 private void parseLeftoverJournals() {
358 if (mJournal != null) {
359 File[] allJournals = mJournalDir.listFiles();
360 for (File f : allJournals) {
361 if (f.compareTo(mJournal) != 0) {
362 // This isn't the current journal, so it must be a leftover. Read
363 // out the package names mentioned there and schedule them for
364 // backup.
365 try {
366 Log.i(TAG, "Found stale backup journal, scheduling:");
367 RandomAccessFile in = new RandomAccessFile(f, "r");
368 while (true) {
369 String packageName = in.readUTF();
370 Log.i(TAG, " + " + packageName);
371 dataChanged(packageName);
372 }
373 } catch (EOFException e) {
374 // no more data; we're done
375 } catch (Exception e) {
376 // can't read it or other error; just skip it
377 } finally {
378 // close/delete the file
379 f.delete();
380 }
381 }
382 }
383 }
384 }
385
Christopher Tate91717492009-06-26 21:07:13 -0700386 // Add a transport to our set of available backends
387 private void registerTransport(String name, IBackupTransport transport) {
388 synchronized (mTransports) {
Christopher Tate34ebd0e2009-07-06 15:44:54 -0700389 if (DEBUG) Log.v(TAG, "Registering transport " + name + " = " + transport);
Christopher Tate91717492009-06-26 21:07:13 -0700390 mTransports.put(name, transport);
391 }
392 }
393
Christopher Tate3799bc22009-05-06 16:13:56 -0700394 // ----- Track installation/removal of packages -----
395 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
396 public void onReceive(Context context, Intent intent) {
397 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
398
399 Uri uri = intent.getData();
400 if (uri == null) {
401 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700402 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700403 String pkgName = uri.getSchemeSpecificPart();
404 if (pkgName == null) {
405 return;
406 }
407
408 String action = intent.getAction();
409 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
410 synchronized (mBackupParticipants) {
411 Bundle extras = intent.getExtras();
412 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
413 // The package was just upgraded
414 updatePackageParticipantsLocked(pkgName);
415 } else {
416 // The package was just added
417 addPackageParticipantsLocked(pkgName);
418 }
419 }
420 }
421 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
422 Bundle extras = intent.getExtras();
423 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
424 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
425 } else {
426 synchronized (mBackupParticipants) {
427 removePackageParticipantsLocked(pkgName);
428 }
429 }
430 }
431 }
432 };
433
Dan Egnor87a02bc2009-06-17 02:30:10 -0700434 // ----- Track connection to GoogleBackupTransport service -----
435 ServiceConnection mGoogleConnection = new ServiceConnection() {
436 public void onServiceConnected(ComponentName name, IBinder service) {
437 if (DEBUG) Log.v(TAG, "Connected to Google transport");
438 mGoogleTransport = IBackupTransport.Stub.asInterface(service);
Christopher Tate91717492009-06-26 21:07:13 -0700439 registerTransport(name.flattenToShortString(), mGoogleTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700440 }
441
442 public void onServiceDisconnected(ComponentName name) {
443 if (DEBUG) Log.v(TAG, "Disconnected from Google transport");
444 mGoogleTransport = null;
Christopher Tate91717492009-06-26 21:07:13 -0700445 registerTransport(name.flattenToShortString(), null);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700446 }
447 };
448
Joe Onorato8ad02812009-05-13 01:41:44 -0400449 // ----- Run the actual backup process asynchronously -----
450
Christopher Tate181fafa2009-05-14 11:12:14 -0700451 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400452 public void handleMessage(Message msg) {
453
454 switch (msg.what) {
455 case MSG_RUN_BACKUP:
Dan Egnor87a02bc2009-06-17 02:30:10 -0700456 {
Christopher Tate91717492009-06-26 21:07:13 -0700457 IBackupTransport transport = getTransport(mCurrentTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700458 if (transport == null) {
459 Log.v(TAG, "Backup requested but no transport available");
Christopher Tateb6787f22009-07-02 17:40:45 -0700460 mWakelock.release();
Dan Egnor87a02bc2009-06-17 02:30:10 -0700461 break;
462 }
463
Joe Onorato8ad02812009-05-13 01:41:44 -0400464 // snapshot the pending-backup set and work on that
Christopher Tate6aa41f42009-06-19 14:14:22 -0700465 ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
Christopher Tatecde87f42009-06-12 12:55:53 -0700466 File oldJournal = mJournal;
Joe Onorato8ad02812009-05-13 01:41:44 -0400467 synchronized (mQueueLock) {
Christopher Tate49401dd2009-07-01 12:34:29 -0700468 // Do we have any work to do?
469 if (mPendingBackups.size() > 0) {
470 for (BackupRequest b: mPendingBackups.values()) {
471 queue.add(b);
Christopher Tatecde87f42009-06-12 12:55:53 -0700472 }
Christopher Tate49401dd2009-07-01 12:34:29 -0700473 Log.v(TAG, "clearing pending backups");
474 mPendingBackups.clear();
Christopher Tatecde87f42009-06-12 12:55:53 -0700475
Christopher Tate49401dd2009-07-01 12:34:29 -0700476 // Start a new backup-queue journal file too
477 if (mJournalStream != null) {
478 try {
479 mJournalStream.close();
480 } catch (IOException e) {
481 // don't need to do anything
482 }
483 makeJournalLocked();
484 }
485
486 // At this point, we have started a new journal file, and the old
487 // file identity is being passed to the backup processing thread.
488 // When it completes successfully, that old journal file will be
489 // deleted. If we crash prior to that, the old journal is parsed
490 // at next boot and the journaled requests fulfilled.
491 (new PerformBackupThread(transport, queue, oldJournal)).start();
492 } else {
493 Log.v(TAG, "Backup requested but nothing pending");
Christopher Tateb6787f22009-07-02 17:40:45 -0700494 mWakelock.release();
Christopher Tate49401dd2009-07-01 12:34:29 -0700495 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400496 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700497 break;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700498 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700499
500 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400501 break;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700502
503 case MSG_RUN_RESTORE:
504 {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700505 RestoreParams params = (RestoreParams)msg.obj;
Joe Onorato9a5e3e12009-07-01 21:04:03 -0400506 Log.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
Christopher Tateb6787f22009-07-02 17:40:45 -0700507 (new PerformRestoreThread(params.transport, params.observer,
508 params.token)).start();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700509 break;
510 }
Christopher Tateee0e78a2009-07-02 11:17:03 -0700511
512 case MSG_RUN_CLEAR:
513 {
514 ClearParams params = (ClearParams)msg.obj;
515 (new PerformClearThread(params.transport, params.packageInfo)).start();
516 break;
517 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400518 }
519 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400520 }
521
Christopher Tate181fafa2009-05-14 11:12:14 -0700522 // Add the backup agents in the given package to our set of known backup participants.
523 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700524 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700525 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700526 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700527 List<PackageInfo> targetApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700528 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700529 }
530
Christopher Tate181fafa2009-05-14 11:12:14 -0700531 private void addPackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700532 List<PackageInfo> targetPkgs) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700533 if (DEBUG) {
Dan Egnorefe52642009-06-24 00:16:33 -0700534 Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
535 for (PackageInfo p : targetPkgs) {
Christopher Tate111bd4a2009-06-24 17:29:38 -0700536 Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName
537 + " uid=" + p.applicationInfo.uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700538 }
539 }
540
Dan Egnorefe52642009-06-24 00:16:33 -0700541 for (PackageInfo pkg : targetPkgs) {
542 if (packageName == null || pkg.packageName.equals(packageName)) {
543 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700544 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700545 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700546 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700547 mBackupParticipants.put(uid, set);
548 }
Dan Egnorefe52642009-06-24 00:16:33 -0700549 set.add(pkg.applicationInfo);
Christopher Tate73e02522009-07-15 14:18:26 -0700550
551 // If we've never seen this app before, schedule a backup for it
552 if (!mEverStoredApps.contains(pkg.packageName)) {
553 if (DEBUG) Log.i(TAG, "New app " + pkg.packageName
554 + " never backed up; scheduling");
555 try {
556 dataChanged(pkg.packageName);
557 } catch (RemoteException e) {
558 // can't happen; it's a local method call
559 }
560 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700561 }
Christopher Tate487529a2009-04-29 14:03:25 -0700562 }
563 }
564
Christopher Tate6785dd82009-06-18 15:58:25 -0700565 // Remove the given package's entry from our known active set. If
566 // 'packageName' is null, *all* participating apps will be removed.
Christopher Tate3799bc22009-05-06 16:13:56 -0700567 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700568 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700569 List<PackageInfo> allApps = null;
Christopher Tate181fafa2009-05-14 11:12:14 -0700570 if (packageName != null) {
Dan Egnorefe52642009-06-24 00:16:33 -0700571 allApps = new ArrayList<PackageInfo>();
Christopher Tate181fafa2009-05-14 11:12:14 -0700572 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700573 int flags = PackageManager.GET_SIGNATURES;
574 allApps.add(mPackageManager.getPackageInfo(packageName, flags));
Christopher Tate181fafa2009-05-14 11:12:14 -0700575 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700576 // just skip it (???)
Christopher Tate181fafa2009-05-14 11:12:14 -0700577 }
578 } else {
579 // all apps with agents
Dan Egnorefe52642009-06-24 00:16:33 -0700580 allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700581 }
582 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700583 }
584
Joe Onorato8ad02812009-05-13 01:41:44 -0400585 private void removePackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700586 List<PackageInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700587 if (DEBUG) {
588 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
589 + ") removing " + agents.size() + " entries");
Dan Egnorefe52642009-06-24 00:16:33 -0700590 for (PackageInfo p : agents) {
591 Log.v(TAG, " - " + p);
Christopher Tate043dadc2009-06-02 16:11:00 -0700592 }
593 }
Dan Egnorefe52642009-06-24 00:16:33 -0700594 for (PackageInfo pkg : agents) {
595 if (packageName == null || pkg.packageName.equals(packageName)) {
596 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700597 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700598 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700599 // Find the existing entry with the same package name, and remove it.
600 // We can't just remove(app) because the instances are different.
601 for (ApplicationInfo entry: set) {
Dan Egnorefe52642009-06-24 00:16:33 -0700602 if (entry.packageName.equals(pkg.packageName)) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700603 set.remove(entry);
Christopher Tatee97e8072009-07-15 16:45:50 -0700604 removeEverBackedUp(pkg.packageName);
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700605 break;
606 }
607 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700608 if (set.size() == 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700609 mBackupParticipants.delete(uid);
610 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700611 }
612 }
613 }
614 }
615
Christopher Tate181fafa2009-05-14 11:12:14 -0700616 // Returns the set of all applications that define an android:backupAgent attribute
Christopher Tate73e02522009-07-15 14:18:26 -0700617 List<PackageInfo> allAgentPackages() {
Christopher Tate6785dd82009-06-18 15:58:25 -0700618 // !!! TODO: cache this and regenerate only when necessary
Dan Egnorefe52642009-06-24 00:16:33 -0700619 int flags = PackageManager.GET_SIGNATURES;
620 List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
621 int N = packages.size();
622 for (int a = N-1; a >= 0; a--) {
623 ApplicationInfo app = packages.get(a).applicationInfo;
624 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
625 || app.backupAgentName == null) {
626 packages.remove(a);
Christopher Tate181fafa2009-05-14 11:12:14 -0700627 }
628 }
Dan Egnorefe52642009-06-24 00:16:33 -0700629 return packages;
Christopher Tate181fafa2009-05-14 11:12:14 -0700630 }
Christopher Tateaa088442009-06-16 18:25:46 -0700631
Christopher Tate3799bc22009-05-06 16:13:56 -0700632 // Reset the given package's known backup participants. Unlike add/remove, the update
633 // action cannot be passed a null package name.
634 void updatePackageParticipantsLocked(String packageName) {
635 if (packageName == null) {
636 Log.e(TAG, "updatePackageParticipants called with null package name");
637 return;
638 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700639 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700640
641 // brute force but small code size
Dan Egnorefe52642009-06-24 00:16:33 -0700642 List<PackageInfo> allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700643 removePackageParticipantsLockedInner(packageName, allApps);
644 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700645 }
646
Christopher Tate73e02522009-07-15 14:18:26 -0700647 // Called from the backup thread: record that the given app has been successfully
648 // backed up at least once
649 void logBackupComplete(String packageName) {
Christopher Tatee97e8072009-07-15 16:45:50 -0700650 if (mEverStoredStream != null && !packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
Christopher Tate73e02522009-07-15 14:18:26 -0700651 synchronized (mEverStoredApps) {
652 if (mEverStoredApps.add(packageName)) {
653 try {
654 mEverStoredStream.writeUTF(packageName);
655 } catch (IOException e) {
656 Log.e(TAG, "Unable to log backup of " + packageName + ", ceasing log");
657 try {
658 mEverStoredStream.close();
659 } catch (IOException ioe) {
660 // we're dropping it; no need to handle an exception on close here
661 }
662 mEverStoredStream = null;
663 }
664 }
665 }
666 }
667 }
668
Christopher Tatee97e8072009-07-15 16:45:50 -0700669 // Remove our awareness of having ever backed up the given package
670 void removeEverBackedUp(String packageName) {
671 if (DEBUG) Log.v(TAG, "Removing backed-up knowledge of " + packageName
672 + ", new set:");
673
674 if (mEverStoredStream != null) {
675 synchronized (mEverStoredApps) {
676 // Rewrite the file and rename to overwrite. If we reboot in the middle,
677 // we'll recognize on initialization time that the package no longer
678 // exists and fix it up then.
679 File tempKnownFile = new File(mBaseStateDir, "processed.new");
680 try {
681 mEverStoredStream.close();
682 RandomAccessFile known = new RandomAccessFile(tempKnownFile, "rw");
683 mEverStoredApps.remove(packageName);
684 for (String s : mEverStoredApps) {
685 known.writeUTF(s);
686 if (DEBUG) Log.v(TAG, " " + s);
687 }
688 known.close();
689 tempKnownFile.renameTo(mEverStored);
690 mEverStoredStream = new RandomAccessFile(mEverStored, "rwd");
691 } catch (IOException e) {
692 // Bad: we couldn't create the new copy. For safety's sake we
693 // abandon the whole process and remove all what's-backed-up
694 // state entirely, meaning we'll force a backup pass for every
695 // participant on the next boot or [re]install.
696 Log.w(TAG, "Error rewriting backed-up set; halting log");
697 mEverStoredStream = null;
698 mEverStoredApps.clear();
699 tempKnownFile.delete();
700 mEverStored.delete();
701 }
702 }
703 }
704 }
705
Dan Egnor87a02bc2009-06-17 02:30:10 -0700706 // Return the given transport
Christopher Tate91717492009-06-26 21:07:13 -0700707 private IBackupTransport getTransport(String transportName) {
708 synchronized (mTransports) {
709 IBackupTransport transport = mTransports.get(transportName);
710 if (transport == null) {
711 Log.w(TAG, "Requested unavailable transport: " + transportName);
712 }
713 return transport;
Christopher Tate8c850b72009-06-07 19:33:20 -0700714 }
Christopher Tate8c850b72009-06-07 19:33:20 -0700715 }
716
Christopher Tatedf01dea2009-06-09 20:45:02 -0700717 // fire off a backup agent, blocking until it attaches or times out
718 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
719 IBackupAgent agent = null;
720 synchronized(mAgentConnectLock) {
721 mConnecting = true;
722 mConnectedAgent = null;
723 try {
724 if (mActivityManager.bindBackupAgent(app, mode)) {
725 Log.d(TAG, "awaiting agent for " + app);
726
727 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -0700728 // only wait 10 seconds for the clear data to happen
729 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
730 while (mConnecting && mConnectedAgent == null
731 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700732 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700733 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700734 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700735 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -0700736 return null;
737 }
738 }
739
740 // if we timed out with no connect, abort and move on
741 if (mConnecting == true) {
742 Log.w(TAG, "Timeout waiting for agent " + app);
743 return null;
744 }
745 agent = mConnectedAgent;
746 }
747 } catch (RemoteException e) {
748 // can't happen
749 }
750 }
751 return agent;
752 }
753
Christopher Tatec7b31e32009-06-10 15:49:30 -0700754 // clear an application's data, blocking until the operation completes or times out
755 void clearApplicationDataSynchronous(String packageName) {
Christopher Tatef7c886b2009-06-26 15:34:09 -0700756 // Don't wipe packages marked allowClearUserData=false
757 try {
758 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
759 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
760 if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping "
761 + packageName);
762 return;
763 }
764 } catch (NameNotFoundException e) {
765 Log.w(TAG, "Tried to clear data for " + packageName + " but not found");
766 return;
767 }
768
Christopher Tatec7b31e32009-06-10 15:49:30 -0700769 ClearDataObserver observer = new ClearDataObserver();
770
771 synchronized(mClearDataLock) {
772 mClearingData = true;
773 mPackageManager.clearApplicationUserData(packageName, observer);
774
775 // only wait 10 seconds for the clear data to happen
776 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
777 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
778 try {
779 mClearDataLock.wait(5000);
780 } catch (InterruptedException e) {
781 // won't happen, but still.
782 mClearingData = false;
783 }
784 }
785 }
786 }
787
788 class ClearDataObserver extends IPackageDataObserver.Stub {
789 public void onRemoveCompleted(String packageName, boolean succeeded)
790 throws android.os.RemoteException {
791 synchronized(mClearDataLock) {
792 mClearingData = false;
Christopher Tatef68eb502009-06-16 11:02:01 -0700793 mClearDataLock.notifyAll();
Christopher Tatec7b31e32009-06-10 15:49:30 -0700794 }
795 }
796 }
797
Christopher Tate043dadc2009-06-02 16:11:00 -0700798 // ----- Back up a set of applications via a worker thread -----
799
800 class PerformBackupThread extends Thread {
801 private static final String TAG = "PerformBackupThread";
Christopher Tateaa088442009-06-16 18:25:46 -0700802 IBackupTransport mTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700803 ArrayList<BackupRequest> mQueue;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700804 File mStateDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700805 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700806
Christopher Tateaa088442009-06-16 18:25:46 -0700807 public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue,
Christopher Tatecde87f42009-06-12 12:55:53 -0700808 File journal) {
Christopher Tateaa088442009-06-16 18:25:46 -0700809 mTransport = transport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700810 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700811 mJournal = journal;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700812
813 try {
814 mStateDir = new File(mBaseStateDir, transport.transportDirName());
815 } catch (RemoteException e) {
816 // can't happen; the transport is local
817 }
818 mStateDir.mkdirs();
Christopher Tate043dadc2009-06-02 16:11:00 -0700819 }
820
821 @Override
822 public void run() {
Christopher Tate043dadc2009-06-02 16:11:00 -0700823 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
824
Christopher Tate79588342009-06-30 16:11:49 -0700825 // Backups run at background priority
826 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
827
Christopher Tate5cb400b2009-06-25 16:03:14 -0700828 // The package manager doesn't have a proper <application> etc, but since
829 // it's running here in the system process we can just set up its agent
830 // directly and use a synthetic BackupRequest. We always run this pass
831 // because it's cheap and this way we guarantee that we don't get out of
832 // step even if we're selecting among various transports at run time.
833 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
834 mPackageManager, allAgentPackages());
835 BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false);
836 pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL;
837 processOneBackup(pmRequest,
838 IBackupAgent.Stub.asInterface(pmAgent.onBind()),
839 mTransport);
Christopher Tate6785dd82009-06-18 15:58:25 -0700840
841 // Now run all the backups in our queue
Christopher Tateaa088442009-06-16 18:25:46 -0700842 doQueuedBackups(mTransport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700843
844 // Finally, tear down the transport
845 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700846 if (!mTransport.finishBackup()) {
847 // STOPSHIP TODO: handle errors
848 Log.e(TAG, "Backup failure in finishBackup()");
849 }
850 } catch (RemoteException e) {
851 Log.e(TAG, "Error in finishBackup()", e);
Christopher Tate043dadc2009-06-02 16:11:00 -0700852 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700853
854 if (!mJournal.delete()) {
855 Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath());
856 }
Christopher Tateb6787f22009-07-02 17:40:45 -0700857
858 // Only once we're entirely finished do we release the wakelock
859 mWakelock.release();
Christopher Tate043dadc2009-06-02 16:11:00 -0700860 }
861
862 private void doQueuedBackups(IBackupTransport transport) {
863 for (BackupRequest request : mQueue) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700864 Log.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -0700865
866 IBackupAgent agent = null;
867 int mode = (request.fullBackup)
868 ? IApplicationThread.BACKUP_MODE_FULL
869 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
870 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700871 agent = bindToAgentSynchronous(request.appInfo, mode);
872 if (agent != null) {
873 processOneBackup(request, agent, transport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700874 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700875
876 // unbind even on timeout, just in case
877 mActivityManager.unbindBackupAgent(request.appInfo);
Christopher Tate043dadc2009-06-02 16:11:00 -0700878 } catch (SecurityException ex) {
879 // Try for the next one.
Christopher Tatec7b31e32009-06-10 15:49:30 -0700880 Log.d(TAG, "error in bind/backup", ex);
Christopher Tate043dadc2009-06-02 16:11:00 -0700881 } catch (RemoteException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700882 Log.v(TAG, "bind/backup threw");
883 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -0700884 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700885
Christopher Tate043dadc2009-06-02 16:11:00 -0700886 }
887 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700888
889 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
890 final String packageName = request.appInfo.packageName;
891 Log.d(TAG, "processOneBackup doBackup() on " + packageName);
892
893 try {
894 // Look up the package info & signatures. This is first so that if it
895 // throws an exception, there's no file setup yet that would need to
896 // be unraveled.
Christopher Tateabce4e82009-06-18 18:35:32 -0700897 PackageInfo packInfo;
898 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
899 // The metadata 'package' is synthetic
900 packInfo = new PackageInfo();
901 packInfo.packageName = packageName;
902 } else {
903 packInfo = mPackageManager.getPackageInfo(packageName,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700904 PackageManager.GET_SIGNATURES);
Christopher Tateabce4e82009-06-18 18:35:32 -0700905 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700906
907 // !!! TODO: get the state file dir from the transport
908 File savedStateName = new File(mStateDir, packageName);
909 File backupDataName = new File(mDataDir, packageName + ".data");
910 File newStateName = new File(mStateDir, packageName + ".new");
911
912 // In a full backup, we pass a null ParcelFileDescriptor as
913 // the saved-state "file"
914 ParcelFileDescriptor savedState = (request.fullBackup) ? null
915 : ParcelFileDescriptor.open(savedStateName,
916 ParcelFileDescriptor.MODE_READ_ONLY |
917 ParcelFileDescriptor.MODE_CREATE);
918
919 backupDataName.delete();
920 ParcelFileDescriptor backupData =
921 ParcelFileDescriptor.open(backupDataName,
922 ParcelFileDescriptor.MODE_READ_WRITE |
923 ParcelFileDescriptor.MODE_CREATE);
924
925 newStateName.delete();
926 ParcelFileDescriptor newState =
927 ParcelFileDescriptor.open(newStateName,
928 ParcelFileDescriptor.MODE_READ_WRITE |
929 ParcelFileDescriptor.MODE_CREATE);
930
931 // Run the target's backup pass
932 boolean success = false;
933 try {
934 agent.doBackup(savedState, backupData, newState);
Christopher Tate73e02522009-07-15 14:18:26 -0700935 logBackupComplete(packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700936 success = true;
937 } finally {
938 if (savedState != null) {
939 savedState.close();
940 }
941 backupData.close();
942 newState.close();
943 }
944
945 // Now propagate the newly-backed-up data to the transport
946 if (success) {
Christopher Tateb407f222009-07-08 13:48:56 -0700947 if (DEBUG) Log.v(TAG, "doBackup() success");
948 if (backupDataName.length() > 0) {
949 backupData =
950 ParcelFileDescriptor.open(backupDataName,
951 ParcelFileDescriptor.MODE_READ_ONLY);
952 if (!transport.performBackup(packInfo, backupData)) {
953 // STOPSHIP TODO: handle errors
954 Log.e(TAG, "Backup failure in performBackup()");
955 }
956 } else {
957 if (DEBUG) {
958 Log.i(TAG, "no backup data written; not calling transport");
959 }
Dan Egnorefe52642009-06-24 00:16:33 -0700960 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700961
Christopher Tateb407f222009-07-08 13:48:56 -0700962 // After successful transport, delete the now-stale data
963 // and juggle the files so that next time we supply the agent
964 // with the new state file it just created.
965 backupDataName.delete();
Christopher Tatec7b31e32009-06-10 15:49:30 -0700966 newStateName.renameTo(savedStateName);
967 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700968 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700969 Log.e(TAG, "Error backing up " + packageName, e);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700970 }
971 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700972 }
973
Christopher Tatedf01dea2009-06-09 20:45:02 -0700974
975 // ----- Restore handling -----
976
Christopher Tateabce4e82009-06-18 18:35:32 -0700977 private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) {
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700978 // Allow unsigned apps, but not signed on one device and unsigned on the other
979 // !!! TODO: is this the right policy?
Christopher Tate6aa41f42009-06-19 14:14:22 -0700980 if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs
981 + " device=" + deviceSigs);
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700982 if ((storedSigs == null || storedSigs.length == 0)
983 && (deviceSigs == null || deviceSigs.length == 0)) {
984 return true;
985 }
986 if (storedSigs == null || deviceSigs == null) {
987 return false;
988 }
989
Christopher Tateabce4e82009-06-18 18:35:32 -0700990 // !!! TODO: this demands that every stored signature match one
991 // that is present on device, and does not demand the converse.
992 // Is this this right policy?
993 int nStored = storedSigs.length;
994 int nDevice = deviceSigs.length;
995
996 for (int i=0; i < nStored; i++) {
997 boolean match = false;
998 for (int j=0; j < nDevice; j++) {
999 if (storedSigs[i].equals(deviceSigs[j])) {
1000 match = true;
1001 break;
1002 }
1003 }
1004 if (!match) {
1005 return false;
1006 }
1007 }
1008 return true;
1009 }
1010
Christopher Tatedf01dea2009-06-09 20:45:02 -07001011 class PerformRestoreThread extends Thread {
1012 private IBackupTransport mTransport;
Christopher Tate7d562ec2009-06-25 18:03:43 -07001013 private IRestoreObserver mObserver;
Dan Egnor156411d2009-06-26 13:20:02 -07001014 private long mToken;
Christopher Tatec7b31e32009-06-10 15:49:30 -07001015 private RestoreSet mImage;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001016 private File mStateDir;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001017
Christopher Tate5cbbf562009-06-22 16:44:51 -07001018 class RestoreRequest {
1019 public PackageInfo app;
1020 public int storedAppVersion;
1021
1022 RestoreRequest(PackageInfo _app, int _version) {
1023 app = _app;
1024 storedAppVersion = _version;
1025 }
1026 }
1027
Christopher Tate7d562ec2009-06-25 18:03:43 -07001028 PerformRestoreThread(IBackupTransport transport, IRestoreObserver observer,
Dan Egnor156411d2009-06-26 13:20:02 -07001029 long restoreSetToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001030 mTransport = transport;
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001031 Log.d(TAG, "PerformRestoreThread mObserver=" + mObserver);
Christopher Tate7d562ec2009-06-25 18:03:43 -07001032 mObserver = observer;
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001033 mToken = restoreSetToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001034
1035 try {
1036 mStateDir = new File(mBaseStateDir, transport.transportDirName());
1037 } catch (RemoteException e) {
1038 // can't happen; the transport is local
1039 }
1040 mStateDir.mkdirs();
Christopher Tatedf01dea2009-06-09 20:45:02 -07001041 }
1042
1043 @Override
1044 public void run() {
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001045 if (DEBUG) Log.v(TAG, "Beginning restore process mTransport=" + mTransport
1046 + " mObserver=" + mObserver + " mToken=" + mToken);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001047 /**
1048 * Restore sequence:
1049 *
Dan Egnorefe52642009-06-24 00:16:33 -07001050 * 1. get the restore set description for our identity
1051 * 2. for each app in the restore set:
Christopher Tatedf01dea2009-06-09 20:45:02 -07001052 * 3.a. if it's restorable on this device, add it to the restore queue
Dan Egnorefe52642009-06-24 00:16:33 -07001053 * 3. for each app in the restore queue:
1054 * 3.a. clear the app data
1055 * 3.b. get the restore data for the app from the transport
1056 * 3.c. launch the backup agent for the app
1057 * 3.d. agent.doRestore() with the data from the server
1058 * 3.e. unbind the agent [and kill the app?]
1059 * 4. shut down the transport
Christopher Tatedf01dea2009-06-09 20:45:02 -07001060 */
1061
Christopher Tate7d562ec2009-06-25 18:03:43 -07001062 int error = -1; // assume error
1063
Dan Egnorefe52642009-06-24 00:16:33 -07001064 // build the set of apps to restore
Christopher Tatedf01dea2009-06-09 20:45:02 -07001065 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001066 RestoreSet[] images = mTransport.getAvailableRestoreSets();
1067 if (images == null) {
1068 // STOPSHIP TODO: Handle the failure somehow?
1069 Log.e(TAG, "Error getting restore sets");
1070 return;
1071 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001072
Dan Egnorefe52642009-06-24 00:16:33 -07001073 if (images.length == 0) {
1074 Log.i(TAG, "No restore sets available");
1075 return;
1076 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001077
Dan Egnorefe52642009-06-24 00:16:33 -07001078 mImage = images[0];
Christopher Tateabce4e82009-06-18 18:35:32 -07001079
Dan Egnorefe52642009-06-24 00:16:33 -07001080 // Get the list of all packages which have backup enabled.
1081 // (Include the Package Manager metadata pseudo-package first.)
1082 ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>();
1083 PackageInfo omPackage = new PackageInfo();
1084 omPackage.packageName = PACKAGE_MANAGER_SENTINEL;
1085 restorePackages.add(omPackage);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001086
Dan Egnorefe52642009-06-24 00:16:33 -07001087 List<PackageInfo> agentPackages = allAgentPackages();
1088 restorePackages.addAll(agentPackages);
1089
Christopher Tate7d562ec2009-06-25 18:03:43 -07001090 // let the observer know that we're running
1091 if (mObserver != null) {
1092 try {
1093 // !!! TODO: get an actual count from the transport after
1094 // its startRestore() runs?
1095 mObserver.restoreStarting(restorePackages.size());
1096 } catch (RemoteException e) {
1097 Log.d(TAG, "Restore observer died at restoreStarting");
1098 mObserver = null;
1099 }
1100 }
1101
Dan Egnor156411d2009-06-26 13:20:02 -07001102 if (!mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0]))) {
Dan Egnorefe52642009-06-24 00:16:33 -07001103 // STOPSHIP TODO: Handle the failure somehow?
1104 Log.e(TAG, "Error starting restore operation");
1105 return;
1106 }
1107
1108 String packageName = mTransport.nextRestorePackage();
1109 if (packageName == null) {
1110 // STOPSHIP TODO: Handle the failure somehow?
1111 Log.e(TAG, "Error getting first restore package");
1112 return;
1113 } else if (packageName.equals("")) {
1114 Log.i(TAG, "No restore data available");
1115 return;
1116 } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
1117 Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL
1118 + "\", found only \"" + packageName + "\"");
1119 return;
1120 }
1121
1122 // Pull the Package Manager metadata from the restore set first
1123 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
1124 mPackageManager, agentPackages);
1125 processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()));
1126
Christopher Tate8c032472009-07-02 14:28:47 -07001127 // Verify that the backup set includes metadata. If not, we can't do
1128 // signature/version verification etc, so we simply do not proceed with
1129 // the restore operation.
Christopher Tate3d7cd132009-07-07 14:23:07 -07001130 if (!pmAgent.hasMetadata()) {
Christopher Tate8c032472009-07-02 14:28:47 -07001131 Log.i(TAG, "No restore metadata available, so not restoring settings");
1132 return;
1133 }
1134
Christopher Tate7d562ec2009-06-25 18:03:43 -07001135 int count = 0;
Dan Egnorefe52642009-06-24 00:16:33 -07001136 for (;;) {
1137 packageName = mTransport.nextRestorePackage();
1138 if (packageName == null) {
1139 // STOPSHIP TODO: Handle the failure somehow?
1140 Log.e(TAG, "Error getting next restore package");
1141 return;
1142 } else if (packageName.equals("")) {
1143 break;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001144 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001145
Christopher Tate7d562ec2009-06-25 18:03:43 -07001146 if (mObserver != null) {
1147 ++count;
1148 try {
1149 mObserver.onUpdate(count);
1150 } catch (RemoteException e) {
1151 Log.d(TAG, "Restore observer died in onUpdate");
1152 mObserver = null;
1153 }
1154 }
1155
Dan Egnorefe52642009-06-24 00:16:33 -07001156 Metadata metaInfo = pmAgent.getRestoredMetadata(packageName);
1157 if (metaInfo == null) {
1158 Log.e(TAG, "Missing metadata for " + packageName);
1159 continue;
1160 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001161
Dan Egnorefe52642009-06-24 00:16:33 -07001162 int flags = PackageManager.GET_SIGNATURES;
1163 PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags);
1164 if (metaInfo.versionCode > packageInfo.versionCode) {
1165 Log.w(TAG, "Package " + packageName
1166 + " restore version [" + metaInfo.versionCode
1167 + "] is too new for installed version ["
1168 + packageInfo.versionCode + "]");
1169 continue;
1170 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001171
Dan Egnorefe52642009-06-24 00:16:33 -07001172 if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) {
1173 Log.w(TAG, "Signature mismatch restoring " + packageName);
1174 continue;
1175 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001176
Dan Egnorefe52642009-06-24 00:16:33 -07001177 if (DEBUG) Log.v(TAG, "Package " + packageName
1178 + " restore version [" + metaInfo.versionCode
1179 + "] is compatible with installed version ["
1180 + packageInfo.versionCode + "]");
Christopher Tatec7b31e32009-06-10 15:49:30 -07001181
Dan Egnorefe52642009-06-24 00:16:33 -07001182 // Now perform the actual restore
1183 clearApplicationDataSynchronous(packageName);
1184 IBackupAgent agent = bindToAgentSynchronous(
1185 packageInfo.applicationInfo,
Christopher Tatec7b31e32009-06-10 15:49:30 -07001186 IApplicationThread.BACKUP_MODE_RESTORE);
Dan Egnorefe52642009-06-24 00:16:33 -07001187 if (agent == null) {
1188 Log.w(TAG, "Can't find backup agent for " + packageName);
1189 continue;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001190 }
1191
Dan Egnorefe52642009-06-24 00:16:33 -07001192 try {
1193 processOneRestore(packageInfo, metaInfo.versionCode, agent);
1194 } finally {
1195 // unbind even on timeout or failure, just in case
1196 mActivityManager.unbindBackupAgent(packageInfo.applicationInfo);
1197 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001198 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001199
1200 // if we get this far, report success to the observer
1201 error = 0;
Dan Egnorefe52642009-06-24 00:16:33 -07001202 } catch (NameNotFoundException e) {
1203 // STOPSHIP TODO: Handle the failure somehow?
1204 Log.e(TAG, "Invalid paackage restoring data", e);
1205 } catch (RemoteException e) {
1206 // STOPSHIP TODO: Handle the failure somehow?
1207 Log.e(TAG, "Error restoring data", e);
1208 } finally {
1209 try {
1210 mTransport.finishRestore();
1211 } catch (RemoteException e) {
1212 Log.e(TAG, "Error finishing restore", e);
1213 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001214
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001215 Log.d(TAG, "finishing restore mObserver=" + mObserver);
1216
Christopher Tate7d562ec2009-06-25 18:03:43 -07001217 if (mObserver != null) {
1218 try {
1219 mObserver.restoreFinished(error);
1220 } catch (RemoteException e) {
1221 Log.d(TAG, "Restore observer died at restoreFinished");
1222 }
1223 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001224
1225 // done; we can finally release the wakelock
1226 mWakelock.release();
Christopher Tatedf01dea2009-06-09 20:45:02 -07001227 }
1228 }
1229
Dan Egnorefe52642009-06-24 00:16:33 -07001230 // Do the guts of a restore of one application, using mTransport.getRestoreData().
1231 void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001232 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -07001233 final String packageName = app.packageName;
1234
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001235 Log.d(TAG, "processOneRestore packageName=" + packageName);
1236
Christopher Tatec7b31e32009-06-10 15:49:30 -07001237 // !!! TODO: get the dirs from the transport
1238 File backupDataName = new File(mDataDir, packageName + ".restore");
1239 backupDataName.delete();
1240 try {
1241 ParcelFileDescriptor backupData =
1242 ParcelFileDescriptor.open(backupDataName,
1243 ParcelFileDescriptor.MODE_READ_WRITE |
1244 ParcelFileDescriptor.MODE_CREATE);
1245
1246 // Run the transport's restore pass
1247 // Run the target's backup pass
Christopher Tatec7b31e32009-06-10 15:49:30 -07001248 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001249 if (!mTransport.getRestoreData(backupData)) {
1250 // STOPSHIP TODO: Handle this error somehow?
1251 Log.e(TAG, "Error getting restore data for " + packageName);
1252 return;
1253 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001254 } finally {
1255 backupData.close();
1256 }
1257
1258 // Okay, we have the data. Now have the agent do the restore.
1259 File newStateName = new File(mStateDir, packageName + ".new");
1260 ParcelFileDescriptor newState =
1261 ParcelFileDescriptor.open(newStateName,
1262 ParcelFileDescriptor.MODE_READ_WRITE |
1263 ParcelFileDescriptor.MODE_CREATE);
1264
1265 backupData = ParcelFileDescriptor.open(backupDataName,
1266 ParcelFileDescriptor.MODE_READ_ONLY);
1267
Christopher Tatec7b31e32009-06-10 15:49:30 -07001268 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001269 agent.doRestore(backupData, appVersionCode, newState);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001270 } finally {
1271 newState.close();
1272 backupData.close();
1273 }
1274
1275 // if everything went okay, remember the recorded state now
Dan Egnorefe52642009-06-24 00:16:33 -07001276 File savedStateName = new File(mStateDir, packageName);
1277 newStateName.renameTo(savedStateName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001278 } catch (Exception e) {
Christopher Tate96733042009-07-20 14:49:13 -07001279 // If the agent fails restore, it might have put the app's data
1280 // into an incoherent state. For consistency we wipe its data
1281 // again in this case before propagating the exception
Dan Egnorefe52642009-06-24 00:16:33 -07001282 Log.e(TAG, "Error restoring data for " + packageName, e);
Christopher Tate96733042009-07-20 14:49:13 -07001283 clearApplicationDataSynchronous(packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001284 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001285 }
1286 }
1287
Christopher Tateee0e78a2009-07-02 11:17:03 -07001288 class PerformClearThread extends Thread {
1289 IBackupTransport mTransport;
1290 PackageInfo mPackage;
1291
1292 PerformClearThread(IBackupTransport transport, PackageInfo packageInfo) {
1293 mTransport = transport;
1294 mPackage = packageInfo;
1295 }
1296
1297 @Override
1298 public void run() {
1299 try {
1300 // Clear the on-device backup state to ensure a full backup next time
1301 File stateDir = new File(mBaseStateDir, mTransport.transportDirName());
1302 File stateFile = new File(stateDir, mPackage.packageName);
1303 stateFile.delete();
1304
1305 // Tell the transport to remove all the persistent storage for the app
1306 mTransport.clearBackupData(mPackage);
1307 } catch (RemoteException e) {
1308 // can't happen; the transport is local
1309 } finally {
1310 try {
1311 mTransport.finishBackup();
1312 } catch (RemoteException e) {
1313 // can't happen; the transport is local
1314 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001315
1316 // Last but not least, release the cpu
1317 mWakelock.release();
Christopher Tateee0e78a2009-07-02 11:17:03 -07001318 }
1319 }
1320 }
1321
Christopher Tatedf01dea2009-06-09 20:45:02 -07001322
Christopher Tate487529a2009-04-29 14:03:25 -07001323 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -07001324
Christopher Tatea8bf8152009-04-30 11:36:21 -07001325 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -07001326 // Record that we need a backup pass for the caller. Since multiple callers
1327 // may share a uid, we need to note all candidates within that uid and schedule
1328 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001329
Christopher Tate63d27002009-06-16 17:16:42 -07001330 // If the caller does not hold the BACKUP permission, it can only request a
1331 // backup of its own data.
1332 HashSet<ApplicationInfo> targets;
Dianne Hackborncf098292009-07-01 19:55:20 -07001333 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tate63d27002009-06-16 17:16:42 -07001334 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1335 targets = mBackupParticipants.get(Binder.getCallingUid());
1336 } else {
1337 // a caller with full permission can ask to back up any participating app
1338 // !!! TODO: allow backup of ANY app?
Christopher Tate63d27002009-06-16 17:16:42 -07001339 targets = new HashSet<ApplicationInfo>();
1340 int N = mBackupParticipants.size();
1341 for (int i = 0; i < N; i++) {
1342 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1343 if (s != null) {
1344 targets.addAll(s);
1345 }
1346 }
1347 }
Christopher Tate487529a2009-04-29 14:03:25 -07001348 if (targets != null) {
1349 synchronized (mQueueLock) {
1350 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -07001351 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -07001352 // validate the caller-supplied package name against the known set of
1353 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -07001354 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -04001355 // Add the caller to the set of pending backups. If there is
1356 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -07001357 BackupRequest req = new BackupRequest(app, false);
Christopher Tatea7de3842009-07-07 14:50:26 -07001358 if (mPendingBackups.put(app, req) == null) {
1359 // Journal this request in case of crash. The put()
1360 // operation returned null when this package was not already
1361 // in the set; we want to avoid touching the disk redundantly.
1362 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001363
Christopher Tate22b60d82009-07-07 16:36:02 -07001364 if (DEBUG) {
1365 int numKeys = mPendingBackups.size();
1366 Log.d(TAG, "Now awaiting backup for " + numKeys + " participants:");
1367 for (BackupRequest b : mPendingBackups.values()) {
1368 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
1369 }
1370 }
1371 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001372 }
1373 }
Christopher Tate487529a2009-04-29 14:03:25 -07001374 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001375 } else {
Christopher Tate73e02522009-07-15 14:18:26 -07001376 Log.w(TAG, "dataChanged but no participant pkg='" + packageName + "'"
1377 + " uid=" + Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -07001378 }
1379 }
Christopher Tate46758122009-05-06 11:22:00 -07001380
Christopher Tatecde87f42009-06-12 12:55:53 -07001381 private void writeToJournalLocked(String str) {
1382 if (mJournalStream != null) {
1383 try {
1384 mJournalStream.writeUTF(str);
1385 } catch (IOException e) {
1386 Log.e(TAG, "Error writing to backup journal");
1387 mJournalStream = null;
1388 mJournal = null;
1389 }
1390 }
1391 }
1392
Christopher Tateee0e78a2009-07-02 11:17:03 -07001393 // Clear the given package's backup data from the current transport
1394 public void clearBackupData(String packageName) {
1395 if (DEBUG) Log.v(TAG, "clearBackupData() of " + packageName);
1396 PackageInfo info;
1397 try {
1398 info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
1399 } catch (NameNotFoundException e) {
1400 Log.d(TAG, "No such package '" + packageName + "' - not clearing backup data");
1401 return;
1402 }
1403
1404 // If the caller does not hold the BACKUP permission, it can only request a
1405 // wipe of its own backed-up data.
1406 HashSet<ApplicationInfo> apps;
Christopher Tate4e3e50c2009-07-02 12:14:05 -07001407 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tateee0e78a2009-07-02 11:17:03 -07001408 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1409 apps = mBackupParticipants.get(Binder.getCallingUid());
1410 } else {
1411 // a caller with full permission can ask to back up any participating app
1412 // !!! TODO: allow data-clear of ANY app?
1413 if (DEBUG) Log.v(TAG, "Privileged caller, allowing clear of other apps");
1414 apps = new HashSet<ApplicationInfo>();
1415 int N = mBackupParticipants.size();
1416 for (int i = 0; i < N; i++) {
1417 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1418 if (s != null) {
1419 apps.addAll(s);
1420 }
1421 }
1422 }
1423
1424 // now find the given package in the set of candidate apps
1425 for (ApplicationInfo app : apps) {
1426 if (app.packageName.equals(packageName)) {
1427 if (DEBUG) Log.v(TAG, "Found the app - running clear process");
1428 // found it; fire off the clear request
1429 synchronized (mQueueLock) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001430 mWakelock.acquire();
Christopher Tateee0e78a2009-07-02 11:17:03 -07001431 Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR,
1432 new ClearParams(getTransport(mCurrentTransport), info));
1433 mBackupHandler.sendMessage(msg);
1434 }
1435 break;
1436 }
1437 }
1438 }
1439
Christopher Tateace7f092009-06-15 18:07:25 -07001440 // Run a backup pass immediately for any applications that have declared
1441 // that they have pending updates.
1442 public void backupNow() throws RemoteException {
Dianne Hackborncf098292009-07-01 19:55:20 -07001443 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "backupNow");
Christopher Tate043dadc2009-06-02 16:11:00 -07001444
Christopher Tateace7f092009-06-15 18:07:25 -07001445 if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass");
Christopher Tate46758122009-05-06 11:22:00 -07001446 synchronized (mQueueLock) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001447 try {
1448 if (DEBUG) Log.v(TAG, "sending immediate backup broadcast");
1449 mRunBackupIntent.send();
1450 } catch (PendingIntent.CanceledException e) {
1451 // should never happen
1452 Log.e(TAG, "run-backup intent cancelled!");
1453 }
Christopher Tate46758122009-05-06 11:22:00 -07001454 }
1455 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001456
Christopher Tate8031a3d2009-07-06 16:36:05 -07001457 // Enable/disable the backup service
Christopher Tate6ef58a12009-06-29 14:56:28 -07001458 public void setBackupEnabled(boolean enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001459 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1460 "setBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07001461
1462 boolean wasEnabled = mEnabled;
1463 synchronized (this) {
Dianne Hackborncf098292009-07-01 19:55:20 -07001464 Settings.Secure.putInt(mContext.getContentResolver(),
1465 Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0);
Christopher Tate6ef58a12009-06-29 14:56:28 -07001466 mEnabled = enable;
1467 }
1468
Christopher Tate49401dd2009-07-01 12:34:29 -07001469 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07001470 if (enable && !wasEnabled && mProvisioned) {
Christopher Tate49401dd2009-07-01 12:34:29 -07001471 // if we've just been enabled, start scheduling backup passes
Christopher Tate8031a3d2009-07-06 16:36:05 -07001472 startBackupAlarmsLocked(BACKUP_INTERVAL);
Christopher Tate49401dd2009-07-01 12:34:29 -07001473 } else if (!enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001474 // No longer enabled, so stop running backups
1475 mAlarmManager.cancel(mRunBackupIntent);
Christopher Tate6ef58a12009-06-29 14:56:28 -07001476 }
1477 }
Christopher Tate49401dd2009-07-01 12:34:29 -07001478 }
Christopher Tate6ef58a12009-06-29 14:56:28 -07001479
Christopher Tate8031a3d2009-07-06 16:36:05 -07001480 // Mark the backup service as having been provisioned
1481 public void setBackupProvisioned(boolean available) {
1482 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1483 "setBackupProvisioned");
1484
1485 boolean wasProvisioned = mProvisioned;
1486 synchronized (this) {
1487 Settings.Secure.putInt(mContext.getContentResolver(),
1488 Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0);
1489 mProvisioned = available;
1490 }
1491
1492 synchronized (mQueueLock) {
1493 if (available && !wasProvisioned && mEnabled) {
1494 // we're now good to go, so start the backup alarms
1495 startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL);
1496 } else if (!available) {
1497 // No longer enabled, so stop running backups
1498 Log.w(TAG, "Backup service no longer provisioned");
1499 mAlarmManager.cancel(mRunBackupIntent);
1500 }
1501 }
1502 }
1503
1504 private void startBackupAlarmsLocked(long delayBeforeFirstBackup) {
1505 long when = System.currentTimeMillis() + delayBeforeFirstBackup;
1506 mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, when,
1507 BACKUP_INTERVAL, mRunBackupIntent);
1508 }
1509
Christopher Tate6ef58a12009-06-29 14:56:28 -07001510 // Report whether the backup mechanism is currently enabled
1511 public boolean isBackupEnabled() {
Dianne Hackborncf098292009-07-01 19:55:20 -07001512 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "isBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07001513 return mEnabled; // no need to synchronize just to read it
1514 }
1515
Christopher Tate91717492009-06-26 21:07:13 -07001516 // Report the name of the currently active transport
1517 public String getCurrentTransport() {
Christopher Tate4e3e50c2009-07-02 12:14:05 -07001518 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
1519 "getCurrentTransport");
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001520 Log.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport);
Christopher Tate91717492009-06-26 21:07:13 -07001521 return mCurrentTransport;
Christopher Tateace7f092009-06-15 18:07:25 -07001522 }
1523
Christopher Tate91717492009-06-26 21:07:13 -07001524 // Report all known, available backup transports
1525 public String[] listAllTransports() {
Christopher Tate34ebd0e2009-07-06 15:44:54 -07001526 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports");
Christopher Tate043dadc2009-06-02 16:11:00 -07001527
Christopher Tate91717492009-06-26 21:07:13 -07001528 String[] list = null;
1529 ArrayList<String> known = new ArrayList<String>();
1530 for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) {
1531 if (entry.getValue() != null) {
1532 known.add(entry.getKey());
1533 }
1534 }
1535
1536 if (known.size() > 0) {
1537 list = new String[known.size()];
1538 known.toArray(list);
1539 }
1540 return list;
1541 }
1542
1543 // Select which transport to use for the next backup operation. If the given
1544 // name is not one of the available transports, no action is taken and the method
1545 // returns null.
1546 public String selectBackupTransport(String transport) {
Dianne Hackborncf098292009-07-01 19:55:20 -07001547 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "selectBackupTransport");
Christopher Tate91717492009-06-26 21:07:13 -07001548
1549 synchronized (mTransports) {
1550 String prevTransport = null;
1551 if (mTransports.get(transport) != null) {
1552 prevTransport = mCurrentTransport;
1553 mCurrentTransport = transport;
Dianne Hackborncf098292009-07-01 19:55:20 -07001554 Settings.Secure.putString(mContext.getContentResolver(),
1555 Settings.Secure.BACKUP_TRANSPORT, transport);
Christopher Tate91717492009-06-26 21:07:13 -07001556 Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport
1557 + " returning " + prevTransport);
1558 } else {
1559 Log.w(TAG, "Attempt to select unavailable transport " + transport);
1560 }
1561 return prevTransport;
1562 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001563 }
1564
1565 // Callback: a requested backup agent has been instantiated. This should only
1566 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001567 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -07001568 synchronized(mAgentConnectLock) {
1569 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1570 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
1571 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
1572 mConnectedAgent = agent;
1573 mConnecting = false;
1574 } else {
1575 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1576 + " claiming agent connected");
1577 }
1578 mAgentConnectLock.notifyAll();
1579 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001580 }
1581
1582 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
1583 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -07001584 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001585 public void agentDisconnected(String packageName) {
1586 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -07001587 synchronized(mAgentConnectLock) {
1588 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1589 mConnectedAgent = null;
1590 mConnecting = false;
1591 } else {
1592 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1593 + " claiming agent disconnected");
1594 }
1595 mAgentConnectLock.notifyAll();
1596 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001597 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001598
Christopher Tate8c850b72009-06-07 19:33:20 -07001599 // Hand off a restore session
Christopher Tate91717492009-06-26 21:07:13 -07001600 public IRestoreSession beginRestoreSession(String transport) {
Dianne Hackborncf098292009-07-01 19:55:20 -07001601 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "beginRestoreSession");
Christopher Tatef68eb502009-06-16 11:02:01 -07001602
1603 synchronized(this) {
1604 if (mActiveRestoreSession != null) {
1605 Log.d(TAG, "Restore session requested but one already active");
1606 return null;
1607 }
Christopher Tate91717492009-06-26 21:07:13 -07001608 mActiveRestoreSession = new RestoreSession(transport);
Christopher Tatef68eb502009-06-16 11:02:01 -07001609 }
1610 return mActiveRestoreSession;
Christopher Tate8c850b72009-06-07 19:33:20 -07001611 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001612
Christopher Tate9b3905c2009-06-08 15:24:01 -07001613 // ----- Restore session -----
1614
1615 class RestoreSession extends IRestoreSession.Stub {
Christopher Tatef68eb502009-06-16 11:02:01 -07001616 private static final String TAG = "RestoreSession";
1617
Christopher Tate9b3905c2009-06-08 15:24:01 -07001618 private IBackupTransport mRestoreTransport = null;
1619 RestoreSet[] mRestoreSets = null;
1620
Christopher Tate91717492009-06-26 21:07:13 -07001621 RestoreSession(String transport) {
1622 mRestoreTransport = getTransport(transport);
Christopher Tate9b3905c2009-06-08 15:24:01 -07001623 }
1624
1625 // --- Binder interface ---
1626 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Dianne Hackborncf098292009-07-01 19:55:20 -07001627 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001628 "getAvailableRestoreSets");
1629
Christopher Tatef68eb502009-06-16 11:02:01 -07001630 try {
Christopher Tate9b3905c2009-06-08 15:24:01 -07001631 synchronized(this) {
Christopher Tate43383042009-07-13 15:17:13 -07001632 if (mRestoreTransport == null) {
1633 Log.w(TAG, "Null transport getting restore sets");
1634 } else if (mRestoreSets == null) { // valid transport; do the one-time fetch
Christopher Tate9b3905c2009-06-08 15:24:01 -07001635 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
1636 }
1637 return mRestoreSets;
1638 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001639 } catch (RuntimeException e) {
1640 Log.d(TAG, "getAvailableRestoreSets exception");
1641 e.printStackTrace();
1642 throw e;
1643 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001644 }
1645
Dan Egnor156411d2009-06-26 13:20:02 -07001646 public int performRestore(long token, IRestoreObserver observer)
Christopher Tate7d562ec2009-06-25 18:03:43 -07001647 throws android.os.RemoteException {
Dianne Hackborncf098292009-07-01 19:55:20 -07001648 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "performRestore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001649
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001650 Log.d(TAG, "performRestore token=" + token + " observer=" + observer);
1651
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001652 if (mRestoreSets != null) {
1653 for (int i = 0; i < mRestoreSets.length; i++) {
1654 if (token == mRestoreSets[i].token) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001655 mWakelock.acquire();
Christopher Tate7d562ec2009-06-25 18:03:43 -07001656 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
Dan Egnor156411d2009-06-26 13:20:02 -07001657 msg.obj = new RestoreParams(mRestoreTransport, observer, token);
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001658 mBackupHandler.sendMessage(msg);
1659 return 0;
1660 }
1661 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001662 } else {
1663 if (DEBUG) Log.v(TAG, "No current restore set, not doing restore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001664 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001665 return -1;
1666 }
1667
1668 public void endRestoreSession() throws android.os.RemoteException {
Dianne Hackborncf098292009-07-01 19:55:20 -07001669 mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001670 "endRestoreSession");
1671
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001672 Log.d(TAG, "endRestoreSession");
1673
Dan Egnorefe52642009-06-24 00:16:33 -07001674 mRestoreTransport.finishRestore();
Christopher Tate9b3905c2009-06-08 15:24:01 -07001675 mRestoreTransport = null;
Christopher Tatef68eb502009-06-16 11:02:01 -07001676 synchronized(BackupManagerService.this) {
1677 if (BackupManagerService.this.mActiveRestoreSession == this) {
1678 BackupManagerService.this.mActiveRestoreSession = null;
1679 } else {
1680 Log.e(TAG, "ending non-current restore session");
1681 }
1682 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001683 }
1684 }
1685
Christopher Tate043dadc2009-06-02 16:11:00 -07001686
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001687 @Override
1688 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1689 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07001690 pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled")
1691 + " / " + (!mProvisioned ? "not " : "") + "provisioned");
Christopher Tate91717492009-06-26 21:07:13 -07001692 pw.println("Available transports:");
1693 for (String t : listAllTransports()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001694 String pad = (t.equals(mCurrentTransport)) ? " * " : " ";
1695 pw.println(pad + t);
Christopher Tate91717492009-06-26 21:07:13 -07001696 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001697 int N = mBackupParticipants.size();
Christopher Tatece0bf062009-07-01 11:43:53 -07001698 pw.println("Participants: " + N);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001699 for (int i=0; i<N; i++) {
1700 int uid = mBackupParticipants.keyAt(i);
1701 pw.print(" uid: ");
1702 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07001703 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
1704 for (ApplicationInfo app: participants) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001705 pw.println(" " + app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001706 }
1707 }
Christopher Tate73e02522009-07-15 14:18:26 -07001708 pw.println("Ever backed up: " + mEverStoredApps.size());
1709 for (String pkg : mEverStoredApps) {
1710 pw.println(" " + pkg);
1711 }
Christopher Tate6aa41f42009-06-19 14:14:22 -07001712 pw.println("Pending: " + mPendingBackups.size());
1713 for (BackupRequest req : mPendingBackups.values()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001714 pw.println(" " + req);
Christopher Tate181fafa2009-05-14 11:12:14 -07001715 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001716 }
1717 }
Christopher Tate487529a2009-04-29 14:03:25 -07001718}