blob: a8e5f8bf637e1a236e7570139599de116f167d8c [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;
Christopher Tateb6787f22009-07-02 17:40:45 -070020import android.app.AlarmManager;
Christopher Tate181fafa2009-05-14 11:12:14 -070021import android.app.IActivityManager;
22import android.app.IApplicationThread;
23import android.app.IBackupAgent;
Christopher Tateb6787f22009-07-02 17:40:45 -070024import android.app.PendingIntent;
Christopher Tate3799bc22009-05-06 16:13:56 -070025import android.content.BroadcastReceiver;
Dan Egnor87a02bc2009-06-17 02:30:10 -070026import android.content.ComponentName;
Christopher Tate487529a2009-04-29 14:03:25 -070027import android.content.Context;
28import android.content.Intent;
Christopher Tate3799bc22009-05-06 16:13:56 -070029import android.content.IntentFilter;
Dan Egnor87a02bc2009-06-17 02:30:10 -070030import android.content.ServiceConnection;
Christopher Tate181fafa2009-05-14 11:12:14 -070031import android.content.pm.ApplicationInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -070032import android.content.pm.IPackageDataObserver;
Christopher Tate7b881282009-06-07 13:52:37 -070033import android.content.pm.PackageInfo;
Christopher Tate043dadc2009-06-02 16:11:00 -070034import android.content.pm.PackageManager.NameNotFoundException;
Dan Egnor87a02bc2009-06-17 02:30:10 -070035import android.content.pm.PackageManager;
Christopher Tateabce4e82009-06-18 18:35:32 -070036import android.content.pm.Signature;
Christopher Tate3799bc22009-05-06 16:13:56 -070037import android.net.Uri;
Christopher Tatece0bf062009-07-01 11:43:53 -070038import android.provider.Settings;
Christopher Tate487529a2009-04-29 14:03:25 -070039import android.os.Binder;
Christopher Tate3799bc22009-05-06 16:13:56 -070040import android.os.Bundle;
Christopher Tate22b87872009-05-04 16:41:53 -070041import android.os.Environment;
Christopher Tate487529a2009-04-29 14:03:25 -070042import android.os.Handler;
43import android.os.IBinder;
44import android.os.Message;
Christopher Tate22b87872009-05-04 16:41:53 -070045import android.os.ParcelFileDescriptor;
Christopher Tateb6787f22009-07-02 17:40:45 -070046import android.os.PowerManager;
Christopher Tate043dadc2009-06-02 16:11:00 -070047import android.os.Process;
Christopher Tate487529a2009-04-29 14:03:25 -070048import android.os.RemoteException;
Dan Egnorbb9001c2009-07-27 12:20:13 -070049import android.os.SystemClock;
50import android.util.EventLog;
Christopher Tate487529a2009-04-29 14:03:25 -070051import android.util.Log;
52import android.util.SparseArray;
53
54import android.backup.IBackupManager;
Christopher Tate7d562ec2009-06-25 18:03:43 -070055import android.backup.IRestoreObserver;
Christopher Tate8c850b72009-06-07 19:33:20 -070056import android.backup.IRestoreSession;
Christopher Tate9b3905c2009-06-08 15:24:01 -070057import android.backup.RestoreSet;
Christopher Tate043dadc2009-06-02 16:11:00 -070058
Christopher Tate9bbc21a2009-06-10 20:23:25 -070059import com.android.internal.backup.LocalTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -070060import com.android.internal.backup.IBackupTransport;
Christopher Tate487529a2009-04-29 14:03:25 -070061
Christopher Tate6785dd82009-06-18 15:58:25 -070062import com.android.server.PackageManagerBackupAgent;
Christopher Tate6aa41f42009-06-19 14:14:22 -070063import com.android.server.PackageManagerBackupAgent.Metadata;
Christopher Tate6785dd82009-06-18 15:58:25 -070064
Christopher Tatecde87f42009-06-12 12:55:53 -070065import java.io.EOFException;
Christopher Tate22b87872009-05-04 16:41:53 -070066import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070067import java.io.FileDescriptor;
Christopher Tatec7b31e32009-06-10 15:49:30 -070068import java.io.IOException;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070069import java.io.PrintWriter;
Christopher Tatecde87f42009-06-12 12:55:53 -070070import java.io.RandomAccessFile;
Christopher Tate487529a2009-04-29 14:03:25 -070071import java.lang.String;
Joe Onorato8ad02812009-05-13 01:41:44 -040072import java.util.ArrayList;
73import java.util.HashMap;
Christopher Tate487529a2009-04-29 14:03:25 -070074import java.util.HashSet;
75import java.util.List;
Christopher Tate91717492009-06-26 21:07:13 -070076import java.util.Map;
Christopher Tate487529a2009-04-29 14:03:25 -070077
78class BackupManagerService extends IBackupManager.Stub {
79 private static final String TAG = "BackupManagerService";
80 private static final boolean DEBUG = true;
Christopher Tateaa088442009-06-16 18:25:46 -070081
Christopher Tate49401dd2009-07-01 12:34:29 -070082 // How often we perform a backup pass. Privileged external callers can
83 // trigger an immediate pass.
Christopher Tateb6787f22009-07-02 17:40:45 -070084 private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR;
Christopher Tate487529a2009-04-29 14:03:25 -070085
Christopher Tate8031a3d2009-07-06 16:36:05 -070086 // The amount of time between the initial provisioning of the device and
87 // the first backup pass.
88 private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR;
89
Christopher Tateb6787f22009-07-02 17:40:45 -070090 private static final String RUN_BACKUP_ACTION = "_backup_run_";
Christopher Tate487529a2009-04-29 14:03:25 -070091 private static final int MSG_RUN_BACKUP = 1;
Christopher Tate043dadc2009-06-02 16:11:00 -070092 private static final int MSG_RUN_FULL_BACKUP = 2;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070093 private static final int MSG_RUN_RESTORE = 3;
Christopher Tateee0e78a2009-07-02 11:17:03 -070094 private static final int MSG_RUN_CLEAR = 4;
Christopher Tatec7b31e32009-06-10 15:49:30 -070095
Dan Egnorbb9001c2009-07-27 12:20:13 -070096 // Event tags -- see system/core/logcat/event-log-tags
97 private static final int BACKUP_DATA_CHANGED_EVENT = 2820;
98 private static final int BACKUP_START_EVENT = 2821;
99 private static final int BACKUP_TRANSPORT_FAILURE_EVENT = 2822;
100 private static final int BACKUP_AGENT_FAILURE_EVENT = 2823;
101 private static final int BACKUP_PACKAGE_EVENT = 2824;
102 private static final int BACKUP_SUCCESS_EVENT = 2825;
103
104 private static final int RESTORE_START_EVENT = 2830;
105 private static final int RESTORE_TRANSPORT_FAILURE_EVENT = 2831;
106 private static final int RESTORE_AGENT_FAILURE_EVENT = 2832;
107 private static final int RESTORE_PACKAGE_EVENT = 2833;
108 private static final int RESTORE_SUCCESS_EVENT = 2834;
109
Christopher Tatec7b31e32009-06-10 15:49:30 -0700110 // Timeout interval for deciding that a bind or clear-data has taken too long
111 static final long TIMEOUT_INTERVAL = 10 * 1000;
112
Christopher Tate487529a2009-04-29 14:03:25 -0700113 private Context mContext;
114 private PackageManager mPackageManager;
Christopher Tate6ef58a12009-06-29 14:56:28 -0700115 private IActivityManager mActivityManager;
Christopher Tateb6787f22009-07-02 17:40:45 -0700116 private PowerManager mPowerManager;
117 private AlarmManager mAlarmManager;
118
Christopher Tate73e02522009-07-15 14:18:26 -0700119 boolean mEnabled; // access to this is synchronized on 'this'
120 boolean mProvisioned;
121 PowerManager.WakeLock mWakelock;
122 final BackupHandler mBackupHandler = new BackupHandler();
123 PendingIntent mRunBackupIntent;
124 BroadcastReceiver mRunBackupReceiver;
125 IntentFilter mRunBackupFilter;
Christopher Tate487529a2009-04-29 14:03:25 -0700126 // map UIDs to the set of backup client services within that UID's app set
Christopher Tate73e02522009-07-15 14:18:26 -0700127 final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants
Christopher Tate181fafa2009-05-14 11:12:14 -0700128 = new SparseArray<HashSet<ApplicationInfo>>();
Christopher Tate487529a2009-04-29 14:03:25 -0700129 // set of backup services that have pending changes
Christopher Tate73e02522009-07-15 14:18:26 -0700130 class BackupRequest {
Christopher Tate181fafa2009-05-14 11:12:14 -0700131 public ApplicationInfo appInfo;
Christopher Tate46758122009-05-06 11:22:00 -0700132 public boolean fullBackup;
Christopher Tateaa088442009-06-16 18:25:46 -0700133
Christopher Tate181fafa2009-05-14 11:12:14 -0700134 BackupRequest(ApplicationInfo app, boolean isFull) {
135 appInfo = app;
Christopher Tate46758122009-05-06 11:22:00 -0700136 fullBackup = isFull;
137 }
Christopher Tate181fafa2009-05-14 11:12:14 -0700138
139 public String toString() {
140 return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}";
141 }
Christopher Tate46758122009-05-06 11:22:00 -0700142 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400143 // Backups that we haven't started yet.
Christopher Tate73e02522009-07-15 14:18:26 -0700144 HashMap<ApplicationInfo,BackupRequest> mPendingBackups
Christopher Tate181fafa2009-05-14 11:12:14 -0700145 = new HashMap<ApplicationInfo,BackupRequest>();
Christopher Tate5cb400b2009-06-25 16:03:14 -0700146
147 // Pseudoname that we use for the Package Manager metadata "package"
Christopher Tate73e02522009-07-15 14:18:26 -0700148 static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
Christopher Tate6aa41f42009-06-19 14:14:22 -0700149
150 // locking around the pending-backup management
Christopher Tate73e02522009-07-15 14:18:26 -0700151 final Object mQueueLock = new Object();
Christopher Tate487529a2009-04-29 14:03:25 -0700152
Christopher Tate043dadc2009-06-02 16:11:00 -0700153 // The thread performing the sequence of queued backups binds to each app's agent
154 // in succession. Bind notifications are asynchronously delivered through the
155 // Activity Manager; use this lock object to signal when a requested binding has
156 // completed.
Christopher Tate73e02522009-07-15 14:18:26 -0700157 final Object mAgentConnectLock = new Object();
158 IBackupAgent mConnectedAgent;
159 volatile boolean mConnecting;
Christopher Tate043dadc2009-06-02 16:11:00 -0700160
Christopher Tatec7b31e32009-06-10 15:49:30 -0700161 // A similar synchronicity mechanism around clearing apps' data for restore
Christopher Tate73e02522009-07-15 14:18:26 -0700162 final Object mClearDataLock = new Object();
163 volatile boolean mClearingData;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700164
Christopher Tate91717492009-06-26 21:07:13 -0700165 // Transport bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700166 final HashMap<String,IBackupTransport> mTransports
Christopher Tate91717492009-06-26 21:07:13 -0700167 = new HashMap<String,IBackupTransport>();
Christopher Tate73e02522009-07-15 14:18:26 -0700168 String mCurrentTransport;
169 IBackupTransport mLocalTransport, mGoogleTransport;
170 RestoreSession mActiveRestoreSession;
Christopher Tate043dadc2009-06-02 16:11:00 -0700171
Christopher Tate73e02522009-07-15 14:18:26 -0700172 class RestoreParams {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700173 public IBackupTransport transport;
174 public IRestoreObserver observer;
Dan Egnor156411d2009-06-26 13:20:02 -0700175 public long token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700176
Dan Egnor156411d2009-06-26 13:20:02 -0700177 RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700178 transport = _transport;
179 observer = _obs;
Dan Egnor156411d2009-06-26 13:20:02 -0700180 token = _token;
Christopher Tate7d562ec2009-06-25 18:03:43 -0700181 }
182 }
183
Christopher Tate73e02522009-07-15 14:18:26 -0700184 class ClearParams {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700185 public IBackupTransport transport;
186 public PackageInfo packageInfo;
187
188 ClearParams(IBackupTransport _transport, PackageInfo _info) {
189 transport = _transport;
190 packageInfo = _info;
191 }
192 }
193
Christopher Tate5cb400b2009-06-25 16:03:14 -0700194 // Where we keep our journal files and other bookkeeping
Christopher Tate73e02522009-07-15 14:18:26 -0700195 File mBaseStateDir;
196 File mDataDir;
197 File mJournalDir;
198 File mJournal;
199 RandomAccessFile mJournalStream;
200
201 // Keep a log of all the apps we've ever backed up
202 private File mEverStored;
203 private RandomAccessFile mEverStoredStream;
204 HashSet<String> mEverStoredApps = new HashSet<String>();
205
Christopher Tateaa088442009-06-16 18:25:46 -0700206
Christopher Tate487529a2009-04-29 14:03:25 -0700207 public BackupManagerService(Context context) {
208 mContext = context;
209 mPackageManager = context.getPackageManager();
Christopher Tate181fafa2009-05-14 11:12:14 -0700210 mActivityManager = ActivityManagerNative.getDefault();
Christopher Tate487529a2009-04-29 14:03:25 -0700211
Christopher Tateb6787f22009-07-02 17:40:45 -0700212 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
213 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
214
Christopher Tate22b87872009-05-04 16:41:53 -0700215 // Set up our bookkeeping
Christopher Tateb6787f22009-07-02 17:40:45 -0700216 boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(),
Dianne Hackborncf098292009-07-01 19:55:20 -0700217 Settings.Secure.BACKUP_ENABLED, 0) != 0;
Christopher Tate8031a3d2009-07-06 16:36:05 -0700218 // !!! TODO: mProvisioned needs to default to 0, not 1.
219 mProvisioned = Settings.Secure.getInt(context.getContentResolver(),
Joe Onoratoab9a2a52009-07-27 08:56:39 -0700220 Settings.Secure.BACKUP_PROVISIONED, 0) != 0;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700221 mBaseStateDir = new File(Environment.getDataDirectory(), "backup");
Christopher Tatef4172472009-05-05 15:50:03 -0700222 mDataDir = Environment.getDownloadCacheDirectory();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700223
Christopher Tateb6787f22009-07-02 17:40:45 -0700224 mRunBackupReceiver = new RunBackupReceiver();
225 mRunBackupFilter = new IntentFilter();
226 mRunBackupFilter.addAction(RUN_BACKUP_ACTION);
227 context.registerReceiver(mRunBackupReceiver, mRunBackupFilter);
228
229 Intent backupIntent = new Intent(RUN_BACKUP_ACTION);
230 // !!! TODO: restrict delivery to our receiver; the naive setClass() doesn't seem to work
231 //backupIntent.setClass(context, mRunBackupReceiver.getClass());
232 backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
233 mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0);
234
Christopher Tatecde87f42009-06-12 12:55:53 -0700235 // Set up the backup-request journaling
Christopher Tate5cb400b2009-06-25 16:03:14 -0700236 mJournalDir = new File(mBaseStateDir, "pending");
237 mJournalDir.mkdirs(); // creates mBaseStateDir along the way
Christopher Tatecde87f42009-06-12 12:55:53 -0700238 makeJournalLocked(); // okay because no other threads are running yet
239
Christopher Tate73e02522009-07-15 14:18:26 -0700240 // Set up the various sorts of package tracking we do
241 initPackageTracking();
242
Christopher Tateabce4e82009-06-18 18:35:32 -0700243 // Build our mapping of uid to backup client services. This implicitly
244 // schedules a backup pass on the Package Manager metadata the first
245 // time anything needs to be backed up.
Christopher Tate3799bc22009-05-06 16:13:56 -0700246 synchronized (mBackupParticipants) {
247 addPackageParticipantsLocked(null);
Christopher Tate487529a2009-04-29 14:03:25 -0700248 }
249
Dan Egnor87a02bc2009-06-17 02:30:10 -0700250 // Set up our transport options and initialize the default transport
251 // TODO: Have transports register themselves somehow?
252 // TODO: Don't create transports that we don't need to?
Dan Egnor87a02bc2009-06-17 02:30:10 -0700253 mLocalTransport = new LocalTransport(context); // This is actually pretty cheap
Christopher Tate91717492009-06-26 21:07:13 -0700254 ComponentName localName = new ComponentName(context, LocalTransport.class);
255 registerTransport(localName.flattenToShortString(), mLocalTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700256
Christopher Tate91717492009-06-26 21:07:13 -0700257 mGoogleTransport = null;
Dianne Hackborncf098292009-07-01 19:55:20 -0700258 mCurrentTransport = Settings.Secure.getString(context.getContentResolver(),
259 Settings.Secure.BACKUP_TRANSPORT);
260 if ("".equals(mCurrentTransport)) {
261 mCurrentTransport = null;
Christopher Tatece0bf062009-07-01 11:43:53 -0700262 }
Christopher Tate91717492009-06-26 21:07:13 -0700263 if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport);
264
265 // Attach to the Google backup transport. When this comes up, it will set
266 // itself as the current transport because we explicitly reset mCurrentTransport
267 // to null.
Dan Egnor87a02bc2009-06-17 02:30:10 -0700268 Intent intent = new Intent().setComponent(new ComponentName(
269 "com.google.android.backup",
270 "com.google.android.backup.BackupTransportService"));
271 context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE);
Christopher Tateaa088442009-06-16 18:25:46 -0700272
Christopher Tatecde87f42009-06-12 12:55:53 -0700273 // Now that we know about valid backup participants, parse any
Christopher Tate49401dd2009-07-01 12:34:29 -0700274 // leftover journal files into the pending backup set
Christopher Tatecde87f42009-06-12 12:55:53 -0700275 parseLeftoverJournals();
276
Christopher Tateb6787f22009-07-02 17:40:45 -0700277 // Power management
278 mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backup");
279
280 // Start the backup passes going
281 setBackupEnabled(areEnabled);
282 }
283
284 private class RunBackupReceiver extends BroadcastReceiver {
285 public void onReceive(Context context, Intent intent) {
286 if (RUN_BACKUP_ACTION.equals(intent.getAction())) {
287 if (DEBUG) Log.v(TAG, "Running a backup pass");
288
289 synchronized (mQueueLock) {
290 // acquire a wakelock and pass it to the backup thread. it will
291 // be released once backup concludes.
292 mWakelock.acquire();
293
294 Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
295 mBackupHandler.sendMessage(msg);
296 }
297 }
Christopher Tate49401dd2009-07-01 12:34:29 -0700298 }
Christopher Tateb6787f22009-07-02 17:40:45 -0700299 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700300
Christopher Tate73e02522009-07-15 14:18:26 -0700301 private void initPackageTracking() {
302 if (DEBUG) Log.v(TAG, "Initializing package tracking");
303
Christopher Tatee97e8072009-07-15 16:45:50 -0700304 // Keep a log of what apps we've ever backed up. Because we might have
305 // rebooted in the middle of an operation that was removing something from
306 // this log, we sanity-check its contents here and reconstruct it.
Christopher Tate73e02522009-07-15 14:18:26 -0700307 mEverStored = new File(mBaseStateDir, "processed");
Christopher Tatee97e8072009-07-15 16:45:50 -0700308 File tempProcessedFile = new File(mBaseStateDir, "processed.new");
Christopher Tate73e02522009-07-15 14:18:26 -0700309 try {
Christopher Tatee97e8072009-07-15 16:45:50 -0700310 RandomAccessFile temp = new RandomAccessFile(tempProcessedFile, "rw");
311 mEverStoredStream = new RandomAccessFile(mEverStored, "r");
Christopher Tate73e02522009-07-15 14:18:26 -0700312
313 // parse its existing contents
314 mEverStoredStream.seek(0);
Christopher Tatee97e8072009-07-15 16:45:50 -0700315 temp.seek(0);
Christopher Tate73e02522009-07-15 14:18:26 -0700316 try {
317 while (true) {
Christopher Tatee97e8072009-07-15 16:45:50 -0700318 PackageInfo info;
Christopher Tate73e02522009-07-15 14:18:26 -0700319 String pkg = mEverStoredStream.readUTF();
Christopher Tatee97e8072009-07-15 16:45:50 -0700320 try {
321 info = mPackageManager.getPackageInfo(pkg, 0);
322 mEverStoredApps.add(pkg);
323 temp.writeUTF(pkg);
324 if (DEBUG) Log.v(TAG, " + " + pkg);
325 } catch (NameNotFoundException e) {
326 // nope, this package was uninstalled; don't include it
327 if (DEBUG) Log.v(TAG, " - " + pkg);
328 }
Christopher Tate73e02522009-07-15 14:18:26 -0700329 }
330 } catch (EOFException e) {
331 // now we're at EOF
332 }
Christopher Tatee97e8072009-07-15 16:45:50 -0700333
334 // Once we've rewritten the backup history log, atomically replace the
335 // old one with the new one then reopen the file for continuing use.
336 temp.close();
337 mEverStoredStream.close();
338 tempProcessedFile.renameTo(mEverStored);
339 mEverStoredStream = new RandomAccessFile(mEverStored, "rwd");
Christopher Tate73e02522009-07-15 14:18:26 -0700340 } catch (IOException e) {
341 Log.e(TAG, "Unable to open known-stored file!");
342 mEverStoredStream = null;
343 }
344
Christopher Tatee97e8072009-07-15 16:45:50 -0700345 // If we were in the middle of removing something from the ever-backed-up
346 // file, there might be a transient "processed.new" file still present.
347 // We've reconstructed a coherent state at this point though, so we can
348 // safely discard that file now.
349 if (tempProcessedFile.exists()) {
350 tempProcessedFile.delete();
351 }
352
Christopher Tate73e02522009-07-15 14:18:26 -0700353 // Register for broadcasts about package install, etc., so we can
354 // update the provider list.
355 IntentFilter filter = new IntentFilter();
356 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
357 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
358 filter.addDataScheme("package");
359 mContext.registerReceiver(mBroadcastReceiver, filter);
360 }
361
Christopher Tatecde87f42009-06-12 12:55:53 -0700362 private void makeJournalLocked() {
363 try {
364 mJournal = File.createTempFile("journal", null, mJournalDir);
365 mJournalStream = new RandomAccessFile(mJournal, "rwd");
366 } catch (IOException e) {
367 Log.e(TAG, "Unable to write backup journals");
368 mJournal = null;
369 mJournalStream = null;
370 }
371 }
372
373 private void parseLeftoverJournals() {
374 if (mJournal != null) {
375 File[] allJournals = mJournalDir.listFiles();
376 for (File f : allJournals) {
377 if (f.compareTo(mJournal) != 0) {
378 // This isn't the current journal, so it must be a leftover. Read
379 // out the package names mentioned there and schedule them for
380 // backup.
381 try {
382 Log.i(TAG, "Found stale backup journal, scheduling:");
383 RandomAccessFile in = new RandomAccessFile(f, "r");
384 while (true) {
385 String packageName = in.readUTF();
386 Log.i(TAG, " + " + packageName);
387 dataChanged(packageName);
388 }
389 } catch (EOFException e) {
390 // no more data; we're done
391 } catch (Exception e) {
392 // can't read it or other error; just skip it
393 } finally {
394 // close/delete the file
395 f.delete();
396 }
397 }
398 }
399 }
400 }
401
Christopher Tate91717492009-06-26 21:07:13 -0700402 // Add a transport to our set of available backends
403 private void registerTransport(String name, IBackupTransport transport) {
404 synchronized (mTransports) {
Christopher Tate34ebd0e2009-07-06 15:44:54 -0700405 if (DEBUG) Log.v(TAG, "Registering transport " + name + " = " + transport);
Christopher Tate91717492009-06-26 21:07:13 -0700406 mTransports.put(name, transport);
407 }
408 }
409
Christopher Tate3799bc22009-05-06 16:13:56 -0700410 // ----- Track installation/removal of packages -----
411 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
412 public void onReceive(Context context, Intent intent) {
413 if (DEBUG) Log.d(TAG, "Received broadcast " + intent);
414
415 Uri uri = intent.getData();
416 if (uri == null) {
417 return;
Christopher Tate487529a2009-04-29 14:03:25 -0700418 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700419 String pkgName = uri.getSchemeSpecificPart();
420 if (pkgName == null) {
421 return;
422 }
423
424 String action = intent.getAction();
425 if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
426 synchronized (mBackupParticipants) {
427 Bundle extras = intent.getExtras();
428 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
429 // The package was just upgraded
430 updatePackageParticipantsLocked(pkgName);
431 } else {
432 // The package was just added
433 addPackageParticipantsLocked(pkgName);
434 }
435 }
436 }
437 else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
438 Bundle extras = intent.getExtras();
439 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
440 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
441 } else {
442 synchronized (mBackupParticipants) {
443 removePackageParticipantsLocked(pkgName);
444 }
445 }
446 }
447 }
448 };
449
Dan Egnor87a02bc2009-06-17 02:30:10 -0700450 // ----- Track connection to GoogleBackupTransport service -----
451 ServiceConnection mGoogleConnection = new ServiceConnection() {
452 public void onServiceConnected(ComponentName name, IBinder service) {
453 if (DEBUG) Log.v(TAG, "Connected to Google transport");
454 mGoogleTransport = IBackupTransport.Stub.asInterface(service);
Christopher Tate91717492009-06-26 21:07:13 -0700455 registerTransport(name.flattenToShortString(), mGoogleTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700456 }
457
458 public void onServiceDisconnected(ComponentName name) {
459 if (DEBUG) Log.v(TAG, "Disconnected from Google transport");
460 mGoogleTransport = null;
Christopher Tate91717492009-06-26 21:07:13 -0700461 registerTransport(name.flattenToShortString(), null);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700462 }
463 };
464
Joe Onorato8ad02812009-05-13 01:41:44 -0400465 // ----- Run the actual backup process asynchronously -----
466
Christopher Tate181fafa2009-05-14 11:12:14 -0700467 private class BackupHandler extends Handler {
Joe Onorato8ad02812009-05-13 01:41:44 -0400468 public void handleMessage(Message msg) {
469
470 switch (msg.what) {
471 case MSG_RUN_BACKUP:
Dan Egnor87a02bc2009-06-17 02:30:10 -0700472 {
Christopher Tate91717492009-06-26 21:07:13 -0700473 IBackupTransport transport = getTransport(mCurrentTransport);
Dan Egnor87a02bc2009-06-17 02:30:10 -0700474 if (transport == null) {
475 Log.v(TAG, "Backup requested but no transport available");
Christopher Tateb6787f22009-07-02 17:40:45 -0700476 mWakelock.release();
Dan Egnor87a02bc2009-06-17 02:30:10 -0700477 break;
478 }
479
Joe Onorato8ad02812009-05-13 01:41:44 -0400480 // snapshot the pending-backup set and work on that
Christopher Tate6aa41f42009-06-19 14:14:22 -0700481 ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
Christopher Tatecde87f42009-06-12 12:55:53 -0700482 File oldJournal = mJournal;
Joe Onorato8ad02812009-05-13 01:41:44 -0400483 synchronized (mQueueLock) {
Christopher Tate49401dd2009-07-01 12:34:29 -0700484 // Do we have any work to do?
485 if (mPendingBackups.size() > 0) {
486 for (BackupRequest b: mPendingBackups.values()) {
487 queue.add(b);
Christopher Tatecde87f42009-06-12 12:55:53 -0700488 }
Christopher Tate49401dd2009-07-01 12:34:29 -0700489 Log.v(TAG, "clearing pending backups");
490 mPendingBackups.clear();
Christopher Tatecde87f42009-06-12 12:55:53 -0700491
Christopher Tate49401dd2009-07-01 12:34:29 -0700492 // Start a new backup-queue journal file too
493 if (mJournalStream != null) {
494 try {
495 mJournalStream.close();
496 } catch (IOException e) {
497 // don't need to do anything
498 }
499 makeJournalLocked();
500 }
501
502 // At this point, we have started a new journal file, and the old
503 // file identity is being passed to the backup processing thread.
504 // When it completes successfully, that old journal file will be
505 // deleted. If we crash prior to that, the old journal is parsed
506 // at next boot and the journaled requests fulfilled.
507 (new PerformBackupThread(transport, queue, oldJournal)).start();
508 } else {
509 Log.v(TAG, "Backup requested but nothing pending");
Christopher Tateb6787f22009-07-02 17:40:45 -0700510 mWakelock.release();
Christopher Tate49401dd2009-07-01 12:34:29 -0700511 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400512 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700513 break;
Dan Egnor87a02bc2009-06-17 02:30:10 -0700514 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700515
516 case MSG_RUN_FULL_BACKUP:
Joe Onorato8ad02812009-05-13 01:41:44 -0400517 break;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700518
519 case MSG_RUN_RESTORE:
520 {
Christopher Tate7d562ec2009-06-25 18:03:43 -0700521 RestoreParams params = (RestoreParams)msg.obj;
Joe Onorato9a5e3e12009-07-01 21:04:03 -0400522 Log.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
Christopher Tateb6787f22009-07-02 17:40:45 -0700523 (new PerformRestoreThread(params.transport, params.observer,
524 params.token)).start();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700525 break;
526 }
Christopher Tateee0e78a2009-07-02 11:17:03 -0700527
528 case MSG_RUN_CLEAR:
529 {
530 ClearParams params = (ClearParams)msg.obj;
531 (new PerformClearThread(params.transport, params.packageInfo)).start();
532 break;
533 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400534 }
535 }
Joe Onorato8ad02812009-05-13 01:41:44 -0400536 }
537
Christopher Tate181fafa2009-05-14 11:12:14 -0700538 // Add the backup agents in the given package to our set of known backup participants.
539 // If 'packageName' is null, adds all backup agents in the whole system.
Christopher Tate3799bc22009-05-06 16:13:56 -0700540 void addPackageParticipantsLocked(String packageName) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700541 // Look for apps that define the android:backupAgent attribute
Christopher Tate043dadc2009-06-02 16:11:00 -0700542 if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700543 List<PackageInfo> targetApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700544 addPackageParticipantsLockedInner(packageName, targetApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700545 }
546
Christopher Tate181fafa2009-05-14 11:12:14 -0700547 private void addPackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700548 List<PackageInfo> targetPkgs) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700549 if (DEBUG) {
Dan Egnorefe52642009-06-24 00:16:33 -0700550 Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
551 for (PackageInfo p : targetPkgs) {
Christopher Tate111bd4a2009-06-24 17:29:38 -0700552 Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName
553 + " uid=" + p.applicationInfo.uid);
Christopher Tate181fafa2009-05-14 11:12:14 -0700554 }
555 }
556
Dan Egnorefe52642009-06-24 00:16:33 -0700557 for (PackageInfo pkg : targetPkgs) {
558 if (packageName == null || pkg.packageName.equals(packageName)) {
559 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700560 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700561 if (set == null) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700562 set = new HashSet<ApplicationInfo>();
Christopher Tate3799bc22009-05-06 16:13:56 -0700563 mBackupParticipants.put(uid, set);
564 }
Dan Egnorefe52642009-06-24 00:16:33 -0700565 set.add(pkg.applicationInfo);
Christopher Tate73e02522009-07-15 14:18:26 -0700566
567 // If we've never seen this app before, schedule a backup for it
568 if (!mEverStoredApps.contains(pkg.packageName)) {
569 if (DEBUG) Log.i(TAG, "New app " + pkg.packageName
570 + " never backed up; scheduling");
571 try {
572 dataChanged(pkg.packageName);
573 } catch (RemoteException e) {
574 // can't happen; it's a local method call
575 }
576 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700577 }
Christopher Tate487529a2009-04-29 14:03:25 -0700578 }
579 }
580
Christopher Tate6785dd82009-06-18 15:58:25 -0700581 // Remove the given package's entry from our known active set. If
582 // 'packageName' is null, *all* participating apps will be removed.
Christopher Tate3799bc22009-05-06 16:13:56 -0700583 void removePackageParticipantsLocked(String packageName) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700584 if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName);
Dan Egnorefe52642009-06-24 00:16:33 -0700585 List<PackageInfo> allApps = null;
Christopher Tate181fafa2009-05-14 11:12:14 -0700586 if (packageName != null) {
Dan Egnorefe52642009-06-24 00:16:33 -0700587 allApps = new ArrayList<PackageInfo>();
Christopher Tate181fafa2009-05-14 11:12:14 -0700588 try {
Dan Egnorefe52642009-06-24 00:16:33 -0700589 int flags = PackageManager.GET_SIGNATURES;
590 allApps.add(mPackageManager.getPackageInfo(packageName, flags));
Christopher Tate181fafa2009-05-14 11:12:14 -0700591 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700592 // just skip it (???)
Christopher Tate181fafa2009-05-14 11:12:14 -0700593 }
594 } else {
595 // all apps with agents
Dan Egnorefe52642009-06-24 00:16:33 -0700596 allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700597 }
598 removePackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700599 }
600
Joe Onorato8ad02812009-05-13 01:41:44 -0400601 private void removePackageParticipantsLockedInner(String packageName,
Dan Egnorefe52642009-06-24 00:16:33 -0700602 List<PackageInfo> agents) {
Christopher Tate043dadc2009-06-02 16:11:00 -0700603 if (DEBUG) {
604 Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName
605 + ") removing " + agents.size() + " entries");
Dan Egnorefe52642009-06-24 00:16:33 -0700606 for (PackageInfo p : agents) {
607 Log.v(TAG, " - " + p);
Christopher Tate043dadc2009-06-02 16:11:00 -0700608 }
609 }
Dan Egnorefe52642009-06-24 00:16:33 -0700610 for (PackageInfo pkg : agents) {
611 if (packageName == null || pkg.packageName.equals(packageName)) {
612 int uid = pkg.applicationInfo.uid;
Christopher Tate181fafa2009-05-14 11:12:14 -0700613 HashSet<ApplicationInfo> set = mBackupParticipants.get(uid);
Christopher Tate3799bc22009-05-06 16:13:56 -0700614 if (set != null) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700615 // Find the existing entry with the same package name, and remove it.
616 // We can't just remove(app) because the instances are different.
617 for (ApplicationInfo entry: set) {
Dan Egnorefe52642009-06-24 00:16:33 -0700618 if (entry.packageName.equals(pkg.packageName)) {
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700619 set.remove(entry);
Christopher Tatee97e8072009-07-15 16:45:50 -0700620 removeEverBackedUp(pkg.packageName);
Christopher Tatecd4ff2e2009-06-05 13:57:54 -0700621 break;
622 }
623 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700624 if (set.size() == 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700625 mBackupParticipants.delete(uid);
626 }
Christopher Tate3799bc22009-05-06 16:13:56 -0700627 }
628 }
629 }
630 }
631
Christopher Tate181fafa2009-05-14 11:12:14 -0700632 // Returns the set of all applications that define an android:backupAgent attribute
Christopher Tate73e02522009-07-15 14:18:26 -0700633 List<PackageInfo> allAgentPackages() {
Christopher Tate6785dd82009-06-18 15:58:25 -0700634 // !!! TODO: cache this and regenerate only when necessary
Dan Egnorefe52642009-06-24 00:16:33 -0700635 int flags = PackageManager.GET_SIGNATURES;
636 List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
637 int N = packages.size();
638 for (int a = N-1; a >= 0; a--) {
639 ApplicationInfo app = packages.get(a).applicationInfo;
640 if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
641 || app.backupAgentName == null) {
642 packages.remove(a);
Christopher Tate181fafa2009-05-14 11:12:14 -0700643 }
644 }
Dan Egnorefe52642009-06-24 00:16:33 -0700645 return packages;
Christopher Tate181fafa2009-05-14 11:12:14 -0700646 }
Christopher Tateaa088442009-06-16 18:25:46 -0700647
Christopher Tate3799bc22009-05-06 16:13:56 -0700648 // Reset the given package's known backup participants. Unlike add/remove, the update
649 // action cannot be passed a null package name.
650 void updatePackageParticipantsLocked(String packageName) {
651 if (packageName == null) {
652 Log.e(TAG, "updatePackageParticipants called with null package name");
653 return;
654 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700655 if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName);
Christopher Tate3799bc22009-05-06 16:13:56 -0700656
657 // brute force but small code size
Dan Egnorefe52642009-06-24 00:16:33 -0700658 List<PackageInfo> allApps = allAgentPackages();
Christopher Tate181fafa2009-05-14 11:12:14 -0700659 removePackageParticipantsLockedInner(packageName, allApps);
660 addPackageParticipantsLockedInner(packageName, allApps);
Christopher Tate3799bc22009-05-06 16:13:56 -0700661 }
662
Christopher Tate73e02522009-07-15 14:18:26 -0700663 // Called from the backup thread: record that the given app has been successfully
664 // backed up at least once
665 void logBackupComplete(String packageName) {
Christopher Tatee97e8072009-07-15 16:45:50 -0700666 if (mEverStoredStream != null && !packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
Christopher Tate73e02522009-07-15 14:18:26 -0700667 synchronized (mEverStoredApps) {
668 if (mEverStoredApps.add(packageName)) {
669 try {
670 mEverStoredStream.writeUTF(packageName);
671 } catch (IOException e) {
672 Log.e(TAG, "Unable to log backup of " + packageName + ", ceasing log");
673 try {
674 mEverStoredStream.close();
675 } catch (IOException ioe) {
676 // we're dropping it; no need to handle an exception on close here
677 }
678 mEverStoredStream = null;
679 }
680 }
681 }
682 }
683 }
684
Christopher Tatee97e8072009-07-15 16:45:50 -0700685 // Remove our awareness of having ever backed up the given package
686 void removeEverBackedUp(String packageName) {
687 if (DEBUG) Log.v(TAG, "Removing backed-up knowledge of " + packageName
688 + ", new set:");
689
690 if (mEverStoredStream != null) {
691 synchronized (mEverStoredApps) {
692 // Rewrite the file and rename to overwrite. If we reboot in the middle,
693 // we'll recognize on initialization time that the package no longer
694 // exists and fix it up then.
695 File tempKnownFile = new File(mBaseStateDir, "processed.new");
696 try {
697 mEverStoredStream.close();
698 RandomAccessFile known = new RandomAccessFile(tempKnownFile, "rw");
699 mEverStoredApps.remove(packageName);
700 for (String s : mEverStoredApps) {
701 known.writeUTF(s);
702 if (DEBUG) Log.v(TAG, " " + s);
703 }
704 known.close();
705 tempKnownFile.renameTo(mEverStored);
706 mEverStoredStream = new RandomAccessFile(mEverStored, "rwd");
707 } catch (IOException e) {
708 // Bad: we couldn't create the new copy. For safety's sake we
709 // abandon the whole process and remove all what's-backed-up
710 // state entirely, meaning we'll force a backup pass for every
711 // participant on the next boot or [re]install.
712 Log.w(TAG, "Error rewriting backed-up set; halting log");
713 mEverStoredStream = null;
714 mEverStoredApps.clear();
715 tempKnownFile.delete();
716 mEverStored.delete();
717 }
718 }
719 }
720 }
721
Dan Egnor87a02bc2009-06-17 02:30:10 -0700722 // Return the given transport
Christopher Tate91717492009-06-26 21:07:13 -0700723 private IBackupTransport getTransport(String transportName) {
724 synchronized (mTransports) {
725 IBackupTransport transport = mTransports.get(transportName);
726 if (transport == null) {
727 Log.w(TAG, "Requested unavailable transport: " + transportName);
728 }
729 return transport;
Christopher Tate8c850b72009-06-07 19:33:20 -0700730 }
Christopher Tate8c850b72009-06-07 19:33:20 -0700731 }
732
Christopher Tatedf01dea2009-06-09 20:45:02 -0700733 // fire off a backup agent, blocking until it attaches or times out
734 IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
735 IBackupAgent agent = null;
736 synchronized(mAgentConnectLock) {
737 mConnecting = true;
738 mConnectedAgent = null;
739 try {
740 if (mActivityManager.bindBackupAgent(app, mode)) {
741 Log.d(TAG, "awaiting agent for " + app);
742
743 // success; wait for the agent to arrive
Christopher Tatec7b31e32009-06-10 15:49:30 -0700744 // only wait 10 seconds for the clear data to happen
745 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
746 while (mConnecting && mConnectedAgent == null
747 && (System.currentTimeMillis() < timeoutMark)) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700748 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700749 mAgentConnectLock.wait(5000);
Christopher Tatedf01dea2009-06-09 20:45:02 -0700750 } catch (InterruptedException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700751 // just bail
Christopher Tatedf01dea2009-06-09 20:45:02 -0700752 return null;
753 }
754 }
755
756 // if we timed out with no connect, abort and move on
757 if (mConnecting == true) {
758 Log.w(TAG, "Timeout waiting for agent " + app);
759 return null;
760 }
761 agent = mConnectedAgent;
762 }
763 } catch (RemoteException e) {
764 // can't happen
765 }
766 }
767 return agent;
768 }
769
Christopher Tatec7b31e32009-06-10 15:49:30 -0700770 // clear an application's data, blocking until the operation completes or times out
771 void clearApplicationDataSynchronous(String packageName) {
Christopher Tatef7c886b2009-06-26 15:34:09 -0700772 // Don't wipe packages marked allowClearUserData=false
773 try {
774 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
775 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
776 if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping "
777 + packageName);
778 return;
779 }
780 } catch (NameNotFoundException e) {
781 Log.w(TAG, "Tried to clear data for " + packageName + " but not found");
782 return;
783 }
784
Christopher Tatec7b31e32009-06-10 15:49:30 -0700785 ClearDataObserver observer = new ClearDataObserver();
786
787 synchronized(mClearDataLock) {
788 mClearingData = true;
789 mPackageManager.clearApplicationUserData(packageName, observer);
790
791 // only wait 10 seconds for the clear data to happen
792 long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
793 while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
794 try {
795 mClearDataLock.wait(5000);
796 } catch (InterruptedException e) {
797 // won't happen, but still.
798 mClearingData = false;
799 }
800 }
801 }
802 }
803
804 class ClearDataObserver extends IPackageDataObserver.Stub {
805 public void onRemoveCompleted(String packageName, boolean succeeded)
806 throws android.os.RemoteException {
807 synchronized(mClearDataLock) {
808 mClearingData = false;
Christopher Tatef68eb502009-06-16 11:02:01 -0700809 mClearDataLock.notifyAll();
Christopher Tatec7b31e32009-06-10 15:49:30 -0700810 }
811 }
812 }
813
Christopher Tate043dadc2009-06-02 16:11:00 -0700814 // ----- Back up a set of applications via a worker thread -----
815
816 class PerformBackupThread extends Thread {
817 private static final String TAG = "PerformBackupThread";
Christopher Tateaa088442009-06-16 18:25:46 -0700818 IBackupTransport mTransport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700819 ArrayList<BackupRequest> mQueue;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700820 File mStateDir;
Christopher Tatecde87f42009-06-12 12:55:53 -0700821 File mJournal;
Christopher Tate043dadc2009-06-02 16:11:00 -0700822
Christopher Tateaa088442009-06-16 18:25:46 -0700823 public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue,
Christopher Tatecde87f42009-06-12 12:55:53 -0700824 File journal) {
Christopher Tateaa088442009-06-16 18:25:46 -0700825 mTransport = transport;
Christopher Tate043dadc2009-06-02 16:11:00 -0700826 mQueue = queue;
Christopher Tatecde87f42009-06-12 12:55:53 -0700827 mJournal = journal;
Christopher Tate5cb400b2009-06-25 16:03:14 -0700828
829 try {
830 mStateDir = new File(mBaseStateDir, transport.transportDirName());
831 } catch (RemoteException e) {
832 // can't happen; the transport is local
833 }
834 mStateDir.mkdirs();
Christopher Tate043dadc2009-06-02 16:11:00 -0700835 }
836
837 @Override
838 public void run() {
Dan Egnorbb9001c2009-07-27 12:20:13 -0700839 long startRealtime = SystemClock.elapsedRealtime();
Christopher Tate043dadc2009-06-02 16:11:00 -0700840 if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
841
Christopher Tate79588342009-06-30 16:11:49 -0700842 // Backups run at background priority
843 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
844
Christopher Tate043dadc2009-06-02 16:11:00 -0700845 try {
Dan Egnorbb9001c2009-07-27 12:20:13 -0700846 EventLog.writeEvent(BACKUP_START_EVENT, mTransport.transportDirName());
847
848 // The package manager doesn't have a proper <application> etc, but since
849 // it's running here in the system process we can just set up its agent
850 // directly and use a synthetic BackupRequest. We always run this pass
851 // because it's cheap and this way we guarantee that we don't get out of
852 // step even if we're selecting among various transports at run time.
853 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
854 mPackageManager, allAgentPackages());
855 BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false);
856 pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL;
857 processOneBackup(pmRequest,
858 IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport);
859
860 // Now run all the backups in our queue
861 int count = mQueue.size();
862 doQueuedBackups(mTransport);
863
864 // Finally, tear down the transport
865 if (mTransport.finishBackup()) {
866 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
867 EventLog.writeEvent(BACKUP_SUCCESS_EVENT, count, millis);
868 } else {
869 EventLog.writeEvent(BACKUP_TRANSPORT_FAILURE_EVENT, "");
870 Log.e(TAG, "Transport error in finishBackup()");
Dan Egnorefe52642009-06-24 00:16:33 -0700871 }
Christopher Tatecde87f42009-06-12 12:55:53 -0700872
Dan Egnorbb9001c2009-07-27 12:20:13 -0700873 if (!mJournal.delete()) {
874 Log.e(TAG, "Unable to remove backup journal file " + mJournal);
875 }
876 } catch (Exception e) {
877 Log.e(TAG, "Error in backup thread", e);
878 } finally {
879 // Only once we're entirely finished do we release the wakelock
880 mWakelock.release();
Christopher Tatecde87f42009-06-12 12:55:53 -0700881 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700882 }
883
884 private void doQueuedBackups(IBackupTransport transport) {
885 for (BackupRequest request : mQueue) {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700886 Log.d(TAG, "starting agent for backup of " + request);
Christopher Tate043dadc2009-06-02 16:11:00 -0700887
888 IBackupAgent agent = null;
889 int mode = (request.fullBackup)
890 ? IApplicationThread.BACKUP_MODE_FULL
891 : IApplicationThread.BACKUP_MODE_INCREMENTAL;
892 try {
Christopher Tatedf01dea2009-06-09 20:45:02 -0700893 agent = bindToAgentSynchronous(request.appInfo, mode);
894 if (agent != null) {
895 processOneBackup(request, agent, transport);
Christopher Tate043dadc2009-06-02 16:11:00 -0700896 }
Christopher Tatedf01dea2009-06-09 20:45:02 -0700897
898 // unbind even on timeout, just in case
899 mActivityManager.unbindBackupAgent(request.appInfo);
Christopher Tate043dadc2009-06-02 16:11:00 -0700900 } catch (SecurityException ex) {
901 // Try for the next one.
Christopher Tatec7b31e32009-06-10 15:49:30 -0700902 Log.d(TAG, "error in bind/backup", ex);
Christopher Tate043dadc2009-06-02 16:11:00 -0700903 } catch (RemoteException e) {
Christopher Tatec7b31e32009-06-10 15:49:30 -0700904 Log.v(TAG, "bind/backup threw");
905 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -0700906 }
907 }
908 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700909
910 void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) {
911 final String packageName = request.appInfo.packageName;
Dan Egnorbb9001c2009-07-27 12:20:13 -0700912 if (DEBUG) Log.d(TAG, "processOneBackup doBackup() on " + packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700913
Dan Egnorbb9001c2009-07-27 12:20:13 -0700914 // !!! TODO: get the state file dir from the transport
915 File savedStateName = new File(mStateDir, packageName);
916 File backupDataName = new File(mDataDir, packageName + ".data");
917 File newStateName = new File(mStateDir, packageName + ".new");
918
919 ParcelFileDescriptor savedState = null;
920 ParcelFileDescriptor backupData = null;
921 ParcelFileDescriptor newState = null;
922
923 PackageInfo packInfo;
Christopher Tatec7b31e32009-06-10 15:49:30 -0700924 try {
925 // Look up the package info & signatures. This is first so that if it
926 // throws an exception, there's no file setup yet that would need to
927 // be unraveled.
Christopher Tateabce4e82009-06-18 18:35:32 -0700928 if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
929 // The metadata 'package' is synthetic
930 packInfo = new PackageInfo();
931 packInfo.packageName = packageName;
932 } else {
933 packInfo = mPackageManager.getPackageInfo(packageName,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700934 PackageManager.GET_SIGNATURES);
Christopher Tateabce4e82009-06-18 18:35:32 -0700935 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700936
Christopher Tatec7b31e32009-06-10 15:49:30 -0700937 // In a full backup, we pass a null ParcelFileDescriptor as
938 // the saved-state "file"
Dan Egnorbb9001c2009-07-27 12:20:13 -0700939 if (!request.fullBackup) {
940 savedState = ParcelFileDescriptor.open(savedStateName,
Christopher Tatec7b31e32009-06-10 15:49:30 -0700941 ParcelFileDescriptor.MODE_READ_ONLY |
Dan Egnorbb9001c2009-07-27 12:20:13 -0700942 ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary
943 }
Christopher Tatec7b31e32009-06-10 15:49:30 -0700944
Dan Egnorbb9001c2009-07-27 12:20:13 -0700945 backupData = ParcelFileDescriptor.open(backupDataName,
946 ParcelFileDescriptor.MODE_READ_WRITE |
947 ParcelFileDescriptor.MODE_CREATE |
948 ParcelFileDescriptor.MODE_TRUNCATE);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700949
Dan Egnorbb9001c2009-07-27 12:20:13 -0700950 newState = ParcelFileDescriptor.open(newStateName,
951 ParcelFileDescriptor.MODE_READ_WRITE |
952 ParcelFileDescriptor.MODE_CREATE |
953 ParcelFileDescriptor.MODE_TRUNCATE);
Christopher Tatec7b31e32009-06-10 15:49:30 -0700954
955 // Run the target's backup pass
Dan Egnorbb9001c2009-07-27 12:20:13 -0700956 agent.doBackup(savedState, backupData, newState);
957 logBackupComplete(packageName);
958 if (DEBUG) Log.v(TAG, "doBackup() success");
Christopher Tatec7b31e32009-06-10 15:49:30 -0700959 } catch (Exception e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700960 Log.e(TAG, "Error backing up " + packageName, e);
Dan Egnorbb9001c2009-07-27 12:20:13 -0700961 EventLog.writeEvent(BACKUP_AGENT_FAILURE_EVENT, packageName, e.toString());
962 backupDataName.delete();
963 newStateName.delete();
964 return;
965 } finally {
966 try { if (savedState != null) savedState.close(); } catch (IOException e) {}
967 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
968 try { if (newState != null) newState.close(); } catch (IOException e) {}
969 savedState = backupData = newState = null;
970 }
971
972 // Now propagate the newly-backed-up data to the transport
973 try {
974 int size = (int) backupDataName.length();
975 if (size > 0) {
976 backupData = ParcelFileDescriptor.open(backupDataName,
977 ParcelFileDescriptor.MODE_READ_ONLY);
978
979 if (!transport.performBackup(packInfo, backupData)) throw new Exception();
980 } else {
981 if (DEBUG) Log.i(TAG, "no backup data written; not calling transport");
982 }
983
984 // After successful transport, delete the now-stale data
985 // and juggle the files so that next time we supply the agent
986 // with the new state file it just created.
987 backupDataName.delete();
988 newStateName.renameTo(savedStateName);
989 EventLog.writeEvent(BACKUP_PACKAGE_EVENT, packageName, size);
990 } catch (Exception e) {
991 Log.e(TAG, "Transport error backing up " + packageName, e);
992 EventLog.writeEvent(BACKUP_TRANSPORT_FAILURE_EVENT, packageName);
993 return;
994 } finally {
995 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
Christopher Tatec7b31e32009-06-10 15:49:30 -0700996 }
997 }
Christopher Tate043dadc2009-06-02 16:11:00 -0700998 }
999
Christopher Tatedf01dea2009-06-09 20:45:02 -07001000
1001 // ----- Restore handling -----
1002
Christopher Tateabce4e82009-06-18 18:35:32 -07001003 private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) {
Christopher Tate20efdf6b2009-06-18 19:41:36 -07001004 // Allow unsigned apps, but not signed on one device and unsigned on the other
1005 // !!! TODO: is this the right policy?
Christopher Tate6aa41f42009-06-19 14:14:22 -07001006 if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs
1007 + " device=" + deviceSigs);
Christopher Tate20efdf6b2009-06-18 19:41:36 -07001008 if ((storedSigs == null || storedSigs.length == 0)
1009 && (deviceSigs == null || deviceSigs.length == 0)) {
1010 return true;
1011 }
1012 if (storedSigs == null || deviceSigs == null) {
1013 return false;
1014 }
1015
Christopher Tateabce4e82009-06-18 18:35:32 -07001016 // !!! TODO: this demands that every stored signature match one
1017 // that is present on device, and does not demand the converse.
1018 // Is this this right policy?
1019 int nStored = storedSigs.length;
1020 int nDevice = deviceSigs.length;
1021
1022 for (int i=0; i < nStored; i++) {
1023 boolean match = false;
1024 for (int j=0; j < nDevice; j++) {
1025 if (storedSigs[i].equals(deviceSigs[j])) {
1026 match = true;
1027 break;
1028 }
1029 }
1030 if (!match) {
1031 return false;
1032 }
1033 }
1034 return true;
1035 }
1036
Christopher Tatedf01dea2009-06-09 20:45:02 -07001037 class PerformRestoreThread extends Thread {
1038 private IBackupTransport mTransport;
Christopher Tate7d562ec2009-06-25 18:03:43 -07001039 private IRestoreObserver mObserver;
Dan Egnor156411d2009-06-26 13:20:02 -07001040 private long mToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001041 private File mStateDir;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001042
Christopher Tate5cbbf562009-06-22 16:44:51 -07001043 class RestoreRequest {
1044 public PackageInfo app;
1045 public int storedAppVersion;
1046
1047 RestoreRequest(PackageInfo _app, int _version) {
1048 app = _app;
1049 storedAppVersion = _version;
1050 }
1051 }
1052
Christopher Tate7d562ec2009-06-25 18:03:43 -07001053 PerformRestoreThread(IBackupTransport transport, IRestoreObserver observer,
Dan Egnor156411d2009-06-26 13:20:02 -07001054 long restoreSetToken) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001055 mTransport = transport;
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001056 Log.d(TAG, "PerformRestoreThread mObserver=" + mObserver);
Christopher Tate7d562ec2009-06-25 18:03:43 -07001057 mObserver = observer;
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001058 mToken = restoreSetToken;
Christopher Tate5cb400b2009-06-25 16:03:14 -07001059
1060 try {
1061 mStateDir = new File(mBaseStateDir, transport.transportDirName());
1062 } catch (RemoteException e) {
1063 // can't happen; the transport is local
1064 }
1065 mStateDir.mkdirs();
Christopher Tatedf01dea2009-06-09 20:45:02 -07001066 }
1067
1068 @Override
1069 public void run() {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001070 long startRealtime = SystemClock.elapsedRealtime();
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001071 if (DEBUG) Log.v(TAG, "Beginning restore process mTransport=" + mTransport
1072 + " mObserver=" + mObserver + " mToken=" + mToken);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001073 /**
1074 * Restore sequence:
1075 *
Dan Egnorefe52642009-06-24 00:16:33 -07001076 * 1. get the restore set description for our identity
1077 * 2. for each app in the restore set:
Dan Egnorbb9001c2009-07-27 12:20:13 -07001078 * 2.a. if it's restorable on this device, add it to the restore queue
Dan Egnorefe52642009-06-24 00:16:33 -07001079 * 3. for each app in the restore queue:
1080 * 3.a. clear the app data
1081 * 3.b. get the restore data for the app from the transport
1082 * 3.c. launch the backup agent for the app
1083 * 3.d. agent.doRestore() with the data from the server
1084 * 3.e. unbind the agent [and kill the app?]
1085 * 4. shut down the transport
Dan Egnorbb9001c2009-07-27 12:20:13 -07001086 *
1087 * On errors, we try our best to recover and move on to the next
1088 * application, but if necessary we abort the whole operation --
1089 * the user is waiting, after al.
Christopher Tatedf01dea2009-06-09 20:45:02 -07001090 */
1091
Christopher Tate7d562ec2009-06-25 18:03:43 -07001092 int error = -1; // assume error
1093
Dan Egnorefe52642009-06-24 00:16:33 -07001094 // build the set of apps to restore
Christopher Tatedf01dea2009-06-09 20:45:02 -07001095 try {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001096 // TODO: Log this before getAvailableRestoreSets, somehow
1097 EventLog.writeEvent(RESTORE_START_EVENT, mTransport.transportDirName());
Christopher Tateabce4e82009-06-18 18:35:32 -07001098
Dan Egnorefe52642009-06-24 00:16:33 -07001099 // Get the list of all packages which have backup enabled.
1100 // (Include the Package Manager metadata pseudo-package first.)
1101 ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>();
1102 PackageInfo omPackage = new PackageInfo();
1103 omPackage.packageName = PACKAGE_MANAGER_SENTINEL;
1104 restorePackages.add(omPackage);
Christopher Tatedf01dea2009-06-09 20:45:02 -07001105
Dan Egnorefe52642009-06-24 00:16:33 -07001106 List<PackageInfo> agentPackages = allAgentPackages();
1107 restorePackages.addAll(agentPackages);
1108
Christopher Tate7d562ec2009-06-25 18:03:43 -07001109 // let the observer know that we're running
1110 if (mObserver != null) {
1111 try {
1112 // !!! TODO: get an actual count from the transport after
1113 // its startRestore() runs?
1114 mObserver.restoreStarting(restorePackages.size());
1115 } catch (RemoteException e) {
1116 Log.d(TAG, "Restore observer died at restoreStarting");
1117 mObserver = null;
1118 }
1119 }
1120
Dan Egnor156411d2009-06-26 13:20:02 -07001121 if (!mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0]))) {
Dan Egnorefe52642009-06-24 00:16:33 -07001122 Log.e(TAG, "Error starting restore operation");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001123 EventLog.writeEvent(RESTORE_TRANSPORT_FAILURE_EVENT);
Dan Egnorefe52642009-06-24 00:16:33 -07001124 return;
1125 }
1126
1127 String packageName = mTransport.nextRestorePackage();
1128 if (packageName == null) {
Dan Egnorefe52642009-06-24 00:16:33 -07001129 Log.e(TAG, "Error getting first restore package");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001130 EventLog.writeEvent(RESTORE_TRANSPORT_FAILURE_EVENT);
Dan Egnorefe52642009-06-24 00:16:33 -07001131 return;
1132 } else if (packageName.equals("")) {
1133 Log.i(TAG, "No restore data available");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001134 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
1135 EventLog.writeEvent(RESTORE_SUCCESS_EVENT, 0, millis);
Dan Egnorefe52642009-06-24 00:16:33 -07001136 return;
1137 } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
1138 Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL
1139 + "\", found only \"" + packageName + "\"");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001140 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, PACKAGE_MANAGER_SENTINEL,
1141 "Package manager data missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001142 return;
1143 }
1144
1145 // Pull the Package Manager metadata from the restore set first
1146 PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
1147 mPackageManager, agentPackages);
1148 processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()));
1149
Christopher Tate8c032472009-07-02 14:28:47 -07001150 // Verify that the backup set includes metadata. If not, we can't do
1151 // signature/version verification etc, so we simply do not proceed with
1152 // the restore operation.
Christopher Tate3d7cd132009-07-07 14:23:07 -07001153 if (!pmAgent.hasMetadata()) {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001154 Log.e(TAG, "No restore metadata available, so not restoring settings");
1155 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, PACKAGE_MANAGER_SENTINEL,
1156 "Package manager restore metadata missing");
Christopher Tate8c032472009-07-02 14:28:47 -07001157 return;
1158 }
1159
Christopher Tate7d562ec2009-06-25 18:03:43 -07001160 int count = 0;
Dan Egnorefe52642009-06-24 00:16:33 -07001161 for (;;) {
1162 packageName = mTransport.nextRestorePackage();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001163
Dan Egnorefe52642009-06-24 00:16:33 -07001164 if (packageName == null) {
Dan Egnorefe52642009-06-24 00:16:33 -07001165 Log.e(TAG, "Error getting next restore package");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001166 EventLog.writeEvent(RESTORE_TRANSPORT_FAILURE_EVENT);
Dan Egnorefe52642009-06-24 00:16:33 -07001167 return;
1168 } else if (packageName.equals("")) {
1169 break;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001170 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001171
Christopher Tate7d562ec2009-06-25 18:03:43 -07001172 if (mObserver != null) {
Christopher Tate7d562ec2009-06-25 18:03:43 -07001173 try {
1174 mObserver.onUpdate(count);
1175 } catch (RemoteException e) {
1176 Log.d(TAG, "Restore observer died in onUpdate");
1177 mObserver = null;
1178 }
1179 }
1180
Dan Egnorefe52642009-06-24 00:16:33 -07001181 Metadata metaInfo = pmAgent.getRestoredMetadata(packageName);
1182 if (metaInfo == null) {
1183 Log.e(TAG, "Missing metadata for " + packageName);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001184 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName,
1185 "Package metadata missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001186 continue;
1187 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001188
Dan Egnorbb9001c2009-07-27 12:20:13 -07001189 PackageInfo packageInfo;
1190 try {
1191 int flags = PackageManager.GET_SIGNATURES;
1192 packageInfo = mPackageManager.getPackageInfo(packageName, flags);
1193 } catch (NameNotFoundException e) {
1194 Log.e(TAG, "Invalid package restoring data", e);
1195 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName,
1196 "Package missing on device");
1197 continue;
1198 }
1199
Dan Egnorefe52642009-06-24 00:16:33 -07001200 if (metaInfo.versionCode > packageInfo.versionCode) {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001201 String message = "Version " + metaInfo.versionCode
1202 + " > installed version " + packageInfo.versionCode;
1203 Log.w(TAG, "Package " + packageName + ": " + message);
1204 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName, message);
Dan Egnorefe52642009-06-24 00:16:33 -07001205 continue;
1206 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001207
Dan Egnorefe52642009-06-24 00:16:33 -07001208 if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) {
1209 Log.w(TAG, "Signature mismatch restoring " + packageName);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001210 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName,
1211 "Signature mismatch");
Dan Egnorefe52642009-06-24 00:16:33 -07001212 continue;
1213 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001214
Dan Egnorefe52642009-06-24 00:16:33 -07001215 if (DEBUG) Log.v(TAG, "Package " + packageName
1216 + " restore version [" + metaInfo.versionCode
1217 + "] is compatible with installed version ["
1218 + packageInfo.versionCode + "]");
Christopher Tatec7b31e32009-06-10 15:49:30 -07001219
Dan Egnorefe52642009-06-24 00:16:33 -07001220 // Now perform the actual restore
1221 clearApplicationDataSynchronous(packageName);
1222 IBackupAgent agent = bindToAgentSynchronous(
1223 packageInfo.applicationInfo,
Christopher Tatec7b31e32009-06-10 15:49:30 -07001224 IApplicationThread.BACKUP_MODE_RESTORE);
Dan Egnorefe52642009-06-24 00:16:33 -07001225 if (agent == null) {
1226 Log.w(TAG, "Can't find backup agent for " + packageName);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001227 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName,
1228 "Restore agent missing");
Dan Egnorefe52642009-06-24 00:16:33 -07001229 continue;
Christopher Tatedf01dea2009-06-09 20:45:02 -07001230 }
1231
Dan Egnorefe52642009-06-24 00:16:33 -07001232 try {
1233 processOneRestore(packageInfo, metaInfo.versionCode, agent);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001234 ++count;
Dan Egnorefe52642009-06-24 00:16:33 -07001235 } finally {
1236 // unbind even on timeout or failure, just in case
1237 mActivityManager.unbindBackupAgent(packageInfo.applicationInfo);
1238 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001239 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001240
1241 // if we get this far, report success to the observer
1242 error = 0;
Dan Egnorbb9001c2009-07-27 12:20:13 -07001243 int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
1244 EventLog.writeEvent(RESTORE_SUCCESS_EVENT, count, millis);
1245 } catch (Exception e) {
1246 Log.e(TAG, "Error in restore thread", e);
Dan Egnorefe52642009-06-24 00:16:33 -07001247 } finally {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001248 if (DEBUG) Log.d(TAG, "finishing restore mObserver=" + mObserver);
1249
Dan Egnorefe52642009-06-24 00:16:33 -07001250 try {
1251 mTransport.finishRestore();
1252 } catch (RemoteException e) {
1253 Log.e(TAG, "Error finishing restore", e);
1254 }
Christopher Tate7d562ec2009-06-25 18:03:43 -07001255
1256 if (mObserver != null) {
1257 try {
1258 mObserver.restoreFinished(error);
1259 } catch (RemoteException e) {
1260 Log.d(TAG, "Restore observer died at restoreFinished");
1261 }
1262 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001263
1264 // done; we can finally release the wakelock
1265 mWakelock.release();
Christopher Tatedf01dea2009-06-09 20:45:02 -07001266 }
1267 }
1268
Dan Egnorefe52642009-06-24 00:16:33 -07001269 // Do the guts of a restore of one application, using mTransport.getRestoreData().
1270 void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) {
Christopher Tatedf01dea2009-06-09 20:45:02 -07001271 // !!! TODO: actually run the restore through mTransport
Christopher Tatec7b31e32009-06-10 15:49:30 -07001272 final String packageName = app.packageName;
1273
Dan Egnorbb9001c2009-07-27 12:20:13 -07001274 if (DEBUG) Log.d(TAG, "processOneRestore packageName=" + packageName);
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001275
Christopher Tatec7b31e32009-06-10 15:49:30 -07001276 // !!! TODO: get the dirs from the transport
1277 File backupDataName = new File(mDataDir, packageName + ".restore");
Dan Egnorbb9001c2009-07-27 12:20:13 -07001278 File newStateName = new File(mStateDir, packageName + ".new");
1279 File savedStateName = new File(mStateDir, packageName);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001280
Dan Egnorbb9001c2009-07-27 12:20:13 -07001281 ParcelFileDescriptor backupData = null;
1282 ParcelFileDescriptor newState = null;
1283
1284 try {
Christopher Tatec7b31e32009-06-10 15:49:30 -07001285 // Run the transport's restore pass
Dan Egnorbb9001c2009-07-27 12:20:13 -07001286 backupData = ParcelFileDescriptor.open(backupDataName,
1287 ParcelFileDescriptor.MODE_READ_WRITE |
1288 ParcelFileDescriptor.MODE_CREATE |
1289 ParcelFileDescriptor.MODE_TRUNCATE);
1290
1291 if (!mTransport.getRestoreData(backupData)) {
1292 Log.e(TAG, "Error getting restore data for " + packageName);
1293 EventLog.writeEvent(RESTORE_TRANSPORT_FAILURE_EVENT);
1294 return;
Christopher Tatec7b31e32009-06-10 15:49:30 -07001295 }
1296
1297 // Okay, we have the data. Now have the agent do the restore.
Dan Egnorbb9001c2009-07-27 12:20:13 -07001298 backupData.close();
Christopher Tatec7b31e32009-06-10 15:49:30 -07001299 backupData = ParcelFileDescriptor.open(backupDataName,
1300 ParcelFileDescriptor.MODE_READ_ONLY);
1301
Dan Egnorbb9001c2009-07-27 12:20:13 -07001302 newState = ParcelFileDescriptor.open(newStateName,
1303 ParcelFileDescriptor.MODE_READ_WRITE |
1304 ParcelFileDescriptor.MODE_CREATE |
1305 ParcelFileDescriptor.MODE_TRUNCATE);
1306
1307 agent.doRestore(backupData, appVersionCode, newState);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001308
1309 // if everything went okay, remember the recorded state now
Dan Egnorefe52642009-06-24 00:16:33 -07001310 newStateName.renameTo(savedStateName);
Dan Egnorbb9001c2009-07-27 12:20:13 -07001311 int size = (int) backupDataName.length();
1312 EventLog.writeEvent(RESTORE_PACKAGE_EVENT, packageName, size);
Christopher Tatec7b31e32009-06-10 15:49:30 -07001313 } catch (Exception e) {
Dan Egnorbb9001c2009-07-27 12:20:13 -07001314 Log.e(TAG, "Error restoring data for " + packageName, e);
1315 EventLog.writeEvent(RESTORE_AGENT_FAILURE_EVENT, packageName, e.toString());
1316
Christopher Tate96733042009-07-20 14:49:13 -07001317 // If the agent fails restore, it might have put the app's data
1318 // into an incoherent state. For consistency we wipe its data
1319 // again in this case before propagating the exception
Christopher Tate96733042009-07-20 14:49:13 -07001320 clearApplicationDataSynchronous(packageName);
Christopher Tate1531dc82009-07-24 16:37:43 -07001321 } finally {
1322 backupDataName.delete();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001323 try { if (backupData != null) backupData.close(); } catch (IOException e) {}
1324 try { if (newState != null) newState.close(); } catch (IOException e) {}
1325 backupData = newState = null;
Christopher Tatec7b31e32009-06-10 15:49:30 -07001326 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001327 }
1328 }
1329
Christopher Tateee0e78a2009-07-02 11:17:03 -07001330 class PerformClearThread extends Thread {
1331 IBackupTransport mTransport;
1332 PackageInfo mPackage;
1333
1334 PerformClearThread(IBackupTransport transport, PackageInfo packageInfo) {
1335 mTransport = transport;
1336 mPackage = packageInfo;
1337 }
1338
1339 @Override
1340 public void run() {
1341 try {
1342 // Clear the on-device backup state to ensure a full backup next time
1343 File stateDir = new File(mBaseStateDir, mTransport.transportDirName());
1344 File stateFile = new File(stateDir, mPackage.packageName);
1345 stateFile.delete();
1346
1347 // Tell the transport to remove all the persistent storage for the app
1348 mTransport.clearBackupData(mPackage);
1349 } catch (RemoteException e) {
1350 // can't happen; the transport is local
1351 } finally {
1352 try {
1353 mTransport.finishBackup();
1354 } catch (RemoteException e) {
1355 // can't happen; the transport is local
1356 }
Christopher Tateb6787f22009-07-02 17:40:45 -07001357
1358 // Last but not least, release the cpu
1359 mWakelock.release();
Christopher Tateee0e78a2009-07-02 11:17:03 -07001360 }
1361 }
1362 }
1363
Christopher Tatedf01dea2009-06-09 20:45:02 -07001364
Christopher Tate487529a2009-04-29 14:03:25 -07001365 // ----- IBackupManager binder interface -----
Christopher Tatedf01dea2009-06-09 20:45:02 -07001366
Christopher Tatea8bf8152009-04-30 11:36:21 -07001367 public void dataChanged(String packageName) throws RemoteException {
Christopher Tate487529a2009-04-29 14:03:25 -07001368 // Record that we need a backup pass for the caller. Since multiple callers
1369 // may share a uid, we need to note all candidates within that uid and schedule
1370 // a backup pass for each of them.
Dan Egnorbb9001c2009-07-27 12:20:13 -07001371 EventLog.writeEvent(BACKUP_DATA_CHANGED_EVENT, packageName);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001372
Christopher Tate63d27002009-06-16 17:16:42 -07001373 // If the caller does not hold the BACKUP permission, it can only request a
1374 // backup of its own data.
1375 HashSet<ApplicationInfo> targets;
Dianne Hackborncf098292009-07-01 19:55:20 -07001376 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tate63d27002009-06-16 17:16:42 -07001377 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1378 targets = mBackupParticipants.get(Binder.getCallingUid());
1379 } else {
1380 // a caller with full permission can ask to back up any participating app
1381 // !!! TODO: allow backup of ANY app?
Christopher Tate63d27002009-06-16 17:16:42 -07001382 targets = new HashSet<ApplicationInfo>();
1383 int N = mBackupParticipants.size();
1384 for (int i = 0; i < N; i++) {
1385 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1386 if (s != null) {
1387 targets.addAll(s);
1388 }
1389 }
1390 }
Christopher Tate487529a2009-04-29 14:03:25 -07001391 if (targets != null) {
1392 synchronized (mQueueLock) {
1393 // Note that this client has made data changes that need to be backed up
Christopher Tate181fafa2009-05-14 11:12:14 -07001394 for (ApplicationInfo app : targets) {
Christopher Tatea8bf8152009-04-30 11:36:21 -07001395 // validate the caller-supplied package name against the known set of
1396 // packages associated with this uid
Christopher Tate181fafa2009-05-14 11:12:14 -07001397 if (app.packageName.equals(packageName)) {
Joe Onorato8ad02812009-05-13 01:41:44 -04001398 // Add the caller to the set of pending backups. If there is
1399 // one already there, then overwrite it, but no harm done.
Christopher Tate181fafa2009-05-14 11:12:14 -07001400 BackupRequest req = new BackupRequest(app, false);
Christopher Tatea7de3842009-07-07 14:50:26 -07001401 if (mPendingBackups.put(app, req) == null) {
1402 // Journal this request in case of crash. The put()
1403 // operation returned null when this package was not already
1404 // in the set; we want to avoid touching the disk redundantly.
1405 writeToJournalLocked(packageName);
Christopher Tate487529a2009-04-29 14:03:25 -07001406
Christopher Tate22b60d82009-07-07 16:36:02 -07001407 if (DEBUG) {
1408 int numKeys = mPendingBackups.size();
1409 Log.d(TAG, "Now awaiting backup for " + numKeys + " participants:");
1410 for (BackupRequest b : mPendingBackups.values()) {
1411 Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName);
1412 }
1413 }
1414 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001415 }
1416 }
Christopher Tate487529a2009-04-29 14:03:25 -07001417 }
Christopher Tatedf01dea2009-06-09 20:45:02 -07001418 } else {
Christopher Tate73e02522009-07-15 14:18:26 -07001419 Log.w(TAG, "dataChanged but no participant pkg='" + packageName + "'"
1420 + " uid=" + Binder.getCallingUid());
Christopher Tate487529a2009-04-29 14:03:25 -07001421 }
1422 }
Christopher Tate46758122009-05-06 11:22:00 -07001423
Christopher Tatecde87f42009-06-12 12:55:53 -07001424 private void writeToJournalLocked(String str) {
1425 if (mJournalStream != null) {
1426 try {
1427 mJournalStream.writeUTF(str);
1428 } catch (IOException e) {
1429 Log.e(TAG, "Error writing to backup journal");
1430 mJournalStream = null;
1431 mJournal = null;
1432 }
1433 }
1434 }
1435
Christopher Tateee0e78a2009-07-02 11:17:03 -07001436 // Clear the given package's backup data from the current transport
1437 public void clearBackupData(String packageName) {
1438 if (DEBUG) Log.v(TAG, "clearBackupData() of " + packageName);
1439 PackageInfo info;
1440 try {
1441 info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
1442 } catch (NameNotFoundException e) {
1443 Log.d(TAG, "No such package '" + packageName + "' - not clearing backup data");
1444 return;
1445 }
1446
1447 // If the caller does not hold the BACKUP permission, it can only request a
1448 // wipe of its own backed-up data.
1449 HashSet<ApplicationInfo> apps;
Christopher Tate4e3e50c2009-07-02 12:14:05 -07001450 if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
Christopher Tateee0e78a2009-07-02 11:17:03 -07001451 Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
1452 apps = mBackupParticipants.get(Binder.getCallingUid());
1453 } else {
1454 // a caller with full permission can ask to back up any participating app
1455 // !!! TODO: allow data-clear of ANY app?
1456 if (DEBUG) Log.v(TAG, "Privileged caller, allowing clear of other apps");
1457 apps = new HashSet<ApplicationInfo>();
1458 int N = mBackupParticipants.size();
1459 for (int i = 0; i < N; i++) {
1460 HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i);
1461 if (s != null) {
1462 apps.addAll(s);
1463 }
1464 }
1465 }
1466
1467 // now find the given package in the set of candidate apps
1468 for (ApplicationInfo app : apps) {
1469 if (app.packageName.equals(packageName)) {
1470 if (DEBUG) Log.v(TAG, "Found the app - running clear process");
1471 // found it; fire off the clear request
1472 synchronized (mQueueLock) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001473 mWakelock.acquire();
Christopher Tateee0e78a2009-07-02 11:17:03 -07001474 Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR,
1475 new ClearParams(getTransport(mCurrentTransport), info));
1476 mBackupHandler.sendMessage(msg);
1477 }
1478 break;
1479 }
1480 }
1481 }
1482
Christopher Tateace7f092009-06-15 18:07:25 -07001483 // Run a backup pass immediately for any applications that have declared
1484 // that they have pending updates.
1485 public void backupNow() throws RemoteException {
Joe Onorato5933a492009-07-23 18:24:08 -04001486 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow");
Christopher Tate043dadc2009-06-02 16:11:00 -07001487
Christopher Tateace7f092009-06-15 18:07:25 -07001488 if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass");
Christopher Tate46758122009-05-06 11:22:00 -07001489 synchronized (mQueueLock) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001490 try {
1491 if (DEBUG) Log.v(TAG, "sending immediate backup broadcast");
1492 mRunBackupIntent.send();
1493 } catch (PendingIntent.CanceledException e) {
1494 // should never happen
1495 Log.e(TAG, "run-backup intent cancelled!");
1496 }
Christopher Tate46758122009-05-06 11:22:00 -07001497 }
1498 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001499
Christopher Tate8031a3d2009-07-06 16:36:05 -07001500 // Enable/disable the backup service
Christopher Tate6ef58a12009-06-29 14:56:28 -07001501 public void setBackupEnabled(boolean enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001502 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1503 "setBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07001504
1505 boolean wasEnabled = mEnabled;
1506 synchronized (this) {
Dianne Hackborncf098292009-07-01 19:55:20 -07001507 Settings.Secure.putInt(mContext.getContentResolver(),
1508 Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0);
Christopher Tate6ef58a12009-06-29 14:56:28 -07001509 mEnabled = enable;
1510 }
1511
Christopher Tate49401dd2009-07-01 12:34:29 -07001512 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07001513 if (enable && !wasEnabled && mProvisioned) {
Christopher Tate49401dd2009-07-01 12:34:29 -07001514 // if we've just been enabled, start scheduling backup passes
Christopher Tate8031a3d2009-07-06 16:36:05 -07001515 startBackupAlarmsLocked(BACKUP_INTERVAL);
Christopher Tate49401dd2009-07-01 12:34:29 -07001516 } else if (!enable) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001517 // No longer enabled, so stop running backups
1518 mAlarmManager.cancel(mRunBackupIntent);
Christopher Tate6ef58a12009-06-29 14:56:28 -07001519 }
1520 }
Christopher Tate49401dd2009-07-01 12:34:29 -07001521 }
Christopher Tate6ef58a12009-06-29 14:56:28 -07001522
Christopher Tate8031a3d2009-07-06 16:36:05 -07001523 // Mark the backup service as having been provisioned
1524 public void setBackupProvisioned(boolean available) {
1525 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1526 "setBackupProvisioned");
1527
1528 boolean wasProvisioned = mProvisioned;
1529 synchronized (this) {
1530 Settings.Secure.putInt(mContext.getContentResolver(),
1531 Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0);
1532 mProvisioned = available;
1533 }
1534
1535 synchronized (mQueueLock) {
1536 if (available && !wasProvisioned && mEnabled) {
1537 // we're now good to go, so start the backup alarms
1538 startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL);
1539 } else if (!available) {
1540 // No longer enabled, so stop running backups
1541 Log.w(TAG, "Backup service no longer provisioned");
1542 mAlarmManager.cancel(mRunBackupIntent);
1543 }
1544 }
1545 }
1546
1547 private void startBackupAlarmsLocked(long delayBeforeFirstBackup) {
1548 long when = System.currentTimeMillis() + delayBeforeFirstBackup;
1549 mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, when,
1550 BACKUP_INTERVAL, mRunBackupIntent);
1551 }
1552
Christopher Tate6ef58a12009-06-29 14:56:28 -07001553 // Report whether the backup mechanism is currently enabled
1554 public boolean isBackupEnabled() {
Joe Onorato5933a492009-07-23 18:24:08 -04001555 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled");
Christopher Tate6ef58a12009-06-29 14:56:28 -07001556 return mEnabled; // no need to synchronize just to read it
1557 }
1558
Christopher Tate91717492009-06-26 21:07:13 -07001559 // Report the name of the currently active transport
1560 public String getCurrentTransport() {
Joe Onorato5933a492009-07-23 18:24:08 -04001561 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate4e3e50c2009-07-02 12:14:05 -07001562 "getCurrentTransport");
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001563 Log.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport);
Christopher Tate91717492009-06-26 21:07:13 -07001564 return mCurrentTransport;
Christopher Tateace7f092009-06-15 18:07:25 -07001565 }
1566
Christopher Tate91717492009-06-26 21:07:13 -07001567 // Report all known, available backup transports
1568 public String[] listAllTransports() {
Christopher Tate34ebd0e2009-07-06 15:44:54 -07001569 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports");
Christopher Tate043dadc2009-06-02 16:11:00 -07001570
Christopher Tate91717492009-06-26 21:07:13 -07001571 String[] list = null;
1572 ArrayList<String> known = new ArrayList<String>();
1573 for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) {
1574 if (entry.getValue() != null) {
1575 known.add(entry.getKey());
1576 }
1577 }
1578
1579 if (known.size() > 0) {
1580 list = new String[known.size()];
1581 known.toArray(list);
1582 }
1583 return list;
1584 }
1585
1586 // Select which transport to use for the next backup operation. If the given
1587 // name is not one of the available transports, no action is taken and the method
1588 // returns null.
1589 public String selectBackupTransport(String transport) {
Joe Onorato5933a492009-07-23 18:24:08 -04001590 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport");
Christopher Tate91717492009-06-26 21:07:13 -07001591
1592 synchronized (mTransports) {
1593 String prevTransport = null;
1594 if (mTransports.get(transport) != null) {
1595 prevTransport = mCurrentTransport;
1596 mCurrentTransport = transport;
Dianne Hackborncf098292009-07-01 19:55:20 -07001597 Settings.Secure.putString(mContext.getContentResolver(),
1598 Settings.Secure.BACKUP_TRANSPORT, transport);
Christopher Tate91717492009-06-26 21:07:13 -07001599 Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport
1600 + " returning " + prevTransport);
1601 } else {
1602 Log.w(TAG, "Attempt to select unavailable transport " + transport);
1603 }
1604 return prevTransport;
1605 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001606 }
1607
1608 // Callback: a requested backup agent has been instantiated. This should only
1609 // be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001610 public void agentConnected(String packageName, IBinder agentBinder) {
Christopher Tate043dadc2009-06-02 16:11:00 -07001611 synchronized(mAgentConnectLock) {
1612 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1613 Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
1614 IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
1615 mConnectedAgent = agent;
1616 mConnecting = false;
1617 } else {
1618 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1619 + " claiming agent connected");
1620 }
1621 mAgentConnectLock.notifyAll();
1622 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001623 }
1624
1625 // Callback: a backup agent has failed to come up, or has unexpectedly quit.
1626 // If the agent failed to come up in the first place, the agentBinder argument
Christopher Tate043dadc2009-06-02 16:11:00 -07001627 // will be null. This should only be called from the Activity Manager.
Christopher Tate181fafa2009-05-14 11:12:14 -07001628 public void agentDisconnected(String packageName) {
1629 // TODO: handle backup being interrupted
Christopher Tate043dadc2009-06-02 16:11:00 -07001630 synchronized(mAgentConnectLock) {
1631 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
1632 mConnectedAgent = null;
1633 mConnecting = false;
1634 } else {
1635 Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
1636 + " claiming agent disconnected");
1637 }
1638 mAgentConnectLock.notifyAll();
1639 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001640 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001641
Christopher Tate8c850b72009-06-07 19:33:20 -07001642 // Hand off a restore session
Christopher Tate91717492009-06-26 21:07:13 -07001643 public IRestoreSession beginRestoreSession(String transport) {
Joe Onorato5933a492009-07-23 18:24:08 -04001644 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "beginRestoreSession");
Christopher Tatef68eb502009-06-16 11:02:01 -07001645
1646 synchronized(this) {
1647 if (mActiveRestoreSession != null) {
1648 Log.d(TAG, "Restore session requested but one already active");
1649 return null;
1650 }
Christopher Tate91717492009-06-26 21:07:13 -07001651 mActiveRestoreSession = new RestoreSession(transport);
Christopher Tatef68eb502009-06-16 11:02:01 -07001652 }
1653 return mActiveRestoreSession;
Christopher Tate8c850b72009-06-07 19:33:20 -07001654 }
Christopher Tate043dadc2009-06-02 16:11:00 -07001655
Christopher Tate9b3905c2009-06-08 15:24:01 -07001656 // ----- Restore session -----
1657
1658 class RestoreSession extends IRestoreSession.Stub {
Christopher Tatef68eb502009-06-16 11:02:01 -07001659 private static final String TAG = "RestoreSession";
1660
Christopher Tate9b3905c2009-06-08 15:24:01 -07001661 private IBackupTransport mRestoreTransport = null;
1662 RestoreSet[] mRestoreSets = null;
1663
Christopher Tate91717492009-06-26 21:07:13 -07001664 RestoreSession(String transport) {
1665 mRestoreTransport = getTransport(transport);
Christopher Tate9b3905c2009-06-08 15:24:01 -07001666 }
1667
1668 // --- Binder interface ---
1669 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Joe Onorato5933a492009-07-23 18:24:08 -04001670 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001671 "getAvailableRestoreSets");
1672
Christopher Tatef68eb502009-06-16 11:02:01 -07001673 try {
Christopher Tate9b3905c2009-06-08 15:24:01 -07001674 synchronized(this) {
Christopher Tate43383042009-07-13 15:17:13 -07001675 if (mRestoreTransport == null) {
1676 Log.w(TAG, "Null transport getting restore sets");
1677 } else if (mRestoreSets == null) { // valid transport; do the one-time fetch
Christopher Tate9b3905c2009-06-08 15:24:01 -07001678 mRestoreSets = mRestoreTransport.getAvailableRestoreSets();
Dan Egnorbb9001c2009-07-27 12:20:13 -07001679 if (mRestoreSets == null) EventLog.writeEvent(RESTORE_TRANSPORT_FAILURE_EVENT);
Christopher Tate9b3905c2009-06-08 15:24:01 -07001680 }
1681 return mRestoreSets;
1682 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001683 } catch (RuntimeException e) {
1684 Log.d(TAG, "getAvailableRestoreSets exception");
1685 e.printStackTrace();
1686 throw e;
1687 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001688 }
1689
Dan Egnor156411d2009-06-26 13:20:02 -07001690 public int performRestore(long token, IRestoreObserver observer)
Christopher Tate7d562ec2009-06-25 18:03:43 -07001691 throws android.os.RemoteException {
Joe Onorato5933a492009-07-23 18:24:08 -04001692 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "performRestore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001693
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001694 Log.d(TAG, "performRestore token=" + token + " observer=" + observer);
1695
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001696 if (mRestoreSets != null) {
1697 for (int i = 0; i < mRestoreSets.length; i++) {
1698 if (token == mRestoreSets[i].token) {
Christopher Tateb6787f22009-07-02 17:40:45 -07001699 mWakelock.acquire();
Christopher Tate7d562ec2009-06-25 18:03:43 -07001700 Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
Dan Egnor156411d2009-06-26 13:20:02 -07001701 msg.obj = new RestoreParams(mRestoreTransport, observer, token);
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001702 mBackupHandler.sendMessage(msg);
1703 return 0;
1704 }
1705 }
Christopher Tatef68eb502009-06-16 11:02:01 -07001706 } else {
1707 if (DEBUG) Log.v(TAG, "No current restore set, not doing restore");
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001708 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001709 return -1;
1710 }
1711
1712 public void endRestoreSession() throws android.os.RemoteException {
Joe Onorato5933a492009-07-23 18:24:08 -04001713 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
Christopher Tate9bbc21a2009-06-10 20:23:25 -07001714 "endRestoreSession");
1715
Joe Onorato9a5e3e12009-07-01 21:04:03 -04001716 Log.d(TAG, "endRestoreSession");
1717
Dan Egnorefe52642009-06-24 00:16:33 -07001718 mRestoreTransport.finishRestore();
Christopher Tate9b3905c2009-06-08 15:24:01 -07001719 mRestoreTransport = null;
Christopher Tatef68eb502009-06-16 11:02:01 -07001720 synchronized(BackupManagerService.this) {
1721 if (BackupManagerService.this.mActiveRestoreSession == this) {
1722 BackupManagerService.this.mActiveRestoreSession = null;
1723 } else {
1724 Log.e(TAG, "ending non-current restore session");
1725 }
1726 }
Christopher Tate9b3905c2009-06-08 15:24:01 -07001727 }
1728 }
1729
Christopher Tate043dadc2009-06-02 16:11:00 -07001730
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001731 @Override
1732 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1733 synchronized (mQueueLock) {
Christopher Tate8031a3d2009-07-06 16:36:05 -07001734 pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled")
1735 + " / " + (!mProvisioned ? "not " : "") + "provisioned");
Christopher Tate91717492009-06-26 21:07:13 -07001736 pw.println("Available transports:");
1737 for (String t : listAllTransports()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001738 String pad = (t.equals(mCurrentTransport)) ? " * " : " ";
1739 pw.println(pad + t);
Christopher Tate91717492009-06-26 21:07:13 -07001740 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001741 int N = mBackupParticipants.size();
Christopher Tatece0bf062009-07-01 11:43:53 -07001742 pw.println("Participants: " + N);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001743 for (int i=0; i<N; i++) {
1744 int uid = mBackupParticipants.keyAt(i);
1745 pw.print(" uid: ");
1746 pw.println(uid);
Christopher Tate181fafa2009-05-14 11:12:14 -07001747 HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i);
1748 for (ApplicationInfo app: participants) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001749 pw.println(" " + app.toString());
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001750 }
1751 }
Christopher Tate73e02522009-07-15 14:18:26 -07001752 pw.println("Ever backed up: " + mEverStoredApps.size());
1753 for (String pkg : mEverStoredApps) {
1754 pw.println(" " + pkg);
1755 }
Christopher Tate6aa41f42009-06-19 14:14:22 -07001756 pw.println("Pending: " + mPendingBackups.size());
1757 for (BackupRequest req : mPendingBackups.values()) {
Christopher Tate6ef58a12009-06-29 14:56:28 -07001758 pw.println(" " + req);
Christopher Tate181fafa2009-05-14 11:12:14 -07001759 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001760 }
1761 }
Christopher Tate487529a2009-04-29 14:03:25 -07001762}