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