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