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