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