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; |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 21 | import android.app.IActivityManager; |
| 22 | import android.app.IApplicationThread; |
| 23 | import android.app.IBackupAgent; |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 24 | import android.app.PendingIntent; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 25 | import android.backup.IBackupManager; |
| 26 | import android.backup.IRestoreObserver; |
| 27 | import android.backup.IRestoreSession; |
| 28 | import android.backup.RestoreSet; |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 30 | import android.content.ComponentName; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 33 | import android.content.IntentFilter; |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 34 | import android.content.ServiceConnection; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 36 | import android.content.pm.IPackageDataObserver; |
Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageInfo; |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 39 | import android.content.pm.Signature; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager.NameNotFoundException; |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 41 | import android.net.Uri; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 42 | import android.os.Binder; |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 43 | import android.os.Bundle; |
Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 44 | import android.os.Environment; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 45 | import android.os.Handler; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 46 | import android.os.HandlerThread; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 47 | import android.os.IBinder; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 48 | import android.os.Looper; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 49 | import android.os.Message; |
Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 50 | import android.os.ParcelFileDescriptor; |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 51 | import android.os.PowerManager; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 52 | import android.os.Process; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 53 | import android.os.RemoteException; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 54 | import android.os.SystemClock; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 55 | import android.provider.Settings; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 56 | import android.util.EventLog; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 57 | import android.util.Log; |
| 58 | import android.util.SparseArray; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 59 | import android.util.SparseIntArray; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 60 | |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 61 | import com.android.internal.backup.BackupConstants; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 62 | import com.android.internal.backup.IBackupTransport; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 63 | import com.android.internal.backup.LocalTransport; |
Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 64 | import com.android.server.PackageManagerBackupAgent.Metadata; |
Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 65 | |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 66 | import java.io.EOFException; |
Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 67 | import java.io.File; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 69 | import java.io.FileOutputStream; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 70 | import java.io.IOException; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 71 | import java.io.PrintWriter; |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 72 | import java.io.RandomAccessFile; |
Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 73 | import java.util.ArrayList; |
| 74 | import java.util.HashMap; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 75 | import java.util.HashSet; |
| 76 | import java.util.List; |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 77 | import java.util.Map; |
Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 78 | import java.util.Random; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 79 | |
| 80 | class BackupManagerService extends IBackupManager.Stub { |
| 81 | private static final String TAG = "BackupManagerService"; |
Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 82 | private static final boolean DEBUG = false; |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 83 | |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 84 | // How often we perform a backup pass. Privileged external callers can |
| 85 | // trigger an immediate pass. |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 86 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 87 | |
Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 88 | // Random variation in backup scheduling time to avoid server load spikes |
| 89 | private static final int FUZZ_MILLIS = 5 * 60 * 1000; |
| 90 | |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 91 | // The amount of time between the initial provisioning of the device and |
| 92 | // the first backup pass. |
| 93 | private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR; |
| 94 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 95 | private static final String RUN_BACKUP_ACTION = "android.backup.intent.RUN"; |
| 96 | private static final String RUN_INITIALIZE_ACTION = "android.backup.intent.INIT"; |
| 97 | private static final String RUN_CLEAR_ACTION = "android.backup.intent.CLEAR"; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 98 | private static final int MSG_RUN_BACKUP = 1; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 99 | private static final int MSG_RUN_FULL_BACKUP = 2; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 100 | private static final int MSG_RUN_RESTORE = 3; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 101 | private static final int MSG_RUN_CLEAR = 4; |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 102 | private static final int MSG_RUN_INITIALIZE = 5; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 103 | private static final int MSG_TIMEOUT = 6; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 104 | |
| 105 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 106 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 107 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 108 | // Timeout intervals for agent backup & restore operations |
| 109 | static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000; |
| 110 | static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000; |
| 111 | |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 112 | private Context mContext; |
| 113 | private PackageManager mPackageManager; |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 114 | private IActivityManager mActivityManager; |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 115 | private PowerManager mPowerManager; |
| 116 | private AlarmManager mAlarmManager; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 117 | IBackupManager mBackupManagerBinder; |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 118 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 119 | boolean mEnabled; // access to this is synchronized on 'this' |
| 120 | boolean mProvisioned; |
Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 121 | boolean mAutoRestore; |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 122 | PowerManager.WakeLock mWakelock; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 123 | HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 124 | BackupHandler mBackupHandler; |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 125 | PendingIntent mRunBackupIntent, mRunInitIntent; |
| 126 | BroadcastReceiver mRunBackupReceiver, mRunInitReceiver; |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 127 | // map UIDs to the set of backup client services within that UID's app set |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 128 | final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 129 | = new SparseArray<HashSet<ApplicationInfo>>(); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 130 | // set of backup services that have pending changes |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 131 | class BackupRequest { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 132 | public ApplicationInfo appInfo; |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 133 | public boolean fullBackup; |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 134 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 135 | BackupRequest(ApplicationInfo app, boolean isFull) { |
| 136 | appInfo = app; |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 137 | fullBackup = isFull; |
| 138 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 139 | |
| 140 | public String toString() { |
| 141 | return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}"; |
| 142 | } |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 143 | } |
Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 144 | // Backups that we haven't started yet. |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 145 | HashMap<ApplicationInfo,BackupRequest> mPendingBackups |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 146 | = new HashMap<ApplicationInfo,BackupRequest>(); |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 147 | |
| 148 | // Pseudoname that we use for the Package Manager metadata "package" |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 149 | static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 150 | |
| 151 | // locking around the pending-backup management |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 152 | final Object mQueueLock = new Object(); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 153 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 154 | // The thread performing the sequence of queued backups binds to each app's agent |
| 155 | // in succession. Bind notifications are asynchronously delivered through the |
| 156 | // Activity Manager; use this lock object to signal when a requested binding has |
| 157 | // completed. |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 158 | final Object mAgentConnectLock = new Object(); |
| 159 | IBackupAgent mConnectedAgent; |
| 160 | volatile boolean mConnecting; |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 161 | volatile long mLastBackupPass; |
| 162 | volatile long mNextBackupPass; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 163 | |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 164 | // A similar synchronization mechanism around clearing apps' data for restore |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 165 | final Object mClearDataLock = new Object(); |
| 166 | volatile boolean mClearingData; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 167 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 168 | // Transport bookkeeping |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 169 | final HashMap<String,IBackupTransport> mTransports |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 170 | = new HashMap<String,IBackupTransport>(); |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 171 | String mCurrentTransport; |
| 172 | IBackupTransport mLocalTransport, mGoogleTransport; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 173 | ActiveRestoreSession mActiveRestoreSession; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 174 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 175 | class RestoreParams { |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 176 | public IBackupTransport transport; |
| 177 | public IRestoreObserver observer; |
Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 178 | public long token; |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 179 | public PackageInfo pkgInfo; |
| 180 | |
| 181 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, |
| 182 | long _token, PackageInfo _pkg) { |
| 183 | transport = _transport; |
| 184 | observer = _obs; |
| 185 | token = _token; |
| 186 | pkgInfo = _pkg; |
| 187 | } |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 188 | |
Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 189 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) { |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 190 | transport = _transport; |
| 191 | observer = _obs; |
Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 192 | token = _token; |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 193 | pkgInfo = null; |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 197 | class ClearParams { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 198 | public IBackupTransport transport; |
| 199 | public PackageInfo packageInfo; |
| 200 | |
| 201 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 202 | transport = _transport; |
| 203 | packageInfo = _info; |
| 204 | } |
| 205 | } |
| 206 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 207 | // Bookkeeping of in-flight operations for timeout etc. purposes. The operation |
| 208 | // token is the index of the entry in the pending-operations list. |
| 209 | static final int OP_PENDING = 0; |
| 210 | static final int OP_ACKNOWLEDGED = 1; |
| 211 | static final int OP_TIMEOUT = -1; |
| 212 | |
| 213 | final SparseIntArray mCurrentOperations = new SparseIntArray(); |
| 214 | final Object mCurrentOpLock = new Object(); |
| 215 | final Random mTokenGenerator = new Random(); |
| 216 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 217 | // Where we keep our journal files and other bookkeeping |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 218 | File mBaseStateDir; |
| 219 | File mDataDir; |
| 220 | File mJournalDir; |
| 221 | File mJournal; |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 222 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 223 | // Keep a log of all the apps we've ever backed up, and what the |
| 224 | // dataset tokens are for both the current backup dataset and |
| 225 | // the ancestral dataset. |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 226 | private File mEverStored; |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 227 | HashSet<String> mEverStoredApps = new HashSet<String>(); |
| 228 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 229 | File mTokenFile; |
| 230 | long mAncestralToken = 0; |
| 231 | long mCurrentToken = 0; |
| 232 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 233 | // Persistently track the need to do a full init |
| 234 | static final String INIT_SENTINEL_FILE_NAME = "_need_init_"; |
| 235 | HashSet<String> mPendingInits = new HashSet<String>(); // transport names |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 236 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 237 | // ----- Asynchronous backup/restore handler thread ----- |
| 238 | |
| 239 | private class BackupHandler extends Handler { |
| 240 | public BackupHandler(Looper looper) { |
| 241 | super(looper); |
| 242 | } |
| 243 | |
| 244 | public void handleMessage(Message msg) { |
| 245 | |
| 246 | switch (msg.what) { |
| 247 | case MSG_RUN_BACKUP: |
| 248 | { |
| 249 | mLastBackupPass = System.currentTimeMillis(); |
| 250 | mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL; |
| 251 | |
| 252 | IBackupTransport transport = getTransport(mCurrentTransport); |
| 253 | if (transport == null) { |
| 254 | Log.v(TAG, "Backup requested but no transport available"); |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 255 | mWakelock.release(); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // snapshot the pending-backup set and work on that |
| 260 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 261 | File oldJournal = mJournal; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 262 | synchronized (mQueueLock) { |
Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 263 | // Do we have any work to do? Construct the work queue |
| 264 | // then release the synchronization lock to actually run |
| 265 | // the backup. |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 266 | if (mPendingBackups.size() > 0) { |
| 267 | for (BackupRequest b: mPendingBackups.values()) { |
| 268 | queue.add(b); |
| 269 | } |
| 270 | if (DEBUG) Log.v(TAG, "clearing pending backups"); |
| 271 | mPendingBackups.clear(); |
| 272 | |
| 273 | // Start a new backup-queue journal file too |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 274 | mJournal = null; |
| 275 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 278 | |
| 279 | if (queue.size() > 0) { |
| 280 | // At this point, we have started a new journal file, and the old |
| 281 | // file identity is being passed to the backup processing thread. |
| 282 | // When it completes successfully, that old journal file will be |
| 283 | // deleted. If we crash prior to that, the old journal is parsed |
| 284 | // at next boot and the journaled requests fulfilled. |
| 285 | (new PerformBackupTask(transport, queue, oldJournal)).run(); |
| 286 | } else { |
| 287 | Log.v(TAG, "Backup requested but nothing pending"); |
| 288 | mWakelock.release(); |
| 289 | } |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | |
| 293 | case MSG_RUN_FULL_BACKUP: |
| 294 | break; |
| 295 | |
| 296 | case MSG_RUN_RESTORE: |
| 297 | { |
| 298 | RestoreParams params = (RestoreParams)msg.obj; |
| 299 | Log.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| 300 | (new PerformRestoreTask(params.transport, params.observer, |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 301 | params.token, params.pkgInfo)).run(); |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | |
| 305 | case MSG_RUN_CLEAR: |
| 306 | { |
| 307 | ClearParams params = (ClearParams)msg.obj; |
| 308 | (new PerformClearTask(params.transport, params.packageInfo)).run(); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | case MSG_RUN_INITIALIZE: |
| 313 | { |
| 314 | HashSet<String> queue; |
| 315 | |
| 316 | // Snapshot the pending-init queue and work on that |
| 317 | synchronized (mQueueLock) { |
| 318 | queue = new HashSet<String>(mPendingInits); |
| 319 | mPendingInits.clear(); |
| 320 | } |
| 321 | |
| 322 | (new PerformInitializeTask(queue)).run(); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | case MSG_TIMEOUT: |
| 327 | { |
| 328 | synchronized (mCurrentOpLock) { |
| 329 | final int token = msg.arg1; |
| 330 | int state = mCurrentOperations.get(token, OP_TIMEOUT); |
| 331 | if (state == OP_PENDING) { |
| 332 | if (DEBUG) Log.v(TAG, "TIMEOUT: token=" + token); |
| 333 | mCurrentOperations.put(token, OP_TIMEOUT); |
| 334 | } |
| 335 | mCurrentOpLock.notifyAll(); |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // ----- Main service implementation ----- |
| 344 | |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 345 | public BackupManagerService(Context context) { |
| 346 | mContext = context; |
| 347 | mPackageManager = context.getPackageManager(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 348 | mActivityManager = ActivityManagerNative.getDefault(); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 349 | |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 350 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 351 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 352 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 353 | mBackupManagerBinder = asInterface(asBinder()); |
| 354 | |
| 355 | // spin up the backup/restore handler thread |
| 356 | mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 357 | mHandlerThread.start(); |
| 358 | mBackupHandler = new BackupHandler(mHandlerThread.getLooper()); |
| 359 | |
Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 360 | // Set up our bookkeeping |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 361 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 362 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 363 | mProvisioned = Settings.Secure.getInt(context.getContentResolver(), |
Joe Onorato | ab9a2a5 | 2009-07-27 08:56:39 -0700 | [diff] [blame] | 364 | Settings.Secure.BACKUP_PROVISIONED, 0) != 0; |
Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 365 | mAutoRestore = Settings.Secure.getInt(context.getContentResolver(), |
| 366 | Settings.Secure.BACKUP_AUTO_RESTORE, 0) != 0; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 367 | // If Encrypted file systems is enabled or disabled, this call will return the |
| 368 | // correct directory. |
| 369 | mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup"); |
| 370 | mBaseStateDir.mkdirs(); |
Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 371 | mDataDir = Environment.getDownloadCacheDirectory(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 372 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 373 | // Alarm receivers for scheduled backups & initialization operations |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 374 | mRunBackupReceiver = new RunBackupReceiver(); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 375 | IntentFilter filter = new IntentFilter(); |
| 376 | filter.addAction(RUN_BACKUP_ACTION); |
| 377 | context.registerReceiver(mRunBackupReceiver, filter, |
| 378 | android.Manifest.permission.BACKUP, null); |
| 379 | |
| 380 | mRunInitReceiver = new RunInitializeReceiver(); |
| 381 | filter = new IntentFilter(); |
| 382 | filter.addAction(RUN_INITIALIZE_ACTION); |
| 383 | context.registerReceiver(mRunInitReceiver, filter, |
| 384 | android.Manifest.permission.BACKUP, null); |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 385 | |
| 386 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 387 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 388 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 389 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 390 | Intent initIntent = new Intent(RUN_INITIALIZE_ACTION); |
| 391 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 392 | mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0); |
| 393 | |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 394 | // Set up the backup-request journaling |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 395 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 396 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 397 | mJournal = null; // will be created on first use |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 398 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 399 | // Set up the various sorts of package tracking we do |
| 400 | initPackageTracking(); |
| 401 | |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 402 | // Build our mapping of uid to backup client services. This implicitly |
| 403 | // schedules a backup pass on the Package Manager metadata the first |
| 404 | // time anything needs to be backed up. |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 405 | synchronized (mBackupParticipants) { |
| 406 | addPackageParticipantsLocked(null); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 409 | // Set up our transport options and initialize the default transport |
| 410 | // TODO: Have transports register themselves somehow? |
| 411 | // TODO: Don't create transports that we don't need to? |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 412 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 413 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 414 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 415 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 416 | mGoogleTransport = null; |
Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 417 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 418 | Settings.Secure.BACKUP_TRANSPORT); |
| 419 | if ("".equals(mCurrentTransport)) { |
| 420 | mCurrentTransport = null; |
Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 421 | } |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 422 | if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport); |
| 423 | |
| 424 | // Attach to the Google backup transport. When this comes up, it will set |
| 425 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 426 | // to null. |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 427 | Intent intent = new Intent().setComponent(new ComponentName( |
| 428 | "com.google.android.backup", |
| 429 | "com.google.android.backup.BackupTransportService")); |
| 430 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 431 | |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 432 | // Now that we know about valid backup participants, parse any |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 433 | // leftover journal files into the pending backup set |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 434 | parseLeftoverJournals(); |
| 435 | |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 436 | // Power management |
| 437 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backup"); |
| 438 | |
| 439 | // Start the backup passes going |
| 440 | setBackupEnabled(areEnabled); |
| 441 | } |
| 442 | |
| 443 | private class RunBackupReceiver extends BroadcastReceiver { |
| 444 | public void onReceive(Context context, Intent intent) { |
| 445 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 446 | synchronized (mQueueLock) { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 447 | if (mPendingInits.size() > 0) { |
| 448 | // If there are pending init operations, we process those |
| 449 | // and then settle into the usual periodic backup schedule. |
| 450 | if (DEBUG) Log.v(TAG, "Init pending at scheduled backup"); |
| 451 | try { |
| 452 | mAlarmManager.cancel(mRunInitIntent); |
| 453 | mRunInitIntent.send(); |
| 454 | } catch (PendingIntent.CanceledException ce) { |
| 455 | Log.e(TAG, "Run init intent cancelled"); |
| 456 | // can't really do more than bail here |
| 457 | } |
| 458 | } else { |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 459 | // Don't run backups now if we're disabled or not yet |
| 460 | // fully set up. |
| 461 | if (mEnabled && mProvisioned) { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 462 | if (DEBUG) Log.v(TAG, "Running a backup pass"); |
| 463 | |
| 464 | // Acquire the wakelock and pass it to the backup thread. it will |
| 465 | // be released once backup concludes. |
| 466 | mWakelock.acquire(); |
| 467 | |
| 468 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 469 | mBackupHandler.sendMessage(msg); |
| 470 | } else { |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 471 | Log.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | private class RunInitializeReceiver extends BroadcastReceiver { |
| 480 | public void onReceive(Context context, Intent intent) { |
| 481 | if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) { |
| 482 | synchronized (mQueueLock) { |
| 483 | if (DEBUG) Log.v(TAG, "Running a device init"); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 484 | |
| 485 | // Acquire the wakelock and pass it to the init thread. it will |
| 486 | // be released once init concludes. |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 487 | mWakelock.acquire(); |
| 488 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 489 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE); |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 490 | mBackupHandler.sendMessage(msg); |
| 491 | } |
| 492 | } |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 493 | } |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 494 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 495 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 496 | private void initPackageTracking() { |
| 497 | if (DEBUG) Log.v(TAG, "Initializing package tracking"); |
| 498 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 499 | // Remember our ancestral dataset |
| 500 | mTokenFile = new File(mBaseStateDir, "ancestral"); |
| 501 | try { |
| 502 | RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r"); |
| 503 | mAncestralToken = tf.readLong(); |
| 504 | mCurrentToken = tf.readLong(); |
| 505 | } catch (IOException e) { |
| 506 | Log.w(TAG, "Unable to read token file", e); |
| 507 | } |
| 508 | |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 509 | // Keep a log of what apps we've ever backed up. Because we might have |
| 510 | // rebooted in the middle of an operation that was removing something from |
| 511 | // this log, we sanity-check its contents here and reconstruct it. |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 512 | mEverStored = new File(mBaseStateDir, "processed"); |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 513 | File tempProcessedFile = new File(mBaseStateDir, "processed.new"); |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 514 | |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 515 | // If we were in the middle of removing something from the ever-backed-up |
| 516 | // file, there might be a transient "processed.new" file still present. |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 517 | // Ignore it -- we'll validate "processed" against the current package set. |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 518 | if (tempProcessedFile.exists()) { |
| 519 | tempProcessedFile.delete(); |
| 520 | } |
| 521 | |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 522 | // If there are previous contents, parse them out then start a new |
| 523 | // file to continue the recordkeeping. |
| 524 | if (mEverStored.exists()) { |
| 525 | RandomAccessFile temp = null; |
| 526 | RandomAccessFile in = null; |
| 527 | |
| 528 | try { |
| 529 | temp = new RandomAccessFile(tempProcessedFile, "rws"); |
| 530 | in = new RandomAccessFile(mEverStored, "r"); |
| 531 | |
| 532 | while (true) { |
| 533 | PackageInfo info; |
| 534 | String pkg = in.readUTF(); |
| 535 | try { |
| 536 | info = mPackageManager.getPackageInfo(pkg, 0); |
| 537 | mEverStoredApps.add(pkg); |
| 538 | temp.writeUTF(pkg); |
| 539 | if (DEBUG) Log.v(TAG, " + " + pkg); |
| 540 | } catch (NameNotFoundException e) { |
| 541 | // nope, this package was uninstalled; don't include it |
| 542 | if (DEBUG) Log.v(TAG, " - " + pkg); |
| 543 | } |
| 544 | } |
| 545 | } catch (EOFException e) { |
| 546 | // Once we've rewritten the backup history log, atomically replace the |
| 547 | // old one with the new one then reopen the file for continuing use. |
| 548 | if (!tempProcessedFile.renameTo(mEverStored)) { |
| 549 | Log.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored); |
| 550 | } |
| 551 | } catch (IOException e) { |
| 552 | Log.e(TAG, "Error in processed file", e); |
| 553 | } finally { |
| 554 | try { if (temp != null) temp.close(); } catch (IOException e) {} |
| 555 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 556 | } |
| 557 | } |
| 558 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 559 | // Register for broadcasts about package install, etc., so we can |
| 560 | // update the provider list. |
| 561 | IntentFilter filter = new IntentFilter(); |
| 562 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 563 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 564 | filter.addDataScheme("package"); |
| 565 | mContext.registerReceiver(mBroadcastReceiver, filter); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 566 | // Register for events related to sdcard installation. |
| 567 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame^] | 568 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 569 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 570 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 573 | private void parseLeftoverJournals() { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 574 | for (File f : mJournalDir.listFiles()) { |
| 575 | if (mJournal == null || f.compareTo(mJournal) != 0) { |
| 576 | // This isn't the current journal, so it must be a leftover. Read |
| 577 | // out the package names mentioned there and schedule them for |
| 578 | // backup. |
| 579 | RandomAccessFile in = null; |
| 580 | try { |
| 581 | Log.i(TAG, "Found stale backup journal, scheduling:"); |
| 582 | in = new RandomAccessFile(f, "r"); |
| 583 | while (true) { |
| 584 | String packageName = in.readUTF(); |
| 585 | Log.i(TAG, " + " + packageName); |
| 586 | dataChanged(packageName); |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 587 | } |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 588 | } catch (EOFException e) { |
| 589 | // no more data; we're done |
| 590 | } catch (Exception e) { |
| 591 | Log.e(TAG, "Can't read " + f, e); |
| 592 | } finally { |
| 593 | // close/delete the file |
| 594 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 595 | f.delete(); |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 601 | // Maintain persistent state around whether need to do an initialize operation. |
| 602 | // Must be called with the queue lock held. |
| 603 | void recordInitPendingLocked(boolean isPending, String transportName) { |
| 604 | if (DEBUG) Log.i(TAG, "recordInitPendingLocked: " + isPending |
| 605 | + " on transport " + transportName); |
| 606 | try { |
| 607 | IBackupTransport transport = getTransport(transportName); |
| 608 | String transportDirName = transport.transportDirName(); |
| 609 | File stateDir = new File(mBaseStateDir, transportDirName); |
| 610 | File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 611 | |
| 612 | if (isPending) { |
| 613 | // We need an init before we can proceed with sending backup data. |
| 614 | // Record that with an entry in our set of pending inits, as well as |
| 615 | // journaling it via creation of a sentinel file. |
| 616 | mPendingInits.add(transportName); |
| 617 | try { |
| 618 | (new FileOutputStream(initPendingFile)).close(); |
| 619 | } catch (IOException ioe) { |
| 620 | // Something is badly wrong with our permissions; just try to move on |
| 621 | } |
| 622 | } else { |
| 623 | // No more initialization needed; wipe the journal and reset our state. |
| 624 | initPendingFile.delete(); |
| 625 | mPendingInits.remove(transportName); |
| 626 | } |
| 627 | } catch (RemoteException e) { |
| 628 | // can't happen; the transport is local |
| 629 | } |
| 630 | } |
| 631 | |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 632 | // Reset all of our bookkeeping, in response to having been told that |
| 633 | // the backend data has been wiped [due to idle expiry, for example], |
| 634 | // so we must re-upload all saved settings. |
| 635 | void resetBackupState(File stateFileDir) { |
| 636 | synchronized (mQueueLock) { |
| 637 | // Wipe the "what we've ever backed up" tracking |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 638 | mEverStoredApps.clear(); |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 639 | mEverStored.delete(); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 640 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 641 | mCurrentToken = 0; |
| 642 | writeRestoreTokens(); |
| 643 | |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 644 | // Remove all the state files |
| 645 | for (File sf : stateFileDir.listFiles()) { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 646 | // ... but don't touch the needs-init sentinel |
| 647 | if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) { |
| 648 | sf.delete(); |
| 649 | } |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // Enqueue a new backup of every participant |
| 653 | int N = mBackupParticipants.size(); |
| 654 | for (int i=0; i<N; i++) { |
| 655 | int uid = mBackupParticipants.keyAt(i); |
| 656 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 657 | for (ApplicationInfo app: participants) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 658 | dataChanged(app.packageName); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Christopher Tate | dfa47b5 | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 664 | // Add a transport to our set of available backends. If 'transport' is null, this |
| 665 | // is an unregistration, and the transport's entry is removed from our bookkeeping. |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 666 | private void registerTransport(String name, IBackupTransport transport) { |
| 667 | synchronized (mTransports) { |
Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 668 | if (DEBUG) Log.v(TAG, "Registering transport " + name + " = " + transport); |
Christopher Tate | dfa47b5 | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 669 | if (transport != null) { |
| 670 | mTransports.put(name, transport); |
| 671 | } else { |
| 672 | mTransports.remove(name); |
Christopher Tate | b0dcaaf | 2010-01-29 16:27:04 -0800 | [diff] [blame] | 673 | if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) { |
Christopher Tate | dfa47b5 | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 674 | mCurrentTransport = null; |
| 675 | } |
| 676 | // Nothing further to do in the unregistration case |
| 677 | return; |
| 678 | } |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 679 | } |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 680 | |
| 681 | // If the init sentinel file exists, we need to be sure to perform the init |
| 682 | // as soon as practical. We also create the state directory at registration |
| 683 | // time to ensure it's present from the outset. |
| 684 | try { |
| 685 | String transportName = transport.transportDirName(); |
| 686 | File stateDir = new File(mBaseStateDir, transportName); |
| 687 | stateDir.mkdirs(); |
| 688 | |
| 689 | File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 690 | if (initSentinel.exists()) { |
| 691 | synchronized (mQueueLock) { |
| 692 | mPendingInits.add(transportName); |
| 693 | |
| 694 | // TODO: pick a better starting time than now + 1 minute |
| 695 | long delay = 1000 * 60; // one minute, in milliseconds |
| 696 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 697 | System.currentTimeMillis() + delay, mRunInitIntent); |
| 698 | } |
| 699 | } |
| 700 | } catch (RemoteException e) { |
| 701 | // can't happen, the transport is local |
| 702 | } |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 705 | // ----- Track installation/removal of packages ----- |
| 706 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 707 | public void onReceive(Context context, Intent intent) { |
| 708 | if (DEBUG) Log.d(TAG, "Received broadcast " + intent); |
| 709 | |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 710 | String action = intent.getAction(); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 711 | boolean replacing = false; |
| 712 | boolean added = false; |
| 713 | Bundle extras = intent.getExtras(); |
| 714 | String pkgList[] = null; |
| 715 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| 716 | Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 717 | Uri uri = intent.getData(); |
| 718 | if (uri == null) { |
| 719 | return; |
| 720 | } |
| 721 | String pkgName = uri.getSchemeSpecificPart(); |
| 722 | if (pkgName != null) { |
| 723 | pkgList = new String[] { pkgName }; |
| 724 | } |
| 725 | added = Intent.ACTION_PACKAGE_ADDED.equals(action); |
| 726 | replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame^] | 727 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 728 | added = true; |
| 729 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame^] | 730 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 731 | added = false; |
| 732 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 733 | } |
| 734 | if (pkgList == null || pkgList.length == 0) { |
| 735 | return; |
| 736 | } |
| 737 | if (added) { |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 738 | synchronized (mBackupParticipants) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 739 | for (String pkgName : pkgList) { |
| 740 | if (replacing) { |
| 741 | // The package was just upgraded |
| 742 | updatePackageParticipantsLocked(pkgName); |
| 743 | } else { |
| 744 | // The package was just added |
| 745 | addPackageParticipantsLocked(pkgName); |
| 746 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 749 | } else { |
| 750 | if (replacing) { |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 751 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 752 | } else { |
| 753 | synchronized (mBackupParticipants) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 754 | for (String pkgName : pkgList) { |
| 755 | removePackageParticipantsLocked(pkgName); |
| 756 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | }; |
| 762 | |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 763 | // ----- Track connection to GoogleBackupTransport service ----- |
| 764 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 765 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 766 | if (DEBUG) Log.v(TAG, "Connected to Google transport"); |
| 767 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 768 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | public void onServiceDisconnected(ComponentName name) { |
| 772 | if (DEBUG) Log.v(TAG, "Disconnected from Google transport"); |
| 773 | mGoogleTransport = null; |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 774 | registerTransport(name.flattenToShortString(), null); |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 775 | } |
| 776 | }; |
| 777 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 778 | // Add the backup agents in the given package to our set of known backup participants. |
| 779 | // If 'packageName' is null, adds all backup agents in the whole system. |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 780 | void addPackageParticipantsLocked(String packageName) { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 781 | // Look for apps that define the android:backupAgent attribute |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 782 | if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 783 | List<PackageInfo> targetApps = allAgentPackages(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 784 | addPackageParticipantsLockedInner(packageName, targetApps); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 787 | private void addPackageParticipantsLockedInner(String packageName, |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 788 | List<PackageInfo> targetPkgs) { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 789 | if (DEBUG) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 790 | Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| 791 | for (PackageInfo p : targetPkgs) { |
Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 792 | Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 793 | + " uid=" + p.applicationInfo.uid |
| 794 | + " killAfterRestore=" |
| 795 | + (((p.applicationInfo.flags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) ? "true" : "false") |
| 796 | + " restoreNeedsApplication=" |
| 797 | + (((p.applicationInfo.flags & ApplicationInfo.FLAG_RESTORE_NEEDS_APPLICATION) != 0) ? "true" : "false") |
| 798 | ); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 802 | for (PackageInfo pkg : targetPkgs) { |
| 803 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 804 | int uid = pkg.applicationInfo.uid; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 805 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 806 | if (set == null) { |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 807 | set = new HashSet<ApplicationInfo>(); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 808 | mBackupParticipants.put(uid, set); |
| 809 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 810 | set.add(pkg.applicationInfo); |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 811 | |
| 812 | // If we've never seen this app before, schedule a backup for it |
| 813 | if (!mEverStoredApps.contains(pkg.packageName)) { |
| 814 | if (DEBUG) Log.i(TAG, "New app " + pkg.packageName |
| 815 | + " never backed up; scheduling"); |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 816 | dataChanged(pkg.packageName); |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 817 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 818 | } |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 822 | // Remove the given package's entry from our known active set. If |
| 823 | // 'packageName' is null, *all* participating apps will be removed. |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 824 | void removePackageParticipantsLocked(String packageName) { |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 825 | if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 826 | List<PackageInfo> allApps = null; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 827 | if (packageName != null) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 828 | allApps = new ArrayList<PackageInfo>(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 829 | try { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 830 | int flags = PackageManager.GET_SIGNATURES; |
| 831 | allApps.add(mPackageManager.getPackageInfo(packageName, flags)); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 832 | } catch (Exception e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 833 | // just skip it (???) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 834 | } |
| 835 | } else { |
| 836 | // all apps with agents |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 837 | allApps = allAgentPackages(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 838 | } |
| 839 | removePackageParticipantsLockedInner(packageName, allApps); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 842 | private void removePackageParticipantsLockedInner(String packageName, |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 843 | List<PackageInfo> agents) { |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 844 | if (DEBUG) { |
| 845 | Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| 846 | + ") removing " + agents.size() + " entries"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 847 | for (PackageInfo p : agents) { |
| 848 | Log.v(TAG, " - " + p); |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 851 | for (PackageInfo pkg : agents) { |
| 852 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 853 | int uid = pkg.applicationInfo.uid; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 854 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 855 | if (set != null) { |
Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 856 | // Find the existing entry with the same package name, and remove it. |
| 857 | // We can't just remove(app) because the instances are different. |
| 858 | for (ApplicationInfo entry: set) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 859 | if (entry.packageName.equals(pkg.packageName)) { |
Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 860 | set.remove(entry); |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 861 | removeEverBackedUp(pkg.packageName); |
Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 865 | if (set.size() == 0) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 866 | mBackupParticipants.delete(uid); |
| 867 | } |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 873 | // Returns the set of all applications that define an android:backupAgent attribute |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 874 | List<PackageInfo> allAgentPackages() { |
Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 875 | // !!! TODO: cache this and regenerate only when necessary |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 876 | int flags = PackageManager.GET_SIGNATURES; |
| 877 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 878 | int N = packages.size(); |
| 879 | for (int a = N-1; a >= 0; a--) { |
Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 880 | PackageInfo pkg = packages.get(a); |
Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 881 | try { |
| 882 | ApplicationInfo app = pkg.applicationInfo; |
| 883 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| 884 | || app.backupAgentName == null |
| 885 | || (mPackageManager.checkPermission(android.Manifest.permission.BACKUP_DATA, |
| 886 | pkg.packageName) != PackageManager.PERMISSION_GRANTED)) { |
| 887 | packages.remove(a); |
| 888 | } |
| 889 | else { |
| 890 | // we will need the shared library path, so look that up and store it here |
| 891 | app = mPackageManager.getApplicationInfo(pkg.packageName, |
| 892 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 893 | pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles; |
| 894 | } |
| 895 | } catch (NameNotFoundException e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 896 | packages.remove(a); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 899 | return packages; |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 900 | } |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 901 | |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 902 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 903 | // action cannot be passed a null package name. |
| 904 | void updatePackageParticipantsLocked(String packageName) { |
| 905 | if (packageName == null) { |
| 906 | Log.e(TAG, "updatePackageParticipants called with null package name"); |
| 907 | return; |
| 908 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 909 | if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 910 | |
| 911 | // brute force but small code size |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 912 | List<PackageInfo> allApps = allAgentPackages(); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 913 | removePackageParticipantsLockedInner(packageName, allApps); |
| 914 | addPackageParticipantsLockedInner(packageName, allApps); |
Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 917 | // Called from the backup task: record that the given app has been successfully |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 918 | // backed up at least once |
| 919 | void logBackupComplete(String packageName) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 920 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return; |
| 921 | |
| 922 | synchronized (mEverStoredApps) { |
| 923 | if (!mEverStoredApps.add(packageName)) return; |
| 924 | |
| 925 | RandomAccessFile out = null; |
| 926 | try { |
| 927 | out = new RandomAccessFile(mEverStored, "rws"); |
| 928 | out.seek(out.length()); |
| 929 | out.writeUTF(packageName); |
| 930 | } catch (IOException e) { |
| 931 | Log.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored); |
| 932 | } finally { |
| 933 | try { if (out != null) out.close(); } catch (IOException e) {} |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 938 | // Remove our awareness of having ever backed up the given package |
| 939 | void removeEverBackedUp(String packageName) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 940 | if (DEBUG) Log.v(TAG, "Removing backed-up knowledge of " + packageName + ", new set:"); |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 941 | |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 942 | synchronized (mEverStoredApps) { |
| 943 | // Rewrite the file and rename to overwrite. If we reboot in the middle, |
| 944 | // we'll recognize on initialization time that the package no longer |
| 945 | // exists and fix it up then. |
| 946 | File tempKnownFile = new File(mBaseStateDir, "processed.new"); |
| 947 | RandomAccessFile known = null; |
| 948 | try { |
| 949 | known = new RandomAccessFile(tempKnownFile, "rws"); |
| 950 | mEverStoredApps.remove(packageName); |
| 951 | for (String s : mEverStoredApps) { |
| 952 | known.writeUTF(s); |
| 953 | if (DEBUG) Log.v(TAG, " " + s); |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 954 | } |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 955 | known.close(); |
| 956 | known = null; |
| 957 | if (!tempKnownFile.renameTo(mEverStored)) { |
| 958 | throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored); |
| 959 | } |
| 960 | } catch (IOException e) { |
| 961 | // Bad: we couldn't create the new copy. For safety's sake we |
| 962 | // abandon the whole process and remove all what's-backed-up |
| 963 | // state entirely, meaning we'll force a backup pass for every |
| 964 | // participant on the next boot or [re]install. |
| 965 | Log.w(TAG, "Error rewriting " + mEverStored, e); |
| 966 | mEverStoredApps.clear(); |
| 967 | tempKnownFile.delete(); |
| 968 | mEverStored.delete(); |
| 969 | } finally { |
| 970 | try { if (known != null) known.close(); } catch (IOException e) {} |
Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 975 | // Record the current and ancestral backup tokens persistently |
| 976 | void writeRestoreTokens() { |
| 977 | try { |
| 978 | RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd"); |
| 979 | af.writeLong(mAncestralToken); |
| 980 | af.writeLong(mCurrentToken); |
| 981 | af.close(); |
| 982 | } catch (IOException e) { |
| 983 | Log.w(TAG, "Unable to write token file:", e); |
| 984 | } |
| 985 | } |
| 986 | |
Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 987 | // Return the given transport |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 988 | private IBackupTransport getTransport(String transportName) { |
| 989 | synchronized (mTransports) { |
| 990 | IBackupTransport transport = mTransports.get(transportName); |
| 991 | if (transport == null) { |
| 992 | Log.w(TAG, "Requested unavailable transport: " + transportName); |
| 993 | } |
| 994 | return transport; |
Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 995 | } |
Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 998 | // fire off a backup agent, blocking until it attaches or times out |
| 999 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 1000 | IBackupAgent agent = null; |
| 1001 | synchronized(mAgentConnectLock) { |
| 1002 | mConnecting = true; |
| 1003 | mConnectedAgent = null; |
| 1004 | try { |
| 1005 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| 1006 | Log.d(TAG, "awaiting agent for " + app); |
| 1007 | |
| 1008 | // success; wait for the agent to arrive |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1009 | // only wait 10 seconds for the clear data to happen |
| 1010 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1011 | while (mConnecting && mConnectedAgent == null |
| 1012 | && (System.currentTimeMillis() < timeoutMark)) { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1013 | try { |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1014 | mAgentConnectLock.wait(5000); |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1015 | } catch (InterruptedException e) { |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1016 | // just bail |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1017 | return null; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // if we timed out with no connect, abort and move on |
| 1022 | if (mConnecting == true) { |
| 1023 | Log.w(TAG, "Timeout waiting for agent " + app); |
| 1024 | return null; |
| 1025 | } |
| 1026 | agent = mConnectedAgent; |
| 1027 | } |
| 1028 | } catch (RemoteException e) { |
| 1029 | // can't happen |
| 1030 | } |
| 1031 | } |
| 1032 | return agent; |
| 1033 | } |
| 1034 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1035 | // clear an application's data, blocking until the operation completes or times out |
| 1036 | void clearApplicationDataSynchronous(String packageName) { |
Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1037 | // Don't wipe packages marked allowClearUserData=false |
| 1038 | try { |
| 1039 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1040 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| 1041 | if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping " |
| 1042 | + packageName); |
| 1043 | return; |
| 1044 | } |
| 1045 | } catch (NameNotFoundException e) { |
| 1046 | Log.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| 1047 | return; |
| 1048 | } |
| 1049 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1050 | ClearDataObserver observer = new ClearDataObserver(); |
| 1051 | |
| 1052 | synchronized(mClearDataLock) { |
| 1053 | mClearingData = true; |
Amith Yamasani | 2e6bca6 | 2009-08-07 20:26:13 -0700 | [diff] [blame] | 1054 | /* This is causing some critical processes to be killed during setup. |
| 1055 | Temporarily revert this change until we find a better solution. |
Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1056 | try { |
| 1057 | mActivityManager.clearApplicationUserData(packageName, observer); |
| 1058 | } catch (RemoteException e) { |
| 1059 | // can't happen because the activity manager is in this process |
| 1060 | } |
Amith Yamasani | 2e6bca6 | 2009-08-07 20:26:13 -0700 | [diff] [blame] | 1061 | */ |
| 1062 | mPackageManager.clearApplicationUserData(packageName, observer); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1063 | |
| 1064 | // only wait 10 seconds for the clear data to happen |
| 1065 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1066 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 1067 | try { |
| 1068 | mClearDataLock.wait(5000); |
| 1069 | } catch (InterruptedException e) { |
| 1070 | // won't happen, but still. |
| 1071 | mClearingData = false; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | class ClearDataObserver extends IPackageDataObserver.Stub { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1078 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1079 | synchronized(mClearDataLock) { |
| 1080 | mClearingData = false; |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1081 | mClearDataLock.notifyAll(); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1086 | // ----- |
| 1087 | // Utility methods used by the asynchronous-with-timeout backup/restore operations |
| 1088 | boolean waitUntilOperationComplete(int token) { |
| 1089 | int finalState = OP_PENDING; |
| 1090 | synchronized (mCurrentOpLock) { |
| 1091 | try { |
| 1092 | while ((finalState = mCurrentOperations.get(token, OP_TIMEOUT)) == OP_PENDING) { |
| 1093 | try { |
| 1094 | mCurrentOpLock.wait(); |
| 1095 | } catch (InterruptedException e) {} |
| 1096 | } |
| 1097 | } catch (IndexOutOfBoundsException e) { |
| 1098 | // the operation has been mysteriously cleared from our |
| 1099 | // bookkeeping -- consider this a success and ignore it. |
| 1100 | } |
| 1101 | } |
| 1102 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 1103 | if (DEBUG) Log.v(TAG, "operation " + token + " complete: finalState=" + finalState); |
| 1104 | return finalState == OP_ACKNOWLEDGED; |
| 1105 | } |
| 1106 | |
| 1107 | void prepareOperationTimeout(int token, long interval) { |
| 1108 | if (DEBUG) Log.v(TAG, "starting timeout: token=" + token + " interval=" + interval); |
| 1109 | mCurrentOperations.put(token, OP_PENDING); |
| 1110 | Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0); |
| 1111 | mBackupHandler.sendMessageDelayed(msg, interval); |
| 1112 | } |
| 1113 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1114 | // ----- Back up a set of applications via a worker thread ----- |
| 1115 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1116 | class PerformBackupTask implements Runnable { |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1117 | private static final String TAG = "PerformBackupThread"; |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1118 | IBackupTransport mTransport; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1119 | ArrayList<BackupRequest> mQueue; |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1120 | File mStateDir; |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1121 | File mJournal; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1122 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1123 | public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue, |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1124 | File journal) { |
Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1125 | mTransport = transport; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1126 | mQueue = queue; |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1127 | mJournal = journal; |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1128 | |
| 1129 | try { |
| 1130 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1131 | } catch (RemoteException e) { |
| 1132 | // can't happen; the transport is local |
| 1133 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1136 | public void run() { |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1137 | int status = BackupConstants.TRANSPORT_OK; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1138 | long startRealtime = SystemClock.elapsedRealtime(); |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1139 | if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| 1140 | |
Christopher Tate | 7958834 | 2009-06-30 16:11:49 -0700 | [diff] [blame] | 1141 | // Backups run at background priority |
| 1142 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 1143 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1144 | try { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1145 | EventLog.writeEvent(EventLogTags.BACKUP_START, mTransport.transportDirName()); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1146 | |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1147 | // If we haven't stored package manager metadata yet, we must init the transport. |
| 1148 | File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| 1149 | if (status == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) { |
| 1150 | Log.i(TAG, "Initializing (wiping) backup state and transport storage"); |
| 1151 | resetBackupState(mStateDir); // Just to make sure. |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1152 | status = mTransport.initializeDevice(); |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1153 | if (status == BackupConstants.TRANSPORT_OK) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1154 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1155 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1156 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1157 | Log.e(TAG, "Transport error in initializeDevice()"); |
| 1158 | } |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1159 | } |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1160 | |
| 1161 | // The package manager doesn't have a proper <application> etc, but since |
| 1162 | // it's running here in the system process we can just set up its agent |
| 1163 | // directly and use a synthetic BackupRequest. We always run this pass |
| 1164 | // because it's cheap and this way we guarantee that we don't get out of |
| 1165 | // step even if we're selecting among various transports at run time. |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1166 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1167 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1168 | mPackageManager, allAgentPackages()); |
| 1169 | BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false); |
| 1170 | pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL; |
| 1171 | status = processOneBackup(pmRequest, |
| 1172 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport); |
| 1173 | } |
Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1174 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1175 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1176 | // Now run all the backups in our queue |
| 1177 | status = doQueuedBackups(mTransport); |
| 1178 | } |
| 1179 | |
| 1180 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1181 | // Tell the transport to finish everything it has buffered |
| 1182 | status = mTransport.finishBackup(); |
| 1183 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1184 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1185 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, mQueue.size(), millis); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1186 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1187 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(finish)"); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1188 | Log.e(TAG, "Transport error in finishBackup()"); |
| 1189 | } |
| 1190 | } |
| 1191 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1192 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1193 | // The backend reports that our dataset has been wiped. We need to |
| 1194 | // reset all of our bookkeeping and instead run a new backup pass for |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1195 | // everything. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1196 | EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName()); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1197 | resetBackupState(mStateDir); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } catch (Exception e) { |
| 1200 | Log.e(TAG, "Error in backup thread", e); |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1201 | status = BackupConstants.TRANSPORT_ERROR; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1202 | } finally { |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1203 | // If everything actually went through and this is the first time we've |
| 1204 | // done a backup, we can now record what the current backup dataset token |
| 1205 | // is. |
| 1206 | if ((mCurrentToken == 0) && (status != BackupConstants.TRANSPORT_OK)) { |
| 1207 | try { |
| 1208 | mCurrentToken = mTransport.getCurrentRestoreSet(); |
| 1209 | } catch (RemoteException e) { /* cannot happen */ } |
| 1210 | writeRestoreTokens(); |
| 1211 | } |
| 1212 | |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1213 | // If things went wrong, we need to re-stage the apps we had expected |
| 1214 | // to be backing up in this pass. This journals the package names in |
| 1215 | // the current active pending-backup file, not in the we are holding |
| 1216 | // here in mJournal. |
| 1217 | if (status != BackupConstants.TRANSPORT_OK) { |
| 1218 | Log.w(TAG, "Backup pass unsuccessful, restaging"); |
| 1219 | for (BackupRequest req : mQueue) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1220 | dataChanged(req.appInfo.packageName); |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1221 | } |
Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 1222 | |
| 1223 | // We also want to reset the backup schedule based on whatever |
| 1224 | // the transport suggests by way of retry/backoff time. |
| 1225 | try { |
| 1226 | startBackupAlarmsLocked(mTransport.requestBackupTime()); |
| 1227 | } catch (RemoteException e) { /* cannot happen */ } |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Either backup was successful, in which case we of course do not need |
| 1231 | // this pass's journal any more; or it failed, in which case we just |
| 1232 | // re-enqueued all of these packages in the current active journal. |
| 1233 | // Either way, we no longer need this pass's journal. |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1234 | if (mJournal != null && !mJournal.delete()) { |
Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1235 | Log.e(TAG, "Unable to remove backup journal file " + mJournal); |
| 1236 | } |
| 1237 | |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1238 | // Only once we're entirely finished do we release the wakelock |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1239 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1240 | backupNow(); |
| 1241 | } |
| 1242 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1243 | mWakelock.release(); |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1244 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1247 | private int doQueuedBackups(IBackupTransport transport) { |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1248 | for (BackupRequest request : mQueue) { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1249 | Log.d(TAG, "starting agent for backup of " + request); |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1250 | |
Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1251 | // Don't run backup, even if requested, if the target app does not have |
| 1252 | // the requisite permission |
| 1253 | if (mPackageManager.checkPermission(android.Manifest.permission.BACKUP_DATA, |
| 1254 | request.appInfo.packageName) != PackageManager.PERMISSION_GRANTED) { |
| 1255 | Log.w(TAG, "Skipping backup of unprivileged package " |
| 1256 | + request.appInfo.packageName); |
| 1257 | continue; |
| 1258 | } |
| 1259 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1260 | IBackupAgent agent = null; |
| 1261 | int mode = (request.fullBackup) |
| 1262 | ? IApplicationThread.BACKUP_MODE_FULL |
| 1263 | : IApplicationThread.BACKUP_MODE_INCREMENTAL; |
| 1264 | try { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1265 | agent = bindToAgentSynchronous(request.appInfo, mode); |
| 1266 | if (agent != null) { |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1267 | int result = processOneBackup(request, agent, transport); |
| 1268 | if (result != BackupConstants.TRANSPORT_OK) return result; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1269 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1270 | } catch (SecurityException ex) { |
| 1271 | // Try for the next one. |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1272 | Log.d(TAG, "error in bind/backup", ex); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1273 | } finally { |
| 1274 | try { // unbind even on timeout, just in case |
| 1275 | mActivityManager.unbindBackupAgent(request.appInfo); |
| 1276 | } catch (RemoteException e) {} |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1279 | |
| 1280 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1281 | } |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1282 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1283 | private int processOneBackup(BackupRequest request, IBackupAgent agent, |
| 1284 | IBackupTransport transport) { |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1285 | final String packageName = request.appInfo.packageName; |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1286 | if (DEBUG) Log.d(TAG, "processOneBackup doBackup() on " + packageName); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1287 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1288 | File savedStateName = new File(mStateDir, packageName); |
| 1289 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 1290 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 1291 | |
| 1292 | ParcelFileDescriptor savedState = null; |
| 1293 | ParcelFileDescriptor backupData = null; |
| 1294 | ParcelFileDescriptor newState = null; |
| 1295 | |
| 1296 | PackageInfo packInfo; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1297 | int token = mTokenGenerator.nextInt(); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1298 | try { |
| 1299 | // Look up the package info & signatures. This is first so that if it |
| 1300 | // throws an exception, there's no file setup yet that would need to |
| 1301 | // be unraveled. |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1302 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 1303 | // The metadata 'package' is synthetic |
| 1304 | packInfo = new PackageInfo(); |
| 1305 | packInfo.packageName = packageName; |
| 1306 | } else { |
| 1307 | packInfo = mPackageManager.getPackageInfo(packageName, |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1308 | PackageManager.GET_SIGNATURES); |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1309 | } |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1310 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1311 | // In a full backup, we pass a null ParcelFileDescriptor as |
| 1312 | // the saved-state "file" |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1313 | if (!request.fullBackup) { |
| 1314 | savedState = ParcelFileDescriptor.open(savedStateName, |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1315 | ParcelFileDescriptor.MODE_READ_ONLY | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1316 | ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary |
| 1317 | } |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1318 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1319 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1320 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1321 | ParcelFileDescriptor.MODE_CREATE | |
| 1322 | ParcelFileDescriptor.MODE_TRUNCATE); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1323 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1324 | newState = ParcelFileDescriptor.open(newStateName, |
| 1325 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1326 | ParcelFileDescriptor.MODE_CREATE | |
| 1327 | ParcelFileDescriptor.MODE_TRUNCATE); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1328 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1329 | // Initiate the target's backup pass |
| 1330 | prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL); |
| 1331 | agent.doBackup(savedState, backupData, newState, token, mBackupManagerBinder); |
| 1332 | boolean success = waitUntilOperationComplete(token); |
| 1333 | |
| 1334 | if (!success) { |
| 1335 | // timeout -- bail out into the failed-transaction logic |
| 1336 | throw new RuntimeException("Backup timeout"); |
| 1337 | } |
| 1338 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1339 | logBackupComplete(packageName); |
| 1340 | if (DEBUG) Log.v(TAG, "doBackup() success"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1341 | } catch (Exception e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1342 | Log.e(TAG, "Error backing up " + packageName, e); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1343 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, e.toString()); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1344 | backupDataName.delete(); |
| 1345 | newStateName.delete(); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1346 | return BackupConstants.TRANSPORT_ERROR; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1347 | } finally { |
| 1348 | try { if (savedState != null) savedState.close(); } catch (IOException e) {} |
| 1349 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 1350 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 1351 | savedState = backupData = newState = null; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1352 | synchronized (mCurrentOpLock) { |
| 1353 | mCurrentOperations.clear(); |
| 1354 | } |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // Now propagate the newly-backed-up data to the transport |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1358 | int result = BackupConstants.TRANSPORT_OK; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1359 | try { |
| 1360 | int size = (int) backupDataName.length(); |
| 1361 | if (size > 0) { |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1362 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1363 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1364 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 1365 | result = transport.performBackup(packInfo, backupData); |
| 1366 | } |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1367 | |
Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1368 | // TODO - We call finishBackup() for each application backed up, because |
| 1369 | // we need to know now whether it succeeded or failed. Instead, we should |
| 1370 | // hold off on finishBackup() until the end, which implies holding off on |
| 1371 | // renaming *all* the output state files (see below) until that happens. |
| 1372 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1373 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1374 | result = transport.finishBackup(); |
Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1375 | } |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1376 | } else { |
| 1377 | if (DEBUG) Log.i(TAG, "no backup data written; not calling transport"); |
| 1378 | } |
| 1379 | |
| 1380 | // After successful transport, delete the now-stale data |
| 1381 | // and juggle the files so that next time we supply the agent |
| 1382 | // with the new state file it just created. |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1383 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1384 | backupDataName.delete(); |
| 1385 | newStateName.renameTo(savedStateName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1386 | EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, packageName, size); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1387 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1388 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1389 | } |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1390 | } catch (Exception e) { |
| 1391 | Log.e(TAG, "Transport error backing up " + packageName, e); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1392 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1393 | result = BackupConstants.TRANSPORT_ERROR; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1394 | } finally { |
| 1395 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1396 | } |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1397 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1398 | return result; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1399 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1402 | |
| 1403 | // ----- Restore handling ----- |
| 1404 | |
Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 1405 | private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) { |
| 1406 | // If the target resides on the system partition, we allow it to restore |
| 1407 | // data from the like-named package in a restore set even if the signatures |
| 1408 | // do not match. (Unlike general applications, those flashed to the system |
| 1409 | // partition will be signed with the device's platform certificate, so on |
| 1410 | // different phones the same system app will have different signatures.) |
| 1411 | if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1412 | if (DEBUG) Log.v(TAG, "System app " + target.packageName + " - skipping sig check"); |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
Christopher Tate | 20efdf6 | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 1416 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 1417 | // !!! TODO: is this the right policy? |
Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 1418 | Signature[] deviceSigs = target.signatures; |
Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 1419 | if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| 1420 | + " device=" + deviceSigs); |
Christopher Tate | 20efdf6 | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 1421 | if ((storedSigs == null || storedSigs.length == 0) |
| 1422 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 1423 | return true; |
| 1424 | } |
| 1425 | if (storedSigs == null || deviceSigs == null) { |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1429 | // !!! TODO: this demands that every stored signature match one |
| 1430 | // that is present on device, and does not demand the converse. |
| 1431 | // Is this this right policy? |
| 1432 | int nStored = storedSigs.length; |
| 1433 | int nDevice = deviceSigs.length; |
| 1434 | |
| 1435 | for (int i=0; i < nStored; i++) { |
| 1436 | boolean match = false; |
| 1437 | for (int j=0; j < nDevice; j++) { |
| 1438 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 1439 | match = true; |
| 1440 | break; |
| 1441 | } |
| 1442 | } |
| 1443 | if (!match) { |
| 1444 | return false; |
| 1445 | } |
| 1446 | } |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1450 | class PerformRestoreTask implements Runnable { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1451 | private IBackupTransport mTransport; |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1452 | private IRestoreObserver mObserver; |
Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 1453 | private long mToken; |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1454 | private PackageInfo mTargetPackage; |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1455 | private File mStateDir; |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1456 | |
Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 1457 | class RestoreRequest { |
| 1458 | public PackageInfo app; |
| 1459 | public int storedAppVersion; |
| 1460 | |
| 1461 | RestoreRequest(PackageInfo _app, int _version) { |
| 1462 | app = _app; |
| 1463 | storedAppVersion = _version; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1467 | PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer, |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1468 | long restoreSetToken, PackageInfo targetPackage) { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1469 | mTransport = transport; |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1470 | mObserver = observer; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1471 | mToken = restoreSetToken; |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1472 | mTargetPackage = targetPackage; |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1473 | |
| 1474 | try { |
| 1475 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1476 | } catch (RemoteException e) { |
| 1477 | // can't happen; the transport is local |
| 1478 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1481 | public void run() { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1482 | long startRealtime = SystemClock.elapsedRealtime(); |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1483 | if (DEBUG) Log.v(TAG, "Beginning restore process mTransport=" + mTransport |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1484 | + " mObserver=" + mObserver + " mToken=" + Long.toHexString(mToken) |
| 1485 | + " mTargetPackage=" + mTargetPackage); |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1486 | /** |
| 1487 | * Restore sequence: |
| 1488 | * |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1489 | * 1. get the restore set description for our identity |
| 1490 | * 2. for each app in the restore set: |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1491 | * 2.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] | 1492 | * 3. for each app in the restore queue: |
| 1493 | * 3.a. clear the app data |
| 1494 | * 3.b. get the restore data for the app from the transport |
| 1495 | * 3.c. launch the backup agent for the app |
| 1496 | * 3.d. agent.doRestore() with the data from the server |
| 1497 | * 3.e. unbind the agent [and kill the app?] |
| 1498 | * 4. shut down the transport |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1499 | * |
| 1500 | * On errors, we try our best to recover and move on to the next |
| 1501 | * application, but if necessary we abort the whole operation -- |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1502 | * the user is waiting, after all. |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1503 | */ |
| 1504 | |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1505 | int error = -1; // assume error |
| 1506 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1507 | // build the set of apps to restore |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1508 | try { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1509 | // TODO: Log this before getAvailableRestoreSets, somehow |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1510 | EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken); |
Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1511 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1512 | // Get the list of all packages which have backup enabled. |
| 1513 | // (Include the Package Manager metadata pseudo-package first.) |
| 1514 | ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>(); |
| 1515 | PackageInfo omPackage = new PackageInfo(); |
| 1516 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 1517 | restorePackages.add(omPackage); |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1518 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1519 | List<PackageInfo> agentPackages = allAgentPackages(); |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1520 | if (mTargetPackage == null) { |
| 1521 | restorePackages.addAll(agentPackages); |
| 1522 | } else { |
| 1523 | // Just one package to attempt restore of |
| 1524 | restorePackages.add(mTargetPackage); |
| 1525 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1526 | |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1527 | // let the observer know that we're running |
| 1528 | if (mObserver != null) { |
| 1529 | try { |
| 1530 | // !!! TODO: get an actual count from the transport after |
| 1531 | // its startRestore() runs? |
| 1532 | mObserver.restoreStarting(restorePackages.size()); |
| 1533 | } catch (RemoteException e) { |
| 1534 | Log.d(TAG, "Restore observer died at restoreStarting"); |
| 1535 | mObserver = null; |
| 1536 | } |
| 1537 | } |
| 1538 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1539 | if (mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0])) != |
| 1540 | BackupConstants.TRANSPORT_OK) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1541 | Log.e(TAG, "Error starting restore operation"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1542 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1543 | return; |
| 1544 | } |
| 1545 | |
| 1546 | String packageName = mTransport.nextRestorePackage(); |
| 1547 | if (packageName == null) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1548 | Log.e(TAG, "Error getting first restore package"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1549 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1550 | return; |
| 1551 | } else if (packageName.equals("")) { |
| 1552 | Log.i(TAG, "No restore data available"); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1553 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1554 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1555 | return; |
| 1556 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 1557 | Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| 1558 | + "\", found only \"" + packageName + "\""); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1559 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1560 | "Package manager data missing"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1561 | return; |
| 1562 | } |
| 1563 | |
| 1564 | // Pull the Package Manager metadata from the restore set first |
| 1565 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1566 | mPackageManager, agentPackages); |
| 1567 | processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind())); |
| 1568 | |
Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 1569 | // Verify that the backup set includes metadata. If not, we can't do |
| 1570 | // signature/version verification etc, so we simply do not proceed with |
| 1571 | // the restore operation. |
Christopher Tate | 3d7cd13 | 2009-07-07 14:23:07 -0700 | [diff] [blame] | 1572 | if (!pmAgent.hasMetadata()) { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1573 | Log.e(TAG, "No restore metadata available, so not restoring settings"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1574 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1575 | "Package manager restore metadata missing"); |
Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 1576 | return; |
| 1577 | } |
| 1578 | |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1579 | int count = 0; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1580 | for (;;) { |
| 1581 | packageName = mTransport.nextRestorePackage(); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1582 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1583 | if (packageName == null) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1584 | Log.e(TAG, "Error getting next restore package"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1585 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1586 | return; |
| 1587 | } else if (packageName.equals("")) { |
| 1588 | break; |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1589 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1590 | |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1591 | if (mObserver != null) { |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1592 | try { |
| 1593 | mObserver.onUpdate(count); |
| 1594 | } catch (RemoteException e) { |
| 1595 | Log.d(TAG, "Restore observer died in onUpdate"); |
| 1596 | mObserver = null; |
| 1597 | } |
| 1598 | } |
| 1599 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1600 | Metadata metaInfo = pmAgent.getRestoredMetadata(packageName); |
| 1601 | if (metaInfo == null) { |
| 1602 | Log.e(TAG, "Missing metadata for " + packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1603 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1604 | "Package metadata missing"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1605 | continue; |
| 1606 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1607 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1608 | PackageInfo packageInfo; |
| 1609 | try { |
| 1610 | int flags = PackageManager.GET_SIGNATURES; |
| 1611 | packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 1612 | } catch (NameNotFoundException e) { |
| 1613 | Log.e(TAG, "Invalid package restoring data", e); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1614 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1615 | "Package missing on device"); |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1619 | if (metaInfo.versionCode > packageInfo.versionCode) { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1620 | String message = "Version " + metaInfo.versionCode |
| 1621 | + " > installed version " + packageInfo.versionCode; |
| 1622 | Log.w(TAG, "Package " + packageName + ": " + message); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1623 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, message); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1624 | continue; |
| 1625 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1626 | |
Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 1627 | if (!signaturesMatch(metaInfo.signatures, packageInfo)) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1628 | Log.w(TAG, "Signature mismatch restoring " + packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1629 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1630 | "Signature mismatch"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1631 | continue; |
| 1632 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1633 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1634 | if (DEBUG) Log.v(TAG, "Package " + packageName |
| 1635 | + " restore version [" + metaInfo.versionCode |
| 1636 | + "] is compatible with installed version [" |
| 1637 | + packageInfo.versionCode + "]"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1638 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1639 | // Now perform the actual restore: first clear the app's data |
| 1640 | // if appropriate |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1641 | clearApplicationDataSynchronous(packageName); |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1642 | |
| 1643 | // Then set up and bind the agent (with a restricted Application object |
| 1644 | // unless the application says otherwise) |
| 1645 | boolean useRealApp = (packageInfo.applicationInfo.flags |
| 1646 | & ApplicationInfo.FLAG_RESTORE_NEEDS_APPLICATION) != 0; |
| 1647 | if (DEBUG && useRealApp) { |
| 1648 | Log.v(TAG, "agent requires real Application subclass for restore"); |
| 1649 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1650 | IBackupAgent agent = bindToAgentSynchronous( |
| 1651 | packageInfo.applicationInfo, |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1652 | (useRealApp ? IApplicationThread.BACKUP_MODE_INCREMENTAL |
| 1653 | : IApplicationThread.BACKUP_MODE_RESTORE)); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1654 | if (agent == null) { |
| 1655 | Log.w(TAG, "Can't find backup agent for " + packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1656 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1657 | "Restore agent missing"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1658 | continue; |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1661 | // And then finally run the restore on this agent |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1662 | try { |
| 1663 | processOneRestore(packageInfo, metaInfo.versionCode, agent); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1664 | ++count; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1665 | } finally { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1666 | // unbind and tidy up even on timeout or failure, just in case |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1667 | mActivityManager.unbindBackupAgent(packageInfo.applicationInfo); |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1668 | |
| 1669 | // The agent was probably running with a stub Application object, |
| 1670 | // which isn't a valid run mode for the main app logic. Shut |
| 1671 | // down the app so that next time it's launched, it gets the |
| 1672 | // usual full initialization. |
| 1673 | if ((packageInfo.applicationInfo.flags |
| 1674 | & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) { |
| 1675 | if (DEBUG) Log.d(TAG, "Restore complete, killing host process of " |
| 1676 | + packageInfo.applicationInfo.processName); |
| 1677 | mActivityManager.killApplicationProcess( |
| 1678 | packageInfo.applicationInfo.processName, |
| 1679 | packageInfo.applicationInfo.uid); |
| 1680 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1681 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1682 | } |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1683 | |
| 1684 | // if we get this far, report success to the observer |
| 1685 | error = 0; |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1686 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1687 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, count, millis); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1688 | } catch (Exception e) { |
| 1689 | Log.e(TAG, "Error in restore thread", e); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1690 | } finally { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1691 | if (DEBUG) Log.d(TAG, "finishing restore mObserver=" + mObserver); |
| 1692 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1693 | try { |
| 1694 | mTransport.finishRestore(); |
| 1695 | } catch (RemoteException e) { |
| 1696 | Log.e(TAG, "Error finishing restore", e); |
| 1697 | } |
Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1698 | |
| 1699 | if (mObserver != null) { |
| 1700 | try { |
| 1701 | mObserver.restoreFinished(error); |
| 1702 | } catch (RemoteException e) { |
| 1703 | Log.d(TAG, "Restore observer died at restoreFinished"); |
| 1704 | } |
| 1705 | } |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1706 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1707 | // If this was a restoreAll operation, record that this was our |
| 1708 | // ancestral dataset |
| 1709 | if (mTargetPackage == null) { |
| 1710 | mAncestralToken = mToken; |
| 1711 | writeRestoreTokens(); |
| 1712 | } |
| 1713 | |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1714 | // done; we can finally release the wakelock |
| 1715 | mWakelock.release(); |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1719 | // Do the guts of a restore of one application, using mTransport.getRestoreData(). |
| 1720 | void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) { |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1721 | // !!! TODO: actually run the restore through mTransport |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1722 | final String packageName = app.packageName; |
| 1723 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1724 | if (DEBUG) Log.d(TAG, "processOneRestore packageName=" + packageName); |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1725 | |
Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1726 | // Don't restore to unprivileged packages |
| 1727 | if (mPackageManager.checkPermission(android.Manifest.permission.BACKUP_DATA, |
| 1728 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 1729 | Log.d(TAG, "Skipping restore of unprivileged package " + packageName); |
| 1730 | } |
| 1731 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1732 | // !!! TODO: get the dirs from the transport |
| 1733 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1734 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 1735 | File savedStateName = new File(mStateDir, packageName); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1736 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1737 | ParcelFileDescriptor backupData = null; |
| 1738 | ParcelFileDescriptor newState = null; |
| 1739 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1740 | int token = mTokenGenerator.nextInt(); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1741 | try { |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1742 | // Run the transport's restore pass |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1743 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1744 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1745 | ParcelFileDescriptor.MODE_CREATE | |
| 1746 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 1747 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1748 | if (mTransport.getRestoreData(backupData) != BackupConstants.TRANSPORT_OK) { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1749 | Log.e(TAG, "Error getting restore data for " + packageName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1750 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1751 | return; |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | // Okay, we have the data. Now have the agent do the restore. |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1755 | backupData.close(); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1756 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1757 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 1758 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1759 | newState = ParcelFileDescriptor.open(newStateName, |
| 1760 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1761 | ParcelFileDescriptor.MODE_CREATE | |
| 1762 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 1763 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1764 | // Kick off the restore, checking for hung agents |
| 1765 | prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL); |
| 1766 | agent.doRestore(backupData, appVersionCode, newState, token, mBackupManagerBinder); |
| 1767 | boolean success = waitUntilOperationComplete(token); |
| 1768 | |
| 1769 | if (!success) { |
| 1770 | throw new RuntimeException("restore timeout"); |
| 1771 | } |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1772 | |
| 1773 | // if everything went okay, remember the recorded state now |
Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1774 | // |
| 1775 | // !!! TODO: the restored data should be migrated on the server |
| 1776 | // side into the current dataset. In that case the new state file |
| 1777 | // we just created would reflect the data already extant in the |
| 1778 | // backend, so there'd be nothing more to do. Until that happens, |
| 1779 | // however, we need to make sure that we record the data to the |
| 1780 | // current backend dataset. (Yes, this means shipping the data over |
| 1781 | // the wire in both directions. That's bad, but consistency comes |
| 1782 | // first, then efficiency.) Once we introduce server-side data |
| 1783 | // migration to the newly-restored device's dataset, we will change |
| 1784 | // the following from a discard of the newly-written state to the |
| 1785 | // "correct" operation of renaming into the canonical state blob. |
| 1786 | newStateName.delete(); // TODO: remove; see above comment |
| 1787 | //newStateName.renameTo(savedStateName); // TODO: replace with this |
| 1788 | |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1789 | int size = (int) backupDataName.length(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1790 | EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, packageName, size); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1791 | } catch (Exception e) { |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1792 | Log.e(TAG, "Error restoring data for " + packageName, e); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1793 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString()); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1794 | |
Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 1795 | // If the agent fails restore, it might have put the app's data |
| 1796 | // into an incoherent state. For consistency we wipe its data |
| 1797 | // again in this case before propagating the exception |
Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 1798 | clearApplicationDataSynchronous(packageName); |
Christopher Tate | 1531dc8 | 2009-07-24 16:37:43 -0700 | [diff] [blame] | 1799 | } finally { |
| 1800 | backupDataName.delete(); |
Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1801 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 1802 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 1803 | backupData = newState = null; |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1804 | mCurrentOperations.delete(token); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1805 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1809 | class PerformClearTask implements Runnable { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1810 | IBackupTransport mTransport; |
| 1811 | PackageInfo mPackage; |
| 1812 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1813 | PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1814 | mTransport = transport; |
| 1815 | mPackage = packageInfo; |
| 1816 | } |
| 1817 | |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1818 | public void run() { |
| 1819 | try { |
| 1820 | // Clear the on-device backup state to ensure a full backup next time |
| 1821 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 1822 | File stateFile = new File(stateDir, mPackage.packageName); |
| 1823 | stateFile.delete(); |
| 1824 | |
| 1825 | // Tell the transport to remove all the persistent storage for the app |
Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 1826 | // TODO - need to handle failures |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1827 | mTransport.clearBackupData(mPackage); |
| 1828 | } catch (RemoteException e) { |
| 1829 | // can't happen; the transport is local |
| 1830 | } finally { |
| 1831 | try { |
Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 1832 | // TODO - need to handle failures |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1833 | mTransport.finishBackup(); |
| 1834 | } catch (RemoteException e) { |
| 1835 | // can't happen; the transport is local |
| 1836 | } |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1837 | |
| 1838 | // Last but not least, release the cpu |
| 1839 | mWakelock.release(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1844 | class PerformInitializeTask implements Runnable { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1845 | HashSet<String> mQueue; |
| 1846 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1847 | PerformInitializeTask(HashSet<String> transportNames) { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1848 | mQueue = transportNames; |
| 1849 | } |
| 1850 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1851 | public void run() { |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1852 | try { |
| 1853 | for (String transportName : mQueue) { |
| 1854 | IBackupTransport transport = getTransport(transportName); |
| 1855 | if (transport == null) { |
| 1856 | Log.e(TAG, "Requested init for " + transportName + " but not found"); |
| 1857 | continue; |
| 1858 | } |
| 1859 | |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1860 | Log.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1861 | EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName()); |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1862 | long startRealtime = SystemClock.elapsedRealtime(); |
| 1863 | int status = transport.initializeDevice(); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1864 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1865 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1866 | status = transport.finishBackup(); |
| 1867 | } |
| 1868 | |
| 1869 | // Okay, the wipe really happened. Clean up our local bookkeeping. |
| 1870 | if (status == BackupConstants.TRANSPORT_OK) { |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1871 | Log.i(TAG, "Device init successful"); |
| 1872 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1873 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1874 | resetBackupState(new File(mBaseStateDir, transport.transportDirName())); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1875 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1876 | synchronized (mQueueLock) { |
| 1877 | recordInitPendingLocked(false, transportName); |
| 1878 | } |
Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1879 | } else { |
| 1880 | // If this didn't work, requeue this one and try again |
| 1881 | // after a suitable interval |
| 1882 | Log.e(TAG, "Transport error in initializeDevice()"); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1883 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1884 | synchronized (mQueueLock) { |
| 1885 | recordInitPendingLocked(true, transportName); |
| 1886 | } |
| 1887 | // do this via another alarm to make sure of the wakelock states |
| 1888 | long delay = transport.requestBackupTime(); |
| 1889 | if (DEBUG) Log.w(TAG, "init failed on " |
| 1890 | + transportName + " resched in " + delay); |
| 1891 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 1892 | System.currentTimeMillis() + delay, mRunInitIntent); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1893 | } |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1894 | } |
| 1895 | } catch (RemoteException e) { |
| 1896 | // can't happen; the transports are local |
| 1897 | } catch (Exception e) { |
| 1898 | Log.e(TAG, "Unexpected error performing init", e); |
| 1899 | } finally { |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1900 | // Done; release the wakelock |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1901 | mWakelock.release(); |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1906 | |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1907 | // ----- IBackupManager binder interface ----- |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1908 | |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1909 | public void dataChanged(String packageName) { |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1910 | // Record that we need a backup pass for the caller. Since multiple callers |
| 1911 | // may share a uid, we need to note all candidates within that uid and schedule |
| 1912 | // a backup pass for each of them. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1913 | EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1914 | |
Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 1915 | // If the caller does not hold the BACKUP permission, it can only request a |
| 1916 | // backup of its own data. |
| 1917 | HashSet<ApplicationInfo> targets; |
Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1918 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 1919 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 1920 | targets = mBackupParticipants.get(Binder.getCallingUid()); |
| 1921 | } else { |
| 1922 | // a caller with full permission can ask to back up any participating app |
| 1923 | // !!! TODO: allow backup of ANY app? |
Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 1924 | targets = new HashSet<ApplicationInfo>(); |
| 1925 | int N = mBackupParticipants.size(); |
| 1926 | for (int i = 0; i < N; i++) { |
| 1927 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 1928 | if (s != null) { |
| 1929 | targets.addAll(s); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1933 | if (targets != null) { |
| 1934 | synchronized (mQueueLock) { |
| 1935 | // 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] | 1936 | for (ApplicationInfo app : targets) { |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 1937 | // validate the caller-supplied package name against the known set of |
| 1938 | // packages associated with this uid |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1939 | if (app.packageName.equals(packageName)) { |
Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1940 | // Add the caller to the set of pending backups. If there is |
| 1941 | // one already there, then overwrite it, but no harm done. |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1942 | BackupRequest req = new BackupRequest(app, false); |
Christopher Tate | a7de384 | 2009-07-07 14:50:26 -0700 | [diff] [blame] | 1943 | if (mPendingBackups.put(app, req) == null) { |
| 1944 | // Journal this request in case of crash. The put() |
| 1945 | // operation returned null when this package was not already |
| 1946 | // in the set; we want to avoid touching the disk redundantly. |
| 1947 | writeToJournalLocked(packageName); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1948 | |
Christopher Tate | 22b60d8 | 2009-07-07 16:36:02 -0700 | [diff] [blame] | 1949 | if (DEBUG) { |
| 1950 | int numKeys = mPendingBackups.size(); |
| 1951 | Log.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| 1952 | for (BackupRequest b : mPendingBackups.values()) { |
| 1953 | Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName); |
| 1954 | } |
| 1955 | } |
| 1956 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1957 | } |
| 1958 | } |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1959 | } |
Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1960 | } else { |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1961 | Log.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 1962 | + " uid=" + Binder.getCallingUid()); |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1963 | } |
| 1964 | } |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1965 | |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1966 | private void writeToJournalLocked(String str) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1967 | RandomAccessFile out = null; |
| 1968 | try { |
| 1969 | if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir); |
| 1970 | out = new RandomAccessFile(mJournal, "rws"); |
| 1971 | out.seek(out.length()); |
| 1972 | out.writeUTF(str); |
| 1973 | } catch (IOException e) { |
| 1974 | Log.e(TAG, "Can't write " + str + " to backup journal", e); |
| 1975 | mJournal = null; |
| 1976 | } finally { |
| 1977 | try { if (out != null) out.close(); } catch (IOException e) {} |
Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1981 | // Clear the given package's backup data from the current transport |
| 1982 | public void clearBackupData(String packageName) { |
| 1983 | if (DEBUG) Log.v(TAG, "clearBackupData() of " + packageName); |
| 1984 | PackageInfo info; |
| 1985 | try { |
| 1986 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 1987 | } catch (NameNotFoundException e) { |
| 1988 | Log.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| 1989 | return; |
| 1990 | } |
| 1991 | |
| 1992 | // If the caller does not hold the BACKUP permission, it can only request a |
| 1993 | // wipe of its own backed-up data. |
| 1994 | HashSet<ApplicationInfo> apps; |
Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 1995 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1996 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 1997 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 1998 | } else { |
| 1999 | // a caller with full permission can ask to back up any participating app |
| 2000 | // !!! TODO: allow data-clear of ANY app? |
| 2001 | if (DEBUG) Log.v(TAG, "Privileged caller, allowing clear of other apps"); |
| 2002 | apps = new HashSet<ApplicationInfo>(); |
| 2003 | int N = mBackupParticipants.size(); |
| 2004 | for (int i = 0; i < N; i++) { |
| 2005 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 2006 | if (s != null) { |
| 2007 | apps.addAll(s); |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | // now find the given package in the set of candidate apps |
| 2013 | for (ApplicationInfo app : apps) { |
| 2014 | if (app.packageName.equals(packageName)) { |
| 2015 | if (DEBUG) Log.v(TAG, "Found the app - running clear process"); |
| 2016 | // found it; fire off the clear request |
| 2017 | synchronized (mQueueLock) { |
Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 2018 | long oldId = Binder.clearCallingIdentity(); |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2019 | mWakelock.acquire(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 2020 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 2021 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 2022 | mBackupHandler.sendMessage(msg); |
Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 2023 | Binder.restoreCallingIdentity(oldId); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 2024 | } |
| 2025 | break; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 2030 | // Run a backup pass immediately for any applications that have declared |
| 2031 | // that they have pending updates. |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 2032 | public void backupNow() { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2033 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow"); |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2034 | |
Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 2035 | if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass"); |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 2036 | synchronized (mQueueLock) { |
Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 2037 | // Because the alarms we are using can jitter, and we want an *immediate* |
| 2038 | // backup pass to happen, we restart the timer beginning with "next time," |
| 2039 | // then manually fire the backup trigger intent ourselves. |
| 2040 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2041 | try { |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2042 | mRunBackupIntent.send(); |
| 2043 | } catch (PendingIntent.CanceledException e) { |
| 2044 | // should never happen |
| 2045 | Log.e(TAG, "run-backup intent cancelled!"); |
| 2046 | } |
Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2049 | |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2050 | // Enable/disable the backup service |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2051 | public void setBackupEnabled(boolean enable) { |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2052 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 2053 | "setBackupEnabled"); |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2054 | |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 2055 | Log.i(TAG, "Backup enabled => " + enable); |
| 2056 | |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2057 | boolean wasEnabled = mEnabled; |
| 2058 | synchronized (this) { |
Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 2059 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 2060 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2061 | mEnabled = enable; |
| 2062 | } |
| 2063 | |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 2064 | synchronized (mQueueLock) { |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2065 | if (enable && !wasEnabled && mProvisioned) { |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 2066 | // if we've just been enabled, start scheduling backup passes |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2067 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 2068 | } else if (!enable) { |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2069 | // No longer enabled, so stop running backups |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 2070 | if (DEBUG) Log.i(TAG, "Opting out of backup"); |
| 2071 | |
Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 2072 | mAlarmManager.cancel(mRunBackupIntent); |
Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 2073 | |
| 2074 | // This also constitutes an opt-out, so we wipe any data for |
| 2075 | // this device from the backend. We start that process with |
| 2076 | // an alarm in order to guarantee wakelock states. |
| 2077 | if (wasEnabled && mProvisioned) { |
| 2078 | // NOTE: we currently flush every registered transport, not just |
| 2079 | // the currently-active one. |
| 2080 | HashSet<String> allTransports; |
| 2081 | synchronized (mTransports) { |
| 2082 | allTransports = new HashSet<String>(mTransports.keySet()); |
| 2083 | } |
| 2084 | // build the set of transports for which we are posting an init |
| 2085 | for (String transport : allTransports) { |
| 2086 | recordInitPendingLocked(true, transport); |
| 2087 | } |
| 2088 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), |
| 2089 | mRunInitIntent); |
| 2090 | } |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 2093 | } |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2094 | |
Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 2095 | // Enable/disable automatic restore of app data at install time |
| 2096 | public void setAutoRestore(boolean doAutoRestore) { |
| 2097 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 2098 | "setBackupEnabled"); |
| 2099 | |
| 2100 | Log.i(TAG, "Auto restore => " + doAutoRestore); |
| 2101 | |
| 2102 | synchronized (this) { |
| 2103 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 2104 | Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0); |
| 2105 | mAutoRestore = doAutoRestore; |
| 2106 | } |
| 2107 | } |
| 2108 | |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2109 | // Mark the backup service as having been provisioned |
| 2110 | public void setBackupProvisioned(boolean available) { |
| 2111 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 2112 | "setBackupProvisioned"); |
| 2113 | |
| 2114 | boolean wasProvisioned = mProvisioned; |
| 2115 | synchronized (this) { |
| 2116 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 2117 | Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0); |
| 2118 | mProvisioned = available; |
| 2119 | } |
| 2120 | |
| 2121 | synchronized (mQueueLock) { |
| 2122 | if (available && !wasProvisioned && mEnabled) { |
| 2123 | // we're now good to go, so start the backup alarms |
| 2124 | startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL); |
| 2125 | } else if (!available) { |
| 2126 | // No longer enabled, so stop running backups |
| 2127 | Log.w(TAG, "Backup service no longer provisioned"); |
| 2128 | mAlarmManager.cancel(mRunBackupIntent); |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | private void startBackupAlarmsLocked(long delayBeforeFirstBackup) { |
Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 2134 | // We used to use setInexactRepeating(), but that may be linked to |
| 2135 | // backups running at :00 more often than not, creating load spikes. |
| 2136 | // Schedule at an exact time for now, and also add a bit of "fuzz". |
| 2137 | |
| 2138 | Random random = new Random(); |
| 2139 | long when = System.currentTimeMillis() + delayBeforeFirstBackup + |
| 2140 | random.nextInt(FUZZ_MILLIS); |
| 2141 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, |
| 2142 | BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent); |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2143 | mNextBackupPass = when; |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2146 | // Report whether the backup mechanism is currently enabled |
| 2147 | public boolean isBackupEnabled() { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2148 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2149 | return mEnabled; // no need to synchronize just to read it |
| 2150 | } |
| 2151 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2152 | // Report the name of the currently active transport |
| 2153 | public String getCurrentTransport() { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2154 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 2155 | "getCurrentTransport"); |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 2156 | Log.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2157 | return mCurrentTransport; |
Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2160 | // Report all known, available backup transports |
| 2161 | public String[] listAllTransports() { |
Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 2162 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2163 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2164 | String[] list = null; |
| 2165 | ArrayList<String> known = new ArrayList<String>(); |
| 2166 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 2167 | if (entry.getValue() != null) { |
| 2168 | known.add(entry.getKey()); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | if (known.size() > 0) { |
| 2173 | list = new String[known.size()]; |
| 2174 | known.toArray(list); |
| 2175 | } |
| 2176 | return list; |
| 2177 | } |
| 2178 | |
| 2179 | // Select which transport to use for the next backup operation. If the given |
| 2180 | // name is not one of the available transports, no action is taken and the method |
| 2181 | // returns null. |
| 2182 | public String selectBackupTransport(String transport) { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2183 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2184 | |
| 2185 | synchronized (mTransports) { |
| 2186 | String prevTransport = null; |
| 2187 | if (mTransports.get(transport) != null) { |
| 2188 | prevTransport = mCurrentTransport; |
| 2189 | mCurrentTransport = transport; |
Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 2190 | Settings.Secure.putString(mContext.getContentResolver(), |
| 2191 | Settings.Secure.BACKUP_TRANSPORT, transport); |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2192 | Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| 2193 | + " returning " + prevTransport); |
| 2194 | } else { |
| 2195 | Log.w(TAG, "Attempt to select unavailable transport " + transport); |
| 2196 | } |
| 2197 | return prevTransport; |
| 2198 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | // Callback: a requested backup agent has been instantiated. This should only |
| 2202 | // be called from the Activity Manager. |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2203 | public void agentConnected(String packageName, IBinder agentBinder) { |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2204 | synchronized(mAgentConnectLock) { |
| 2205 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 2206 | Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| 2207 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 2208 | mConnectedAgent = agent; |
| 2209 | mConnecting = false; |
| 2210 | } else { |
| 2211 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 2212 | + " claiming agent connected"); |
| 2213 | } |
| 2214 | mAgentConnectLock.notifyAll(); |
| 2215 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 2219 | // 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] | 2220 | // will be null. This should only be called from the Activity Manager. |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2221 | public void agentDisconnected(String packageName) { |
| 2222 | // TODO: handle backup being interrupted |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2223 | synchronized(mAgentConnectLock) { |
| 2224 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 2225 | mConnectedAgent = null; |
| 2226 | mConnecting = false; |
| 2227 | } else { |
| 2228 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 2229 | + " claiming agent disconnected"); |
| 2230 | } |
| 2231 | mAgentConnectLock.notifyAll(); |
| 2232 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2233 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2234 | |
Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 2235 | // Hand off a restore session |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2236 | public IRestoreSession beginRestoreSession(String transport) { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2237 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "beginRestoreSession"); |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2238 | |
| 2239 | synchronized(this) { |
| 2240 | if (mActiveRestoreSession != null) { |
| 2241 | Log.d(TAG, "Restore session requested but one already active"); |
| 2242 | return null; |
| 2243 | } |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 2244 | mActiveRestoreSession = new ActiveRestoreSession(transport); |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2245 | } |
| 2246 | return mActiveRestoreSession; |
Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 2247 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2248 | |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 2249 | // Note that a currently-active backup agent has notified us that it has |
| 2250 | // completed the given outstanding asynchronous backup/restore operation. |
| 2251 | public void opComplete(int token) { |
| 2252 | synchronized (mCurrentOpLock) { |
| 2253 | if (DEBUG) Log.v(TAG, "opComplete: " + token); |
| 2254 | mCurrentOperations.put(token, OP_ACKNOWLEDGED); |
| 2255 | mCurrentOpLock.notifyAll(); |
| 2256 | } |
| 2257 | } |
| 2258 | |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2259 | // ----- Restore session ----- |
| 2260 | |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 2261 | class ActiveRestoreSession extends IRestoreSession.Stub { |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2262 | private static final String TAG = "RestoreSession"; |
| 2263 | |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2264 | private IBackupTransport mRestoreTransport = null; |
| 2265 | RestoreSet[] mRestoreSets = null; |
| 2266 | |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 2267 | ActiveRestoreSession(String transport) { |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2268 | mRestoreTransport = getTransport(transport); |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | // --- Binder interface --- |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2272 | public synchronized RestoreSet[] getAvailableRestoreSets() { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2273 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 2274 | "getAvailableRestoreSets"); |
| 2275 | |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2276 | try { |
Christopher Tate | 4338304 | 2009-07-13 15:17:13 -0700 | [diff] [blame] | 2277 | if (mRestoreTransport == null) { |
| 2278 | Log.w(TAG, "Null transport getting restore sets"); |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2279 | return null; |
| 2280 | } |
| 2281 | if (mRestoreSets == null) { // valid transport; do the one-time fetch |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2282 | mRestoreSets = mRestoreTransport.getAvailableRestoreSets(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2283 | if (mRestoreSets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2284 | } |
| 2285 | return mRestoreSets; |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2286 | } catch (Exception e) { |
| 2287 | Log.e(TAG, "Error in getAvailableRestoreSets", e); |
| 2288 | return null; |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2289 | } |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 2292 | public synchronized int restoreAll(long token, IRestoreObserver observer) { |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2293 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 2294 | "performRestore"); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 2295 | |
Christopher Tate | f2c321a | 2009-08-10 15:43:36 -0700 | [diff] [blame] | 2296 | if (DEBUG) Log.d(TAG, "performRestore token=" + Long.toHexString(token) |
| 2297 | + " observer=" + observer); |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 2298 | |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2299 | if (mRestoreTransport == null || mRestoreSets == null) { |
| 2300 | Log.e(TAG, "Ignoring performRestore() with no restore set"); |
| 2301 | return -1; |
| 2302 | } |
| 2303 | |
Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 2304 | synchronized (mQueueLock) { |
Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 2305 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 2306 | if (token == mRestoreSets[i].token) { |
| 2307 | long oldId = Binder.clearCallingIdentity(); |
Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 2308 | mWakelock.acquire(); |
| 2309 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 2310 | msg.obj = new RestoreParams(mRestoreTransport, observer, token); |
| 2311 | mBackupHandler.sendMessage(msg); |
| 2312 | Binder.restoreCallingIdentity(oldId); |
| 2313 | return 0; |
| 2314 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 2315 | } |
| 2316 | } |
Christopher Tate | 0e0b4ae | 2009-08-10 16:13:47 -0700 | [diff] [blame] | 2317 | |
| 2318 | Log.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2319 | return -1; |
| 2320 | } |
| 2321 | |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 2322 | public synchronized int restorePackage(String packageName, IRestoreObserver observer) { |
| 2323 | if (DEBUG) Log.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer); |
| 2324 | |
| 2325 | PackageInfo app = null; |
| 2326 | try { |
| 2327 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 2328 | } catch (NameNotFoundException nnf) { |
| 2329 | Log.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| 2330 | return -1; |
| 2331 | } |
| 2332 | |
| 2333 | // If the caller is not privileged and is not coming from the target |
| 2334 | // app's uid, throw a permission exception back to the caller. |
| 2335 | int perm = mContext.checkPermission(android.Manifest.permission.BACKUP, |
| 2336 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 2337 | if ((perm == PackageManager.PERMISSION_DENIED) && |
| 2338 | (app.applicationInfo.uid != Binder.getCallingUid())) { |
| 2339 | Log.w(TAG, "restorePackage: bad packageName=" + packageName |
| 2340 | + " or calling uid=" + Binder.getCallingUid()); |
| 2341 | throw new SecurityException("No permission to restore other packages"); |
| 2342 | } |
| 2343 | |
| 2344 | // So far so good; we're allowed to try to restore this package. Now |
| 2345 | // check whether there is data for it in the current dataset, falling back |
| 2346 | // to the ancestral dataset if not. |
| 2347 | long token = mAncestralToken; |
| 2348 | synchronized (mQueueLock) { |
| 2349 | if (mEverStoredApps.contains(packageName)) { |
| 2350 | token = mCurrentToken; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | // If we didn't come up with a place to look -- no ancestral dataset and |
| 2355 | // the app has never been backed up from this device -- there's nothing |
| 2356 | // to do but return failure. |
| 2357 | if (token == 0) { |
| 2358 | return -1; |
| 2359 | } |
| 2360 | |
| 2361 | // Ready to go: enqueue the restore request and claim success |
| 2362 | long oldId = Binder.clearCallingIdentity(); |
| 2363 | mWakelock.acquire(); |
| 2364 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 2365 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, app); |
| 2366 | mBackupHandler.sendMessage(msg); |
| 2367 | Binder.restoreCallingIdentity(oldId); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2371 | public synchronized void endRestoreSession() { |
Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 2372 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 2373 | "endRestoreSession"); |
| 2374 | |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2375 | if (DEBUG) Log.d(TAG, "endRestoreSession"); |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 2376 | |
Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 2377 | synchronized (this) { |
| 2378 | try { |
| 2379 | if (mRestoreTransport != null) mRestoreTransport.finishRestore(); |
| 2380 | } catch (Exception e) { |
| 2381 | Log.e(TAG, "Error in finishRestore", e); |
| 2382 | } finally { |
| 2383 | mRestoreTransport = null; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | synchronized (BackupManagerService.this) { |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 2388 | if (BackupManagerService.this.mActiveRestoreSession == this) { |
| 2389 | BackupManagerService.this.mActiveRestoreSession = null; |
| 2390 | } else { |
| 2391 | Log.e(TAG, "ending non-current restore session"); |
| 2392 | } |
| 2393 | } |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2397 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2398 | @Override |
| 2399 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2400 | synchronized (mQueueLock) { |
Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 2401 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled") |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2402 | + " / " + (!mProvisioned ? "not " : "") + "provisioned / " |
Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 2403 | + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init"); |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2404 | pw.println("Last backup pass: " + mLastBackupPass |
| 2405 | + " (now = " + System.currentTimeMillis() + ')'); |
| 2406 | pw.println(" next scheduled: " + mNextBackupPass); |
| 2407 | |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2408 | pw.println("Available transports:"); |
| 2409 | for (String t : listAllTransports()) { |
Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 2410 | pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t); |
| 2411 | try { |
| 2412 | File dir = new File(mBaseStateDir, getTransport(t).transportDirName()); |
| 2413 | for (File f : dir.listFiles()) { |
| 2414 | pw.println(" " + f.getName() + " - " + f.length() + " state bytes"); |
| 2415 | } |
| 2416 | } catch (RemoteException e) { |
| 2417 | Log.e(TAG, "Error in transportDirName()", e); |
| 2418 | pw.println(" Error: " + e); |
| 2419 | } |
Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 2420 | } |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2421 | |
| 2422 | pw.println("Pending init: " + mPendingInits.size()); |
| 2423 | for (String s : mPendingInits) { |
| 2424 | pw.println(" " + s); |
| 2425 | } |
| 2426 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2427 | int N = mBackupParticipants.size(); |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2428 | pw.println("Participants:"); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2429 | for (int i=0; i<N; i++) { |
| 2430 | int uid = mBackupParticipants.keyAt(i); |
| 2431 | pw.print(" uid: "); |
| 2432 | pw.println(uid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2433 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 2434 | for (ApplicationInfo app: participants) { |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2435 | pw.println(" " + app.packageName); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2436 | } |
| 2437 | } |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2438 | |
Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 2439 | pw.println("Ever backed up: " + mEverStoredApps.size()); |
| 2440 | for (String pkg : mEverStoredApps) { |
| 2441 | pw.println(" " + pkg); |
| 2442 | } |
Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 2443 | |
| 2444 | pw.println("Pending backup: " + mPendingBackups.size()); |
Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 2445 | for (BackupRequest req : mPendingBackups.values()) { |
Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 2446 | pw.println(" " + req); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 2447 | } |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 2450 | } |