| 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; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 26 | import android.content.IntentFilter; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 28 | import android.content.pm.IPackageDataObserver; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageInfo; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager.NameNotFoundException; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 35 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.IBinder; |
| 38 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 39 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 40 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
| 42 | import android.util.Log; |
| 43 | import android.util.SparseArray; |
| 44 | |
| 45 | import android.backup.IBackupManager; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 46 | import android.backup.IRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 47 | import android.backup.BackupManager; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 48 | import android.backup.RestoreSet; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 49 | |
| 50 | import com.android.internal.backup.AdbTransport; |
| 51 | import com.android.internal.backup.GoogleTransport; |
| 52 | import com.android.internal.backup.IBackupTransport; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 53 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 54 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 55 | import java.io.FileDescriptor; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 56 | import java.io.FileNotFoundException; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 57 | import java.io.IOException; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 58 | import java.io.PrintWriter; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 59 | import java.lang.String; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 60 | import java.util.ArrayList; |
| 61 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 63 | import java.util.Iterator; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 64 | import java.util.List; |
| 65 | |
| 66 | class BackupManagerService extends IBackupManager.Stub { |
| 67 | private static final String TAG = "BackupManagerService"; |
| 68 | private static final boolean DEBUG = true; |
| 69 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 70 | private static final long COLLECTION_INTERVAL = 1000; |
| 71 | //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 72 | |
| 73 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 74 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 75 | |
| 76 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 77 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 78 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 79 | private Context mContext; |
| 80 | private PackageManager mPackageManager; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 81 | private final IActivityManager mActivityManager; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 82 | private final BackupHandler mBackupHandler = new BackupHandler(); |
| 83 | // 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] | 84 | private SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| 85 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 86 | // set of backup services that have pending changes |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 87 | private class BackupRequest { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 88 | public ApplicationInfo appInfo; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 89 | public boolean fullBackup; |
| 90 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 91 | BackupRequest(ApplicationInfo app, boolean isFull) { |
| 92 | appInfo = app; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 93 | fullBackup = isFull; |
| 94 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 95 | |
| 96 | public String toString() { |
| 97 | return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}"; |
| 98 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 99 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 100 | // Backups that we haven't started yet. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 101 | private HashMap<ApplicationInfo,BackupRequest> mPendingBackups |
| 102 | = new HashMap<ApplicationInfo,BackupRequest>(); |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 103 | // Backups that we have started. These are separate to prevent starvation |
| 104 | // if an app keeps re-enqueuing itself. |
| 105 | private ArrayList<BackupRequest> mBackupQueue; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 106 | private final Object mQueueLock = new Object(); |
| 107 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 108 | // The thread performing the sequence of queued backups binds to each app's agent |
| 109 | // in succession. Bind notifications are asynchronously delivered through the |
| 110 | // Activity Manager; use this lock object to signal when a requested binding has |
| 111 | // completed. |
| 112 | private final Object mAgentConnectLock = new Object(); |
| 113 | private IBackupAgent mConnectedAgent; |
| 114 | private volatile boolean mConnecting; |
| 115 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 116 | // A similar synchronicity mechanism around clearing apps' data for restore |
| 117 | private final Object mClearDataLock = new Object(); |
| 118 | private volatile boolean mClearingData; |
| 119 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 120 | private int mTransportId; |
| 121 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 122 | private File mStateDir; |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 123 | private File mDataDir; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 124 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 125 | public BackupManagerService(Context context) { |
| 126 | mContext = context; |
| 127 | mPackageManager = context.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 128 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 129 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 130 | // Set up our bookkeeping |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 131 | mStateDir = new File(Environment.getDataDirectory(), "backup"); |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 132 | mStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 133 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 134 | mTransportId = BackupManager.TRANSPORT_GOOGLE; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 135 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 136 | // Build our mapping of uid to backup client services |
| 137 | synchronized (mBackupParticipants) { |
| 138 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 141 | // Register for broadcasts about package install, etc., so we can |
| 142 | // update the provider list. |
| 143 | IntentFilter filter = new IntentFilter(); |
| 144 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 145 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 146 | filter.addDataScheme("package"); |
| 147 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 148 | } |
| 149 | |
| 150 | // ----- Track installation/removal of packages ----- |
| 151 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 152 | public void onReceive(Context context, Intent intent) { |
| 153 | if (DEBUG) Log.d(TAG, "Received broadcast " + intent); |
| 154 | |
| 155 | Uri uri = intent.getData(); |
| 156 | if (uri == null) { |
| 157 | return; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 158 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 159 | String pkgName = uri.getSchemeSpecificPart(); |
| 160 | if (pkgName == null) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | String action = intent.getAction(); |
| 165 | if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 166 | synchronized (mBackupParticipants) { |
| 167 | Bundle extras = intent.getExtras(); |
| 168 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 169 | // The package was just upgraded |
| 170 | updatePackageParticipantsLocked(pkgName); |
| 171 | } else { |
| 172 | // The package was just added |
| 173 | addPackageParticipantsLocked(pkgName); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 178 | Bundle extras = intent.getExtras(); |
| 179 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 180 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 181 | } else { |
| 182 | synchronized (mBackupParticipants) { |
| 183 | removePackageParticipantsLocked(pkgName); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 190 | // ----- Run the actual backup process asynchronously ----- |
| 191 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 192 | private class BackupHandler extends Handler { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 193 | public void handleMessage(Message msg) { |
| 194 | |
| 195 | switch (msg.what) { |
| 196 | case MSG_RUN_BACKUP: |
| 197 | // snapshot the pending-backup set and work on that |
| 198 | synchronized (mQueueLock) { |
| Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 199 | if (mBackupQueue == null) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 200 | mBackupQueue = new ArrayList<BackupRequest>(); |
| Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 201 | for (BackupRequest b: mPendingBackups.values()) { |
| 202 | mBackupQueue.add(b); |
| 203 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 204 | mPendingBackups = new HashMap<ApplicationInfo,BackupRequest>(); |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 205 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 206 | // !!! TODO: start a new backup-queue journal file too |
| 207 | // WARNING: If we crash after this line, anything in mPendingBackups will |
| 208 | // be lost. FIX THIS. |
| 209 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 210 | (new PerformBackupThread(mTransportId, mBackupQueue)).run(); |
| 211 | break; |
| 212 | |
| 213 | case MSG_RUN_FULL_BACKUP: |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 219 | // Add the backup agents in the given package to our set of known backup participants. |
| 220 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 221 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 222 | // Look for apps that define the android:backupAgent attribute |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 223 | if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 224 | List<ApplicationInfo> targetApps = allAgentApps(); |
| 225 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 228 | private void addPackageParticipantsLockedInner(String packageName, |
| 229 | List<ApplicationInfo> targetApps) { |
| 230 | if (DEBUG) { |
| 231 | Log.v(TAG, "Adding " + targetApps.size() + " backup participants:"); |
| 232 | for (ApplicationInfo a : targetApps) { |
| 233 | Log.v(TAG, " " + a + " agent=" + a.backupAgentName); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | for (ApplicationInfo app : targetApps) { |
| 238 | if (packageName == null || app.packageName.equals(packageName)) { |
| 239 | int uid = app.uid; |
| 240 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 241 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 242 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 243 | mBackupParticipants.put(uid, set); |
| 244 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 245 | set.add(app); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 246 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 250 | // Remove the given package's backup services from our known active set. If |
| 251 | // 'packageName' is null, *all* backup services will be removed. |
| 252 | void removePackageParticipantsLocked(String packageName) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 253 | if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 254 | List<ApplicationInfo> allApps = null; |
| 255 | if (packageName != null) { |
| 256 | allApps = new ArrayList<ApplicationInfo>(); |
| 257 | try { |
| 258 | ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0); |
| 259 | allApps.add(app); |
| 260 | } catch (Exception e) { |
| 261 | // just skip it |
| 262 | } |
| 263 | } else { |
| 264 | // all apps with agents |
| 265 | allApps = allAgentApps(); |
| 266 | } |
| 267 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 270 | private void removePackageParticipantsLockedInner(String packageName, |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 271 | List<ApplicationInfo> agents) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 272 | if (DEBUG) { |
| 273 | Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| 274 | + ") removing " + agents.size() + " entries"); |
| 275 | for (ApplicationInfo a : agents) { |
| 276 | Log.v(TAG, " - " + a); |
| 277 | } |
| 278 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 279 | for (ApplicationInfo app : agents) { |
| 280 | if (packageName == null || app.packageName.equals(packageName)) { |
| 281 | int uid = app.uid; |
| 282 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 283 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 284 | // Find the existing entry with the same package name, and remove it. |
| 285 | // We can't just remove(app) because the instances are different. |
| 286 | for (ApplicationInfo entry: set) { |
| 287 | if (entry.packageName.equals(app.packageName)) { |
| 288 | set.remove(entry); |
| 289 | break; |
| 290 | } |
| 291 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 292 | if (set.size() == 0) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 293 | mBackupParticipants.delete(uid); } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 299 | // Returns the set of all applications that define an android:backupAgent attribute |
| 300 | private List<ApplicationInfo> allAgentApps() { |
| 301 | List<ApplicationInfo> allApps = mPackageManager.getInstalledApplications(0); |
| 302 | int N = allApps.size(); |
| 303 | if (N > 0) { |
| 304 | for (int a = N-1; a >= 0; a--) { |
| 305 | ApplicationInfo app = allApps.get(a); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 306 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| 307 | || app.backupAgentName == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 308 | allApps.remove(a); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | return allApps; |
| 313 | } |
| 314 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 315 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 316 | // action cannot be passed a null package name. |
| 317 | void updatePackageParticipantsLocked(String packageName) { |
| 318 | if (packageName == null) { |
| 319 | Log.e(TAG, "updatePackageParticipants called with null package name"); |
| 320 | return; |
| 321 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 322 | if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 323 | |
| 324 | // brute force but small code size |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 325 | List<ApplicationInfo> allApps = allAgentApps(); |
| 326 | removePackageParticipantsLockedInner(packageName, allApps); |
| 327 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 330 | // Instantiate the given transport |
| 331 | private IBackupTransport createTransport(int transportID) { |
| 332 | IBackupTransport transport = null; |
| 333 | switch (transportID) { |
| 334 | case BackupManager.TRANSPORT_ADB: |
| 335 | if (DEBUG) Log.v(TAG, "Initializing adb transport"); |
| 336 | transport = new AdbTransport(); |
| 337 | break; |
| 338 | |
| 339 | case BackupManager.TRANSPORT_GOOGLE: |
| 340 | if (DEBUG) Log.v(TAG, "Initializing Google transport"); |
| 341 | //!!! TODO: stand up the google backup transport for real here |
| 342 | transport = new GoogleTransport(); |
| 343 | break; |
| 344 | |
| 345 | default: |
| 346 | Log.e(TAG, "creating unknown transport " + transportID); |
| 347 | } |
| 348 | return transport; |
| 349 | } |
| 350 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 351 | // fire off a backup agent, blocking until it attaches or times out |
| 352 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 353 | IBackupAgent agent = null; |
| 354 | synchronized(mAgentConnectLock) { |
| 355 | mConnecting = true; |
| 356 | mConnectedAgent = null; |
| 357 | try { |
| 358 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| 359 | Log.d(TAG, "awaiting agent for " + app); |
| 360 | |
| 361 | // success; wait for the agent to arrive |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 362 | // only wait 10 seconds for the clear data to happen |
| 363 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 364 | while (mConnecting && mConnectedAgent == null |
| 365 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 366 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 367 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 368 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 369 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 370 | return null; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // if we timed out with no connect, abort and move on |
| 375 | if (mConnecting == true) { |
| 376 | Log.w(TAG, "Timeout waiting for agent " + app); |
| 377 | return null; |
| 378 | } |
| 379 | agent = mConnectedAgent; |
| 380 | } |
| 381 | } catch (RemoteException e) { |
| 382 | // can't happen |
| 383 | } |
| 384 | } |
| 385 | return agent; |
| 386 | } |
| 387 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 388 | // clear an application's data, blocking until the operation completes or times out |
| 389 | void clearApplicationDataSynchronous(String packageName) { |
| 390 | ClearDataObserver observer = new ClearDataObserver(); |
| 391 | |
| 392 | synchronized(mClearDataLock) { |
| 393 | mClearingData = true; |
| 394 | mPackageManager.clearApplicationUserData(packageName, observer); |
| 395 | |
| 396 | // only wait 10 seconds for the clear data to happen |
| 397 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 398 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 399 | try { |
| 400 | mClearDataLock.wait(5000); |
| 401 | } catch (InterruptedException e) { |
| 402 | // won't happen, but still. |
| 403 | mClearingData = false; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| 410 | public void onRemoveCompleted(String packageName, boolean succeeded) |
| 411 | throws android.os.RemoteException { |
| 412 | synchronized(mClearDataLock) { |
| 413 | mClearingData = false; |
| 414 | notifyAll(); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 419 | // ----- Back up a set of applications via a worker thread ----- |
| 420 | |
| 421 | class PerformBackupThread extends Thread { |
| 422 | private static final String TAG = "PerformBackupThread"; |
| 423 | int mTransport; |
| 424 | ArrayList<BackupRequest> mQueue; |
| 425 | |
| 426 | public PerformBackupThread(int transportId, ArrayList<BackupRequest> queue) { |
| 427 | mTransport = transportId; |
| 428 | mQueue = queue; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void run() { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 433 | if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| 434 | |
| 435 | // stand up the current transport |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 436 | IBackupTransport transport = createTransport(mTransport); |
| 437 | if (transport == null) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 441 | // start up the transport |
| 442 | try { |
| 443 | transport.startSession(); |
| 444 | } catch (Exception e) { |
| 445 | Log.e(TAG, "Error session transport"); |
| 446 | e.printStackTrace(); |
| 447 | return; |
| 448 | } |
| 449 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 450 | // The transport is up and running; now run all the backups in our queue |
| 451 | doQueuedBackups(transport); |
| 452 | |
| 453 | // Finally, tear down the transport |
| 454 | try { |
| 455 | transport.endSession(); |
| 456 | } catch (Exception e) { |
| 457 | Log.e(TAG, "Error ending transport"); |
| 458 | e.printStackTrace(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | private void doQueuedBackups(IBackupTransport transport) { |
| 463 | for (BackupRequest request : mQueue) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 464 | Log.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 465 | |
| 466 | IBackupAgent agent = null; |
| 467 | int mode = (request.fullBackup) |
| 468 | ? IApplicationThread.BACKUP_MODE_FULL |
| 469 | : IApplicationThread.BACKUP_MODE_INCREMENTAL; |
| 470 | try { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 471 | agent = bindToAgentSynchronous(request.appInfo, mode); |
| 472 | if (agent != null) { |
| 473 | processOneBackup(request, agent, transport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 474 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 475 | |
| 476 | // unbind even on timeout, just in case |
| 477 | mActivityManager.unbindBackupAgent(request.appInfo); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 478 | } catch (SecurityException ex) { |
| 479 | // Try for the next one. |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 480 | Log.d(TAG, "error in bind/backup", ex); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 481 | } catch (RemoteException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 482 | Log.v(TAG, "bind/backup threw"); |
| 483 | e.printStackTrace(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 484 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 485 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 488 | |
| 489 | void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) { |
| 490 | final String packageName = request.appInfo.packageName; |
| 491 | Log.d(TAG, "processOneBackup doBackup() on " + packageName); |
| 492 | |
| 493 | try { |
| 494 | // Look up the package info & signatures. This is first so that if it |
| 495 | // throws an exception, there's no file setup yet that would need to |
| 496 | // be unraveled. |
| 497 | PackageInfo packInfo = mPackageManager.getPackageInfo(packageName, |
| 498 | PackageManager.GET_SIGNATURES); |
| 499 | |
| 500 | // !!! TODO: get the state file dir from the transport |
| 501 | File savedStateName = new File(mStateDir, packageName); |
| 502 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 503 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 504 | |
| 505 | // In a full backup, we pass a null ParcelFileDescriptor as |
| 506 | // the saved-state "file" |
| 507 | ParcelFileDescriptor savedState = (request.fullBackup) ? null |
| 508 | : ParcelFileDescriptor.open(savedStateName, |
| 509 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 510 | ParcelFileDescriptor.MODE_CREATE); |
| 511 | |
| 512 | backupDataName.delete(); |
| 513 | ParcelFileDescriptor backupData = |
| 514 | ParcelFileDescriptor.open(backupDataName, |
| 515 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 516 | ParcelFileDescriptor.MODE_CREATE); |
| 517 | |
| 518 | newStateName.delete(); |
| 519 | ParcelFileDescriptor newState = |
| 520 | ParcelFileDescriptor.open(newStateName, |
| 521 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 522 | ParcelFileDescriptor.MODE_CREATE); |
| 523 | |
| 524 | // Run the target's backup pass |
| 525 | boolean success = false; |
| 526 | try { |
| 527 | agent.doBackup(savedState, backupData, newState); |
| 528 | success = true; |
| 529 | } finally { |
| 530 | if (savedState != null) { |
| 531 | savedState.close(); |
| 532 | } |
| 533 | backupData.close(); |
| 534 | newState.close(); |
| 535 | } |
| 536 | |
| 537 | // Now propagate the newly-backed-up data to the transport |
| 538 | if (success) { |
| 539 | if (DEBUG) Log.v(TAG, "doBackup() success; calling transport"); |
| 540 | backupData = |
| 541 | ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY); |
| 542 | int error = transport.performBackup(packInfo, backupData); |
| 543 | |
| 544 | // !!! TODO: After successful transport, delete the now-stale data |
| 545 | // and juggle the files so that next time the new state is passed |
| 546 | //backupDataName.delete(); |
| 547 | newStateName.renameTo(savedStateName); |
| 548 | } |
| 549 | } catch (NameNotFoundException e) { |
| 550 | Log.e(TAG, "Package not found on backup: " + packageName); |
| 551 | } catch (FileNotFoundException fnf) { |
| 552 | Log.w(TAG, "File not found on backup: "); |
| 553 | fnf.printStackTrace(); |
| 554 | } catch (RemoteException e) { |
| 555 | Log.d(TAG, "Remote target " + request.appInfo.packageName + " threw during backup:"); |
| 556 | e.printStackTrace(); |
| 557 | } catch (Exception e) { |
| 558 | Log.w(TAG, "Final exception guard in backup: "); |
| 559 | e.printStackTrace(); |
| 560 | } |
| 561 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 564 | |
| 565 | // ----- Restore handling ----- |
| 566 | |
| 567 | // Is the given package restorable on this device? Returns the on-device app's |
| 568 | // ApplicationInfo struct if it is; null if not. |
| 569 | // |
| 570 | // !!! TODO: also consider signatures |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 571 | PackageInfo isRestorable(PackageInfo packageInfo) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 572 | if (packageInfo.packageName != null) { |
| 573 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 574 | PackageInfo app = mPackageManager.getPackageInfo(packageInfo.packageName, |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 575 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 576 | if ((app.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 577 | return app; |
| 578 | } |
| 579 | } catch (Exception e) { |
| 580 | // doesn't exist on this device, or other error -- just ignore it. |
| 581 | } |
| 582 | } |
| 583 | return null; |
| 584 | } |
| 585 | |
| 586 | class PerformRestoreThread extends Thread { |
| 587 | private IBackupTransport mTransport; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 588 | private RestoreSet mImage; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 589 | |
| 590 | PerformRestoreThread(IBackupTransport transport) { |
| 591 | mTransport = transport; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public void run() { |
| 596 | /** |
| 597 | * Restore sequence: |
| 598 | * |
| 599 | * 1. start up the transport session |
| 600 | * 2. get the restore set description for our identity |
| 601 | * 3. for each app in the restore set: |
| 602 | * 3.a. if it's restorable on this device, add it to the restore queue |
| 603 | * 4. for each app in the restore queue: |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 604 | * 4.a. clear the app data |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 605 | * 4.b. get the restore data for the app from the transport |
| 606 | * 4.c. launch the backup agent for the app |
| 607 | * 4.d. agent.doRestore() with the data from the server |
| 608 | * 4.e. unbind the agent [and kill the app?] |
| 609 | * 5. shut down the transport |
| 610 | */ |
| 611 | |
| 612 | int err = -1; |
| 613 | try { |
| 614 | err = mTransport.startSession(); |
| 615 | } catch (Exception e) { |
| 616 | Log.e(TAG, "Error starting transport for restore"); |
| 617 | e.printStackTrace(); |
| 618 | } |
| 619 | |
| 620 | if (err == 0) { |
| 621 | // build the set of apps to restore |
| 622 | try { |
| 623 | RestoreSet[] images = mTransport.getAvailableRestoreSets(); |
| 624 | if (images.length > 0) { |
| 625 | // !!! for now we always take the first set |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 626 | mImage = images[0]; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 627 | |
| 628 | // build the set of apps we will attempt to restore |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 629 | PackageInfo[] packages = mTransport.getAppSet(mImage.token); |
| 630 | HashSet<PackageInfo> appsToRestore = new HashSet<PackageInfo>(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 631 | for (PackageInfo pkg: packages) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 632 | // get the real PackageManager idea of the package |
| 633 | PackageInfo app = isRestorable(pkg); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 634 | if (app != null) { |
| 635 | appsToRestore.add(app); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // now run the restore queue |
| 640 | doQueuedRestores(appsToRestore); |
| 641 | } |
| 642 | } catch (RemoteException e) { |
| 643 | // can't happen; transports run locally |
| 644 | } |
| 645 | |
| 646 | // done; shut down the transport |
| 647 | try { |
| 648 | mTransport.endSession(); |
| 649 | } catch (Exception e) { |
| 650 | Log.e(TAG, "Error ending transport for restore"); |
| 651 | e.printStackTrace(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // even if the initial session startup failed, report that we're done here |
| 656 | } |
| 657 | |
| 658 | // restore each app in the queue |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 659 | void doQueuedRestores(HashSet<PackageInfo> appsToRestore) { |
| 660 | for (PackageInfo app : appsToRestore) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 661 | Log.d(TAG, "starting agent for restore of " + app); |
| 662 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 663 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 664 | // Remove the app's data first |
| 665 | clearApplicationDataSynchronous(app.packageName); |
| 666 | |
| 667 | // Now perform the restore into the clean app |
| 668 | IBackupAgent agent = bindToAgentSynchronous(app.applicationInfo, |
| 669 | IApplicationThread.BACKUP_MODE_RESTORE); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 670 | if (agent != null) { |
| 671 | processOneRestore(app, agent); |
| 672 | } |
| 673 | |
| 674 | // unbind even on timeout, just in case |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 675 | mActivityManager.unbindBackupAgent(app.applicationInfo); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 676 | } catch (SecurityException ex) { |
| 677 | // Try for the next one. |
| 678 | Log.d(TAG, "error in bind", ex); |
| 679 | } catch (RemoteException e) { |
| 680 | // can't happen |
| 681 | } |
| 682 | |
| 683 | } |
| 684 | } |
| 685 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 686 | // Do the guts of a restore of one application, derived from the 'mImage' |
| 687 | // restore set via the 'mTransport' transport. |
| 688 | void processOneRestore(PackageInfo app, IBackupAgent agent) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 689 | // !!! TODO: actually run the restore through mTransport |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 690 | final String packageName = app.packageName; |
| 691 | |
| 692 | // !!! TODO: get the dirs from the transport |
| 693 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
| 694 | backupDataName.delete(); |
| 695 | try { |
| 696 | ParcelFileDescriptor backupData = |
| 697 | ParcelFileDescriptor.open(backupDataName, |
| 698 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 699 | ParcelFileDescriptor.MODE_CREATE); |
| 700 | |
| 701 | // Run the transport's restore pass |
| 702 | // Run the target's backup pass |
| 703 | int err = -1; |
| 704 | try { |
| 705 | err = mTransport.getRestoreData(mImage.token, app, backupData); |
| 706 | } catch (RemoteException e) { |
| 707 | // can't happen |
| 708 | } finally { |
| 709 | backupData.close(); |
| 710 | } |
| 711 | |
| 712 | // Okay, we have the data. Now have the agent do the restore. |
| 713 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 714 | ParcelFileDescriptor newState = |
| 715 | ParcelFileDescriptor.open(newStateName, |
| 716 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 717 | ParcelFileDescriptor.MODE_CREATE); |
| 718 | |
| 719 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 720 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 721 | |
| 722 | boolean success = false; |
| 723 | try { |
| 724 | agent.doRestore(backupData, newState); |
| 725 | success = true; |
| 726 | } catch (Exception e) { |
| 727 | Log.e(TAG, "Restore failed for " + packageName); |
| 728 | e.printStackTrace(); |
| 729 | } finally { |
| 730 | newState.close(); |
| 731 | backupData.close(); |
| 732 | } |
| 733 | |
| 734 | // if everything went okay, remember the recorded state now |
| 735 | if (success) { |
| 736 | File savedStateName = new File(mStateDir, packageName); |
| 737 | newStateName.renameTo(savedStateName); |
| 738 | } |
| 739 | } catch (FileNotFoundException fnfe) { |
| 740 | Log.v(TAG, "Couldn't open file for restore: " + fnfe); |
| 741 | } catch (IOException ioe) { |
| 742 | Log.e(TAG, "Unable to process restore file: " + ioe); |
| 743 | } catch (Exception e) { |
| 744 | Log.e(TAG, "Final exception guard in restore:"); |
| 745 | e.printStackTrace(); |
| 746 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 751 | // ----- IBackupManager binder interface ----- |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 752 | |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 753 | public void dataChanged(String packageName) throws RemoteException { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 754 | // Record that we need a backup pass for the caller. Since multiple callers |
| 755 | // may share a uid, we need to note all candidates within that uid and schedule |
| 756 | // a backup pass for each of them. |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 757 | |
| 758 | Log.d(TAG, "dataChanged packageName=" + packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 759 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 760 | HashSet<ApplicationInfo> targets = mBackupParticipants.get(Binder.getCallingUid()); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 761 | if (targets != null) { |
| 762 | synchronized (mQueueLock) { |
| 763 | // 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] | 764 | for (ApplicationInfo app : targets) { |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 765 | // validate the caller-supplied package name against the known set of |
| 766 | // packages associated with this uid |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 767 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 768 | // Add the caller to the set of pending backups. If there is |
| 769 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 770 | BackupRequest req = new BackupRequest(app, false); |
| 771 | mPendingBackups.put(app, req); |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 772 | // !!! TODO: write to the pending-backup journal file in case of crash |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 776 | if (DEBUG) { |
| 777 | int numKeys = mPendingBackups.size(); |
| 778 | Log.d(TAG, "Scheduling backup for " + numKeys + " participants:"); |
| 779 | for (BackupRequest b : mPendingBackups.values()) { |
| 780 | Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName); |
| 781 | } |
| 782 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 783 | // Schedule a backup pass in a few minutes. As backup-eligible data |
| 784 | // keeps changing, continue to defer the backup pass until things |
| 785 | // settle down, to avoid extra overhead. |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 786 | mBackupHandler.removeMessages(MSG_RUN_BACKUP); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 787 | mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL); |
| 788 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 789 | } else { |
| 790 | Log.w(TAG, "dataChanged but no participant pkg " + packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 793 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 794 | // Schedule a backup pass for a given package. This method will schedule a |
| 795 | // full backup even for apps that do not declare an android:backupAgent, so |
| 796 | // use with care. |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 797 | public void scheduleFullBackup(String packageName) throws RemoteException { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 798 | mContext.enforceCallingPermission("android.permission.BACKUP", "scheduleFullBackup"); |
| 799 | |
| 800 | if (DEBUG) Log.v(TAG, "Scheduling immediate full backup for " + packageName); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 801 | synchronized (mQueueLock) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 802 | try { |
| 803 | ApplicationInfo app = mPackageManager.getApplicationInfo(packageName, 0); |
| 804 | mPendingBackups.put(app, new BackupRequest(app, true)); |
| 805 | mBackupHandler.sendEmptyMessage(MSG_RUN_FULL_BACKUP); |
| 806 | } catch (NameNotFoundException e) { |
| 807 | Log.w(TAG, "Could not find app for " + packageName + " to schedule full backup"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 811 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 812 | // Select which transport to use for the next backup operation |
| 813 | public int selectBackupTransport(int transportId) { |
| 814 | mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport"); |
| 815 | |
| 816 | int prevTransport = mTransportId; |
| 817 | mTransportId = transportId; |
| 818 | return prevTransport; |
| 819 | } |
| 820 | |
| 821 | // Callback: a requested backup agent has been instantiated. This should only |
| 822 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 823 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 824 | synchronized(mAgentConnectLock) { |
| 825 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 826 | Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| 827 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 828 | mConnectedAgent = agent; |
| 829 | mConnecting = false; |
| 830 | } else { |
| 831 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 832 | + " claiming agent connected"); |
| 833 | } |
| 834 | mAgentConnectLock.notifyAll(); |
| 835 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 839 | // 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] | 840 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 841 | public void agentDisconnected(String packageName) { |
| 842 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 843 | synchronized(mAgentConnectLock) { |
| 844 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 845 | mConnectedAgent = null; |
| 846 | mConnecting = false; |
| 847 | } else { |
| 848 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 849 | + " claiming agent disconnected"); |
| 850 | } |
| 851 | mAgentConnectLock.notifyAll(); |
| 852 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 853 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 854 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 855 | // Hand off a restore session |
| 856 | public IRestoreSession beginRestoreSession(int transportID) { |
| 857 | mContext.enforceCallingPermission("android.permission.BACKUP", "beginRestoreSession"); |
| 858 | return null; |
| 859 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 860 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 861 | // ----- Restore session ----- |
| 862 | |
| 863 | class RestoreSession extends IRestoreSession.Stub { |
| 864 | private IBackupTransport mRestoreTransport = null; |
| 865 | RestoreSet[] mRestoreSets = null; |
| 866 | |
| 867 | RestoreSession(int transportID) { |
| 868 | mRestoreTransport = createTransport(transportID); |
| 869 | } |
| 870 | |
| 871 | // --- Binder interface --- |
| 872 | public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException { |
| 873 | synchronized(this) { |
| 874 | if (mRestoreSets == null) { |
| 875 | mRestoreSets = mRestoreTransport.getAvailableRestoreSets(); |
| 876 | } |
| 877 | return mRestoreSets; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | public int performRestore(int token) throws android.os.RemoteException { |
| 882 | return -1; |
| 883 | } |
| 884 | |
| 885 | public void endRestoreSession() throws android.os.RemoteException { |
| 886 | mRestoreTransport.endSession(); |
| 887 | mRestoreTransport = null; |
| 888 | } |
| 889 | } |
| 890 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 891 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 892 | @Override |
| 893 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 894 | synchronized (mQueueLock) { |
| 895 | int N = mBackupParticipants.size(); |
| 896 | pw.println("Participants:"); |
| 897 | for (int i=0; i<N; i++) { |
| 898 | int uid = mBackupParticipants.keyAt(i); |
| 899 | pw.print(" uid: "); |
| 900 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 901 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 902 | for (ApplicationInfo app: participants) { |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 903 | pw.print(" "); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 904 | pw.println(app.toString()); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 907 | pw.println("Pending:"); |
| 908 | Iterator<BackupRequest> br = mPendingBackups.values().iterator(); |
| 909 | while (br.hasNext()) { |
| 910 | pw.print(" "); |
| 911 | pw.println(br); |
| 912 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 915 | } |