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