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