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