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