blob: 47a6edeb0bec8a7aa6130556ec80b1ffe58b695f [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 Tatec7b31e32009-06-10 15:49:30 -070028import android.content.pm.IPackageDataObserver;
Christopher Tate7b881282009-06-07 13:52:37 -070029import android.content.pm.PackageInfo;
Christopher Tate487529a2009-04-29 14:03:25 -070030import android.content.pm.PackageManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070031import android.content.pm.PackageManager.NameNotFoundException;
Christopher Tate3799bc22009-05-06 16:13:56 -070032import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070033import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070034import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070035import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070036import android.os.Handler;
37import android.os.IBinder;
38import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070039import android.os.ParcelFileDescriptor;
Christopher Tate043dadc2009-06-02 16:11:00 -070040import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070041import android.os.RemoteException;
42import android.util.Log;
43import android.util.SparseArray;
44
45import android.backup.IBackupManager;
Christopher Tate8c850b72009-06-07 19:33:20 -070046import android.backup.IRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -070047import android.backup.BackupManager;
Christopher Tate9b3905c2009-06-08 15:24:01 -070048import android.backup.RestoreSet;
Christopher Tate043dadc2009-06-02 16:11:00 -070049
Christopher Tate9bbc21a2009-06-10 20:23:25 -070050import com.android.internal.backup.LocalTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -070051import com.android.internal.backup.GoogleTransport;
52import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070053
Christopher Tatecde87f42009-06-12 12:55:53 -070054import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070055import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070056import java.io.FileDescriptor;
Christopher Tate22b87872009-05-04 16:41:53 -070057import java.io.FileNotFoundException;
Christopher Tatec7b31e32009-06-10 15:49:30 -070058import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070059import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070060import java.io.RandomAccessFile;
Christopher Tate487529a2009-04-29 14:03:25 -070061import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040062import java.util.ArrayList;
63import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070064import java.util.HashSet;
Christopher Tate181fafa2009-05-14 11:12:14 -070065import java.util.Iterator;
Christopher Tate487529a2009-04-29 14:03:25 -070066import java.util.List;
67
68class BackupManagerService extends IBackupManager.Stub {
69 private static final String TAG = "BackupManagerService";
70 private static final boolean DEBUG = true;
71
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070072 private static final long COLLECTION_INTERVAL = 1000;
73 //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
Christopher Tate487529a2009-04-29 14:03:25 -070074
75 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070076 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070077 private static final int MSG_RUN_RESTORE = 3;
Christopher Tatec7b31e32009-06-10 15:49:30 -070078
79 // Timeout interval for deciding that a bind or clear-data has taken too long
80 static final long TIMEOUT_INTERVAL = 10 * 1000;
81
Christopher Tate487529a2009-04-29 14:03:25 -070082 private Context mContext;
83 private PackageManager mPackageManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070084 private final IActivityManager mActivityManager;
Christopher Tate487529a2009-04-29 14:03:25 -070085 private final BackupHandler mBackupHandler = new BackupHandler();
86 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate181fafa2009-05-14 11:12:14 -070087 private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
88 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -070089 // set of backup services that have pending changes
Christopher Tate46758122009-05-06 11:22:00 -070090 private class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -070091 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -070092 public boolean fullBackup;
93
Christopher Tate181fafa2009-05-14 11:12:14 -070094 BackupRequest(ApplicationInfo app, boolean isFull) {
95 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -070096 fullBackup = isFull;
97 }
Christopher Tate181fafa2009-05-14 11:12:14 -070098
99 public String toString() {
100 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
101 }
Christopher Tate46758122009-05-06 11:22:00 -0700102 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400103 // Backups that we haven't started yet.
Christopher Tate181fafa2009-05-14 11:12:14 -0700104 private HashMap<ApplicationInfo,BackupRequest> mPendingBackups
105 = new HashMap<ApplicationInfo,BackupRequest>();
Joe Onorato8ad02812009-05-13 01:41:44 -0400106 // Backups that we have started. These are separate to prevent starvation
107 // if an app keeps re-enqueuing itself.
108 private ArrayList<BackupRequest> mBackupQueue;
Christopher Tate487529a2009-04-29 14:03:25 -0700109 private final Object mQueueLock = new Object();
110
Christopher Tate043dadc2009-06-02 16:11:00 -0700111 // The thread performing the sequence of queued backups binds to each app's agent
112 // in succession. Bind notifications are asynchronously delivered through the
113 // Activity Manager; use this lock object to signal when a requested binding has
114 // completed.
115 private final Object mAgentConnectLock = new Object();
116 private IBackupAgent mConnectedAgent;
117 private volatile boolean mConnecting;
118
Christopher Tatec7b31e32009-06-10 15:49:30 -0700119 // A similar synchronicity mechanism around clearing apps' data for restore
120 private final Object mClearDataLock = new Object();
121 private volatile boolean mClearingData;
122
Christopher Tate043dadc2009-06-02 16:11:00 -0700123 private int mTransportId;
124
Christopher Tate22b87872009-05-04 16:41:53 -0700125 private File mStateDir;
Christopher Tatef4172472009-05-05 15:50:03 -0700126 private File mDataDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700127 private File mJournalDir;
128 private File mJournal;
129 private RandomAccessFile mJournalStream;
Christopher Tate487529a2009-04-29 14:03:25 -0700130
Christopher Tate487529a2009-04-29 14:03:25 -0700131 public BackupManagerService(Context context) {
132 mContext = context;
133 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700134 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700135
Christopher Tate22b87872009-05-04 16:41:53 -0700136 // Set up our bookkeeping
Christopher Tatef4172472009-05-05 15:50:03 -0700137 mStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tate22b87872009-05-04 16:41:53 -0700138 mStateDir.mkdirs();
Christopher Tatef4172472009-05-05 15:50:03 -0700139 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700140
Christopher Tatecde87f42009-06-12 12:55:53 -0700141 // Set up the backup-request journaling
142 mJournalDir = new File(mStateDir, "pending");
143 mJournalDir.mkdirs();
144 makeJournalLocked(); // okay because no other threads are running yet
145
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700146 //!!! TODO: default to cloud transport, not local
147 mTransportId = BackupManager.TRANSPORT_LOCAL;
Christopher Tate22b87872009-05-04 16:41:53 -0700148
Christopher Tate3799bc22009-05-06 16:13:56 -0700149 // Build our mapping of uid to backup client services
150 synchronized (mBackupParticipants) {
151 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700152 }
153
Christopher Tatecde87f42009-06-12 12:55:53 -0700154 // Now that we know about valid backup participants, parse any
155 // leftover journal files and schedule a new backup pass
156 parseLeftoverJournals();
157
Christopher Tate3799bc22009-05-06 16:13:56 -0700158 // Register for broadcasts about package install, etc., so we can
159 // update the provider list.
160 IntentFilter filter = new IntentFilter();
161 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
162 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
163 filter.addDataScheme("package");
164 mContext.registerReceiver(mBroadcastReceiver, filter);
165 }
166
Christopher Tatecde87f42009-06-12 12:55:53 -0700167 private void makeJournalLocked() {
168 try {
169 mJournal = File.createTempFile("journal", null, mJournalDir);
170 mJournalStream = new RandomAccessFile(mJournal, "rwd");
171 } catch (IOException e) {
172 Log.e(TAG, "Unable to write backup journals");
173 mJournal = null;
174 mJournalStream = null;
175 }
176 }
177
178 private void parseLeftoverJournals() {
179 if (mJournal != null) {
180 File[] allJournals = mJournalDir.listFiles();
181 for (File f : allJournals) {
182 if (f.compareTo(mJournal) != 0) {
183 // This isn't the current journal, so it must be a leftover. Read
184 // out the package names mentioned there and schedule them for
185 // backup.
186 try {
187 Log.i(TAG, "Found stale backup journal, scheduling:");
188 RandomAccessFile in = new RandomAccessFile(f, "r");
189 while (true) {
190 String packageName = in.readUTF();
191 Log.i(TAG, " + " + packageName);
192 dataChanged(packageName);
193 }
194 } catch (EOFException e) {
195 // no more data; we're done
196 } catch (Exception e) {
197 // can't read it or other error; just skip it
198 } finally {
199 // close/delete the file
200 f.delete();
201 }
202 }
203 }
204 }
205 }
206
Christopher Tate3799bc22009-05-06 16:13:56 -0700207 // ----- Track installation/removal of packages -----
208 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
209 public void onReceive(Context context, Intent intent) {
210 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
211
212 Uri uri = intent.getData();
213 if (uri == null) {
214 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700215 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700216 String pkgName = uri.getSchemeSpecificPart();
217 if (pkgName == null) {
218 return;
219 }
220
221 String action = intent.getAction();
222 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
223 synchronized (mBackupParticipants) {
224 Bundle extras = intent.getExtras();
225 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
226 // The package was just upgraded
227 updatePackageParticipantsLocked(pkgName);
228 } else {
229 // The package was just added
230 addPackageParticipantsLocked(pkgName);
231 }
232 }
233 }
234 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
235 Bundle extras = intent.getExtras();
236 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
237 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
238 } else {
239 synchronized (mBackupParticipants) {
240 removePackageParticipantsLocked(pkgName);
241 }
242 }
243 }
244 }
245 };
246
Joe Onorato8ad02812009-05-13 01:41:44 -0400247 // ----- Run the actual backup process asynchronously -----
248
Christopher Tate181fafa2009-05-14 11:12:14 -0700249 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400250 public void handleMessage(Message msg) {
251
252 switch (msg.what) {
253 case MSG_RUN_BACKUP:
254 // snapshot the pending-backup set and work on that
Christopher Tatecde87f42009-06-12 12:55:53 -0700255 File oldJournal = mJournal;
256 RandomAccessFile oldJournalStream = mJournalStream;
Joe Onorato8ad02812009-05-13 01:41:44 -0400257 synchronized (mQueueLock) {
Joe Onoratod2110db2009-05-19 13:41:21 -0700258 if (mBackupQueue == null) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700259 mBackupQueue = new ArrayList<BackupRequest>();
Joe Onoratod2110db2009-05-19 13:41:21 -0700260 for (BackupRequest b: mPendingBackups.values()) {
261 mBackupQueue.add(b);
262 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700263 mPendingBackups = new HashMap<ApplicationInfo,BackupRequest>();
Joe Onorato8ad02812009-05-13 01:41:44 -0400264 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400265 // !!! TODO: start a new backup-queue journal file too
Christopher Tatecde87f42009-06-12 12:55:53 -0700266 if (mJournalStream != null) {
267 try {
268 mJournalStream.close();
269 } catch (IOException e) {
270 // don't need to do anything
271 }
272 makeJournalLocked();
273 }
274
275 // At this point, we have started a new journal file, and the old
276 // file identity is being passed to the backup processing thread.
277 // When it completes successfully, that old journal file will be
278 // deleted. If we crash prior to that, the old journal is parsed
279 // at next boot and the journaled requests fulfilled.
Joe Onorato8ad02812009-05-13 01:41:44 -0400280 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700281 (new PerformBackupThread(mTransportId, mBackupQueue, oldJournal)).run();
Christopher Tate043dadc2009-06-02 16:11:00 -0700282 break;
283
284 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400285 break;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700286
287 case MSG_RUN_RESTORE:
288 {
289 int token = msg.arg1;
290 IBackupTransport transport = (IBackupTransport)msg.obj;
291 (new PerformRestoreThread(transport, token)).run();
292 break;
293 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400294 }
295 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400296 }
297
Christopher Tate181fafa2009-05-14 11:12:14 -0700298 // Add the backup agents in the given package to our set of known backup participants.
299 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700300 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700301 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700302 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700303 List<ApplicationInfo> targetApps = allAgentApps();
304 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700305 }
306
Christopher Tate181fafa2009-05-14 11:12:14 -0700307 private void addPackageParticipantsLockedInner(String packageName,
308 List<ApplicationInfo> targetApps) {
309 if (DEBUG) {
310 Log.v(TAG, "Adding " + targetApps.size() + " backup participants:");
311 for (ApplicationInfo a : targetApps) {
312 Log.v(TAG, " " + a + " agent=" + a.backupAgentName);
313 }
314 }
315
316 for (ApplicationInfo app : targetApps) {
317 if (packageName == null || app.packageName.equals(packageName)) {
318 int uid = app.uid;
319 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700320 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700321 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700322 mBackupParticipants.put(uid, set);
323 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700324 set.add(app);
Christopher Tate3799bc22009-05-06 16:13:56 -0700325 }
Christopher Tate487529a2009-04-29 14:03:25 -0700326 }
327 }
328
Christopher Tate3799bc22009-05-06 16:13:56 -0700329 // Remove the given package's backup services from our known active set. If
330 // 'packageName' is null, *all* backup services will be removed.
331 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700332 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -0700333 List<ApplicationInfo> allApps = null;
334 if (packageName != null) {
335 allApps = new ArrayList<ApplicationInfo>();
336 try {
337 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
338 allApps.add(app);
339 } catch (Exception e) {
340 // just skip it
341 }
342 } else {
343 // all apps with agents
344 allApps = allAgentApps();
345 }
346 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700347 }
348
Joe Onorato8ad02812009-05-13 01:41:44 -0400349 private void removePackageParticipantsLockedInner(String packageName,
Christopher Tate181fafa2009-05-14 11:12:14 -0700350 List<ApplicationInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700351 if (DEBUG) {
352 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
353 + ") removing " + agents.size() + " entries");
354 for (ApplicationInfo a : agents) {
355 Log.v(TAG, " - " + a);
356 }
357 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700358 for (ApplicationInfo app : agents) {
359 if (packageName == null || app.packageName.equals(packageName)) {
360 int uid = app.uid;
361 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700362 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700363 // Find the existing entry with the same package name, and remove it.
364 // We can't just remove(app) because the instances are different.
365 for (ApplicationInfo entry: set) {
366 if (entry.packageName.equals(app.packageName)) {
367 set.remove(entry);
368 break;
369 }
370 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700371 if (set.size() == 0) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700372 mBackupParticipants.delete(uid); }
Christopher Tate3799bc22009-05-06 16:13:56 -0700373 }
374 }
375 }
376 }
377
Christopher Tate181fafa2009-05-14 11:12:14 -0700378 // Returns the set of all applications that define an android:backupAgent attribute
379 private List<ApplicationInfo> allAgentApps() {
380 List<ApplicationInfo> allApps = mPackageManager.getInstalledApplications(0);
381 int N = allApps.size();
382 if (N > 0) {
383 for (int a = N-1; a >= 0; a--) {
384 ApplicationInfo app = allApps.get(a);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700385 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
386 || app.backupAgentName == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700387 allApps.remove(a);
388 }
389 }
390 }
391 return allApps;
392 }
393
Christopher Tate3799bc22009-05-06 16:13:56 -0700394 // Reset the given package's known backup participants. Unlike add/remove, the update
395 // action cannot be passed a null package name.
396 void updatePackageParticipantsLocked(String packageName) {
397 if (packageName == null) {
398 Log.e(TAG, "updatePackageParticipants called with null package name");
399 return;
400 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700401 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700402
403 // brute force but small code size
Christopher Tate181fafa2009-05-14 11:12:14 -0700404 List<ApplicationInfo> allApps = allAgentApps();
405 removePackageParticipantsLockedInner(packageName, allApps);
406 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700407 }
408
Christopher Tate8c850b72009-06-07 19:33:20 -0700409 // Instantiate the given transport
410 private IBackupTransport createTransport(int transportID) {
411 IBackupTransport transport = null;
412 switch (transportID) {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700413 case BackupManager.TRANSPORT_LOCAL:
414 if (DEBUG) Log.v(TAG, "Initializing local transport");
415 transport = new LocalTransport(mContext);
Christopher Tate8c850b72009-06-07 19:33:20 -0700416 break;
417
418 case BackupManager.TRANSPORT_GOOGLE:
419 if (DEBUG) Log.v(TAG, "Initializing Google transport");
420 //!!! TODO: stand up the google backup transport for real here
421 transport = new GoogleTransport();
422 break;
423
424 default:
425 Log.e(TAG, "creating unknown transport " + transportID);
426 }
427 return transport;
428 }
429
Christopher Tatedf01dea2009-06-09 20:45:02 -0700430 // fire off a backup agent, blocking until it attaches or times out
431 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
432 IBackupAgent agent = null;
433 synchronized(mAgentConnectLock) {
434 mConnecting = true;
435 mConnectedAgent = null;
436 try {
437 if (mActivityManager.bindBackupAgent(app, mode)) {
438 Log.d(TAG, "awaiting agent for " + app);
439
440 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -0700441 // only wait 10 seconds for the clear data to happen
442 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
443 while (mConnecting && mConnectedAgent == null
444 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700445 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700446 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700447 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700448 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -0700449 return null;
450 }
451 }
452
453 // if we timed out with no connect, abort and move on
454 if (mConnecting == true) {
455 Log.w(TAG, "Timeout waiting for agent " + app);
456 return null;
457 }
458 agent = mConnectedAgent;
459 }
460 } catch (RemoteException e) {
461 // can't happen
462 }
463 }
464 return agent;
465 }
466
Christopher Tatec7b31e32009-06-10 15:49:30 -0700467 // clear an application's data, blocking until the operation completes or times out
468 void clearApplicationDataSynchronous(String packageName) {
469 ClearDataObserver observer = new ClearDataObserver();
470
471 synchronized(mClearDataLock) {
472 mClearingData = true;
473 mPackageManager.clearApplicationUserData(packageName, observer);
474
475 // only wait 10 seconds for the clear data to happen
476 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
477 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
478 try {
479 mClearDataLock.wait(5000);
480 } catch (InterruptedException e) {
481 // won't happen, but still.
482 mClearingData = false;
483 }
484 }
485 }
486 }
487
488 class ClearDataObserver extends IPackageDataObserver.Stub {
489 public void onRemoveCompleted(String packageName, boolean succeeded)
490 throws android.os.RemoteException {
491 synchronized(mClearDataLock) {
492 mClearingData = false;
493 notifyAll();
494 }
495 }
496 }
497
Christopher Tate043dadc2009-06-02 16:11:00 -0700498 // ----- Back up a set of applications via a worker thread -----
499
500 class PerformBackupThread extends Thread {
501 private static final String TAG = "PerformBackupThread";
502 int mTransport;
503 ArrayList<BackupRequest> mQueue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700504 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700505
Christopher Tatecde87f42009-06-12 12:55:53 -0700506 public PerformBackupThread(int transportId, ArrayList<BackupRequest> queue,
507 File journal) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700508 mTransport = transportId;
509 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700510 mJournal = journal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700511 }
512
513 @Override
514 public void run() {
Christopher Tate043dadc2009-06-02 16:11:00 -0700515 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
516
517 // stand up the current transport
Christopher Tate8c850b72009-06-07 19:33:20 -0700518 IBackupTransport transport = createTransport(mTransport);
519 if (transport == null) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700520 return;
521 }
522
Christopher Tatedf01dea2009-06-09 20:45:02 -0700523 // start up the transport
524 try {
525 transport.startSession();
526 } catch (Exception e) {
527 Log.e(TAG, "Error session transport");
528 e.printStackTrace();
529 return;
530 }
531
Christopher Tate043dadc2009-06-02 16:11:00 -0700532 // The transport is up and running; now run all the backups in our queue
533 doQueuedBackups(transport);
534
535 // Finally, tear down the transport
536 try {
537 transport.endSession();
538 } catch (Exception e) {
539 Log.e(TAG, "Error ending transport");
540 e.printStackTrace();
541 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700542
543 if (!mJournal.delete()) {
544 Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath());
545 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700546 }
547
548 private void doQueuedBackups(IBackupTransport transport) {
549 for (BackupRequest request : mQueue) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700550 Log.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -0700551
552 IBackupAgent agent = null;
553 int mode = (request.fullBackup)
554 ? IApplicationThread.BACKUP_MODE_FULL
555 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
556 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700557 agent = bindToAgentSynchronous(request.appInfo, mode);
558 if (agent != null) {
559 processOneBackup(request, agent, transport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700560 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700561
562 // unbind even on timeout, just in case
563 mActivityManager.unbindBackupAgent(request.appInfo);
Christopher Tate043dadc2009-06-02 16:11:00 -0700564 } catch (SecurityException ex) {
565 // Try for the next one.
Christopher Tatec7b31e32009-06-10 15:49:30 -0700566 Log.d(TAG, "error in bind/backup", ex);
Christopher Tate043dadc2009-06-02 16:11:00 -0700567 } catch (RemoteException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700568 Log.v(TAG, "bind/backup threw");
569 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -0700570 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700571
Christopher Tate043dadc2009-06-02 16:11:00 -0700572 }
573 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700574
575 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
576 final String packageName = request.appInfo.packageName;
577 Log.d(TAG, "processOneBackup doBackup() on " + packageName);
578
579 try {
580 // Look up the package info & signatures. This is first so that if it
581 // throws an exception, there's no file setup yet that would need to
582 // be unraveled.
583 PackageInfo packInfo = mPackageManager.getPackageInfo(packageName,
584 PackageManager.GET_SIGNATURES);
585
586 // !!! TODO: get the state file dir from the transport
587 File savedStateName = new File(mStateDir, packageName);
588 File backupDataName = new File(mDataDir, packageName + ".data");
589 File newStateName = new File(mStateDir, packageName + ".new");
590
591 // In a full backup, we pass a null ParcelFileDescriptor as
592 // the saved-state "file"
593 ParcelFileDescriptor savedState = (request.fullBackup) ? null
594 : ParcelFileDescriptor.open(savedStateName,
595 ParcelFileDescriptor.MODE_READ_ONLY |
596 ParcelFileDescriptor.MODE_CREATE);
597
598 backupDataName.delete();
599 ParcelFileDescriptor backupData =
600 ParcelFileDescriptor.open(backupDataName,
601 ParcelFileDescriptor.MODE_READ_WRITE |
602 ParcelFileDescriptor.MODE_CREATE);
603
604 newStateName.delete();
605 ParcelFileDescriptor newState =
606 ParcelFileDescriptor.open(newStateName,
607 ParcelFileDescriptor.MODE_READ_WRITE |
608 ParcelFileDescriptor.MODE_CREATE);
609
610 // Run the target's backup pass
611 boolean success = false;
612 try {
613 agent.doBackup(savedState, backupData, newState);
614 success = true;
615 } finally {
616 if (savedState != null) {
617 savedState.close();
618 }
619 backupData.close();
620 newState.close();
621 }
622
623 // Now propagate the newly-backed-up data to the transport
624 if (success) {
625 if (DEBUG) Log.v(TAG, "doBackup() success; calling transport");
626 backupData =
627 ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY);
628 int error = transport.performBackup(packInfo, backupData);
629
630 // !!! TODO: After successful transport, delete the now-stale data
631 // and juggle the files so that next time the new state is passed
632 //backupDataName.delete();
633 newStateName.renameTo(savedStateName);
634 }
635 } catch (NameNotFoundException e) {
636 Log.e(TAG, "Package not found on backup: " + packageName);
637 } catch (FileNotFoundException fnf) {
638 Log.w(TAG, "File not found on backup: ");
639 fnf.printStackTrace();
640 } catch (RemoteException e) {
641 Log.d(TAG, "Remote target " + request.appInfo.packageName + " threw during backup:");
642 e.printStackTrace();
643 } catch (Exception e) {
644 Log.w(TAG, "Final exception guard in backup: ");
645 e.printStackTrace();
646 }
647 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700648 }
649
Christopher Tatedf01dea2009-06-09 20:45:02 -0700650
651 // ----- Restore handling -----
652
653 // Is the given package restorable on this device? Returns the on-device app's
654 // ApplicationInfo struct if it is; null if not.
655 //
656 // !!! TODO: also consider signatures
Christopher Tatec7b31e32009-06-10 15:49:30 -0700657 PackageInfo isRestorable(PackageInfo packageInfo) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700658 if (packageInfo.packageName != null) {
659 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700660 PackageInfo app = mPackageManager.getPackageInfo(packageInfo.packageName,
Christopher Tatedf01dea2009-06-09 20:45:02 -0700661 PackageManager.GET_SIGNATURES);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700662 if ((app.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700663 return app;
664 }
665 } catch (Exception e) {
666 // doesn't exist on this device, or other error -- just ignore it.
667 }
668 }
669 return null;
670 }
671
672 class PerformRestoreThread extends Thread {
673 private IBackupTransport mTransport;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700674 private int mToken;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700675 private RestoreSet mImage;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700676
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700677 PerformRestoreThread(IBackupTransport transport, int restoreSetToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700678 mTransport = transport;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700679 mToken = restoreSetToken;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700680 }
681
682 @Override
683 public void run() {
684 /**
685 * Restore sequence:
686 *
687 * 1. start up the transport session
688 * 2. get the restore set description for our identity
689 * 3. for each app in the restore set:
690 * 3.a. if it's restorable on this device, add it to the restore queue
691 * 4. for each app in the restore queue:
Christopher Tatec7b31e32009-06-10 15:49:30 -0700692 * 4.a. clear the app data
Christopher Tatedf01dea2009-06-09 20:45:02 -0700693 * 4.b. get the restore data for the app from the transport
694 * 4.c. launch the backup agent for the app
695 * 4.d. agent.doRestore() with the data from the server
696 * 4.e. unbind the agent [and kill the app?]
697 * 5. shut down the transport
698 */
699
700 int err = -1;
701 try {
702 err = mTransport.startSession();
703 } catch (Exception e) {
704 Log.e(TAG, "Error starting transport for restore");
705 e.printStackTrace();
706 }
707
708 if (err == 0) {
709 // build the set of apps to restore
710 try {
711 RestoreSet[] images = mTransport.getAvailableRestoreSets();
712 if (images.length > 0) {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700713 // !!! TODO: pick out the set for this token
Christopher Tatec7b31e32009-06-10 15:49:30 -0700714 mImage = images[0];
Christopher Tatedf01dea2009-06-09 20:45:02 -0700715
716 // build the set of apps we will attempt to restore
Christopher Tatec7b31e32009-06-10 15:49:30 -0700717 PackageInfo[] packages = mTransport.getAppSet(mImage.token);
718 HashSet<PackageInfo> appsToRestore = new HashSet<PackageInfo>();
Christopher Tatedf01dea2009-06-09 20:45:02 -0700719 for (PackageInfo pkg: packages) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700720 // get the real PackageManager idea of the package
721 PackageInfo app = isRestorable(pkg);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700722 if (app != null) {
723 appsToRestore.add(app);
724 }
725 }
726
727 // now run the restore queue
728 doQueuedRestores(appsToRestore);
729 }
730 } catch (RemoteException e) {
731 // can't happen; transports run locally
732 }
733
734 // done; shut down the transport
735 try {
736 mTransport.endSession();
737 } catch (Exception e) {
738 Log.e(TAG, "Error ending transport for restore");
739 e.printStackTrace();
740 }
741 }
742
743 // even if the initial session startup failed, report that we're done here
744 }
745
746 // restore each app in the queue
Christopher Tatec7b31e32009-06-10 15:49:30 -0700747 void doQueuedRestores(HashSet<PackageInfo> appsToRestore) {
748 for (PackageInfo app : appsToRestore) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700749 Log.d(TAG, "starting agent for restore of " + app);
750
Christopher Tatedf01dea2009-06-09 20:45:02 -0700751 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700752 // Remove the app's data first
753 clearApplicationDataSynchronous(app.packageName);
754
755 // Now perform the restore into the clean app
756 IBackupAgent agent = bindToAgentSynchronous(app.applicationInfo,
757 IApplicationThread.BACKUP_MODE_RESTORE);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700758 if (agent != null) {
759 processOneRestore(app, agent);
760 }
761
762 // unbind even on timeout, just in case
Christopher Tatec7b31e32009-06-10 15:49:30 -0700763 mActivityManager.unbindBackupAgent(app.applicationInfo);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700764 } catch (SecurityException ex) {
765 // Try for the next one.
766 Log.d(TAG, "error in bind", ex);
767 } catch (RemoteException e) {
768 // can't happen
769 }
770
771 }
772 }
773
Christopher Tatec7b31e32009-06-10 15:49:30 -0700774 // Do the guts of a restore of one application, derived from the 'mImage'
775 // restore set via the 'mTransport' transport.
776 void processOneRestore(PackageInfo app, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700777 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -0700778 final String packageName = app.packageName;
779
780 // !!! TODO: get the dirs from the transport
781 File backupDataName = new File(mDataDir, packageName + ".restore");
782 backupDataName.delete();
783 try {
784 ParcelFileDescriptor backupData =
785 ParcelFileDescriptor.open(backupDataName,
786 ParcelFileDescriptor.MODE_READ_WRITE |
787 ParcelFileDescriptor.MODE_CREATE);
788
789 // Run the transport's restore pass
790 // Run the target's backup pass
791 int err = -1;
792 try {
793 err = mTransport.getRestoreData(mImage.token, app, backupData);
794 } catch (RemoteException e) {
795 // can't happen
796 } finally {
797 backupData.close();
798 }
799
800 // Okay, we have the data. Now have the agent do the restore.
801 File newStateName = new File(mStateDir, packageName + ".new");
802 ParcelFileDescriptor newState =
803 ParcelFileDescriptor.open(newStateName,
804 ParcelFileDescriptor.MODE_READ_WRITE |
805 ParcelFileDescriptor.MODE_CREATE);
806
807 backupData = ParcelFileDescriptor.open(backupDataName,
808 ParcelFileDescriptor.MODE_READ_ONLY);
809
810 boolean success = false;
811 try {
812 agent.doRestore(backupData, newState);
813 success = true;
814 } catch (Exception e) {
815 Log.e(TAG, "Restore failed for " + packageName);
816 e.printStackTrace();
817 } finally {
818 newState.close();
819 backupData.close();
820 }
821
822 // if everything went okay, remember the recorded state now
823 if (success) {
824 File savedStateName = new File(mStateDir, packageName);
825 newStateName.renameTo(savedStateName);
826 }
827 } catch (FileNotFoundException fnfe) {
828 Log.v(TAG, "Couldn't open file for restore: " + fnfe);
829 } catch (IOException ioe) {
830 Log.e(TAG, "Unable to process restore file: " + ioe);
831 } catch (Exception e) {
832 Log.e(TAG, "Final exception guard in restore:");
833 e.printStackTrace();
834 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700835 }
836 }
837
838
Christopher Tate487529a2009-04-29 14:03:25 -0700839 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -0700840
Christopher Tatea8bf8152009-04-30 11:36:21 -0700841 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -0700842 // Record that we need a backup pass for the caller. Since multiple callers
843 // may share a uid, we need to note all candidates within that uid and schedule
844 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700845
846 Log.d(TAG, "dataChanged packageName=" + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -0700847
Christopher Tate181fafa2009-05-14 11:12:14 -0700848 HashSet<ApplicationInfo> targets = mBackupParticipants.get(Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -0700849 if (targets != null) {
850 synchronized (mQueueLock) {
851 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -0700852 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -0700853 // validate the caller-supplied package name against the known set of
854 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -0700855 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -0400856 // Add the caller to the set of pending backups. If there is
857 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -0700858 BackupRequest req = new BackupRequest(app, false);
859 mPendingBackups.put(app, req);
Christopher Tatecde87f42009-06-12 12:55:53 -0700860
861 // Journal this request in case of crash
862 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -0700863 }
864 }
865
Christopher Tate181fafa2009-05-14 11:12:14 -0700866 if (DEBUG) {
867 int numKeys = mPendingBackups.size();
868 Log.d(TAG, "Scheduling backup for " + numKeys + " participants:");
869 for (BackupRequest b : mPendingBackups.values()) {
870 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
871 }
872 }
Christopher Tate487529a2009-04-29 14:03:25 -0700873 // Schedule a backup pass in a few minutes. As backup-eligible data
874 // keeps changing, continue to defer the backup pass until things
875 // settle down, to avoid extra overhead.
Christopher Tate043dadc2009-06-02 16:11:00 -0700876 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
Christopher Tate487529a2009-04-29 14:03:25 -0700877 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL);
878 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700879 } else {
880 Log.w(TAG, "dataChanged but no participant pkg " + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -0700881 }
882 }
Christopher Tate46758122009-05-06 11:22:00 -0700883
Christopher Tatecde87f42009-06-12 12:55:53 -0700884 private void writeToJournalLocked(String str) {
885 if (mJournalStream != null) {
886 try {
887 mJournalStream.writeUTF(str);
888 } catch (IOException e) {
889 Log.e(TAG, "Error writing to backup journal");
890 mJournalStream = null;
891 mJournal = null;
892 }
893 }
894 }
895
Christopher Tate043dadc2009-06-02 16:11:00 -0700896 // Schedule a backup pass for a given package. This method will schedule a
897 // full backup even for apps that do not declare an android:backupAgent, so
898 // use with care.
Christopher Tate46758122009-05-06 11:22:00 -0700899 public void scheduleFullBackup(String packageName) throws RemoteException {
Christopher Tate043dadc2009-06-02 16:11:00 -0700900 mContext.enforceCallingPermission("android.permission.BACKUP", "scheduleFullBackup");
901
902 if (DEBUG) Log.v(TAG, "Scheduling immediate full backup for " + packageName);
Christopher Tate46758122009-05-06 11:22:00 -0700903 synchronized (mQueueLock) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700904 try {
905 ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0);
906 mPendingBackups.put(app, new BackupRequest(app, true));
907 mBackupHandler.sendEmptyMessage(MSG_RUN_FULL_BACKUP);
908 } catch (NameNotFoundException e) {
909 Log.w(TAG, "Could not find app for " + packageName + " to schedule full backup");
Christopher Tate46758122009-05-06 11:22:00 -0700910 }
911 }
912 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700913
Christopher Tate043dadc2009-06-02 16:11:00 -0700914 // Select which transport to use for the next backup operation
915 public int selectBackupTransport(int transportId) {
916 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
917
918 int prevTransport = mTransportId;
919 mTransportId = transportId;
920 return prevTransport;
921 }
922
923 // Callback: a requested backup agent has been instantiated. This should only
924 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700925 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700926 synchronized(mAgentConnectLock) {
927 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
928 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
929 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
930 mConnectedAgent = agent;
931 mConnecting = false;
932 } else {
933 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
934 + " claiming agent connected");
935 }
936 mAgentConnectLock.notifyAll();
937 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700938 }
939
940 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
941 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -0700942 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -0700943 public void agentDisconnected(String packageName) {
944 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -0700945 synchronized(mAgentConnectLock) {
946 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
947 mConnectedAgent = null;
948 mConnecting = false;
949 } else {
950 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
951 + " claiming agent disconnected");
952 }
953 mAgentConnectLock.notifyAll();
954 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700955 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700956
Christopher Tate8c850b72009-06-07 19:33:20 -0700957 // Hand off a restore session
958 public IRestoreSession beginRestoreSession(int transportID) {
959 mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession");
960 return null;
961 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700962
Christopher Tate9b3905c2009-06-08 15:24:01 -0700963 // ----- Restore session -----
964
965 class RestoreSession extends IRestoreSession.Stub {
966 private IBackupTransport mRestoreTransport = null;
967 RestoreSet[] mRestoreSets = null;
968
969 RestoreSession(int transportID) {
970 mRestoreTransport = createTransport(transportID);
971 }
972
973 // --- Binder interface ---
974 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700975 mContext.enforceCallingPermission("android.permission.BACKUP",
976 "getAvailableRestoreSets");
977
Christopher Tate9b3905c2009-06-08 15:24:01 -0700978 synchronized(this) {
979 if (mRestoreSets == null) {
980 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
981 }
982 return mRestoreSets;
983 }
984 }
985
986 public int performRestore(int token) throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700987 mContext.enforceCallingPermission("android.permission.BACKUP", "performRestore");
988
989 if (mRestoreSets != null) {
990 for (int i = 0; i < mRestoreSets.length; i++) {
991 if (token == mRestoreSets[i].token) {
992 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE,
993 mRestoreTransport);
994 msg.arg1 = token;
995 mBackupHandler.sendMessage(msg);
996 return 0;
997 }
998 }
999 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001000 return -1;
1001 }
1002
1003 public void endRestoreSession() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001004 mContext.enforceCallingPermission("android.permission.BACKUP",
1005 "endRestoreSession");
1006
Christopher Tate9b3905c2009-06-08 15:24:01 -07001007 mRestoreTransport.endSession();
1008 mRestoreTransport = null;
1009 }
1010 }
1011
Christopher Tate043dadc2009-06-02 16:11:00 -07001012
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001013 @Override
1014 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1015 synchronized (mQueueLock) {
1016 int N = mBackupParticipants.size();
1017 pw.println("Participants:");
1018 for (int i=0; i<N; i++) {
1019 int uid = mBackupParticipants.keyAt(i);
1020 pw.print(" uid: ");
1021 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07001022 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
1023 for (ApplicationInfo app: participants) {
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001024 pw.print(" ");
Christopher Tate181fafa2009-05-14 11:12:14 -07001025 pw.println(app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001026 }
1027 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001028 pw.println("Pending:");
1029 Iterator<BackupRequest> br = mPendingBackups.values().iterator();
1030 while (br.hasNext()) {
1031 pw.print(" ");
1032 pw.println(br);
1033 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001034 }
1035 }
Christopher Tate487529a2009-04-29 14:03:25 -07001036}