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