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