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