| 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 | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 42 | import android.content.pm.IPackageDeleteObserver; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 43 | import android.content.pm.IPackageInstallObserver; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 44 | import android.content.pm.IPackageManager; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageInfo; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
| Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 47 | import android.content.pm.Signature; |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 48 | import android.content.pm.PackageManager.NameNotFoundException; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 49 | import android.net.Uri; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 50 | import android.os.Binder; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 51 | import android.os.Build; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 52 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 53 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 54 | import android.os.Handler; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 55 | import android.os.HandlerThread; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 56 | import android.os.IBinder; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 57 | import android.os.Looper; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 58 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 59 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 60 | import android.os.PowerManager; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 61 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 62 | import android.os.RemoteException; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 63 | import android.os.SystemClock; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 64 | import android.os.WorkSource; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 65 | import android.provider.Settings; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 66 | import android.util.EventLog; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 67 | import android.util.Slog; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 68 | import android.util.SparseArray; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 69 | import android.util.SparseIntArray; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 70 | import android.util.StringBuilderPrinter; |
| 71 | |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 72 | import com.android.internal.backup.BackupConstants; |
| 73 | import com.android.internal.backup.IBackupTransport; |
| 74 | import com.android.internal.backup.LocalTransport; |
| 75 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| 76 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 77 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 78 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 79 | import java.io.FileDescriptor; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 80 | import java.io.FileInputStream; |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 81 | import java.io.FileNotFoundException; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 82 | import java.io.FileOutputStream; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 83 | import java.io.IOException; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 84 | import java.io.InputStream; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 85 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 86 | import java.io.RandomAccessFile; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 87 | import java.text.SimpleDateFormat; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 88 | import java.util.ArrayList; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 89 | import java.util.Date; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 90 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 91 | import java.util.HashSet; |
| 92 | import java.util.List; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 93 | import java.util.Map; |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 94 | import java.util.Random; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 95 | import java.util.Set; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 96 | import java.util.concurrent.atomic.AtomicBoolean; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 97 | |
| 98 | class BackupManagerService extends IBackupManager.Stub { |
| 99 | private static final String TAG = "BackupManagerService"; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 100 | private static final boolean DEBUG = true; |
| 101 | |
| 102 | // Name and current contents version of the full-backup manifest file |
| 103 | static final String BACKUP_MANIFEST_FILENAME = "_manifest"; |
| 104 | static final int BACKUP_MANIFEST_VERSION = 1; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 105 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 106 | // How often we perform a backup pass. Privileged external callers can |
| 107 | // trigger an immediate pass. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 108 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 109 | |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 110 | // Random variation in backup scheduling time to avoid server load spikes |
| 111 | private static final int FUZZ_MILLIS = 5 * 60 * 1000; |
| 112 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 113 | // The amount of time between the initial provisioning of the device and |
| 114 | // the first backup pass. |
| 115 | private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR; |
| 116 | |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 117 | private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN"; |
| 118 | private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT"; |
| 119 | private static final String RUN_CLEAR_ACTION = "android.app.backup.intent.CLEAR"; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 120 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 121 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 122 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 123 | private static final int MSG_RUN_CLEAR = 4; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 124 | private static final int MSG_RUN_INITIALIZE = 5; |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 125 | private static final int MSG_RUN_GET_RESTORE_SETS = 6; |
| 126 | private static final int MSG_TIMEOUT = 7; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 127 | private static final int MSG_RESTORE_TIMEOUT = 8; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 128 | private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9; |
| 129 | private static final int MSG_RUN_FULL_RESTORE = 10; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 130 | |
| 131 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 132 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 133 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 134 | // Timeout intervals for agent backup & restore operations |
| 135 | static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 136 | static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 137 | static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 138 | static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000; |
| 139 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 140 | // User confirmation timeout for a full backup/restore operation |
| 141 | static final long TIMEOUT_FULL_CONFIRMATION = 30 * 1000; |
| 142 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 143 | private Context mContext; |
| 144 | private PackageManager mPackageManager; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 145 | IPackageManager mPackageManagerBinder; |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 146 | private IActivityManager mActivityManager; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 147 | private PowerManager mPowerManager; |
| 148 | private AlarmManager mAlarmManager; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 149 | IBackupManager mBackupManagerBinder; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 150 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 151 | boolean mEnabled; // access to this is synchronized on 'this' |
| 152 | boolean mProvisioned; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 153 | boolean mAutoRestore; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 154 | PowerManager.WakeLock mWakelock; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 155 | HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 156 | BackupHandler mBackupHandler; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 157 | PendingIntent mRunBackupIntent, mRunInitIntent; |
| 158 | BroadcastReceiver mRunBackupReceiver, mRunInitReceiver; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 159 | // 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] | 160 | final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 161 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 162 | // set of backup services that have pending changes |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 163 | class BackupRequest { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 164 | public ApplicationInfo appInfo; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 165 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 166 | BackupRequest(ApplicationInfo app) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 167 | appInfo = app; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 168 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 169 | |
| 170 | public String toString() { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 171 | return "BackupRequest{app=" + appInfo + "}"; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 172 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 173 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 174 | // Backups that we haven't started yet. Keys are package names. |
| 175 | HashMap<String,BackupRequest> mPendingBackups |
| 176 | = new HashMap<String,BackupRequest>(); |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 177 | |
| 178 | // Pseudoname that we use for the Package Manager metadata "package" |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 179 | static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 180 | |
| 181 | // locking around the pending-backup management |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 182 | final Object mQueueLock = new Object(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 183 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 184 | // The thread performing the sequence of queued backups binds to each app's agent |
| 185 | // in succession. Bind notifications are asynchronously delivered through the |
| 186 | // Activity Manager; use this lock object to signal when a requested binding has |
| 187 | // completed. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 188 | final Object mAgentConnectLock = new Object(); |
| 189 | IBackupAgent mConnectedAgent; |
| 190 | volatile boolean mConnecting; |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 191 | volatile long mLastBackupPass; |
| 192 | volatile long mNextBackupPass; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 193 | |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 194 | // A similar synchronization mechanism around clearing apps' data for restore |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 195 | final Object mClearDataLock = new Object(); |
| 196 | volatile boolean mClearingData; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 197 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 198 | // Transport bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 199 | final HashMap<String,IBackupTransport> mTransports |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 200 | = new HashMap<String,IBackupTransport>(); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 201 | String mCurrentTransport; |
| 202 | IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 203 | ActiveRestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 204 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 205 | class RestoreGetSetsParams { |
| 206 | public IBackupTransport transport; |
| 207 | public ActiveRestoreSession session; |
| 208 | public IRestoreObserver observer; |
| 209 | |
| 210 | RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session, |
| 211 | IRestoreObserver _observer) { |
| 212 | transport = _transport; |
| 213 | session = _session; |
| 214 | observer = _observer; |
| 215 | } |
| 216 | } |
| 217 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 218 | class RestoreParams { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 219 | public IBackupTransport transport; |
| 220 | public IRestoreObserver observer; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 221 | public long token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 222 | public PackageInfo pkgInfo; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 223 | 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] | 224 | public boolean needFullBackup; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 225 | |
| 226 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 227 | long _token, PackageInfo _pkg, int _pmToken, boolean _needFullBackup) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 228 | transport = _transport; |
| 229 | observer = _obs; |
| 230 | token = _token; |
| 231 | pkgInfo = _pkg; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 232 | pmToken = _pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 233 | needFullBackup = _needFullBackup; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 234 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 235 | |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 236 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 237 | boolean _needFullBackup) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 238 | transport = _transport; |
| 239 | observer = _obs; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 240 | token = _token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 241 | pkgInfo = null; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 242 | pmToken = 0; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 243 | needFullBackup = _needFullBackup; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 247 | class ClearParams { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 248 | public IBackupTransport transport; |
| 249 | public PackageInfo packageInfo; |
| 250 | |
| 251 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 252 | transport = _transport; |
| 253 | packageInfo = _info; |
| 254 | } |
| 255 | } |
| 256 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 257 | class FullParams { |
| 258 | public ParcelFileDescriptor fd; |
| 259 | public final AtomicBoolean latch; |
| 260 | public IFullBackupRestoreObserver observer; |
| 261 | |
| 262 | FullParams() { |
| 263 | latch = new AtomicBoolean(false); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | class FullBackupParams extends FullParams { |
| 268 | public boolean includeApks; |
| 269 | public boolean includeShared; |
| 270 | public boolean allApps; |
| 271 | public String[] packages; |
| 272 | |
| 273 | FullBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveShared, |
| 274 | boolean doAllApps, String[] pkgList) { |
| 275 | fd = output; |
| 276 | includeApks = saveApks; |
| 277 | includeShared = saveShared; |
| 278 | allApps = doAllApps; |
| 279 | packages = pkgList; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | class FullRestoreParams extends FullParams { |
| 284 | FullRestoreParams(ParcelFileDescriptor input) { |
| 285 | fd = input; |
| 286 | } |
| 287 | } |
| 288 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 289 | // Bookkeeping of in-flight operations for timeout etc. purposes. The operation |
| 290 | // token is the index of the entry in the pending-operations list. |
| 291 | static final int OP_PENDING = 0; |
| 292 | static final int OP_ACKNOWLEDGED = 1; |
| 293 | static final int OP_TIMEOUT = -1; |
| 294 | |
| 295 | final SparseIntArray mCurrentOperations = new SparseIntArray(); |
| 296 | final Object mCurrentOpLock = new Object(); |
| 297 | final Random mTokenGenerator = new Random(); |
| 298 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 299 | final SparseArray<FullParams> mFullConfirmations = new SparseArray<FullParams>(); |
| 300 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 301 | // Where we keep our journal files and other bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 302 | File mBaseStateDir; |
| 303 | File mDataDir; |
| 304 | File mJournalDir; |
| 305 | File mJournal; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 306 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 307 | // Keep a log of all the apps we've ever backed up, and what the |
| 308 | // dataset tokens are for both the current backup dataset and |
| 309 | // the ancestral dataset. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 310 | private File mEverStored; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 311 | HashSet<String> mEverStoredApps = new HashSet<String>(); |
| 312 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 313 | 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] | 314 | File mTokenFile; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 315 | Set<String> mAncestralPackages = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 316 | long mAncestralToken = 0; |
| 317 | long mCurrentToken = 0; |
| 318 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 319 | // Persistently track the need to do a full init |
| 320 | static final String INIT_SENTINEL_FILE_NAME = "_need_init_"; |
| 321 | HashSet<String> mPendingInits = new HashSet<String>(); // transport names |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 322 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 323 | // Utility: build a new random integer token |
| 324 | int generateToken() { |
| 325 | int token; |
| 326 | do { |
| 327 | synchronized (mTokenGenerator) { |
| 328 | token = mTokenGenerator.nextInt(); |
| 329 | } |
| 330 | } while (token < 0); |
| 331 | return token; |
| 332 | } |
| 333 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 334 | // ----- Asynchronous backup/restore handler thread ----- |
| 335 | |
| 336 | private class BackupHandler extends Handler { |
| 337 | public BackupHandler(Looper looper) { |
| 338 | super(looper); |
| 339 | } |
| 340 | |
| 341 | public void handleMessage(Message msg) { |
| 342 | |
| 343 | switch (msg.what) { |
| 344 | case MSG_RUN_BACKUP: |
| 345 | { |
| 346 | mLastBackupPass = System.currentTimeMillis(); |
| 347 | mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL; |
| 348 | |
| 349 | IBackupTransport transport = getTransport(mCurrentTransport); |
| 350 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 351 | Slog.v(TAG, "Backup requested but no transport available"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 352 | mWakelock.release(); |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | // snapshot the pending-backup set and work on that |
| 357 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 358 | File oldJournal = mJournal; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 359 | synchronized (mQueueLock) { |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 360 | // Do we have any work to do? Construct the work queue |
| 361 | // then release the synchronization lock to actually run |
| 362 | // the backup. |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 363 | if (mPendingBackups.size() > 0) { |
| 364 | for (BackupRequest b: mPendingBackups.values()) { |
| 365 | queue.add(b); |
| 366 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 367 | if (DEBUG) Slog.v(TAG, "clearing pending backups"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 368 | mPendingBackups.clear(); |
| 369 | |
| 370 | // Start a new backup-queue journal file too |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 371 | mJournal = null; |
| 372 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 375 | |
| 376 | if (queue.size() > 0) { |
| 377 | // At this point, we have started a new journal file, and the old |
| 378 | // file identity is being passed to the backup processing thread. |
| 379 | // When it completes successfully, that old journal file will be |
| 380 | // deleted. If we crash prior to that, the old journal is parsed |
| 381 | // at next boot and the journaled requests fulfilled. |
| 382 | (new PerformBackupTask(transport, queue, oldJournal)).run(); |
| 383 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 384 | Slog.v(TAG, "Backup requested but nothing pending"); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 385 | mWakelock.release(); |
| 386 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 387 | break; |
| 388 | } |
| 389 | |
| 390 | case MSG_RUN_FULL_BACKUP: |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 391 | { |
| 392 | FullBackupParams params = (FullBackupParams)msg.obj; |
| 393 | (new PerformFullBackupTask(params.fd, params.observer, params.includeApks, |
| 394 | params.includeShared, params.allApps, params.packages, |
| 395 | params.latch)).run(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 396 | break; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 397 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 398 | |
| 399 | case MSG_RUN_RESTORE: |
| 400 | { |
| 401 | RestoreParams params = (RestoreParams)msg.obj; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 402 | Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 403 | (new PerformRestoreTask(params.transport, params.observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 404 | params.token, params.pkgInfo, params.pmToken, |
| 405 | params.needFullBackup)).run(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 409 | case MSG_RUN_FULL_RESTORE: |
| 410 | { |
| 411 | FullRestoreParams params = (FullRestoreParams)msg.obj; |
| 412 | (new PerformFullRestoreTask(params.fd, params.observer, params.latch)).run(); |
| 413 | break; |
| 414 | } |
| 415 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 416 | case MSG_RUN_CLEAR: |
| 417 | { |
| 418 | ClearParams params = (ClearParams)msg.obj; |
| 419 | (new PerformClearTask(params.transport, params.packageInfo)).run(); |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | case MSG_RUN_INITIALIZE: |
| 424 | { |
| 425 | HashSet<String> queue; |
| 426 | |
| 427 | // Snapshot the pending-init queue and work on that |
| 428 | synchronized (mQueueLock) { |
| 429 | queue = new HashSet<String>(mPendingInits); |
| 430 | mPendingInits.clear(); |
| 431 | } |
| 432 | |
| 433 | (new PerformInitializeTask(queue)).run(); |
| 434 | break; |
| 435 | } |
| 436 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 437 | case MSG_RUN_GET_RESTORE_SETS: |
| 438 | { |
| 439 | // Like other async operations, this is entered with the wakelock held |
| 440 | RestoreSet[] sets = null; |
| 441 | RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj; |
| 442 | try { |
| 443 | sets = params.transport.getAvailableRestoreSets(); |
| 444 | // cache the result in the active session |
| 445 | synchronized (params.session) { |
| 446 | params.session.mRestoreSets = sets; |
| 447 | } |
| 448 | if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 449 | } catch (Exception e) { |
| 450 | Slog.e(TAG, "Error from transport getting set list"); |
| 451 | } finally { |
| 452 | if (params.observer != null) { |
| 453 | try { |
| 454 | params.observer.restoreSetsAvailable(sets); |
| 455 | } catch (RemoteException re) { |
| 456 | Slog.e(TAG, "Unable to report listing to observer"); |
| 457 | } catch (Exception e) { |
| 458 | Slog.e(TAG, "Restore observer threw", e); |
| 459 | } |
| 460 | } |
| 461 | |
| Christopher Tate | 2a93509 | 2011-03-03 17:30:32 -0800 | [diff] [blame] | 462 | // Done: reset the session timeout clock |
| 463 | removeMessages(MSG_RESTORE_TIMEOUT); |
| 464 | sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| 465 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 466 | mWakelock.release(); |
| 467 | } |
| 468 | break; |
| 469 | } |
| 470 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 471 | case MSG_TIMEOUT: |
| 472 | { |
| 473 | synchronized (mCurrentOpLock) { |
| 474 | final int token = msg.arg1; |
| 475 | int state = mCurrentOperations.get(token, OP_TIMEOUT); |
| 476 | if (state == OP_PENDING) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 477 | if (DEBUG) Slog.v(TAG, "TIMEOUT: token=" + token); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 478 | mCurrentOperations.put(token, OP_TIMEOUT); |
| 479 | } |
| 480 | mCurrentOpLock.notifyAll(); |
| 481 | } |
| 482 | break; |
| 483 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 484 | |
| 485 | case MSG_RESTORE_TIMEOUT: |
| 486 | { |
| 487 | synchronized (BackupManagerService.this) { |
| 488 | if (mActiveRestoreSession != null) { |
| 489 | // Client app left the restore session dangling. We know that it |
| 490 | // can't be in the middle of an actual restore operation because |
| 491 | // those are executed serially on this same handler thread. Clean |
| 492 | // up now. |
| 493 | Slog.w(TAG, "Restore session timed out; aborting"); |
| 494 | post(mActiveRestoreSession.new EndRestoreRunnable( |
| 495 | BackupManagerService.this, mActiveRestoreSession)); |
| 496 | } |
| 497 | } |
| 498 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 499 | |
| 500 | case MSG_FULL_CONFIRMATION_TIMEOUT: |
| 501 | { |
| 502 | synchronized (mFullConfirmations) { |
| 503 | FullParams params = mFullConfirmations.get(msg.arg1); |
| 504 | if (params != null) { |
| 505 | Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation"); |
| 506 | |
| 507 | // Release the waiter; timeout == completion |
| 508 | signalFullBackupRestoreCompletion(params); |
| 509 | |
| 510 | // Remove the token from the set |
| 511 | mFullConfirmations.delete(msg.arg1); |
| 512 | |
| 513 | // Report a timeout to the observer, if any |
| 514 | if (params.observer != null) { |
| 515 | try { |
| 516 | params.observer.onTimeout(); |
| 517 | } catch (RemoteException e) { |
| 518 | /* don't care if the app has gone away */ |
| 519 | } |
| 520 | } |
| 521 | } else { |
| 522 | Slog.d(TAG, "couldn't find params for token " + msg.arg1); |
| 523 | } |
| 524 | } |
| 525 | break; |
| 526 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // ----- Main service implementation ----- |
| 532 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 533 | public BackupManagerService(Context context) { |
| 534 | mContext = context; |
| 535 | mPackageManager = context.getPackageManager(); |
| Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 536 | mPackageManagerBinder = AppGlobals.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 537 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 538 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 539 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 540 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 541 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 542 | mBackupManagerBinder = asInterface(asBinder()); |
| 543 | |
| 544 | // spin up the backup/restore handler thread |
| 545 | mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 546 | mHandlerThread.start(); |
| 547 | mBackupHandler = new BackupHandler(mHandlerThread.getLooper()); |
| 548 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 549 | // Set up our bookkeeping |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 550 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 551 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 552 | mProvisioned = Settings.Secure.getInt(context.getContentResolver(), |
| Joe Onorato | ab9a2a5 | 2009-07-27 08:56:39 -0700 | [diff] [blame] | 553 | Settings.Secure.BACKUP_PROVISIONED, 0) != 0; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 554 | mAutoRestore = Settings.Secure.getInt(context.getContentResolver(), |
| Christopher Tate | 5035fda | 2010-02-25 18:01:14 -0800 | [diff] [blame] | 555 | Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 556 | // If Encrypted file systems is enabled or disabled, this call will return the |
| 557 | // correct directory. |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 558 | mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 559 | mBaseStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 560 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 561 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 562 | // Alarm receivers for scheduled backups & initialization operations |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 563 | mRunBackupReceiver = new RunBackupReceiver(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 564 | IntentFilter filter = new IntentFilter(); |
| 565 | filter.addAction(RUN_BACKUP_ACTION); |
| 566 | context.registerReceiver(mRunBackupReceiver, filter, |
| 567 | android.Manifest.permission.BACKUP, null); |
| 568 | |
| 569 | mRunInitReceiver = new RunInitializeReceiver(); |
| 570 | filter = new IntentFilter(); |
| 571 | filter.addAction(RUN_INITIALIZE_ACTION); |
| 572 | context.registerReceiver(mRunInitReceiver, filter, |
| 573 | android.Manifest.permission.BACKUP, null); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 574 | |
| 575 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 576 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 577 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 578 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 579 | Intent initIntent = new Intent(RUN_INITIALIZE_ACTION); |
| 580 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 581 | mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0); |
| 582 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 583 | // Set up the backup-request journaling |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 584 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 585 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 586 | mJournal = null; // will be created on first use |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 587 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 588 | // Set up the various sorts of package tracking we do |
| 589 | initPackageTracking(); |
| 590 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 591 | // Build our mapping of uid to backup client services. This implicitly |
| 592 | // schedules a backup pass on the Package Manager metadata the first |
| 593 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 594 | synchronized (mBackupParticipants) { |
| 595 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 598 | // Set up our transport options and initialize the default transport |
| 599 | // TODO: Have transports register themselves somehow? |
| 600 | // TODO: Don't create transports that we don't need to? |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 601 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 602 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 603 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 604 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 605 | mGoogleTransport = null; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 606 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 607 | Settings.Secure.BACKUP_TRANSPORT); |
| 608 | if ("".equals(mCurrentTransport)) { |
| 609 | mCurrentTransport = null; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 610 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 611 | if (DEBUG) Slog.v(TAG, "Starting with transport " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 612 | |
| 613 | // Attach to the Google backup transport. When this comes up, it will set |
| 614 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 615 | // to null. |
| Christopher Tate | a32504f | 2010-04-21 17:58:07 -0700 | [diff] [blame] | 616 | ComponentName transportComponent = new ComponentName("com.google.android.backup", |
| 617 | "com.google.android.backup.BackupTransportService"); |
| 618 | try { |
| 619 | // If there's something out there that is supposed to be the Google |
| 620 | // backup transport, make sure it's legitimately part of the OS build |
| 621 | // and not an app lying about its package name. |
| 622 | ApplicationInfo info = mPackageManager.getApplicationInfo( |
| 623 | transportComponent.getPackageName(), 0); |
| 624 | if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 625 | if (DEBUG) Slog.v(TAG, "Binding to Google transport"); |
| 626 | Intent intent = new Intent().setComponent(transportComponent); |
| 627 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| 628 | } else { |
| 629 | Slog.w(TAG, "Possible Google transport spoof: ignoring " + info); |
| 630 | } |
| 631 | } catch (PackageManager.NameNotFoundException nnf) { |
| 632 | // No such package? No binding. |
| 633 | if (DEBUG) Slog.v(TAG, "Google transport not present"); |
| 634 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 635 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 636 | // Now that we know about valid backup participants, parse any |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 637 | // leftover journal files into the pending backup set |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 638 | parseLeftoverJournals(); |
| 639 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 640 | // Power management |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 641 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 642 | |
| 643 | // Start the backup passes going |
| 644 | setBackupEnabled(areEnabled); |
| 645 | } |
| 646 | |
| 647 | private class RunBackupReceiver extends BroadcastReceiver { |
| 648 | public void onReceive(Context context, Intent intent) { |
| 649 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 650 | synchronized (mQueueLock) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 651 | if (mPendingInits.size() > 0) { |
| 652 | // If there are pending init operations, we process those |
| 653 | // and then settle into the usual periodic backup schedule. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 654 | if (DEBUG) Slog.v(TAG, "Init pending at scheduled backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 655 | try { |
| 656 | mAlarmManager.cancel(mRunInitIntent); |
| 657 | mRunInitIntent.send(); |
| 658 | } catch (PendingIntent.CanceledException ce) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 659 | Slog.e(TAG, "Run init intent cancelled"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 660 | // can't really do more than bail here |
| 661 | } |
| 662 | } else { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 663 | // Don't run backups now if we're disabled or not yet |
| 664 | // fully set up. |
| 665 | if (mEnabled && mProvisioned) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 666 | if (DEBUG) Slog.v(TAG, "Running a backup pass"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 667 | |
| 668 | // Acquire the wakelock and pass it to the backup thread. it will |
| 669 | // be released once backup concludes. |
| 670 | mWakelock.acquire(); |
| 671 | |
| 672 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 673 | mBackupHandler.sendMessage(msg); |
| 674 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 675 | Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | private class RunInitializeReceiver extends BroadcastReceiver { |
| 684 | public void onReceive(Context context, Intent intent) { |
| 685 | if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) { |
| 686 | synchronized (mQueueLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 687 | if (DEBUG) Slog.v(TAG, "Running a device init"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 688 | |
| 689 | // Acquire the wakelock and pass it to the init thread. it will |
| 690 | // be released once init concludes. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 691 | mWakelock.acquire(); |
| 692 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 693 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 694 | mBackupHandler.sendMessage(msg); |
| 695 | } |
| 696 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 697 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 698 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 699 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 700 | private void initPackageTracking() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 701 | if (DEBUG) Slog.v(TAG, "Initializing package tracking"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 702 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 703 | // Remember our ancestral dataset |
| 704 | mTokenFile = new File(mBaseStateDir, "ancestral"); |
| 705 | try { |
| 706 | RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 707 | int version = tf.readInt(); |
| 708 | if (version == CURRENT_ANCESTRAL_RECORD_VERSION) { |
| 709 | mAncestralToken = tf.readLong(); |
| 710 | mCurrentToken = tf.readLong(); |
| 711 | |
| 712 | int numPackages = tf.readInt(); |
| 713 | if (numPackages >= 0) { |
| 714 | mAncestralPackages = new HashSet<String>(); |
| 715 | for (int i = 0; i < numPackages; i++) { |
| 716 | String pkgName = tf.readUTF(); |
| 717 | mAncestralPackages.add(pkgName); |
| 718 | } |
| 719 | } |
| 720 | } |
| Brad Fitzpatrick | 725d8f0 | 2010-11-15 11:12:42 -0800 | [diff] [blame] | 721 | tf.close(); |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 722 | } catch (FileNotFoundException fnf) { |
| 723 | // Probably innocuous |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 724 | Slog.v(TAG, "No ancestral data"); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 725 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 726 | Slog.w(TAG, "Unable to read token file", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 729 | // Keep a log of what apps we've ever backed up. Because we might have |
| 730 | // rebooted in the middle of an operation that was removing something from |
| 731 | // this log, we sanity-check its contents here and reconstruct it. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 732 | mEverStored = new File(mBaseStateDir, "processed"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 733 | File tempProcessedFile = new File(mBaseStateDir, "processed.new"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 734 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 735 | // If we were in the middle of removing something from the ever-backed-up |
| 736 | // file, there might be a transient "processed.new" file still present. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 737 | // Ignore it -- we'll validate "processed" against the current package set. |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 738 | if (tempProcessedFile.exists()) { |
| 739 | tempProcessedFile.delete(); |
| 740 | } |
| 741 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 742 | // If there are previous contents, parse them out then start a new |
| 743 | // file to continue the recordkeeping. |
| 744 | if (mEverStored.exists()) { |
| 745 | RandomAccessFile temp = null; |
| 746 | RandomAccessFile in = null; |
| 747 | |
| 748 | try { |
| 749 | temp = new RandomAccessFile(tempProcessedFile, "rws"); |
| 750 | in = new RandomAccessFile(mEverStored, "r"); |
| 751 | |
| 752 | while (true) { |
| 753 | PackageInfo info; |
| 754 | String pkg = in.readUTF(); |
| 755 | try { |
| 756 | info = mPackageManager.getPackageInfo(pkg, 0); |
| 757 | mEverStoredApps.add(pkg); |
| 758 | temp.writeUTF(pkg); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 759 | if (DEBUG) Slog.v(TAG, " + " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 760 | } catch (NameNotFoundException e) { |
| 761 | // nope, this package was uninstalled; don't include it |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 762 | if (DEBUG) Slog.v(TAG, " - " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } catch (EOFException e) { |
| 766 | // Once we've rewritten the backup history log, atomically replace the |
| 767 | // old one with the new one then reopen the file for continuing use. |
| 768 | if (!tempProcessedFile.renameTo(mEverStored)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 769 | Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 770 | } |
| 771 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 772 | Slog.e(TAG, "Error in processed file", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 773 | } finally { |
| 774 | try { if (temp != null) temp.close(); } catch (IOException e) {} |
| 775 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 776 | } |
| 777 | } |
| 778 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 779 | // Register for broadcasts about package install, etc., so we can |
| 780 | // update the provider list. |
| 781 | IntentFilter filter = new IntentFilter(); |
| 782 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 783 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 784 | filter.addDataScheme("package"); |
| 785 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 786 | // Register for events related to sdcard installation. |
| 787 | IntentFilter sdFilter = new IntentFilter(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 788 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 789 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 790 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 793 | private void parseLeftoverJournals() { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 794 | for (File f : mJournalDir.listFiles()) { |
| 795 | if (mJournal == null || f.compareTo(mJournal) != 0) { |
| 796 | // This isn't the current journal, so it must be a leftover. Read |
| 797 | // out the package names mentioned there and schedule them for |
| 798 | // backup. |
| 799 | RandomAccessFile in = null; |
| 800 | try { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 801 | Slog.i(TAG, "Found stale backup journal, scheduling"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 802 | in = new RandomAccessFile(f, "r"); |
| 803 | while (true) { |
| 804 | String packageName = in.readUTF(); |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 805 | Slog.i(TAG, " " + packageName); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 806 | dataChangedImpl(packageName); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 807 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 808 | } catch (EOFException e) { |
| 809 | // no more data; we're done |
| 810 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 811 | Slog.e(TAG, "Can't read " + f, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 812 | } finally { |
| 813 | // close/delete the file |
| 814 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 815 | f.delete(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 821 | // Maintain persistent state around whether need to do an initialize operation. |
| 822 | // Must be called with the queue lock held. |
| 823 | void recordInitPendingLocked(boolean isPending, String transportName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 824 | if (DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 825 | + " on transport " + transportName); |
| 826 | try { |
| 827 | IBackupTransport transport = getTransport(transportName); |
| 828 | String transportDirName = transport.transportDirName(); |
| 829 | File stateDir = new File(mBaseStateDir, transportDirName); |
| 830 | File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 831 | |
| 832 | if (isPending) { |
| 833 | // We need an init before we can proceed with sending backup data. |
| 834 | // Record that with an entry in our set of pending inits, as well as |
| 835 | // journaling it via creation of a sentinel file. |
| 836 | mPendingInits.add(transportName); |
| 837 | try { |
| 838 | (new FileOutputStream(initPendingFile)).close(); |
| 839 | } catch (IOException ioe) { |
| 840 | // Something is badly wrong with our permissions; just try to move on |
| 841 | } |
| 842 | } else { |
| 843 | // No more initialization needed; wipe the journal and reset our state. |
| 844 | initPendingFile.delete(); |
| 845 | mPendingInits.remove(transportName); |
| 846 | } |
| 847 | } catch (RemoteException e) { |
| 848 | // can't happen; the transport is local |
| 849 | } |
| 850 | } |
| 851 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 852 | // Reset all of our bookkeeping, in response to having been told that |
| 853 | // the backend data has been wiped [due to idle expiry, for example], |
| 854 | // so we must re-upload all saved settings. |
| 855 | void resetBackupState(File stateFileDir) { |
| 856 | synchronized (mQueueLock) { |
| 857 | // Wipe the "what we've ever backed up" tracking |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 858 | mEverStoredApps.clear(); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 859 | mEverStored.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 860 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 861 | mCurrentToken = 0; |
| 862 | writeRestoreTokens(); |
| 863 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 864 | // Remove all the state files |
| 865 | for (File sf : stateFileDir.listFiles()) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 866 | // ... but don't touch the needs-init sentinel |
| 867 | if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) { |
| 868 | sf.delete(); |
| 869 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 870 | } |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 871 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 872 | |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 873 | // Enqueue a new backup of every participant |
| 874 | int N = mBackupParticipants.size(); |
| 875 | for (int i=0; i<N; i++) { |
| 876 | int uid = mBackupParticipants.keyAt(i); |
| 877 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 878 | for (ApplicationInfo app: participants) { |
| 879 | dataChangedImpl(app.packageName); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 884 | // Add a transport to our set of available backends. If 'transport' is null, this |
| 885 | // 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] | 886 | private void registerTransport(String name, IBackupTransport transport) { |
| 887 | synchronized (mTransports) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 888 | if (DEBUG) Slog.v(TAG, "Registering transport " + name + " = " + transport); |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 889 | if (transport != null) { |
| 890 | mTransports.put(name, transport); |
| 891 | } else { |
| 892 | mTransports.remove(name); |
| Christopher Tate | b0dcaaf | 2010-01-29 16:27:04 -0800 | [diff] [blame] | 893 | if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) { |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 894 | mCurrentTransport = null; |
| 895 | } |
| 896 | // Nothing further to do in the unregistration case |
| 897 | return; |
| 898 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 899 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 900 | |
| 901 | // If the init sentinel file exists, we need to be sure to perform the init |
| 902 | // as soon as practical. We also create the state directory at registration |
| 903 | // time to ensure it's present from the outset. |
| 904 | try { |
| 905 | String transportName = transport.transportDirName(); |
| 906 | File stateDir = new File(mBaseStateDir, transportName); |
| 907 | stateDir.mkdirs(); |
| 908 | |
| 909 | File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 910 | if (initSentinel.exists()) { |
| 911 | synchronized (mQueueLock) { |
| 912 | mPendingInits.add(transportName); |
| 913 | |
| 914 | // TODO: pick a better starting time than now + 1 minute |
| 915 | long delay = 1000 * 60; // one minute, in milliseconds |
| 916 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 917 | System.currentTimeMillis() + delay, mRunInitIntent); |
| 918 | } |
| 919 | } |
| 920 | } catch (RemoteException e) { |
| 921 | // can't happen, the transport is local |
| 922 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 925 | // ----- Track installation/removal of packages ----- |
| 926 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 927 | public void onReceive(Context context, Intent intent) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 928 | if (DEBUG) Slog.d(TAG, "Received broadcast " + intent); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 929 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 930 | String action = intent.getAction(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 931 | boolean replacing = false; |
| 932 | boolean added = false; |
| 933 | Bundle extras = intent.getExtras(); |
| 934 | String pkgList[] = null; |
| 935 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| 936 | Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 937 | Uri uri = intent.getData(); |
| 938 | if (uri == null) { |
| 939 | return; |
| 940 | } |
| 941 | String pkgName = uri.getSchemeSpecificPart(); |
| 942 | if (pkgName != null) { |
| 943 | pkgList = new String[] { pkgName }; |
| 944 | } |
| 945 | added = Intent.ACTION_PACKAGE_ADDED.equals(action); |
| 946 | replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 947 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 948 | added = true; |
| 949 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 950 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 951 | added = false; |
| 952 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 953 | } |
| 954 | if (pkgList == null || pkgList.length == 0) { |
| 955 | return; |
| 956 | } |
| 957 | if (added) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 958 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 959 | for (String pkgName : pkgList) { |
| 960 | if (replacing) { |
| 961 | // The package was just upgraded |
| 962 | updatePackageParticipantsLocked(pkgName); |
| 963 | } else { |
| 964 | // The package was just added |
| 965 | addPackageParticipantsLocked(pkgName); |
| 966 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 969 | } else { |
| 970 | if (replacing) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 971 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 972 | } else { |
| 973 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 974 | for (String pkgName : pkgList) { |
| 975 | removePackageParticipantsLocked(pkgName); |
| 976 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | }; |
| 982 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 983 | // ----- Track connection to GoogleBackupTransport service ----- |
| 984 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 985 | public void onServiceConnected(ComponentName name, IBinder service) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 986 | if (DEBUG) Slog.v(TAG, "Connected to Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 987 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 988 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | public void onServiceDisconnected(ComponentName name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 992 | if (DEBUG) Slog.v(TAG, "Disconnected from Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 993 | mGoogleTransport = null; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 994 | registerTransport(name.flattenToShortString(), null); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 995 | } |
| 996 | }; |
| 997 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 998 | // Add the backup agents in the given package to our set of known backup participants. |
| 999 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1000 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1001 | // Look for apps that define the android:backupAgent attribute |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1002 | if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1003 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1004 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1007 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1008 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1009 | if (DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1010 | Slog.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1011 | for (PackageInfo p : targetPkgs) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1012 | Slog.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1013 | + " uid=" + p.applicationInfo.uid |
| 1014 | + " killAfterRestore=" |
| 1015 | + (((p.applicationInfo.flags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) ? "true" : "false") |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1016 | ); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1020 | for (PackageInfo pkg : targetPkgs) { |
| 1021 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 1022 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1023 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1024 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1025 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1026 | mBackupParticipants.put(uid, set); |
| 1027 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1028 | set.add(pkg.applicationInfo); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1029 | |
| 1030 | // If we've never seen this app before, schedule a backup for it |
| 1031 | if (!mEverStoredApps.contains(pkg.packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1032 | if (DEBUG) Slog.i(TAG, "New app " + pkg.packageName |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1033 | + " never backed up; scheduling"); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 1034 | dataChangedImpl(pkg.packageName); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1035 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1036 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1040 | // Remove the given package's entry from our known active set. If |
| 1041 | // 'packageName' is null, *all* participating apps will be removed. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1042 | void removePackageParticipantsLocked(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1043 | if (DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1044 | List<String> allApps = new ArrayList<String>(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1045 | if (packageName != null) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1046 | allApps.add(packageName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1047 | } else { |
| 1048 | // all apps with agents |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1049 | List<PackageInfo> knownPackages = allAgentPackages(); |
| 1050 | for (PackageInfo pkg : knownPackages) { |
| 1051 | allApps.add(pkg.packageName); |
| 1052 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1053 | } |
| 1054 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1057 | private void removePackageParticipantsLockedInner(String packageName, |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1058 | List<String> allPackageNames) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1059 | if (DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1060 | Slog.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1061 | + ") removing " + allPackageNames.size() + " entries"); |
| 1062 | for (String p : allPackageNames) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1063 | Slog.v(TAG, " - " + p); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1066 | for (String pkg : allPackageNames) { |
| 1067 | if (packageName == null || pkg.equals(packageName)) { |
| 1068 | int uid = -1; |
| 1069 | try { |
| 1070 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1071 | uid = info.applicationInfo.uid; |
| 1072 | } catch (NameNotFoundException e) { |
| 1073 | // we don't know this package name, so just skip it for now |
| 1074 | continue; |
| 1075 | } |
| 1076 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1077 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1078 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1079 | // Find the existing entry with the same package name, and remove it. |
| 1080 | // We can't just remove(app) because the instances are different. |
| 1081 | for (ApplicationInfo entry: set) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1082 | if (entry.packageName.equals(pkg)) { |
| 1083 | if (DEBUG) Slog.v(TAG, " removing participant " + pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1084 | set.remove(entry); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1085 | removeEverBackedUp(pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1089 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1090 | mBackupParticipants.delete(uid); |
| 1091 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1097 | // Returns the set of all applications that define an android:backupAgent attribute |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1098 | List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1099 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1100 | int flags = PackageManager.GET_SIGNATURES; |
| 1101 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 1102 | int N = packages.size(); |
| 1103 | for (int a = N-1; a >= 0; a--) { |
| Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1104 | PackageInfo pkg = packages.get(a); |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1105 | try { |
| 1106 | ApplicationInfo app = pkg.applicationInfo; |
| 1107 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| Christopher Tate | a87240c | 2010-02-12 14:12:34 -0800 | [diff] [blame] | 1108 | || app.backupAgentName == null) { |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1109 | packages.remove(a); |
| 1110 | } |
| 1111 | else { |
| 1112 | // we will need the shared library path, so look that up and store it here |
| 1113 | app = mPackageManager.getApplicationInfo(pkg.packageName, |
| 1114 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 1115 | pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles; |
| 1116 | } |
| 1117 | } catch (NameNotFoundException e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1118 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1119 | } |
| 1120 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1121 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1122 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1123 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1124 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 1125 | // action cannot be passed a null package name. |
| 1126 | void updatePackageParticipantsLocked(String packageName) { |
| 1127 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1128 | Slog.e(TAG, "updatePackageParticipants called with null package name"); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1129 | return; |
| 1130 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1131 | if (DEBUG) Slog.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1132 | |
| 1133 | // brute force but small code size |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1134 | List<PackageInfo> allApps = allAgentPackages(); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1135 | List<String> allAppNames = new ArrayList<String>(); |
| 1136 | for (PackageInfo pkg : allApps) { |
| 1137 | allAppNames.add(pkg.packageName); |
| 1138 | } |
| 1139 | removePackageParticipantsLockedInner(packageName, allAppNames); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1140 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1143 | // 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] | 1144 | // backed up at least once |
| 1145 | void logBackupComplete(String packageName) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1146 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return; |
| 1147 | |
| 1148 | synchronized (mEverStoredApps) { |
| 1149 | if (!mEverStoredApps.add(packageName)) return; |
| 1150 | |
| 1151 | RandomAccessFile out = null; |
| 1152 | try { |
| 1153 | out = new RandomAccessFile(mEverStored, "rws"); |
| 1154 | out.seek(out.length()); |
| 1155 | out.writeUTF(packageName); |
| 1156 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1157 | Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1158 | } finally { |
| 1159 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1164 | // Remove our awareness of having ever backed up the given package |
| 1165 | void removeEverBackedUp(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1166 | 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] | 1167 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1168 | synchronized (mEverStoredApps) { |
| 1169 | // Rewrite the file and rename to overwrite. If we reboot in the middle, |
| 1170 | // we'll recognize on initialization time that the package no longer |
| 1171 | // exists and fix it up then. |
| 1172 | File tempKnownFile = new File(mBaseStateDir, "processed.new"); |
| 1173 | RandomAccessFile known = null; |
| 1174 | try { |
| 1175 | known = new RandomAccessFile(tempKnownFile, "rws"); |
| 1176 | mEverStoredApps.remove(packageName); |
| 1177 | for (String s : mEverStoredApps) { |
| 1178 | known.writeUTF(s); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1179 | if (DEBUG) Slog.v(TAG, " " + s); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1180 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1181 | known.close(); |
| 1182 | known = null; |
| 1183 | if (!tempKnownFile.renameTo(mEverStored)) { |
| 1184 | throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored); |
| 1185 | } |
| 1186 | } catch (IOException e) { |
| 1187 | // Bad: we couldn't create the new copy. For safety's sake we |
| 1188 | // abandon the whole process and remove all what's-backed-up |
| 1189 | // state entirely, meaning we'll force a backup pass for every |
| 1190 | // participant on the next boot or [re]install. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1191 | Slog.w(TAG, "Error rewriting " + mEverStored, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1192 | mEverStoredApps.clear(); |
| 1193 | tempKnownFile.delete(); |
| 1194 | mEverStored.delete(); |
| 1195 | } finally { |
| 1196 | try { if (known != null) known.close(); } catch (IOException e) {} |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1201 | // Persistently record the current and ancestral backup tokens as well |
| 1202 | // as the set of packages with data [supposedly] available in the |
| 1203 | // ancestral dataset. |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1204 | void writeRestoreTokens() { |
| 1205 | try { |
| 1206 | RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1207 | |
| 1208 | // First, the version number of this record, for futureproofing |
| 1209 | af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION); |
| 1210 | |
| 1211 | // Write the ancestral and current tokens |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1212 | af.writeLong(mAncestralToken); |
| 1213 | af.writeLong(mCurrentToken); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1214 | |
| 1215 | // Now write the set of ancestral packages |
| 1216 | if (mAncestralPackages == null) { |
| 1217 | af.writeInt(-1); |
| 1218 | } else { |
| 1219 | af.writeInt(mAncestralPackages.size()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1220 | if (DEBUG) Slog.v(TAG, "Ancestral packages: " + mAncestralPackages.size()); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1221 | for (String pkgName : mAncestralPackages) { |
| 1222 | af.writeUTF(pkgName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1223 | if (DEBUG) Slog.v(TAG, " " + pkgName); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1226 | af.close(); |
| 1227 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1228 | Slog.w(TAG, "Unable to write token file:", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1232 | // Return the given transport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1233 | private IBackupTransport getTransport(String transportName) { |
| 1234 | synchronized (mTransports) { |
| 1235 | IBackupTransport transport = mTransports.get(transportName); |
| 1236 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1237 | Slog.w(TAG, "Requested unavailable transport: " + transportName); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1238 | } |
| 1239 | return transport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1240 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1243 | // fire off a backup agent, blocking until it attaches or times out |
| 1244 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 1245 | IBackupAgent agent = null; |
| 1246 | synchronized(mAgentConnectLock) { |
| 1247 | mConnecting = true; |
| 1248 | mConnectedAgent = null; |
| 1249 | try { |
| 1250 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1251 | Slog.d(TAG, "awaiting agent for " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1252 | |
| 1253 | // success; wait for the agent to arrive |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1254 | // only wait 10 seconds for the bind to happen |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1255 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1256 | while (mConnecting && mConnectedAgent == null |
| 1257 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1258 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1259 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1260 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1261 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1262 | return null; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // if we timed out with no connect, abort and move on |
| 1267 | if (mConnecting == true) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1268 | Slog.w(TAG, "Timeout waiting for agent " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1269 | return null; |
| 1270 | } |
| 1271 | agent = mConnectedAgent; |
| 1272 | } |
| 1273 | } catch (RemoteException e) { |
| 1274 | // can't happen |
| 1275 | } |
| 1276 | } |
| 1277 | return agent; |
| 1278 | } |
| 1279 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1280 | // clear an application's data, blocking until the operation completes or times out |
| 1281 | void clearApplicationDataSynchronous(String packageName) { |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1282 | // Don't wipe packages marked allowClearUserData=false |
| 1283 | try { |
| 1284 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1285 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1286 | if (DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping " |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1287 | + packageName); |
| 1288 | return; |
| 1289 | } |
| 1290 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1291 | Slog.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1295 | ClearDataObserver observer = new ClearDataObserver(); |
| 1296 | |
| 1297 | synchronized(mClearDataLock) { |
| 1298 | mClearingData = true; |
| Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1299 | try { |
| 1300 | mActivityManager.clearApplicationUserData(packageName, observer); |
| 1301 | } catch (RemoteException e) { |
| 1302 | // can't happen because the activity manager is in this process |
| 1303 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1304 | |
| 1305 | // only wait 10 seconds for the clear data to happen |
| 1306 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1307 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 1308 | try { |
| 1309 | mClearDataLock.wait(5000); |
| 1310 | } catch (InterruptedException e) { |
| 1311 | // won't happen, but still. |
| 1312 | mClearingData = false; |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1319 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1320 | synchronized(mClearDataLock) { |
| 1321 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1322 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1327 | // Get the restore-set token for the best-available restore set for this package: |
| 1328 | // the active set if possible, else the ancestral one. Returns zero if none available. |
| 1329 | long getAvailableRestoreToken(String packageName) { |
| 1330 | long token = mAncestralToken; |
| 1331 | synchronized (mQueueLock) { |
| 1332 | if (mEverStoredApps.contains(packageName)) { |
| 1333 | token = mCurrentToken; |
| 1334 | } |
| 1335 | } |
| 1336 | return token; |
| 1337 | } |
| 1338 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1339 | // ----- |
| 1340 | // Utility methods used by the asynchronous-with-timeout backup/restore operations |
| 1341 | boolean waitUntilOperationComplete(int token) { |
| 1342 | int finalState = OP_PENDING; |
| 1343 | synchronized (mCurrentOpLock) { |
| 1344 | try { |
| 1345 | while ((finalState = mCurrentOperations.get(token, OP_TIMEOUT)) == OP_PENDING) { |
| 1346 | try { |
| 1347 | mCurrentOpLock.wait(); |
| 1348 | } catch (InterruptedException e) {} |
| 1349 | } |
| 1350 | } catch (IndexOutOfBoundsException e) { |
| 1351 | // the operation has been mysteriously cleared from our |
| 1352 | // bookkeeping -- consider this a success and ignore it. |
| 1353 | } |
| 1354 | } |
| 1355 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1356 | if (DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1357 | + " complete: finalState=" + finalState); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1358 | return finalState == OP_ACKNOWLEDGED; |
| 1359 | } |
| 1360 | |
| 1361 | void prepareOperationTimeout(int token, long interval) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1362 | if (DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1363 | + " interval=" + interval); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1364 | synchronized (mCurrentOpLock) { |
| 1365 | mCurrentOperations.put(token, OP_PENDING); |
| 1366 | Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0); |
| 1367 | mBackupHandler.sendMessageDelayed(msg, interval); |
| 1368 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1371 | // ----- Back up a set of applications via a worker thread ----- |
| 1372 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1373 | class PerformBackupTask implements Runnable { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1374 | private static final String TAG = "PerformBackupThread"; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1375 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1376 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1377 | File mStateDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1378 | File mJournal; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1379 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1380 | public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1381 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1382 | mTransport = transport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1383 | mQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1384 | mJournal = journal; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1385 | |
| 1386 | try { |
| 1387 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1388 | } catch (RemoteException e) { |
| 1389 | // can't happen; the transport is local |
| 1390 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1393 | public void run() { |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1394 | int status = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1395 | long startRealtime = SystemClock.elapsedRealtime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1396 | if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1397 | |
| Christopher Tate | 7958834 | 2009-06-30 16:11:49 -0700 | [diff] [blame] | 1398 | // Backups run at background priority |
| 1399 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 1400 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1401 | try { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1402 | EventLog.writeEvent(EventLogTags.BACKUP_START, mTransport.transportDirName()); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1403 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1404 | // If we haven't stored package manager metadata yet, we must init the transport. |
| 1405 | File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| 1406 | if (status == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1407 | Slog.i(TAG, "Initializing (wiping) backup state and transport storage"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1408 | resetBackupState(mStateDir); // Just to make sure. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1409 | status = mTransport.initializeDevice(); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1410 | if (status == BackupConstants.TRANSPORT_OK) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1411 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1412 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1413 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1414 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1415 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1416 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1417 | |
| 1418 | // The package manager doesn't have a proper <application> etc, but since |
| 1419 | // it's running here in the system process we can just set up its agent |
| 1420 | // directly and use a synthetic BackupRequest. We always run this pass |
| 1421 | // because it's cheap and this way we guarantee that we don't get out of |
| 1422 | // step even if we're selecting among various transports at run time. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1423 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1424 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1425 | mPackageManager, allAgentPackages()); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1426 | BackupRequest pmRequest = new BackupRequest(new ApplicationInfo()); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1427 | pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL; |
| 1428 | status = processOneBackup(pmRequest, |
| 1429 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport); |
| 1430 | } |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1431 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1432 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1433 | // Now run all the backups in our queue |
| 1434 | status = doQueuedBackups(mTransport); |
| 1435 | } |
| 1436 | |
| 1437 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1438 | // Tell the transport to finish everything it has buffered |
| 1439 | status = mTransport.finishBackup(); |
| 1440 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1441 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1442 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, mQueue.size(), millis); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1443 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1444 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(finish)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1445 | Slog.e(TAG, "Transport error in finishBackup()"); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1449 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1450 | // The backend reports that our dataset has been wiped. We need to |
| 1451 | // 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] | 1452 | // everything. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1453 | EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName()); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1454 | resetBackupState(mStateDir); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1457 | Slog.e(TAG, "Error in backup thread", e); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1458 | status = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1459 | } finally { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1460 | // If everything actually went through and this is the first time we've |
| 1461 | // done a backup, we can now record what the current backup dataset token |
| 1462 | // is. |
| Christopher Tate | 2950555 | 2010-06-24 15:58:01 -0700 | [diff] [blame] | 1463 | if ((mCurrentToken == 0) && (status == BackupConstants.TRANSPORT_OK)) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1464 | try { |
| 1465 | mCurrentToken = mTransport.getCurrentRestoreSet(); |
| 1466 | } catch (RemoteException e) { /* cannot happen */ } |
| 1467 | writeRestoreTokens(); |
| 1468 | } |
| 1469 | |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1470 | // If things went wrong, we need to re-stage the apps we had expected |
| 1471 | // to be backing up in this pass. This journals the package names in |
| 1472 | // the current active pending-backup file, not in the we are holding |
| 1473 | // here in mJournal. |
| 1474 | if (status != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1475 | Slog.w(TAG, "Backup pass unsuccessful, restaging"); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1476 | for (BackupRequest req : mQueue) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 1477 | dataChangedImpl(req.appInfo.packageName); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1478 | } |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 1479 | |
| 1480 | // We also want to reset the backup schedule based on whatever |
| 1481 | // the transport suggests by way of retry/backoff time. |
| 1482 | try { |
| 1483 | startBackupAlarmsLocked(mTransport.requestBackupTime()); |
| 1484 | } catch (RemoteException e) { /* cannot happen */ } |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | // Either backup was successful, in which case we of course do not need |
| 1488 | // this pass's journal any more; or it failed, in which case we just |
| 1489 | // re-enqueued all of these packages in the current active journal. |
| 1490 | // Either way, we no longer need this pass's journal. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1491 | if (mJournal != null && !mJournal.delete()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1492 | Slog.e(TAG, "Unable to remove backup journal file " + mJournal); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1495 | // Only once we're entirely finished do we release the wakelock |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1496 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1497 | backupNow(); |
| 1498 | } |
| 1499 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1500 | mWakelock.release(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1501 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1504 | private int doQueuedBackups(IBackupTransport transport) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1505 | for (BackupRequest request : mQueue) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1506 | Slog.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1507 | |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1508 | // Verify that the requested app exists; it might be something that |
| 1509 | // requested a backup but was then uninstalled. The request was |
| 1510 | // journalled and rather than tamper with the journal it's safer |
| 1511 | // to sanity-check here. |
| 1512 | try { |
| 1513 | mPackageManager.getPackageInfo(request.appInfo.packageName, 0); |
| 1514 | } catch (NameNotFoundException e) { |
| 1515 | Slog.d(TAG, "Package does not exist; skipping"); |
| 1516 | continue; |
| 1517 | } |
| 1518 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1519 | IBackupAgent agent = null; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1520 | try { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1521 | mWakelock.setWorkSource(new WorkSource(request.appInfo.uid)); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1522 | agent = bindToAgentSynchronous(request.appInfo, |
| 1523 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1524 | if (agent != null) { |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1525 | int result = processOneBackup(request, agent, transport); |
| 1526 | if (result != BackupConstants.TRANSPORT_OK) return result; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1527 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1528 | } catch (SecurityException ex) { |
| 1529 | // Try for the next one. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1530 | Slog.d(TAG, "error in bind/backup", ex); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1531 | } finally { |
| 1532 | try { // unbind even on timeout, just in case |
| 1533 | mActivityManager.unbindBackupAgent(request.appInfo); |
| 1534 | } catch (RemoteException e) {} |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1535 | } |
| 1536 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1537 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1538 | mWakelock.setWorkSource(null); |
| 1539 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1540 | return BackupConstants.TRANSPORT_OK; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1541 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1542 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1543 | private int processOneBackup(BackupRequest request, IBackupAgent agent, |
| 1544 | IBackupTransport transport) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1545 | final String packageName = request.appInfo.packageName; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1546 | if (DEBUG) Slog.d(TAG, "processOneBackup doBackup() on " + packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1547 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1548 | File savedStateName = new File(mStateDir, packageName); |
| 1549 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 1550 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 1551 | |
| 1552 | ParcelFileDescriptor savedState = null; |
| 1553 | ParcelFileDescriptor backupData = null; |
| 1554 | ParcelFileDescriptor newState = null; |
| 1555 | |
| 1556 | PackageInfo packInfo; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1557 | final int token = generateToken(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1558 | try { |
| 1559 | // Look up the package info & signatures. This is first so that if it |
| 1560 | // throws an exception, there's no file setup yet that would need to |
| 1561 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1562 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 1563 | // The metadata 'package' is synthetic |
| 1564 | packInfo = new PackageInfo(); |
| 1565 | packInfo.packageName = packageName; |
| 1566 | } else { |
| 1567 | packInfo = mPackageManager.getPackageInfo(packageName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1568 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1569 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1570 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1571 | // In a full backup, we pass a null ParcelFileDescriptor as |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1572 | // the saved-state "file". This is by definition an incremental, |
| 1573 | // so we build a saved state file to pass. |
| 1574 | savedState = ParcelFileDescriptor.open(savedStateName, |
| 1575 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 1576 | ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1577 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1578 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1579 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1580 | ParcelFileDescriptor.MODE_CREATE | |
| 1581 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1582 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1583 | newState = ParcelFileDescriptor.open(newStateName, |
| 1584 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1585 | ParcelFileDescriptor.MODE_CREATE | |
| 1586 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1587 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1588 | // Initiate the target's backup pass |
| 1589 | prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1590 | agent.doBackup(savedState, backupData, newState, false, |
| 1591 | token, mBackupManagerBinder); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1592 | boolean success = waitUntilOperationComplete(token); |
| 1593 | |
| 1594 | if (!success) { |
| 1595 | // timeout -- bail out into the failed-transaction logic |
| 1596 | throw new RuntimeException("Backup timeout"); |
| 1597 | } |
| 1598 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1599 | logBackupComplete(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1600 | if (DEBUG) Slog.v(TAG, "doBackup() success"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1601 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1602 | Slog.e(TAG, "Error backing up " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1603 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, e.toString()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1604 | backupDataName.delete(); |
| 1605 | newStateName.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1606 | return BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1607 | } finally { |
| 1608 | try { if (savedState != null) savedState.close(); } catch (IOException e) {} |
| 1609 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 1610 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 1611 | savedState = backupData = newState = null; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1612 | synchronized (mCurrentOpLock) { |
| 1613 | mCurrentOperations.clear(); |
| 1614 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | // Now propagate the newly-backed-up data to the transport |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1618 | int result = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1619 | try { |
| 1620 | int size = (int) backupDataName.length(); |
| 1621 | if (size > 0) { |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1622 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1623 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1624 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 1625 | result = transport.performBackup(packInfo, backupData); |
| 1626 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1627 | |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1628 | // TODO - We call finishBackup() for each application backed up, because |
| 1629 | // we need to know now whether it succeeded or failed. Instead, we should |
| 1630 | // hold off on finishBackup() until the end, which implies holding off on |
| 1631 | // renaming *all* the output state files (see below) until that happens. |
| 1632 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1633 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1634 | result = transport.finishBackup(); |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1635 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1636 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1637 | if (DEBUG) Slog.i(TAG, "no backup data written; not calling transport"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | // After successful transport, delete the now-stale data |
| 1641 | // and juggle the files so that next time we supply the agent |
| 1642 | // with the new state file it just created. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1643 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1644 | backupDataName.delete(); |
| 1645 | newStateName.renameTo(savedStateName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1646 | EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, packageName, size); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1647 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1648 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1649 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1650 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1651 | Slog.e(TAG, "Transport error backing up " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1652 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1653 | result = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1654 | } finally { |
| 1655 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1656 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1657 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1658 | return result; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1659 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1662 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1663 | // ----- Full backup to a file/socket ----- |
| 1664 | |
| 1665 | class PerformFullBackupTask implements Runnable { |
| 1666 | ParcelFileDescriptor mOutputFile; |
| 1667 | IFullBackupRestoreObserver mObserver; |
| 1668 | boolean mIncludeApks; |
| 1669 | boolean mIncludeShared; |
| 1670 | boolean mAllApps; |
| 1671 | String[] mPackages; |
| 1672 | AtomicBoolean mLatchObject; |
| 1673 | File mFilesDir; |
| 1674 | File mManifestFile; |
| 1675 | |
| 1676 | PerformFullBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer, |
| 1677 | boolean includeApks, boolean includeShared, |
| 1678 | boolean doAllApps, String[] packages, AtomicBoolean latch) { |
| 1679 | mOutputFile = fd; |
| 1680 | mObserver = observer; |
| 1681 | mIncludeApks = includeApks; |
| 1682 | mIncludeShared = includeShared; |
| 1683 | mAllApps = doAllApps; |
| 1684 | mPackages = packages; |
| 1685 | mLatchObject = latch; |
| 1686 | |
| 1687 | mFilesDir = new File("/data/system"); |
| 1688 | mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME); |
| 1689 | } |
| 1690 | |
| 1691 | @Override |
| 1692 | public void run() { |
| 1693 | final List<PackageInfo> packagesToBackup; |
| 1694 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1695 | Slog.i(TAG, "--- Performing full-dataset backup ---"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1696 | sendStartBackup(); |
| 1697 | |
| 1698 | // doAllApps supersedes the package set if any |
| 1699 | if (mAllApps) { |
| 1700 | packagesToBackup = mPackageManager.getInstalledPackages( |
| 1701 | PackageManager.GET_SIGNATURES); |
| 1702 | } else { |
| 1703 | packagesToBackup = new ArrayList<PackageInfo>(); |
| 1704 | for (String pkgName : mPackages) { |
| 1705 | try { |
| 1706 | packagesToBackup.add(mPackageManager.getPackageInfo(pkgName, |
| 1707 | PackageManager.GET_SIGNATURES)); |
| 1708 | } catch (NameNotFoundException e) { |
| 1709 | Slog.w(TAG, "Unknown package " + pkgName + ", skipping"); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 1714 | // Cull any packages that have indicated that backups are not permitted. |
| 1715 | for (int i = 0; i < packagesToBackup.size(); ) { |
| 1716 | PackageInfo info = packagesToBackup.get(i); |
| 1717 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 1718 | packagesToBackup.remove(i); |
| 1719 | } else { |
| 1720 | i++; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1724 | PackageInfo pkg = null; |
| 1725 | try { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1726 | // Now back up the app data via the agent mechanism |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1727 | int N = packagesToBackup.size(); |
| 1728 | for (int i = 0; i < N; i++) { |
| 1729 | pkg = packagesToBackup.get(i); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1730 | backupOnePackage(pkg); |
| 1731 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1732 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1733 | // Finally, shared storage if requested |
| 1734 | if (mIncludeShared) { |
| 1735 | backupSharedStorage(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } catch (RemoteException e) { |
| 1738 | Slog.e(TAG, "App died during full backup"); |
| 1739 | } finally { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1740 | tearDown(pkg); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1741 | try { |
| 1742 | mOutputFile.close(); |
| 1743 | } catch (IOException e) { |
| 1744 | /* nothing we can do about this */ |
| 1745 | } |
| 1746 | synchronized (mCurrentOpLock) { |
| 1747 | mCurrentOperations.clear(); |
| 1748 | } |
| 1749 | synchronized (mLatchObject) { |
| 1750 | mLatchObject.set(true); |
| 1751 | mLatchObject.notifyAll(); |
| 1752 | } |
| 1753 | sendEndBackup(); |
| 1754 | mWakelock.release(); |
| 1755 | if (DEBUG) Slog.d(TAG, "Full backup pass complete."); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1759 | private void backupOnePackage(PackageInfo pkg) throws RemoteException { |
| 1760 | Slog.d(TAG, "Binding to full backup agent : " + pkg.packageName); |
| 1761 | |
| 1762 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 1763 | IApplicationThread.BACKUP_MODE_FULL); |
| 1764 | if (agent != null) { |
| 1765 | try { |
| 1766 | ApplicationInfo app = pkg.applicationInfo; |
| 1767 | boolean sendApk = mIncludeApks |
| 1768 | && ((app.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0) |
| 1769 | && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 || |
| 1770 | (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); |
| 1771 | |
| 1772 | sendOnBackupPackage(pkg.packageName); |
| 1773 | |
| 1774 | { |
| 1775 | BackupDataOutput output = new BackupDataOutput( |
| 1776 | mOutputFile.getFileDescriptor()); |
| 1777 | |
| 1778 | if (DEBUG) Slog.d(TAG, "Writing manifest for " + pkg.packageName); |
| 1779 | writeAppManifest(pkg, mManifestFile, sendApk); |
| 1780 | FullBackup.backupToTar(pkg.packageName, null, null, |
| 1781 | mFilesDir.getAbsolutePath(), |
| 1782 | mManifestFile.getAbsolutePath(), |
| 1783 | output); |
| 1784 | } |
| 1785 | |
| 1786 | if (DEBUG) Slog.d(TAG, "Calling doBackup()"); |
| 1787 | final int token = generateToken(); |
| 1788 | prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL); |
| 1789 | agent.doBackup(null, mOutputFile, null, sendApk, |
| 1790 | token, mBackupManagerBinder); |
| 1791 | if (!waitUntilOperationComplete(token)) { |
| 1792 | Slog.e(TAG, "Full backup failed on package " + pkg.packageName); |
| 1793 | } else { |
| 1794 | if (DEBUG) Slog.d(TAG, "Full backup success: " + pkg.packageName); |
| 1795 | } |
| 1796 | } catch (IOException e) { |
| 1797 | Slog.e(TAG, "Error backing up " + pkg.packageName, e); |
| 1798 | } |
| 1799 | } else { |
| 1800 | Slog.w(TAG, "Unable to bind to full agent for " + pkg.packageName); |
| 1801 | } |
| 1802 | tearDown(pkg); |
| 1803 | } |
| 1804 | |
| 1805 | private void backupSharedStorage() throws RemoteException { |
| 1806 | PackageInfo pkg = null; |
| 1807 | try { |
| 1808 | pkg = mPackageManager.getPackageInfo("com.android.sharedstoragebackup", 0); |
| 1809 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 1810 | IApplicationThread.BACKUP_MODE_FULL); |
| 1811 | if (agent != null) { |
| 1812 | sendOnBackupPackage("Shared storage"); |
| 1813 | |
| 1814 | final int token = generateToken(); |
| 1815 | prepareOperationTimeout(token, TIMEOUT_SHARED_BACKUP_INTERVAL); |
| 1816 | agent.doBackup(null, mOutputFile, null, false, token, mBackupManagerBinder); |
| 1817 | if (!waitUntilOperationComplete(token)) { |
| 1818 | Slog.e(TAG, "Full backup failed on shared storage"); |
| 1819 | } else { |
| 1820 | if (DEBUG) Slog.d(TAG, "Full shared storage backup success"); |
| 1821 | } |
| 1822 | } else { |
| 1823 | Slog.e(TAG, "Could not bind to shared storage backup agent"); |
| 1824 | } |
| 1825 | } catch (NameNotFoundException e) { |
| 1826 | Slog.e(TAG, "Shared storage backup package not found"); |
| 1827 | } finally { |
| 1828 | tearDown(pkg); |
| 1829 | } |
| 1830 | } |
| 1831 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1832 | private void writeAppManifest(PackageInfo pkg, File manifestFile, boolean withApk) |
| 1833 | throws IOException { |
| 1834 | // Manifest format. All data are strings ending in LF: |
| 1835 | // BACKUP_MANIFEST_VERSION, currently 1 |
| 1836 | // |
| 1837 | // Version 1: |
| 1838 | // package name |
| 1839 | // package's versionCode |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1840 | // platform versionCode |
| 1841 | // getInstallerPackageName() for this package (maybe empty) |
| 1842 | // boolean: "1" if archive includes .apk; any other string means not |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1843 | // number of signatures == N |
| 1844 | // N*: signature byte array in ascii format per Signature.toCharsString() |
| 1845 | StringBuilder builder = new StringBuilder(4096); |
| 1846 | StringBuilderPrinter printer = new StringBuilderPrinter(builder); |
| 1847 | |
| 1848 | printer.println(Integer.toString(BACKUP_MANIFEST_VERSION)); |
| 1849 | printer.println(pkg.packageName); |
| 1850 | printer.println(Integer.toString(pkg.versionCode)); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1851 | printer.println(Integer.toString(Build.VERSION.SDK_INT)); |
| 1852 | |
| 1853 | String installerName = mPackageManager.getInstallerPackageName(pkg.packageName); |
| 1854 | printer.println((installerName != null) ? installerName : ""); |
| 1855 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1856 | printer.println(withApk ? "1" : "0"); |
| 1857 | if (pkg.signatures == null) { |
| 1858 | printer.println("0"); |
| 1859 | } else { |
| 1860 | printer.println(Integer.toString(pkg.signatures.length)); |
| 1861 | for (Signature sig : pkg.signatures) { |
| 1862 | printer.println(sig.toCharsString()); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | FileOutputStream outstream = new FileOutputStream(manifestFile); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1867 | outstream.write(builder.toString().getBytes()); |
| 1868 | outstream.close(); |
| 1869 | } |
| 1870 | |
| 1871 | private void tearDown(PackageInfo pkg) { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1872 | if (pkg != null) { |
| 1873 | final ApplicationInfo app = pkg.applicationInfo; |
| 1874 | if (app != null) { |
| 1875 | try { |
| 1876 | // unbind and tidy up even on timeout or failure, just in case |
| 1877 | mActivityManager.unbindBackupAgent(app); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1878 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 1879 | // The agent was running with a stub Application object, so shut it down. |
| 1880 | if (app.uid != Process.SYSTEM_UID) { |
| 1881 | if (DEBUG) Slog.d(TAG, "Backup complete, killing host process"); |
| 1882 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 1883 | } else { |
| 1884 | if (DEBUG) Slog.d(TAG, "Not killing after restore: " + app.processName); |
| 1885 | } |
| 1886 | } catch (RemoteException e) { |
| 1887 | Slog.d(TAG, "Lost app trying to shut down"); |
| 1888 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1889 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | // wrappers for observer use |
| 1894 | void sendStartBackup() { |
| 1895 | if (mObserver != null) { |
| 1896 | try { |
| 1897 | mObserver.onStartBackup(); |
| 1898 | } catch (RemoteException e) { |
| 1899 | Slog.w(TAG, "full backup observer went away: startBackup"); |
| 1900 | mObserver = null; |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | void sendOnBackupPackage(String name) { |
| 1906 | if (mObserver != null) { |
| 1907 | try { |
| 1908 | // TODO: use a more user-friendly name string |
| 1909 | mObserver.onBackupPackage(name); |
| 1910 | } catch (RemoteException e) { |
| 1911 | Slog.w(TAG, "full backup observer went away: backupPackage"); |
| 1912 | mObserver = null; |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | void sendEndBackup() { |
| 1918 | if (mObserver != null) { |
| 1919 | try { |
| 1920 | mObserver.onEndBackup(); |
| 1921 | } catch (RemoteException e) { |
| 1922 | Slog.w(TAG, "full backup observer went away: endBackup"); |
| 1923 | mObserver = null; |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1930 | // ----- Full restore from a file/socket ----- |
| 1931 | |
| 1932 | // Description of a file in the restore datastream |
| 1933 | static class FileMetadata { |
| 1934 | String packageName; // name of the owning app |
| 1935 | String installerPackageName; // name of the market-type app that installed the owner |
| 1936 | int type; // e.g. FullBackup.TYPE_DIRECTORY |
| 1937 | String domain; // e.g. FullBackup.DATABASE_TREE_TOKEN |
| 1938 | String path; // subpath within the semantic domain |
| 1939 | long mode; // e.g. 0666 (actually int) |
| 1940 | long mtime; // last mod time, UTC time_t (actually int) |
| 1941 | long size; // bytes of content |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 1942 | |
| 1943 | @Override |
| 1944 | public String toString() { |
| 1945 | StringBuilder sb = new StringBuilder(128); |
| 1946 | sb.append("FileMetadata{"); |
| 1947 | sb.append(packageName); sb.append(','); |
| 1948 | sb.append(type); sb.append(','); |
| 1949 | sb.append(domain); sb.append(':'); sb.append(path); sb.append(','); |
| 1950 | sb.append(size); |
| 1951 | sb.append('}'); |
| 1952 | return sb.toString(); |
| 1953 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | enum RestorePolicy { |
| 1957 | IGNORE, |
| 1958 | ACCEPT, |
| 1959 | ACCEPT_IF_APK |
| 1960 | } |
| 1961 | |
| 1962 | class PerformFullRestoreTask implements Runnable { |
| 1963 | ParcelFileDescriptor mInputFile; |
| 1964 | IFullBackupRestoreObserver mObserver; |
| 1965 | AtomicBoolean mLatchObject; |
| 1966 | IBackupAgent mAgent; |
| 1967 | String mAgentPackage; |
| 1968 | ApplicationInfo mTargetApp; |
| 1969 | ParcelFileDescriptor[] mPipes = null; |
| 1970 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 1971 | long mBytes; |
| 1972 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1973 | // possible handling states for a given package in the restore dataset |
| 1974 | final HashMap<String, RestorePolicy> mPackagePolicies |
| 1975 | = new HashMap<String, RestorePolicy>(); |
| 1976 | |
| 1977 | // installer package names for each encountered app, derived from the manifests |
| 1978 | final HashMap<String, String> mPackageInstallers = new HashMap<String, String>(); |
| 1979 | |
| 1980 | // Signatures for a given package found in its manifest file |
| 1981 | final HashMap<String, Signature[]> mManifestSignatures |
| 1982 | = new HashMap<String, Signature[]>(); |
| 1983 | |
| 1984 | // Packages we've already wiped data on when restoring their first file |
| 1985 | final HashSet<String> mClearedPackages = new HashSet<String>(); |
| 1986 | |
| 1987 | PerformFullRestoreTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer, |
| 1988 | AtomicBoolean latch) { |
| 1989 | mInputFile = fd; |
| 1990 | mObserver = observer; |
| 1991 | mLatchObject = latch; |
| 1992 | mAgent = null; |
| 1993 | mAgentPackage = null; |
| 1994 | mTargetApp = null; |
| 1995 | |
| 1996 | // Which packages we've already wiped data on. We prepopulate this |
| 1997 | // with a whitelist of packages known to be unclearable. |
| 1998 | mClearedPackages.add("android"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1999 | mClearedPackages.add("com.android.providers.settings"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2000 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | class RestoreFileRunnable implements Runnable { |
| 2004 | IBackupAgent mAgent; |
| 2005 | FileMetadata mInfo; |
| 2006 | ParcelFileDescriptor mSocket; |
| 2007 | int mToken; |
| 2008 | |
| 2009 | RestoreFileRunnable(IBackupAgent agent, FileMetadata info, |
| 2010 | ParcelFileDescriptor socket, int token) throws IOException { |
| 2011 | mAgent = agent; |
| 2012 | mInfo = info; |
| 2013 | mToken = token; |
| 2014 | |
| 2015 | // This class is used strictly for process-local binder invocations. The |
| 2016 | // semantics of ParcelFileDescriptor differ in this case; in particular, we |
| 2017 | // do not automatically get a 'dup'ed descriptor that we can can continue |
| 2018 | // to use asynchronously from the caller. So, we make sure to dup it ourselves |
| 2019 | // before proceeding to do the restore. |
| 2020 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2021 | } |
| 2022 | |
| 2023 | @Override |
| 2024 | public void run() { |
| 2025 | try { |
| 2026 | mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type, |
| 2027 | mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime, |
| 2028 | mToken, mBackupManagerBinder); |
| 2029 | } catch (RemoteException e) { |
| 2030 | // never happens; this is used strictly for local binder calls |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | @Override |
| 2036 | public void run() { |
| 2037 | Slog.i(TAG, "--- Performing full-dataset restore ---"); |
| 2038 | sendStartRestore(); |
| 2039 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2040 | // Are we able to restore shared-storage data? |
| 2041 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| 2042 | mPackagePolicies.put("com.android.sharedstoragebackup", RestorePolicy.ACCEPT); |
| 2043 | } |
| 2044 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2045 | try { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2046 | mBytes = 0; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2047 | byte[] buffer = new byte[32 * 1024]; |
| 2048 | FileInputStream instream = new FileInputStream(mInputFile.getFileDescriptor()); |
| 2049 | |
| 2050 | boolean didRestore; |
| 2051 | do { |
| 2052 | didRestore = restoreOneFile(instream, buffer); |
| 2053 | } while (didRestore); |
| 2054 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2055 | if (DEBUG) Slog.v(TAG, "Done consuming input tarfile, total bytes=" + mBytes); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2056 | } finally { |
| 2057 | tearDownPipes(); |
| 2058 | tearDownAgent(mTargetApp); |
| 2059 | |
| 2060 | try { |
| 2061 | mInputFile.close(); |
| 2062 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2063 | Slog.w(TAG, "Close of restore data pipe threw", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2064 | /* nothing we can do about this */ |
| 2065 | } |
| 2066 | synchronized (mCurrentOpLock) { |
| 2067 | mCurrentOperations.clear(); |
| 2068 | } |
| 2069 | synchronized (mLatchObject) { |
| 2070 | mLatchObject.set(true); |
| 2071 | mLatchObject.notifyAll(); |
| 2072 | } |
| 2073 | sendEndRestore(); |
| 2074 | mWakelock.release(); |
| 2075 | if (DEBUG) Slog.d(TAG, "Full restore pass complete."); |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | boolean restoreOneFile(InputStream instream, byte[] buffer) { |
| 2080 | FileMetadata info; |
| 2081 | try { |
| 2082 | info = readTarHeaders(instream); |
| 2083 | if (info != null) { |
| 2084 | if (DEBUG) { |
| 2085 | dumpFileMetadata(info); |
| 2086 | } |
| 2087 | |
| 2088 | final String pkg = info.packageName; |
| 2089 | if (!pkg.equals(mAgentPackage)) { |
| 2090 | // okay, change in package; set up our various |
| 2091 | // bookkeeping if we haven't seen it yet |
| 2092 | if (!mPackagePolicies.containsKey(pkg)) { |
| 2093 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2094 | } |
| 2095 | |
| 2096 | // Clean up the previous agent relationship if necessary, |
| 2097 | // and let the observer know we're considering a new app. |
| 2098 | if (mAgent != null) { |
| 2099 | if (DEBUG) Slog.d(TAG, "Saw new package; tearing down old one"); |
| 2100 | tearDownPipes(); |
| 2101 | tearDownAgent(mTargetApp); |
| 2102 | mTargetApp = null; |
| 2103 | mAgentPackage = null; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | if (info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 2108 | mPackagePolicies.put(pkg, readAppManifest(info, instream)); |
| 2109 | mPackageInstallers.put(pkg, info.installerPackageName); |
| 2110 | // We've read only the manifest content itself at this point, |
| 2111 | // so consume the footer before looping around to the next |
| 2112 | // input file |
| 2113 | skipTarPadding(info.size, instream); |
| 2114 | sendOnRestorePackage(pkg); |
| 2115 | } else { |
| 2116 | // Non-manifest, so it's actual file data. Is this a package |
| 2117 | // we're ignoring? |
| 2118 | boolean okay = true; |
| 2119 | RestorePolicy policy = mPackagePolicies.get(pkg); |
| 2120 | switch (policy) { |
| 2121 | case IGNORE: |
| 2122 | okay = false; |
| 2123 | break; |
| 2124 | |
| 2125 | case ACCEPT_IF_APK: |
| 2126 | // If we're in accept-if-apk state, then the first file we |
| 2127 | // see MUST be the apk. |
| 2128 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 2129 | if (DEBUG) Slog.d(TAG, "APK file; installing"); |
| 2130 | // Try to install the app. |
| 2131 | String installerName = mPackageInstallers.get(pkg); |
| 2132 | okay = installApk(info, installerName, instream); |
| 2133 | // good to go; promote to ACCEPT |
| 2134 | mPackagePolicies.put(pkg, (okay) |
| 2135 | ? RestorePolicy.ACCEPT |
| 2136 | : RestorePolicy.IGNORE); |
| 2137 | // At this point we've consumed this file entry |
| 2138 | // ourselves, so just strip the tar footer and |
| 2139 | // go on to the next file in the input stream |
| 2140 | skipTarPadding(info.size, instream); |
| 2141 | return true; |
| 2142 | } else { |
| 2143 | // File data before (or without) the apk. We can't |
| 2144 | // handle it coherently in this case so ignore it. |
| 2145 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2146 | okay = false; |
| 2147 | } |
| 2148 | break; |
| 2149 | |
| 2150 | case ACCEPT: |
| 2151 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 2152 | if (DEBUG) Slog.d(TAG, "apk present but ACCEPT"); |
| 2153 | // we can take the data without the apk, so we |
| 2154 | // *want* to do so. skip the apk by declaring this |
| 2155 | // one file not-okay without changing the restore |
| 2156 | // policy for the package. |
| 2157 | okay = false; |
| 2158 | } |
| 2159 | break; |
| 2160 | |
| 2161 | default: |
| 2162 | // Something has gone dreadfully wrong when determining |
| 2163 | // the restore policy from the manifest. Ignore the |
| 2164 | // rest of this package's data. |
| 2165 | Slog.e(TAG, "Invalid policy from manifest"); |
| 2166 | okay = false; |
| 2167 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2168 | break; |
| 2169 | } |
| 2170 | |
| 2171 | // If the policy is satisfied, go ahead and set up to pipe the |
| 2172 | // data to the agent. |
| 2173 | if (DEBUG && okay && mAgent != null) { |
| 2174 | Slog.i(TAG, "Reusing existing agent instance"); |
| 2175 | } |
| 2176 | if (okay && mAgent == null) { |
| 2177 | if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg); |
| 2178 | |
| 2179 | try { |
| 2180 | mTargetApp = mPackageManager.getApplicationInfo(pkg, 0); |
| 2181 | |
| 2182 | // If we haven't sent any data to this app yet, we probably |
| 2183 | // need to clear it first. Check that. |
| 2184 | if (!mClearedPackages.contains(pkg)) { |
| 2185 | // apps with their own full backup agents are |
| 2186 | // responsible for coherently managing a full |
| 2187 | // restore. |
| 2188 | if (mTargetApp.fullBackupAgentName == null) { |
| 2189 | if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore"); |
| 2190 | clearApplicationDataSynchronous(pkg); |
| 2191 | } else { |
| 2192 | if (DEBUG) Slog.d(TAG, "full backup agent (" |
| 2193 | + mTargetApp.fullBackupAgentName + ") => no clear"); |
| 2194 | } |
| 2195 | mClearedPackages.add(pkg); |
| 2196 | } else { |
| 2197 | if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required"); |
| 2198 | } |
| 2199 | |
| 2200 | // All set; now set up the IPC and launch the agent |
| 2201 | setUpPipes(); |
| 2202 | mAgent = bindToAgentSynchronous(mTargetApp, |
| 2203 | IApplicationThread.BACKUP_MODE_RESTORE_FULL); |
| 2204 | mAgentPackage = pkg; |
| 2205 | } catch (IOException e) { |
| 2206 | // fall through to error handling |
| 2207 | } catch (NameNotFoundException e) { |
| 2208 | // fall through to error handling |
| 2209 | } |
| 2210 | |
| 2211 | if (mAgent == null) { |
| 2212 | if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg); |
| 2213 | okay = false; |
| 2214 | tearDownPipes(); |
| 2215 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | // Sanity check: make sure we never give data to the wrong app. This |
| 2220 | // should never happen but a little paranoia here won't go amiss. |
| 2221 | if (okay && !pkg.equals(mAgentPackage)) { |
| 2222 | Slog.e(TAG, "Restoring data for " + pkg |
| 2223 | + " but agent is for " + mAgentPackage); |
| 2224 | okay = false; |
| 2225 | } |
| 2226 | |
| 2227 | // At this point we have an agent ready to handle the full |
| 2228 | // restore data as well as a pipe for sending data to |
| 2229 | // that agent. Tell the agent to start reading from the |
| 2230 | // pipe. |
| 2231 | if (okay) { |
| 2232 | boolean agentSuccess = true; |
| 2233 | long toCopy = info.size; |
| 2234 | final int token = generateToken(); |
| 2235 | try { |
| 2236 | if (DEBUG) Slog.d(TAG, "Invoking agent to restore file " |
| 2237 | + info.path); |
| 2238 | prepareOperationTimeout(token, |
| 2239 | TIMEOUT_FULL_BACKUP_INTERVAL); |
| 2240 | // fire up the app's agent listening on the socket. If |
| 2241 | // the agent is running in the system process we can't |
| 2242 | // just invoke it asynchronously, so we provide a thread |
| 2243 | // for it here. |
| 2244 | if (mTargetApp.processName.equals("system")) { |
| 2245 | Slog.d(TAG, "system process agent - spinning a thread"); |
| 2246 | RestoreFileRunnable runner = new RestoreFileRunnable( |
| 2247 | mAgent, info, mPipes[0], token); |
| 2248 | new Thread(runner).start(); |
| 2249 | } else { |
| 2250 | mAgent.doRestoreFile(mPipes[0], info.size, info.type, |
| 2251 | info.domain, info.path, info.mode, info.mtime, |
| 2252 | token, mBackupManagerBinder); |
| 2253 | } |
| 2254 | } catch (IOException e) { |
| 2255 | // couldn't dup the socket for a process-local restore |
| 2256 | Slog.d(TAG, "Couldn't establish restore"); |
| 2257 | agentSuccess = false; |
| 2258 | okay = false; |
| 2259 | } catch (RemoteException e) { |
| 2260 | // whoops, remote agent went away. We'll eat the content |
| 2261 | // ourselves, then, and not copy it over. |
| 2262 | Slog.e(TAG, "Agent crashed during full restore"); |
| 2263 | agentSuccess = false; |
| 2264 | okay = false; |
| 2265 | } |
| 2266 | |
| 2267 | // Copy over the data if the agent is still good |
| 2268 | if (okay) { |
| 2269 | boolean pipeOkay = true; |
| 2270 | FileOutputStream pipe = new FileOutputStream( |
| 2271 | mPipes[1].getFileDescriptor()); |
| 2272 | if (DEBUG) Slog.d(TAG, "Piping data to agent"); |
| 2273 | while (toCopy > 0) { |
| 2274 | int toRead = (toCopy > buffer.length) |
| 2275 | ? buffer.length : (int)toCopy; |
| 2276 | int nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2277 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2278 | if (nRead <= 0) break; |
| 2279 | toCopy -= nRead; |
| 2280 | |
| 2281 | // send it to the output pipe as long as things |
| 2282 | // are still good |
| 2283 | if (pipeOkay) { |
| 2284 | try { |
| 2285 | pipe.write(buffer, 0, nRead); |
| 2286 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2287 | Slog.e(TAG, "Failed to write to restore pipe", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2288 | pipeOkay = false; |
| 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | // done sending that file! Now we just need to consume |
| 2294 | // the delta from info.size to the end of block. |
| 2295 | skipTarPadding(info.size, instream); |
| 2296 | |
| 2297 | // and now that we've sent it all, wait for the remote |
| 2298 | // side to acknowledge receipt |
| 2299 | agentSuccess = waitUntilOperationComplete(token); |
| 2300 | } |
| 2301 | |
| 2302 | // okay, if the remote end failed at any point, deal with |
| 2303 | // it by ignoring the rest of the restore on it |
| 2304 | if (!agentSuccess) { |
| 2305 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 2306 | tearDownPipes(); |
| 2307 | tearDownAgent(mTargetApp); |
| 2308 | mAgent = null; |
| 2309 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | // Problems setting up the agent communication, or an already- |
| 2314 | // ignored package: skip to the next tar stream entry by |
| 2315 | // reading and discarding this file. |
| 2316 | if (!okay) { |
| 2317 | if (DEBUG) Slog.d(TAG, "[discarding file content]"); |
| 2318 | long bytesToConsume = (info.size + 511) & ~511; |
| 2319 | while (bytesToConsume > 0) { |
| 2320 | int toRead = (bytesToConsume > buffer.length) |
| 2321 | ? buffer.length : (int)bytesToConsume; |
| 2322 | long nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2323 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2324 | if (nRead <= 0) break; |
| 2325 | bytesToConsume -= nRead; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | } catch (IOException e) { |
| 2331 | Slog.w(TAG, "io exception on restore socket read", e); |
| 2332 | // treat as EOF |
| 2333 | info = null; |
| 2334 | } |
| 2335 | |
| 2336 | return (info != null); |
| 2337 | } |
| 2338 | |
| 2339 | void setUpPipes() throws IOException { |
| 2340 | mPipes = ParcelFileDescriptor.createPipe(); |
| 2341 | } |
| 2342 | |
| 2343 | void tearDownPipes() { |
| 2344 | if (mPipes != null) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2345 | try { |
| 2346 | mPipes[0].close(); |
| 2347 | mPipes[0] = null; |
| 2348 | mPipes[1].close(); |
| 2349 | mPipes[1] = null; |
| 2350 | } catch (IOException e) { |
| 2351 | Slog.w(TAG, "Couldn't close agent pipes", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2352 | } |
| 2353 | mPipes = null; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | void tearDownAgent(ApplicationInfo app) { |
| 2358 | if (mAgent != null) { |
| 2359 | try { |
| 2360 | // unbind and tidy up even on timeout or failure, just in case |
| 2361 | mActivityManager.unbindBackupAgent(app); |
| 2362 | |
| 2363 | // The agent was running with a stub Application object, so shut it down. |
| 2364 | // !!! We hardcode the confirmation UI's package name here rather than use a |
| 2365 | // manifest flag! TODO something less direct. |
| 2366 | if (app.uid != Process.SYSTEM_UID |
| 2367 | && !app.packageName.equals("com.android.backupconfirm")) { |
| 2368 | if (DEBUG) Slog.d(TAG, "Killing host process"); |
| 2369 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 2370 | } else { |
| 2371 | if (DEBUG) Slog.d(TAG, "Not killing after full restore"); |
| 2372 | } |
| 2373 | } catch (RemoteException e) { |
| 2374 | Slog.d(TAG, "Lost app trying to shut down"); |
| 2375 | } |
| 2376 | mAgent = null; |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | class RestoreInstallObserver extends IPackageInstallObserver.Stub { |
| 2381 | final AtomicBoolean mDone = new AtomicBoolean(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2382 | String mPackageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2383 | int mResult; |
| 2384 | |
| 2385 | public void reset() { |
| 2386 | synchronized (mDone) { |
| 2387 | mDone.set(false); |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | public void waitForCompletion() { |
| 2392 | synchronized (mDone) { |
| 2393 | while (mDone.get() == false) { |
| 2394 | try { |
| 2395 | mDone.wait(); |
| 2396 | } catch (InterruptedException e) { } |
| 2397 | } |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | int getResult() { |
| 2402 | return mResult; |
| 2403 | } |
| 2404 | |
| 2405 | @Override |
| 2406 | public void packageInstalled(String packageName, int returnCode) |
| 2407 | throws RemoteException { |
| 2408 | synchronized (mDone) { |
| 2409 | mResult = returnCode; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2410 | mPackageName = packageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2411 | mDone.set(true); |
| 2412 | mDone.notifyAll(); |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2416 | |
| 2417 | class RestoreDeleteObserver extends IPackageDeleteObserver.Stub { |
| 2418 | final AtomicBoolean mDone = new AtomicBoolean(); |
| 2419 | int mResult; |
| 2420 | |
| 2421 | public void reset() { |
| 2422 | synchronized (mDone) { |
| 2423 | mDone.set(false); |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | public void waitForCompletion() { |
| 2428 | synchronized (mDone) { |
| 2429 | while (mDone.get() == false) { |
| 2430 | try { |
| 2431 | mDone.wait(); |
| 2432 | } catch (InterruptedException e) { } |
| 2433 | } |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | @Override |
| 2438 | public void packageDeleted(String packageName, int returnCode) throws RemoteException { |
| 2439 | synchronized (mDone) { |
| 2440 | mResult = returnCode; |
| 2441 | mDone.set(true); |
| 2442 | mDone.notifyAll(); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2447 | final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2448 | final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2449 | |
| 2450 | boolean installApk(FileMetadata info, String installerPackage, InputStream instream) { |
| 2451 | boolean okay = true; |
| 2452 | |
| 2453 | if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName); |
| 2454 | |
| 2455 | // The file content is an .apk file. Copy it out to a staging location and |
| 2456 | // attempt to install it. |
| 2457 | File apkFile = new File(mDataDir, info.packageName); |
| 2458 | try { |
| 2459 | FileOutputStream apkStream = new FileOutputStream(apkFile); |
| 2460 | byte[] buffer = new byte[32 * 1024]; |
| 2461 | long size = info.size; |
| 2462 | while (size > 0) { |
| 2463 | long toRead = (buffer.length < size) ? buffer.length : size; |
| 2464 | int didRead = instream.read(buffer, 0, (int)toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2465 | if (didRead >= 0) mBytes += didRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2466 | apkStream.write(buffer, 0, didRead); |
| 2467 | size -= didRead; |
| 2468 | } |
| 2469 | apkStream.close(); |
| 2470 | |
| 2471 | // make sure the installer can read it |
| 2472 | apkFile.setReadable(true, false); |
| 2473 | |
| 2474 | // Now install it |
| 2475 | Uri packageUri = Uri.fromFile(apkFile); |
| 2476 | mInstallObserver.reset(); |
| 2477 | mPackageManager.installPackage(packageUri, mInstallObserver, |
| 2478 | PackageManager.INSTALL_REPLACE_EXISTING, installerPackage); |
| 2479 | mInstallObserver.waitForCompletion(); |
| 2480 | |
| 2481 | if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) { |
| 2482 | // The only time we continue to accept install of data even if the |
| 2483 | // apk install failed is if we had already determined that we could |
| 2484 | // accept the data regardless. |
| 2485 | if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) { |
| 2486 | okay = false; |
| 2487 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2488 | } else { |
| 2489 | // Okay, the install succeeded. Make sure it was the right app. |
| 2490 | boolean uninstall = false; |
| 2491 | if (!mInstallObserver.mPackageName.equals(info.packageName)) { |
| 2492 | Slog.w(TAG, "Restore stream claimed to include apk for " |
| 2493 | + info.packageName + " but apk was really " |
| 2494 | + mInstallObserver.mPackageName); |
| 2495 | // delete the package we just put in place; it might be fraudulent |
| 2496 | okay = false; |
| 2497 | uninstall = true; |
| 2498 | } else { |
| 2499 | try { |
| 2500 | PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName, |
| 2501 | PackageManager.GET_SIGNATURES); |
| 2502 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 2503 | Slog.w(TAG, "Restore stream contains apk of package " |
| 2504 | + info.packageName + " but it disallows backup/restore"); |
| 2505 | okay = false; |
| 2506 | } else { |
| 2507 | // So far so good -- do the signatures match the manifest? |
| 2508 | Signature[] sigs = mManifestSignatures.get(info.packageName); |
| 2509 | if (!signaturesMatch(sigs, pkg)) { |
| 2510 | Slog.w(TAG, "Installed app " + info.packageName |
| 2511 | + " signatures do not match restore manifest"); |
| 2512 | okay = false; |
| 2513 | uninstall = true; |
| 2514 | } |
| 2515 | } |
| 2516 | } catch (NameNotFoundException e) { |
| 2517 | Slog.w(TAG, "Install of package " + info.packageName |
| 2518 | + " succeeded but now not found"); |
| 2519 | okay = false; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | // If we're not okay at this point, we need to delete the package |
| 2524 | // that we just installed. |
| 2525 | if (uninstall) { |
| 2526 | mDeleteObserver.reset(); |
| 2527 | mPackageManager.deletePackage(mInstallObserver.mPackageName, |
| 2528 | mDeleteObserver, 0); |
| 2529 | mDeleteObserver.waitForCompletion(); |
| 2530 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2531 | } |
| 2532 | } catch (IOException e) { |
| 2533 | Slog.e(TAG, "Unable to transcribe restored apk for install"); |
| 2534 | okay = false; |
| 2535 | } finally { |
| 2536 | apkFile.delete(); |
| 2537 | } |
| 2538 | |
| 2539 | return okay; |
| 2540 | } |
| 2541 | |
| 2542 | // Given an actual file content size, consume the post-content padding mandated |
| 2543 | // by the tar format. |
| 2544 | void skipTarPadding(long size, InputStream instream) throws IOException { |
| 2545 | long partial = (size + 512) % 512; |
| 2546 | if (partial > 0) { |
| 2547 | byte[] buffer = new byte[512]; |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2548 | int nRead = instream.read(buffer, 0, 512 - (int)partial); |
| 2549 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | // Returns a policy constant; takes a buffer arg to reduce memory churn |
| 2554 | RestorePolicy readAppManifest(FileMetadata info, InputStream instream) |
| 2555 | throws IOException { |
| 2556 | // Fail on suspiciously large manifest files |
| 2557 | if (info.size > 64 * 1024) { |
| 2558 | throw new IOException("Restore manifest too big; corrupt? size=" + info.size); |
| 2559 | } |
| 2560 | byte[] buffer = new byte[(int) info.size]; |
| 2561 | int nRead = 0; |
| 2562 | while (nRead < info.size) { |
| 2563 | nRead += instream.read(buffer, nRead, (int)info.size - nRead); |
| 2564 | } |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2565 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2566 | |
| 2567 | RestorePolicy policy = RestorePolicy.IGNORE; |
| 2568 | String[] str = new String[1]; |
| 2569 | int offset = 0; |
| 2570 | |
| 2571 | try { |
| 2572 | offset = extractLine(buffer, offset, str); |
| 2573 | int version = Integer.parseInt(str[0]); |
| 2574 | if (version == BACKUP_MANIFEST_VERSION) { |
| 2575 | offset = extractLine(buffer, offset, str); |
| 2576 | String manifestPackage = str[0]; |
| 2577 | // TODO: handle <original-package> |
| 2578 | if (manifestPackage.equals(info.packageName)) { |
| 2579 | offset = extractLine(buffer, offset, str); |
| 2580 | version = Integer.parseInt(str[0]); // app version |
| 2581 | offset = extractLine(buffer, offset, str); |
| 2582 | int platformVersion = Integer.parseInt(str[0]); |
| 2583 | offset = extractLine(buffer, offset, str); |
| 2584 | info.installerPackageName = (str[0].length() > 0) ? str[0] : null; |
| 2585 | offset = extractLine(buffer, offset, str); |
| 2586 | boolean hasApk = str[0].equals("1"); |
| 2587 | offset = extractLine(buffer, offset, str); |
| 2588 | int numSigs = Integer.parseInt(str[0]); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2589 | if (numSigs > 0) { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2590 | Signature[] sigs = new Signature[numSigs]; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2591 | for (int i = 0; i < numSigs; i++) { |
| 2592 | offset = extractLine(buffer, offset, str); |
| 2593 | sigs[i] = new Signature(str[0]); |
| 2594 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2595 | mManifestSignatures.put(info.packageName, sigs); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2596 | |
| 2597 | // Okay, got the manifest info we need... |
| 2598 | try { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2599 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2600 | info.packageName, PackageManager.GET_SIGNATURES); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2601 | // Fall through to IGNORE if the app explicitly disallows backup |
| 2602 | final int flags = pkgInfo.applicationInfo.flags; |
| 2603 | if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) { |
| 2604 | // Verify signatures against any installed version; if they |
| 2605 | // don't match, then we fall though and ignore the data. The |
| 2606 | // signatureMatch() method explicitly ignores the signature |
| 2607 | // check for packages installed on the system partition, because |
| 2608 | // such packages are signed with the platform cert instead of |
| 2609 | // the app developer's cert, so they're different on every |
| 2610 | // device. |
| 2611 | if (signaturesMatch(sigs, pkgInfo)) { |
| 2612 | if (pkgInfo.versionCode >= version) { |
| 2613 | Slog.i(TAG, "Sig + version match; taking data"); |
| 2614 | policy = RestorePolicy.ACCEPT; |
| 2615 | } else { |
| 2616 | // The data is from a newer version of the app than |
| 2617 | // is presently installed. That means we can only |
| 2618 | // use it if the matching apk is also supplied. |
| 2619 | Slog.d(TAG, "Data version " + version |
| 2620 | + " is newer than installed version " |
| 2621 | + pkgInfo.versionCode + " - requiring apk"); |
| 2622 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 2623 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2624 | } else { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2625 | Slog.w(TAG, "Restore manifest signatures do not match " |
| 2626 | + "installed application for " + info.packageName); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2627 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2628 | } else { |
| 2629 | if (DEBUG) Slog.i(TAG, "Restore manifest from " |
| 2630 | + info.packageName + " but allowBackup=false"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2631 | } |
| 2632 | } catch (NameNotFoundException e) { |
| 2633 | // Okay, the target app isn't installed. We can process |
| 2634 | // the restore properly only if the dataset provides the |
| 2635 | // apk file and we can successfully install it. |
| 2636 | if (DEBUG) Slog.i(TAG, "Package " + info.packageName |
| 2637 | + " not installed; requiring apk in dataset"); |
| 2638 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 2639 | } |
| 2640 | |
| 2641 | if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) { |
| 2642 | Slog.i(TAG, "Cannot restore package " + info.packageName |
| 2643 | + " without the matching .apk"); |
| 2644 | } |
| 2645 | } else { |
| 2646 | Slog.i(TAG, "Missing signature on backed-up package " |
| 2647 | + info.packageName); |
| 2648 | } |
| 2649 | } else { |
| 2650 | Slog.i(TAG, "Expected package " + info.packageName |
| 2651 | + " but restore manifest claims " + manifestPackage); |
| 2652 | } |
| 2653 | } else { |
| 2654 | Slog.i(TAG, "Unknown restore manifest version " + version |
| 2655 | + " for package " + info.packageName); |
| 2656 | } |
| 2657 | } catch (NumberFormatException e) { |
| 2658 | Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName); |
| 2659 | } |
| 2660 | |
| 2661 | return policy; |
| 2662 | } |
| 2663 | |
| 2664 | // Builds a line from a byte buffer starting at 'offset', and returns |
| 2665 | // the index of the next unconsumed data in the buffer. |
| 2666 | int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException { |
| 2667 | final int end = buffer.length; |
| 2668 | if (offset >= end) throw new IOException("Incomplete data"); |
| 2669 | |
| 2670 | int pos; |
| 2671 | for (pos = offset; pos < end; pos++) { |
| 2672 | byte c = buffer[pos]; |
| 2673 | // at LF we declare end of line, and return the next char as the |
| 2674 | // starting point for the next time through |
| 2675 | if (c == '\n') { |
| 2676 | break; |
| 2677 | } |
| 2678 | } |
| 2679 | outStr[0] = new String(buffer, offset, pos - offset); |
| 2680 | pos++; // may be pointing an extra byte past the end but that's okay |
| 2681 | return pos; |
| 2682 | } |
| 2683 | |
| 2684 | void dumpFileMetadata(FileMetadata info) { |
| 2685 | if (DEBUG) { |
| 2686 | StringBuilder b = new StringBuilder(128); |
| 2687 | |
| 2688 | // mode string |
| 2689 | b.append((info.type == FullBackup.TYPE_DIRECTORY) ? 'd' : '-'); |
| 2690 | b.append(((info.mode & 0400) != 0) ? 'r' : '-'); |
| 2691 | b.append(((info.mode & 0200) != 0) ? 'w' : '-'); |
| 2692 | b.append(((info.mode & 0100) != 0) ? 'x' : '-'); |
| 2693 | b.append(((info.mode & 0040) != 0) ? 'r' : '-'); |
| 2694 | b.append(((info.mode & 0020) != 0) ? 'w' : '-'); |
| 2695 | b.append(((info.mode & 0010) != 0) ? 'x' : '-'); |
| 2696 | b.append(((info.mode & 0004) != 0) ? 'r' : '-'); |
| 2697 | b.append(((info.mode & 0002) != 0) ? 'w' : '-'); |
| 2698 | b.append(((info.mode & 0001) != 0) ? 'x' : '-'); |
| 2699 | b.append(String.format(" %9d ", info.size)); |
| 2700 | |
| 2701 | Date stamp = new Date(info.mtime); |
| 2702 | b.append(new SimpleDateFormat("MMM dd kk:mm:ss ").format(stamp)); |
| 2703 | |
| 2704 | b.append(info.packageName); |
| 2705 | b.append(" :: "); |
| 2706 | b.append(info.domain); |
| 2707 | b.append(" :: "); |
| 2708 | b.append(info.path); |
| 2709 | |
| 2710 | Slog.i(TAG, b.toString()); |
| 2711 | } |
| 2712 | } |
| 2713 | // Consume a tar file header block [sequence] and accumulate the relevant metadata |
| 2714 | FileMetadata readTarHeaders(InputStream instream) throws IOException { |
| 2715 | byte[] block = new byte[512]; |
| 2716 | FileMetadata info = null; |
| 2717 | |
| 2718 | boolean gotHeader = readTarHeader(instream, block); |
| 2719 | if (gotHeader) { |
| 2720 | // okay, presume we're okay, and extract the various metadata |
| 2721 | info = new FileMetadata(); |
| 2722 | info.size = extractRadix(block, 124, 12, 8); |
| 2723 | info.mtime = extractRadix(block, 136, 12, 8); |
| 2724 | info.mode = extractRadix(block, 100, 8, 8); |
| 2725 | |
| 2726 | info.path = extractString(block, 345, 155); // prefix |
| 2727 | String path = extractString(block, 0, 100); |
| 2728 | if (path.length() > 0) { |
| 2729 | if (info.path.length() > 0) info.path += '/'; |
| 2730 | info.path += path; |
| 2731 | } |
| 2732 | |
| 2733 | // tar link indicator field: 1 byte at offset 156 in the header. |
| 2734 | int typeChar = block[156]; |
| 2735 | if (typeChar == 'x') { |
| 2736 | // pax extended header, so we need to read that |
| 2737 | gotHeader = readPaxExtendedHeader(instream, info); |
| 2738 | if (gotHeader) { |
| 2739 | // and after a pax extended header comes another real header -- read |
| 2740 | // that to find the real file type |
| 2741 | gotHeader = readTarHeader(instream, block); |
| 2742 | } |
| 2743 | if (!gotHeader) throw new IOException("Bad or missing pax header"); |
| 2744 | |
| 2745 | typeChar = block[156]; |
| 2746 | } |
| 2747 | |
| 2748 | switch (typeChar) { |
| 2749 | case '0': info.type = FullBackup.TYPE_FILE; break; |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2750 | case '5': { |
| 2751 | info.type = FullBackup.TYPE_DIRECTORY; |
| 2752 | if (info.size != 0) { |
| 2753 | Slog.w(TAG, "Directory entry with nonzero size in header"); |
| 2754 | info.size = 0; |
| 2755 | } |
| 2756 | break; |
| 2757 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2758 | case 0: { |
| 2759 | // presume EOF |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2760 | if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2761 | return null; |
| 2762 | } |
| 2763 | default: { |
| 2764 | Slog.e(TAG, "Unknown tar entity type: " + typeChar); |
| 2765 | throw new IOException("Unknown entity type " + typeChar); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | // Parse out the path |
| 2770 | // |
| 2771 | // first: apps/shared/unrecognized |
| 2772 | if (FullBackup.SHARED_PREFIX.regionMatches(0, |
| 2773 | info.path, 0, FullBackup.SHARED_PREFIX.length())) { |
| 2774 | // File in shared storage. !!! TODO: implement this. |
| 2775 | info.path = info.path.substring(FullBackup.SHARED_PREFIX.length()); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2776 | info.packageName = "com.android.sharedstoragebackup"; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2777 | info.domain = FullBackup.SHARED_STORAGE_TOKEN; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2778 | if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2779 | } else if (FullBackup.APPS_PREFIX.regionMatches(0, |
| 2780 | info.path, 0, FullBackup.APPS_PREFIX.length())) { |
| 2781 | // App content! Parse out the package name and domain |
| 2782 | |
| 2783 | // strip the apps/ prefix |
| 2784 | info.path = info.path.substring(FullBackup.APPS_PREFIX.length()); |
| 2785 | |
| 2786 | // extract the package name |
| 2787 | int slash = info.path.indexOf('/'); |
| 2788 | if (slash < 0) throw new IOException("Illegal semantic path in " + info.path); |
| 2789 | info.packageName = info.path.substring(0, slash); |
| 2790 | info.path = info.path.substring(slash+1); |
| 2791 | |
| 2792 | // if it's a manifest we're done, otherwise parse out the domains |
| 2793 | if (!info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 2794 | slash = info.path.indexOf('/'); |
| 2795 | if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path); |
| 2796 | info.domain = info.path.substring(0, slash); |
| 2797 | // validate that it's one of the domains we understand |
| 2798 | if (!info.domain.equals(FullBackup.APK_TREE_TOKEN) |
| 2799 | && !info.domain.equals(FullBackup.DATA_TREE_TOKEN) |
| 2800 | && !info.domain.equals(FullBackup.DATABASE_TREE_TOKEN) |
| 2801 | && !info.domain.equals(FullBackup.ROOT_TREE_TOKEN) |
| 2802 | && !info.domain.equals(FullBackup.SHAREDPREFS_TREE_TOKEN) |
| 2803 | && !info.domain.equals(FullBackup.OBB_TREE_TOKEN) |
| 2804 | && !info.domain.equals(FullBackup.CACHE_TREE_TOKEN)) { |
| 2805 | throw new IOException("Unrecognized domain " + info.domain); |
| 2806 | } |
| 2807 | |
| 2808 | info.path = info.path.substring(slash + 1); |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | return info; |
| 2813 | } |
| 2814 | |
| 2815 | boolean readTarHeader(InputStream instream, byte[] block) throws IOException { |
| 2816 | int nRead = instream.read(block, 0, 512); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2817 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2818 | if (nRead > 0 && nRead != 512) { |
| 2819 | // if we read only a partial block, then things are |
| 2820 | // clearly screwed up. terminate the restore. |
| 2821 | throw new IOException("Partial header block: " + nRead); |
| 2822 | } |
| 2823 | return (nRead > 0); |
| 2824 | } |
| 2825 | |
| 2826 | // overwrites 'info' fields based on the pax extended header |
| 2827 | boolean readPaxExtendedHeader(InputStream instream, FileMetadata info) |
| 2828 | throws IOException { |
| 2829 | // We should never see a pax extended header larger than this |
| 2830 | if (info.size > 32*1024) { |
| 2831 | Slog.w(TAG, "Suspiciously large pax header size " + info.size |
| 2832 | + " - aborting"); |
| 2833 | throw new IOException("Sanity failure: pax header size " + info.size); |
| 2834 | } |
| 2835 | |
| 2836 | // read whole blocks, not just the content size |
| 2837 | int numBlocks = (int)((info.size + 511) >> 9); |
| 2838 | byte[] data = new byte[numBlocks * 512]; |
| 2839 | int nRead = instream.read(data); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2840 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2841 | if (nRead != data.length) { |
| 2842 | return false; |
| 2843 | } |
| 2844 | |
| 2845 | final int contentSize = (int) info.size; |
| 2846 | int offset = 0; |
| 2847 | do { |
| 2848 | // extract the line at 'offset' |
| 2849 | int eol = offset+1; |
| 2850 | while (eol < contentSize && data[eol] != ' ') eol++; |
| 2851 | if (eol >= contentSize) { |
| 2852 | // error: we just hit EOD looking for the end of the size field |
| 2853 | throw new IOException("Invalid pax data"); |
| 2854 | } |
| 2855 | // eol points to the space between the count and the key |
| 2856 | int linelen = (int) extractRadix(data, offset, eol - offset, 10); |
| 2857 | int key = eol + 1; // start of key=value |
| 2858 | eol = offset + linelen - 1; // trailing LF |
| 2859 | int value; |
| 2860 | for (value = key+1; data[value] != '=' && value <= eol; value++); |
| 2861 | if (value > eol) { |
| 2862 | throw new IOException("Invalid pax declaration"); |
| 2863 | } |
| 2864 | |
| 2865 | // pax requires that key/value strings be in UTF-8 |
| 2866 | String keyStr = new String(data, key, value-key, "UTF-8"); |
| 2867 | // -1 to strip the trailing LF |
| 2868 | String valStr = new String(data, value+1, eol-value-1, "UTF-8"); |
| 2869 | |
| 2870 | if ("path".equals(keyStr)) { |
| 2871 | info.path = valStr; |
| 2872 | } else if ("size".equals(keyStr)) { |
| 2873 | info.size = Long.parseLong(valStr); |
| 2874 | } else { |
| 2875 | if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key); |
| 2876 | } |
| 2877 | |
| 2878 | offset += linelen; |
| 2879 | } while (offset < contentSize); |
| 2880 | |
| 2881 | return true; |
| 2882 | } |
| 2883 | |
| 2884 | long extractRadix(byte[] data, int offset, int maxChars, int radix) |
| 2885 | throws IOException { |
| 2886 | long value = 0; |
| 2887 | final int end = offset + maxChars; |
| 2888 | for (int i = offset; i < end; i++) { |
| 2889 | final byte b = data[i]; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 2890 | // Numeric fields in tar can terminate with either NUL or SPC |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2891 | if (b == 0 || b == ' ') break; |
| 2892 | if (b < '0' || b > ('0' + radix - 1)) { |
| 2893 | throw new IOException("Invalid number in header"); |
| 2894 | } |
| 2895 | value = radix * value + (b - '0'); |
| 2896 | } |
| 2897 | return value; |
| 2898 | } |
| 2899 | |
| 2900 | String extractString(byte[] data, int offset, int maxChars) throws IOException { |
| 2901 | final int end = offset + maxChars; |
| 2902 | int eos = offset; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 2903 | // tar string fields terminate early with a NUL |
| 2904 | while (eos < end && data[eos] != 0) eos++; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2905 | return new String(data, offset, eos-offset, "US-ASCII"); |
| 2906 | } |
| 2907 | |
| 2908 | void sendStartRestore() { |
| 2909 | if (mObserver != null) { |
| 2910 | try { |
| 2911 | mObserver.onStartRestore(); |
| 2912 | } catch (RemoteException e) { |
| 2913 | Slog.w(TAG, "full restore observer went away: startRestore"); |
| 2914 | mObserver = null; |
| 2915 | } |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | void sendOnRestorePackage(String name) { |
| 2920 | if (mObserver != null) { |
| 2921 | try { |
| 2922 | // TODO: use a more user-friendly name string |
| 2923 | mObserver.onRestorePackage(name); |
| 2924 | } catch (RemoteException e) { |
| 2925 | Slog.w(TAG, "full restore observer went away: restorePackage"); |
| 2926 | mObserver = null; |
| 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | void sendEndRestore() { |
| 2932 | if (mObserver != null) { |
| 2933 | try { |
| 2934 | mObserver.onEndRestore(); |
| 2935 | } catch (RemoteException e) { |
| 2936 | Slog.w(TAG, "full restore observer went away: endRestore"); |
| 2937 | mObserver = null; |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2943 | // ----- Restore handling ----- |
| 2944 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 2945 | private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) { |
| 2946 | // If the target resides on the system partition, we allow it to restore |
| 2947 | // data from the like-named package in a restore set even if the signatures |
| 2948 | // do not match. (Unlike general applications, those flashed to the system |
| 2949 | // partition will be signed with the device's platform certificate, so on |
| 2950 | // different phones the same system app will have different signatures.) |
| 2951 | if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2952 | if (DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check"); |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 2953 | return true; |
| 2954 | } |
| 2955 | |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 2956 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 2957 | // !!! TODO: is this the right policy? |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 2958 | Signature[] deviceSigs = target.signatures; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2959 | if (DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 2960 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 2961 | if ((storedSigs == null || storedSigs.length == 0) |
| 2962 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 2963 | return true; |
| 2964 | } |
| 2965 | if (storedSigs == null || deviceSigs == null) { |
| 2966 | return false; |
| 2967 | } |
| 2968 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 2969 | // !!! TODO: this demands that every stored signature match one |
| 2970 | // that is present on device, and does not demand the converse. |
| 2971 | // Is this this right policy? |
| 2972 | int nStored = storedSigs.length; |
| 2973 | int nDevice = deviceSigs.length; |
| 2974 | |
| 2975 | for (int i=0; i < nStored; i++) { |
| 2976 | boolean match = false; |
| 2977 | for (int j=0; j < nDevice; j++) { |
| 2978 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 2979 | match = true; |
| 2980 | break; |
| 2981 | } |
| 2982 | } |
| 2983 | if (!match) { |
| 2984 | return false; |
| 2985 | } |
| 2986 | } |
| 2987 | return true; |
| 2988 | } |
| 2989 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 2990 | class PerformRestoreTask implements Runnable { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2991 | private IBackupTransport mTransport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 2992 | private IRestoreObserver mObserver; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 2993 | private long mToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 2994 | private PackageInfo mTargetPackage; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 2995 | private File mStateDir; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 2996 | private int mPmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 2997 | private boolean mNeedFullBackup; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2998 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 2999 | class RestoreRequest { |
| 3000 | public PackageInfo app; |
| 3001 | public int storedAppVersion; |
| 3002 | |
| 3003 | RestoreRequest(PackageInfo _app, int _version) { |
| 3004 | app = _app; |
| 3005 | storedAppVersion = _version; |
| 3006 | } |
| 3007 | } |
| 3008 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3009 | PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3010 | long restoreSetToken, PackageInfo targetPackage, int pmToken, |
| 3011 | boolean needFullBackup) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3012 | mTransport = transport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3013 | mObserver = observer; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 3014 | mToken = restoreSetToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3015 | mTargetPackage = targetPackage; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3016 | mPmToken = pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3017 | mNeedFullBackup = needFullBackup; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3018 | |
| 3019 | try { |
| 3020 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 3021 | } catch (RemoteException e) { |
| 3022 | // can't happen; the transport is local |
| 3023 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3026 | public void run() { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3027 | long startRealtime = SystemClock.elapsedRealtime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3028 | if (DEBUG) Slog.v(TAG, "Beginning restore process mTransport=" + mTransport |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3029 | + " mObserver=" + mObserver + " mToken=" + Long.toHexString(mToken) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3030 | + " mTargetPackage=" + mTargetPackage + " mPmToken=" + mPmToken); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3031 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3032 | PackageManagerBackupAgent pmAgent = null; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3033 | int error = -1; // assume error |
| 3034 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3035 | // build the set of apps to restore |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3036 | try { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3037 | // TODO: Log this before getAvailableRestoreSets, somehow |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3038 | EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 3039 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3040 | // Get the list of all packages which have backup enabled. |
| 3041 | // (Include the Package Manager metadata pseudo-package first.) |
| 3042 | ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>(); |
| 3043 | PackageInfo omPackage = new PackageInfo(); |
| 3044 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 3045 | restorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3046 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3047 | List<PackageInfo> agentPackages = allAgentPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3048 | if (mTargetPackage == null) { |
| 3049 | restorePackages.addAll(agentPackages); |
| 3050 | } else { |
| 3051 | // Just one package to attempt restore of |
| 3052 | restorePackages.add(mTargetPackage); |
| 3053 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3054 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3055 | // let the observer know that we're running |
| 3056 | if (mObserver != null) { |
| 3057 | try { |
| 3058 | // !!! TODO: get an actual count from the transport after |
| 3059 | // its startRestore() runs? |
| 3060 | mObserver.restoreStarting(restorePackages.size()); |
| 3061 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3062 | Slog.d(TAG, "Restore observer died at restoreStarting"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3063 | mObserver = null; |
| 3064 | } |
| 3065 | } |
| 3066 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 3067 | if (mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0])) != |
| 3068 | BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3069 | Slog.e(TAG, "Error starting restore operation"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3070 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3071 | return; |
| 3072 | } |
| 3073 | |
| 3074 | String packageName = mTransport.nextRestorePackage(); |
| 3075 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3076 | Slog.e(TAG, "Error getting first restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3077 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3078 | return; |
| 3079 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3080 | Slog.i(TAG, "No restore data available"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3081 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3082 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3083 | return; |
| 3084 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3085 | Slog.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3086 | + "\", found only \"" + packageName + "\""); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3087 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3088 | "Package manager data missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3089 | return; |
| 3090 | } |
| 3091 | |
| 3092 | // Pull the Package Manager metadata from the restore set first |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3093 | pmAgent = new PackageManagerBackupAgent( |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3094 | mPackageManager, agentPackages); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3095 | processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()), |
| 3096 | mNeedFullBackup); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3097 | |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 3098 | // Verify that the backup set includes metadata. If not, we can't do |
| 3099 | // signature/version verification etc, so we simply do not proceed with |
| 3100 | // the restore operation. |
| Christopher Tate | 3d7cd13 | 2009-07-07 14:23:07 -0700 | [diff] [blame] | 3101 | if (!pmAgent.hasMetadata()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3102 | Slog.e(TAG, "No restore metadata available, so not restoring settings"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3103 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3104 | "Package manager restore metadata missing"); |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 3105 | return; |
| 3106 | } |
| 3107 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3108 | int count = 0; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3109 | for (;;) { |
| 3110 | packageName = mTransport.nextRestorePackage(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3111 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3112 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3113 | Slog.e(TAG, "Error getting next restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3114 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3115 | return; |
| 3116 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3117 | if (DEBUG) Slog.v(TAG, "No next package, finishing restore"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3118 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3119 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3120 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3121 | if (mObserver != null) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3122 | try { |
| Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 3123 | mObserver.onUpdate(count, packageName); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3124 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3125 | Slog.d(TAG, "Restore observer died in onUpdate"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3126 | mObserver = null; |
| 3127 | } |
| 3128 | } |
| 3129 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3130 | Metadata metaInfo = pmAgent.getRestoredMetadata(packageName); |
| 3131 | if (metaInfo == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3132 | Slog.e(TAG, "Missing metadata for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3133 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3134 | "Package metadata missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3135 | continue; |
| 3136 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3137 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3138 | PackageInfo packageInfo; |
| 3139 | try { |
| 3140 | int flags = PackageManager.GET_SIGNATURES; |
| 3141 | packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 3142 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3143 | Slog.e(TAG, "Invalid package restoring data", e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3144 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3145 | "Package missing on device"); |
| 3146 | continue; |
| 3147 | } |
| 3148 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3149 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3150 | // Data is from a "newer" version of the app than we have currently |
| 3151 | // installed. If the app has not declared that it is prepared to |
| 3152 | // handle this case, we do not attempt the restore. |
| 3153 | if ((packageInfo.applicationInfo.flags |
| 3154 | & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) { |
| 3155 | String message = "Version " + metaInfo.versionCode |
| 3156 | + " > installed version " + packageInfo.versionCode; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3157 | Slog.w(TAG, "Package " + packageName + ": " + message); |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3158 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 3159 | packageName, message); |
| 3160 | continue; |
| 3161 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3162 | if (DEBUG) Slog.v(TAG, "Version " + metaInfo.versionCode |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3163 | + " > installed " + packageInfo.versionCode |
| 3164 | + " but restoreAnyVersion"); |
| 3165 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3166 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3167 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3168 | if (!signaturesMatch(metaInfo.signatures, packageInfo)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3169 | Slog.w(TAG, "Signature mismatch restoring " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3170 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3171 | "Signature mismatch"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3172 | continue; |
| 3173 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3174 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3175 | if (DEBUG) Slog.v(TAG, "Package " + packageName |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3176 | + " restore version [" + metaInfo.versionCode |
| 3177 | + "] is compatible with installed version [" |
| 3178 | + packageInfo.versionCode + "]"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3179 | |
| Christopher Tate | 3de55bc | 2010-03-12 17:28:08 -0800 | [diff] [blame] | 3180 | // Then set up and bind the agent |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3181 | IBackupAgent agent = bindToAgentSynchronous( |
| 3182 | packageInfo.applicationInfo, |
| Christopher Tate | 3de55bc | 2010-03-12 17:28:08 -0800 | [diff] [blame] | 3183 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3184 | if (agent == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3185 | Slog.w(TAG, "Can't find backup agent for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3186 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3187 | "Restore agent missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3188 | continue; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3191 | // And then finally run the restore on this agent |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3192 | try { |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3193 | processOneRestore(packageInfo, metaInfo.versionCode, agent, |
| 3194 | mNeedFullBackup); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3195 | ++count; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3196 | } finally { |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3197 | // unbind and tidy up even on timeout or failure, just in case |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3198 | mActivityManager.unbindBackupAgent(packageInfo.applicationInfo); |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3199 | |
| 3200 | // The agent was probably running with a stub Application object, |
| 3201 | // which isn't a valid run mode for the main app logic. Shut |
| 3202 | // 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] | 3203 | // usual full initialization. Note that this is only done for |
| 3204 | // full-system restores: when a single app has requested a restore, |
| 3205 | // it is explicitly not killed following that operation. |
| 3206 | if (mTargetPackage == null && (packageInfo.applicationInfo.flags |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3207 | & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3208 | if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of " |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3209 | + packageInfo.applicationInfo.processName); |
| 3210 | mActivityManager.killApplicationProcess( |
| 3211 | packageInfo.applicationInfo.processName, |
| 3212 | packageInfo.applicationInfo.uid); |
| 3213 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3214 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3215 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3216 | |
| 3217 | // if we get this far, report success to the observer |
| 3218 | error = 0; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3219 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3220 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, count, millis); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3221 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3222 | Slog.e(TAG, "Error in restore thread", e); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3223 | } finally { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3224 | if (DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3225 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3226 | try { |
| 3227 | mTransport.finishRestore(); |
| 3228 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3229 | Slog.e(TAG, "Error finishing restore", e); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3230 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3231 | |
| 3232 | if (mObserver != null) { |
| 3233 | try { |
| 3234 | mObserver.restoreFinished(error); |
| 3235 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3236 | Slog.d(TAG, "Restore observer died at restoreFinished"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3237 | } |
| 3238 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3239 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3240 | // If this was a restoreAll operation, record that this was our |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3241 | // ancestral dataset, as well as the set of apps that are possibly |
| 3242 | // restoreable from the dataset |
| 3243 | if (mTargetPackage == null && pmAgent != null) { |
| 3244 | mAncestralPackages = pmAgent.getRestoredPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3245 | mAncestralToken = mToken; |
| 3246 | writeRestoreTokens(); |
| 3247 | } |
| 3248 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3249 | // We must under all circumstances tell the Package Manager to |
| 3250 | // proceed with install notifications if it's waiting for us. |
| 3251 | if (mPmToken > 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3252 | if (DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3253 | try { |
| 3254 | mPackageManagerBinder.finishPackageInstall(mPmToken); |
| 3255 | } catch (RemoteException e) { /* can't happen */ } |
| 3256 | } |
| 3257 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 3258 | // Furthermore we need to reset the session timeout clock |
| 3259 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 3260 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, |
| 3261 | TIMEOUT_RESTORE_INTERVAL); |
| 3262 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3263 | // done; we can finally release the wakelock |
| 3264 | mWakelock.release(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3268 | // Do the guts of a restore of one application, using mTransport.getRestoreData(). |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3269 | void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent, |
| 3270 | boolean needFullBackup) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3271 | // !!! TODO: actually run the restore through mTransport |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3272 | final String packageName = app.packageName; |
| 3273 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3274 | if (DEBUG) Slog.d(TAG, "processOneRestore packageName=" + packageName); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 3275 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3276 | // !!! TODO: get the dirs from the transport |
| 3277 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3278 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 3279 | File savedStateName = new File(mStateDir, packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3280 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3281 | ParcelFileDescriptor backupData = null; |
| 3282 | ParcelFileDescriptor newState = null; |
| 3283 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3284 | final int token = generateToken(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3285 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3286 | // Run the transport's restore pass |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3287 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 3288 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 3289 | ParcelFileDescriptor.MODE_CREATE | |
| 3290 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 3291 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 3292 | if (mTransport.getRestoreData(backupData) != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3293 | Slog.e(TAG, "Error getting restore data for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3294 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3295 | return; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | // Okay, we have the data. Now have the agent do the restore. |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3299 | backupData.close(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3300 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 3301 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 3302 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3303 | newState = ParcelFileDescriptor.open(newStateName, |
| 3304 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 3305 | ParcelFileDescriptor.MODE_CREATE | |
| 3306 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 3307 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3308 | // Kick off the restore, checking for hung agents |
| 3309 | prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL); |
| 3310 | agent.doRestore(backupData, appVersionCode, newState, token, mBackupManagerBinder); |
| 3311 | boolean success = waitUntilOperationComplete(token); |
| 3312 | |
| 3313 | if (!success) { |
| 3314 | throw new RuntimeException("restore timeout"); |
| 3315 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3316 | |
| 3317 | // if everything went okay, remember the recorded state now |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 3318 | // |
| 3319 | // !!! TODO: the restored data should be migrated on the server |
| 3320 | // side into the current dataset. In that case the new state file |
| 3321 | // we just created would reflect the data already extant in the |
| 3322 | // backend, so there'd be nothing more to do. Until that happens, |
| 3323 | // however, we need to make sure that we record the data to the |
| 3324 | // current backend dataset. (Yes, this means shipping the data over |
| 3325 | // the wire in both directions. That's bad, but consistency comes |
| 3326 | // first, then efficiency.) Once we introduce server-side data |
| 3327 | // migration to the newly-restored device's dataset, we will change |
| 3328 | // the following from a discard of the newly-written state to the |
| 3329 | // "correct" operation of renaming into the canonical state blob. |
| 3330 | newStateName.delete(); // TODO: remove; see above comment |
| 3331 | //newStateName.renameTo(savedStateName); // TODO: replace with this |
| 3332 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3333 | int size = (int) backupDataName.length(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3334 | EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, packageName, size); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3335 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3336 | Slog.e(TAG, "Error restoring data for " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3337 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3338 | |
| Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 3339 | // If the agent fails restore, it might have put the app's data |
| 3340 | // into an incoherent state. For consistency we wipe its data |
| 3341 | // again in this case before propagating the exception |
| Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 3342 | clearApplicationDataSynchronous(packageName); |
| Christopher Tate | 1531dc8 | 2009-07-24 16:37:43 -0700 | [diff] [blame] | 3343 | } finally { |
| 3344 | backupDataName.delete(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3345 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 3346 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 3347 | backupData = newState = null; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3348 | synchronized (mCurrentOperations) { |
| 3349 | mCurrentOperations.delete(token); |
| 3350 | } |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3351 | |
| 3352 | // If we know a priori that we'll need to perform a full post-restore backup |
| 3353 | // pass, clear the new state file data. This means we're discarding work that |
| 3354 | // was just done by the app's agent, but this way the agent doesn't need to |
| 3355 | // take any special action based on global device state. |
| 3356 | if (needFullBackup) { |
| 3357 | newStateName.delete(); |
| 3358 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3359 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3363 | class PerformClearTask implements Runnable { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3364 | IBackupTransport mTransport; |
| 3365 | PackageInfo mPackage; |
| 3366 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3367 | PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3368 | mTransport = transport; |
| 3369 | mPackage = packageInfo; |
| 3370 | } |
| 3371 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3372 | public void run() { |
| 3373 | try { |
| 3374 | // Clear the on-device backup state to ensure a full backup next time |
| 3375 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 3376 | File stateFile = new File(stateDir, mPackage.packageName); |
| 3377 | stateFile.delete(); |
| 3378 | |
| 3379 | // Tell the transport to remove all the persistent storage for the app |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 3380 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3381 | mTransport.clearBackupData(mPackage); |
| 3382 | } catch (RemoteException e) { |
| 3383 | // can't happen; the transport is local |
| 3384 | } finally { |
| 3385 | try { |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 3386 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3387 | mTransport.finishBackup(); |
| 3388 | } catch (RemoteException e) { |
| 3389 | // can't happen; the transport is local |
| 3390 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3391 | |
| 3392 | // Last but not least, release the cpu |
| 3393 | mWakelock.release(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | } |
| 3397 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3398 | class PerformInitializeTask implements Runnable { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3399 | HashSet<String> mQueue; |
| 3400 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3401 | PerformInitializeTask(HashSet<String> transportNames) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3402 | mQueue = transportNames; |
| 3403 | } |
| 3404 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3405 | public void run() { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3406 | try { |
| 3407 | for (String transportName : mQueue) { |
| 3408 | IBackupTransport transport = getTransport(transportName); |
| 3409 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3410 | Slog.e(TAG, "Requested init for " + transportName + " but not found"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3411 | continue; |
| 3412 | } |
| 3413 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3414 | Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3415 | EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName()); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 3416 | long startRealtime = SystemClock.elapsedRealtime(); |
| 3417 | int status = transport.initializeDevice(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3418 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3419 | if (status == BackupConstants.TRANSPORT_OK) { |
| 3420 | status = transport.finishBackup(); |
| 3421 | } |
| 3422 | |
| 3423 | // Okay, the wipe really happened. Clean up our local bookkeeping. |
| 3424 | if (status == BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3425 | Slog.i(TAG, "Device init successful"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 3426 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3427 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 3428 | resetBackupState(new File(mBaseStateDir, transport.transportDirName())); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3429 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3430 | synchronized (mQueueLock) { |
| 3431 | recordInitPendingLocked(false, transportName); |
| 3432 | } |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 3433 | } else { |
| 3434 | // If this didn't work, requeue this one and try again |
| 3435 | // after a suitable interval |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3436 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3437 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3438 | synchronized (mQueueLock) { |
| 3439 | recordInitPendingLocked(true, transportName); |
| 3440 | } |
| 3441 | // do this via another alarm to make sure of the wakelock states |
| 3442 | long delay = transport.requestBackupTime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3443 | if (DEBUG) Slog.w(TAG, "init failed on " |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3444 | + transportName + " resched in " + delay); |
| 3445 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 3446 | System.currentTimeMillis() + delay, mRunInitIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3447 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3448 | } |
| 3449 | } catch (RemoteException e) { |
| 3450 | // can't happen; the transports are local |
| 3451 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3452 | Slog.e(TAG, "Unexpected error performing init", e); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3453 | } finally { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 3454 | // Done; release the wakelock |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3455 | mWakelock.release(); |
| 3456 | } |
| 3457 | } |
| 3458 | } |
| 3459 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3460 | private void dataChangedImpl(String packageName) { |
| 3461 | HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 3462 | dataChangedImpl(packageName, targets); |
| 3463 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3464 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3465 | private void dataChangedImpl(String packageName, HashSet<ApplicationInfo> targets) { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 3466 | // Record that we need a backup pass for the caller. Since multiple callers |
| 3467 | // may share a uid, we need to note all candidates within that uid and schedule |
| 3468 | // a backup pass for each of them. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3469 | EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 3470 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3471 | if (targets == null) { |
| 3472 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 3473 | + " uid=" + Binder.getCallingUid()); |
| 3474 | return; |
| 3475 | } |
| 3476 | |
| 3477 | synchronized (mQueueLock) { |
| 3478 | // Note that this client has made data changes that need to be backed up |
| 3479 | for (ApplicationInfo app : targets) { |
| 3480 | // validate the caller-supplied package name against the known set of |
| 3481 | // packages associated with this uid |
| 3482 | if (app.packageName.equals(packageName)) { |
| 3483 | // Add the caller to the set of pending backups. If there is |
| 3484 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3485 | BackupRequest req = new BackupRequest(app); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 3486 | if (mPendingBackups.put(app.packageName, req) == null) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3487 | // Journal this request in case of crash. The put() |
| 3488 | // operation returned null when this package was not already |
| 3489 | // in the set; we want to avoid touching the disk redundantly. |
| 3490 | writeToJournalLocked(packageName); |
| 3491 | |
| 3492 | if (DEBUG) { |
| 3493 | int numKeys = mPendingBackups.size(); |
| 3494 | Slog.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| 3495 | for (BackupRequest b : mPendingBackups.values()) { |
| 3496 | Slog.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName); |
| 3497 | } |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | // Note: packageName is currently unused, but may be in the future |
| 3506 | private HashSet<ApplicationInfo> dataChangedTargets(String packageName) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 3507 | // If the caller does not hold the BACKUP permission, it can only request a |
| 3508 | // backup of its own data. |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 3509 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 3510 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3511 | synchronized (mBackupParticipants) { |
| 3512 | return mBackupParticipants.get(Binder.getCallingUid()); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | // a caller with full permission can ask to back up any participating app |
| 3517 | // !!! TODO: allow backup of ANY app? |
| 3518 | HashSet<ApplicationInfo> targets = new HashSet<ApplicationInfo>(); |
| 3519 | synchronized (mBackupParticipants) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 3520 | int N = mBackupParticipants.size(); |
| 3521 | for (int i = 0; i < N; i++) { |
| 3522 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 3523 | if (s != null) { |
| 3524 | targets.addAll(s); |
| 3525 | } |
| 3526 | } |
| 3527 | } |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3528 | return targets; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 3529 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 3530 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 3531 | private void writeToJournalLocked(String str) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 3532 | RandomAccessFile out = null; |
| 3533 | try { |
| 3534 | if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir); |
| 3535 | out = new RandomAccessFile(mJournal, "rws"); |
| 3536 | out.seek(out.length()); |
| 3537 | out.writeUTF(str); |
| 3538 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3539 | Slog.e(TAG, "Can't write " + str + " to backup journal", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 3540 | mJournal = null; |
| 3541 | } finally { |
| 3542 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 3546 | // ----- IBackupManager binder interface ----- |
| 3547 | |
| 3548 | public void dataChanged(final String packageName) { |
| 3549 | final HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 3550 | if (targets == null) { |
| 3551 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 3552 | + " uid=" + Binder.getCallingUid()); |
| 3553 | return; |
| 3554 | } |
| 3555 | |
| 3556 | mBackupHandler.post(new Runnable() { |
| 3557 | public void run() { |
| 3558 | dataChangedImpl(packageName, targets); |
| 3559 | } |
| 3560 | }); |
| 3561 | } |
| 3562 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3563 | // Clear the given package's backup data from the current transport |
| 3564 | public void clearBackupData(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3565 | if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3566 | PackageInfo info; |
| 3567 | try { |
| 3568 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 3569 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3570 | Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3571 | return; |
| 3572 | } |
| 3573 | |
| 3574 | // If the caller does not hold the BACKUP permission, it can only request a |
| 3575 | // wipe of its own backed-up data. |
| 3576 | HashSet<ApplicationInfo> apps; |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 3577 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3578 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 3579 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 3580 | } else { |
| 3581 | // a caller with full permission can ask to back up any participating app |
| 3582 | // !!! TODO: allow data-clear of ANY app? |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3583 | if (DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3584 | apps = new HashSet<ApplicationInfo>(); |
| 3585 | int N = mBackupParticipants.size(); |
| 3586 | for (int i = 0; i < N; i++) { |
| 3587 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 3588 | if (s != null) { |
| 3589 | apps.addAll(s); |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | // now find the given package in the set of candidate apps |
| 3595 | for (ApplicationInfo app : apps) { |
| 3596 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3597 | if (DEBUG) Slog.v(TAG, "Found the app - running clear process"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3598 | // found it; fire off the clear request |
| 3599 | synchronized (mQueueLock) { |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 3600 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3601 | mWakelock.acquire(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3602 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 3603 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 3604 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 3605 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 3606 | } |
| 3607 | break; |
| 3608 | } |
| 3609 | } |
| 3610 | } |
| 3611 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 3612 | // Run a backup pass immediately for any applications that have declared |
| 3613 | // that they have pending updates. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 3614 | public void backupNow() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 3615 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 3616 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3617 | if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 3618 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 3619 | // Because the alarms we are using can jitter, and we want an *immediate* |
| 3620 | // backup pass to happen, we restart the timer beginning with "next time," |
| 3621 | // then manually fire the backup trigger intent ourselves. |
| 3622 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3623 | try { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3624 | mRunBackupIntent.send(); |
| 3625 | } catch (PendingIntent.CanceledException e) { |
| 3626 | // should never happen |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3627 | Slog.e(TAG, "run-backup intent cancelled!"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3628 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 3629 | } |
| 3630 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 3631 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3632 | // Run a *full* backup pass for the given package, writing the resulting data stream |
| 3633 | // to the supplied file descriptor. This method is synchronous and does not return |
| 3634 | // to the caller until the backup has been completed. |
| 3635 | public void fullBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeShared, |
| 3636 | boolean doAllApps, String[] pkgList) { |
| 3637 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 3638 | |
| 3639 | // Validate |
| 3640 | if (!doAllApps) { |
| 3641 | if (!includeShared) { |
| 3642 | // If we're backing up shared data (sdcard or equivalent), then we can run |
| 3643 | // without any supplied app names. Otherwise, we'd be doing no work, so |
| 3644 | // report the error. |
| 3645 | if (pkgList == null || pkgList.length == 0) { |
| 3646 | throw new IllegalArgumentException( |
| 3647 | "Backup requested but neither shared nor any apps named"); |
| 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | if (DEBUG) Slog.v(TAG, "Requesting full backup: apks=" + includeApks |
| 3653 | + " shared=" + includeShared + " all=" + doAllApps |
| 3654 | + " pkgs=" + pkgList); |
| 3655 | |
| 3656 | long oldId = Binder.clearCallingIdentity(); |
| 3657 | try { |
| 3658 | FullBackupParams params = new FullBackupParams(fd, includeApks, includeShared, |
| 3659 | doAllApps, pkgList); |
| 3660 | final int token = generateToken(); |
| 3661 | synchronized (mFullConfirmations) { |
| 3662 | mFullConfirmations.put(token, params); |
| 3663 | } |
| 3664 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3665 | // start up the confirmation UI |
| 3666 | if (DEBUG) Slog.d(TAG, "Starting backup confirmation UI, token=" + token); |
| 3667 | if (!startConfirmationUi(token, FullBackup.FULL_BACKUP_INTENT_ACTION)) { |
| 3668 | Slog.e(TAG, "Unable to launch full backup confirmation"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3669 | mFullConfirmations.delete(token); |
| 3670 | return; |
| 3671 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3672 | |
| 3673 | // make sure the screen is lit for the user interaction |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3674 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 3675 | |
| 3676 | // start the confirmation countdown |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3677 | startConfirmationTimeout(token, params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3678 | |
| 3679 | // wait for the backup to be performed |
| 3680 | if (DEBUG) Slog.d(TAG, "Waiting for full backup completion..."); |
| 3681 | waitForCompletion(params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3682 | } finally { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3683 | try { |
| 3684 | fd.close(); |
| 3685 | } catch (IOException e) { |
| 3686 | // just eat it |
| 3687 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3688 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3689 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3690 | if (DEBUG) Slog.d(TAG, "Full backup done; returning to caller"); |
| 3691 | } |
| 3692 | |
| 3693 | public void fullRestore(ParcelFileDescriptor fd) { |
| 3694 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 3695 | Slog.i(TAG, "Beginning full restore..."); |
| 3696 | |
| 3697 | long oldId = Binder.clearCallingIdentity(); |
| 3698 | |
| 3699 | try { |
| 3700 | FullRestoreParams params = new FullRestoreParams(fd); |
| 3701 | final int token = generateToken(); |
| 3702 | synchronized (mFullConfirmations) { |
| 3703 | mFullConfirmations.put(token, params); |
| 3704 | } |
| 3705 | |
| 3706 | // start up the confirmation UI |
| 3707 | if (DEBUG) Slog.d(TAG, "Starting restore confirmation UI, token=" + token); |
| 3708 | if (!startConfirmationUi(token, FullBackup.FULL_RESTORE_INTENT_ACTION)) { |
| 3709 | Slog.e(TAG, "Unable to launch full restore confirmation"); |
| 3710 | mFullConfirmations.delete(token); |
| 3711 | return; |
| 3712 | } |
| 3713 | |
| 3714 | // make sure the screen is lit for the user interaction |
| 3715 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 3716 | |
| 3717 | // start the confirmation countdown |
| 3718 | startConfirmationTimeout(token, params); |
| 3719 | |
| 3720 | // wait for the restore to be performed |
| 3721 | if (DEBUG) Slog.d(TAG, "Waiting for full restore completion..."); |
| 3722 | waitForCompletion(params); |
| 3723 | } finally { |
| 3724 | try { |
| 3725 | fd.close(); |
| 3726 | } catch (IOException e) { |
| 3727 | Slog.w(TAG, "Error trying to close fd after full restore: " + e); |
| 3728 | } |
| 3729 | Binder.restoreCallingIdentity(oldId); |
| 3730 | Slog.i(TAG, "Full restore completed"); |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | boolean startConfirmationUi(int token, String action) { |
| 3735 | try { |
| 3736 | Intent confIntent = new Intent(action); |
| 3737 | confIntent.setClassName("com.android.backupconfirm", |
| 3738 | "com.android.backupconfirm.BackupRestoreConfirmation"); |
| 3739 | confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token); |
| 3740 | confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3741 | mContext.startActivity(confIntent); |
| 3742 | } catch (ActivityNotFoundException e) { |
| 3743 | return false; |
| 3744 | } |
| 3745 | return true; |
| 3746 | } |
| 3747 | |
| 3748 | void startConfirmationTimeout(int token, FullParams params) { |
| 3749 | if (DEBUG) Slog.d(TAG, "Posting conf timeout msg after " |
| 3750 | + TIMEOUT_FULL_CONFIRMATION + " millis"); |
| 3751 | Message msg = mBackupHandler.obtainMessage(MSG_FULL_CONFIRMATION_TIMEOUT, |
| 3752 | token, 0, params); |
| 3753 | mBackupHandler.sendMessageDelayed(msg, TIMEOUT_FULL_CONFIRMATION); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | void waitForCompletion(FullParams params) { |
| 3757 | synchronized (params.latch) { |
| 3758 | while (params.latch.get() == false) { |
| 3759 | try { |
| 3760 | params.latch.wait(); |
| 3761 | } catch (InterruptedException e) { /* never interrupted */ } |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | void signalFullBackupRestoreCompletion(FullParams params) { |
| 3767 | synchronized (params.latch) { |
| 3768 | params.latch.set(true); |
| 3769 | params.latch.notifyAll(); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | // Confirm that the previously-requested full backup/restore operation can proceed. This |
| 3774 | // is used to require a user-facing disclosure about the operation. |
| 3775 | public void acknowledgeFullBackupOrRestore(int token, boolean allow, |
| 3776 | IFullBackupRestoreObserver observer) { |
| 3777 | if (DEBUG) Slog.d(TAG, "acknowledgeFullBackupOrRestore : token=" + token |
| 3778 | + " allow=" + allow); |
| 3779 | |
| 3780 | // TODO: possibly require not just this signature-only permission, but even |
| 3781 | // require that the specific designated confirmation-UI app uid is the caller? |
| 3782 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 3783 | |
| 3784 | long oldId = Binder.clearCallingIdentity(); |
| 3785 | try { |
| 3786 | |
| 3787 | FullParams params; |
| 3788 | synchronized (mFullConfirmations) { |
| 3789 | params = mFullConfirmations.get(token); |
| 3790 | if (params != null) { |
| 3791 | mBackupHandler.removeMessages(MSG_FULL_CONFIRMATION_TIMEOUT, params); |
| 3792 | mFullConfirmations.delete(token); |
| 3793 | |
| 3794 | if (allow) { |
| 3795 | params.observer = observer; |
| 3796 | final int verb = params instanceof FullBackupParams |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3797 | ? MSG_RUN_FULL_BACKUP |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3798 | : MSG_RUN_FULL_RESTORE; |
| 3799 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3800 | if (DEBUG) Slog.d(TAG, "Sending conf message with verb " + verb); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3801 | mWakelock.acquire(); |
| 3802 | Message msg = mBackupHandler.obtainMessage(verb, params); |
| 3803 | mBackupHandler.sendMessage(msg); |
| 3804 | } else { |
| 3805 | Slog.w(TAG, "User rejected full backup/restore operation"); |
| 3806 | // indicate completion without having actually transferred any data |
| 3807 | signalFullBackupRestoreCompletion(params); |
| 3808 | } |
| 3809 | } else { |
| 3810 | Slog.w(TAG, "Attempted to ack full backup/restore with invalid token"); |
| 3811 | } |
| 3812 | } |
| 3813 | } finally { |
| 3814 | Binder.restoreCallingIdentity(oldId); |
| 3815 | } |
| 3816 | } |
| 3817 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3818 | // Enable/disable the backup service |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3819 | public void setBackupEnabled(boolean enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3820 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3821 | "setBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3822 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3823 | Slog.i(TAG, "Backup enabled => " + enable); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3824 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3825 | boolean wasEnabled = mEnabled; |
| 3826 | synchronized (this) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 3827 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 3828 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3829 | mEnabled = enable; |
| 3830 | } |
| 3831 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 3832 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3833 | if (enable && !wasEnabled && mProvisioned) { |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 3834 | // if we've just been enabled, start scheduling backup passes |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3835 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 3836 | } else if (!enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3837 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3838 | if (DEBUG) Slog.i(TAG, "Opting out of backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3839 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3840 | mAlarmManager.cancel(mRunBackupIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 3841 | |
| 3842 | // This also constitutes an opt-out, so we wipe any data for |
| 3843 | // this device from the backend. We start that process with |
| 3844 | // an alarm in order to guarantee wakelock states. |
| 3845 | if (wasEnabled && mProvisioned) { |
| 3846 | // NOTE: we currently flush every registered transport, not just |
| 3847 | // the currently-active one. |
| 3848 | HashSet<String> allTransports; |
| 3849 | synchronized (mTransports) { |
| 3850 | allTransports = new HashSet<String>(mTransports.keySet()); |
| 3851 | } |
| 3852 | // build the set of transports for which we are posting an init |
| 3853 | for (String transport : allTransports) { |
| 3854 | recordInitPendingLocked(true, transport); |
| 3855 | } |
| 3856 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), |
| 3857 | mRunInitIntent); |
| 3858 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3859 | } |
| 3860 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 3861 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3862 | |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 3863 | // Enable/disable automatic restore of app data at install time |
| 3864 | public void setAutoRestore(boolean doAutoRestore) { |
| 3865 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 3866 | "setBackupEnabled"); |
| 3867 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3868 | Slog.i(TAG, "Auto restore => " + doAutoRestore); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 3869 | |
| 3870 | synchronized (this) { |
| 3871 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 3872 | Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0); |
| 3873 | mAutoRestore = doAutoRestore; |
| 3874 | } |
| 3875 | } |
| 3876 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3877 | // Mark the backup service as having been provisioned |
| 3878 | public void setBackupProvisioned(boolean available) { |
| 3879 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 3880 | "setBackupProvisioned"); |
| 3881 | |
| 3882 | boolean wasProvisioned = mProvisioned; |
| 3883 | synchronized (this) { |
| 3884 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 3885 | Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0); |
| 3886 | mProvisioned = available; |
| 3887 | } |
| 3888 | |
| 3889 | synchronized (mQueueLock) { |
| 3890 | if (available && !wasProvisioned && mEnabled) { |
| 3891 | // we're now good to go, so start the backup alarms |
| 3892 | startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL); |
| 3893 | } else if (!available) { |
| 3894 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3895 | Slog.w(TAG, "Backup service no longer provisioned"); |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3896 | mAlarmManager.cancel(mRunBackupIntent); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | private void startBackupAlarmsLocked(long delayBeforeFirstBackup) { |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 3902 | // We used to use setInexactRepeating(), but that may be linked to |
| 3903 | // backups running at :00 more often than not, creating load spikes. |
| 3904 | // Schedule at an exact time for now, and also add a bit of "fuzz". |
| 3905 | |
| 3906 | Random random = new Random(); |
| 3907 | long when = System.currentTimeMillis() + delayBeforeFirstBackup + |
| 3908 | random.nextInt(FUZZ_MILLIS); |
| 3909 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, |
| 3910 | BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 3911 | mNextBackupPass = when; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3914 | // Report whether the backup mechanism is currently enabled |
| 3915 | public boolean isBackupEnabled() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 3916 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 3917 | return mEnabled; // no need to synchronize just to read it |
| 3918 | } |
| 3919 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3920 | // Report the name of the currently active transport |
| 3921 | public String getCurrentTransport() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 3922 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 3923 | "getCurrentTransport"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3924 | if (DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3925 | return mCurrentTransport; |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3928 | // Report all known, available backup transports |
| 3929 | public String[] listAllTransports() { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 3930 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 3931 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3932 | String[] list = null; |
| 3933 | ArrayList<String> known = new ArrayList<String>(); |
| 3934 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 3935 | if (entry.getValue() != null) { |
| 3936 | known.add(entry.getKey()); |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | if (known.size() > 0) { |
| 3941 | list = new String[known.size()]; |
| 3942 | known.toArray(list); |
| 3943 | } |
| 3944 | return list; |
| 3945 | } |
| 3946 | |
| 3947 | // Select which transport to use for the next backup operation. If the given |
| 3948 | // name is not one of the available transports, no action is taken and the method |
| 3949 | // returns null. |
| 3950 | public String selectBackupTransport(String transport) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 3951 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3952 | |
| 3953 | synchronized (mTransports) { |
| 3954 | String prevTransport = null; |
| 3955 | if (mTransports.get(transport) != null) { |
| 3956 | prevTransport = mCurrentTransport; |
| 3957 | mCurrentTransport = transport; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 3958 | Settings.Secure.putString(mContext.getContentResolver(), |
| 3959 | Settings.Secure.BACKUP_TRANSPORT, transport); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3960 | Slog.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3961 | + " returning " + prevTransport); |
| 3962 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3963 | Slog.w(TAG, "Attempt to select unavailable transport " + transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 3964 | } |
| 3965 | return prevTransport; |
| 3966 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 3967 | } |
| 3968 | |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 3969 | // Supply the configuration Intent for the given transport. If the name is not one |
| 3970 | // of the available transports, or if the transport does not supply any configuration |
| 3971 | // UI, the method returns null. |
| 3972 | public Intent getConfigurationIntent(String transportName) { |
| 3973 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 3974 | "getConfigurationIntent"); |
| 3975 | |
| 3976 | synchronized (mTransports) { |
| 3977 | final IBackupTransport transport = mTransports.get(transportName); |
| 3978 | if (transport != null) { |
| 3979 | try { |
| 3980 | final Intent intent = transport.configurationIntent(); |
| 3981 | if (DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent " |
| 3982 | + intent); |
| 3983 | return intent; |
| 3984 | } catch (RemoteException e) { |
| 3985 | /* fall through to return null */ |
| 3986 | } |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | return null; |
| 3991 | } |
| 3992 | |
| 3993 | // Supply the configuration summary string for the given transport. If the name is |
| 3994 | // not one of the available transports, or if the transport does not supply any |
| 3995 | // summary / destination string, the method can return null. |
| 3996 | // |
| 3997 | // This string is used VERBATIM as the summary text of the relevant Settings item! |
| 3998 | public String getDestinationString(String transportName) { |
| 3999 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4000 | "getConfigurationIntent"); |
| 4001 | |
| 4002 | synchronized (mTransports) { |
| 4003 | final IBackupTransport transport = mTransports.get(transportName); |
| 4004 | if (transport != null) { |
| 4005 | try { |
| 4006 | final String text = transport.currentDestinationString(); |
| 4007 | if (DEBUG) Slog.d(TAG, "getDestinationString() returning " + text); |
| 4008 | return text; |
| 4009 | } catch (RemoteException e) { |
| 4010 | /* fall through to return null */ |
| 4011 | } |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | return null; |
| 4016 | } |
| 4017 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4018 | // Callback: a requested backup agent has been instantiated. This should only |
| 4019 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4020 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4021 | synchronized(mAgentConnectLock) { |
| 4022 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4023 | Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4024 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 4025 | mConnectedAgent = agent; |
| 4026 | mConnecting = false; |
| 4027 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4028 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4029 | + " claiming agent connected"); |
| 4030 | } |
| 4031 | mAgentConnectLock.notifyAll(); |
| 4032 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 4036 | // 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] | 4037 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4038 | public void agentDisconnected(String packageName) { |
| 4039 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4040 | synchronized(mAgentConnectLock) { |
| 4041 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 4042 | mConnectedAgent = null; |
| 4043 | mConnecting = false; |
| 4044 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4045 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4046 | + " claiming agent disconnected"); |
| 4047 | } |
| 4048 | mAgentConnectLock.notifyAll(); |
| 4049 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4050 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4051 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4052 | // An application being installed will need a restore pass, then the Package Manager |
| 4053 | // will need to be told when the restore is finished. |
| 4054 | public void restoreAtInstall(String packageName, int token) { |
| 4055 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4056 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4057 | + " attemping install-time restore"); |
| 4058 | return; |
| 4059 | } |
| 4060 | |
| 4061 | long restoreSet = getAvailableRestoreToken(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4062 | if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4063 | + " token=" + Integer.toHexString(token)); |
| 4064 | |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 4065 | if (mAutoRestore && mProvisioned && restoreSet != 0) { |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4066 | // okay, we're going to attempt a restore of this package from this restore set. |
| 4067 | // The eventual message back into the Package Manager to run the post-install |
| 4068 | // steps for 'token' will be issued from the restore handling code. |
| 4069 | |
| 4070 | // We can use a synthetic PackageInfo here because: |
| 4071 | // 1. We know it's valid, since the Package Manager supplied the name |
| 4072 | // 2. Only the packageName field will be used by the restore code |
| 4073 | PackageInfo pkg = new PackageInfo(); |
| 4074 | pkg.packageName = packageName; |
| 4075 | |
| 4076 | mWakelock.acquire(); |
| 4077 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 4078 | msg.obj = new RestoreParams(getTransport(mCurrentTransport), null, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4079 | restoreSet, pkg, token, true); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4080 | mBackupHandler.sendMessage(msg); |
| 4081 | } else { |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 4082 | // Auto-restore disabled or no way to attempt a restore; just tell the Package |
| 4083 | // Manager to proceed with the post-install handling for this package. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4084 | if (DEBUG) Slog.v(TAG, "No restore set -- skipping restore"); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4085 | try { |
| 4086 | mPackageManagerBinder.finishPackageInstall(token); |
| 4087 | } catch (RemoteException e) { /* can't happen */ } |
| 4088 | } |
| 4089 | } |
| 4090 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 4091 | // Hand off a restore session |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4092 | public IRestoreSession beginRestoreSession(String packageName, String transport) { |
| 4093 | if (DEBUG) Slog.v(TAG, "beginRestoreSession: pkg=" + packageName |
| 4094 | + " transport=" + transport); |
| 4095 | |
| 4096 | boolean needPermission = true; |
| 4097 | if (transport == null) { |
| 4098 | transport = mCurrentTransport; |
| 4099 | |
| 4100 | if (packageName != null) { |
| 4101 | PackageInfo app = null; |
| 4102 | try { |
| 4103 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 4104 | } catch (NameNotFoundException nnf) { |
| 4105 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| 4106 | throw new IllegalArgumentException("Package " + packageName + " not found"); |
| 4107 | } |
| 4108 | |
| 4109 | if (app.applicationInfo.uid == Binder.getCallingUid()) { |
| 4110 | // So: using the current active transport, and the caller has asked |
| 4111 | // that its own package will be restored. In this narrow use case |
| 4112 | // we do not require the caller to hold the permission. |
| 4113 | needPermission = false; |
| 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | |
| 4118 | if (needPermission) { |
| 4119 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4120 | "beginRestoreSession"); |
| 4121 | } else { |
| 4122 | if (DEBUG) Slog.d(TAG, "restoring self on current transport; no permission needed"); |
| 4123 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4124 | |
| 4125 | synchronized(this) { |
| 4126 | if (mActiveRestoreSession != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4127 | Slog.d(TAG, "Restore session requested but one already active"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4128 | return null; |
| 4129 | } |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4130 | mActiveRestoreSession = new ActiveRestoreSession(packageName, transport); |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4131 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4132 | } |
| 4133 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 4134 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4135 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4136 | void clearRestoreSession(ActiveRestoreSession currentSession) { |
| 4137 | synchronized(this) { |
| 4138 | if (currentSession != mActiveRestoreSession) { |
| 4139 | Slog.e(TAG, "ending non-current restore session"); |
| 4140 | } else { |
| 4141 | if (DEBUG) Slog.v(TAG, "Clearing restore session and halting timeout"); |
| 4142 | mActiveRestoreSession = null; |
| 4143 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4148 | // Note that a currently-active backup agent has notified us that it has |
| 4149 | // completed the given outstanding asynchronous backup/restore operation. |
| 4150 | public void opComplete(int token) { |
| 4151 | synchronized (mCurrentOpLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4152 | if (DEBUG) Slog.v(TAG, "opComplete: " + Integer.toHexString(token)); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4153 | mCurrentOperations.put(token, OP_ACKNOWLEDGED); |
| 4154 | mCurrentOpLock.notifyAll(); |
| 4155 | } |
| 4156 | } |
| 4157 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4158 | // ----- Restore session ----- |
| 4159 | |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 4160 | class ActiveRestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4161 | private static final String TAG = "RestoreSession"; |
| 4162 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4163 | private String mPackageName; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4164 | private IBackupTransport mRestoreTransport = null; |
| 4165 | RestoreSet[] mRestoreSets = null; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4166 | boolean mEnded = false; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4167 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4168 | ActiveRestoreSession(String packageName, String transport) { |
| 4169 | mPackageName = packageName; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4170 | mRestoreTransport = getTransport(transport); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | // --- Binder interface --- |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4174 | public synchronized int getAvailableRestoreSets(IRestoreObserver observer) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4175 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4176 | "getAvailableRestoreSets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4177 | if (observer == null) { |
| 4178 | throw new IllegalArgumentException("Observer must not be null"); |
| 4179 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4180 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4181 | if (mEnded) { |
| 4182 | throw new IllegalStateException("Restore session already ended"); |
| 4183 | } |
| 4184 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4185 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4186 | try { |
| Christopher Tate | 4338304 | 2009-07-13 15:17:13 -0700 | [diff] [blame] | 4187 | if (mRestoreTransport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4188 | Slog.w(TAG, "Null transport getting restore sets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4189 | return -1; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4190 | } |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4191 | // spin off the transport request to our service thread |
| 4192 | mWakelock.acquire(); |
| 4193 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS, |
| 4194 | new RestoreGetSetsParams(mRestoreTransport, this, observer)); |
| 4195 | mBackupHandler.sendMessage(msg); |
| 4196 | return 0; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4197 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4198 | Slog.e(TAG, "Error in getAvailableRestoreSets", e); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4199 | return -1; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4200 | } finally { |
| 4201 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4202 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4205 | public synchronized int restoreAll(long token, IRestoreObserver observer) { |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4206 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4207 | "performRestore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4208 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4209 | if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token) |
| Christopher Tate | f2c321a | 2009-08-10 15:43:36 -0700 | [diff] [blame] | 4210 | + " observer=" + observer); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4211 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4212 | if (mEnded) { |
| 4213 | throw new IllegalStateException("Restore session already ended"); |
| 4214 | } |
| 4215 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4216 | if (mRestoreTransport == null || mRestoreSets == null) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4217 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 4218 | return -1; |
| 4219 | } |
| 4220 | |
| 4221 | if (mPackageName != null) { |
| 4222 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4223 | return -1; |
| 4224 | } |
| 4225 | |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4226 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4227 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 4228 | if (token == mRestoreSets[i].token) { |
| 4229 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4230 | mWakelock.acquire(); |
| 4231 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4232 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, true); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4233 | mBackupHandler.sendMessage(msg); |
| 4234 | Binder.restoreCallingIdentity(oldId); |
| 4235 | return 0; |
| 4236 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4237 | } |
| 4238 | } |
| Christopher Tate | 0e0b4ae | 2009-08-10 16:13:47 -0700 | [diff] [blame] | 4239 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4240 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4241 | return -1; |
| 4242 | } |
| 4243 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4244 | public synchronized int restorePackage(String packageName, IRestoreObserver observer) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4245 | if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4246 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4247 | if (mEnded) { |
| 4248 | throw new IllegalStateException("Restore session already ended"); |
| 4249 | } |
| 4250 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4251 | if (mPackageName != null) { |
| 4252 | if (! mPackageName.equals(packageName)) { |
| 4253 | Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName |
| 4254 | + " on session for package " + mPackageName); |
| 4255 | return -1; |
| 4256 | } |
| 4257 | } |
| 4258 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4259 | PackageInfo app = null; |
| 4260 | try { |
| 4261 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 4262 | } catch (NameNotFoundException nnf) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4263 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4264 | return -1; |
| 4265 | } |
| 4266 | |
| 4267 | // If the caller is not privileged and is not coming from the target |
| 4268 | // app's uid, throw a permission exception back to the caller. |
| 4269 | int perm = mContext.checkPermission(android.Manifest.permission.BACKUP, |
| 4270 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 4271 | if ((perm == PackageManager.PERMISSION_DENIED) && |
| 4272 | (app.applicationInfo.uid != Binder.getCallingUid())) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4273 | Slog.w(TAG, "restorePackage: bad packageName=" + packageName |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4274 | + " or calling uid=" + Binder.getCallingUid()); |
| 4275 | throw new SecurityException("No permission to restore other packages"); |
| 4276 | } |
| 4277 | |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 4278 | // If the package has no backup agent, we obviously cannot proceed |
| 4279 | if (app.applicationInfo.backupAgentName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4280 | Slog.w(TAG, "Asked to restore package " + packageName + " with no agent"); |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 4281 | return -1; |
| 4282 | } |
| 4283 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4284 | // So far so good; we're allowed to try to restore this package. Now |
| 4285 | // check whether there is data for it in the current dataset, falling back |
| 4286 | // to the ancestral dataset if not. |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4287 | long token = getAvailableRestoreToken(packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4288 | |
| 4289 | // If we didn't come up with a place to look -- no ancestral dataset and |
| 4290 | // the app has never been backed up from this device -- there's nothing |
| 4291 | // to do but return failure. |
| 4292 | if (token == 0) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4293 | 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] | 4294 | return -1; |
| 4295 | } |
| 4296 | |
| 4297 | // Ready to go: enqueue the restore request and claim success |
| 4298 | long oldId = Binder.clearCallingIdentity(); |
| 4299 | mWakelock.acquire(); |
| 4300 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4301 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, app, 0, false); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4302 | mBackupHandler.sendMessage(msg); |
| 4303 | Binder.restoreCallingIdentity(oldId); |
| 4304 | return 0; |
| 4305 | } |
| 4306 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4307 | // Posted to the handler to tear down a restore session in a cleanly synchronized way |
| 4308 | class EndRestoreRunnable implements Runnable { |
| 4309 | BackupManagerService mBackupManager; |
| 4310 | ActiveRestoreSession mSession; |
| 4311 | |
| 4312 | EndRestoreRunnable(BackupManagerService manager, ActiveRestoreSession session) { |
| 4313 | mBackupManager = manager; |
| 4314 | mSession = session; |
| 4315 | } |
| 4316 | |
| 4317 | public void run() { |
| 4318 | // clean up the session's bookkeeping |
| 4319 | synchronized (mSession) { |
| 4320 | try { |
| 4321 | if (mSession.mRestoreTransport != null) { |
| 4322 | mSession.mRestoreTransport.finishRestore(); |
| 4323 | } |
| 4324 | } catch (Exception e) { |
| 4325 | Slog.e(TAG, "Error in finishRestore", e); |
| 4326 | } finally { |
| 4327 | mSession.mRestoreTransport = null; |
| 4328 | mSession.mEnded = true; |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | // clean up the BackupManagerService side of the bookkeeping |
| 4333 | // and cancel any pending timeout message |
| 4334 | mBackupManager.clearRestoreSession(mSession); |
| 4335 | } |
| 4336 | } |
| 4337 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4338 | public synchronized void endRestoreSession() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4339 | if (DEBUG) Slog.d(TAG, "endRestoreSession"); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4340 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4341 | if (mEnded) { |
| 4342 | throw new IllegalStateException("Restore session already ended"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4343 | } |
| 4344 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4345 | mBackupHandler.post(new EndRestoreRunnable(BackupManagerService.this, this)); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4349 | @Override |
| 4350 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 4351 | long identityToken = Binder.clearCallingIdentity(); |
| 4352 | try { |
| 4353 | dumpInternal(pw); |
| 4354 | } finally { |
| 4355 | Binder.restoreCallingIdentity(identityToken); |
| 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | private void dumpInternal(PrintWriter pw) { |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4360 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4361 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled") |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4362 | + " / " + (!mProvisioned ? "not " : "") + "provisioned / " |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 4363 | + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init"); |
| Christopher Tate | ae06ed9 | 2010-02-25 17:13:28 -0800 | [diff] [blame] | 4364 | pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled")); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4365 | pw.println("Last backup pass: " + mLastBackupPass |
| 4366 | + " (now = " + System.currentTimeMillis() + ')'); |
| 4367 | pw.println(" next scheduled: " + mNextBackupPass); |
| 4368 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4369 | pw.println("Available transports:"); |
| 4370 | for (String t : listAllTransports()) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4371 | pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t); |
| 4372 | try { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 4373 | IBackupTransport transport = getTransport(t); |
| 4374 | File dir = new File(mBaseStateDir, transport.transportDirName()); |
| 4375 | pw.println(" destination: " + transport.currentDestinationString()); |
| 4376 | pw.println(" intent: " + transport.configurationIntent()); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4377 | for (File f : dir.listFiles()) { |
| 4378 | pw.println(" " + f.getName() + " - " + f.length() + " state bytes"); |
| 4379 | } |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 4380 | } catch (Exception e) { |
| 4381 | Slog.e(TAG, "Error in transport", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4382 | pw.println(" Error: " + e); |
| 4383 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4384 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4385 | |
| 4386 | pw.println("Pending init: " + mPendingInits.size()); |
| 4387 | for (String s : mPendingInits) { |
| 4388 | pw.println(" " + s); |
| 4389 | } |
| 4390 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4391 | int N = mBackupParticipants.size(); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4392 | pw.println("Participants:"); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4393 | for (int i=0; i<N; i++) { |
| 4394 | int uid = mBackupParticipants.keyAt(i); |
| 4395 | pw.print(" uid: "); |
| 4396 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4397 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 4398 | for (ApplicationInfo app: participants) { |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4399 | pw.println(" " + app.packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4400 | } |
| 4401 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4402 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 4403 | pw.println("Ancestral packages: " |
| 4404 | + (mAncestralPackages == null ? "none" : mAncestralPackages.size())); |
| Christopher Tate | 5923c97 | 2010-04-04 17:45:35 -0700 | [diff] [blame] | 4405 | if (mAncestralPackages != null) { |
| 4406 | for (String pkg : mAncestralPackages) { |
| 4407 | pw.println(" " + pkg); |
| 4408 | } |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 4409 | } |
| 4410 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 4411 | pw.println("Ever backed up: " + mEverStoredApps.size()); |
| 4412 | for (String pkg : mEverStoredApps) { |
| 4413 | pw.println(" " + pkg); |
| 4414 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4415 | |
| 4416 | pw.println("Pending backup: " + mPendingBackups.size()); |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 4417 | for (BackupRequest req : mPendingBackups.values()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4418 | pw.println(" " + req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4419 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4420 | } |
| 4421 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4422 | } |