blob: 5b70c2c47292c95975f4e973b7aafa501d42ef41 [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;
Christopher Tate487529a2009-04-29 14:03:25 -070024import android.content.Context;
25import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070026import android.content.IntentFilter;
Christopher Tate181fafa2009-05-14 11:12:14 -070027import android.content.pm.ApplicationInfo;
Christopher Tate487529a2009-04-29 14:03:25 -070028import android.content.pm.PackageManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070029import android.content.pm.PackageManager.NameNotFoundException;
Christopher Tate3799bc22009-05-06 16:13:56 -070030import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070031import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070032import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070033import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070034import android.os.Handler;
35import android.os.IBinder;
36import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070037import android.os.ParcelFileDescriptor;
Christopher Tate043dadc2009-06-02 16:11:00 -070038import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070039import android.os.RemoteException;
40import android.util.Log;
41import android.util.SparseArray;
42
43import android.backup.IBackupManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070044import android.backup.BackupManager;
45
46import com.android.internal.backup.AdbTransport;
47import com.android.internal.backup.GoogleTransport;
48import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070049
Christopher Tate22b87872009-05-04 16:41:53 -070050import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070051import java.io.FileDescriptor;
Christopher Tate22b87872009-05-04 16:41:53 -070052import java.io.FileNotFoundException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070053import java.io.PrintWriter;
Christopher Tate487529a2009-04-29 14:03:25 -070054import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040055import java.util.ArrayList;
56import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070057import java.util.HashSet;
Christopher Tate181fafa2009-05-14 11:12:14 -070058import java.util.Iterator;
Christopher Tate487529a2009-04-29 14:03:25 -070059import java.util.List;
60
61class BackupManagerService extends IBackupManager.Stub {
62 private static final String TAG = "BackupManagerService";
63 private static final boolean DEBUG = true;
64
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070065 private static final long COLLECTION_INTERVAL = 1000;
66 //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
Christopher Tate487529a2009-04-29 14:03:25 -070067
68 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070069 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate487529a2009-04-29 14:03:25 -070070
71 private Context mContext;
72 private PackageManager mPackageManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070073 private final IActivityManager mActivityManager;
Christopher Tate487529a2009-04-29 14:03:25 -070074 private final BackupHandler mBackupHandler = new BackupHandler();
75 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate181fafa2009-05-14 11:12:14 -070076 private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
77 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -070078 // set of backup services that have pending changes
Christopher Tate46758122009-05-06 11:22:00 -070079 private class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -070080 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -070081 public boolean fullBackup;
82
Christopher Tate181fafa2009-05-14 11:12:14 -070083 BackupRequest(ApplicationInfo app, boolean isFull) {
84 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -070085 fullBackup = isFull;
86 }
Christopher Tate181fafa2009-05-14 11:12:14 -070087
88 public String toString() {
89 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
90 }
Christopher Tate46758122009-05-06 11:22:00 -070091 }
Joe Onorato8ad02812009-05-13 01:41:44 -040092 // Backups that we haven't started yet.
Christopher Tate181fafa2009-05-14 11:12:14 -070093 private HashMap<ApplicationInfo,BackupRequest> mPendingBackups
94 = new HashMap<ApplicationInfo,BackupRequest>();
Joe Onorato8ad02812009-05-13 01:41:44 -040095 // Backups that we have started. These are separate to prevent starvation
96 // if an app keeps re-enqueuing itself.
97 private ArrayList<BackupRequest> mBackupQueue;
Christopher Tate487529a2009-04-29 14:03:25 -070098 private final Object mQueueLock = new Object();
99
Christopher Tate043dadc2009-06-02 16:11:00 -0700100 // The thread performing the sequence of queued backups binds to each app's agent
101 // in succession. Bind notifications are asynchronously delivered through the
102 // Activity Manager; use this lock object to signal when a requested binding has
103 // completed.
104 private final Object mAgentConnectLock = new Object();
105 private IBackupAgent mConnectedAgent;
106 private volatile boolean mConnecting;
107
108 private int mTransportId;
109
Christopher Tate22b87872009-05-04 16:41:53 -0700110 private File mStateDir;
Christopher Tatef4172472009-05-05 15:50:03 -0700111 private File mDataDir;
Christopher Tate487529a2009-04-29 14:03:25 -0700112
Christopher Tate487529a2009-04-29 14:03:25 -0700113 public BackupManagerService(Context context) {
114 mContext = context;
115 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700116 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700117
Christopher Tate22b87872009-05-04 16:41:53 -0700118 // Set up our bookkeeping
Christopher Tatef4172472009-05-05 15:50:03 -0700119 mStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tate22b87872009-05-04 16:41:53 -0700120 mStateDir.mkdirs();
Christopher Tatef4172472009-05-05 15:50:03 -0700121 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate043dadc2009-06-02 16:11:00 -0700122 mTransportId = BackupManager.TRANSPORT_GOOGLE;
Christopher Tate22b87872009-05-04 16:41:53 -0700123
Christopher Tate3799bc22009-05-06 16:13:56 -0700124 // Build our mapping of uid to backup client services
125 synchronized (mBackupParticipants) {
126 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700127 }
128
Christopher Tate3799bc22009-05-06 16:13:56 -0700129 // Register for broadcasts about package install, etc., so we can
130 // update the provider list.
131 IntentFilter filter = new IntentFilter();
132 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
133 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
134 filter.addDataScheme("package");
135 mContext.registerReceiver(mBroadcastReceiver, filter);
136 }
137
138 // ----- Track installation/removal of packages -----
139 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
140 public void onReceive(Context context, Intent intent) {
141 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
142
143 Uri uri = intent.getData();
144 if (uri == null) {
145 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700146 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700147 String pkgName = uri.getSchemeSpecificPart();
148 if (pkgName == null) {
149 return;
150 }
151
Christopher Tate043dadc2009-06-02 16:11:00 -0700152 // !!! TODO: this is buggy right now; we wind up with duplicate participant entries
153 // after using 'adb install -r' of a participating app
Christopher Tate3799bc22009-05-06 16:13:56 -0700154 String action = intent.getAction();
155 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
156 synchronized (mBackupParticipants) {
157 Bundle extras = intent.getExtras();
158 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
159 // The package was just upgraded
160 updatePackageParticipantsLocked(pkgName);
161 } else {
162 // The package was just added
163 addPackageParticipantsLocked(pkgName);
164 }
165 }
166 }
167 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
168 Bundle extras = intent.getExtras();
169 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
170 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
171 } else {
172 synchronized (mBackupParticipants) {
173 removePackageParticipantsLocked(pkgName);
174 }
175 }
176 }
177 }
178 };
179
Joe Onorato8ad02812009-05-13 01:41:44 -0400180 // ----- Run the actual backup process asynchronously -----
181
Christopher Tate181fafa2009-05-14 11:12:14 -0700182 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400183 public void handleMessage(Message msg) {
184
185 switch (msg.what) {
186 case MSG_RUN_BACKUP:
187 // snapshot the pending-backup set and work on that
188 synchronized (mQueueLock) {
Joe Onoratod2110db2009-05-19 13:41:21 -0700189 if (mBackupQueue == null) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700190 mBackupQueue = new ArrayList<BackupRequest>();
Joe Onoratod2110db2009-05-19 13:41:21 -0700191 for (BackupRequest b: mPendingBackups.values()) {
192 mBackupQueue.add(b);
193 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700194 mPendingBackups = new HashMap<ApplicationInfo,BackupRequest>();
Joe Onorato8ad02812009-05-13 01:41:44 -0400195 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400196 // !!! TODO: start a new backup-queue journal file too
197 // WARNING: If we crash after this line, anything in mPendingBackups will
198 // be lost. FIX THIS.
199 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700200 (new PerformBackupThread(mTransportId, mBackupQueue)).run();
201 break;
202
203 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400204 break;
205 }
206 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400207 }
208
Christopher Tate043dadc2009-06-02 16:11:00 -0700209 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
210 final String packageName = request.appInfo.packageName;
Christopher Tate181fafa2009-05-14 11:12:14 -0700211 Log.d(TAG, "processOneBackup doBackup() on " + packageName);
Joe Onorato8ad02812009-05-13 01:41:44 -0400212
Christopher Tate181fafa2009-05-14 11:12:14 -0700213 try {
Christopher Tate043dadc2009-06-02 16:11:00 -0700214 // !!! TODO: get the state file dir from the transport
215 File savedStateName = new File(mStateDir, packageName);
216 File backupDataName = new File(mDataDir, packageName + ".data");
217 File newStateName = new File(mStateDir, packageName + ".new");
Joe Onorato8ad02812009-05-13 01:41:44 -0400218
219 // In a full backup, we pass a null ParcelFileDescriptor as
220 // the saved-state "file"
221 ParcelFileDescriptor savedState = (request.fullBackup) ? null
222 : ParcelFileDescriptor.open(savedStateName,
223 ParcelFileDescriptor.MODE_READ_ONLY |
224 ParcelFileDescriptor.MODE_CREATE);
225
226 backupDataName.delete();
227 ParcelFileDescriptor backupData =
228 ParcelFileDescriptor.open(backupDataName,
229 ParcelFileDescriptor.MODE_READ_WRITE |
230 ParcelFileDescriptor.MODE_CREATE);
231
232 newStateName.delete();
233 ParcelFileDescriptor newState =
234 ParcelFileDescriptor.open(newStateName,
235 ParcelFileDescriptor.MODE_READ_WRITE |
236 ParcelFileDescriptor.MODE_CREATE);
237
238 // Run the target's backup pass
Christopher Tate043dadc2009-06-02 16:11:00 -0700239 boolean success = false;
Joe Onorato8ad02812009-05-13 01:41:44 -0400240 try {
Christopher Tate043dadc2009-06-02 16:11:00 -0700241 agent.doBackup(savedState, backupData, newState);
242 success = true;
Joe Onorato8ad02812009-05-13 01:41:44 -0400243 } finally {
244 if (savedState != null) {
245 savedState.close();
246 }
247 backupData.close();
248 newState.close();
249 }
250
Christopher Tate043dadc2009-06-02 16:11:00 -0700251 // Now propagate the newly-backed-up data to the transport
252 if (success) {
Christopher Tate1885b372009-06-04 15:00:33 -0700253 if (DEBUG) Log.v(TAG, "doBackup() success; calling transport");
Christopher Tate043dadc2009-06-02 16:11:00 -0700254 backupData =
255 ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY);
256 int error = transport.performBackup(packageName, backupData);
257
258 // !!! TODO: After successful transport, delete the now-stale data
259 // and juggle the files so that next time the new state is passed
260 //backupDataName.delete();
261 newStateName.renameTo(savedStateName);
262 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400263 } catch (FileNotFoundException fnf) {
264 Log.d(TAG, "File not found on backup: ");
265 fnf.printStackTrace();
266 } catch (RemoteException e) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700267 Log.d(TAG, "Remote target " + request.appInfo.packageName + " threw during backup:");
Joe Onorato8ad02812009-05-13 01:41:44 -0400268 e.printStackTrace();
269 } catch (Exception e) {
270 Log.w(TAG, "Final exception guard in backup: ");
271 e.printStackTrace();
272 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400273 }
274
Christopher Tate181fafa2009-05-14 11:12:14 -0700275 // Add the backup agents in the given package to our set of known backup participants.
276 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700277 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700278 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700279 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700280 List<ApplicationInfo> targetApps = allAgentApps();
281 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700282 }
283
Christopher Tate181fafa2009-05-14 11:12:14 -0700284 private void addPackageParticipantsLockedInner(String packageName,
285 List<ApplicationInfo> targetApps) {
286 if (DEBUG) {
287 Log.v(TAG, "Adding " + targetApps.size() + " backup participants:");
288 for (ApplicationInfo a : targetApps) {
289 Log.v(TAG, " " + a + " agent=" + a.backupAgentName);
290 }
291 }
292
293 for (ApplicationInfo app : targetApps) {
294 if (packageName == null || app.packageName.equals(packageName)) {
295 int uid = app.uid;
296 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700297 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700298 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700299 mBackupParticipants.put(uid, set);
300 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700301 set.add(app);
Christopher Tate3799bc22009-05-06 16:13:56 -0700302 }
Christopher Tate487529a2009-04-29 14:03:25 -0700303 }
304 }
305
Christopher Tate3799bc22009-05-06 16:13:56 -0700306 // Remove the given package's backup services from our known active set. If
307 // 'packageName' is null, *all* backup services will be removed.
308 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700309 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700310 List<ApplicationInfo> allApps = null;
311 if (packageName != null) {
312 allApps = new ArrayList<ApplicationInfo>();
313 try {
314 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
315 allApps.add(app);
316 } catch (Exception e) {
317 // just skip it
318 }
319 } else {
320 // all apps with agents
321 allApps = allAgentApps();
322 }
323 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700324 }
325
Joe Onorato8ad02812009-05-13 01:41:44 -0400326 private void removePackageParticipantsLockedInner(String packageName,
Christopher Tate181fafa2009-05-14 11:12:14 -0700327 List<ApplicationInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700328 if (DEBUG) {
329 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
330 + ") removing " + agents.size() + " entries");
331 for (ApplicationInfo a : agents) {
332 Log.v(TAG, " - " + a);
333 }
334 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700335 for (ApplicationInfo app : agents) {
336 if (packageName == null || app.packageName.equals(packageName)) {
337 int uid = app.uid;
338 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700339 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700340 // Find the existing entry with the same package name, and remove it.
341 // We can't just remove(app) because the instances are different.
342 for (ApplicationInfo entry: set) {
343 if (entry.packageName.equals(app.packageName)) {
344 set.remove(entry);
345 break;
346 }
347 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700348 if (set.size() == 0) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700349 mBackupParticipants.delete(uid); }
Christopher Tate3799bc22009-05-06 16:13:56 -0700350 }
351 }
352 }
353 }
354
Christopher Tate181fafa2009-05-14 11:12:14 -0700355 // Returns the set of all applications that define an android:backupAgent attribute
356 private List<ApplicationInfo> allAgentApps() {
357 List<ApplicationInfo> allApps = mPackageManager.getInstalledApplications(0);
358 int N = allApps.size();
359 if (N > 0) {
360 for (int a = N-1; a >= 0; a--) {
361 ApplicationInfo app = allApps.get(a);
362 if (app.backupAgentName == null) {
363 allApps.remove(a);
364 }
365 }
366 }
367 return allApps;
368 }
369
Christopher Tate3799bc22009-05-06 16:13:56 -0700370 // Reset the given package's known backup participants. Unlike add/remove, the update
371 // action cannot be passed a null package name.
372 void updatePackageParticipantsLocked(String packageName) {
373 if (packageName == null) {
374 Log.e(TAG, "updatePackageParticipants called with null package name");
375 return;
376 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700377 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700378
379 // brute force but small code size
Christopher Tate181fafa2009-05-14 11:12:14 -0700380 List<ApplicationInfo> allApps = allAgentApps();
381 removePackageParticipantsLockedInner(packageName, allApps);
382 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700383 }
384
Christopher Tate043dadc2009-06-02 16:11:00 -0700385 // ----- Back up a set of applications via a worker thread -----
386
387 class PerformBackupThread extends Thread {
388 private static final String TAG = "PerformBackupThread";
389 int mTransport;
390 ArrayList<BackupRequest> mQueue;
391
392 public PerformBackupThread(int transportId, ArrayList<BackupRequest> queue) {
393 mTransport = transportId;
394 mQueue = queue;
395 }
396
397 @Override
398 public void run() {
399 /*
400 * 1. start up the current transport
401 * 2. for each item in the queue:
402 * 2a. bind the agent [wait for async attach]
403 * 2b. set up the files and call doBackup()
404 * 2c. unbind the agent
405 * 3. tear down the transport
406 * 4. done!
407 */
408 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
409
410 // stand up the current transport
411 IBackupTransport transport = null;
412 switch (mTransport) {
413 case BackupManager.TRANSPORT_ADB:
414 if (DEBUG) Log.v(TAG, "Initializing adb transport");
415 transport = new AdbTransport();
416 break;
417
418 case BackupManager.TRANSPORT_GOOGLE:
419 if (DEBUG) Log.v(TAG, "Initializing Google transport");
420 //!!! TODO: stand up the google backup transport here
421 transport = new GoogleTransport();
422 break;
423
424 default:
425 Log.e(TAG, "Perform backup with unknown transport " + mTransport);
426 // !!! TODO: re-enqueue the backup queue for later?
427 return;
428 }
429
430 try {
431 transport.startSession();
432 } catch (Exception e) {
433 Log.e(TAG, "Error starting backup session");
434 e.printStackTrace();
435 // !!! TODO: re-enqueue the backup queue for later?
436 return;
437 }
438
439 // The transport is up and running; now run all the backups in our queue
440 doQueuedBackups(transport);
441
442 // Finally, tear down the transport
443 try {
444 transport.endSession();
445 } catch (Exception e) {
446 Log.e(TAG, "Error ending transport");
447 e.printStackTrace();
448 }
449 }
450
451 private void doQueuedBackups(IBackupTransport transport) {
452 for (BackupRequest request : mQueue) {
453 Log.d(TAG, "starting agent for " + request);
454 // !!! TODO: need to handle the restore case?
455
456 IBackupAgent agent = null;
457 int mode = (request.fullBackup)
458 ? IApplicationThread.BACKUP_MODE_FULL
459 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
460 try {
461 synchronized(mAgentConnectLock) {
462 mConnecting = true;
463 mConnectedAgent = null;
464 if (mActivityManager.bindBackupAgent(request.appInfo, mode)) {
465 Log.d(TAG, "awaiting agent for " + request);
466
467 // success; wait for the agent to arrive
468 while (mConnecting && mConnectedAgent == null) {
469 try {
470 mAgentConnectLock.wait(10000);
471 } catch (InterruptedException e) {
472 // just retry
473 continue;
474 }
475 }
476
477 // if we timed out with no connect, abort and move on
478 if (mConnecting == true) {
479 Log.w(TAG, "Timeout waiting for agent " + request);
480 continue;
481 }
482 agent = mConnectedAgent;
483 }
484 }
485 } catch (RemoteException e) {
486 // can't happen; activity manager is local
487 } catch (SecurityException ex) {
488 // Try for the next one.
489 Log.d(TAG, "error in bind", ex);
490 }
491
492 // successful bind? run the backup for this agent
493 if (agent != null) {
494 processOneBackup(request, agent, transport);
495 }
496
497 // send the unbind even on timeout, just in case
498 try {
499 mActivityManager.unbindBackupAgent(request.appInfo);
500 } catch (RemoteException e) {
501 // can't happen
502 }
503 }
504 }
505 }
506
Christopher Tate487529a2009-04-29 14:03:25 -0700507 // ----- IBackupManager binder interface -----
508
Christopher Tatea8bf8152009-04-30 11:36:21 -0700509 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -0700510 // Record that we need a backup pass for the caller. Since multiple callers
511 // may share a uid, we need to note all candidates within that uid and schedule
512 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700513
514 Log.d(TAG, "dataChanged packageName=" + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -0700515
Christopher Tate181fafa2009-05-14 11:12:14 -0700516 HashSet<ApplicationInfo> targets = mBackupParticipants.get(Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -0700517 if (targets != null) {
518 synchronized (mQueueLock) {
519 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -0700520 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -0700521 // validate the caller-supplied package name against the known set of
522 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -0700523 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -0400524 // Add the caller to the set of pending backups. If there is
525 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -0700526 BackupRequest req = new BackupRequest(app, false);
527 mPendingBackups.put(app, req);
Joe Onorato8ad02812009-05-13 01:41:44 -0400528 // !!! TODO: write to the pending-backup journal file in case of crash
Christopher Tate487529a2009-04-29 14:03:25 -0700529 }
530 }
531
Christopher Tate181fafa2009-05-14 11:12:14 -0700532 if (DEBUG) {
533 int numKeys = mPendingBackups.size();
534 Log.d(TAG, "Scheduling backup for " + numKeys + " participants:");
535 for (BackupRequest b : mPendingBackups.values()) {
536 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
537 }
538 }
Christopher Tate487529a2009-04-29 14:03:25 -0700539 // Schedule a backup pass in a few minutes. As backup-eligible data
540 // keeps changing, continue to defer the backup pass until things
541 // settle down, to avoid extra overhead.
Christopher Tate043dadc2009-06-02 16:11:00 -0700542 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
Christopher Tate487529a2009-04-29 14:03:25 -0700543 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL);
544 }
545 }
546 }
Christopher Tate46758122009-05-06 11:22:00 -0700547
Christopher Tate043dadc2009-06-02 16:11:00 -0700548 // Schedule a backup pass for a given package. This method will schedule a
549 // full backup even for apps that do not declare an android:backupAgent, so
550 // use with care.
Christopher Tate46758122009-05-06 11:22:00 -0700551 public void scheduleFullBackup(String packageName) throws RemoteException {
Christopher Tate043dadc2009-06-02 16:11:00 -0700552 mContext.enforceCallingPermission("android.permission.BACKUP", "scheduleFullBackup");
553
554 if (DEBUG) Log.v(TAG, "Scheduling immediate full backup for " + packageName);
Christopher Tate46758122009-05-06 11:22:00 -0700555 synchronized (mQueueLock) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700556 try {
557 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
558 mPendingBackups.put(app, new BackupRequest(app, true));
559 mBackupHandler.sendEmptyMessage(MSG_RUN_FULL_BACKUP);
560 } catch (NameNotFoundException e) {
561 Log.w(TAG, "Could not find app for " + packageName + " to schedule full backup");
Christopher Tate46758122009-05-06 11:22:00 -0700562 }
563 }
564 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700565
Christopher Tate043dadc2009-06-02 16:11:00 -0700566 // Select which transport to use for the next backup operation
567 public int selectBackupTransport(int transportId) {
568 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
569
570 int prevTransport = mTransportId;
571 mTransportId = transportId;
572 return prevTransport;
573 }
574
575 // Callback: a requested backup agent has been instantiated. This should only
576 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700577 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700578 synchronized(mAgentConnectLock) {
579 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
580 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
581 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
582 mConnectedAgent = agent;
583 mConnecting = false;
584 } else {
585 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
586 + " claiming agent connected");
587 }
588 mAgentConnectLock.notifyAll();
589 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700590 }
591
592 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
593 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -0700594 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700595 public void agentDisconnected(String packageName) {
596 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -0700597 synchronized(mAgentConnectLock) {
598 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
599 mConnectedAgent = null;
600 mConnecting = false;
601 } else {
602 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
603 + " claiming agent disconnected");
604 }
605 mAgentConnectLock.notifyAll();
606 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700607 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700608
Christopher Tate043dadc2009-06-02 16:11:00 -0700609
610
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700611 @Override
612 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
613 synchronized (mQueueLock) {
614 int N = mBackupParticipants.size();
615 pw.println("Participants:");
616 for (int i=0; i<N; i++) {
617 int uid = mBackupParticipants.keyAt(i);
618 pw.print(" uid: ");
619 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700620 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
621 for (ApplicationInfo app: participants) {
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700622 pw.print(" ");
Christopher Tate181fafa2009-05-14 11:12:14 -0700623 pw.println(app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700624 }
625 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700626 pw.println("Pending:");
627 Iterator<BackupRequest> br = mPendingBackups.values().iterator();
628 while (br.hasNext()) {
629 pw.print(" ");
630 pw.println(br);
631 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700632 }
633 }
Christopher Tate487529a2009-04-29 14:03:25 -0700634}