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