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