blob: cbec1b4b86cb8d2ee66397bcbc247655c7d1640b [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 Tate7b881282009-06-07 13:52:37 -070028import android.content.pm.PackageInfo;
Christopher Tate487529a2009-04-29 14:03:25 -070029import android.content.pm.PackageManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070030import android.content.pm.PackageManager.NameNotFoundException;
Christopher Tate3799bc22009-05-06 16:13:56 -070031import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070032import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070033import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070034import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070035import android.os.Handler;
36import android.os.IBinder;
37import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070038import android.os.ParcelFileDescriptor;
Christopher Tate043dadc2009-06-02 16:11:00 -070039import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070040import android.os.RemoteException;
41import android.util.Log;
42import android.util.SparseArray;
43
44import android.backup.IBackupManager;
Christopher Tate8c850b72009-06-07 19:33:20 -070045import android.backup.IRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -070046import android.backup.BackupManager;
47
48import com.android.internal.backup.AdbTransport;
49import com.android.internal.backup.GoogleTransport;
50import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070051
Christopher Tate22b87872009-05-04 16:41:53 -070052import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070053import java.io.FileDescriptor;
Christopher Tate22b87872009-05-04 16:41:53 -070054import java.io.FileNotFoundException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070055import java.io.PrintWriter;
Christopher Tate487529a2009-04-29 14:03:25 -070056import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040057import java.util.ArrayList;
58import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070059import java.util.HashSet;
Christopher Tate181fafa2009-05-14 11:12:14 -070060import java.util.Iterator;
Christopher Tate487529a2009-04-29 14:03:25 -070061import java.util.List;
62
63class BackupManagerService extends IBackupManager.Stub {
64 private static final String TAG = "BackupManagerService";
65 private static final boolean DEBUG = true;
66
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070067 private static final long COLLECTION_INTERVAL = 1000;
68 //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
Christopher Tate487529a2009-04-29 14:03:25 -070069
70 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070071 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate487529a2009-04-29 14:03:25 -070072
73 private Context mContext;
74 private PackageManager mPackageManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070075 private final IActivityManager mActivityManager;
Christopher Tate487529a2009-04-29 14:03:25 -070076 private final BackupHandler mBackupHandler = new BackupHandler();
77 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate181fafa2009-05-14 11:12:14 -070078 private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
79 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -070080 // set of backup services that have pending changes
Christopher Tate46758122009-05-06 11:22:00 -070081 private class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -070082 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -070083 public boolean fullBackup;
84
Christopher Tate181fafa2009-05-14 11:12:14 -070085 BackupRequest(ApplicationInfo app, boolean isFull) {
86 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -070087 fullBackup = isFull;
88 }
Christopher Tate181fafa2009-05-14 11:12:14 -070089
90 public String toString() {
91 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
92 }
Christopher Tate46758122009-05-06 11:22:00 -070093 }
Joe Onorato8ad02812009-05-13 01:41:44 -040094 // Backups that we haven't started yet.
Christopher Tate181fafa2009-05-14 11:12:14 -070095 private HashMap<ApplicationInfo,BackupRequest> mPendingBackups
96 = new HashMap<ApplicationInfo,BackupRequest>();
Joe Onorato8ad02812009-05-13 01:41:44 -040097 // Backups that we have started. These are separate to prevent starvation
98 // if an app keeps re-enqueuing itself.
99 private ArrayList<BackupRequest> mBackupQueue;
Christopher Tate487529a2009-04-29 14:03:25 -0700100 private final Object mQueueLock = new Object();
101
Christopher Tate043dadc2009-06-02 16:11:00 -0700102 // The thread performing the sequence of queued backups binds to each app's agent
103 // in succession. Bind notifications are asynchronously delivered through the
104 // Activity Manager; use this lock object to signal when a requested binding has
105 // completed.
106 private final Object mAgentConnectLock = new Object();
107 private IBackupAgent mConnectedAgent;
108 private volatile boolean mConnecting;
109
110 private int mTransportId;
111
Christopher Tate22b87872009-05-04 16:41:53 -0700112 private File mStateDir;
Christopher Tatef4172472009-05-05 15:50:03 -0700113 private File mDataDir;
Christopher Tate487529a2009-04-29 14:03:25 -0700114
Christopher Tate487529a2009-04-29 14:03:25 -0700115 public BackupManagerService(Context context) {
116 mContext = context;
117 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700118 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700119
Christopher Tate22b87872009-05-04 16:41:53 -0700120 // Set up our bookkeeping
Christopher Tatef4172472009-05-05 15:50:03 -0700121 mStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tate22b87872009-05-04 16:41:53 -0700122 mStateDir.mkdirs();
Christopher Tatef4172472009-05-05 15:50:03 -0700123 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate043dadc2009-06-02 16:11:00 -0700124 mTransportId = BackupManager.TRANSPORT_GOOGLE;
Christopher Tate22b87872009-05-04 16:41:53 -0700125
Christopher Tate3799bc22009-05-06 16:13:56 -0700126 // Build our mapping of uid to backup client services
127 synchronized (mBackupParticipants) {
128 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700129 }
130
Christopher Tate3799bc22009-05-06 16:13:56 -0700131 // Register for broadcasts about package install, etc., so we can
132 // update the provider list.
133 IntentFilter filter = new IntentFilter();
134 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
135 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
136 filter.addDataScheme("package");
137 mContext.registerReceiver(mBroadcastReceiver, filter);
138 }
139
140 // ----- Track installation/removal of packages -----
141 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
142 public void onReceive(Context context, Intent intent) {
143 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
144
145 Uri uri = intent.getData();
146 if (uri == null) {
147 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700148 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700149 String pkgName = uri.getSchemeSpecificPart();
150 if (pkgName == null) {
151 return;
152 }
153
154 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 Tate7b881282009-06-07 13:52:37 -0700214 // Look up the package info & signatures. This is first so that if it
215 // throws an exception, there's no file setup yet that would need to
216 // be unraveled.
217 PackageInfo packInfo = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
218
Christopher Tate043dadc2009-06-02 16:11:00 -0700219 // !!! TODO: get the state file dir from the transport
220 File savedStateName = new File(mStateDir, packageName);
221 File backupDataName = new File(mDataDir, packageName + ".data");
222 File newStateName = new File(mStateDir, packageName + ".new");
Joe Onorato8ad02812009-05-13 01:41:44 -0400223
224 // In a full backup, we pass a null ParcelFileDescriptor as
225 // the saved-state "file"
226 ParcelFileDescriptor savedState = (request.fullBackup) ? null
227 : ParcelFileDescriptor.open(savedStateName,
228 ParcelFileDescriptor.MODE_READ_ONLY |
229 ParcelFileDescriptor.MODE_CREATE);
230
231 backupDataName.delete();
232 ParcelFileDescriptor backupData =
233 ParcelFileDescriptor.open(backupDataName,
234 ParcelFileDescriptor.MODE_READ_WRITE |
235 ParcelFileDescriptor.MODE_CREATE);
236
237 newStateName.delete();
238 ParcelFileDescriptor newState =
239 ParcelFileDescriptor.open(newStateName,
240 ParcelFileDescriptor.MODE_READ_WRITE |
241 ParcelFileDescriptor.MODE_CREATE);
242
243 // Run the target's backup pass
Christopher Tate043dadc2009-06-02 16:11:00 -0700244 boolean success = false;
Joe Onorato8ad02812009-05-13 01:41:44 -0400245 try {
Christopher Tate043dadc2009-06-02 16:11:00 -0700246 agent.doBackup(savedState, backupData, newState);
247 success = true;
Joe Onorato8ad02812009-05-13 01:41:44 -0400248 } finally {
249 if (savedState != null) {
250 savedState.close();
251 }
252 backupData.close();
253 newState.close();
254 }
255
Christopher Tate043dadc2009-06-02 16:11:00 -0700256 // Now propagate the newly-backed-up data to the transport
257 if (success) {
Christopher Tate1885b372009-06-04 15:00:33 -0700258 if (DEBUG) Log.v(TAG, "doBackup() success; calling transport");
Christopher Tate043dadc2009-06-02 16:11:00 -0700259 backupData =
260 ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY);
Christopher Tate7b881282009-06-07 13:52:37 -0700261 int error = transport.performBackup(packInfo, backupData);
Christopher Tate043dadc2009-06-02 16:11:00 -0700262
263 // !!! TODO: After successful transport, delete the now-stale data
264 // and juggle the files so that next time the new state is passed
265 //backupDataName.delete();
266 newStateName.renameTo(savedStateName);
267 }
Christopher Tate7b881282009-06-07 13:52:37 -0700268 } catch (NameNotFoundException e) {
269 Log.e(TAG, "Package not found on backup: " + packageName);
Joe Onorato8ad02812009-05-13 01:41:44 -0400270 } catch (FileNotFoundException fnf) {
Christopher Tate7b881282009-06-07 13:52:37 -0700271 Log.w(TAG, "File not found on backup: ");
Joe Onorato8ad02812009-05-13 01:41:44 -0400272 fnf.printStackTrace();
273 } catch (RemoteException e) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700274 Log.d(TAG, "Remote target " + request.appInfo.packageName + " threw during backup:");
Joe Onorato8ad02812009-05-13 01:41:44 -0400275 e.printStackTrace();
276 } catch (Exception e) {
277 Log.w(TAG, "Final exception guard in backup: ");
278 e.printStackTrace();
279 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400280 }
281
Christopher Tate181fafa2009-05-14 11:12:14 -0700282 // Add the backup agents in the given package to our set of known backup participants.
283 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700284 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700285 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700286 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700287 List<ApplicationInfo> targetApps = allAgentApps();
288 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700289 }
290
Christopher Tate181fafa2009-05-14 11:12:14 -0700291 private void addPackageParticipantsLockedInner(String packageName,
292 List<ApplicationInfo> targetApps) {
293 if (DEBUG) {
294 Log.v(TAG, "Adding " + targetApps.size() + " backup participants:");
295 for (ApplicationInfo a : targetApps) {
296 Log.v(TAG, " " + a + " agent=" + a.backupAgentName);
297 }
298 }
299
300 for (ApplicationInfo app : targetApps) {
301 if (packageName == null || app.packageName.equals(packageName)) {
302 int uid = app.uid;
303 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700304 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700305 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700306 mBackupParticipants.put(uid, set);
307 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700308 set.add(app);
Christopher Tate3799bc22009-05-06 16:13:56 -0700309 }
Christopher Tate487529a2009-04-29 14:03:25 -0700310 }
311 }
312
Christopher Tate3799bc22009-05-06 16:13:56 -0700313 // Remove the given package's backup services from our known active set. If
314 // 'packageName' is null, *all* backup services will be removed.
315 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700316 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700317 List<ApplicationInfo> allApps = null;
318 if (packageName != null) {
319 allApps = new ArrayList<ApplicationInfo>();
320 try {
321 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
322 allApps.add(app);
323 } catch (Exception e) {
324 // just skip it
325 }
326 } else {
327 // all apps with agents
328 allApps = allAgentApps();
329 }
330 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700331 }
332
Joe Onorato8ad02812009-05-13 01:41:44 -0400333 private void removePackageParticipantsLockedInner(String packageName,
Christopher Tate181fafa2009-05-14 11:12:14 -0700334 List<ApplicationInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700335 if (DEBUG) {
336 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
337 + ") removing " + agents.size() + " entries");
338 for (ApplicationInfo a : agents) {
339 Log.v(TAG, " - " + a);
340 }
341 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700342 for (ApplicationInfo app : agents) {
343 if (packageName == null || app.packageName.equals(packageName)) {
344 int uid = app.uid;
345 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700346 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700347 // Find the existing entry with the same package name, and remove it.
348 // We can't just remove(app) because the instances are different.
349 for (ApplicationInfo entry: set) {
350 if (entry.packageName.equals(app.packageName)) {
351 set.remove(entry);
352 break;
353 }
354 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700355 if (set.size() == 0) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700356 mBackupParticipants.delete(uid); }
Christopher Tate3799bc22009-05-06 16:13:56 -0700357 }
358 }
359 }
360 }
361
Christopher Tate181fafa2009-05-14 11:12:14 -0700362 // Returns the set of all applications that define an android:backupAgent attribute
363 private List<ApplicationInfo> allAgentApps() {
364 List<ApplicationInfo> allApps = mPackageManager.getInstalledApplications(0);
365 int N = allApps.size();
366 if (N > 0) {
367 for (int a = N-1; a >= 0; a--) {
368 ApplicationInfo app = allApps.get(a);
369 if (app.backupAgentName == null) {
370 allApps.remove(a);
371 }
372 }
373 }
374 return allApps;
375 }
376
Christopher Tate3799bc22009-05-06 16:13:56 -0700377 // Reset the given package's known backup participants. Unlike add/remove, the update
378 // action cannot be passed a null package name.
379 void updatePackageParticipantsLocked(String packageName) {
380 if (packageName == null) {
381 Log.e(TAG, "updatePackageParticipants called with null package name");
382 return;
383 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700384 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700385
386 // brute force but small code size
Christopher Tate181fafa2009-05-14 11:12:14 -0700387 List<ApplicationInfo> allApps = allAgentApps();
388 removePackageParticipantsLockedInner(packageName, allApps);
389 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700390 }
391
Christopher Tate8c850b72009-06-07 19:33:20 -0700392 // Instantiate the given transport
393 private IBackupTransport createTransport(int transportID) {
394 IBackupTransport transport = null;
395 switch (transportID) {
396 case BackupManager.TRANSPORT_ADB:
397 if (DEBUG) Log.v(TAG, "Initializing adb transport");
398 transport = new AdbTransport();
399 break;
400
401 case BackupManager.TRANSPORT_GOOGLE:
402 if (DEBUG) Log.v(TAG, "Initializing Google transport");
403 //!!! TODO: stand up the google backup transport for real here
404 transport = new GoogleTransport();
405 break;
406
407 default:
408 Log.e(TAG, "creating unknown transport " + transportID);
409 }
410 return transport;
411 }
412
Christopher Tate043dadc2009-06-02 16:11:00 -0700413 // ----- Back up a set of applications via a worker thread -----
414
415 class PerformBackupThread extends Thread {
416 private static final String TAG = "PerformBackupThread";
417 int mTransport;
418 ArrayList<BackupRequest> mQueue;
419
420 public PerformBackupThread(int transportId, ArrayList<BackupRequest> queue) {
421 mTransport = transportId;
422 mQueue = queue;
423 }
424
425 @Override
426 public void run() {
427 /*
428 * 1. start up the current transport
429 * 2. for each item in the queue:
430 * 2a. bind the agent [wait for async attach]
431 * 2b. set up the files and call doBackup()
432 * 2c. unbind the agent
433 * 3. tear down the transport
434 * 4. done!
435 */
436 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
437
438 // stand up the current transport
Christopher Tate8c850b72009-06-07 19:33:20 -0700439 IBackupTransport transport = createTransport(mTransport);
440 if (transport == null) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700441 return;
442 }
443
444 // The transport is up and running; now run all the backups in our queue
445 doQueuedBackups(transport);
446
447 // Finally, tear down the transport
448 try {
449 transport.endSession();
450 } catch (Exception e) {
451 Log.e(TAG, "Error ending transport");
452 e.printStackTrace();
453 }
454 }
455
456 private void doQueuedBackups(IBackupTransport transport) {
457 for (BackupRequest request : mQueue) {
458 Log.d(TAG, "starting agent for " + request);
459 // !!! TODO: need to handle the restore case?
460
461 IBackupAgent agent = null;
462 int mode = (request.fullBackup)
463 ? IApplicationThread.BACKUP_MODE_FULL
464 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
465 try {
466 synchronized(mAgentConnectLock) {
467 mConnecting = true;
468 mConnectedAgent = null;
469 if (mActivityManager.bindBackupAgent(request.appInfo, mode)) {
470 Log.d(TAG, "awaiting agent for " + request);
471
472 // success; wait for the agent to arrive
473 while (mConnecting && mConnectedAgent == null) {
474 try {
475 mAgentConnectLock.wait(10000);
476 } catch (InterruptedException e) {
477 // just retry
478 continue;
479 }
480 }
481
482 // if we timed out with no connect, abort and move on
483 if (mConnecting == true) {
484 Log.w(TAG, "Timeout waiting for agent " + request);
485 continue;
486 }
487 agent = mConnectedAgent;
488 }
489 }
490 } catch (RemoteException e) {
491 // can't happen; activity manager is local
492 } catch (SecurityException ex) {
493 // Try for the next one.
494 Log.d(TAG, "error in bind", ex);
495 }
496
497 // successful bind? run the backup for this agent
498 if (agent != null) {
499 processOneBackup(request, agent, transport);
500 }
501
502 // send the unbind even on timeout, just in case
503 try {
504 mActivityManager.unbindBackupAgent(request.appInfo);
505 } catch (RemoteException e) {
506 // can't happen
507 }
508 }
509 }
510 }
511
Christopher Tate487529a2009-04-29 14:03:25 -0700512 // ----- IBackupManager binder interface -----
513
Christopher Tatea8bf8152009-04-30 11:36:21 -0700514 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -0700515 // Record that we need a backup pass for the caller. Since multiple callers
516 // may share a uid, we need to note all candidates within that uid and schedule
517 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700518
519 Log.d(TAG, "dataChanged packageName=" + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -0700520
Christopher Tate181fafa2009-05-14 11:12:14 -0700521 HashSet<ApplicationInfo> targets = mBackupParticipants.get(Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -0700522 if (targets != null) {
523 synchronized (mQueueLock) {
524 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -0700525 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -0700526 // validate the caller-supplied package name against the known set of
527 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -0700528 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -0400529 // Add the caller to the set of pending backups. If there is
530 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -0700531 BackupRequest req = new BackupRequest(app, false);
532 mPendingBackups.put(app, req);
Joe Onorato8ad02812009-05-13 01:41:44 -0400533 // !!! TODO: write to the pending-backup journal file in case of crash
Christopher Tate487529a2009-04-29 14:03:25 -0700534 }
535 }
536
Christopher Tate181fafa2009-05-14 11:12:14 -0700537 if (DEBUG) {
538 int numKeys = mPendingBackups.size();
539 Log.d(TAG, "Scheduling backup for " + numKeys + " participants:");
540 for (BackupRequest b : mPendingBackups.values()) {
541 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
542 }
543 }
Christopher Tate487529a2009-04-29 14:03:25 -0700544 // Schedule a backup pass in a few minutes. As backup-eligible data
545 // keeps changing, continue to defer the backup pass until things
546 // settle down, to avoid extra overhead.
Christopher Tate043dadc2009-06-02 16:11:00 -0700547 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
Christopher Tate487529a2009-04-29 14:03:25 -0700548 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL);
549 }
550 }
551 }
Christopher Tate46758122009-05-06 11:22:00 -0700552
Christopher Tate043dadc2009-06-02 16:11:00 -0700553 // Schedule a backup pass for a given package. This method will schedule a
554 // full backup even for apps that do not declare an android:backupAgent, so
555 // use with care.
Christopher Tate46758122009-05-06 11:22:00 -0700556 public void scheduleFullBackup(String packageName) throws RemoteException {
Christopher Tate043dadc2009-06-02 16:11:00 -0700557 mContext.enforceCallingPermission("android.permission.BACKUP", "scheduleFullBackup");
558
559 if (DEBUG) Log.v(TAG, "Scheduling immediate full backup for " + packageName);
Christopher Tate46758122009-05-06 11:22:00 -0700560 synchronized (mQueueLock) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700561 try {
562 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
563 mPendingBackups.put(app, new BackupRequest(app, true));
564 mBackupHandler.sendEmptyMessage(MSG_RUN_FULL_BACKUP);
565 } catch (NameNotFoundException e) {
566 Log.w(TAG, "Could not find app for " + packageName + " to schedule full backup");
Christopher Tate46758122009-05-06 11:22:00 -0700567 }
568 }
569 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700570
Christopher Tate043dadc2009-06-02 16:11:00 -0700571 // Select which transport to use for the next backup operation
572 public int selectBackupTransport(int transportId) {
573 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
574
575 int prevTransport = mTransportId;
576 mTransportId = transportId;
577 return prevTransport;
578 }
579
580 // Callback: a requested backup agent has been instantiated. This should only
581 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700582 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700583 synchronized(mAgentConnectLock) {
584 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
585 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
586 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
587 mConnectedAgent = agent;
588 mConnecting = false;
589 } else {
590 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
591 + " claiming agent connected");
592 }
593 mAgentConnectLock.notifyAll();
594 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700595 }
596
597 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
598 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -0700599 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700600 public void agentDisconnected(String packageName) {
601 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -0700602 synchronized(mAgentConnectLock) {
603 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
604 mConnectedAgent = null;
605 mConnecting = false;
606 } else {
607 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
608 + " claiming agent disconnected");
609 }
610 mAgentConnectLock.notifyAll();
611 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700612 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700613
Christopher Tate8c850b72009-06-07 19:33:20 -0700614 // Hand off a restore session
615 public IRestoreSession beginRestoreSession(int transportID) {
616 mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession");
617 return null;
618 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700619
620
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700621 @Override
622 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
623 synchronized (mQueueLock) {
624 int N = mBackupParticipants.size();
625 pw.println("Participants:");
626 for (int i=0; i<N; i++) {
627 int uid = mBackupParticipants.keyAt(i);
628 pw.print(" uid: ");
629 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700630 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
631 for (ApplicationInfo app: participants) {
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700632 pw.print(" ");
Christopher Tate181fafa2009-05-14 11:12:14 -0700633 pw.println(app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700634 }
635 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700636 pw.println("Pending:");
637 Iterator<BackupRequest> br = mPendingBackups.values().iterator();
638 while (br.hasNext()) {
639 pw.print(" ");
640 pw.println(br);
641 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700642 }
643 }
Christopher Tate487529a2009-04-29 14:03:25 -0700644}