blob: ecdf92684f08f70ff9df009877423c33124d526e [file] [log] [blame]
Christopher Tate487529a2009-04-29 14:03:25 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Christopher Tate181fafa2009-05-14 11:12:14 -070019import android.app.ActivityManagerNative;
20import android.app.IActivityManager;
21import android.app.IApplicationThread;
22import android.app.IBackupAgent;
Christopher Tate3799bc22009-05-06 16:13:56 -070023import android.content.BroadcastReceiver;
Dan Egnor87a02bc2009-06-17 02:30:10 -070024import android.content.ComponentName;
Christopher Tate487529a2009-04-29 14:03:25 -070025import android.content.Context;
26import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070027import android.content.IntentFilter;
Dan Egnor87a02bc2009-06-17 02:30:10 -070028import android.content.ServiceConnection;
Christopher Tate181fafa2009-05-14 11:12:14 -070029import android.content.pm.ApplicationInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -070030import android.content.pm.IPackageDataObserver;
Christopher Tate7b881282009-06-07 13:52:37 -070031import android.content.pm.PackageInfo;
Christopher Tate043dadc2009-06-02 16:11:00 -070032import android.content.pm.PackageManager.NameNotFoundException;
Dan Egnor87a02bc2009-06-17 02:30:10 -070033import android.content.pm.PackageManager;
Christopher Tateabce4e82009-06-18 18:35:32 -070034import android.content.pm.Signature;
Christopher Tate3799bc22009-05-06 16:13:56 -070035import android.net.Uri;
Christopher Tate487529a2009-04-29 14:03:25 -070036import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070037import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070038import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070039import android.os.Handler;
40import android.os.IBinder;
41import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070042import android.os.ParcelFileDescriptor;
Christopher Tate043dadc2009-06-02 16:11:00 -070043import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070044import android.os.RemoteException;
Christopher Tate91717492009-06-26 21:07:13 -070045import android.os.SystemProperties;
Christopher Tate487529a2009-04-29 14:03:25 -070046import android.util.Log;
47import android.util.SparseArray;
48
49import android.backup.IBackupManager;
Christopher Tate7d562ec2009-06-25 18:03:43 -070050import android.backup.IRestoreObserver;
Christopher Tate8c850b72009-06-07 19:33:20 -070051import android.backup.IRestoreSession;
Christopher Tate9b3905c2009-06-08 15:24:01 -070052import android.backup.RestoreSet;
Christopher Tate043dadc2009-06-02 16:11:00 -070053
Christopher Tate9bbc21a2009-06-10 20:23:25 -070054import com.android.internal.backup.LocalTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -070055import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070056
Christopher Tate6785dd82009-06-18 15:58:25 -070057import com.android.server.PackageManagerBackupAgent;
Christopher Tate6aa41f42009-06-19 14:14:22 -070058import com.android.server.PackageManagerBackupAgent.Metadata;
Christopher Tate6785dd82009-06-18 15:58:25 -070059
Christopher Tatecde87f42009-06-12 12:55:53 -070060import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070061import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070062import java.io.FileDescriptor;
Christopher Tatec7b31e32009-06-10 15:49:30 -070063import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070064import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070065import java.io.RandomAccessFile;
Christopher Tate487529a2009-04-29 14:03:25 -070066import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040067import java.util.ArrayList;
68import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070069import java.util.HashSet;
70import java.util.List;
Christopher Tate91717492009-06-26 21:07:13 -070071import java.util.Map;
Christopher Tate487529a2009-04-29 14:03:25 -070072
73class BackupManagerService extends IBackupManager.Stub {
74 private static final String TAG = "BackupManagerService";
75 private static final boolean DEBUG = true;
Christopher Tateaa088442009-06-16 18:25:46 -070076
Christopher Tate6ef58a12009-06-29 14:56:28 -070077 // Persistent properties
78 private static final String BACKUP_TRANSPORT_PROPERTY = "persist.service.bkup.trans";
79 private static final String BACKUP_ENABLED_PROPERTY = "persist.service.bkup.enabled";
80
Christopher Tate6785dd82009-06-18 15:58:25 -070081 // Default time to wait after data changes before we back up the data
Christopher Tate111bd4a2009-06-24 17:29:38 -070082 private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
Christopher Tate487529a2009-04-29 14:03:25 -070083
84 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070085 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070086 private static final int MSG_RUN_RESTORE = 3;
Christopher Tatec7b31e32009-06-10 15:49:30 -070087
88 // Timeout interval for deciding that a bind or clear-data has taken too long
89 static final long TIMEOUT_INTERVAL = 10 * 1000;
90
Christopher Tate487529a2009-04-29 14:03:25 -070091 private Context mContext;
92 private PackageManager mPackageManager;
Christopher Tate6ef58a12009-06-29 14:56:28 -070093 private IActivityManager mActivityManager;
94 private boolean mEnabled; // access to this is synchronized on 'this'
Christopher Tate487529a2009-04-29 14:03:25 -070095 private final BackupHandler mBackupHandler = new BackupHandler();
96 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate6ef58a12009-06-29 14:56:28 -070097 private final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
Christopher Tate181fafa2009-05-14 11:12:14 -070098 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -070099 // set of backup services that have pending changes
Christopher Tate46758122009-05-06 11:22:00 -0700100 private class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -0700101 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -0700102 public boolean fullBackup;
Christopher Tateaa088442009-06-16 18:25:46 -0700103
Christopher Tate181fafa2009-05-14 11:12:14 -0700104 BackupRequest(ApplicationInfo app, boolean isFull) {
105 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -0700106 fullBackup = isFull;
107 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700108
109 public String toString() {
110 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
111 }
Christopher Tate46758122009-05-06 11:22:00 -0700112 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400113 // Backups that we haven't started yet.
Christopher Tate181fafa2009-05-14 11:12:14 -0700114 private HashMap<ApplicationInfo,BackupRequest> mPendingBackups
115 = new HashMap<ApplicationInfo,BackupRequest>();
Christopher Tate5cb400b2009-06-25 16:03:14 -0700116
117 // Pseudoname that we use for the Package Manager metadata "package"
Christopher Tate6785dd82009-06-18 15:58:25 -0700118 private static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
Christopher Tate6aa41f42009-06-19 14:14:22 -0700119
120 // locking around the pending-backup management
Christopher Tate487529a2009-04-29 14:03:25 -0700121 private final Object mQueueLock = new Object();
122
Christopher Tate043dadc2009-06-02 16:11:00 -0700123 // The thread performing the sequence of queued backups binds to each app's agent
124 // in succession. Bind notifications are asynchronously delivered through the
125 // Activity Manager; use this lock object to signal when a requested binding has
126 // completed.
127 private final Object mAgentConnectLock = new Object();
128 private IBackupAgent mConnectedAgent;
129 private volatile boolean mConnecting;
130
Christopher Tatec7b31e32009-06-10 15:49:30 -0700131 // A similar synchronicity mechanism around clearing apps' data for restore
132 private final Object mClearDataLock = new Object();
133 private volatile boolean mClearingData;
134
Christopher Tate91717492009-06-26 21:07:13 -0700135 // Transport bookkeeping
Christopher Tate91717492009-06-26 21:07:13 -0700136 private final HashMap<String,IBackupTransport> mTransports
137 = new HashMap<String,IBackupTransport>();
138 private String mCurrentTransport;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700139 private IBackupTransport mLocalTransport, mGoogleTransport;
Christopher Tatef68eb502009-06-16 11:02:01 -0700140 private RestoreSession mActiveRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -0700141
Christopher Tate7d562ec2009-06-25 18:03:43 -0700142 private class RestoreParams {
143 public IBackupTransport transport;
144 public IRestoreObserver observer;
Dan Egnor156411d2009-06-26 13:20:02 -0700145 public long token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700146
Dan Egnor156411d2009-06-26 13:20:02 -0700147 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700148 transport = _transport;
149 observer = _obs;
Dan Egnor156411d2009-06-26 13:20:02 -0700150 token = _token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700151 }
152 }
153
Christopher Tate5cb400b2009-06-25 16:03:14 -0700154 // Where we keep our journal files and other bookkeeping
155 private File mBaseStateDir;
Christopher Tatef4172472009-05-05 15:50:03 -0700156 private File mDataDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700157 private File mJournalDir;
158 private File mJournal;
159 private RandomAccessFile mJournalStream;
Christopher Tateaa088442009-06-16 18:25:46 -0700160
Christopher Tate487529a2009-04-29 14:03:25 -0700161 public BackupManagerService(Context context) {
162 mContext = context;
163 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700164 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700165
Christopher Tate22b87872009-05-04 16:41:53 -0700166 // Set up our bookkeeping
Christopher Tate6ef58a12009-06-29 14:56:28 -0700167 // !!! STOPSHIP: make this disabled by default so that we then gate on
168 // setupwizard or other opt-out UI
169 mEnabled = SystemProperties.getBoolean(BACKUP_ENABLED_PROPERTY, true);
Christopher Tate5cb400b2009-06-25 16:03:14 -0700170 mBaseStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tatef4172472009-05-05 15:50:03 -0700171 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700172
Christopher Tatecde87f42009-06-12 12:55:53 -0700173 // Set up the backup-request journaling
Christopher Tate5cb400b2009-06-25 16:03:14 -0700174 mJournalDir = new File(mBaseStateDir, "pending");
175 mJournalDir.mkdirs(); // creates mBaseStateDir along the way
Christopher Tatecde87f42009-06-12 12:55:53 -0700176 makeJournalLocked(); // okay because no other threads are running yet
177
Christopher Tateabce4e82009-06-18 18:35:32 -0700178 // Build our mapping of uid to backup client services. This implicitly
179 // schedules a backup pass on the Package Manager metadata the first
180 // time anything needs to be backed up.
Christopher Tate3799bc22009-05-06 16:13:56 -0700181 synchronized (mBackupParticipants) {
182 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700183 }
184
Dan Egnor87a02bc2009-06-17 02:30:10 -0700185 // Set up our transport options and initialize the default transport
186 // TODO: Have transports register themselves somehow?
187 // TODO: Don't create transports that we don't need to?
Dan Egnor87a02bc2009-06-17 02:30:10 -0700188 mLocalTransport = new LocalTransport(context); // This is actually pretty cheap
Christopher Tate91717492009-06-26 21:07:13 -0700189 ComponentName localName = new ComponentName(context, LocalTransport.class);
190 registerTransport(localName.flattenToShortString(), mLocalTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700191
Christopher Tate91717492009-06-26 21:07:13 -0700192 mGoogleTransport = null;
193 // !!! TODO: set up the default transport name "the right way"
194 mCurrentTransport = SystemProperties.get(BACKUP_TRANSPORT_PROPERTY,
195 "com.google.android.backup/.BackupTransportService");
196 if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport);
197
198 // Attach to the Google backup transport. When this comes up, it will set
199 // itself as the current transport because we explicitly reset mCurrentTransport
200 // to null.
Dan Egnor87a02bc2009-06-17 02:30:10 -0700201 Intent intent = new Intent().setComponent(new ComponentName(
202 "com.google.android.backup",
203 "com.google.android.backup.BackupTransportService"));
204 context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE);
Christopher Tateaa088442009-06-16 18:25:46 -0700205
Christopher Tatecde87f42009-06-12 12:55:53 -0700206 // Now that we know about valid backup participants, parse any
207 // leftover journal files and schedule a new backup pass
208 parseLeftoverJournals();
209
Christopher Tate3799bc22009-05-06 16:13:56 -0700210 // Register for broadcasts about package install, etc., so we can
211 // update the provider list.
212 IntentFilter filter = new IntentFilter();
213 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
214 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
215 filter.addDataScheme("package");
216 mContext.registerReceiver(mBroadcastReceiver, filter);
217 }
218
Christopher Tatecde87f42009-06-12 12:55:53 -0700219 private void makeJournalLocked() {
220 try {
221 mJournal = File.createTempFile("journal", null, mJournalDir);
222 mJournalStream = new RandomAccessFile(mJournal, "rwd");
223 } catch (IOException e) {
224 Log.e(TAG, "Unable to write backup journals");
225 mJournal = null;
226 mJournalStream = null;
227 }
228 }
229
230 private void parseLeftoverJournals() {
231 if (mJournal != null) {
232 File[] allJournals = mJournalDir.listFiles();
233 for (File f : allJournals) {
234 if (f.compareTo(mJournal) != 0) {
235 // This isn't the current journal, so it must be a leftover. Read
236 // out the package names mentioned there and schedule them for
237 // backup.
238 try {
239 Log.i(TAG, "Found stale backup journal, scheduling:");
240 RandomAccessFile in = new RandomAccessFile(f, "r");
241 while (true) {
242 String packageName = in.readUTF();
243 Log.i(TAG, " + " + packageName);
244 dataChanged(packageName);
245 }
246 } catch (EOFException e) {
247 // no more data; we're done
248 } catch (Exception e) {
249 // can't read it or other error; just skip it
250 } finally {
251 // close/delete the file
252 f.delete();
253 }
254 }
255 }
256 }
257 }
258
Christopher Tate91717492009-06-26 21:07:13 -0700259 // Add a transport to our set of available backends
260 private void registerTransport(String name, IBackupTransport transport) {
261 synchronized (mTransports) {
262 mTransports.put(name, transport);
263 }
264 }
265
Christopher Tate3799bc22009-05-06 16:13:56 -0700266 // ----- Track installation/removal of packages -----
267 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
268 public void onReceive(Context context, Intent intent) {
269 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
270
271 Uri uri = intent.getData();
272 if (uri == null) {
273 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700274 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700275 String pkgName = uri.getSchemeSpecificPart();
276 if (pkgName == null) {
277 return;
278 }
279
280 String action = intent.getAction();
281 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
282 synchronized (mBackupParticipants) {
283 Bundle extras = intent.getExtras();
284 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
285 // The package was just upgraded
286 updatePackageParticipantsLocked(pkgName);
287 } else {
288 // The package was just added
289 addPackageParticipantsLocked(pkgName);
290 }
291 }
292 }
293 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
294 Bundle extras = intent.getExtras();
295 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
296 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
297 } else {
298 synchronized (mBackupParticipants) {
299 removePackageParticipantsLocked(pkgName);
300 }
301 }
302 }
303 }
304 };
305
Dan Egnor87a02bc2009-06-17 02:30:10 -0700306 // ----- Track connection to GoogleBackupTransport service -----
307 ServiceConnection mGoogleConnection = new ServiceConnection() {
308 public void onServiceConnected(ComponentName name, IBinder service) {
309 if (DEBUG) Log.v(TAG, "Connected to Google transport");
310 mGoogleTransport = IBackupTransport.Stub.asInterface(service);
Christopher Tate91717492009-06-26 21:07:13 -0700311 registerTransport(name.flattenToShortString(), mGoogleTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700312 }
313
314 public void onServiceDisconnected(ComponentName name) {
315 if (DEBUG) Log.v(TAG, "Disconnected from Google transport");
316 mGoogleTransport = null;
Christopher Tate91717492009-06-26 21:07:13 -0700317 registerTransport(name.flattenToShortString(), null);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700318 }
319 };
320
Joe Onorato8ad02812009-05-13 01:41:44 -0400321 // ----- Run the actual backup process asynchronously -----
322
Christopher Tate181fafa2009-05-14 11:12:14 -0700323 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400324 public void handleMessage(Message msg) {
325
326 switch (msg.what) {
327 case MSG_RUN_BACKUP:
Dan Egnor87a02bc2009-06-17 02:30:10 -0700328 {
Christopher Tate91717492009-06-26 21:07:13 -0700329 IBackupTransport transport = getTransport(mCurrentTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700330 if (transport == null) {
331 Log.v(TAG, "Backup requested but no transport available");
332 break;
333 }
334
Joe Onorato8ad02812009-05-13 01:41:44 -0400335 // snapshot the pending-backup set and work on that
Christopher Tate6aa41f42009-06-19 14:14:22 -0700336 ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
Christopher Tatecde87f42009-06-12 12:55:53 -0700337 File oldJournal = mJournal;
Joe Onorato8ad02812009-05-13 01:41:44 -0400338 synchronized (mQueueLock) {
Christopher Tateace7f092009-06-15 18:07:25 -0700339 if (mPendingBackups.size() == 0) {
340 Log.v(TAG, "Backup requested but nothing pending");
341 break;
342 }
343
Christopher Tate6aa41f42009-06-19 14:14:22 -0700344 for (BackupRequest b: mPendingBackups.values()) {
345 queue.add(b);
Joe Onorato8ad02812009-05-13 01:41:44 -0400346 }
Christopher Tate6aa41f42009-06-19 14:14:22 -0700347 Log.v(TAG, "clearing pending backups");
348 mPendingBackups.clear();
Christopher Tateace7f092009-06-15 18:07:25 -0700349
350 // Start a new backup-queue journal file too
Christopher Tatecde87f42009-06-12 12:55:53 -0700351 if (mJournalStream != null) {
352 try {
353 mJournalStream.close();
354 } catch (IOException e) {
355 // don't need to do anything
356 }
357 makeJournalLocked();
358 }
359
360 // At this point, we have started a new journal file, and the old
361 // file identity is being passed to the backup processing thread.
362 // When it completes successfully, that old journal file will be
363 // deleted. If we crash prior to that, the old journal is parsed
364 // at next boot and the journaled requests fulfilled.
Joe Onorato8ad02812009-05-13 01:41:44 -0400365 }
Dan Egnor87a02bc2009-06-17 02:30:10 -0700366
Christopher Tate6aa41f42009-06-19 14:14:22 -0700367 (new PerformBackupThread(transport, queue, oldJournal)).start();
Christopher Tate043dadc2009-06-02 16:11:00 -0700368 break;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700369 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700370
371 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400372 break;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700373
374 case MSG_RUN_RESTORE:
375 {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700376 RestoreParams params = (RestoreParams)msg.obj;
Dan Egnor156411d2009-06-26 13:20:02 -0700377 (new PerformRestoreThread(params.transport, params.observer, params.token)).start();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700378 break;
379 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400380 }
381 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400382 }
383
Christopher Tate181fafa2009-05-14 11:12:14 -0700384 // Add the backup agents in the given package to our set of known backup participants.
385 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700386 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700387 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700388 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700389 List<PackageInfo> targetApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700390 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700391 }
392
Christopher Tate181fafa2009-05-14 11:12:14 -0700393 private void addPackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700394 List<PackageInfo> targetPkgs) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700395 if (DEBUG) {
Dan Egnorefe52642009-06-24 00:16:33 -0700396 Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
397 for (PackageInfo p : targetPkgs) {
Christopher Tate111bd4a2009-06-24 17:29:38 -0700398 Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName
399 + " uid=" + p.applicationInfo.uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700400 }
401 }
402
Dan Egnorefe52642009-06-24 00:16:33 -0700403 for (PackageInfo pkg : targetPkgs) {
404 if (packageName == null || pkg.packageName.equals(packageName)) {
405 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700406 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700407 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700408 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700409 mBackupParticipants.put(uid, set);
410 }
Dan Egnorefe52642009-06-24 00:16:33 -0700411 set.add(pkg.applicationInfo);
Christopher Tate3799bc22009-05-06 16:13:56 -0700412 }
Christopher Tate487529a2009-04-29 14:03:25 -0700413 }
414 }
415
Christopher Tate6785dd82009-06-18 15:58:25 -0700416 // Remove the given package's entry from our known active set. If
417 // 'packageName' is null, *all* participating apps will be removed.
Christopher Tate3799bc22009-05-06 16:13:56 -0700418 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700419 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700420 List<PackageInfo> allApps = null;
Christopher Tate181fafa2009-05-14 11:12:14 -0700421 if (packageName != null) {
Dan Egnorefe52642009-06-24 00:16:33 -0700422 allApps = new ArrayList<PackageInfo>();
Christopher Tate181fafa2009-05-14 11:12:14 -0700423 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700424 int flags = PackageManager.GET_SIGNATURES;
425 allApps.add(mPackageManager.getPackageInfo(packageName, flags));
Christopher Tate181fafa2009-05-14 11:12:14 -0700426 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700427 // just skip it (???)
Christopher Tate181fafa2009-05-14 11:12:14 -0700428 }
429 } else {
430 // all apps with agents
Dan Egnorefe52642009-06-24 00:16:33 -0700431 allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700432 }
433 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700434 }
435
Joe Onorato8ad02812009-05-13 01:41:44 -0400436 private void removePackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700437 List<PackageInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700438 if (DEBUG) {
439 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
440 + ") removing " + agents.size() + " entries");
Dan Egnorefe52642009-06-24 00:16:33 -0700441 for (PackageInfo p : agents) {
442 Log.v(TAG, " - " + p);
Christopher Tate043dadc2009-06-02 16:11:00 -0700443 }
444 }
Dan Egnorefe52642009-06-24 00:16:33 -0700445 for (PackageInfo pkg : agents) {
446 if (packageName == null || pkg.packageName.equals(packageName)) {
447 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700448 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700449 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700450 // Find the existing entry with the same package name, and remove it.
451 // We can't just remove(app) because the instances are different.
452 for (ApplicationInfo entry: set) {
Dan Egnorefe52642009-06-24 00:16:33 -0700453 if (entry.packageName.equals(pkg.packageName)) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700454 set.remove(entry);
455 break;
456 }
457 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700458 if (set.size() == 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700459 mBackupParticipants.delete(uid);
460 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700461 }
462 }
463 }
464 }
465
Christopher Tate181fafa2009-05-14 11:12:14 -0700466 // Returns the set of all applications that define an android:backupAgent attribute
Dan Egnorefe52642009-06-24 00:16:33 -0700467 private List<PackageInfo> allAgentPackages() {
Christopher Tate6785dd82009-06-18 15:58:25 -0700468 // !!! TODO: cache this and regenerate only when necessary
Dan Egnorefe52642009-06-24 00:16:33 -0700469 int flags = PackageManager.GET_SIGNATURES;
470 List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
471 int N = packages.size();
472 for (int a = N-1; a >= 0; a--) {
473 ApplicationInfo app = packages.get(a).applicationInfo;
474 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
475 || app.backupAgentName == null) {
476 packages.remove(a);
Christopher Tate181fafa2009-05-14 11:12:14 -0700477 }
478 }
Dan Egnorefe52642009-06-24 00:16:33 -0700479 return packages;
Christopher Tate181fafa2009-05-14 11:12:14 -0700480 }
Christopher Tateaa088442009-06-16 18:25:46 -0700481
Christopher Tate3799bc22009-05-06 16:13:56 -0700482 // Reset the given package's known backup participants. Unlike add/remove, the update
483 // action cannot be passed a null package name.
484 void updatePackageParticipantsLocked(String packageName) {
485 if (packageName == null) {
486 Log.e(TAG, "updatePackageParticipants called with null package name");
487 return;
488 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700489 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700490
491 // brute force but small code size
Dan Egnorefe52642009-06-24 00:16:33 -0700492 List<PackageInfo> allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700493 removePackageParticipantsLockedInner(packageName, allApps);
494 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700495 }
496
Christopher Tate6785dd82009-06-18 15:58:25 -0700497 // The queue lock should be held when scheduling a backup pass
498 private void scheduleBackupPassLocked(long timeFromNowMillis) {
Christopher Tate6ef58a12009-06-29 14:56:28 -0700499 // We only schedule backups when we're actually enabled
500 synchronized (this) {
501 if (mEnabled) {
502 mBackupHandler.removeMessages(MSG_RUN_BACKUP);
503 mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, timeFromNowMillis);
504 } else if (DEBUG) {
505 Log.v(TAG, "Disabled, so not scheduling backup pass");
506 }
507 }
Christopher Tate6785dd82009-06-18 15:58:25 -0700508 }
509
Dan Egnor87a02bc2009-06-17 02:30:10 -0700510 // Return the given transport
Christopher Tate91717492009-06-26 21:07:13 -0700511 private IBackupTransport getTransport(String transportName) {
512 synchronized (mTransports) {
513 IBackupTransport transport = mTransports.get(transportName);
514 if (transport == null) {
515 Log.w(TAG, "Requested unavailable transport: " + transportName);
516 }
517 return transport;
Christopher Tate8c850b72009-06-07 19:33:20 -0700518 }
Christopher Tate8c850b72009-06-07 19:33:20 -0700519 }
520
Christopher Tatedf01dea2009-06-09 20:45:02 -0700521 // fire off a backup agent, blocking until it attaches or times out
522 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
523 IBackupAgent agent = null;
524 synchronized(mAgentConnectLock) {
525 mConnecting = true;
526 mConnectedAgent = null;
527 try {
528 if (mActivityManager.bindBackupAgent(app, mode)) {
529 Log.d(TAG, "awaiting agent for " + app);
530
531 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -0700532 // only wait 10 seconds for the clear data to happen
533 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
534 while (mConnecting && mConnectedAgent == null
535 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700536 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700537 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700538 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700539 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -0700540 return null;
541 }
542 }
543
544 // if we timed out with no connect, abort and move on
545 if (mConnecting == true) {
546 Log.w(TAG, "Timeout waiting for agent " + app);
547 return null;
548 }
549 agent = mConnectedAgent;
550 }
551 } catch (RemoteException e) {
552 // can't happen
553 }
554 }
555 return agent;
556 }
557
Christopher Tatec7b31e32009-06-10 15:49:30 -0700558 // clear an application's data, blocking until the operation completes or times out
559 void clearApplicationDataSynchronous(String packageName) {
Christopher Tatef7c886b2009-06-26 15:34:09 -0700560 // Don't wipe packages marked allowClearUserData=false
561 try {
562 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
563 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
564 if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping "
565 + packageName);
566 return;
567 }
568 } catch (NameNotFoundException e) {
569 Log.w(TAG, "Tried to clear data for " + packageName + " but not found");
570 return;
571 }
572
Christopher Tatec7b31e32009-06-10 15:49:30 -0700573 ClearDataObserver observer = new ClearDataObserver();
574
575 synchronized(mClearDataLock) {
576 mClearingData = true;
577 mPackageManager.clearApplicationUserData(packageName, observer);
578
579 // only wait 10 seconds for the clear data to happen
580 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
581 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
582 try {
583 mClearDataLock.wait(5000);
584 } catch (InterruptedException e) {
585 // won't happen, but still.
586 mClearingData = false;
587 }
588 }
589 }
590 }
591
592 class ClearDataObserver extends IPackageDataObserver.Stub {
593 public void onRemoveCompleted(String packageName, boolean succeeded)
594 throws android.os.RemoteException {
595 synchronized(mClearDataLock) {
596 mClearingData = false;
Christopher Tatef68eb502009-06-16 11:02:01 -0700597 mClearDataLock.notifyAll();
Christopher Tatec7b31e32009-06-10 15:49:30 -0700598 }
599 }
600 }
601
Christopher Tate043dadc2009-06-02 16:11:00 -0700602 // ----- Back up a set of applications via a worker thread -----
603
604 class PerformBackupThread extends Thread {
605 private static final String TAG = "PerformBackupThread";
Christopher Tateaa088442009-06-16 18:25:46 -0700606 IBackupTransport mTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700607 ArrayList<BackupRequest> mQueue;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700608 File mStateDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700609 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700610
Christopher Tateaa088442009-06-16 18:25:46 -0700611 public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue,
Christopher Tatecde87f42009-06-12 12:55:53 -0700612 File journal) {
Christopher Tateaa088442009-06-16 18:25:46 -0700613 mTransport = transport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700614 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700615 mJournal = journal;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700616
617 try {
618 mStateDir = new File(mBaseStateDir, transport.transportDirName());
619 } catch (RemoteException e) {
620 // can't happen; the transport is local
621 }
622 mStateDir.mkdirs();
Christopher Tate043dadc2009-06-02 16:11:00 -0700623 }
624
625 @Override
626 public void run() {
Christopher Tate043dadc2009-06-02 16:11:00 -0700627 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
628
Christopher Tate79588342009-06-30 16:11:49 -0700629 // Backups run at background priority
630 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
631
Christopher Tate5cb400b2009-06-25 16:03:14 -0700632 // The package manager doesn't have a proper <application> etc, but since
633 // it's running here in the system process we can just set up its agent
634 // directly and use a synthetic BackupRequest. We always run this pass
635 // because it's cheap and this way we guarantee that we don't get out of
636 // step even if we're selecting among various transports at run time.
637 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
638 mPackageManager, allAgentPackages());
639 BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false);
640 pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL;
641 processOneBackup(pmRequest,
642 IBackupAgent.Stub.asInterface(pmAgent.onBind()),
643 mTransport);
Christopher Tate6785dd82009-06-18 15:58:25 -0700644
645 // Now run all the backups in our queue
Christopher Tateaa088442009-06-16 18:25:46 -0700646 doQueuedBackups(mTransport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700647
648 // Finally, tear down the transport
649 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700650 if (!mTransport.finishBackup()) {
651 // STOPSHIP TODO: handle errors
652 Log.e(TAG, "Backup failure in finishBackup()");
653 }
654 } catch (RemoteException e) {
655 Log.e(TAG, "Error in finishBackup()", e);
Christopher Tate043dadc2009-06-02 16:11:00 -0700656 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700657
658 if (!mJournal.delete()) {
659 Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath());
660 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700661 }
662
663 private void doQueuedBackups(IBackupTransport transport) {
664 for (BackupRequest request : mQueue) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700665 Log.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -0700666
667 IBackupAgent agent = null;
668 int mode = (request.fullBackup)
669 ? IApplicationThread.BACKUP_MODE_FULL
670 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
671 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700672 agent = bindToAgentSynchronous(request.appInfo, mode);
673 if (agent != null) {
674 processOneBackup(request, agent, transport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700675 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700676
677 // unbind even on timeout, just in case
678 mActivityManager.unbindBackupAgent(request.appInfo);
Christopher Tate043dadc2009-06-02 16:11:00 -0700679 } catch (SecurityException ex) {
680 // Try for the next one.
Christopher Tatec7b31e32009-06-10 15:49:30 -0700681 Log.d(TAG, "error in bind/backup", ex);
Christopher Tate043dadc2009-06-02 16:11:00 -0700682 } catch (RemoteException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700683 Log.v(TAG, "bind/backup threw");
684 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -0700685 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700686
Christopher Tate043dadc2009-06-02 16:11:00 -0700687 }
688 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700689
690 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
691 final String packageName = request.appInfo.packageName;
692 Log.d(TAG, "processOneBackup doBackup() on " + packageName);
693
694 try {
695 // Look up the package info & signatures. This is first so that if it
696 // throws an exception, there's no file setup yet that would need to
697 // be unraveled.
Christopher Tateabce4e82009-06-18 18:35:32 -0700698 PackageInfo packInfo;
699 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
700 // The metadata 'package' is synthetic
701 packInfo = new PackageInfo();
702 packInfo.packageName = packageName;
703 } else {
704 packInfo = mPackageManager.getPackageInfo(packageName,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700705 PackageManager.GET_SIGNATURES);
Christopher Tateabce4e82009-06-18 18:35:32 -0700706 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700707
708 // !!! TODO: get the state file dir from the transport
709 File savedStateName = new File(mStateDir, packageName);
710 File backupDataName = new File(mDataDir, packageName + ".data");
711 File newStateName = new File(mStateDir, packageName + ".new");
712
713 // In a full backup, we pass a null ParcelFileDescriptor as
714 // the saved-state "file"
715 ParcelFileDescriptor savedState = (request.fullBackup) ? null
716 : ParcelFileDescriptor.open(savedStateName,
717 ParcelFileDescriptor.MODE_READ_ONLY |
718 ParcelFileDescriptor.MODE_CREATE);
719
720 backupDataName.delete();
721 ParcelFileDescriptor backupData =
722 ParcelFileDescriptor.open(backupDataName,
723 ParcelFileDescriptor.MODE_READ_WRITE |
724 ParcelFileDescriptor.MODE_CREATE);
725
726 newStateName.delete();
727 ParcelFileDescriptor newState =
728 ParcelFileDescriptor.open(newStateName,
729 ParcelFileDescriptor.MODE_READ_WRITE |
730 ParcelFileDescriptor.MODE_CREATE);
731
732 // Run the target's backup pass
733 boolean success = false;
734 try {
735 agent.doBackup(savedState, backupData, newState);
736 success = true;
737 } finally {
738 if (savedState != null) {
739 savedState.close();
740 }
741 backupData.close();
742 newState.close();
743 }
744
745 // Now propagate the newly-backed-up data to the transport
746 if (success) {
747 if (DEBUG) Log.v(TAG, "doBackup() success; calling transport");
748 backupData =
749 ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY);
Dan Egnorefe52642009-06-24 00:16:33 -0700750 if (!transport.performBackup(packInfo, backupData)) {
751 // STOPSHIP TODO: handle errors
752 Log.e(TAG, "Backup failure in performBackup()");
753 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700754
755 // !!! TODO: After successful transport, delete the now-stale data
756 // and juggle the files so that next time the new state is passed
757 //backupDataName.delete();
758 newStateName.renameTo(savedStateName);
759 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700760 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700761 Log.e(TAG, "Error backing up " + packageName, e);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700762 }
763 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700764 }
765
Christopher Tatedf01dea2009-06-09 20:45:02 -0700766
767 // ----- Restore handling -----
768
Christopher Tateabce4e82009-06-18 18:35:32 -0700769 private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) {
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700770 // Allow unsigned apps, but not signed on one device and unsigned on the other
771 // !!! TODO: is this the right policy?
Christopher Tate6aa41f42009-06-19 14:14:22 -0700772 if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs
773 + " device=" + deviceSigs);
Christopher Tate20efdf6b2009-06-18 19:41:36 -0700774 if ((storedSigs == null || storedSigs.length == 0)
775 && (deviceSigs == null || deviceSigs.length == 0)) {
776 return true;
777 }
778 if (storedSigs == null || deviceSigs == null) {
779 return false;
780 }
781
Christopher Tateabce4e82009-06-18 18:35:32 -0700782 // !!! TODO: this demands that every stored signature match one
783 // that is present on device, and does not demand the converse.
784 // Is this this right policy?
785 int nStored = storedSigs.length;
786 int nDevice = deviceSigs.length;
787
788 for (int i=0; i < nStored; i++) {
789 boolean match = false;
790 for (int j=0; j < nDevice; j++) {
791 if (storedSigs[i].equals(deviceSigs[j])) {
792 match = true;
793 break;
794 }
795 }
796 if (!match) {
797 return false;
798 }
799 }
800 return true;
801 }
802
Christopher Tatedf01dea2009-06-09 20:45:02 -0700803 class PerformRestoreThread extends Thread {
804 private IBackupTransport mTransport;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700805 private IRestoreObserver mObserver;
Dan Egnor156411d2009-06-26 13:20:02 -0700806 private long mToken;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700807 private RestoreSet mImage;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700808 private File mStateDir;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700809
Christopher Tate5cbbf562009-06-22 16:44:51 -0700810 class RestoreRequest {
811 public PackageInfo app;
812 public int storedAppVersion;
813
814 RestoreRequest(PackageInfo _app, int _version) {
815 app = _app;
816 storedAppVersion = _version;
817 }
818 }
819
Christopher Tate7d562ec2009-06-25 18:03:43 -0700820 PerformRestoreThread(IBackupTransport transport, IRestoreObserver observer,
Dan Egnor156411d2009-06-26 13:20:02 -0700821 long restoreSetToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700822 mTransport = transport;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700823 mObserver = observer;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700824 mToken = restoreSetToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700825
826 try {
827 mStateDir = new File(mBaseStateDir, transport.transportDirName());
828 } catch (RemoteException e) {
829 // can't happen; the transport is local
830 }
831 mStateDir.mkdirs();
Christopher Tatedf01dea2009-06-09 20:45:02 -0700832 }
833
834 @Override
835 public void run() {
Christopher Tate6aa41f42009-06-19 14:14:22 -0700836 if (DEBUG) Log.v(TAG, "Beginning restore process");
Christopher Tatedf01dea2009-06-09 20:45:02 -0700837 /**
838 * Restore sequence:
839 *
Dan Egnorefe52642009-06-24 00:16:33 -0700840 * 1. get the restore set description for our identity
841 * 2. for each app in the restore set:
Christopher Tatedf01dea2009-06-09 20:45:02 -0700842 * 3.a. if it's restorable on this device, add it to the restore queue
Dan Egnorefe52642009-06-24 00:16:33 -0700843 * 3. for each app in the restore queue:
844 * 3.a. clear the app data
845 * 3.b. get the restore data for the app from the transport
846 * 3.c. launch the backup agent for the app
847 * 3.d. agent.doRestore() with the data from the server
848 * 3.e. unbind the agent [and kill the app?]
849 * 4. shut down the transport
Christopher Tatedf01dea2009-06-09 20:45:02 -0700850 */
851
Christopher Tate7d562ec2009-06-25 18:03:43 -0700852 int error = -1; // assume error
853
Dan Egnorefe52642009-06-24 00:16:33 -0700854 // build the set of apps to restore
Christopher Tatedf01dea2009-06-09 20:45:02 -0700855 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700856 RestoreSet[] images = mTransport.getAvailableRestoreSets();
857 if (images == null) {
858 // STOPSHIP TODO: Handle the failure somehow?
859 Log.e(TAG, "Error getting restore sets");
860 return;
861 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700862
Dan Egnorefe52642009-06-24 00:16:33 -0700863 if (images.length == 0) {
864 Log.i(TAG, "No restore sets available");
865 return;
866 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700867
Dan Egnorefe52642009-06-24 00:16:33 -0700868 mImage = images[0];
Christopher Tateabce4e82009-06-18 18:35:32 -0700869
Dan Egnorefe52642009-06-24 00:16:33 -0700870 // Get the list of all packages which have backup enabled.
871 // (Include the Package Manager metadata pseudo-package first.)
872 ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>();
873 PackageInfo omPackage = new PackageInfo();
874 omPackage.packageName = PACKAGE_MANAGER_SENTINEL;
875 restorePackages.add(omPackage);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700876
Dan Egnorefe52642009-06-24 00:16:33 -0700877 List<PackageInfo> agentPackages = allAgentPackages();
878 restorePackages.addAll(agentPackages);
879
Christopher Tate7d562ec2009-06-25 18:03:43 -0700880 // let the observer know that we're running
881 if (mObserver != null) {
882 try {
883 // !!! TODO: get an actual count from the transport after
884 // its startRestore() runs?
885 mObserver.restoreStarting(restorePackages.size());
886 } catch (RemoteException e) {
887 Log.d(TAG, "Restore observer died at restoreStarting");
888 mObserver = null;
889 }
890 }
891
Dan Egnor156411d2009-06-26 13:20:02 -0700892 if (!mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0]))) {
Dan Egnorefe52642009-06-24 00:16:33 -0700893 // STOPSHIP TODO: Handle the failure somehow?
894 Log.e(TAG, "Error starting restore operation");
895 return;
896 }
897
898 String packageName = mTransport.nextRestorePackage();
899 if (packageName == null) {
900 // STOPSHIP TODO: Handle the failure somehow?
901 Log.e(TAG, "Error getting first restore package");
902 return;
903 } else if (packageName.equals("")) {
904 Log.i(TAG, "No restore data available");
905 return;
906 } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
907 Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL
908 + "\", found only \"" + packageName + "\"");
909 return;
910 }
911
912 // Pull the Package Manager metadata from the restore set first
913 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
914 mPackageManager, agentPackages);
915 processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()));
916
Christopher Tate7d562ec2009-06-25 18:03:43 -0700917 int count = 0;
Dan Egnorefe52642009-06-24 00:16:33 -0700918 for (;;) {
919 packageName = mTransport.nextRestorePackage();
920 if (packageName == null) {
921 // STOPSHIP TODO: Handle the failure somehow?
922 Log.e(TAG, "Error getting next restore package");
923 return;
924 } else if (packageName.equals("")) {
925 break;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700926 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700927
Christopher Tate7d562ec2009-06-25 18:03:43 -0700928 if (mObserver != null) {
929 ++count;
930 try {
931 mObserver.onUpdate(count);
932 } catch (RemoteException e) {
933 Log.d(TAG, "Restore observer died in onUpdate");
934 mObserver = null;
935 }
936 }
937
Dan Egnorefe52642009-06-24 00:16:33 -0700938 Metadata metaInfo = pmAgent.getRestoredMetadata(packageName);
939 if (metaInfo == null) {
940 Log.e(TAG, "Missing metadata for " + packageName);
941 continue;
942 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700943
Dan Egnorefe52642009-06-24 00:16:33 -0700944 int flags = PackageManager.GET_SIGNATURES;
945 PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags);
946 if (metaInfo.versionCode > packageInfo.versionCode) {
947 Log.w(TAG, "Package " + packageName
948 + " restore version [" + metaInfo.versionCode
949 + "] is too new for installed version ["
950 + packageInfo.versionCode + "]");
951 continue;
952 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700953
Dan Egnorefe52642009-06-24 00:16:33 -0700954 if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) {
955 Log.w(TAG, "Signature mismatch restoring " + packageName);
956 continue;
957 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700958
Dan Egnorefe52642009-06-24 00:16:33 -0700959 if (DEBUG) Log.v(TAG, "Package " + packageName
960 + " restore version [" + metaInfo.versionCode
961 + "] is compatible with installed version ["
962 + packageInfo.versionCode + "]");
Christopher Tatec7b31e32009-06-10 15:49:30 -0700963
Dan Egnorefe52642009-06-24 00:16:33 -0700964 // Now perform the actual restore
965 clearApplicationDataSynchronous(packageName);
966 IBackupAgent agent = bindToAgentSynchronous(
967 packageInfo.applicationInfo,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700968 IApplicationThread.BACKUP_MODE_RESTORE);
Dan Egnorefe52642009-06-24 00:16:33 -0700969 if (agent == null) {
970 Log.w(TAG, "Can't find backup agent for " + packageName);
971 continue;
Christopher Tatedf01dea2009-06-09 20:45:02 -0700972 }
973
Dan Egnorefe52642009-06-24 00:16:33 -0700974 try {
975 processOneRestore(packageInfo, metaInfo.versionCode, agent);
976 } finally {
977 // unbind even on timeout or failure, just in case
978 mActivityManager.unbindBackupAgent(packageInfo.applicationInfo);
979 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700980 }
Christopher Tate7d562ec2009-06-25 18:03:43 -0700981
982 // if we get this far, report success to the observer
983 error = 0;
Dan Egnorefe52642009-06-24 00:16:33 -0700984 } catch (NameNotFoundException e) {
985 // STOPSHIP TODO: Handle the failure somehow?
986 Log.e(TAG, "Invalid paackage restoring data", e);
987 } catch (RemoteException e) {
988 // STOPSHIP TODO: Handle the failure somehow?
989 Log.e(TAG, "Error restoring data", e);
990 } finally {
991 try {
992 mTransport.finishRestore();
993 } catch (RemoteException e) {
994 Log.e(TAG, "Error finishing restore", e);
995 }
Christopher Tate7d562ec2009-06-25 18:03:43 -0700996
997 if (mObserver != null) {
998 try {
999 mObserver.restoreFinished(error);
1000 } catch (RemoteException e) {
1001 Log.d(TAG, "Restore observer died at restoreFinished");
1002 }
1003 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001004 }
1005 }
1006
Dan Egnorefe52642009-06-24 00:16:33 -07001007 // Do the guts of a restore of one application, using mTransport.getRestoreData().
1008 void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001009 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -07001010 final String packageName = app.packageName;
1011
1012 // !!! TODO: get the dirs from the transport
1013 File backupDataName = new File(mDataDir, packageName + ".restore");
1014 backupDataName.delete();
1015 try {
1016 ParcelFileDescriptor backupData =
1017 ParcelFileDescriptor.open(backupDataName,
1018 ParcelFileDescriptor.MODE_READ_WRITE |
1019 ParcelFileDescriptor.MODE_CREATE);
1020
1021 // Run the transport's restore pass
1022 // Run the target's backup pass
Christopher Tatec7b31e32009-06-10 15:49:30 -07001023 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001024 if (!mTransport.getRestoreData(backupData)) {
1025 // STOPSHIP TODO: Handle this error somehow?
1026 Log.e(TAG, "Error getting restore data for " + packageName);
1027 return;
1028 }
Christopher Tatec7b31e32009-06-10 15:49:30 -07001029 } finally {
1030 backupData.close();
1031 }
1032
1033 // Okay, we have the data. Now have the agent do the restore.
1034 File newStateName = new File(mStateDir, packageName + ".new");
1035 ParcelFileDescriptor newState =
1036 ParcelFileDescriptor.open(newStateName,
1037 ParcelFileDescriptor.MODE_READ_WRITE |
1038 ParcelFileDescriptor.MODE_CREATE);
1039
1040 backupData = ParcelFileDescriptor.open(backupDataName,
1041 ParcelFileDescriptor.MODE_READ_ONLY);
1042
Christopher Tatec7b31e32009-06-10 15:49:30 -07001043 try {
Dan Egnorefe52642009-06-24 00:16:33 -07001044 agent.doRestore(backupData, appVersionCode, newState);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001045 } finally {
1046 newState.close();
1047 backupData.close();
1048 }
1049
1050 // if everything went okay, remember the recorded state now
Dan Egnorefe52642009-06-24 00:16:33 -07001051 File savedStateName = new File(mStateDir, packageName);
1052 newStateName.renameTo(savedStateName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001053 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -07001054 Log.e(TAG, "Error restoring data for " + packageName, e);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001055 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001056 }
1057 }
1058
1059
Christopher Tate487529a2009-04-29 14:03:25 -07001060 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -07001061
Christopher Tatea8bf8152009-04-30 11:36:21 -07001062 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -07001063 // Record that we need a backup pass for the caller. Since multiple callers
1064 // may share a uid, we need to note all candidates within that uid and schedule
1065 // a backup pass for each of them.
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001066
1067 Log.d(TAG, "dataChanged packageName=" + packageName);
Christopher Tate63d27002009-06-16 17:16:42 -07001068
1069 // If the caller does not hold the BACKUP permission, it can only request a
1070 // backup of its own data.
1071 HashSet<ApplicationInfo> targets;
1072 if ((mContext.checkPermission("android.permission.BACKUP", Binder.getCallingPid(),
1073 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1074 targets = mBackupParticipants.get(Binder.getCallingUid());
1075 } else {
1076 // a caller with full permission can ask to back up any participating app
1077 // !!! TODO: allow backup of ANY app?
1078 if (DEBUG) Log.v(TAG, "Privileged caller, allowing backup of other apps");
1079 targets = new HashSet<ApplicationInfo>();
1080 int N = mBackupParticipants.size();
1081 for (int i = 0; i < N; i++) {
1082 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1083 if (s != null) {
1084 targets.addAll(s);
1085 }
1086 }
1087 }
Christopher Tate487529a2009-04-29 14:03:25 -07001088 if (targets != null) {
1089 synchronized (mQueueLock) {
1090 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -07001091 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -07001092 // validate the caller-supplied package name against the known set of
1093 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -07001094 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -04001095 // Add the caller to the set of pending backups. If there is
1096 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -07001097 BackupRequest req = new BackupRequest(app, false);
1098 mPendingBackups.put(app, req);
Christopher Tatecde87f42009-06-12 12:55:53 -07001099
1100 // Journal this request in case of crash
1101 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001102 }
1103 }
1104
Christopher Tate181fafa2009-05-14 11:12:14 -07001105 if (DEBUG) {
1106 int numKeys = mPendingBackups.size();
Christopher Tate6ef58a12009-06-29 14:56:28 -07001107 Log.d(TAG, "Now awaiting backup for " + numKeys + " participants:");
Christopher Tate181fafa2009-05-14 11:12:14 -07001108 for (BackupRequest b : mPendingBackups.values()) {
1109 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
1110 }
1111 }
Christopher Tate487529a2009-04-29 14:03:25 -07001112 // Schedule a backup pass in a few minutes. As backup-eligible data
1113 // keeps changing, continue to defer the backup pass until things
1114 // settle down, to avoid extra overhead.
Christopher Tate6785dd82009-06-18 15:58:25 -07001115 scheduleBackupPassLocked(COLLECTION_INTERVAL);
Christopher Tate487529a2009-04-29 14:03:25 -07001116 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001117 } else {
1118 Log.w(TAG, "dataChanged but no participant pkg " + packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001119 }
1120 }
Christopher Tate46758122009-05-06 11:22:00 -07001121
Christopher Tatecde87f42009-06-12 12:55:53 -07001122 private void writeToJournalLocked(String str) {
1123 if (mJournalStream != null) {
1124 try {
1125 mJournalStream.writeUTF(str);
1126 } catch (IOException e) {
1127 Log.e(TAG, "Error writing to backup journal");
1128 mJournalStream = null;
1129 mJournal = null;
1130 }
1131 }
1132 }
1133
Christopher Tateace7f092009-06-15 18:07:25 -07001134 // Run a backup pass immediately for any applications that have declared
1135 // that they have pending updates.
1136 public void backupNow() throws RemoteException {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001137 mContext.enforceCallingPermission("android.permission.BACKUP", "backupNow");
Christopher Tate043dadc2009-06-02 16:11:00 -07001138
Christopher Tateace7f092009-06-15 18:07:25 -07001139 if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass");
Christopher Tate46758122009-05-06 11:22:00 -07001140 synchronized (mQueueLock) {
Christopher Tate6785dd82009-06-18 15:58:25 -07001141 scheduleBackupPassLocked(0);
Christopher Tate46758122009-05-06 11:22:00 -07001142 }
1143 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001144
Christopher Tate6ef58a12009-06-29 14:56:28 -07001145 // Enable/disable the backup transport
1146 public void setBackupEnabled(boolean enable) {
1147 mContext.enforceCallingPermission("android.permission.BACKUP", "setBackupEnabled");
1148
1149 boolean wasEnabled = mEnabled;
1150 synchronized (this) {
1151 SystemProperties.set(BACKUP_ENABLED_PROPERTY, enable ? "true" : "false");
1152 mEnabled = enable;
1153 }
1154
1155 if (enable && !wasEnabled) {
1156 synchronized (mQueueLock) {
1157 if (mPendingBackups.size() > 0) {
1158 // !!! TODO: better policy around timing of the first backup pass
1159 if (DEBUG) Log.v(TAG, "Backup enabled with pending data changes, scheduling");
1160 this.scheduleBackupPassLocked(COLLECTION_INTERVAL);
1161 }
1162 }
1163 }
1164}
1165
1166 // Report whether the backup mechanism is currently enabled
1167 public boolean isBackupEnabled() {
1168 mContext.enforceCallingPermission("android.permission.BACKUP", "isBackupEnabled");
1169 return mEnabled; // no need to synchronize just to read it
1170 }
1171
Christopher Tate91717492009-06-26 21:07:13 -07001172 // Report the name of the currently active transport
1173 public String getCurrentTransport() {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001174 mContext.enforceCallingPermission("android.permission.BACKUP", "getCurrentTransport");
Christopher Tate91717492009-06-26 21:07:13 -07001175 Log.v(TAG, "getCurrentTransport() returning " + mCurrentTransport);
1176 return mCurrentTransport;
Christopher Tateace7f092009-06-15 18:07:25 -07001177 }
1178
Christopher Tate91717492009-06-26 21:07:13 -07001179 // Report all known, available backup transports
1180 public String[] listAllTransports() {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001181 mContext.enforceCallingPermission("android.permission.BACKUP", "listAllTransports");
Christopher Tate043dadc2009-06-02 16:11:00 -07001182
Christopher Tate91717492009-06-26 21:07:13 -07001183 String[] list = null;
1184 ArrayList<String> known = new ArrayList<String>();
1185 for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) {
1186 if (entry.getValue() != null) {
1187 known.add(entry.getKey());
1188 }
1189 }
1190
1191 if (known.size() > 0) {
1192 list = new String[known.size()];
1193 known.toArray(list);
1194 }
1195 return list;
1196 }
1197
1198 // Select which transport to use for the next backup operation. If the given
1199 // name is not one of the available transports, no action is taken and the method
1200 // returns null.
1201 public String selectBackupTransport(String transport) {
1202 mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
1203
1204 synchronized (mTransports) {
1205 String prevTransport = null;
1206 if (mTransports.get(transport) != null) {
1207 prevTransport = mCurrentTransport;
1208 mCurrentTransport = transport;
1209 SystemProperties.set(BACKUP_TRANSPORT_PROPERTY, transport);
1210 Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport
1211 + " returning " + prevTransport);
1212 } else {
1213 Log.w(TAG, "Attempt to select unavailable transport " + transport);
1214 }
1215 return prevTransport;
1216 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001217 }
1218
1219 // Callback: a requested backup agent has been instantiated. This should only
1220 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001221 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -07001222 synchronized(mAgentConnectLock) {
1223 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1224 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
1225 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
1226 mConnectedAgent = agent;
1227 mConnecting = false;
1228 } else {
1229 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1230 + " claiming agent connected");
1231 }
1232 mAgentConnectLock.notifyAll();
1233 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001234 }
1235
1236 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
1237 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -07001238 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001239 public void agentDisconnected(String packageName) {
1240 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -07001241 synchronized(mAgentConnectLock) {
1242 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1243 mConnectedAgent = null;
1244 mConnecting = false;
1245 } else {
1246 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1247 + " claiming agent disconnected");
1248 }
1249 mAgentConnectLock.notifyAll();
1250 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001251 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001252
Christopher Tate8c850b72009-06-07 19:33:20 -07001253 // Hand off a restore session
Christopher Tate91717492009-06-26 21:07:13 -07001254 public IRestoreSession beginRestoreSession(String transport) {
Christopher Tate8c850b72009-06-07 19:33:20 -07001255 mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession");
Christopher Tatef68eb502009-06-16 11:02:01 -07001256
1257 synchronized(this) {
1258 if (mActiveRestoreSession != null) {
1259 Log.d(TAG, "Restore session requested but one already active");
1260 return null;
1261 }
Christopher Tate91717492009-06-26 21:07:13 -07001262 mActiveRestoreSession = new RestoreSession(transport);
Christopher Tatef68eb502009-06-16 11:02:01 -07001263 }
1264 return mActiveRestoreSession;
Christopher Tate8c850b72009-06-07 19:33:20 -07001265 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001266
Christopher Tate9b3905c2009-06-08 15:24:01 -07001267 // ----- Restore session -----
1268
1269 class RestoreSession extends IRestoreSession.Stub {
Christopher Tatef68eb502009-06-16 11:02:01 -07001270 private static final String TAG = "RestoreSession";
1271
Christopher Tate9b3905c2009-06-08 15:24:01 -07001272 private IBackupTransport mRestoreTransport = null;
1273 RestoreSet[] mRestoreSets = null;
1274
Christopher Tate91717492009-06-26 21:07:13 -07001275 RestoreSession(String transport) {
1276 mRestoreTransport = getTransport(transport);
Christopher Tate9b3905c2009-06-08 15:24:01 -07001277 }
1278
1279 // --- Binder interface ---
1280 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001281 mContext.enforceCallingPermission("android.permission.BACKUP",
1282 "getAvailableRestoreSets");
1283
Christopher Tatef68eb502009-06-16 11:02:01 -07001284 try {
Christopher Tate9b3905c2009-06-08 15:24:01 -07001285 synchronized(this) {
1286 if (mRestoreSets == null) {
1287 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
1288 }
1289 return mRestoreSets;
1290 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001291 } catch (RuntimeException e) {
1292 Log.d(TAG, "getAvailableRestoreSets exception");
1293 e.printStackTrace();
1294 throw e;
1295 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001296 }
1297
Dan Egnor156411d2009-06-26 13:20:02 -07001298 public int performRestore(long token, IRestoreObserver observer)
Christopher Tate7d562ec2009-06-25 18:03:43 -07001299 throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001300 mContext.enforceCallingPermission("android.permission.BACKUP", "performRestore");
1301
1302 if (mRestoreSets != null) {
1303 for (int i = 0; i < mRestoreSets.length; i++) {
1304 if (token == mRestoreSets[i].token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -07001305 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
Dan Egnor156411d2009-06-26 13:20:02 -07001306 msg.obj = new RestoreParams(mRestoreTransport, observer, token);
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001307 mBackupHandler.sendMessage(msg);
1308 return 0;
1309 }
1310 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001311 } else {
1312 if (DEBUG) Log.v(TAG, "No current restore set, not doing restore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001313 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001314 return -1;
1315 }
1316
1317 public void endRestoreSession() throws android.os.RemoteException {
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001318 mContext.enforceCallingPermission("android.permission.BACKUP",
1319 "endRestoreSession");
1320
Dan Egnorefe52642009-06-24 00:16:33 -07001321 mRestoreTransport.finishRestore();
Christopher Tate9b3905c2009-06-08 15:24:01 -07001322 mRestoreTransport = null;
Christopher Tatef68eb502009-06-16 11:02:01 -07001323 synchronized(BackupManagerService.this) {
1324 if (BackupManagerService.this.mActiveRestoreSession == this) {
1325 BackupManagerService.this.mActiveRestoreSession = null;
1326 } else {
1327 Log.e(TAG, "ending non-current restore session");
1328 }
1329 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001330 }
1331 }
1332
Christopher Tate043dadc2009-06-02 16:11:00 -07001333
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001334 @Override
1335 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1336 synchronized (mQueueLock) {
Christopher Tate91717492009-06-26 21:07:13 -07001337 pw.println("Available transports:");
1338 for (String t : listAllTransports()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001339 String pad = (t.equals(mCurrentTransport)) ? " * " : " ";
1340 pw.println(pad + t);
Christopher Tate91717492009-06-26 21:07:13 -07001341 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001342 int N = mBackupParticipants.size();
1343 pw.println("Participants:");
1344 for (int i=0; i<N; i++) {
1345 int uid = mBackupParticipants.keyAt(i);
1346 pw.print(" uid: ");
1347 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07001348 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
1349 for (ApplicationInfo app: participants) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001350 pw.println(" " + app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001351 }
1352 }
Christopher Tate6aa41f42009-06-19 14:14:22 -07001353 pw.println("Pending: " + mPendingBackups.size());
1354 for (BackupRequest req : mPendingBackups.values()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001355 pw.println(" " + req);
Christopher Tate181fafa2009-05-14 11:12:14 -07001356 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001357 }
1358 }
Christopher Tate487529a2009-04-29 14:03:25 -07001359}