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