blob: 2b9ac4da28d2684b661e1f5cc49c93d866639c59 [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;
20import android.app.IActivityManager;
21import android.app.IApplicationThread;
22import android.app.IBackupAgent;
Christopher Tate3799bc22009-05-06 16:13:56 -070023import android.content.BroadcastReceiver;
Dan Egnor87a02bc2009-06-17 02:30:10 -070024import android.content.ComponentName;
Christopher Tate487529a2009-04-29 14:03:25 -070025import android.content.Context;
26import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070027import android.content.IntentFilter;
Dan Egnor87a02bc2009-06-17 02:30:10 -070028import android.content.ServiceConnection;
Christopher Tate181fafa2009-05-14 11:12:14 -070029import android.content.pm.ApplicationInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -070030import android.content.pm.IPackageDataObserver;
Christopher Tate7b881282009-06-07 13:52:37 -070031import android.content.pm.PackageInfo;
Christopher Tate043dadc2009-06-02 16:11:00 -070032import android.content.pm.PackageManager.NameNotFoundException;
Dan Egnor87a02bc2009-06-17 02:30:10 -070033import android.content.pm.PackageManager;
Christopher Tateabce4e82009-06-18 18:35:32 -070034import android.content.pm.Signature;
Christopher Tate3799bc22009-05-06 16:13:56 -070035import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070036import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070037import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070038import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070039import android.os.Handler;
40import android.os.IBinder;
41import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070042import android.os.ParcelFileDescriptor;
Christopher Tate043dadc2009-06-02 16:11:00 -070043import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070044import android.os.RemoteException;
Christopher Tate91717492009-06-26 21:07:13 -070045import android.os.SystemProperties;
Christopher Tate487529a2009-04-29 14:03:25 -070046import android.util.Log;
47import android.util.SparseArray;
48
49import android.backup.IBackupManager;
Christopher Tate7d562ec2009-06-25 18:03:43 -070050import android.backup.IRestoreObserver;
Christopher Tate8c850b72009-06-07 19:33:20 -070051import android.backup.IRestoreSession;
Christopher Tate9b3905c2009-06-08 15:24:01 -070052import android.backup.RestoreSet;
Christopher Tate043dadc2009-06-02 16:11:00 -070053
Christopher Tate9bbc21a2009-06-10 20:23:25 -070054import com.android.internal.backup.LocalTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -070055import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070056
Christopher Tate6785dd82009-06-18 15:58:25 -070057import com.android.server.PackageManagerBackupAgent;
Christopher Tate6aa41f42009-06-19 14:14:22 -070058import com.android.server.PackageManagerBackupAgent.Metadata;
Christopher Tate6785dd82009-06-18 15:58:25 -070059
Christopher Tatecde87f42009-06-12 12:55:53 -070060import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070061import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070062import java.io.FileDescriptor;
Christopher Tatec7b31e32009-06-10 15:49:30 -070063import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070064import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070065import java.io.RandomAccessFile;
Christopher Tate487529a2009-04-29 14:03:25 -070066import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040067import java.util.ArrayList;
68import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070069import java.util.HashSet;
70import java.util.List;
Christopher Tate91717492009-06-26 21:07:13 -070071import java.util.Map;
Christopher Tate487529a2009-04-29 14:03:25 -070072
73class BackupManagerService extends IBackupManager.Stub {
74 private static final String TAG = "BackupManagerService";
75 private static final boolean DEBUG = true;
Christopher Tateaa088442009-06-16 18:25:46 -070076
Christopher Tate6785dd82009-06-18 15:58:25 -070077 // Default time to wait after data changes before we back up the data
Christopher Tate111bd4a2009-06-24 17:29:38 -070078 private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
Christopher Tate487529a2009-04-29 14:03:25 -070079
80 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070081 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070082 private static final int MSG_RUN_RESTORE = 3;
Christopher Tatec7b31e32009-06-10 15:49:30 -070083
84 // Timeout interval for deciding that a bind or clear-data has taken too long
85 static final long TIMEOUT_INTERVAL = 10 * 1000;
86
Christopher Tate487529a2009-04-29 14:03:25 -070087 private Context mContext;
88 private PackageManager mPackageManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070089 private final IActivityManager mActivityManager;
Christopher Tate487529a2009-04-29 14:03:25 -070090 private final BackupHandler mBackupHandler = new BackupHandler();
91 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate181fafa2009-05-14 11:12:14 -070092 private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
93 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -070094 // set of backup services that have pending changes
Christopher Tate46758122009-05-06 11:22:00 -070095 private class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -070096 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -070097 public boolean fullBackup;
Christopher Tateaa088442009-06-16 18:25:46 -070098
Christopher Tate181fafa2009-05-14 11:12:14 -070099 BackupRequest(ApplicationInfo app, boolean isFull) {
100 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -0700101 fullBackup = isFull;
102 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700103
104 public String toString() {
105 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
106 }
Christopher Tate46758122009-05-06 11:22:00 -0700107 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400108 // Backups that we haven't started yet.
Christopher Tate181fafa2009-05-14 11:12:14 -0700109 private HashMap<ApplicationInfo,BackupRequest> mPendingBackups
110 = new HashMap<ApplicationInfo,BackupRequest>();
Christopher Tate5cb400b2009-06-25 16:03:14 -0700111
112 // Pseudoname that we use for the Package Manager metadata "package"
Christopher Tate6785dd82009-06-18 15:58:25 -0700113 private static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
Christopher Tate6aa41f42009-06-19 14:14:22 -0700114
115 // locking around the pending-backup management
Christopher Tate487529a2009-04-29 14:03:25 -0700116 private final Object mQueueLock = new Object();
117
Christopher Tate043dadc2009-06-02 16:11:00 -0700118 // The thread performing the sequence of queued backups binds to each app's agent
119 // in succession. Bind notifications are asynchronously delivered through the
120 // Activity Manager; use this lock object to signal when a requested binding has
121 // completed.
122 private final Object mAgentConnectLock = new Object();
123 private IBackupAgent mConnectedAgent;
124 private volatile boolean mConnecting;
125
Christopher Tatec7b31e32009-06-10 15:49:30 -0700126 // A similar synchronicity mechanism around clearing apps' data for restore
127 private final Object mClearDataLock = new Object();
128 private volatile boolean mClearingData;
129
Christopher Tate91717492009-06-26 21:07:13 -0700130 // Transport bookkeeping
131 static private final String BACKUP_TRANSPORT_PROPERTY = "persist.service.bkup.trans";
132 private final HashMap<String,IBackupTransport> mTransports
133 = new HashMap<String,IBackupTransport>();
134 private String mCurrentTransport;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700135 private IBackupTransport mLocalTransport, mGoogleTransport;
Christopher Tatef68eb502009-06-16 11:02:01 -0700136 private RestoreSession mActiveRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -0700137
Christopher Tate7d562ec2009-06-25 18:03:43 -0700138 private class RestoreParams {
139 public IBackupTransport transport;
140 public IRestoreObserver observer;
Dan Egnor156411d2009-06-26 13:20:02 -0700141 public long token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700142
Dan Egnor156411d2009-06-26 13:20:02 -0700143 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700144 transport = _transport;
145 observer = _obs;
Dan Egnor156411d2009-06-26 13:20:02 -0700146 token = _token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700147 }
148 }
149
Christopher Tate5cb400b2009-06-25 16:03:14 -0700150 // Where we keep our journal files and other bookkeeping
151 private File mBaseStateDir;
Christopher Tatef4172472009-05-05 15:50:03 -0700152 private File mDataDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700153 private File mJournalDir;
154 private File mJournal;
155 private RandomAccessFile mJournalStream;
Christopher Tateaa088442009-06-16 18:25:46 -0700156
Christopher Tate487529a2009-04-29 14:03:25 -0700157 public BackupManagerService(Context context) {
158 mContext = context;
159 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700160 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700161
Christopher Tate22b87872009-05-04 16:41:53 -0700162 // Set up our bookkeeping
Christopher Tate5cb400b2009-06-25 16:03:14 -0700163 mBaseStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tatef4172472009-05-05 15:50:03 -0700164 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700165
Christopher Tatecde87f42009-06-12 12:55:53 -0700166 // Set up the backup-request journaling
Christopher Tate5cb400b2009-06-25 16:03:14 -0700167 mJournalDir = new File(mBaseStateDir, "pending");
168 mJournalDir.mkdirs(); // creates mBaseStateDir along the way
Christopher Tatecde87f42009-06-12 12:55:53 -0700169 makeJournalLocked(); // okay because no other threads are running yet
170
Christopher Tateabce4e82009-06-18 18:35:32 -0700171 // Build our mapping of uid to backup client services. This implicitly
172 // schedules a backup pass on the Package Manager metadata the first
173 // time anything needs to be backed up.
Christopher Tate3799bc22009-05-06 16:13:56 -0700174 synchronized (mBackupParticipants) {
175 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700176 }
177
Dan Egnor87a02bc2009-06-17 02:30:10 -0700178 // Set up our transport options and initialize the default transport
179 // TODO: Have transports register themselves somehow?
180 // TODO: Don't create transports that we don't need to?
Dan Egnor87a02bc2009-06-17 02:30:10 -0700181 mLocalTransport = new LocalTransport(context); // This is actually pretty cheap
Christopher Tate91717492009-06-26 21:07:13 -0700182 ComponentName localName = new ComponentName(context, LocalTransport.class);
183 registerTransport(localName.flattenToShortString(), mLocalTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700184
Christopher Tate91717492009-06-26 21:07:13 -0700185 mGoogleTransport = null;
186 // !!! TODO: set up the default transport name "the right way"
187 mCurrentTransport = SystemProperties.get(BACKUP_TRANSPORT_PROPERTY,
188 "com.google.android.backup/.BackupTransportService");
189 if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport);
190
191 // Attach to the Google backup transport. When this comes up, it will set
192 // itself as the current transport because we explicitly reset mCurrentTransport
193 // to null.
Dan Egnor87a02bc2009-06-17 02:30:10 -0700194 Intent intent = new Intent().setComponent(new ComponentName(
195 "com.google.android.backup",
196 "com.google.android.backup.BackupTransportService"));
197 context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE);
Christopher Tateaa088442009-06-16 18:25:46 -0700198
Christopher Tatecde87f42009-06-12 12:55:53 -0700199 // Now that we know about valid backup participants, parse any
200 // leftover journal files and schedule a new backup pass
201 parseLeftoverJournals();
202
Christopher Tate3799bc22009-05-06 16:13:56 -0700203 // Register for broadcasts about package install, etc., so we can
204 // update the provider list.
205 IntentFilter filter = new IntentFilter();
206 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
207 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
208 filter.addDataScheme("package");
209 mContext.registerReceiver(mBroadcastReceiver, filter);
210 }
211
Christopher Tatecde87f42009-06-12 12:55:53 -0700212 private void makeJournalLocked() {
213 try {
214 mJournal = File.createTempFile("journal", null, mJournalDir);
215 mJournalStream = new RandomAccessFile(mJournal, "rwd");
216 } catch (IOException e) {
217 Log.e(TAG, "Unable to write backup journals");
218 mJournal = null;
219 mJournalStream = null;
220 }
221 }
222
223 private void parseLeftoverJournals() {
224 if (mJournal != null) {
225 File[] allJournals = mJournalDir.listFiles();
226 for (File f : allJournals) {
227 if (f.compareTo(mJournal) != 0) {
228 // This isn't the current journal, so it must be a leftover. Read
229 // out the package names mentioned there and schedule them for
230 // backup.
231 try {
232 Log.i(TAG, "Found stale backup journal, scheduling:");
233 RandomAccessFile in = new RandomAccessFile(f, "r");
234 while (true) {
235 String packageName = in.readUTF();
236 Log.i(TAG, " + " + packageName);
237 dataChanged(packageName);
238 }
239 } catch (EOFException e) {
240 // no more data; we're done
241 } catch (Exception e) {
242 // can't read it or other error; just skip it
243 } finally {
244 // close/delete the file
245 f.delete();
246 }
247 }
248 }
249 }
250 }
251
Christopher Tate91717492009-06-26 21:07:13 -0700252 // Add a transport to our set of available backends
253 private void registerTransport(String name, IBackupTransport transport) {
254 synchronized (mTransports) {
255 mTransports.put(name, transport);
256 }
257 }
258
Christopher Tate3799bc22009-05-06 16:13:56 -0700259 // ----- Track installation/removal of packages -----
260 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
261 public void onReceive(Context context, Intent intent) {
262 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
263
264 Uri uri = intent.getData();
265 if (uri == null) {
266 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700267 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700268 String pkgName = uri.getSchemeSpecificPart();
269 if (pkgName == null) {
270 return;
271 }
272
273 String action = intent.getAction();
274 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
275 synchronized (mBackupParticipants) {
276 Bundle extras = intent.getExtras();
277 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
278 // The package was just upgraded
279 updatePackageParticipantsLocked(pkgName);
280 } else {
281 // The package was just added
282 addPackageParticipantsLocked(pkgName);
283 }
284 }
285 }
286 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
287 Bundle extras = intent.getExtras();
288 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
289 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
290 } else {
291 synchronized (mBackupParticipants) {
292 removePackageParticipantsLocked(pkgName);
293 }
294 }
295 }
296 }
297 };
298
Dan Egnor87a02bc2009-06-17 02:30:10 -0700299 // ----- Track connection to GoogleBackupTransport service -----
300 ServiceConnection mGoogleConnection = new ServiceConnection() {
301 public void onServiceConnected(ComponentName name, IBinder service) {
302 if (DEBUG) Log.v(TAG, "Connected to Google transport");
303 mGoogleTransport = IBackupTransport.Stub.asInterface(service);
Christopher Tate91717492009-06-26 21:07:13 -0700304 registerTransport(name.flattenToShortString(), mGoogleTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700305 }
306
307 public void onServiceDisconnected(ComponentName name) {
308 if (DEBUG) Log.v(TAG, "Disconnected from Google transport");
309 mGoogleTransport = null;
Christopher Tate91717492009-06-26 21:07:13 -0700310 registerTransport(name.flattenToShortString(), null);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700311 }
312 };
313
Joe Onorato8ad02812009-05-13 01:41:44 -0400314 // ----- Run the actual backup process asynchronously -----
315
Christopher Tate181fafa2009-05-14 11:12:14 -0700316 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400317 public void handleMessage(Message msg) {
318
319 switch (msg.what) {
320 case MSG_RUN_BACKUP:
Dan Egnor87a02bc2009-06-17 02:30:10 -0700321 {
Christopher Tate91717492009-06-26 21:07:13 -0700322 IBackupTransport transport = getTransport(mCurrentTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700323 if (transport == null) {
324 Log.v(TAG, "Backup requested but no transport available");
325 break;
326 }
327
Joe Onorato8ad02812009-05-13 01:41:44 -0400328 // snapshot the pending-backup set and work on that
Christopher Tate6aa41f42009-06-19 14:14:22 -0700329 ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
Christopher Tatecde87f42009-06-12 12:55:53 -0700330 File oldJournal = mJournal;
Joe Onorato8ad02812009-05-13 01:41:44 -0400331 synchronized (mQueueLock) {
Christopher Tateace7f092009-06-15 18:07:25 -0700332 if (mPendingBackups.size() == 0) {
333 Log.v(TAG, "Backup requested but nothing pending");
334 break;
335 }
336
Christopher Tate6aa41f42009-06-19 14:14:22 -0700337 for (BackupRequest b: mPendingBackups.values()) {
338 queue.add(b);
Joe Onorato8ad02812009-05-13 01:41:44 -0400339 }
Christopher Tate6aa41f42009-06-19 14:14:22 -0700340 Log.v(TAG, "clearing pending backups");
341 mPendingBackups.clear();
Christopher Tateace7f092009-06-15 18:07:25 -0700342
343 // Start a new backup-queue journal file too
Christopher Tatecde87f42009-06-12 12:55:53 -0700344 if (mJournalStream != null) {
345 try {
346 mJournalStream.close();
347 } catch (IOException e) {
348 // don't need to do anything
349 }
350 makeJournalLocked();
351 }
352
353 // At this point, we have started a new journal file, and the old
354 // file identity is being passed to the backup processing thread.
355 // When it completes successfully, that old journal file will be
356 // deleted. If we crash prior to that, the old journal is parsed
357 // at next boot and the journaled requests fulfilled.
Joe Onorato8ad02812009-05-13 01:41:44 -0400358 }
Dan Egnor87a02bc2009-06-17 02:30:10 -0700359
Christopher Tate6aa41f42009-06-19 14:14:22 -0700360 (new PerformBackupThread(transport, queue, oldJournal)).start();
Christopher Tate043dadc2009-06-02 16:11:00 -0700361 break;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700362 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700363
364 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400365 break;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700366
367 case MSG_RUN_RESTORE:
368 {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700369 RestoreParams params = (RestoreParams)msg.obj;
Dan Egnor156411d2009-06-26 13:20:02 -0700370 (new PerformRestoreThread(params.transport, params.observer, params.token)).start();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700371 break;
372 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400373 }
374 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400375 }
376
Christopher Tate181fafa2009-05-14 11:12:14 -0700377 // Add the backup agents in the given package to our set of known backup participants.
378 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700379 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700380 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700381 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700382 List<PackageInfo> targetApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700383 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700384 }
385
Christopher Tate181fafa2009-05-14 11:12:14 -0700386 private void addPackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700387 List<PackageInfo> targetPkgs) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700388 if (DEBUG) {
Dan Egnorefe52642009-06-24 00:16:33 -0700389 Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
390 for (PackageInfo p : targetPkgs) {
Christopher Tate111bd4a2009-06-24 17:29:38 -0700391 Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName
392 + " uid=" + p.applicationInfo.uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700393 }
394 }
395
Dan Egnorefe52642009-06-24 00:16:33 -0700396 for (PackageInfo pkg : targetPkgs) {
397 if (packageName == null || pkg.packageName.equals(packageName)) {
398 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700399 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700400 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700401 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700402 mBackupParticipants.put(uid, set);
403 }
Dan Egnorefe52642009-06-24 00:16:33 -0700404 set.add(pkg.applicationInfo);
Christopher Tate3799bc22009-05-06 16:13:56 -0700405 }
Christopher Tate487529a2009-04-29 14:03:25 -0700406 }
407 }
408
Christopher Tate6785dd82009-06-18 15:58:25 -0700409 // Remove the given package's entry from our known active set. If
410 // 'packageName' is null, *all* participating apps will be removed.
Christopher Tate3799bc22009-05-06 16:13:56 -0700411 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700412 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700413 List<PackageInfo> allApps = null;
Christopher Tate181fafa2009-05-14 11:12:14 -0700414 if (packageName != null) {
Dan Egnorefe52642009-06-24 00:16:33 -0700415 allApps = new ArrayList<PackageInfo>();
Christopher Tate181fafa2009-05-14 11:12:14 -0700416 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700417 int flags = PackageManager.GET_SIGNATURES;
418 allApps.add(mPackageManager.getPackageInfo(packageName, flags));
Christopher Tate181fafa2009-05-14 11:12:14 -0700419 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700420 // just skip it (???)
Christopher Tate181fafa2009-05-14 11:12:14 -0700421 }
422 } else {
423 // all apps with agents
Dan Egnorefe52642009-06-24 00:16:33 -0700424 allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700425 }
426 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700427 }
428
Joe Onorato8ad02812009-05-13 01:41:44 -0400429 private void removePackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700430 List<PackageInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700431 if (DEBUG) {
432 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
433 + ") removing " + agents.size() + " entries");
Dan Egnorefe52642009-06-24 00:16:33 -0700434 for (PackageInfo p : agents) {
435 Log.v(TAG, " - " + p);
Christopher Tate043dadc2009-06-02 16:11:00 -0700436 }
437 }
Dan Egnorefe52642009-06-24 00:16:33 -0700438 for (PackageInfo pkg : agents) {
439 if (packageName == null || pkg.packageName.equals(packageName)) {
440 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700441 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700442 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700443 // Find the existing entry with the same package name, and remove it.
444 // We can't just remove(app) because the instances are different.
445 for (ApplicationInfo entry: set) {
Dan Egnorefe52642009-06-24 00:16:33 -0700446 if (entry.packageName.equals(pkg.packageName)) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700447 set.remove(entry);
448 break;
449 }
450 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700451 if (set.size() == 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700452 mBackupParticipants.delete(uid);
453 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700454 }
455 }
456 }
457 }
458
Christopher Tate181fafa2009-05-14 11:12:14 -0700459 // Returns the set of all applications that define an android:backupAgent attribute
Dan Egnorefe52642009-06-24 00:16:33 -0700460 private List<PackageInfo> allAgentPackages() {
Christopher Tate6785dd82009-06-18 15:58:25 -0700461 // !!! TODO: cache this and regenerate only when necessary
Dan Egnorefe52642009-06-24 00:16:33 -0700462 int flags = PackageManager.GET_SIGNATURES;
463 List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
464 int N = packages.size();
465 for (int a = N-1; a >= 0; a--) {
466 ApplicationInfo app = packages.get(a).applicationInfo;
467 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
468 || app.backupAgentName == null) {
469 packages.remove(a);
Christopher Tate181fafa2009-05-14 11:12:14 -0700470 }
471 }
Dan Egnorefe52642009-06-24 00:16:33 -0700472 return packages;
Christopher Tate181fafa2009-05-14 11:12:14 -0700473 }
Christopher Tateaa088442009-06-16 18:25:46 -0700474
Christopher Tate3799bc22009-05-06 16:13:56 -0700475 // Reset the given package's known backup participants. Unlike add/remove, the update
476 // action cannot be passed a null package name.
477 void updatePackageParticipantsLocked(String packageName) {
478 if (packageName == null) {
479 Log.e(TAG, "updatePackageParticipants called with null package name");
480 return;
481 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700482 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700483
484 // brute force but small code size
Dan Egnorefe52642009-06-24 00:16:33 -0700485 List<PackageInfo> allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700486 removePackageParticipantsLockedInner(packageName, allApps);
487 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700488 }
489
Christopher Tate6785dd82009-06-18 15:58:25 -0700490 // The queue lock should be held when scheduling a backup pass
491 private void scheduleBackupPassLocked(long timeFromNowMillis) {
492 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
493 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, timeFromNowMillis);
494 }
495
Dan Egnor87a02bc2009-06-17 02:30:10 -0700496 // Return the given transport
Christopher Tate91717492009-06-26 21:07:13 -0700497 private IBackupTransport getTransport(String transportName) {
498 synchronized (mTransports) {
499 IBackupTransport transport = mTransports.get(transportName);
500 if (transport == null) {
501 Log.w(TAG, "Requested unavailable transport: " + transportName);
502 }
503 return transport;
Christopher Tate8c850b72009-06-07 19:33:20 -0700504 }
Christopher Tate8c850b72009-06-07 19:33:20 -0700505 }
506
Christopher Tatedf01dea2009-06-09 20:45:02 -0700507 // fire off a backup agent, blocking until it attaches or times out
508 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
509 IBackupAgent agent = null;
510 synchronized(mAgentConnectLock) {
511 mConnecting = true;
512 mConnectedAgent = null;
513 try {
514 if (mActivityManager.bindBackupAgent(app, mode)) {
515 Log.d(TAG, "awaiting agent for " + app);
516
517 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -0700518 // only wait 10 seconds for the clear data to happen
519 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
520 while (mConnecting && mConnectedAgent == null
521 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700522 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700523 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700524 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700525 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -0700526 return null;
527 }
528 }
529
530 // if we timed out with no connect, abort and move on
531 if (mConnecting == true) {
532 Log.w(TAG, "Timeout waiting for agent " + app);
533 return null;
534 }
535 agent = mConnectedAgent;
536 }
537 } catch (RemoteException e) {
538 // can't happen
539 }
540 }
541 return agent;
542 }
543
Christopher Tatec7b31e32009-06-10 15:49:30 -0700544 // clear an application's data, blocking until the operation completes or times out
545 void clearApplicationDataSynchronous(String packageName) {
Christopher Tatef7c886b2009-06-26 15:34:09 -0700546 // Don't wipe packages marked allowClearUserData=false
547 try {
548 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
549 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
550 if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping "
551 + packageName);
552 return;
553 }
554 } catch (NameNotFoundException e) {
555 Log.w(TAG, "Tried to clear data for " + packageName + " but not found");
556 return;
557 }
558
Christopher Tatec7b31e32009-06-10 15:49:30 -0700559 ClearDataObserver observer = new ClearDataObserver();
560
561 synchronized(mClearDataLock) {
562 mClearingData = true;
563 mPackageManager.clearApplicationUserData(packageName, observer);
564
565 // only wait 10 seconds for the clear data to happen
566 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
567 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
568 try {
569 mClearDataLock.wait(5000);
570 } catch (InterruptedException e) {
571 // won't happen, but still.
572 mClearingData = false;
573 }
574 }
575 }
576 }
577
578 class ClearDataObserver extends IPackageDataObserver.Stub {
579 public void onRemoveCompleted(String packageName, boolean succeeded)
580 throws android.os.RemoteException {
581 synchronized(mClearDataLock) {
582 mClearingData = false;
Christopher Tatef68eb502009-06-16 11:02:01 -0700583 mClearDataLock.notifyAll();
Christopher Tatec7b31e32009-06-10 15:49:30 -0700584 }
585 }
586 }
587
Christopher Tate043dadc2009-06-02 16:11:00 -0700588 // ----- Back up a set of applications via a worker thread -----
589
590 class PerformBackupThread extends Thread {
591 private static final String TAG = "PerformBackupThread";
Christopher Tateaa088442009-06-16 18:25:46 -0700592 IBackupTransport mTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700593 ArrayList<BackupRequest> mQueue;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700594 File mStateDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700595 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700596
Christopher Tateaa088442009-06-16 18:25:46 -0700597 public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue,
Christopher Tatecde87f42009-06-12 12:55:53 -0700598 File journal) {
Christopher Tateaa088442009-06-16 18:25:46 -0700599 mTransport = transport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700600 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700601 mJournal = journal;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700602
603 try {
604 mStateDir = new File(mBaseStateDir, transport.transportDirName());
605 } catch (RemoteException e) {
606 // can't happen; the transport is local
607 }
608 mStateDir.mkdirs();
Christopher Tate043dadc2009-06-02 16:11:00 -0700609 }
610
611 @Override
612 public void run() {
Christopher Tate043dadc2009-06-02 16:11:00 -0700613 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
614
Christopher Tate5cb400b2009-06-25 16:03:14 -0700615 // The package manager doesn't have a proper <application> etc, but since
616 // it's running here in the system process we can just set up its agent
617 // directly and use a synthetic BackupRequest. We always run this pass
618 // because it's cheap and this way we guarantee that we don't get out of
619 // step even if we're selecting among various transports at run time.
620 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
621 mPackageManager, allAgentPackages());
622 BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false);
623 pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL;
624 processOneBackup(pmRequest,
625 IBackupAgent.Stub.asInterface(pmAgent.onBind()),
626 mTransport);
Christopher Tate6785dd82009-06-18 15:58:25 -0700627
628 // Now run all the backups in our queue
Christopher Tateaa088442009-06-16 18:25:46 -0700629 doQueuedBackups(mTransport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700630
631 // Finally, tear down the transport
632 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700633 if (!mTransport.finishBackup()) {
634 // STOPSHIP TODO: handle errors
635 Log.e(TAG, "Backup failure in finishBackup()");
636 }
637 } catch (RemoteException e) {
638 Log.e(TAG, "Error in finishBackup()", e);
Christopher Tate043dadc2009-06-02 16:11:00 -0700639 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700640
641 if (!mJournal.delete()) {
642 Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath());
643 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700644 }
645
646 private void doQueuedBackups(IBackupTransport transport) {
647 for (BackupRequest request : mQueue) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700648 Log.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -0700649
650 IBackupAgent agent = null;
651 int mode = (request.fullBackup)
652 ? IApplicationThread.BACKUP_MODE_FULL
653 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
654 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700655 agent = bindToAgentSynchronous(request.appInfo, mode);
656 if (agent != null) {
657 processOneBackup(request, agent, transport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700658 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700659
660 // unbind even on timeout, just in case
661 mActivityManager.unbindBackupAgent(request.appInfo);
Christopher Tate043dadc2009-06-02 16:11:00 -0700662 } catch (SecurityException ex) {
663 // Try for the next one.
Christopher Tatec7b31e32009-06-10 15:49:30 -0700664 Log.d(TAG, "error in bind/backup", ex);
Christopher Tate043dadc2009-06-02 16:11:00 -0700665 } catch (RemoteException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700666 Log.v(TAG, "bind/backup threw");
667 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -0700668 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700669
Christopher Tate043dadc2009-06-02 16:11:00 -0700670 }
671 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700672
673 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
674 final String packageName = request.appInfo.packageName;
675 Log.d(TAG, "processOneBackup doBackup() on " + packageName);
676
677 try {
678 // Look up the package info & signatures. This is first so that if it
679 // throws an exception, there's no file setup yet that would need to
680 // be unraveled.
Christopher Tateabce4e82009-06-18 18:35:32 -0700681 PackageInfo packInfo;
682 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
683 // The metadata 'package' is synthetic
684 packInfo = new PackageInfo();
685 packInfo.packageName = packageName;
686 } else {
687 packInfo = mPackageManager.getPackageInfo(packageName,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700688 PackageManager.GET_SIGNATURES);
Christopher Tateabce4e82009-06-18 18:35:32 -0700689 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700690
691 // !!! TODO: get the state file dir from the transport
692 File savedStateName = new File(mStateDir, packageName);
693 File backupDataName = new File(mDataDir, packageName + ".data");
694 File newStateName = new File(mStateDir, packageName + ".new");
695
696 // In a full backup, we pass a null ParcelFileDescriptor as
697 // the saved-state "file"
698 ParcelFileDescriptor savedState = (request.fullBackup) ? null
699 : ParcelFileDescriptor.open(savedStateName,
700 ParcelFileDescriptor.MODE_READ_ONLY |
701 ParcelFileDescriptor.MODE_CREATE);
702
703 backupDataName.delete();
704 ParcelFileDescriptor backupData =
705 ParcelFileDescriptor.open(backupDataName,
706 ParcelFileDescriptor.MODE_READ_WRITE |
707 ParcelFileDescriptor.MODE_CREATE);
708
709 newStateName.delete();
710 ParcelFileDescriptor newState =
711 ParcelFileDescriptor.open(newStateName,
712 ParcelFileDescriptor.MODE_READ_WRITE |
713 ParcelFileDescriptor.MODE_CREATE);
714
715 // Run the target's backup pass
716 boolean success = false;
717 try {
718 agent.doBackup(savedState, backupData, newState);
719 success = true;
720 } finally {
721 if (savedState != null) {
722 savedState.close();
723 }
724 backupData.close();
725 newState.close();
726 }
727
728 // Now propagate the newly-backed-up data to the transport
729 if (success) {
730 if (DEBUG) Log.v(TAG, "doBackup() success; calling transport");
731 backupData =
732 ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY);
Dan Egnorefe52642009-06-24 00:16:33 -0700733 if (!transport.performBackup(packInfo, backupData)) {
734 // STOPSHIP TODO: handle errors
735 Log.e(TAG, "Backup failure in performBackup()");
736 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700737
738 // !!! TODO: After successful transport, delete the now-stale data
739 // and juggle the files so that next time the new state is passed
740 //backupDataName.delete();
741 newStateName.renameTo(savedStateName);
742 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700743 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700744 Log.e(TAG, "Error backing up " + packageName, e);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700745 }
746 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700747 }
748
Christopher Tatedf01dea2009-06-09 20:45:02 -0700749
750 // ----- Restore handling -----
751
Christopher Tateabce4e82009-06-18 18:35:32 -0700752 private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) {
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700753 // Allow unsigned apps, but not signed on one device and unsigned on the other
754 // !!! TODO: is this the right policy?
Christopher Tate6aa41f42009-06-19 14:14:22 -0700755 if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs
756 + " device=" + deviceSigs);
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700757 if ((storedSigs == null || storedSigs.length == 0)
758 && (deviceSigs == null || deviceSigs.length == 0)) {
759 return true;
760 }
761 if (storedSigs == null || deviceSigs == null) {
762 return false;
763 }
764
Christopher Tateabce4e82009-06-18 18:35:32 -0700765 // !!! TODO: this demands that every stored signature match one
766 // that is present on device, and does not demand the converse.
767 // Is this this right policy?
768 int nStored = storedSigs.length;
769 int nDevice = deviceSigs.length;
770
771 for (int i=0; i < nStored; i++) {
772 boolean match = false;
773 for (int j=0; j < nDevice; j++) {
774 if (storedSigs[i].equals(deviceSigs[j])) {
775 match = true;
776 break;
777 }
778 }
779 if (!match) {
780 return false;
781 }
782 }
783 return true;
784 }
785
Christopher Tatedf01dea2009-06-09 20:45:02 -0700786 class PerformRestoreThread extends Thread {
787 private IBackupTransport mTransport;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700788 private IRestoreObserver mObserver;
Dan Egnor156411d2009-06-26 13:20:02 -0700789 private long mToken;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700790 private RestoreSet mImage;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700791 private File mStateDir;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700792
Christopher Tate5cbbf562009-06-22 16:44:51 -0700793 class RestoreRequest {
794 public PackageInfo app;
795 public int storedAppVersion;
796
797 RestoreRequest(PackageInfo _app, int _version) {
798 app = _app;
799 storedAppVersion = _version;
800 }
801 }
802
Christopher Tate7d562ec2009-06-25 18:03:43 -0700803 PerformRestoreThread(IBackupTransport transport, IRestoreObserver observer,
Dan Egnor156411d2009-06-26 13:20:02 -0700804 long restoreSetToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700805 mTransport = transport;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700806 mObserver = observer;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700807 mToken = restoreSetToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700808
809 try {
810 mStateDir = new File(mBaseStateDir, transport.transportDirName());
811 } catch (RemoteException e) {
812 // can't happen; the transport is local
813 }
814 mStateDir.mkdirs();
Christopher Tatedf01dea2009-06-09 20:45:02 -0700815 }
816
817 @Override
818 public void run() {
Christopher Tate6aa41f42009-06-19 14:14:22 -0700819 if (DEBUG) Log.v(TAG, "Beginning restore process");
Christopher Tatedf01dea2009-06-09 20:45:02 -0700820 /**
821 * Restore sequence:
822 *
Dan Egnorefe52642009-06-24 00:16:33 -0700823 * 1. get the restore set description for our identity
824 * 2. for each app in the restore set:
Christopher Tatedf01dea2009-06-09 20:45:02 -0700825 * 3.a. if it's restorable on this device, add it to the restore queue
Dan Egnorefe52642009-06-24 00:16:33 -0700826 * 3. for each app in the restore queue:
827 * 3.a. clear the app data
828 * 3.b. get the restore data for the app from the transport
829 * 3.c. launch the backup agent for the app
830 * 3.d. agent.doRestore() with the data from the server
831 * 3.e. unbind the agent [and kill the app?]
832 * 4. shut down the transport
Christopher Tatedf01dea2009-06-09 20:45:02 -0700833 */
834
Christopher Tate7d562ec2009-06-25 18:03:43 -0700835 int error = -1; // assume error
836
Dan Egnorefe52642009-06-24 00:16:33 -0700837 // build the set of apps to restore
Christopher Tatedf01dea2009-06-09 20:45:02 -0700838 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700839 RestoreSet[] images = mTransport.getAvailableRestoreSets();
840 if (images == null) {
841 // STOPSHIP TODO: Handle the failure somehow?
842 Log.e(TAG, "Error getting restore sets");
843 return;
844 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700845
Dan Egnorefe52642009-06-24 00:16:33 -0700846 if (images.length == 0) {
847 Log.i(TAG, "No restore sets available");
848 return;
849 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700850
Dan Egnorefe52642009-06-24 00:16:33 -0700851 mImage = images[0];
Christopher Tateabce4e82009-06-18 18:35:32 -0700852
Dan Egnorefe52642009-06-24 00:16:33 -0700853 // Get the list of all packages which have backup enabled.
854 // (Include the Package Manager metadata pseudo-package first.)
855 ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>();
856 PackageInfo omPackage = new PackageInfo();
857 omPackage.packageName = PACKAGE_MANAGER_SENTINEL;
858 restorePackages.add(omPackage);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700859
Dan Egnorefe52642009-06-24 00:16:33 -0700860 List<PackageInfo> agentPackages = allAgentPackages();
861 restorePackages.addAll(agentPackages);
862
Christopher Tate7d562ec2009-06-25 18:03:43 -0700863 // let the observer know that we're running
864 if (mObserver != null) {
865 try {
866 // !!! TODO: get an actual count from the transport after
867 // its startRestore() runs?
868 mObserver.restoreStarting(restorePackages.size());
869 } catch (RemoteException e) {
870 Log.d(TAG, "Restore observer died at restoreStarting");
871 mObserver = null;
872 }
873 }
874
Dan Egnor156411d2009-06-26 13:20:02 -0700875 if (!mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0]))) {
Dan Egnorefe52642009-06-24 00:16:33 -0700876 // STOPSHIP TODO: Handle the failure somehow?
877 Log.e(TAG, "Error starting restore operation");
878 return;
879 }
880
881 String packageName = mTransport.nextRestorePackage();
882 if (packageName == null) {
883 // STOPSHIP TODO: Handle the failure somehow?
884 Log.e(TAG, "Error getting first restore package");
885 return;
886 } else if (packageName.equals("")) {
887 Log.i(TAG, "No restore data available");
888 return;
889 } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
890 Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL
891 + "\", found only \"" + packageName + "\"");
892 return;
893 }
894
895 // Pull the Package Manager metadata from the restore set first
896 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
897 mPackageManager, agentPackages);
898 processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()));
899
Christopher Tate7d562ec2009-06-25 18:03:43 -0700900 int count = 0;
Dan Egnorefe52642009-06-24 00:16:33 -0700901 for (;;) {
902 packageName = mTransport.nextRestorePackage();
903 if (packageName == null) {
904 // STOPSHIP TODO: Handle the failure somehow?
905 Log.e(TAG, "Error getting next restore package");
906 return;
907 } else if (packageName.equals("")) {
908 break;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700909 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700910
Christopher Tate7d562ec2009-06-25 18:03:43 -0700911 if (mObserver != null) {
912 ++count;
913 try {
914 mObserver.onUpdate(count);
915 } catch (RemoteException e) {
916 Log.d(TAG, "Restore observer died in onUpdate");
917 mObserver = null;
918 }
919 }
920
Dan Egnorefe52642009-06-24 00:16:33 -0700921 Metadata metaInfo = pmAgent.getRestoredMetadata(packageName);
922 if (metaInfo == null) {
923 Log.e(TAG, "Missing metadata for " + packageName);
924 continue;
925 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700926
Dan Egnorefe52642009-06-24 00:16:33 -0700927 int flags = PackageManager.GET_SIGNATURES;
928 PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags);
929 if (metaInfo.versionCode > packageInfo.versionCode) {
930 Log.w(TAG, "Package " + packageName
931 + " restore version [" + metaInfo.versionCode
932 + "] is too new for installed version ["
933 + packageInfo.versionCode + "]");
934 continue;
935 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700936
Dan Egnorefe52642009-06-24 00:16:33 -0700937 if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) {
938 Log.w(TAG, "Signature mismatch restoring " + packageName);
939 continue;
940 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700941
Dan Egnorefe52642009-06-24 00:16:33 -0700942 if (DEBUG) Log.v(TAG, "Package " + packageName
943 + " restore version [" + metaInfo.versionCode
944 + "] is compatible with installed version ["
945 + packageInfo.versionCode + "]");
Christopher Tatec7b31e32009-06-10 15:49:30 -0700946
Dan Egnorefe52642009-06-24 00:16:33 -0700947 // Now perform the actual restore
948 clearApplicationDataSynchronous(packageName);
949 IBackupAgent agent = bindToAgentSynchronous(
950 packageInfo.applicationInfo,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700951 IApplicationThread.BACKUP_MODE_RESTORE);
Dan Egnorefe52642009-06-24 00:16:33 -0700952 if (agent == null) {
953 Log.w(TAG, "Can't find backup agent for " + packageName);
954 continue;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700955 }
956
Dan Egnorefe52642009-06-24 00:16:33 -0700957 try {
958 processOneRestore(packageInfo, metaInfo.versionCode, agent);
959 } finally {
960 // unbind even on timeout or failure, just in case
961 mActivityManager.unbindBackupAgent(packageInfo.applicationInfo);
962 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700963 }
Christopher Tate7d562ec2009-06-25 18:03:43 -0700964
965 // if we get this far, report success to the observer
966 error = 0;
Dan Egnorefe52642009-06-24 00:16:33 -0700967 } catch (NameNotFoundException e) {
968 // STOPSHIP TODO: Handle the failure somehow?
969 Log.e(TAG, "Invalid paackage restoring data", e);
970 } catch (RemoteException e) {
971 // STOPSHIP TODO: Handle the failure somehow?
972 Log.e(TAG, "Error restoring data", e);
973 } finally {
974 try {
975 mTransport.finishRestore();
976 } catch (RemoteException e) {
977 Log.e(TAG, "Error finishing restore", e);
978 }
Christopher Tate7d562ec2009-06-25 18:03:43 -0700979
980 if (mObserver != null) {
981 try {
982 mObserver.restoreFinished(error);
983 } catch (RemoteException e) {
984 Log.d(TAG, "Restore observer died at restoreFinished");
985 }
986 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700987 }
988 }
989
Dan Egnorefe52642009-06-24 00:16:33 -0700990 // Do the guts of a restore of one application, using mTransport.getRestoreData().
991 void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700992 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -0700993 final String packageName = app.packageName;
994
995 // !!! TODO: get the dirs from the transport
996 File backupDataName = new File(mDataDir, packageName + ".restore");
997 backupDataName.delete();
998 try {
999 ParcelFileDescriptor backupData =
1000 ParcelFileDescriptor.open(backupDataName,
1001 ParcelFileDescriptor.MODE_READ_WRITE |
1002 ParcelFileDescriptor.MODE_CREATE);
1003
1004 // Run the transport's restore pass
1005 // Run the target's backup pass
Christopher Tatec7b31e32009-06-10 15:49:30 -07001006 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001007 if (!mTransport.getRestoreData(backupData)) {
1008 // STOPSHIP TODO: Handle this error somehow?
1009 Log.e(TAG, "Error getting restore data for " + packageName);
1010 return;
1011 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001012 } finally {
1013 backupData.close();
1014 }
1015
1016 // Okay, we have the data. Now have the agent do the restore.
1017 File newStateName = new File(mStateDir, packageName + ".new");
1018 ParcelFileDescriptor newState =
1019 ParcelFileDescriptor.open(newStateName,
1020 ParcelFileDescriptor.MODE_READ_WRITE |
1021 ParcelFileDescriptor.MODE_CREATE);
1022
1023 backupData = ParcelFileDescriptor.open(backupDataName,
1024 ParcelFileDescriptor.MODE_READ_ONLY);
1025
Christopher Tatec7b31e32009-06-10 15:49:30 -07001026 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001027 agent.doRestore(backupData, appVersionCode, newState);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001028 } finally {
1029 newState.close();
1030 backupData.close();
1031 }
1032
1033 // if everything went okay, remember the recorded state now
Dan Egnorefe52642009-06-24 00:16:33 -07001034 File savedStateName = new File(mStateDir, packageName);
1035 newStateName.renameTo(savedStateName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001036 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -07001037 Log.e(TAG, "Error restoring data for " + packageName, e);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001038 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001039 }
1040 }
1041
1042
Christopher Tate487529a2009-04-29 14:03:25 -07001043 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -07001044
Christopher Tatea8bf8152009-04-30 11:36:21 -07001045 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -07001046 // Record that we need a backup pass for the caller. Since multiple callers
1047 // may share a uid, we need to note all candidates within that uid and schedule
1048 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001049
1050 Log.d(TAG, "dataChanged packageName=" + packageName);
Christopher Tate63d27002009-06-16 17:16:42 -07001051
1052 // If the caller does not hold the BACKUP permission, it can only request a
1053 // backup of its own data.
1054 HashSet<ApplicationInfo> targets;
1055 if ((mContext.checkPermission("android.permission.BACKUP", Binder.getCallingPid(),
1056 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1057 targets = mBackupParticipants.get(Binder.getCallingUid());
1058 } else {
1059 // a caller with full permission can ask to back up any participating app
1060 // !!! TODO: allow backup of ANY app?
1061 if (DEBUG) Log.v(TAG, "Privileged caller, allowing backup of other apps");
1062 targets = new HashSet<ApplicationInfo>();
1063 int N = mBackupParticipants.size();
1064 for (int i = 0; i < N; i++) {
1065 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1066 if (s != null) {
1067 targets.addAll(s);
1068 }
1069 }
1070 }
Christopher Tate487529a2009-04-29 14:03:25 -07001071 if (targets != null) {
1072 synchronized (mQueueLock) {
1073 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -07001074 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -07001075 // validate the caller-supplied package name against the known set of
1076 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -07001077 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -04001078 // Add the caller to the set of pending backups. If there is
1079 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -07001080 BackupRequest req = new BackupRequest(app, false);
1081 mPendingBackups.put(app, req);
Christopher Tatecde87f42009-06-12 12:55:53 -07001082
1083 // Journal this request in case of crash
1084 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001085 }
1086 }
1087
Christopher Tate181fafa2009-05-14 11:12:14 -07001088 if (DEBUG) {
1089 int numKeys = mPendingBackups.size();
1090 Log.d(TAG, "Scheduling backup for " + numKeys + " participants:");
1091 for (BackupRequest b : mPendingBackups.values()) {
1092 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
1093 }
1094 }
Christopher Tate487529a2009-04-29 14:03:25 -07001095 // Schedule a backup pass in a few minutes. As backup-eligible data
1096 // keeps changing, continue to defer the backup pass until things
1097 // settle down, to avoid extra overhead.
Christopher Tate6785dd82009-06-18 15:58:25 -07001098 scheduleBackupPassLocked(COLLECTION_INTERVAL);
Christopher Tate487529a2009-04-29 14:03:25 -07001099 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001100 } else {
1101 Log.w(TAG, "dataChanged but no participant pkg " + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001102 }
1103 }
Christopher Tate46758122009-05-06 11:22:00 -07001104
Christopher Tatecde87f42009-06-12 12:55:53 -07001105 private void writeToJournalLocked(String str) {
1106 if (mJournalStream != null) {
1107 try {
1108 mJournalStream.writeUTF(str);
1109 } catch (IOException e) {
1110 Log.e(TAG, "Error writing to backup journal");
1111 mJournalStream = null;
1112 mJournal = null;
1113 }
1114 }
1115 }
1116
Christopher Tateace7f092009-06-15 18:07:25 -07001117 // Run a backup pass immediately for any applications that have declared
1118 // that they have pending updates.
1119 public void backupNow() throws RemoteException {
1120 mContext.enforceCallingPermission("android.permission.BACKUP", "tryBackupNow");
Christopher Tate043dadc2009-06-02 16:11:00 -07001121
Christopher Tateace7f092009-06-15 18:07:25 -07001122 if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass");
Christopher Tate46758122009-05-06 11:22:00 -07001123 synchronized (mQueueLock) {
Christopher Tate6785dd82009-06-18 15:58:25 -07001124 scheduleBackupPassLocked(0);
Christopher Tate46758122009-05-06 11:22:00 -07001125 }
1126 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001127
Christopher Tate91717492009-06-26 21:07:13 -07001128 // Report the name of the currently active transport
1129 public String getCurrentTransport() {
Christopher Tateace7f092009-06-15 18:07:25 -07001130 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
Christopher Tate91717492009-06-26 21:07:13 -07001131 Log.v(TAG, "getCurrentTransport() returning " + mCurrentTransport);
1132 return mCurrentTransport;
Christopher Tateace7f092009-06-15 18:07:25 -07001133 }
1134
Christopher Tate91717492009-06-26 21:07:13 -07001135 // Report all known, available backup transports
1136 public String[] listAllTransports() {
Christopher Tate043dadc2009-06-02 16:11:00 -07001137 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
1138
Christopher Tate91717492009-06-26 21:07:13 -07001139 String[] list = null;
1140 ArrayList<String> known = new ArrayList<String>();
1141 for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) {
1142 if (entry.getValue() != null) {
1143 known.add(entry.getKey());
1144 }
1145 }
1146
1147 if (known.size() > 0) {
1148 list = new String[known.size()];
1149 known.toArray(list);
1150 }
1151 return list;
1152 }
1153
1154 // Select which transport to use for the next backup operation. If the given
1155 // name is not one of the available transports, no action is taken and the method
1156 // returns null.
1157 public String selectBackupTransport(String transport) {
1158 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
1159
1160 synchronized (mTransports) {
1161 String prevTransport = null;
1162 if (mTransports.get(transport) != null) {
1163 prevTransport = mCurrentTransport;
1164 mCurrentTransport = transport;
1165 SystemProperties.set(BACKUP_TRANSPORT_PROPERTY, transport);
1166 Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport
1167 + " returning " + prevTransport);
1168 } else {
1169 Log.w(TAG, "Attempt to select unavailable transport " + transport);
1170 }
1171 return prevTransport;
1172 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001173 }
1174
1175 // Callback: a requested backup agent has been instantiated. This should only
1176 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001177 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -07001178 synchronized(mAgentConnectLock) {
1179 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1180 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
1181 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
1182 mConnectedAgent = agent;
1183 mConnecting = false;
1184 } else {
1185 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1186 + " claiming agent connected");
1187 }
1188 mAgentConnectLock.notifyAll();
1189 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001190 }
1191
1192 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
1193 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -07001194 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001195 public void agentDisconnected(String packageName) {
1196 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -07001197 synchronized(mAgentConnectLock) {
1198 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1199 mConnectedAgent = null;
1200 mConnecting = false;
1201 } else {
1202 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1203 + " claiming agent disconnected");
1204 }
1205 mAgentConnectLock.notifyAll();
1206 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001207 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001208
Christopher Tate8c850b72009-06-07 19:33:20 -07001209 // Hand off a restore session
Christopher Tate91717492009-06-26 21:07:13 -07001210 public IRestoreSession beginRestoreSession(String transport) {
Christopher Tate8c850b72009-06-07 19:33:20 -07001211 mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession");
Christopher Tatef68eb502009-06-16 11:02:01 -07001212
1213 synchronized(this) {
1214 if (mActiveRestoreSession != null) {
1215 Log.d(TAG, "Restore session requested but one already active");
1216 return null;
1217 }
Christopher Tate91717492009-06-26 21:07:13 -07001218 mActiveRestoreSession = new RestoreSession(transport);
Christopher Tatef68eb502009-06-16 11:02:01 -07001219 }
1220 return mActiveRestoreSession;
Christopher Tate8c850b72009-06-07 19:33:20 -07001221 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001222
Christopher Tate9b3905c2009-06-08 15:24:01 -07001223 // ----- Restore session -----
1224
1225 class RestoreSession extends IRestoreSession.Stub {
Christopher Tatef68eb502009-06-16 11:02:01 -07001226 private static final String TAG = "RestoreSession";
1227
Christopher Tate9b3905c2009-06-08 15:24:01 -07001228 private IBackupTransport mRestoreTransport = null;
1229 RestoreSet[] mRestoreSets = null;
1230
Christopher Tate91717492009-06-26 21:07:13 -07001231 RestoreSession(String transport) {
1232 mRestoreTransport = getTransport(transport);
Christopher Tate9b3905c2009-06-08 15:24:01 -07001233 }
1234
1235 // --- Binder interface ---
1236 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001237 mContext.enforceCallingPermission("android.permission.BACKUP",
1238 "getAvailableRestoreSets");
1239
Christopher Tatef68eb502009-06-16 11:02:01 -07001240 try {
Christopher Tate9b3905c2009-06-08 15:24:01 -07001241 synchronized(this) {
1242 if (mRestoreSets == null) {
1243 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
1244 }
1245 return mRestoreSets;
1246 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001247 } catch (RuntimeException e) {
1248 Log.d(TAG, "getAvailableRestoreSets exception");
1249 e.printStackTrace();
1250 throw e;
1251 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001252 }
1253
Dan Egnor156411d2009-06-26 13:20:02 -07001254 public int performRestore(long token, IRestoreObserver observer)
Christopher Tate7d562ec2009-06-25 18:03:43 -07001255 throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001256 mContext.enforceCallingPermission("android.permission.BACKUP", "performRestore");
1257
1258 if (mRestoreSets != null) {
1259 for (int i = 0; i < mRestoreSets.length; i++) {
1260 if (token == mRestoreSets[i].token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -07001261 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
Dan Egnor156411d2009-06-26 13:20:02 -07001262 msg.obj = new RestoreParams(mRestoreTransport, observer, token);
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001263 mBackupHandler.sendMessage(msg);
1264 return 0;
1265 }
1266 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001267 } else {
1268 if (DEBUG) Log.v(TAG, "No current restore set, not doing restore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001269 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001270 return -1;
1271 }
1272
1273 public void endRestoreSession() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001274 mContext.enforceCallingPermission("android.permission.BACKUP",
1275 "endRestoreSession");
1276
Dan Egnorefe52642009-06-24 00:16:33 -07001277 mRestoreTransport.finishRestore();
Christopher Tate9b3905c2009-06-08 15:24:01 -07001278 mRestoreTransport = null;
Christopher Tatef68eb502009-06-16 11:02:01 -07001279 synchronized(BackupManagerService.this) {
1280 if (BackupManagerService.this.mActiveRestoreSession == this) {
1281 BackupManagerService.this.mActiveRestoreSession = null;
1282 } else {
1283 Log.e(TAG, "ending non-current restore session");
1284 }
1285 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001286 }
1287 }
1288
Christopher Tate043dadc2009-06-02 16:11:00 -07001289
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001290 @Override
1291 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1292 synchronized (mQueueLock) {
Christopher Tate91717492009-06-26 21:07:13 -07001293 pw.println("Available transports:");
1294 for (String t : listAllTransports()) {
1295 pw.println(" " + t);
1296 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001297 int N = mBackupParticipants.size();
1298 pw.println("Participants:");
1299 for (int i=0; i<N; i++) {
1300 int uid = mBackupParticipants.keyAt(i);
1301 pw.print(" uid: ");
1302 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07001303 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
1304 for (ApplicationInfo app: participants) {
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001305 pw.print(" ");
Christopher Tate181fafa2009-05-14 11:12:14 -07001306 pw.println(app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001307 }
1308 }
Christopher Tate6aa41f42009-06-19 14:14:22 -07001309 pw.println("Pending: " + mPendingBackups.size());
1310 for (BackupRequest req : mPendingBackups.values()) {
1311 pw.print(" ");
1312 pw.println(req);
Christopher Tate181fafa2009-05-14 11:12:14 -07001313 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001314 }
1315 }
Christopher Tate487529a2009-04-29 14:03:25 -07001316}