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