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