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