| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
| 20 | import android.app.IActivityManager; |
| 21 | import android.app.IApplicationThread; |
| 22 | import android.app.IBackupAgent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 27 | import android.content.IntentFilter; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 29 | import android.content.pm.ApplicationInfo; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 30 | import android.content.pm.IPackageDataObserver; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager.NameNotFoundException; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 34 | import android.content.pm.Signature; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 36 | import android.os.Binder; |
| Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 37 | import android.os.Build; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 38 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 39 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.IBinder; |
| 42 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 43 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 44 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 45 | import android.os.RemoteException; |
| 46 | import android.util.Log; |
| 47 | import android.util.SparseArray; |
| 48 | |
| 49 | import android.backup.IBackupManager; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 50 | import android.backup.IRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 51 | import android.backup.BackupManager; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 52 | import android.backup.RestoreSet; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 53 | |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 54 | import com.android.internal.backup.LocalTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 55 | import com.android.internal.backup.IBackupTransport; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 56 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 57 | import com.android.server.PackageManagerBackupAgent; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 58 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 59 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 60 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 61 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 63 | import java.io.FileNotFoundException; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 64 | import java.io.IOException; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 65 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 66 | import java.io.RandomAccessFile; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 67 | import java.lang.String; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 70 | import java.util.HashSet; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 71 | import java.util.Iterator; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 72 | import java.util.List; |
| 73 | |
| 74 | class BackupManagerService extends IBackupManager.Stub { |
| 75 | private static final String TAG = "BackupManagerService"; |
| 76 | private static final boolean DEBUG = true; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 77 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 78 | // Default time to wait after data changes before we back up the data |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 79 | private static final long COLLECTION_INTERVAL = 1000; |
| 80 | //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 81 | |
| 82 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 83 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 84 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 85 | |
| 86 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 87 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 88 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 89 | private Context mContext; |
| 90 | private PackageManager mPackageManager; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 91 | private final IActivityManager mActivityManager; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 92 | private final BackupHandler mBackupHandler = new BackupHandler(); |
| 93 | // map UIDs to the set of backup client services within that UID's app set |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 94 | private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| 95 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 96 | // set of backup services that have pending changes |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 97 | private class BackupRequest { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 98 | public ApplicationInfo appInfo; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 99 | public boolean fullBackup; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 100 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 101 | BackupRequest(ApplicationInfo app, boolean isFull) { |
| 102 | appInfo = app; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 103 | fullBackup = isFull; |
| 104 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 105 | |
| 106 | public String toString() { |
| 107 | return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}"; |
| 108 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 109 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 110 | // Backups that we haven't started yet. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 111 | private HashMap<ApplicationInfo,BackupRequest> mPendingBackups |
| 112 | = new HashMap<ApplicationInfo,BackupRequest>(); |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 113 | // Do we need to back up the package manager metadata on the next pass? |
| 114 | private boolean mDoPackageManager; |
| 115 | private static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 116 | |
| 117 | // locking around the pending-backup management |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 118 | private final Object mQueueLock = new Object(); |
| 119 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 120 | // The thread performing the sequence of queued backups binds to each app's agent |
| 121 | // in succession. Bind notifications are asynchronously delivered through the |
| 122 | // Activity Manager; use this lock object to signal when a requested binding has |
| 123 | // completed. |
| 124 | private final Object mAgentConnectLock = new Object(); |
| 125 | private IBackupAgent mConnectedAgent; |
| 126 | private volatile boolean mConnecting; |
| 127 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 128 | // A similar synchronicity mechanism around clearing apps' data for restore |
| 129 | private final Object mClearDataLock = new Object(); |
| 130 | private volatile boolean mClearingData; |
| 131 | |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 132 | // Current active transport & restore session |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 133 | private int mTransportId; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 134 | private IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 135 | private RestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 136 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 137 | private File mStateDir; |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 138 | private File mDataDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 139 | private File mJournalDir; |
| 140 | private File mJournal; |
| 141 | private RandomAccessFile mJournalStream; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 142 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 143 | public BackupManagerService(Context context) { |
| 144 | mContext = context; |
| 145 | mPackageManager = context.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 146 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 147 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 148 | // Set up our bookkeeping |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 149 | mStateDir = new File(Environment.getDataDirectory(), "backup"); |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 150 | mStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 151 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 152 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 153 | // Set up the backup-request journaling |
| 154 | mJournalDir = new File(mStateDir, "pending"); |
| 155 | mJournalDir.mkdirs(); |
| 156 | makeJournalLocked(); // okay because no other threads are running yet |
| 157 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 158 | // Build our mapping of uid to backup client services. This implicitly |
| 159 | // schedules a backup pass on the Package Manager metadata the first |
| 160 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 161 | synchronized (mBackupParticipants) { |
| 162 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 165 | // Set up our transport options and initialize the default transport |
| 166 | // TODO: Have transports register themselves somehow? |
| 167 | // TODO: Don't create transports that we don't need to? |
| Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 168 | mTransportId = BackupManager.TRANSPORT_LOCAL; |
| 169 | //mTransportId = BackupManager.TRANSPORT_GOOGLE; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 170 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| 171 | mGoogleTransport = null; |
| 172 | |
| 173 | // Attach to the Google backup transport. |
| 174 | Intent intent = new Intent().setComponent(new ComponentName( |
| 175 | "com.google.android.backup", |
| 176 | "com.google.android.backup.BackupTransportService")); |
| 177 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 178 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 179 | // Now that we know about valid backup participants, parse any |
| 180 | // leftover journal files and schedule a new backup pass |
| 181 | parseLeftoverJournals(); |
| 182 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 183 | // Register for broadcasts about package install, etc., so we can |
| 184 | // update the provider list. |
| 185 | IntentFilter filter = new IntentFilter(); |
| 186 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 187 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 188 | filter.addDataScheme("package"); |
| 189 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 190 | } |
| 191 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 192 | private void makeJournalLocked() { |
| 193 | try { |
| 194 | mJournal = File.createTempFile("journal", null, mJournalDir); |
| 195 | mJournalStream = new RandomAccessFile(mJournal, "rwd"); |
| 196 | } catch (IOException e) { |
| 197 | Log.e(TAG, "Unable to write backup journals"); |
| 198 | mJournal = null; |
| 199 | mJournalStream = null; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private void parseLeftoverJournals() { |
| 204 | if (mJournal != null) { |
| 205 | File[] allJournals = mJournalDir.listFiles(); |
| 206 | for (File f : allJournals) { |
| 207 | if (f.compareTo(mJournal) != 0) { |
| 208 | // This isn't the current journal, so it must be a leftover. Read |
| 209 | // out the package names mentioned there and schedule them for |
| 210 | // backup. |
| 211 | try { |
| 212 | Log.i(TAG, "Found stale backup journal, scheduling:"); |
| 213 | RandomAccessFile in = new RandomAccessFile(f, "r"); |
| 214 | while (true) { |
| 215 | String packageName = in.readUTF(); |
| 216 | Log.i(TAG, " + " + packageName); |
| 217 | dataChanged(packageName); |
| 218 | } |
| 219 | } catch (EOFException e) { |
| 220 | // no more data; we're done |
| 221 | } catch (Exception e) { |
| 222 | // can't read it or other error; just skip it |
| 223 | } finally { |
| 224 | // close/delete the file |
| 225 | f.delete(); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 232 | // ----- Track installation/removal of packages ----- |
| 233 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 234 | public void onReceive(Context context, Intent intent) { |
| 235 | if (DEBUG) Log.d(TAG, "Received broadcast " + intent); |
| 236 | |
| 237 | Uri uri = intent.getData(); |
| 238 | if (uri == null) { |
| 239 | return; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 240 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 241 | String pkgName = uri.getSchemeSpecificPart(); |
| 242 | if (pkgName == null) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | String action = intent.getAction(); |
| 247 | if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 248 | synchronized (mBackupParticipants) { |
| 249 | Bundle extras = intent.getExtras(); |
| 250 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 251 | // The package was just upgraded |
| 252 | updatePackageParticipantsLocked(pkgName); |
| 253 | } else { |
| 254 | // The package was just added |
| 255 | addPackageParticipantsLocked(pkgName); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 260 | Bundle extras = intent.getExtras(); |
| 261 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 262 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 263 | } else { |
| 264 | synchronized (mBackupParticipants) { |
| 265 | removePackageParticipantsLocked(pkgName); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | }; |
| 271 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 272 | // ----- Track connection to GoogleBackupTransport service ----- |
| 273 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 274 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 275 | if (DEBUG) Log.v(TAG, "Connected to Google transport"); |
| 276 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| 277 | } |
| 278 | |
| 279 | public void onServiceDisconnected(ComponentName name) { |
| 280 | if (DEBUG) Log.v(TAG, "Disconnected from Google transport"); |
| 281 | mGoogleTransport = null; |
| 282 | } |
| 283 | }; |
| 284 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 285 | // ----- Run the actual backup process asynchronously ----- |
| 286 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 287 | private class BackupHandler extends Handler { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 288 | public void handleMessage(Message msg) { |
| 289 | |
| 290 | switch (msg.what) { |
| 291 | case MSG_RUN_BACKUP: |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 292 | { |
| 293 | IBackupTransport transport = getTransport(mTransportId); |
| 294 | if (transport == null) { |
| 295 | Log.v(TAG, "Backup requested but no transport available"); |
| 296 | break; |
| 297 | } |
| 298 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 299 | // snapshot the pending-backup set and work on that |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 300 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 301 | File oldJournal = mJournal; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 302 | synchronized (mQueueLock) { |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 303 | if (mPendingBackups.size() == 0) { |
| 304 | Log.v(TAG, "Backup requested but nothing pending"); |
| 305 | break; |
| 306 | } |
| 307 | |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 308 | for (BackupRequest b: mPendingBackups.values()) { |
| 309 | queue.add(b); |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 310 | } |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 311 | Log.v(TAG, "clearing pending backups"); |
| 312 | mPendingBackups.clear(); |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 313 | |
| 314 | // Start a new backup-queue journal file too |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 315 | if (mJournalStream != null) { |
| 316 | try { |
| 317 | mJournalStream.close(); |
| 318 | } catch (IOException e) { |
| 319 | // don't need to do anything |
| 320 | } |
| 321 | makeJournalLocked(); |
| 322 | } |
| 323 | |
| 324 | // At this point, we have started a new journal file, and the old |
| 325 | // file identity is being passed to the backup processing thread. |
| 326 | // When it completes successfully, that old journal file will be |
| 327 | // deleted. If we crash prior to that, the old journal is parsed |
| 328 | // at next boot and the journaled requests fulfilled. |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 329 | } |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 330 | |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 331 | (new PerformBackupThread(transport, queue, oldJournal)).start(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 332 | break; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 333 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 334 | |
| 335 | case MSG_RUN_FULL_BACKUP: |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 336 | break; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 337 | |
| 338 | case MSG_RUN_RESTORE: |
| 339 | { |
| 340 | int token = msg.arg1; |
| 341 | IBackupTransport transport = (IBackupTransport)msg.obj; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 342 | (new PerformRestoreThread(transport, token)).start(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 343 | break; |
| 344 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 345 | } |
| 346 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 349 | // Add the backup agents in the given package to our set of known backup participants. |
| 350 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 351 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 352 | // Look for apps that define the android:backupAgent attribute |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 353 | if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 354 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 355 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 358 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 359 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 360 | if (DEBUG) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 361 | Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| 362 | for (PackageInfo p : targetPkgs) { |
| 363 | Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 367 | for (PackageInfo pkg : targetPkgs) { |
| 368 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 369 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 370 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 371 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 372 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 373 | mBackupParticipants.put(uid, set); |
| 374 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 375 | set.add(pkg.applicationInfo); |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 376 | backUpPackageManagerData(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 377 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 381 | // Remove the given package's entry from our known active set. If |
| 382 | // 'packageName' is null, *all* participating apps will be removed. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 383 | void removePackageParticipantsLocked(String packageName) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 384 | if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 385 | List<PackageInfo> allApps = null; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 386 | if (packageName != null) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 387 | allApps = new ArrayList<PackageInfo>(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 388 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 389 | int flags = PackageManager.GET_SIGNATURES; |
| 390 | allApps.add(mPackageManager.getPackageInfo(packageName, flags)); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 391 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 392 | // just skip it (???) |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 393 | } |
| 394 | } else { |
| 395 | // all apps with agents |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 396 | allApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 397 | } |
| 398 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 401 | private void removePackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 402 | List<PackageInfo> agents) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 403 | if (DEBUG) { |
| 404 | Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| 405 | + ") removing " + agents.size() + " entries"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 406 | for (PackageInfo p : agents) { |
| 407 | Log.v(TAG, " - " + p); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 410 | for (PackageInfo pkg : agents) { |
| 411 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 412 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 413 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 414 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 415 | // Find the existing entry with the same package name, and remove it. |
| 416 | // We can't just remove(app) because the instances are different. |
| 417 | for (ApplicationInfo entry: set) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 418 | if (entry.packageName.equals(pkg.packageName)) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 419 | set.remove(entry); |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 420 | backUpPackageManagerData(); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 424 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 425 | mBackupParticipants.delete(uid); |
| 426 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 432 | // Returns the set of all applications that define an android:backupAgent attribute |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 433 | private List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 434 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 435 | int flags = PackageManager.GET_SIGNATURES; |
| 436 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 437 | int N = packages.size(); |
| 438 | for (int a = N-1; a >= 0; a--) { |
| 439 | ApplicationInfo app = packages.get(a).applicationInfo; |
| 440 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| 441 | || app.backupAgentName == null) { |
| 442 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 445 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 446 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 447 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 448 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 449 | // action cannot be passed a null package name. |
| 450 | void updatePackageParticipantsLocked(String packageName) { |
| 451 | if (packageName == null) { |
| 452 | Log.e(TAG, "updatePackageParticipants called with null package name"); |
| 453 | return; |
| 454 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 455 | if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 456 | |
| 457 | // brute force but small code size |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 458 | List<PackageInfo> allApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 459 | removePackageParticipantsLockedInner(packageName, allApps); |
| 460 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 463 | private void backUpPackageManagerData() { |
| 464 | // No need to schedule a backup just for the metadata; just piggyback on |
| 465 | // the next actual data backup. |
| 466 | synchronized(this) { |
| 467 | mDoPackageManager = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // The queue lock should be held when scheduling a backup pass |
| 472 | private void scheduleBackupPassLocked(long timeFromNowMillis) { |
| 473 | mBackupHandler.removeMessages(MSG_RUN_BACKUP); |
| 474 | mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, timeFromNowMillis); |
| 475 | } |
| 476 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 477 | // Return the given transport |
| 478 | private IBackupTransport getTransport(int transportID) { |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 479 | switch (transportID) { |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 480 | case BackupManager.TRANSPORT_LOCAL: |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 481 | Log.v(TAG, "Supplying local transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 482 | return mLocalTransport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 483 | |
| 484 | case BackupManager.TRANSPORT_GOOGLE: |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 485 | Log.v(TAG, "Supplying Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 486 | return mGoogleTransport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 487 | |
| 488 | default: |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 489 | Log.e(TAG, "Asked for unknown transport " + transportID); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 490 | return null; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 491 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 494 | // fire off a backup agent, blocking until it attaches or times out |
| 495 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 496 | IBackupAgent agent = null; |
| 497 | synchronized(mAgentConnectLock) { |
| 498 | mConnecting = true; |
| 499 | mConnectedAgent = null; |
| 500 | try { |
| 501 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| 502 | Log.d(TAG, "awaiting agent for " + app); |
| 503 | |
| 504 | // success; wait for the agent to arrive |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 505 | // only wait 10 seconds for the clear data to happen |
| 506 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 507 | while (mConnecting && mConnectedAgent == null |
| 508 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 509 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 510 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 511 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 512 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 513 | return null; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // if we timed out with no connect, abort and move on |
| 518 | if (mConnecting == true) { |
| 519 | Log.w(TAG, "Timeout waiting for agent " + app); |
| 520 | return null; |
| 521 | } |
| 522 | agent = mConnectedAgent; |
| 523 | } |
| 524 | } catch (RemoteException e) { |
| 525 | // can't happen |
| 526 | } |
| 527 | } |
| 528 | return agent; |
| 529 | } |
| 530 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 531 | // clear an application's data, blocking until the operation completes or times out |
| 532 | void clearApplicationDataSynchronous(String packageName) { |
| 533 | ClearDataObserver observer = new ClearDataObserver(); |
| 534 | |
| 535 | synchronized(mClearDataLock) { |
| 536 | mClearingData = true; |
| 537 | mPackageManager.clearApplicationUserData(packageName, observer); |
| 538 | |
| 539 | // only wait 10 seconds for the clear data to happen |
| 540 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 541 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 542 | try { |
| 543 | mClearDataLock.wait(5000); |
| 544 | } catch (InterruptedException e) { |
| 545 | // won't happen, but still. |
| 546 | mClearingData = false; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| 553 | public void onRemoveCompleted(String packageName, boolean succeeded) |
| 554 | throws android.os.RemoteException { |
| 555 | synchronized(mClearDataLock) { |
| 556 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 557 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 562 | // ----- Back up a set of applications via a worker thread ----- |
| 563 | |
| 564 | class PerformBackupThread extends Thread { |
| 565 | private static final String TAG = "PerformBackupThread"; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 566 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 567 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 568 | File mJournal; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 569 | |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 570 | public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 571 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 572 | mTransport = transport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 573 | mQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 574 | mJournal = journal; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void run() { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 579 | if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| 580 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 581 | // First, back up the package manager metadata if necessary |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 582 | boolean doPackageManager; |
| 583 | synchronized (BackupManagerService.this) { |
| 584 | doPackageManager = mDoPackageManager; |
| 585 | mDoPackageManager = false; |
| 586 | } |
| 587 | if (doPackageManager) { |
| 588 | // The package manager doesn't have a proper <application> etc, but since |
| 589 | // it's running here in the system process we can just set up its agent |
| 590 | // directly and use a synthetic BackupRequest. |
| 591 | if (DEBUG) Log.i(TAG, "Running PM backup pass as well"); |
| 592 | |
| 593 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 594 | mPackageManager, allAgentPackages()); |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 595 | BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false); |
| 596 | pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL; |
| 597 | processOneBackup(pmRequest, |
| 598 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), |
| 599 | mTransport); |
| 600 | } |
| 601 | |
| 602 | // Now run all the backups in our queue |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 603 | doQueuedBackups(mTransport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 604 | |
| 605 | // Finally, tear down the transport |
| 606 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 607 | if (!mTransport.finishBackup()) { |
| 608 | // STOPSHIP TODO: handle errors |
| 609 | Log.e(TAG, "Backup failure in finishBackup()"); |
| 610 | } |
| 611 | } catch (RemoteException e) { |
| 612 | Log.e(TAG, "Error in finishBackup()", e); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 613 | } |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 614 | |
| 615 | if (!mJournal.delete()) { |
| 616 | Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath()); |
| 617 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | private void doQueuedBackups(IBackupTransport transport) { |
| 621 | for (BackupRequest request : mQueue) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 622 | Log.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 623 | |
| 624 | IBackupAgent agent = null; |
| 625 | int mode = (request.fullBackup) |
| 626 | ? IApplicationThread.BACKUP_MODE_FULL |
| 627 | : IApplicationThread.BACKUP_MODE_INCREMENTAL; |
| 628 | try { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 629 | agent = bindToAgentSynchronous(request.appInfo, mode); |
| 630 | if (agent != null) { |
| 631 | processOneBackup(request, agent, transport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 632 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 633 | |
| 634 | // unbind even on timeout, just in case |
| 635 | mActivityManager.unbindBackupAgent(request.appInfo); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 636 | } catch (SecurityException ex) { |
| 637 | // Try for the next one. |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 638 | Log.d(TAG, "error in bind/backup", ex); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 639 | } catch (RemoteException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 640 | Log.v(TAG, "bind/backup threw"); |
| 641 | e.printStackTrace(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 642 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 643 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 646 | |
| 647 | void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) { |
| 648 | final String packageName = request.appInfo.packageName; |
| 649 | Log.d(TAG, "processOneBackup doBackup() on " + packageName); |
| 650 | |
| 651 | try { |
| 652 | // Look up the package info & signatures. This is first so that if it |
| 653 | // throws an exception, there's no file setup yet that would need to |
| 654 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 655 | PackageInfo packInfo; |
| 656 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 657 | // The metadata 'package' is synthetic |
| 658 | packInfo = new PackageInfo(); |
| 659 | packInfo.packageName = packageName; |
| 660 | } else { |
| 661 | packInfo = mPackageManager.getPackageInfo(packageName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 662 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 663 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 664 | |
| 665 | // !!! TODO: get the state file dir from the transport |
| 666 | File savedStateName = new File(mStateDir, packageName); |
| 667 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 668 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 669 | |
| 670 | // In a full backup, we pass a null ParcelFileDescriptor as |
| 671 | // the saved-state "file" |
| 672 | ParcelFileDescriptor savedState = (request.fullBackup) ? null |
| 673 | : ParcelFileDescriptor.open(savedStateName, |
| 674 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 675 | ParcelFileDescriptor.MODE_CREATE); |
| 676 | |
| 677 | backupDataName.delete(); |
| 678 | ParcelFileDescriptor backupData = |
| 679 | ParcelFileDescriptor.open(backupDataName, |
| 680 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 681 | ParcelFileDescriptor.MODE_CREATE); |
| 682 | |
| 683 | newStateName.delete(); |
| 684 | ParcelFileDescriptor newState = |
| 685 | ParcelFileDescriptor.open(newStateName, |
| 686 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 687 | ParcelFileDescriptor.MODE_CREATE); |
| 688 | |
| 689 | // Run the target's backup pass |
| 690 | boolean success = false; |
| 691 | try { |
| 692 | agent.doBackup(savedState, backupData, newState); |
| 693 | success = true; |
| 694 | } finally { |
| 695 | if (savedState != null) { |
| 696 | savedState.close(); |
| 697 | } |
| 698 | backupData.close(); |
| 699 | newState.close(); |
| 700 | } |
| 701 | |
| 702 | // Now propagate the newly-backed-up data to the transport |
| 703 | if (success) { |
| 704 | if (DEBUG) Log.v(TAG, "doBackup() success; calling transport"); |
| 705 | backupData = |
| 706 | ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 707 | if (!transport.performBackup(packInfo, backupData)) { |
| 708 | // STOPSHIP TODO: handle errors |
| 709 | Log.e(TAG, "Backup failure in performBackup()"); |
| 710 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 711 | |
| 712 | // !!! TODO: After successful transport, delete the now-stale data |
| 713 | // and juggle the files so that next time the new state is passed |
| 714 | //backupDataName.delete(); |
| 715 | newStateName.renameTo(savedStateName); |
| 716 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 717 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 718 | Log.e(TAG, "Error backing up " + packageName, e); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 723 | |
| 724 | // ----- Restore handling ----- |
| 725 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 726 | private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) { |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 727 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 728 | // !!! TODO: is this the right policy? |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 729 | if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| 730 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 731 | if ((storedSigs == null || storedSigs.length == 0) |
| 732 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 733 | return true; |
| 734 | } |
| 735 | if (storedSigs == null || deviceSigs == null) { |
| 736 | return false; |
| 737 | } |
| 738 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 739 | // !!! TODO: this demands that every stored signature match one |
| 740 | // that is present on device, and does not demand the converse. |
| 741 | // Is this this right policy? |
| 742 | int nStored = storedSigs.length; |
| 743 | int nDevice = deviceSigs.length; |
| 744 | |
| 745 | for (int i=0; i < nStored; i++) { |
| 746 | boolean match = false; |
| 747 | for (int j=0; j < nDevice; j++) { |
| 748 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 749 | match = true; |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | if (!match) { |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | return true; |
| 758 | } |
| 759 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 760 | class PerformRestoreThread extends Thread { |
| 761 | private IBackupTransport mTransport; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 762 | private int mToken; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 763 | private RestoreSet mImage; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 764 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 765 | class RestoreRequest { |
| 766 | public PackageInfo app; |
| 767 | public int storedAppVersion; |
| 768 | |
| 769 | RestoreRequest(PackageInfo _app, int _version) { |
| 770 | app = _app; |
| 771 | storedAppVersion = _version; |
| 772 | } |
| 773 | } |
| 774 | |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 775 | PerformRestoreThread(IBackupTransport transport, int restoreSetToken) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 776 | mTransport = transport; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 777 | mToken = restoreSetToken; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public void run() { |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 782 | if (DEBUG) Log.v(TAG, "Beginning restore process"); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 783 | /** |
| 784 | * Restore sequence: |
| 785 | * |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 786 | * 1. get the restore set description for our identity |
| 787 | * 2. for each app in the restore set: |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 788 | * 3.a. if it's restorable on this device, add it to the restore queue |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 789 | * 3. for each app in the restore queue: |
| 790 | * 3.a. clear the app data |
| 791 | * 3.b. get the restore data for the app from the transport |
| 792 | * 3.c. launch the backup agent for the app |
| 793 | * 3.d. agent.doRestore() with the data from the server |
| 794 | * 3.e. unbind the agent [and kill the app?] |
| 795 | * 4. shut down the transport |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 796 | */ |
| 797 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 798 | // build the set of apps to restore |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 799 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 800 | RestoreSet[] images = mTransport.getAvailableRestoreSets(); |
| 801 | if (images == null) { |
| 802 | // STOPSHIP TODO: Handle the failure somehow? |
| 803 | Log.e(TAG, "Error getting restore sets"); |
| 804 | return; |
| 805 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 806 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 807 | if (images.length == 0) { |
| 808 | Log.i(TAG, "No restore sets available"); |
| 809 | return; |
| 810 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 811 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 812 | mImage = images[0]; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 813 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 814 | // Get the list of all packages which have backup enabled. |
| 815 | // (Include the Package Manager metadata pseudo-package first.) |
| 816 | ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>(); |
| 817 | PackageInfo omPackage = new PackageInfo(); |
| 818 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 819 | restorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 820 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 821 | List<PackageInfo> agentPackages = allAgentPackages(); |
| 822 | restorePackages.addAll(agentPackages); |
| 823 | |
| 824 | // STOPSHIP TODO: pick out the set for this token (instead of images[0]) |
| 825 | long token = images[0].token; |
| 826 | if (!mTransport.startRestore(token, restorePackages.toArray(new PackageInfo[0]))) { |
| 827 | // STOPSHIP TODO: Handle the failure somehow? |
| 828 | Log.e(TAG, "Error starting restore operation"); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | String packageName = mTransport.nextRestorePackage(); |
| 833 | if (packageName == null) { |
| 834 | // STOPSHIP TODO: Handle the failure somehow? |
| 835 | Log.e(TAG, "Error getting first restore package"); |
| 836 | return; |
| 837 | } else if (packageName.equals("")) { |
| 838 | Log.i(TAG, "No restore data available"); |
| 839 | return; |
| 840 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 841 | Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| 842 | + "\", found only \"" + packageName + "\""); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | // Pull the Package Manager metadata from the restore set first |
| 847 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 848 | mPackageManager, agentPackages); |
| 849 | processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind())); |
| 850 | |
| 851 | for (;;) { |
| 852 | packageName = mTransport.nextRestorePackage(); |
| 853 | if (packageName == null) { |
| 854 | // STOPSHIP TODO: Handle the failure somehow? |
| 855 | Log.e(TAG, "Error getting next restore package"); |
| 856 | return; |
| 857 | } else if (packageName.equals("")) { |
| 858 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 859 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 860 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 861 | Metadata metaInfo = pmAgent.getRestoredMetadata(packageName); |
| 862 | if (metaInfo == null) { |
| 863 | Log.e(TAG, "Missing metadata for " + packageName); |
| 864 | continue; |
| 865 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 866 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 867 | int flags = PackageManager.GET_SIGNATURES; |
| 868 | PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 869 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| 870 | Log.w(TAG, "Package " + packageName |
| 871 | + " restore version [" + metaInfo.versionCode |
| 872 | + "] is too new for installed version [" |
| 873 | + packageInfo.versionCode + "]"); |
| 874 | continue; |
| 875 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 876 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 877 | if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) { |
| 878 | Log.w(TAG, "Signature mismatch restoring " + packageName); |
| 879 | continue; |
| 880 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 881 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 882 | if (DEBUG) Log.v(TAG, "Package " + packageName |
| 883 | + " restore version [" + metaInfo.versionCode |
| 884 | + "] is compatible with installed version [" |
| 885 | + packageInfo.versionCode + "]"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 886 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 887 | // Now perform the actual restore |
| 888 | clearApplicationDataSynchronous(packageName); |
| 889 | IBackupAgent agent = bindToAgentSynchronous( |
| 890 | packageInfo.applicationInfo, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 891 | IApplicationThread.BACKUP_MODE_RESTORE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 892 | if (agent == null) { |
| 893 | Log.w(TAG, "Can't find backup agent for " + packageName); |
| 894 | continue; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 897 | try { |
| 898 | processOneRestore(packageInfo, metaInfo.versionCode, agent); |
| 899 | } finally { |
| 900 | // unbind even on timeout or failure, just in case |
| 901 | mActivityManager.unbindBackupAgent(packageInfo.applicationInfo); |
| 902 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 903 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 904 | } catch (NameNotFoundException e) { |
| 905 | // STOPSHIP TODO: Handle the failure somehow? |
| 906 | Log.e(TAG, "Invalid paackage restoring data", e); |
| 907 | } catch (RemoteException e) { |
| 908 | // STOPSHIP TODO: Handle the failure somehow? |
| 909 | Log.e(TAG, "Error restoring data", e); |
| 910 | } finally { |
| 911 | try { |
| 912 | mTransport.finishRestore(); |
| 913 | } catch (RemoteException e) { |
| 914 | Log.e(TAG, "Error finishing restore", e); |
| 915 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 919 | // Do the guts of a restore of one application, using mTransport.getRestoreData(). |
| 920 | void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 921 | // !!! TODO: actually run the restore through mTransport |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 922 | final String packageName = app.packageName; |
| 923 | |
| 924 | // !!! TODO: get the dirs from the transport |
| 925 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
| 926 | backupDataName.delete(); |
| 927 | try { |
| 928 | ParcelFileDescriptor backupData = |
| 929 | ParcelFileDescriptor.open(backupDataName, |
| 930 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 931 | ParcelFileDescriptor.MODE_CREATE); |
| 932 | |
| 933 | // Run the transport's restore pass |
| 934 | // Run the target's backup pass |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 935 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 936 | if (!mTransport.getRestoreData(backupData)) { |
| 937 | // STOPSHIP TODO: Handle this error somehow? |
| 938 | Log.e(TAG, "Error getting restore data for " + packageName); |
| 939 | return; |
| 940 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 941 | } finally { |
| 942 | backupData.close(); |
| 943 | } |
| 944 | |
| 945 | // Okay, we have the data. Now have the agent do the restore. |
| 946 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 947 | ParcelFileDescriptor newState = |
| 948 | ParcelFileDescriptor.open(newStateName, |
| 949 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 950 | ParcelFileDescriptor.MODE_CREATE); |
| 951 | |
| 952 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 953 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 954 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 955 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 956 | agent.doRestore(backupData, appVersionCode, newState); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 957 | } finally { |
| 958 | newState.close(); |
| 959 | backupData.close(); |
| 960 | } |
| 961 | |
| 962 | // if everything went okay, remember the recorded state now |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 963 | File savedStateName = new File(mStateDir, packageName); |
| 964 | newStateName.renameTo(savedStateName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 965 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 966 | Log.e(TAG, "Error restoring data for " + packageName, e); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 967 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
| 971 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 972 | // ----- IBackupManager binder interface ----- |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 973 | |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 974 | public void dataChanged(String packageName) throws RemoteException { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 975 | // Record that we need a backup pass for the caller. Since multiple callers |
| 976 | // may share a uid, we need to note all candidates within that uid and schedule |
| 977 | // a backup pass for each of them. |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 978 | |
| 979 | Log.d(TAG, "dataChanged packageName=" + packageName); |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 980 | |
| 981 | // If the caller does not hold the BACKUP permission, it can only request a |
| 982 | // backup of its own data. |
| 983 | HashSet<ApplicationInfo> targets; |
| 984 | if ((mContext.checkPermission("android.permission.BACKUP", Binder.getCallingPid(), |
| 985 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 986 | targets = mBackupParticipants.get(Binder.getCallingUid()); |
| 987 | } else { |
| 988 | // a caller with full permission can ask to back up any participating app |
| 989 | // !!! TODO: allow backup of ANY app? |
| 990 | if (DEBUG) Log.v(TAG, "Privileged caller, allowing backup of other apps"); |
| 991 | targets = new HashSet<ApplicationInfo>(); |
| 992 | int N = mBackupParticipants.size(); |
| 993 | for (int i = 0; i < N; i++) { |
| 994 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 995 | if (s != null) { |
| 996 | targets.addAll(s); |
| 997 | } |
| 998 | } |
| 999 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1000 | if (targets != null) { |
| 1001 | synchronized (mQueueLock) { |
| 1002 | // Note that this client has made data changes that need to be backed up |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1003 | for (ApplicationInfo app : targets) { |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 1004 | // validate the caller-supplied package name against the known set of |
| 1005 | // packages associated with this uid |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1006 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1007 | // Add the caller to the set of pending backups. If there is |
| 1008 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1009 | BackupRequest req = new BackupRequest(app, false); |
| 1010 | mPendingBackups.put(app, req); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1011 | |
| 1012 | // Journal this request in case of crash |
| 1013 | writeToJournalLocked(packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1017 | if (DEBUG) { |
| 1018 | int numKeys = mPendingBackups.size(); |
| 1019 | Log.d(TAG, "Scheduling backup for " + numKeys + " participants:"); |
| 1020 | for (BackupRequest b : mPendingBackups.values()) { |
| 1021 | Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName); |
| 1022 | } |
| 1023 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1024 | // Schedule a backup pass in a few minutes. As backup-eligible data |
| 1025 | // keeps changing, continue to defer the backup pass until things |
| 1026 | // settle down, to avoid extra overhead. |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1027 | scheduleBackupPassLocked(COLLECTION_INTERVAL); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1028 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1029 | } else { |
| 1030 | Log.w(TAG, "dataChanged but no participant pkg " + packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1033 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1034 | private void writeToJournalLocked(String str) { |
| 1035 | if (mJournalStream != null) { |
| 1036 | try { |
| 1037 | mJournalStream.writeUTF(str); |
| 1038 | } catch (IOException e) { |
| 1039 | Log.e(TAG, "Error writing to backup journal"); |
| 1040 | mJournalStream = null; |
| 1041 | mJournal = null; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1046 | // Run a backup pass immediately for any applications that have declared |
| 1047 | // that they have pending updates. |
| 1048 | public void backupNow() throws RemoteException { |
| 1049 | mContext.enforceCallingPermission("android.permission.BACKUP", "tryBackupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1050 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1051 | if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1052 | synchronized (mQueueLock) { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1053 | scheduleBackupPassLocked(0); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1056 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1057 | // Report the currently active transport |
| 1058 | public int getCurrentTransport() { |
| 1059 | mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport"); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1060 | Log.v(TAG, "getCurrentTransport() returning " + mTransportId); |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1061 | return mTransportId; |
| 1062 | } |
| 1063 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1064 | // Select which transport to use for the next backup operation |
| 1065 | public int selectBackupTransport(int transportId) { |
| 1066 | mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport"); |
| 1067 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1068 | int prevTransport = mTransportId; |
| 1069 | mTransportId = transportId; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1070 | Log.v(TAG, "selectBackupTransport() set " + mTransportId + " returning " + prevTransport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1071 | return prevTransport; |
| 1072 | } |
| 1073 | |
| 1074 | // Callback: a requested backup agent has been instantiated. This should only |
| 1075 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1076 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1077 | synchronized(mAgentConnectLock) { |
| 1078 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1079 | Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| 1080 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 1081 | mConnectedAgent = agent; |
| 1082 | mConnecting = false; |
| 1083 | } else { |
| 1084 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 1085 | + " claiming agent connected"); |
| 1086 | } |
| 1087 | mAgentConnectLock.notifyAll(); |
| 1088 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 1092 | // If the agent failed to come up in the first place, the agentBinder argument |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1093 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1094 | public void agentDisconnected(String packageName) { |
| 1095 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1096 | synchronized(mAgentConnectLock) { |
| 1097 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1098 | mConnectedAgent = null; |
| 1099 | mConnecting = false; |
| 1100 | } else { |
| 1101 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 1102 | + " claiming agent disconnected"); |
| 1103 | } |
| 1104 | mAgentConnectLock.notifyAll(); |
| 1105 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1106 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1107 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1108 | // Hand off a restore session |
| 1109 | public IRestoreSession beginRestoreSession(int transportID) { |
| 1110 | mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1111 | |
| 1112 | synchronized(this) { |
| 1113 | if (mActiveRestoreSession != null) { |
| 1114 | Log.d(TAG, "Restore session requested but one already active"); |
| 1115 | return null; |
| 1116 | } |
| 1117 | mActiveRestoreSession = new RestoreSession(transportID); |
| 1118 | } |
| 1119 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1120 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1121 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1122 | // ----- Restore session ----- |
| 1123 | |
| 1124 | class RestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1125 | private static final String TAG = "RestoreSession"; |
| 1126 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1127 | private IBackupTransport mRestoreTransport = null; |
| 1128 | RestoreSet[] mRestoreSets = null; |
| 1129 | |
| 1130 | RestoreSession(int transportID) { |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1131 | mRestoreTransport = getTransport(transportID); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | // --- Binder interface --- |
| 1135 | public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException { |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1136 | mContext.enforceCallingPermission("android.permission.BACKUP", |
| 1137 | "getAvailableRestoreSets"); |
| 1138 | |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1139 | try { |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1140 | synchronized(this) { |
| 1141 | if (mRestoreSets == null) { |
| 1142 | mRestoreSets = mRestoreTransport.getAvailableRestoreSets(); |
| 1143 | } |
| 1144 | return mRestoreSets; |
| 1145 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1146 | } catch (RuntimeException e) { |
| 1147 | Log.d(TAG, "getAvailableRestoreSets exception"); |
| 1148 | e.printStackTrace(); |
| 1149 | throw e; |
| 1150 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | public int performRestore(int token) throws android.os.RemoteException { |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1154 | mContext.enforceCallingPermission("android.permission.BACKUP", "performRestore"); |
| 1155 | |
| 1156 | if (mRestoreSets != null) { |
| 1157 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 1158 | if (token == mRestoreSets[i].token) { |
| 1159 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE, |
| 1160 | mRestoreTransport); |
| 1161 | msg.arg1 = token; |
| 1162 | mBackupHandler.sendMessage(msg); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1166 | } else { |
| 1167 | if (DEBUG) Log.v(TAG, "No current restore set, not doing restore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1168 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1169 | return -1; |
| 1170 | } |
| 1171 | |
| 1172 | public void endRestoreSession() throws android.os.RemoteException { |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1173 | mContext.enforceCallingPermission("android.permission.BACKUP", |
| 1174 | "endRestoreSession"); |
| 1175 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame^] | 1176 | mRestoreTransport.finishRestore(); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1177 | mRestoreTransport = null; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1178 | synchronized(BackupManagerService.this) { |
| 1179 | if (BackupManagerService.this.mActiveRestoreSession == this) { |
| 1180 | BackupManagerService.this.mActiveRestoreSession = null; |
| 1181 | } else { |
| 1182 | Log.e(TAG, "ending non-current restore session"); |
| 1183 | } |
| 1184 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1188 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1189 | @Override |
| 1190 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1191 | synchronized (mQueueLock) { |
| 1192 | int N = mBackupParticipants.size(); |
| 1193 | pw.println("Participants:"); |
| 1194 | for (int i=0; i<N; i++) { |
| 1195 | int uid = mBackupParticipants.keyAt(i); |
| 1196 | pw.print(" uid: "); |
| 1197 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1198 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 1199 | for (ApplicationInfo app: participants) { |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1200 | pw.print(" "); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1201 | pw.println(app.toString()); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 1204 | pw.println("Pending: " + mPendingBackups.size()); |
| 1205 | for (BackupRequest req : mPendingBackups.values()) { |
| 1206 | pw.print(" "); |
| 1207 | pw.println(req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1208 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1211 | } |