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