| 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 | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 37 | import android.content.ContentResolver; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 40 | import android.content.IntentFilter; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 42 | import android.content.pm.ApplicationInfo; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 43 | import android.content.pm.IPackageDataObserver; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 44 | import android.content.pm.IPackageDeleteObserver; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 45 | import android.content.pm.IPackageInstallObserver; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 46 | import android.content.pm.IPackageManager; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 47 | import android.content.pm.PackageInfo; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 48 | import android.content.pm.PackageManager; |
| Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 49 | import android.content.pm.Signature; |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 50 | import android.content.pm.PackageManager.NameNotFoundException; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 52 | import android.os.Binder; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 53 | import android.os.Build; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 54 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 55 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 56 | import android.os.Handler; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 57 | import android.os.HandlerThread; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 58 | import android.os.IBinder; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 59 | import android.os.Looper; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 60 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 61 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 62 | import android.os.PowerManager; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 63 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 64 | import android.os.RemoteException; |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 65 | import android.os.ServiceManager; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 66 | import android.os.SystemClock; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 67 | import android.os.WorkSource; |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 68 | import android.os.storage.IMountService; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 69 | import android.provider.Settings; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 70 | import android.util.EventLog; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 71 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 72 | import android.util.Slog; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 73 | import android.util.SparseArray; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 74 | import android.util.StringBuilderPrinter; |
| 75 | |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 76 | import com.android.internal.backup.BackupConstants; |
| 77 | import com.android.internal.backup.IBackupTransport; |
| 78 | import com.android.internal.backup.LocalTransport; |
| 79 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| 80 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 81 | import java.io.BufferedInputStream; |
| 82 | import java.io.BufferedOutputStream; |
| 83 | import java.io.ByteArrayOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 84 | import java.io.DataInputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 85 | import java.io.DataOutputStream; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 86 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 87 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 88 | import java.io.FileDescriptor; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 89 | import java.io.FileInputStream; |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 90 | import java.io.FileNotFoundException; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 91 | import java.io.FileOutputStream; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 92 | import java.io.IOException; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 93 | import java.io.InputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 94 | import java.io.OutputStream; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 95 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 96 | import java.io.RandomAccessFile; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 97 | import java.security.InvalidAlgorithmParameterException; |
| 98 | import java.security.InvalidKeyException; |
| 99 | import java.security.Key; |
| 100 | import java.security.NoSuchAlgorithmException; |
| 101 | import java.security.SecureRandom; |
| 102 | import java.security.spec.InvalidKeySpecException; |
| 103 | import java.security.spec.KeySpec; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 104 | import java.text.SimpleDateFormat; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 105 | import java.util.ArrayList; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 106 | import java.util.Arrays; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 107 | import java.util.Date; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 108 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 109 | import java.util.HashSet; |
| 110 | import java.util.List; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 111 | import java.util.Map; |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 112 | import java.util.Random; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 113 | import java.util.Set; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 114 | import java.util.concurrent.atomic.AtomicBoolean; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 115 | import java.util.zip.Deflater; |
| 116 | import java.util.zip.DeflaterOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 117 | import java.util.zip.InflaterInputStream; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 118 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 119 | import javax.crypto.BadPaddingException; |
| 120 | import javax.crypto.Cipher; |
| 121 | import javax.crypto.CipherInputStream; |
| 122 | import javax.crypto.CipherOutputStream; |
| 123 | import javax.crypto.IllegalBlockSizeException; |
| 124 | import javax.crypto.NoSuchPaddingException; |
| 125 | import javax.crypto.SecretKey; |
| 126 | import javax.crypto.SecretKeyFactory; |
| 127 | import javax.crypto.spec.IvParameterSpec; |
| 128 | import javax.crypto.spec.PBEKeySpec; |
| 129 | import javax.crypto.spec.SecretKeySpec; |
| 130 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 131 | class BackupManagerService extends IBackupManager.Stub { |
| 132 | private static final String TAG = "BackupManagerService"; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 133 | private static final boolean DEBUG = true; |
| Christopher Tate | b1543a9 | 2011-09-07 12:11:09 -0700 | [diff] [blame] | 134 | private static final boolean MORE_DEBUG = false; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 135 | |
| 136 | // Name and current contents version of the full-backup manifest file |
| 137 | static final String BACKUP_MANIFEST_FILENAME = "_manifest"; |
| 138 | static final int BACKUP_MANIFEST_VERSION = 1; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 139 | static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n"; |
| 140 | static final int BACKUP_FILE_VERSION = 1; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 141 | static final boolean COMPRESS_FULL_BACKUPS = true; // should be true in production |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 142 | |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 143 | static final String SHARED_BACKUP_AGENT_PACKAGE = "com.android.sharedstoragebackup"; |
| 144 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 145 | // How often we perform a backup pass. Privileged external callers can |
| 146 | // trigger an immediate pass. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 147 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 148 | |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 149 | // Random variation in backup scheduling time to avoid server load spikes |
| 150 | private static final int FUZZ_MILLIS = 5 * 60 * 1000; |
| 151 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 152 | // The amount of time between the initial provisioning of the device and |
| 153 | // the first backup pass. |
| 154 | private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR; |
| 155 | |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 156 | private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN"; |
| 157 | private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT"; |
| 158 | private static final String RUN_CLEAR_ACTION = "android.app.backup.intent.CLEAR"; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 159 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 160 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 161 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 162 | private static final int MSG_RUN_CLEAR = 4; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 163 | private static final int MSG_RUN_INITIALIZE = 5; |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 164 | private static final int MSG_RUN_GET_RESTORE_SETS = 6; |
| 165 | private static final int MSG_TIMEOUT = 7; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 166 | private static final int MSG_RESTORE_TIMEOUT = 8; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 167 | private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9; |
| 168 | private static final int MSG_RUN_FULL_RESTORE = 10; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 169 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 170 | // backup task state machine tick |
| 171 | static final int MSG_BACKUP_RESTORE_STEP = 20; |
| 172 | static final int MSG_OP_COMPLETE = 21; |
| 173 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 174 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 175 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 176 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 177 | // Timeout intervals for agent backup & restore operations |
| 178 | static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 179 | static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 180 | static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 181 | static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000; |
| 182 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 183 | // User confirmation timeout for a full backup/restore operation. It's this long in |
| 184 | // order to give them time to enter the backup password. |
| 185 | static final long TIMEOUT_FULL_CONFIRMATION = 60 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 186 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 187 | private Context mContext; |
| 188 | private PackageManager mPackageManager; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 189 | IPackageManager mPackageManagerBinder; |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 190 | private IActivityManager mActivityManager; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 191 | private PowerManager mPowerManager; |
| 192 | private AlarmManager mAlarmManager; |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 193 | private IMountService mMountService; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 194 | IBackupManager mBackupManagerBinder; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 195 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 196 | boolean mEnabled; // access to this is synchronized on 'this' |
| 197 | boolean mProvisioned; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 198 | boolean mAutoRestore; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 199 | PowerManager.WakeLock mWakelock; |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 200 | HandlerThread mHandlerThread; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 201 | BackupHandler mBackupHandler; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 202 | PendingIntent mRunBackupIntent, mRunInitIntent; |
| 203 | BroadcastReceiver mRunBackupReceiver, mRunInitReceiver; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 204 | // 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] | 205 | final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 206 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 207 | // set of backup services that have pending changes |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 208 | class BackupRequest { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 209 | public String packageName; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 210 | |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 211 | BackupRequest(String pkgName) { |
| 212 | packageName = pkgName; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 213 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 214 | |
| 215 | public String toString() { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 216 | return "BackupRequest{pkg=" + packageName + "}"; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 217 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 218 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 219 | // Backups that we haven't started yet. Keys are package names. |
| 220 | HashMap<String,BackupRequest> mPendingBackups |
| 221 | = new HashMap<String,BackupRequest>(); |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 222 | |
| 223 | // Pseudoname that we use for the Package Manager metadata "package" |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 224 | static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 225 | |
| 226 | // locking around the pending-backup management |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 227 | final Object mQueueLock = new Object(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 228 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 229 | // The thread performing the sequence of queued backups binds to each app's agent |
| 230 | // in succession. Bind notifications are asynchronously delivered through the |
| 231 | // Activity Manager; use this lock object to signal when a requested binding has |
| 232 | // completed. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 233 | final Object mAgentConnectLock = new Object(); |
| 234 | IBackupAgent mConnectedAgent; |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 235 | volatile boolean mBackupRunning; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 236 | volatile boolean mConnecting; |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 237 | volatile long mLastBackupPass; |
| 238 | volatile long mNextBackupPass; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 239 | |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 240 | // For debugging, we maintain a progress trace of operations during backup |
| 241 | static final boolean DEBUG_BACKUP_TRACE = true; |
| 242 | final List<String> mBackupTrace = new ArrayList<String>(); |
| 243 | |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 244 | // A similar synchronization mechanism around clearing apps' data for restore |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 245 | final Object mClearDataLock = new Object(); |
| 246 | volatile boolean mClearingData; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 247 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 248 | // Transport bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 249 | final HashMap<String,IBackupTransport> mTransports |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 250 | = new HashMap<String,IBackupTransport>(); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 251 | String mCurrentTransport; |
| 252 | IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 253 | ActiveRestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 254 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 255 | class RestoreGetSetsParams { |
| 256 | public IBackupTransport transport; |
| 257 | public ActiveRestoreSession session; |
| 258 | public IRestoreObserver observer; |
| 259 | |
| 260 | RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session, |
| 261 | IRestoreObserver _observer) { |
| 262 | transport = _transport; |
| 263 | session = _session; |
| 264 | observer = _observer; |
| 265 | } |
| 266 | } |
| 267 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 268 | class RestoreParams { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 269 | public IBackupTransport transport; |
| 270 | public IRestoreObserver observer; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 271 | public long token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 272 | public PackageInfo pkgInfo; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 273 | 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] | 274 | public boolean needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 275 | public String[] filterSet; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 276 | |
| 277 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 278 | long _token, PackageInfo _pkg, int _pmToken, boolean _needFullBackup) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 279 | transport = _transport; |
| 280 | observer = _obs; |
| 281 | token = _token; |
| 282 | pkgInfo = _pkg; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 283 | pmToken = _pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 284 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 285 | filterSet = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 286 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 287 | |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 288 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 289 | boolean _needFullBackup) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 290 | transport = _transport; |
| 291 | observer = _obs; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 292 | token = _token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 293 | pkgInfo = null; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 294 | pmToken = 0; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 295 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 296 | filterSet = null; |
| 297 | } |
| 298 | |
| 299 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 300 | String[] _filterSet, boolean _needFullBackup) { |
| 301 | transport = _transport; |
| 302 | observer = _obs; |
| 303 | token = _token; |
| 304 | pkgInfo = null; |
| 305 | pmToken = 0; |
| 306 | needFullBackup = _needFullBackup; |
| 307 | filterSet = _filterSet; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 311 | class ClearParams { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 312 | public IBackupTransport transport; |
| 313 | public PackageInfo packageInfo; |
| 314 | |
| 315 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 316 | transport = _transport; |
| 317 | packageInfo = _info; |
| 318 | } |
| 319 | } |
| 320 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 321 | class FullParams { |
| 322 | public ParcelFileDescriptor fd; |
| 323 | public final AtomicBoolean latch; |
| 324 | public IFullBackupRestoreObserver observer; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 325 | public String curPassword; // filled in by the confirmation step |
| 326 | public String encryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 327 | |
| 328 | FullParams() { |
| 329 | latch = new AtomicBoolean(false); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | class FullBackupParams extends FullParams { |
| 334 | public boolean includeApks; |
| 335 | public boolean includeShared; |
| 336 | public boolean allApps; |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 337 | public boolean includeSystem; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 338 | public String[] packages; |
| 339 | |
| 340 | FullBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveShared, |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 341 | boolean doAllApps, boolean doSystem, String[] pkgList) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 342 | fd = output; |
| 343 | includeApks = saveApks; |
| 344 | includeShared = saveShared; |
| 345 | allApps = doAllApps; |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 346 | includeSystem = doSystem; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 347 | packages = pkgList; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | class FullRestoreParams extends FullParams { |
| 352 | FullRestoreParams(ParcelFileDescriptor input) { |
| 353 | fd = input; |
| 354 | } |
| 355 | } |
| 356 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 357 | // Bookkeeping of in-flight operations for timeout etc. purposes. The operation |
| 358 | // token is the index of the entry in the pending-operations list. |
| 359 | static final int OP_PENDING = 0; |
| 360 | static final int OP_ACKNOWLEDGED = 1; |
| 361 | static final int OP_TIMEOUT = -1; |
| 362 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 363 | class Operation { |
| 364 | public int state; |
| 365 | public BackupRestoreTask callback; |
| 366 | |
| 367 | Operation(int initialState, BackupRestoreTask callbackObj) { |
| 368 | state = initialState; |
| 369 | callback = callbackObj; |
| 370 | } |
| 371 | } |
| 372 | final SparseArray<Operation> mCurrentOperations = new SparseArray<Operation>(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 373 | final Object mCurrentOpLock = new Object(); |
| 374 | final Random mTokenGenerator = new Random(); |
| 375 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 376 | final SparseArray<FullParams> mFullConfirmations = new SparseArray<FullParams>(); |
| 377 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 378 | // Where we keep our journal files and other bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 379 | File mBaseStateDir; |
| 380 | File mDataDir; |
| 381 | File mJournalDir; |
| 382 | File mJournal; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 383 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 384 | // Backup password, if any, and the file where it's saved. What is stored is not the |
| 385 | // password text itself; it's the result of a PBKDF2 hash with a randomly chosen (but |
| 386 | // persisted) salt. Validation is performed by running the challenge text through the |
| 387 | // same PBKDF2 cycle with the persisted salt; if the resulting derived key string matches |
| 388 | // the saved hash string, then the challenge text matches the originally supplied |
| 389 | // password text. |
| 390 | private final SecureRandom mRng = new SecureRandom(); |
| 391 | private String mPasswordHash; |
| 392 | private File mPasswordHashFile; |
| 393 | private byte[] mPasswordSalt; |
| 394 | |
| 395 | // Configuration of PBKDF2 that we use for generating pw hashes and intermediate keys |
| 396 | static final int PBKDF2_HASH_ROUNDS = 10000; |
| 397 | static final int PBKDF2_KEY_SIZE = 256; // bits |
| 398 | static final int PBKDF2_SALT_SIZE = 512; // bits |
| 399 | static final String ENCRYPTION_ALGORITHM_NAME = "AES-256"; |
| 400 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 401 | // Keep a log of all the apps we've ever backed up, and what the |
| 402 | // dataset tokens are for both the current backup dataset and |
| 403 | // the ancestral dataset. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 404 | private File mEverStored; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 405 | HashSet<String> mEverStoredApps = new HashSet<String>(); |
| 406 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 407 | 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] | 408 | File mTokenFile; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 409 | Set<String> mAncestralPackages = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 410 | long mAncestralToken = 0; |
| 411 | long mCurrentToken = 0; |
| 412 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 413 | // Persistently track the need to do a full init |
| 414 | static final String INIT_SENTINEL_FILE_NAME = "_need_init_"; |
| 415 | HashSet<String> mPendingInits = new HashSet<String>(); // transport names |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 416 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 417 | // Utility: build a new random integer token |
| 418 | int generateToken() { |
| 419 | int token; |
| 420 | do { |
| 421 | synchronized (mTokenGenerator) { |
| 422 | token = mTokenGenerator.nextInt(); |
| 423 | } |
| 424 | } while (token < 0); |
| 425 | return token; |
| 426 | } |
| 427 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 428 | // ----- Asynchronous backup/restore handler thread ----- |
| 429 | |
| 430 | private class BackupHandler extends Handler { |
| 431 | public BackupHandler(Looper looper) { |
| 432 | super(looper); |
| 433 | } |
| 434 | |
| 435 | public void handleMessage(Message msg) { |
| 436 | |
| 437 | switch (msg.what) { |
| 438 | case MSG_RUN_BACKUP: |
| 439 | { |
| 440 | mLastBackupPass = System.currentTimeMillis(); |
| 441 | mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL; |
| 442 | |
| 443 | IBackupTransport transport = getTransport(mCurrentTransport); |
| 444 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 445 | Slog.v(TAG, "Backup requested but no transport available"); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 446 | synchronized (mQueueLock) { |
| 447 | mBackupRunning = false; |
| 448 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 449 | mWakelock.release(); |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | // snapshot the pending-backup set and work on that |
| 454 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 455 | File oldJournal = mJournal; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 456 | synchronized (mQueueLock) { |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 457 | // Do we have any work to do? Construct the work queue |
| 458 | // then release the synchronization lock to actually run |
| 459 | // the backup. |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 460 | if (mPendingBackups.size() > 0) { |
| 461 | for (BackupRequest b: mPendingBackups.values()) { |
| 462 | queue.add(b); |
| 463 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 464 | if (DEBUG) Slog.v(TAG, "clearing pending backups"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 465 | mPendingBackups.clear(); |
| 466 | |
| 467 | // Start a new backup-queue journal file too |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 468 | mJournal = null; |
| 469 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 472 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 473 | // At this point, we have started a new journal file, and the old |
| 474 | // file identity is being passed to the backup processing task. |
| 475 | // When it completes successfully, that old journal file will be |
| 476 | // deleted. If we crash prior to that, the old journal is parsed |
| 477 | // at next boot and the journaled requests fulfilled. |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 478 | if (queue.size() > 0) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 479 | // Spin up a backup state sequence and set it running |
| 480 | PerformBackupTask pbt = new PerformBackupTask(transport, queue, oldJournal); |
| 481 | Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt); |
| 482 | sendMessage(pbtMessage); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 483 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 484 | Slog.v(TAG, "Backup requested but nothing pending"); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 485 | synchronized (mQueueLock) { |
| 486 | mBackupRunning = false; |
| 487 | } |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 488 | mWakelock.release(); |
| 489 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 493 | case MSG_BACKUP_RESTORE_STEP: |
| 494 | { |
| 495 | try { |
| 496 | BackupRestoreTask task = (BackupRestoreTask) msg.obj; |
| 497 | if (MORE_DEBUG) Slog.v(TAG, "Got next step for " + task + ", executing"); |
| 498 | task.execute(); |
| 499 | } catch (ClassCastException e) { |
| 500 | Slog.e(TAG, "Invalid backup task in flight, obj=" + msg.obj); |
| 501 | } |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | case MSG_OP_COMPLETE: |
| 506 | { |
| 507 | try { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 508 | BackupRestoreTask task = (BackupRestoreTask) msg.obj; |
| 509 | task.operationComplete(); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 510 | } catch (ClassCastException e) { |
| 511 | Slog.e(TAG, "Invalid completion in flight, obj=" + msg.obj); |
| 512 | } |
| 513 | break; |
| 514 | } |
| 515 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 516 | case MSG_RUN_FULL_BACKUP: |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 517 | { |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 518 | // TODO: refactor full backup to be a looper-based state machine |
| 519 | // similar to normal backup/restore. |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 520 | FullBackupParams params = (FullBackupParams)msg.obj; |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 521 | PerformFullBackupTask task = new PerformFullBackupTask(params.fd, |
| 522 | params.observer, params.includeApks, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 523 | params.includeShared, params.curPassword, params.encryptPassword, |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 524 | params.allApps, params.includeSystem, params.packages, params.latch); |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 525 | (new Thread(task)).start(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 526 | break; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 527 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 528 | |
| 529 | case MSG_RUN_RESTORE: |
| 530 | { |
| 531 | RestoreParams params = (RestoreParams)msg.obj; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 532 | Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 533 | PerformRestoreTask task = new PerformRestoreTask( |
| 534 | params.transport, params.observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 535 | params.token, params.pkgInfo, params.pmToken, |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 536 | params.needFullBackup, params.filterSet); |
| 537 | Message restoreMsg = obtainMessage(MSG_BACKUP_RESTORE_STEP, task); |
| 538 | sendMessage(restoreMsg); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 542 | case MSG_RUN_FULL_RESTORE: |
| 543 | { |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 544 | // TODO: refactor full restore to be a looper-based state machine |
| 545 | // similar to normal backup/restore. |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 546 | FullRestoreParams params = (FullRestoreParams)msg.obj; |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 547 | PerformFullRestoreTask task = new PerformFullRestoreTask(params.fd, |
| 548 | params.curPassword, params.encryptPassword, |
| 549 | params.observer, params.latch); |
| 550 | (new Thread(task)).start(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 554 | case MSG_RUN_CLEAR: |
| 555 | { |
| 556 | ClearParams params = (ClearParams)msg.obj; |
| 557 | (new PerformClearTask(params.transport, params.packageInfo)).run(); |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | case MSG_RUN_INITIALIZE: |
| 562 | { |
| 563 | HashSet<String> queue; |
| 564 | |
| 565 | // Snapshot the pending-init queue and work on that |
| 566 | synchronized (mQueueLock) { |
| 567 | queue = new HashSet<String>(mPendingInits); |
| 568 | mPendingInits.clear(); |
| 569 | } |
| 570 | |
| 571 | (new PerformInitializeTask(queue)).run(); |
| 572 | break; |
| 573 | } |
| 574 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 575 | case MSG_RUN_GET_RESTORE_SETS: |
| 576 | { |
| 577 | // Like other async operations, this is entered with the wakelock held |
| 578 | RestoreSet[] sets = null; |
| 579 | RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj; |
| 580 | try { |
| 581 | sets = params.transport.getAvailableRestoreSets(); |
| 582 | // cache the result in the active session |
| 583 | synchronized (params.session) { |
| 584 | params.session.mRestoreSets = sets; |
| 585 | } |
| 586 | if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 587 | } catch (Exception e) { |
| 588 | Slog.e(TAG, "Error from transport getting set list"); |
| 589 | } finally { |
| 590 | if (params.observer != null) { |
| 591 | try { |
| 592 | params.observer.restoreSetsAvailable(sets); |
| 593 | } catch (RemoteException re) { |
| 594 | Slog.e(TAG, "Unable to report listing to observer"); |
| 595 | } catch (Exception e) { |
| 596 | Slog.e(TAG, "Restore observer threw", e); |
| 597 | } |
| 598 | } |
| 599 | |
| Christopher Tate | 2a93509 | 2011-03-03 17:30:32 -0800 | [diff] [blame] | 600 | // Done: reset the session timeout clock |
| 601 | removeMessages(MSG_RESTORE_TIMEOUT); |
| 602 | sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| 603 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 604 | mWakelock.release(); |
| 605 | } |
| 606 | break; |
| 607 | } |
| 608 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 609 | case MSG_TIMEOUT: |
| 610 | { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 611 | handleTimeout(msg.arg1, msg.obj); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 612 | break; |
| 613 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 614 | |
| 615 | case MSG_RESTORE_TIMEOUT: |
| 616 | { |
| 617 | synchronized (BackupManagerService.this) { |
| 618 | if (mActiveRestoreSession != null) { |
| 619 | // Client app left the restore session dangling. We know that it |
| 620 | // can't be in the middle of an actual restore operation because |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 621 | // the timeout is suspended while a restore is in progress. Clean |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 622 | // up now. |
| 623 | Slog.w(TAG, "Restore session timed out; aborting"); |
| 624 | post(mActiveRestoreSession.new EndRestoreRunnable( |
| 625 | BackupManagerService.this, mActiveRestoreSession)); |
| 626 | } |
| 627 | } |
| 628 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 629 | |
| 630 | case MSG_FULL_CONFIRMATION_TIMEOUT: |
| 631 | { |
| 632 | synchronized (mFullConfirmations) { |
| 633 | FullParams params = mFullConfirmations.get(msg.arg1); |
| 634 | if (params != null) { |
| 635 | Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation"); |
| 636 | |
| 637 | // Release the waiter; timeout == completion |
| 638 | signalFullBackupRestoreCompletion(params); |
| 639 | |
| 640 | // Remove the token from the set |
| 641 | mFullConfirmations.delete(msg.arg1); |
| 642 | |
| 643 | // Report a timeout to the observer, if any |
| 644 | if (params.observer != null) { |
| 645 | try { |
| 646 | params.observer.onTimeout(); |
| 647 | } catch (RemoteException e) { |
| 648 | /* don't care if the app has gone away */ |
| 649 | } |
| 650 | } |
| 651 | } else { |
| 652 | Slog.d(TAG, "couldn't find params for token " + msg.arg1); |
| 653 | } |
| 654 | } |
| 655 | break; |
| 656 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 661 | // ----- Debug-only backup operation trace ----- |
| 662 | void addBackupTrace(String s) { |
| 663 | if (DEBUG_BACKUP_TRACE) { |
| 664 | synchronized (mBackupTrace) { |
| 665 | mBackupTrace.add(s); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | void clearBackupTrace() { |
| 671 | if (DEBUG_BACKUP_TRACE) { |
| 672 | synchronized (mBackupTrace) { |
| 673 | mBackupTrace.clear(); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 678 | // ----- Main service implementation ----- |
| 679 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 680 | public BackupManagerService(Context context) { |
| 681 | mContext = context; |
| 682 | mPackageManager = context.getPackageManager(); |
| Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 683 | mPackageManagerBinder = AppGlobals.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 684 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 685 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 686 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 687 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 688 | mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 689 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 690 | mBackupManagerBinder = asInterface(asBinder()); |
| 691 | |
| 692 | // spin up the backup/restore handler thread |
| 693 | mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 694 | mHandlerThread.start(); |
| 695 | mBackupHandler = new BackupHandler(mHandlerThread.getLooper()); |
| 696 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 697 | // Set up our bookkeeping |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 698 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 699 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 700 | mProvisioned = Settings.Secure.getInt(context.getContentResolver(), |
| Joe Onorato | ab9a2a5 | 2009-07-27 08:56:39 -0700 | [diff] [blame] | 701 | Settings.Secure.BACKUP_PROVISIONED, 0) != 0; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 702 | mAutoRestore = Settings.Secure.getInt(context.getContentResolver(), |
| Christopher Tate | 5035fda | 2010-02-25 18:01:14 -0800 | [diff] [blame] | 703 | Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 704 | // If Encrypted file systems is enabled or disabled, this call will return the |
| 705 | // correct directory. |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 706 | mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 707 | mBaseStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 708 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 709 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 710 | mPasswordHashFile = new File(mBaseStateDir, "pwhash"); |
| 711 | if (mPasswordHashFile.exists()) { |
| 712 | FileInputStream fin = null; |
| 713 | DataInputStream in = null; |
| 714 | try { |
| 715 | fin = new FileInputStream(mPasswordHashFile); |
| 716 | in = new DataInputStream(new BufferedInputStream(fin)); |
| 717 | // integer length of the salt array, followed by the salt, |
| 718 | // then the hex pw hash string |
| 719 | int saltLen = in.readInt(); |
| 720 | byte[] salt = new byte[saltLen]; |
| 721 | in.readFully(salt); |
| 722 | mPasswordHash = in.readUTF(); |
| 723 | mPasswordSalt = salt; |
| 724 | } catch (IOException e) { |
| 725 | Slog.e(TAG, "Unable to read saved backup pw hash"); |
| 726 | } finally { |
| 727 | try { |
| 728 | if (in != null) in.close(); |
| 729 | if (fin != null) fin.close(); |
| 730 | } catch (IOException e) { |
| 731 | Slog.w(TAG, "Unable to close streams"); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 736 | // Alarm receivers for scheduled backups & initialization operations |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 737 | mRunBackupReceiver = new RunBackupReceiver(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 738 | IntentFilter filter = new IntentFilter(); |
| 739 | filter.addAction(RUN_BACKUP_ACTION); |
| 740 | context.registerReceiver(mRunBackupReceiver, filter, |
| 741 | android.Manifest.permission.BACKUP, null); |
| 742 | |
| 743 | mRunInitReceiver = new RunInitializeReceiver(); |
| 744 | filter = new IntentFilter(); |
| 745 | filter.addAction(RUN_INITIALIZE_ACTION); |
| 746 | context.registerReceiver(mRunInitReceiver, filter, |
| 747 | android.Manifest.permission.BACKUP, null); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 748 | |
| 749 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 750 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 751 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 752 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 753 | Intent initIntent = new Intent(RUN_INITIALIZE_ACTION); |
| 754 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 755 | mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0); |
| 756 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 757 | // Set up the backup-request journaling |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 758 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 759 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 760 | mJournal = null; // will be created on first use |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 761 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 762 | // Set up the various sorts of package tracking we do |
| 763 | initPackageTracking(); |
| 764 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 765 | // Build our mapping of uid to backup client services. This implicitly |
| 766 | // schedules a backup pass on the Package Manager metadata the first |
| 767 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 768 | synchronized (mBackupParticipants) { |
| 769 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 772 | // Set up our transport options and initialize the default transport |
| 773 | // TODO: Have transports register themselves somehow? |
| 774 | // TODO: Don't create transports that we don't need to? |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 775 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 776 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 777 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 778 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 779 | mGoogleTransport = null; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 780 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 781 | Settings.Secure.BACKUP_TRANSPORT); |
| 782 | if ("".equals(mCurrentTransport)) { |
| 783 | mCurrentTransport = null; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 784 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 785 | if (DEBUG) Slog.v(TAG, "Starting with transport " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 786 | |
| 787 | // Attach to the Google backup transport. When this comes up, it will set |
| 788 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 789 | // to null. |
| Christopher Tate | a32504f | 2010-04-21 17:58:07 -0700 | [diff] [blame] | 790 | ComponentName transportComponent = new ComponentName("com.google.android.backup", |
| 791 | "com.google.android.backup.BackupTransportService"); |
| 792 | try { |
| 793 | // If there's something out there that is supposed to be the Google |
| 794 | // backup transport, make sure it's legitimately part of the OS build |
| 795 | // and not an app lying about its package name. |
| 796 | ApplicationInfo info = mPackageManager.getApplicationInfo( |
| 797 | transportComponent.getPackageName(), 0); |
| 798 | if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 799 | if (DEBUG) Slog.v(TAG, "Binding to Google transport"); |
| 800 | Intent intent = new Intent().setComponent(transportComponent); |
| 801 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| 802 | } else { |
| 803 | Slog.w(TAG, "Possible Google transport spoof: ignoring " + info); |
| 804 | } |
| 805 | } catch (PackageManager.NameNotFoundException nnf) { |
| 806 | // No such package? No binding. |
| 807 | if (DEBUG) Slog.v(TAG, "Google transport not present"); |
| 808 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 809 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 810 | // Now that we know about valid backup participants, parse any |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 811 | // leftover journal files into the pending backup set |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 812 | parseLeftoverJournals(); |
| 813 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 814 | // Power management |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 815 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 816 | |
| 817 | // Start the backup passes going |
| 818 | setBackupEnabled(areEnabled); |
| 819 | } |
| 820 | |
| 821 | private class RunBackupReceiver extends BroadcastReceiver { |
| 822 | public void onReceive(Context context, Intent intent) { |
| 823 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 824 | synchronized (mQueueLock) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 825 | if (mPendingInits.size() > 0) { |
| 826 | // If there are pending init operations, we process those |
| 827 | // and then settle into the usual periodic backup schedule. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | if (DEBUG) Slog.v(TAG, "Init pending at scheduled backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 829 | try { |
| 830 | mAlarmManager.cancel(mRunInitIntent); |
| 831 | mRunInitIntent.send(); |
| 832 | } catch (PendingIntent.CanceledException ce) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 833 | Slog.e(TAG, "Run init intent cancelled"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 834 | // can't really do more than bail here |
| 835 | } |
| 836 | } else { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 837 | // Don't run backups now if we're disabled or not yet |
| 838 | // fully set up. |
| 839 | if (mEnabled && mProvisioned) { |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 840 | if (!mBackupRunning) { |
| 841 | if (DEBUG) Slog.v(TAG, "Running a backup pass"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 842 | |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 843 | // Acquire the wakelock and pass it to the backup thread. it will |
| 844 | // be released once backup concludes. |
| 845 | mBackupRunning = true; |
| 846 | mWakelock.acquire(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 847 | |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 848 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 849 | mBackupHandler.sendMessage(msg); |
| 850 | } else { |
| 851 | Slog.i(TAG, "Backup time but one already running"); |
| 852 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 853 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 854 | Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | private class RunInitializeReceiver extends BroadcastReceiver { |
| 863 | public void onReceive(Context context, Intent intent) { |
| 864 | if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) { |
| 865 | synchronized (mQueueLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 866 | if (DEBUG) Slog.v(TAG, "Running a device init"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 867 | |
| 868 | // Acquire the wakelock and pass it to the init thread. it will |
| 869 | // be released once init concludes. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 870 | mWakelock.acquire(); |
| 871 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 872 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 873 | mBackupHandler.sendMessage(msg); |
| 874 | } |
| 875 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 876 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 877 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 878 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 879 | private void initPackageTracking() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 880 | if (DEBUG) Slog.v(TAG, "Initializing package tracking"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 881 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 882 | // Remember our ancestral dataset |
| 883 | mTokenFile = new File(mBaseStateDir, "ancestral"); |
| 884 | try { |
| 885 | RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 886 | int version = tf.readInt(); |
| 887 | if (version == CURRENT_ANCESTRAL_RECORD_VERSION) { |
| 888 | mAncestralToken = tf.readLong(); |
| 889 | mCurrentToken = tf.readLong(); |
| 890 | |
| 891 | int numPackages = tf.readInt(); |
| 892 | if (numPackages >= 0) { |
| 893 | mAncestralPackages = new HashSet<String>(); |
| 894 | for (int i = 0; i < numPackages; i++) { |
| 895 | String pkgName = tf.readUTF(); |
| 896 | mAncestralPackages.add(pkgName); |
| 897 | } |
| 898 | } |
| 899 | } |
| Brad Fitzpatrick | 725d8f0 | 2010-11-15 11:12:42 -0800 | [diff] [blame] | 900 | tf.close(); |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 901 | } catch (FileNotFoundException fnf) { |
| 902 | // Probably innocuous |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 903 | Slog.v(TAG, "No ancestral data"); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 904 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 905 | Slog.w(TAG, "Unable to read token file", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 908 | // Keep a log of what apps we've ever backed up. Because we might have |
| 909 | // rebooted in the middle of an operation that was removing something from |
| 910 | // this log, we sanity-check its contents here and reconstruct it. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 911 | mEverStored = new File(mBaseStateDir, "processed"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 912 | File tempProcessedFile = new File(mBaseStateDir, "processed.new"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 913 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 914 | // If we were in the middle of removing something from the ever-backed-up |
| 915 | // file, there might be a transient "processed.new" file still present. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 916 | // Ignore it -- we'll validate "processed" against the current package set. |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 917 | if (tempProcessedFile.exists()) { |
| 918 | tempProcessedFile.delete(); |
| 919 | } |
| 920 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 921 | // If there are previous contents, parse them out then start a new |
| 922 | // file to continue the recordkeeping. |
| 923 | if (mEverStored.exists()) { |
| 924 | RandomAccessFile temp = null; |
| 925 | RandomAccessFile in = null; |
| 926 | |
| 927 | try { |
| 928 | temp = new RandomAccessFile(tempProcessedFile, "rws"); |
| 929 | in = new RandomAccessFile(mEverStored, "r"); |
| 930 | |
| 931 | while (true) { |
| 932 | PackageInfo info; |
| 933 | String pkg = in.readUTF(); |
| 934 | try { |
| 935 | info = mPackageManager.getPackageInfo(pkg, 0); |
| 936 | mEverStoredApps.add(pkg); |
| 937 | temp.writeUTF(pkg); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 938 | if (MORE_DEBUG) Slog.v(TAG, " + " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 939 | } catch (NameNotFoundException e) { |
| 940 | // nope, this package was uninstalled; don't include it |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 941 | if (MORE_DEBUG) Slog.v(TAG, " - " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } catch (EOFException e) { |
| 945 | // Once we've rewritten the backup history log, atomically replace the |
| 946 | // old one with the new one then reopen the file for continuing use. |
| 947 | if (!tempProcessedFile.renameTo(mEverStored)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 948 | Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 949 | } |
| 950 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 951 | Slog.e(TAG, "Error in processed file", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 952 | } finally { |
| 953 | try { if (temp != null) temp.close(); } catch (IOException e) {} |
| 954 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 955 | } |
| 956 | } |
| 957 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 958 | // Register for broadcasts about package install, etc., so we can |
| 959 | // update the provider list. |
| 960 | IntentFilter filter = new IntentFilter(); |
| 961 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 962 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 963 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 964 | filter.addDataScheme("package"); |
| 965 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 966 | // Register for events related to sdcard installation. |
| 967 | IntentFilter sdFilter = new IntentFilter(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 968 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 969 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 970 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 973 | private void parseLeftoverJournals() { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 974 | for (File f : mJournalDir.listFiles()) { |
| 975 | if (mJournal == null || f.compareTo(mJournal) != 0) { |
| 976 | // This isn't the current journal, so it must be a leftover. Read |
| 977 | // out the package names mentioned there and schedule them for |
| 978 | // backup. |
| 979 | RandomAccessFile in = null; |
| 980 | try { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 981 | Slog.i(TAG, "Found stale backup journal, scheduling"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 982 | in = new RandomAccessFile(f, "r"); |
| 983 | while (true) { |
| 984 | String packageName = in.readUTF(); |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 985 | Slog.i(TAG, " " + packageName); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 986 | dataChangedImpl(packageName); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 987 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 988 | } catch (EOFException e) { |
| 989 | // no more data; we're done |
| 990 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 991 | Slog.e(TAG, "Can't read " + f, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 992 | } finally { |
| 993 | // close/delete the file |
| 994 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 995 | f.delete(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 1001 | private SecretKey buildPasswordKey(String pw, byte[] salt, int rounds) { |
| 1002 | return buildCharArrayKey(pw.toCharArray(), salt, rounds); |
| 1003 | } |
| 1004 | |
| 1005 | private SecretKey buildCharArrayKey(char[] pwArray, byte[] salt, int rounds) { |
| 1006 | try { |
| 1007 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 1008 | KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE); |
| 1009 | return keyFactory.generateSecret(ks); |
| 1010 | } catch (InvalidKeySpecException e) { |
| 1011 | Slog.e(TAG, "Invalid key spec for PBKDF2!"); |
| 1012 | } catch (NoSuchAlgorithmException e) { |
| 1013 | Slog.e(TAG, "PBKDF2 unavailable!"); |
| 1014 | } |
| 1015 | return null; |
| 1016 | } |
| 1017 | |
| 1018 | private String buildPasswordHash(String pw, byte[] salt, int rounds) { |
| 1019 | SecretKey key = buildPasswordKey(pw, salt, rounds); |
| 1020 | if (key != null) { |
| 1021 | return byteArrayToHex(key.getEncoded()); |
| 1022 | } |
| 1023 | return null; |
| 1024 | } |
| 1025 | |
| 1026 | private String byteArrayToHex(byte[] data) { |
| 1027 | StringBuilder buf = new StringBuilder(data.length * 2); |
| 1028 | for (int i = 0; i < data.length; i++) { |
| 1029 | buf.append(Byte.toHexString(data[i], true)); |
| 1030 | } |
| 1031 | return buf.toString(); |
| 1032 | } |
| 1033 | |
| 1034 | private byte[] hexToByteArray(String digits) { |
| 1035 | final int bytes = digits.length() / 2; |
| 1036 | if (2*bytes != digits.length()) { |
| 1037 | throw new IllegalArgumentException("Hex string must have an even number of digits"); |
| 1038 | } |
| 1039 | |
| 1040 | byte[] result = new byte[bytes]; |
| 1041 | for (int i = 0; i < digits.length(); i += 2) { |
| 1042 | result[i/2] = (byte) Integer.parseInt(digits.substring(i, i+2), 16); |
| 1043 | } |
| 1044 | return result; |
| 1045 | } |
| 1046 | |
| 1047 | private byte[] makeKeyChecksum(byte[] pwBytes, byte[] salt, int rounds) { |
| 1048 | char[] mkAsChar = new char[pwBytes.length]; |
| 1049 | for (int i = 0; i < pwBytes.length; i++) { |
| 1050 | mkAsChar[i] = (char) pwBytes[i]; |
| 1051 | } |
| 1052 | |
| 1053 | Key checksum = buildCharArrayKey(mkAsChar, salt, rounds); |
| 1054 | return checksum.getEncoded(); |
| 1055 | } |
| 1056 | |
| 1057 | // Used for generating random salts or passwords |
| 1058 | private byte[] randomBytes(int bits) { |
| 1059 | byte[] array = new byte[bits / 8]; |
| 1060 | mRng.nextBytes(array); |
| 1061 | return array; |
| 1062 | } |
| 1063 | |
| 1064 | // Backup password management |
| 1065 | boolean passwordMatchesSaved(String candidatePw, int rounds) { |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 1066 | // First, on an encrypted device we require matching the device pw |
| 1067 | final boolean isEncrypted; |
| 1068 | try { |
| 1069 | isEncrypted = (mMountService.getEncryptionState() != MountService.ENCRYPTION_STATE_NONE); |
| 1070 | if (isEncrypted) { |
| 1071 | if (DEBUG) { |
| 1072 | Slog.i(TAG, "Device encrypted; verifying against device data pw"); |
| 1073 | } |
| 1074 | // 0 means the password validated |
| 1075 | // -2 means device not encrypted |
| 1076 | // Any other result is either password failure or an error condition, |
| 1077 | // so we refuse the match |
| 1078 | final int result = mMountService.verifyEncryptionPassword(candidatePw); |
| 1079 | if (result == 0) { |
| 1080 | if (MORE_DEBUG) Slog.d(TAG, "Pw verifies"); |
| 1081 | return true; |
| 1082 | } else if (result != -2) { |
| 1083 | if (MORE_DEBUG) Slog.d(TAG, "Pw mismatch"); |
| 1084 | return false; |
| 1085 | } else { |
| 1086 | // ...else the device is supposedly not encrypted. HOWEVER, the |
| 1087 | // query about the encryption state said that the device *is* |
| 1088 | // encrypted, so ... we may have a problem. Log it and refuse |
| 1089 | // the backup. |
| 1090 | Slog.e(TAG, "verified encryption state mismatch against query; no match allowed"); |
| 1091 | return false; |
| 1092 | } |
| 1093 | } |
| 1094 | } catch (Exception e) { |
| 1095 | // Something went wrong talking to the mount service. This is very bad; |
| 1096 | // assume that we fail password validation. |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 1100 | if (mPasswordHash == null) { |
| 1101 | // no current password case -- require that 'currentPw' be null or empty |
| 1102 | if (candidatePw == null || "".equals(candidatePw)) { |
| 1103 | return true; |
| 1104 | } // else the non-empty candidate does not match the empty stored pw |
| 1105 | } else { |
| 1106 | // hash the stated current pw and compare to the stored one |
| 1107 | if (candidatePw != null && candidatePw.length() > 0) { |
| 1108 | String currentPwHash = buildPasswordHash(candidatePw, mPasswordSalt, rounds); |
| 1109 | if (mPasswordHash.equalsIgnoreCase(currentPwHash)) { |
| 1110 | // candidate hash matches the stored hash -- the password matches |
| 1111 | return true; |
| 1112 | } |
| 1113 | } // else the stored pw is nonempty but the candidate is empty; no match |
| 1114 | } |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | @Override |
| 1119 | public boolean setBackupPassword(String currentPw, String newPw) { |
| 1120 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1121 | "setBackupPassword"); |
| 1122 | |
| 1123 | // If the supplied pw doesn't hash to the the saved one, fail |
| 1124 | if (!passwordMatchesSaved(currentPw, PBKDF2_HASH_ROUNDS)) { |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
| 1128 | // Clearing the password is okay |
| 1129 | if (newPw == null || newPw.isEmpty()) { |
| 1130 | if (mPasswordHashFile.exists()) { |
| 1131 | if (!mPasswordHashFile.delete()) { |
| 1132 | // Unable to delete the old pw file, so fail |
| 1133 | Slog.e(TAG, "Unable to clear backup password"); |
| 1134 | return false; |
| 1135 | } |
| 1136 | } |
| 1137 | mPasswordHash = null; |
| 1138 | mPasswordSalt = null; |
| 1139 | return true; |
| 1140 | } |
| 1141 | |
| 1142 | try { |
| 1143 | // Okay, build the hash of the new backup password |
| 1144 | byte[] salt = randomBytes(PBKDF2_SALT_SIZE); |
| 1145 | String newPwHash = buildPasswordHash(newPw, salt, PBKDF2_HASH_ROUNDS); |
| 1146 | |
| 1147 | OutputStream pwf = null, buffer = null; |
| 1148 | DataOutputStream out = null; |
| 1149 | try { |
| 1150 | pwf = new FileOutputStream(mPasswordHashFile); |
| 1151 | buffer = new BufferedOutputStream(pwf); |
| 1152 | out = new DataOutputStream(buffer); |
| 1153 | // integer length of the salt array, followed by the salt, |
| 1154 | // then the hex pw hash string |
| 1155 | out.writeInt(salt.length); |
| 1156 | out.write(salt); |
| 1157 | out.writeUTF(newPwHash); |
| 1158 | out.flush(); |
| 1159 | mPasswordHash = newPwHash; |
| 1160 | mPasswordSalt = salt; |
| 1161 | return true; |
| 1162 | } finally { |
| 1163 | if (out != null) out.close(); |
| 1164 | if (buffer != null) buffer.close(); |
| 1165 | if (pwf != null) pwf.close(); |
| 1166 | } |
| 1167 | } catch (IOException e) { |
| 1168 | Slog.e(TAG, "Unable to set backup password"); |
| 1169 | } |
| 1170 | return false; |
| 1171 | } |
| 1172 | |
| 1173 | @Override |
| 1174 | public boolean hasBackupPassword() { |
| 1175 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1176 | "hasBackupPassword"); |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 1177 | |
| 1178 | try { |
| 1179 | return (mMountService.getEncryptionState() != IMountService.ENCRYPTION_STATE_NONE) |
| 1180 | || (mPasswordHash != null && mPasswordHash.length() > 0); |
| 1181 | } catch (Exception e) { |
| 1182 | // If we can't talk to the mount service we have a serious problem; fail |
| 1183 | // "secure" i.e. assuming that we require a password |
| 1184 | return true; |
| 1185 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1188 | // Maintain persistent state around whether need to do an initialize operation. |
| 1189 | // Must be called with the queue lock held. |
| 1190 | void recordInitPendingLocked(boolean isPending, String transportName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1191 | if (DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1192 | + " on transport " + transportName); |
| 1193 | try { |
| 1194 | IBackupTransport transport = getTransport(transportName); |
| 1195 | String transportDirName = transport.transportDirName(); |
| 1196 | File stateDir = new File(mBaseStateDir, transportDirName); |
| 1197 | File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1198 | |
| 1199 | if (isPending) { |
| 1200 | // We need an init before we can proceed with sending backup data. |
| 1201 | // Record that with an entry in our set of pending inits, as well as |
| 1202 | // journaling it via creation of a sentinel file. |
| 1203 | mPendingInits.add(transportName); |
| 1204 | try { |
| 1205 | (new FileOutputStream(initPendingFile)).close(); |
| 1206 | } catch (IOException ioe) { |
| 1207 | // Something is badly wrong with our permissions; just try to move on |
| 1208 | } |
| 1209 | } else { |
| 1210 | // No more initialization needed; wipe the journal and reset our state. |
| 1211 | initPendingFile.delete(); |
| 1212 | mPendingInits.remove(transportName); |
| 1213 | } |
| 1214 | } catch (RemoteException e) { |
| 1215 | // can't happen; the transport is local |
| 1216 | } |
| 1217 | } |
| 1218 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1219 | // Reset all of our bookkeeping, in response to having been told that |
| 1220 | // the backend data has been wiped [due to idle expiry, for example], |
| 1221 | // so we must re-upload all saved settings. |
| 1222 | void resetBackupState(File stateFileDir) { |
| 1223 | synchronized (mQueueLock) { |
| 1224 | // Wipe the "what we've ever backed up" tracking |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1225 | mEverStoredApps.clear(); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1226 | mEverStored.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1227 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1228 | mCurrentToken = 0; |
| 1229 | writeRestoreTokens(); |
| 1230 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1231 | // Remove all the state files |
| 1232 | for (File sf : stateFileDir.listFiles()) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1233 | // ... but don't touch the needs-init sentinel |
| 1234 | if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) { |
| 1235 | sf.delete(); |
| 1236 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1237 | } |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1238 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1239 | |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1240 | // Enqueue a new backup of every participant |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1241 | synchronized (mBackupParticipants) { |
| 1242 | int N = mBackupParticipants.size(); |
| 1243 | for (int i=0; i<N; i++) { |
| 1244 | int uid = mBackupParticipants.keyAt(i); |
| 1245 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 1246 | for (ApplicationInfo app: participants) { |
| 1247 | dataChangedImpl(app.packageName); |
| 1248 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1253 | // Add a transport to our set of available backends. If 'transport' is null, this |
| 1254 | // 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] | 1255 | private void registerTransport(String name, IBackupTransport transport) { |
| 1256 | synchronized (mTransports) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1257 | if (DEBUG) Slog.v(TAG, "Registering transport " + name + " = " + transport); |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1258 | if (transport != null) { |
| 1259 | mTransports.put(name, transport); |
| 1260 | } else { |
| 1261 | mTransports.remove(name); |
| Christopher Tate | b0dcaaf | 2010-01-29 16:27:04 -0800 | [diff] [blame] | 1262 | if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) { |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1263 | mCurrentTransport = null; |
| 1264 | } |
| 1265 | // Nothing further to do in the unregistration case |
| 1266 | return; |
| 1267 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1268 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1269 | |
| 1270 | // If the init sentinel file exists, we need to be sure to perform the init |
| 1271 | // as soon as practical. We also create the state directory at registration |
| 1272 | // time to ensure it's present from the outset. |
| 1273 | try { |
| 1274 | String transportName = transport.transportDirName(); |
| 1275 | File stateDir = new File(mBaseStateDir, transportName); |
| 1276 | stateDir.mkdirs(); |
| 1277 | |
| 1278 | File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1279 | if (initSentinel.exists()) { |
| 1280 | synchronized (mQueueLock) { |
| 1281 | mPendingInits.add(transportName); |
| 1282 | |
| 1283 | // TODO: pick a better starting time than now + 1 minute |
| 1284 | long delay = 1000 * 60; // one minute, in milliseconds |
| 1285 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 1286 | System.currentTimeMillis() + delay, mRunInitIntent); |
| 1287 | } |
| 1288 | } |
| 1289 | } catch (RemoteException e) { |
| 1290 | // can't happen, the transport is local |
| 1291 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1294 | // ----- Track installation/removal of packages ----- |
| 1295 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1296 | public void onReceive(Context context, Intent intent) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1297 | if (DEBUG) Slog.d(TAG, "Received broadcast " + intent); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1298 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1299 | String action = intent.getAction(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1300 | boolean replacing = false; |
| 1301 | boolean added = false; |
| 1302 | Bundle extras = intent.getExtras(); |
| 1303 | String pkgList[] = null; |
| 1304 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1305 | Intent.ACTION_PACKAGE_REMOVED.equals(action) || |
| 1306 | Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1307 | Uri uri = intent.getData(); |
| 1308 | if (uri == null) { |
| 1309 | return; |
| 1310 | } |
| 1311 | String pkgName = uri.getSchemeSpecificPart(); |
| 1312 | if (pkgName != null) { |
| 1313 | pkgList = new String[] { pkgName }; |
| 1314 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1315 | if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 1316 | // use the existing "add with replacement" logic |
| 1317 | if (MORE_DEBUG) Slog.d(TAG, "PACKAGE_REPLACED, updating package " + pkgName); |
| 1318 | added = replacing = true; |
| 1319 | } else { |
| 1320 | added = Intent.ACTION_PACKAGE_ADDED.equals(action); |
| 1321 | replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false); |
| 1322 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1323 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1324 | added = true; |
| 1325 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1326 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1327 | added = false; |
| 1328 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1329 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1330 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1331 | if (pkgList == null || pkgList.length == 0) { |
| 1332 | return; |
| 1333 | } |
| 1334 | if (added) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1335 | synchronized (mBackupParticipants) { |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1336 | if (replacing) { |
| 1337 | updatePackageParticipantsLocked(pkgList); |
| 1338 | } else { |
| 1339 | addPackageParticipantsLocked(pkgList); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1342 | } else { |
| 1343 | if (replacing) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1344 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 1345 | } else { |
| 1346 | synchronized (mBackupParticipants) { |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1347 | removePackageParticipantsLocked(pkgList); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | }; |
| 1353 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1354 | // ----- Track connection to GoogleBackupTransport service ----- |
| 1355 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 1356 | public void onServiceConnected(ComponentName name, IBinder service) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1357 | if (DEBUG) Slog.v(TAG, "Connected to Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1358 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1359 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | public void onServiceDisconnected(ComponentName name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1363 | if (DEBUG) Slog.v(TAG, "Disconnected from Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1364 | mGoogleTransport = null; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1365 | registerTransport(name.flattenToShortString(), null); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1366 | } |
| 1367 | }; |
| 1368 | |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1369 | // Add the backup agents in the given packages to our set of known backup participants. |
| 1370 | // If 'packageNames' is null, adds all backup agents in the whole system. |
| 1371 | void addPackageParticipantsLocked(String[] packageNames) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1372 | // Look for apps that define the android:backupAgent attribute |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1373 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1374 | if (packageNames != null) { |
| 1375 | if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: #" + packageNames.length); |
| 1376 | for (String packageName : packageNames) { |
| 1377 | addPackageParticipantsLockedInner(packageName, targetApps); |
| 1378 | } |
| 1379 | } else { |
| 1380 | if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: all"); |
| 1381 | addPackageParticipantsLockedInner(null, targetApps); |
| 1382 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1385 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1386 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1387 | if (MORE_DEBUG) { |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1388 | Slog.v(TAG, "Examining " + packageName + " for backup agent"); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1391 | for (PackageInfo pkg : targetPkgs) { |
| 1392 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 1393 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1394 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1395 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1396 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1397 | mBackupParticipants.put(uid, set); |
| 1398 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1399 | set.add(pkg.applicationInfo); |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1400 | if (MORE_DEBUG) Slog.v(TAG, "Agent found; added"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1401 | |
| 1402 | // If we've never seen this app before, schedule a backup for it |
| 1403 | if (!mEverStoredApps.contains(pkg.packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1404 | if (DEBUG) Slog.i(TAG, "New app " + pkg.packageName |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1405 | + " never backed up; scheduling"); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 1406 | dataChangedImpl(pkg.packageName); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1407 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1408 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1412 | // Remove the given packages' entries from our known active set. |
| 1413 | void removePackageParticipantsLocked(String[] packageNames) { |
| 1414 | if (packageNames == null) { |
| 1415 | Slog.w(TAG, "removePackageParticipants with null list"); |
| 1416 | return; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1417 | } |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1418 | |
| 1419 | if (DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: #" + packageNames.length); |
| 1420 | List<PackageInfo> knownPackages = allAgentPackages(); |
| 1421 | for (String pkg : packageNames) { |
| 1422 | removePackageParticipantsLockedInner(pkg, knownPackages); |
| 1423 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1426 | private void removePackageParticipantsLockedInner(String packageName, |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1427 | List<PackageInfo> allPackages) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1428 | if (MORE_DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1429 | Slog.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1430 | + ") removing from " + allPackages.size() + " entries"); |
| 1431 | for (PackageInfo p : allPackages) { |
| 1432 | Slog.v(TAG, " - " + p.packageName); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1433 | } |
| 1434 | } |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1435 | for (PackageInfo pkg : allPackages) { |
| 1436 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 1437 | /* |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1438 | int uid = -1; |
| 1439 | try { |
| 1440 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1441 | uid = info.applicationInfo.uid; |
| 1442 | } catch (NameNotFoundException e) { |
| 1443 | // we don't know this package name, so just skip it for now |
| 1444 | continue; |
| 1445 | } |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1446 | */ |
| 1447 | final int uid = pkg.applicationInfo.uid; |
| 1448 | if (MORE_DEBUG) Slog.i(TAG, " found pkg " + packageName + " uid=" + uid); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1449 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1450 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1451 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1452 | // Find the existing entry with the same package name, and remove it. |
| 1453 | // We can't just remove(app) because the instances are different. |
| 1454 | for (ApplicationInfo entry: set) { |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1455 | if (MORE_DEBUG) Slog.i(TAG, " checking against " + entry.packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1456 | if (entry.packageName.equals(pkg)) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1457 | if (MORE_DEBUG) Slog.v(TAG, " removing participant " + pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1458 | set.remove(entry); |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1459 | removeEverBackedUp(pkg.packageName); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1460 | break; |
| 1461 | } |
| 1462 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1463 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1464 | mBackupParticipants.delete(uid); |
| 1465 | } |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1466 | } else { |
| 1467 | if (MORE_DEBUG) Slog.i(TAG, " ... not found in uid mapping"); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1473 | // Returns the set of all applications that define an android:backupAgent attribute |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1474 | List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1475 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1476 | int flags = PackageManager.GET_SIGNATURES; |
| 1477 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 1478 | int N = packages.size(); |
| 1479 | for (int a = N-1; a >= 0; a--) { |
| Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1480 | PackageInfo pkg = packages.get(a); |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1481 | try { |
| 1482 | ApplicationInfo app = pkg.applicationInfo; |
| 1483 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| Christopher Tate | a87240c | 2010-02-12 14:12:34 -0800 | [diff] [blame] | 1484 | || app.backupAgentName == null) { |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1485 | packages.remove(a); |
| 1486 | } |
| 1487 | else { |
| 1488 | // we will need the shared library path, so look that up and store it here |
| 1489 | app = mPackageManager.getApplicationInfo(pkg.packageName, |
| 1490 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 1491 | pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles; |
| 1492 | } |
| 1493 | } catch (NameNotFoundException e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1494 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1495 | } |
| 1496 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1497 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1498 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1499 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1500 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 1501 | // action cannot be passed a null package name. |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1502 | void updatePackageParticipantsLocked(String[] packageNames) { |
| 1503 | if (packageNames == null) { |
| 1504 | Slog.e(TAG, "updatePackageParticipants called with null package list"); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1505 | return; |
| 1506 | } |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1507 | if (DEBUG) Slog.v(TAG, "updatePackageParticipantsLocked: #" + packageNames.length); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1508 | |
| Christopher Tate | 0bacfd2 | 2012-01-11 14:41:19 -0800 | [diff] [blame] | 1509 | if (packageNames.length > 0) { |
| 1510 | List<PackageInfo> allApps = allAgentPackages(); |
| 1511 | for (String packageName : packageNames) { |
| 1512 | removePackageParticipantsLockedInner(packageName, allApps); |
| 1513 | addPackageParticipantsLockedInner(packageName, allApps); |
| 1514 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1515 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1518 | // 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] | 1519 | // backed up at least once |
| 1520 | void logBackupComplete(String packageName) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1521 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return; |
| 1522 | |
| 1523 | synchronized (mEverStoredApps) { |
| 1524 | if (!mEverStoredApps.add(packageName)) return; |
| 1525 | |
| 1526 | RandomAccessFile out = null; |
| 1527 | try { |
| 1528 | out = new RandomAccessFile(mEverStored, "rws"); |
| 1529 | out.seek(out.length()); |
| 1530 | out.writeUTF(packageName); |
| 1531 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1532 | Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1533 | } finally { |
| 1534 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1539 | // Remove our awareness of having ever backed up the given package |
| 1540 | void removeEverBackedUp(String packageName) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1541 | if (DEBUG) Slog.v(TAG, "Removing backed-up knowledge of " + packageName); |
| 1542 | if (MORE_DEBUG) Slog.v(TAG, "New set:"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1543 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1544 | synchronized (mEverStoredApps) { |
| 1545 | // Rewrite the file and rename to overwrite. If we reboot in the middle, |
| 1546 | // we'll recognize on initialization time that the package no longer |
| 1547 | // exists and fix it up then. |
| 1548 | File tempKnownFile = new File(mBaseStateDir, "processed.new"); |
| 1549 | RandomAccessFile known = null; |
| 1550 | try { |
| 1551 | known = new RandomAccessFile(tempKnownFile, "rws"); |
| 1552 | mEverStoredApps.remove(packageName); |
| 1553 | for (String s : mEverStoredApps) { |
| 1554 | known.writeUTF(s); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1555 | if (MORE_DEBUG) Slog.v(TAG, " " + s); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1556 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1557 | known.close(); |
| 1558 | known = null; |
| 1559 | if (!tempKnownFile.renameTo(mEverStored)) { |
| 1560 | throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored); |
| 1561 | } |
| 1562 | } catch (IOException e) { |
| 1563 | // Bad: we couldn't create the new copy. For safety's sake we |
| 1564 | // abandon the whole process and remove all what's-backed-up |
| 1565 | // state entirely, meaning we'll force a backup pass for every |
| 1566 | // participant on the next boot or [re]install. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1567 | Slog.w(TAG, "Error rewriting " + mEverStored, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1568 | mEverStoredApps.clear(); |
| 1569 | tempKnownFile.delete(); |
| 1570 | mEverStored.delete(); |
| 1571 | } finally { |
| 1572 | try { if (known != null) known.close(); } catch (IOException e) {} |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1577 | // Persistently record the current and ancestral backup tokens as well |
| 1578 | // as the set of packages with data [supposedly] available in the |
| 1579 | // ancestral dataset. |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1580 | void writeRestoreTokens() { |
| 1581 | try { |
| 1582 | RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1583 | |
| 1584 | // First, the version number of this record, for futureproofing |
| 1585 | af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION); |
| 1586 | |
| 1587 | // Write the ancestral and current tokens |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1588 | af.writeLong(mAncestralToken); |
| 1589 | af.writeLong(mCurrentToken); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1590 | |
| 1591 | // Now write the set of ancestral packages |
| 1592 | if (mAncestralPackages == null) { |
| 1593 | af.writeInt(-1); |
| 1594 | } else { |
| 1595 | af.writeInt(mAncestralPackages.size()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1596 | if (DEBUG) Slog.v(TAG, "Ancestral packages: " + mAncestralPackages.size()); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1597 | for (String pkgName : mAncestralPackages) { |
| 1598 | af.writeUTF(pkgName); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1599 | if (MORE_DEBUG) Slog.v(TAG, " " + pkgName); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1600 | } |
| 1601 | } |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1602 | af.close(); |
| 1603 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1604 | Slog.w(TAG, "Unable to write token file:", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1608 | // Return the given transport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1609 | private IBackupTransport getTransport(String transportName) { |
| 1610 | synchronized (mTransports) { |
| 1611 | IBackupTransport transport = mTransports.get(transportName); |
| 1612 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1613 | Slog.w(TAG, "Requested unavailable transport: " + transportName); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1614 | } |
| 1615 | return transport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1616 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1619 | // fire off a backup agent, blocking until it attaches or times out |
| 1620 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 1621 | IBackupAgent agent = null; |
| 1622 | synchronized(mAgentConnectLock) { |
| 1623 | mConnecting = true; |
| 1624 | mConnectedAgent = null; |
| 1625 | try { |
| 1626 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1627 | Slog.d(TAG, "awaiting agent for " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1628 | |
| 1629 | // success; wait for the agent to arrive |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1630 | // only wait 10 seconds for the bind to happen |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1631 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1632 | while (mConnecting && mConnectedAgent == null |
| 1633 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1634 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1635 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1636 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1637 | // just bail |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1638 | if (DEBUG) Slog.w(TAG, "Interrupted: " + e); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1639 | return null; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | // if we timed out with no connect, abort and move on |
| 1644 | if (mConnecting == true) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1645 | Slog.w(TAG, "Timeout waiting for agent " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1646 | return null; |
| 1647 | } |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1648 | if (DEBUG) Slog.i(TAG, "got agent " + mConnectedAgent); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1649 | agent = mConnectedAgent; |
| 1650 | } |
| 1651 | } catch (RemoteException e) { |
| 1652 | // can't happen |
| 1653 | } |
| 1654 | } |
| 1655 | return agent; |
| 1656 | } |
| 1657 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1658 | // clear an application's data, blocking until the operation completes or times out |
| 1659 | void clearApplicationDataSynchronous(String packageName) { |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1660 | // Don't wipe packages marked allowClearUserData=false |
| 1661 | try { |
| 1662 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1663 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1664 | if (MORE_DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping " |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1665 | + packageName); |
| 1666 | return; |
| 1667 | } |
| 1668 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1669 | Slog.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1670 | return; |
| 1671 | } |
| 1672 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1673 | ClearDataObserver observer = new ClearDataObserver(); |
| 1674 | |
| 1675 | synchronized(mClearDataLock) { |
| 1676 | mClearingData = true; |
| Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1677 | try { |
| Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1678 | mActivityManager.clearApplicationUserData(packageName, observer, |
| 1679 | Binder.getOrigCallingUser()); |
| Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1680 | } catch (RemoteException e) { |
| 1681 | // can't happen because the activity manager is in this process |
| 1682 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1683 | |
| 1684 | // only wait 10 seconds for the clear data to happen |
| 1685 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1686 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 1687 | try { |
| 1688 | mClearDataLock.wait(5000); |
| 1689 | } catch (InterruptedException e) { |
| 1690 | // won't happen, but still. |
| 1691 | mClearingData = false; |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1698 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1699 | synchronized(mClearDataLock) { |
| 1700 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1701 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1706 | // Get the restore-set token for the best-available restore set for this package: |
| 1707 | // the active set if possible, else the ancestral one. Returns zero if none available. |
| 1708 | long getAvailableRestoreToken(String packageName) { |
| 1709 | long token = mAncestralToken; |
| 1710 | synchronized (mQueueLock) { |
| 1711 | if (mEverStoredApps.contains(packageName)) { |
| 1712 | token = mCurrentToken; |
| 1713 | } |
| 1714 | } |
| 1715 | return token; |
| 1716 | } |
| 1717 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1718 | // ----- |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1719 | // Interface and methods used by the asynchronous-with-timeout backup/restore operations |
| 1720 | |
| 1721 | interface BackupRestoreTask { |
| 1722 | // Execute one tick of whatever state machine the task implements |
| 1723 | void execute(); |
| 1724 | |
| 1725 | // An operation that wanted a callback has completed |
| 1726 | void operationComplete(); |
| 1727 | |
| 1728 | // An operation that wanted a callback has timed out |
| 1729 | void handleTimeout(); |
| 1730 | } |
| 1731 | |
| 1732 | void prepareOperationTimeout(int token, long interval, BackupRestoreTask callback) { |
| 1733 | if (MORE_DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token) |
| 1734 | + " interval=" + interval); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1735 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1736 | mCurrentOperations.put(token, new Operation(OP_PENDING, callback)); |
| 1737 | |
| 1738 | Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0, callback); |
| 1739 | mBackupHandler.sendMessageDelayed(msg, interval); |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | // synchronous waiter case |
| 1744 | boolean waitUntilOperationComplete(int token) { |
| 1745 | if (MORE_DEBUG) Slog.i(TAG, "Blocking until operation complete for " |
| 1746 | + Integer.toHexString(token)); |
| 1747 | int finalState = OP_PENDING; |
| 1748 | Operation op = null; |
| 1749 | synchronized (mCurrentOpLock) { |
| 1750 | while (true) { |
| 1751 | op = mCurrentOperations.get(token); |
| 1752 | if (op == null) { |
| 1753 | // mysterious disappearance: treat as success with no callback |
| 1754 | break; |
| 1755 | } else { |
| 1756 | if (op.state == OP_PENDING) { |
| 1757 | try { |
| 1758 | mCurrentOpLock.wait(); |
| 1759 | } catch (InterruptedException e) {} |
| 1760 | // When the wait is notified we loop around and recheck the current state |
| 1761 | } else { |
| 1762 | // No longer pending; we're done |
| 1763 | finalState = op.state; |
| 1764 | break; |
| 1765 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1766 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1767 | } |
| 1768 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1769 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1770 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1771 | if (MORE_DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1772 | + " complete: finalState=" + finalState); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1773 | return finalState == OP_ACKNOWLEDGED; |
| 1774 | } |
| 1775 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1776 | void handleTimeout(int token, Object obj) { |
| 1777 | // Notify any synchronous waiters |
| 1778 | Operation op = null; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1779 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1780 | op = mCurrentOperations.get(token); |
| 1781 | if (MORE_DEBUG) { |
| 1782 | if (op == null) Slog.w(TAG, "Timeout of token " + Integer.toHexString(token) |
| 1783 | + " but no op found"); |
| 1784 | } |
| 1785 | int state = (op != null) ? op.state : OP_TIMEOUT; |
| 1786 | if (state == OP_PENDING) { |
| 1787 | if (DEBUG) Slog.v(TAG, "TIMEOUT: token=" + Integer.toHexString(token)); |
| 1788 | op.state = OP_TIMEOUT; |
| 1789 | mCurrentOperations.put(token, op); |
| 1790 | } |
| 1791 | mCurrentOpLock.notifyAll(); |
| 1792 | } |
| 1793 | |
| 1794 | // If there's a TimeoutHandler for this event, call it |
| 1795 | if (op != null && op.callback != null) { |
| 1796 | op.callback.handleTimeout(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1797 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1800 | // ----- Back up a set of applications via a worker thread ----- |
| 1801 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1802 | enum BackupState { |
| 1803 | INITIAL, |
| 1804 | RUNNING_QUEUE, |
| 1805 | FINAL |
| 1806 | } |
| 1807 | |
| 1808 | class PerformBackupTask implements BackupRestoreTask { |
| 1809 | private static final String TAG = "PerformBackupTask"; |
| 1810 | |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1811 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1812 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1813 | ArrayList<BackupRequest> mOriginalQueue; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1814 | File mStateDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1815 | File mJournal; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1816 | BackupState mCurrentState; |
| 1817 | |
| 1818 | // carried information about the current in-flight operation |
| 1819 | PackageInfo mCurrentPackage; |
| 1820 | File mSavedStateName; |
| 1821 | File mBackupDataName; |
| 1822 | File mNewStateName; |
| 1823 | ParcelFileDescriptor mSavedState; |
| 1824 | ParcelFileDescriptor mBackupData; |
| 1825 | ParcelFileDescriptor mNewState; |
| 1826 | int mStatus; |
| 1827 | boolean mFinished; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1828 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1829 | public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1830 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1831 | mTransport = transport; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1832 | mOriginalQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1833 | mJournal = journal; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1834 | |
| 1835 | try { |
| 1836 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1837 | } catch (RemoteException e) { |
| 1838 | // can't happen; the transport is local |
| 1839 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1840 | |
| 1841 | mCurrentState = BackupState.INITIAL; |
| 1842 | mFinished = false; |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1843 | |
| 1844 | addBackupTrace("STATE => INITIAL"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1847 | // Main entry point: perform one chunk of work, updating the state as appropriate |
| 1848 | // and reposting the next chunk to the primary backup handler thread. |
| 1849 | @Override |
| 1850 | public void execute() { |
| 1851 | switch (mCurrentState) { |
| 1852 | case INITIAL: |
| 1853 | beginBackup(); |
| 1854 | break; |
| 1855 | |
| 1856 | case RUNNING_QUEUE: |
| 1857 | invokeNextAgent(); |
| 1858 | break; |
| 1859 | |
| 1860 | case FINAL: |
| 1861 | if (!mFinished) finalizeBackup(); |
| 1862 | else { |
| 1863 | Slog.e(TAG, "Duplicate finish"); |
| 1864 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 1865 | mFinished = true; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1866 | break; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | // We're starting a backup pass. Initialize the transport and send |
| 1871 | // the PM metadata blob if we haven't already. |
| 1872 | void beginBackup() { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1873 | if (DEBUG_BACKUP_TRACE) { |
| 1874 | clearBackupTrace(); |
| 1875 | StringBuilder b = new StringBuilder(256); |
| 1876 | b.append("beginBackup: ["); |
| 1877 | for (BackupRequest req : mOriginalQueue) { |
| 1878 | b.append(' '); |
| 1879 | b.append(req.packageName); |
| 1880 | } |
| 1881 | b.append(" ]"); |
| 1882 | addBackupTrace(b.toString()); |
| 1883 | } |
| 1884 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1885 | mStatus = BackupConstants.TRANSPORT_OK; |
| 1886 | |
| 1887 | // Sanity check: if the queue is empty we have no work to do. |
| 1888 | if (mOriginalQueue.isEmpty()) { |
| 1889 | Slog.w(TAG, "Backup begun with an empty queue - nothing to do."); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1890 | addBackupTrace("queue empty at begin"); |
| 1891 | executeNextState(BackupState.FINAL); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1892 | return; |
| 1893 | } |
| 1894 | |
| 1895 | // We need to retain the original queue contents in case of transport |
| 1896 | // failure, but we want a working copy that we can manipulate along |
| 1897 | // the way. |
| 1898 | mQueue = (ArrayList<BackupRequest>) mOriginalQueue.clone(); |
| 1899 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1900 | if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1901 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1902 | File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1903 | try { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1904 | final String transportName = mTransport.transportDirName(); |
| 1905 | EventLog.writeEvent(EventLogTags.BACKUP_START, transportName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1906 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1907 | // If we haven't stored package manager metadata yet, we must init the transport. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1908 | if (mStatus == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1909 | Slog.i(TAG, "Initializing (wiping) backup state and transport storage"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1910 | addBackupTrace("initializing transport " + transportName); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1911 | resetBackupState(mStateDir); // Just to make sure. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1912 | mStatus = mTransport.initializeDevice(); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1913 | |
| 1914 | addBackupTrace("transport.initializeDevice() == " + mStatus); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1915 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1916 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1917 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1918 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1919 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1920 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1921 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1922 | |
| 1923 | // The package manager doesn't have a proper <application> etc, but since |
| 1924 | // it's running here in the system process we can just set up its agent |
| 1925 | // directly and use a synthetic BackupRequest. We always run this pass |
| 1926 | // because it's cheap and this way we guarantee that we don't get out of |
| 1927 | // step even if we're selecting among various transports at run time. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1928 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1929 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1930 | mPackageManager, allAgentPackages()); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1931 | mStatus = invokeAgentForBackup(PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1932 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1933 | addBackupTrace("PMBA invoke: " + mStatus); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1934 | } |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1935 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1936 | if (mStatus == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| 1937 | // The backend reports that our dataset has been wiped. Note this in |
| 1938 | // the event log; the no-success code below will reset the backup |
| 1939 | // state as well. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1940 | EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1941 | } |
| 1942 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1943 | Slog.e(TAG, "Error in backup thread", e); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1944 | addBackupTrace("Exception in backup thread: " + e); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1945 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1946 | } finally { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1947 | // If we've succeeded so far, invokeAgentForBackup() will have run the PM |
| 1948 | // metadata and its completion/timeout callback will continue the state |
| 1949 | // machine chain. If it failed that won't happen; we handle that now. |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1950 | addBackupTrace("exiting prelim: " + mStatus); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1951 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 1952 | // if things went wrong at this point, we need to |
| 1953 | // restage everything and try again later. |
| 1954 | resetBackupState(mStateDir); // Just to make sure. |
| 1955 | executeNextState(BackupState.FINAL); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1956 | } |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1957 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1960 | // Transport has been initialized and the PM metadata submitted successfully |
| 1961 | // if that was warranted. Now we process the single next thing in the queue. |
| 1962 | void invokeNextAgent() { |
| 1963 | mStatus = BackupConstants.TRANSPORT_OK; |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1964 | addBackupTrace("invoke q=" + mQueue.size()); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1965 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1966 | // Sanity check that we have work to do. If not, skip to the end where |
| 1967 | // we reestablish the wakelock invariants etc. |
| 1968 | if (mQueue.isEmpty()) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1969 | if (DEBUG) Slog.i(TAG, "queue now empty"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1970 | executeNextState(BackupState.FINAL); |
| 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | // pop the entry we're going to process on this step |
| 1975 | BackupRequest request = mQueue.get(0); |
| 1976 | mQueue.remove(0); |
| 1977 | |
| 1978 | Slog.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 1979 | addBackupTrace("launch agent for " + request.packageName); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1980 | |
| 1981 | // Verify that the requested app exists; it might be something that |
| 1982 | // requested a backup but was then uninstalled. The request was |
| 1983 | // journalled and rather than tamper with the journal it's safer |
| 1984 | // to sanity-check here. This also gives us the classname of the |
| 1985 | // package's backup agent. |
| 1986 | try { |
| 1987 | mCurrentPackage = mPackageManager.getPackageInfo(request.packageName, |
| 1988 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | 9c2efb3 | 2012-03-23 13:00:05 -0700 | [diff] [blame] | 1989 | if (mCurrentPackage.applicationInfo.backupAgentName == null) { |
| 1990 | // The manifest has changed but we had a stale backup request pending. |
| 1991 | // This won't happen again because the app won't be requesting further |
| 1992 | // backups. |
| 1993 | Slog.i(TAG, "Package " + request.packageName |
| 1994 | + " no longer supports backup; skipping"); |
| 1995 | addBackupTrace("skipping - no agent, completion is noop"); |
| 1996 | executeNextState(BackupState.RUNNING_QUEUE); |
| 1997 | return; |
| 1998 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1999 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2000 | IBackupAgent agent = null; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2001 | try { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2002 | mWakelock.setWorkSource(new WorkSource(mCurrentPackage.applicationInfo.uid)); |
| 2003 | agent = bindToAgentSynchronous(mCurrentPackage.applicationInfo, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2004 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2005 | addBackupTrace("agent bound; a? = " + (agent != null)); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2006 | if (agent != null) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2007 | mStatus = invokeAgentForBackup(request.packageName, agent, mTransport); |
| 2008 | // at this point we'll either get a completion callback from the |
| 2009 | // agent, or a timeout message on the main handler. either way, we're |
| 2010 | // done here as long as we're successful so far. |
| 2011 | } else { |
| 2012 | // Timeout waiting for the agent |
| 2013 | mStatus = BackupConstants.AGENT_ERROR; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2014 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2015 | } catch (SecurityException ex) { |
| 2016 | // Try for the next one. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2017 | Slog.d(TAG, "error in bind/backup", ex); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2018 | mStatus = BackupConstants.AGENT_ERROR; |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2019 | addBackupTrace("agent SE"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2020 | } |
| 2021 | } catch (NameNotFoundException e) { |
| 2022 | Slog.d(TAG, "Package does not exist; skipping"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2023 | addBackupTrace("no such package"); |
| 2024 | mStatus = BackupConstants.AGENT_UNKNOWN; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2025 | } finally { |
| 2026 | mWakelock.setWorkSource(null); |
| 2027 | |
| 2028 | // If there was an agent error, no timeout/completion handling will occur. |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2029 | // That means we need to direct to the next state ourselves. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2030 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 2031 | BackupState nextState = BackupState.RUNNING_QUEUE; |
| 2032 | |
| 2033 | // An agent-level failure means we reenqueue this one agent for |
| 2034 | // a later retry, but otherwise proceed normally. |
| 2035 | if (mStatus == BackupConstants.AGENT_ERROR) { |
| 2036 | if (MORE_DEBUG) Slog.i(TAG, "Agent failure for " + request.packageName |
| 2037 | + " - restaging"); |
| 2038 | dataChangedImpl(request.packageName); |
| 2039 | mStatus = BackupConstants.TRANSPORT_OK; |
| 2040 | if (mQueue.isEmpty()) nextState = BackupState.FINAL; |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2041 | } else if (mStatus == BackupConstants.AGENT_UNKNOWN) { |
| 2042 | // Failed lookup of the app, so we couldn't bring up an agent, but |
| 2043 | // we're otherwise fine. Just drop it and go on to the next as usual. |
| 2044 | mStatus = BackupConstants.TRANSPORT_OK; |
| 2045 | } else { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2046 | // Transport-level failure means we reenqueue everything |
| 2047 | revertAndEndBackup(); |
| 2048 | nextState = BackupState.FINAL; |
| 2049 | } |
| 2050 | |
| 2051 | executeNextState(nextState); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2052 | } else { |
| 2053 | addBackupTrace("expecting completion/timeout callback"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2057 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2058 | void finalizeBackup() { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2059 | addBackupTrace("finishing"); |
| 2060 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2061 | // Either backup was successful, in which case we of course do not need |
| 2062 | // this pass's journal any more; or it failed, in which case we just |
| 2063 | // re-enqueued all of these packages in the current active journal. |
| 2064 | // Either way, we no longer need this pass's journal. |
| 2065 | if (mJournal != null && !mJournal.delete()) { |
| 2066 | Slog.e(TAG, "Unable to remove backup journal file " + mJournal); |
| 2067 | } |
| 2068 | |
| 2069 | // If everything actually went through and this is the first time we've |
| 2070 | // done a backup, we can now record what the current backup dataset token |
| 2071 | // is. |
| 2072 | if ((mCurrentToken == 0) && (mStatus == BackupConstants.TRANSPORT_OK)) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2073 | addBackupTrace("success; recording token"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2074 | try { |
| 2075 | mCurrentToken = mTransport.getCurrentRestoreSet(); |
| 2076 | } catch (RemoteException e) {} // can't happen |
| 2077 | writeRestoreTokens(); |
| 2078 | } |
| 2079 | |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 2080 | // Set up the next backup pass - at this point we can set mBackupRunning |
| 2081 | // to false to allow another pass to fire, because we're done with the |
| 2082 | // state machine sequence and the wakelock is refcounted. |
| 2083 | synchronized (mQueueLock) { |
| 2084 | mBackupRunning = false; |
| 2085 | if (mStatus == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| Christopher Tate | e659fb9 | 2011-10-10 16:34:50 -0700 | [diff] [blame] | 2086 | // Make sure we back up everything and perform the one-time init |
| 2087 | clearMetadata(); |
| 2088 | if (DEBUG) Slog.d(TAG, "Server requires init; rerunning"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2089 | addBackupTrace("init required; rerunning"); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 2090 | backupNow(); |
| 2091 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | // Only once we're entirely finished do we release the wakelock |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2095 | clearBackupTrace(); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2096 | Slog.i(TAG, "Backup pass finished."); |
| 2097 | mWakelock.release(); |
| 2098 | } |
| 2099 | |
| Christopher Tate | e659fb9 | 2011-10-10 16:34:50 -0700 | [diff] [blame] | 2100 | // Remove the PM metadata state. This will generate an init on the next pass. |
| 2101 | void clearMetadata() { |
| 2102 | final File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| 2103 | if (pmState.exists()) pmState.delete(); |
| 2104 | } |
| 2105 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2106 | // Invoke an agent's doBackup() and start a timeout message spinning on the main |
| 2107 | // handler in case it doesn't get back to us. |
| 2108 | int invokeAgentForBackup(String packageName, IBackupAgent agent, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2109 | IBackupTransport transport) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2110 | if (DEBUG) Slog.d(TAG, "invokeAgentForBackup on " + packageName); |
| 2111 | addBackupTrace("invoking " + packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2112 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2113 | mSavedStateName = new File(mStateDir, packageName); |
| 2114 | mBackupDataName = new File(mDataDir, packageName + ".data"); |
| 2115 | mNewStateName = new File(mStateDir, packageName + ".new"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2116 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2117 | mSavedState = null; |
| 2118 | mBackupData = null; |
| 2119 | mNewState = null; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2120 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2121 | final int token = generateToken(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2122 | try { |
| 2123 | // Look up the package info & signatures. This is first so that if it |
| 2124 | // throws an exception, there's no file setup yet that would need to |
| 2125 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 2126 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2127 | // The metadata 'package' is synthetic; construct one and make |
| 2128 | // sure our global state is pointed at it |
| 2129 | mCurrentPackage = new PackageInfo(); |
| 2130 | mCurrentPackage.packageName = packageName; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 2131 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2132 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2133 | // In a full backup, we pass a null ParcelFileDescriptor as |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2134 | // the saved-state "file". This is by definition an incremental, |
| 2135 | // so we build a saved state file to pass. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2136 | mSavedState = ParcelFileDescriptor.open(mSavedStateName, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2137 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 2138 | ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2139 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2140 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2141 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 2142 | ParcelFileDescriptor.MODE_CREATE | |
| 2143 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2144 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2145 | mNewState = ParcelFileDescriptor.open(mNewStateName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2146 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 2147 | ParcelFileDescriptor.MODE_CREATE | |
| 2148 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2149 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 2150 | // Initiate the target's backup pass |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2151 | addBackupTrace("setting timeout"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2152 | prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL, this); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2153 | addBackupTrace("calling agent doBackup()"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2154 | agent.doBackup(mSavedState, mBackupData, mNewState, token, mBackupManagerBinder); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2155 | } catch (Exception e) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2156 | Slog.e(TAG, "Error invoking for backup on " + packageName); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2157 | addBackupTrace("exception: " + e); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2158 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, |
| 2159 | e.toString()); |
| 2160 | agentErrorCleanup(); |
| 2161 | return BackupConstants.AGENT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2164 | // At this point the agent is off and running. The next thing to happen will |
| 2165 | // either be a callback from the agent, at which point we'll process its data |
| 2166 | // for transport, or a timeout. Either way the next phase will happen in |
| 2167 | // response to the TimeoutHandler interface callbacks. |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2168 | addBackupTrace("invoke success"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2169 | return BackupConstants.TRANSPORT_OK; |
| 2170 | } |
| 2171 | |
| 2172 | @Override |
| 2173 | public void operationComplete() { |
| 2174 | // Okay, the agent successfully reported back to us. Spin the data off to the |
| 2175 | // transport and proceed with the next stage. |
| 2176 | if (MORE_DEBUG) Slog.v(TAG, "operationComplete(): sending data to transport for " |
| 2177 | + mCurrentPackage.packageName); |
| 2178 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 2179 | clearAgentState(); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2180 | addBackupTrace("operation complete"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2181 | |
| 2182 | ParcelFileDescriptor backupData = null; |
| 2183 | mStatus = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2184 | try { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2185 | int size = (int) mBackupDataName.length(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2186 | if (size > 0) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2187 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| 2188 | backupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2189 | ParcelFileDescriptor.MODE_READ_ONLY); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2190 | addBackupTrace("sending data to transport"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2191 | mStatus = mTransport.performBackup(mCurrentPackage, backupData); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2192 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2193 | |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 2194 | // TODO - We call finishBackup() for each application backed up, because |
| 2195 | // we need to know now whether it succeeded or failed. Instead, we should |
| 2196 | // hold off on finishBackup() until the end, which implies holding off on |
| 2197 | // renaming *all* the output state files (see below) until that happens. |
| 2198 | |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2199 | addBackupTrace("data delivered: " + mStatus); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2200 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2201 | addBackupTrace("finishing op on transport"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2202 | mStatus = mTransport.finishBackup(); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2203 | addBackupTrace("finished: " + mStatus); |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 2204 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2205 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2206 | if (DEBUG) Slog.i(TAG, "no backup data written; not calling transport"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2207 | addBackupTrace("no data to send"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // After successful transport, delete the now-stale data |
| 2211 | // and juggle the files so that next time we supply the agent |
| 2212 | // with the new state file it just created. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2213 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| 2214 | mBackupDataName.delete(); |
| 2215 | mNewStateName.renameTo(mSavedStateName); |
| 2216 | EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, |
| 2217 | mCurrentPackage.packageName, size); |
| 2218 | logBackupComplete(mCurrentPackage.packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2219 | } else { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2220 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, |
| 2221 | mCurrentPackage.packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2222 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2223 | } catch (Exception e) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2224 | Slog.e(TAG, "Transport error backing up " + mCurrentPackage.packageName, e); |
| 2225 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, |
| 2226 | mCurrentPackage.packageName); |
| 2227 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2228 | } finally { |
| 2229 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2230 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 2231 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2232 | // If we encountered an error here it's a transport-level failure. That |
| 2233 | // means we need to halt everything and reschedule everything for next time. |
| 2234 | final BackupState nextState; |
| 2235 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 2236 | revertAndEndBackup(); |
| 2237 | nextState = BackupState.FINAL; |
| 2238 | } else { |
| 2239 | // Success! Proceed with the next app if any, otherwise we're done. |
| 2240 | nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE; |
| 2241 | } |
| 2242 | |
| 2243 | executeNextState(nextState); |
| 2244 | } |
| 2245 | |
| 2246 | @Override |
| 2247 | public void handleTimeout() { |
| 2248 | // Whoops, the current agent timed out running doBackup(). Tidy up and restage |
| 2249 | // it for the next time we run a backup pass. |
| 2250 | // !!! TODO: keep track of failure counts per agent, and blacklist those which |
| 2251 | // fail repeatedly (i.e. have proved themselves to be buggy). |
| 2252 | Slog.e(TAG, "Timeout backing up " + mCurrentPackage.packageName); |
| 2253 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, mCurrentPackage.packageName, |
| 2254 | "timeout"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2255 | addBackupTrace("timeout of " + mCurrentPackage.packageName); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2256 | agentErrorCleanup(); |
| 2257 | dataChangedImpl(mCurrentPackage.packageName); |
| 2258 | } |
| 2259 | |
| 2260 | void revertAndEndBackup() { |
| 2261 | if (MORE_DEBUG) Slog.i(TAG, "Reverting backup queue - restaging everything"); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2262 | addBackupTrace("transport error; reverting"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2263 | for (BackupRequest request : mOriginalQueue) { |
| 2264 | dataChangedImpl(request.packageName); |
| 2265 | } |
| 2266 | // We also want to reset the backup schedule based on whatever |
| 2267 | // the transport suggests by way of retry/backoff time. |
| 2268 | restartBackupAlarm(); |
| 2269 | } |
| 2270 | |
| 2271 | void agentErrorCleanup() { |
| 2272 | mBackupDataName.delete(); |
| 2273 | mNewStateName.delete(); |
| 2274 | clearAgentState(); |
| 2275 | |
| 2276 | executeNextState(mQueue.isEmpty() ? BackupState.FINAL : BackupState.RUNNING_QUEUE); |
| 2277 | } |
| 2278 | |
| 2279 | // Cleanup common to both success and failure cases |
| 2280 | void clearAgentState() { |
| 2281 | try { if (mSavedState != null) mSavedState.close(); } catch (IOException e) {} |
| 2282 | try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {} |
| 2283 | try { if (mNewState != null) mNewState.close(); } catch (IOException e) {} |
| 2284 | mSavedState = mBackupData = mNewState = null; |
| 2285 | synchronized (mCurrentOpLock) { |
| 2286 | mCurrentOperations.clear(); |
| 2287 | } |
| 2288 | |
| 2289 | // If this was a pseudopackage there's no associated Activity Manager state |
| 2290 | if (mCurrentPackage.applicationInfo != null) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2291 | addBackupTrace("unbinding " + mCurrentPackage.packageName); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2292 | try { // unbind even on timeout, just in case |
| 2293 | mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo); |
| 2294 | } catch (RemoteException e) {} |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | void restartBackupAlarm() { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2299 | addBackupTrace("setting backup trigger"); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2300 | synchronized (mQueueLock) { |
| 2301 | try { |
| 2302 | startBackupAlarmsLocked(mTransport.requestBackupTime()); |
| 2303 | } catch (RemoteException e) { /* cannot happen */ } |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | void executeNextState(BackupState nextState) { |
| 2308 | if (MORE_DEBUG) Slog.i(TAG, " => executing next step on " |
| 2309 | + this + " nextState=" + nextState); |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 2310 | addBackupTrace("executeNextState => " + nextState); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2311 | mCurrentState = nextState; |
| 2312 | Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this); |
| 2313 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2314 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2317 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2318 | // ----- Full backup to a file/socket ----- |
| 2319 | |
| 2320 | class PerformFullBackupTask implements Runnable { |
| 2321 | ParcelFileDescriptor mOutputFile; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2322 | DeflaterOutputStream mDeflater; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2323 | IFullBackupRestoreObserver mObserver; |
| 2324 | boolean mIncludeApks; |
| 2325 | boolean mIncludeShared; |
| 2326 | boolean mAllApps; |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2327 | final boolean mIncludeSystem; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2328 | String[] mPackages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2329 | String mCurrentPassword; |
| 2330 | String mEncryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2331 | AtomicBoolean mLatchObject; |
| 2332 | File mFilesDir; |
| 2333 | File mManifestFile; |
| 2334 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2335 | class FullBackupRunner implements Runnable { |
| 2336 | PackageInfo mPackage; |
| 2337 | IBackupAgent mAgent; |
| 2338 | ParcelFileDescriptor mPipe; |
| 2339 | int mToken; |
| 2340 | boolean mSendApk; |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2341 | boolean mWriteManifest; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2342 | |
| 2343 | FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe, |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2344 | int token, boolean sendApk, boolean writeManifest) throws IOException { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2345 | mPackage = pack; |
| 2346 | mAgent = agent; |
| 2347 | mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor()); |
| 2348 | mToken = token; |
| 2349 | mSendApk = sendApk; |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2350 | mWriteManifest = writeManifest; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | @Override |
| 2354 | public void run() { |
| 2355 | try { |
| 2356 | BackupDataOutput output = new BackupDataOutput( |
| 2357 | mPipe.getFileDescriptor()); |
| 2358 | |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2359 | if (mWriteManifest) { |
| 2360 | if (MORE_DEBUG) Slog.d(TAG, "Writing manifest for " + mPackage.packageName); |
| 2361 | writeAppManifest(mPackage, mManifestFile, mSendApk); |
| 2362 | FullBackup.backupToTar(mPackage.packageName, null, null, |
| 2363 | mFilesDir.getAbsolutePath(), |
| 2364 | mManifestFile.getAbsolutePath(), |
| 2365 | output); |
| 2366 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2367 | |
| 2368 | if (mSendApk) { |
| 2369 | writeApkToBackup(mPackage, output); |
| 2370 | } |
| 2371 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2372 | if (DEBUG) Slog.d(TAG, "Calling doFullBackup() on " + mPackage.packageName); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2373 | prepareOperationTimeout(mToken, TIMEOUT_FULL_BACKUP_INTERVAL, null); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2374 | mAgent.doFullBackup(mPipe, mToken, mBackupManagerBinder); |
| 2375 | } catch (IOException e) { |
| 2376 | Slog.e(TAG, "Error running full backup for " + mPackage.packageName); |
| 2377 | } catch (RemoteException e) { |
| 2378 | Slog.e(TAG, "Remote agent vanished during full backup of " |
| 2379 | + mPackage.packageName); |
| 2380 | } finally { |
| 2381 | try { |
| 2382 | mPipe.close(); |
| 2383 | } catch (IOException e) {} |
| 2384 | } |
| 2385 | } |
| 2386 | } |
| 2387 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2388 | PerformFullBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2389 | boolean includeApks, boolean includeShared, String curPassword, |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2390 | String encryptPassword, boolean doAllApps, boolean doSystem, String[] packages, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2391 | AtomicBoolean latch) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2392 | mOutputFile = fd; |
| 2393 | mObserver = observer; |
| 2394 | mIncludeApks = includeApks; |
| 2395 | mIncludeShared = includeShared; |
| 2396 | mAllApps = doAllApps; |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2397 | mIncludeSystem = doSystem; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2398 | mPackages = packages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2399 | mCurrentPassword = curPassword; |
| 2400 | // when backing up, if there is a current backup password, we require that |
| 2401 | // the user use a nonempty encryption password as well. if one is supplied |
| 2402 | // in the UI we use that, but if the UI was left empty we fall back to the |
| 2403 | // current backup password (which was supplied by the user as well). |
| 2404 | if (encryptPassword == null || "".equals(encryptPassword)) { |
| 2405 | mEncryptPassword = curPassword; |
| 2406 | } else { |
| 2407 | mEncryptPassword = encryptPassword; |
| 2408 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2409 | mLatchObject = latch; |
| 2410 | |
| 2411 | mFilesDir = new File("/data/system"); |
| 2412 | mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME); |
| 2413 | } |
| 2414 | |
| 2415 | @Override |
| 2416 | public void run() { |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2417 | List<PackageInfo> packagesToBackup = new ArrayList<PackageInfo>(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2418 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2419 | Slog.i(TAG, "--- Performing full-dataset backup ---"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2420 | sendStartBackup(); |
| 2421 | |
| 2422 | // doAllApps supersedes the package set if any |
| 2423 | if (mAllApps) { |
| 2424 | packagesToBackup = mPackageManager.getInstalledPackages( |
| 2425 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2426 | // Exclude system apps if we've been asked to do so |
| 2427 | if (mIncludeSystem == false) { |
| 2428 | for (int i = 0; i < packagesToBackup.size(); ) { |
| 2429 | PackageInfo pkg = packagesToBackup.get(i); |
| 2430 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2431 | packagesToBackup.remove(i); |
| 2432 | } else { |
| 2433 | i++; |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | // Now process the command line argument packages, if any. Note that explicitly- |
| 2440 | // named system-partition packages will be included even if includeSystem was |
| 2441 | // set to false. |
| 2442 | if (mPackages != null) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2443 | for (String pkgName : mPackages) { |
| 2444 | try { |
| 2445 | packagesToBackup.add(mPackageManager.getPackageInfo(pkgName, |
| 2446 | PackageManager.GET_SIGNATURES)); |
| 2447 | } catch (NameNotFoundException e) { |
| 2448 | Slog.w(TAG, "Unknown package " + pkgName + ", skipping"); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2453 | // Cull any packages that have indicated that backups are not permitted, as well |
| 2454 | // as any explicit mention of the 'special' shared-storage agent package (we |
| 2455 | // handle that one at the end). |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2456 | for (int i = 0; i < packagesToBackup.size(); ) { |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 2457 | PackageInfo pkg = packagesToBackup.get(i); |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2458 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0 |
| 2459 | || pkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE)) { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2460 | packagesToBackup.remove(i); |
| 2461 | } else { |
| 2462 | i++; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2466 | FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2467 | OutputStream out = null; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2468 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2469 | PackageInfo pkg = null; |
| 2470 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2471 | boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2472 | boolean compressing = COMPRESS_FULL_BACKUPS; |
| 2473 | OutputStream finalOutput = ofstream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2474 | |
| Christopher Tate | eef4ae4 | 2011-08-05 13:15:53 -0700 | [diff] [blame] | 2475 | // Verify that the given password matches the currently-active |
| 2476 | // backup password, if any |
| 2477 | if (hasBackupPassword()) { |
| 2478 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2479 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2480 | return; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2484 | // Write the global file header. All strings are UTF-8 encoded; lines end |
| 2485 | // with a '\n' byte. Actual backup data begins immediately following the |
| 2486 | // final '\n'. |
| 2487 | // |
| 2488 | // line 1: "ANDROID BACKUP" |
| 2489 | // line 2: backup file format version, currently "1" |
| 2490 | // line 3: compressed? "0" if not compressed, "1" if compressed. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2491 | // line 4: name of encryption algorithm [currently only "none" or "AES-256"] |
| 2492 | // |
| 2493 | // When line 4 is not "none", then additional header data follows: |
| 2494 | // |
| 2495 | // line 5: user password salt [hex] |
| 2496 | // line 6: master key checksum salt [hex] |
| 2497 | // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal] |
| 2498 | // line 8: IV of the user key [hex] |
| 2499 | // line 9: master key blob [hex] |
| 2500 | // IV of the master key, master key itself, master key checksum hash |
| 2501 | // |
| 2502 | // The master key checksum is the master key plus its checksum salt, run through |
| 2503 | // 10k rounds of PBKDF2. This is used to verify that the user has supplied the |
| 2504 | // correct password for decrypting the archive: the master key decrypted from |
| 2505 | // the archive using the user-supplied password is also run through PBKDF2 in |
| 2506 | // this way, and if the result does not match the checksum as stored in the |
| 2507 | // archive, then we know that the user-supplied password does not match the |
| 2508 | // archive's. |
| 2509 | StringBuilder headerbuf = new StringBuilder(1024); |
| 2510 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2511 | headerbuf.append(BACKUP_FILE_HEADER_MAGIC); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2512 | headerbuf.append(BACKUP_FILE_VERSION); // integer, no trailing \n |
| 2513 | headerbuf.append(compressing ? "\n1\n" : "\n0\n"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2514 | |
| 2515 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2516 | // Set up the encryption stage if appropriate, and emit the correct header |
| 2517 | if (encrypting) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2518 | finalOutput = emitAesBackupHeader(headerbuf, finalOutput); |
| 2519 | } else { |
| 2520 | headerbuf.append("none\n"); |
| 2521 | } |
| 2522 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2523 | byte[] header = headerbuf.toString().getBytes("UTF-8"); |
| 2524 | ofstream.write(header); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2525 | |
| 2526 | // Set up the compression stage feeding into the encryption stage (if any) |
| 2527 | if (compressing) { |
| 2528 | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); |
| 2529 | finalOutput = new DeflaterOutputStream(finalOutput, deflater, true); |
| 2530 | } |
| 2531 | |
| 2532 | out = finalOutput; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2533 | } catch (Exception e) { |
| 2534 | // Should never happen! |
| 2535 | Slog.e(TAG, "Unable to emit archive header", e); |
| 2536 | return; |
| 2537 | } |
| 2538 | |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2539 | // Shared storage if requested |
| 2540 | if (mIncludeShared) { |
| 2541 | try { |
| 2542 | pkg = mPackageManager.getPackageInfo(SHARED_BACKUP_AGENT_PACKAGE, 0); |
| 2543 | packagesToBackup.add(pkg); |
| 2544 | } catch (NameNotFoundException e) { |
| 2545 | Slog.e(TAG, "Unable to find shared-storage backup handler"); |
| 2546 | } |
| 2547 | } |
| 2548 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2549 | // Now back up the app data via the agent mechanism |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2550 | int N = packagesToBackup.size(); |
| 2551 | for (int i = 0; i < N; i++) { |
| 2552 | pkg = packagesToBackup.get(i); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2553 | backupOnePackage(pkg, out); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2554 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2555 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2556 | // Done! |
| 2557 | finalizeBackup(out); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2558 | } catch (RemoteException e) { |
| 2559 | Slog.e(TAG, "App died during full backup"); |
| Christopher Tate | aa0c02d | 2012-03-23 13:56:34 -0700 | [diff] [blame] | 2560 | } catch (Exception e) { |
| 2561 | Slog.e(TAG, "Internal exception during full backup", e); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2562 | } finally { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2563 | tearDown(pkg); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2564 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2565 | if (out != null) out.close(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2566 | mOutputFile.close(); |
| 2567 | } catch (IOException e) { |
| 2568 | /* nothing we can do about this */ |
| 2569 | } |
| 2570 | synchronized (mCurrentOpLock) { |
| 2571 | mCurrentOperations.clear(); |
| 2572 | } |
| 2573 | synchronized (mLatchObject) { |
| 2574 | mLatchObject.set(true); |
| 2575 | mLatchObject.notifyAll(); |
| 2576 | } |
| 2577 | sendEndBackup(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2578 | if (DEBUG) Slog.d(TAG, "Full backup pass complete."); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 2579 | mWakelock.release(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2583 | private OutputStream emitAesBackupHeader(StringBuilder headerbuf, |
| 2584 | OutputStream ofstream) throws Exception { |
| 2585 | // User key will be used to encrypt the master key. |
| 2586 | byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2587 | SecretKey userKey = buildPasswordKey(mEncryptPassword, newUserSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2588 | PBKDF2_HASH_ROUNDS); |
| 2589 | |
| 2590 | // the master key is random for each backup |
| 2591 | byte[] masterPw = new byte[256 / 8]; |
| 2592 | mRng.nextBytes(masterPw); |
| 2593 | byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE); |
| 2594 | |
| 2595 | // primary encryption of the datastream with the random key |
| 2596 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2597 | SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES"); |
| 2598 | c.init(Cipher.ENCRYPT_MODE, masterKeySpec); |
| 2599 | OutputStream finalOutput = new CipherOutputStream(ofstream, c); |
| 2600 | |
| 2601 | // line 4: name of encryption algorithm |
| 2602 | headerbuf.append(ENCRYPTION_ALGORITHM_NAME); |
| 2603 | headerbuf.append('\n'); |
| 2604 | // line 5: user password salt [hex] |
| 2605 | headerbuf.append(byteArrayToHex(newUserSalt)); |
| 2606 | headerbuf.append('\n'); |
| 2607 | // line 6: master key checksum salt [hex] |
| 2608 | headerbuf.append(byteArrayToHex(checksumSalt)); |
| 2609 | headerbuf.append('\n'); |
| 2610 | // line 7: number of PBKDF2 rounds used [decimal] |
| 2611 | headerbuf.append(PBKDF2_HASH_ROUNDS); |
| 2612 | headerbuf.append('\n'); |
| 2613 | |
| 2614 | // line 8: IV of the user key [hex] |
| 2615 | Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2616 | mkC.init(Cipher.ENCRYPT_MODE, userKey); |
| 2617 | |
| 2618 | byte[] IV = mkC.getIV(); |
| 2619 | headerbuf.append(byteArrayToHex(IV)); |
| 2620 | headerbuf.append('\n'); |
| 2621 | |
| 2622 | // line 9: master IV + key blob, encrypted by the user key [hex]. Blob format: |
| 2623 | // [byte] IV length = Niv |
| 2624 | // [array of Niv bytes] IV itself |
| 2625 | // [byte] master key length = Nmk |
| 2626 | // [array of Nmk bytes] master key itself |
| 2627 | // [byte] MK checksum hash length = Nck |
| 2628 | // [array of Nck bytes] master key checksum hash |
| 2629 | // |
| 2630 | // The checksum is the (master key + checksum salt), run through the |
| 2631 | // stated number of PBKDF2 rounds |
| 2632 | IV = c.getIV(); |
| 2633 | byte[] mk = masterKeySpec.getEncoded(); |
| 2634 | byte[] checksum = makeKeyChecksum(masterKeySpec.getEncoded(), |
| 2635 | checksumSalt, PBKDF2_HASH_ROUNDS); |
| 2636 | |
| 2637 | ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length |
| 2638 | + checksum.length + 3); |
| 2639 | DataOutputStream mkOut = new DataOutputStream(blob); |
| 2640 | mkOut.writeByte(IV.length); |
| 2641 | mkOut.write(IV); |
| 2642 | mkOut.writeByte(mk.length); |
| 2643 | mkOut.write(mk); |
| 2644 | mkOut.writeByte(checksum.length); |
| 2645 | mkOut.write(checksum); |
| 2646 | mkOut.flush(); |
| 2647 | byte[] encryptedMk = mkC.doFinal(blob.toByteArray()); |
| 2648 | headerbuf.append(byteArrayToHex(encryptedMk)); |
| 2649 | headerbuf.append('\n'); |
| 2650 | |
| 2651 | return finalOutput; |
| 2652 | } |
| 2653 | |
| 2654 | private void backupOnePackage(PackageInfo pkg, OutputStream out) |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2655 | throws RemoteException { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2656 | Slog.d(TAG, "Binding to full backup agent : " + pkg.packageName); |
| 2657 | |
| 2658 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 2659 | IApplicationThread.BACKUP_MODE_FULL); |
| 2660 | if (agent != null) { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2661 | ParcelFileDescriptor[] pipes = null; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2662 | try { |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2663 | pipes = ParcelFileDescriptor.createPipe(); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2664 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2665 | ApplicationInfo app = pkg.applicationInfo; |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2666 | final boolean isSharedStorage = pkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2667 | final boolean sendApk = mIncludeApks |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2668 | && !isSharedStorage |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2669 | && ((app.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0) |
| 2670 | && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 || |
| 2671 | (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); |
| 2672 | |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2673 | sendOnBackupPackage(isSharedStorage ? "Shared storage" : pkg.packageName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2674 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2675 | final int token = generateToken(); |
| 2676 | FullBackupRunner runner = new FullBackupRunner(pkg, agent, pipes[1], |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2677 | token, sendApk, !isSharedStorage); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2678 | pipes[1].close(); // the runner has dup'd it |
| 2679 | pipes[1] = null; |
| 2680 | Thread t = new Thread(runner); |
| 2681 | t.start(); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2682 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2683 | // Now pull data from the app and stuff it into the compressor |
| 2684 | try { |
| 2685 | FileInputStream raw = new FileInputStream(pipes[0].getFileDescriptor()); |
| 2686 | DataInputStream in = new DataInputStream(raw); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2687 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2688 | byte[] buffer = new byte[16 * 1024]; |
| 2689 | int chunkTotal; |
| 2690 | while ((chunkTotal = in.readInt()) > 0) { |
| 2691 | while (chunkTotal > 0) { |
| 2692 | int toRead = (chunkTotal > buffer.length) |
| 2693 | ? buffer.length : chunkTotal; |
| 2694 | int nRead = in.read(buffer, 0, toRead); |
| 2695 | out.write(buffer, 0, nRead); |
| 2696 | chunkTotal -= nRead; |
| 2697 | } |
| 2698 | } |
| 2699 | } catch (IOException e) { |
| 2700 | Slog.i(TAG, "Caught exception reading from agent", e); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2703 | if (!waitUntilOperationComplete(token)) { |
| 2704 | Slog.e(TAG, "Full backup failed on package " + pkg.packageName); |
| 2705 | } else { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2706 | if (DEBUG) Slog.d(TAG, "Full package backup success: " + pkg.packageName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2707 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2708 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2709 | } catch (IOException e) { |
| 2710 | Slog.e(TAG, "Error backing up " + pkg.packageName, e); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2711 | } finally { |
| 2712 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2713 | // flush after every package |
| 2714 | out.flush(); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2715 | if (pipes != null) { |
| 2716 | if (pipes[0] != null) pipes[0].close(); |
| 2717 | if (pipes[1] != null) pipes[1].close(); |
| 2718 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2719 | } catch (IOException e) { |
| 2720 | Slog.w(TAG, "Error bringing down backup stack"); |
| 2721 | } |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2722 | } |
| 2723 | } else { |
| 2724 | Slog.w(TAG, "Unable to bind to full agent for " + pkg.packageName); |
| 2725 | } |
| 2726 | tearDown(pkg); |
| 2727 | } |
| 2728 | |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2729 | private void writeApkToBackup(PackageInfo pkg, BackupDataOutput output) { |
| 2730 | // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here |
| 2731 | final String appSourceDir = pkg.applicationInfo.sourceDir; |
| 2732 | final String apkDir = new File(appSourceDir).getParent(); |
| 2733 | FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null, |
| 2734 | apkDir, appSourceDir, output); |
| 2735 | |
| 2736 | // Save associated .obb content if it exists and we did save the apk |
| 2737 | // check for .obb and save those too |
| 2738 | final File obbDir = Environment.getExternalStorageAppObbDirectory(pkg.packageName); |
| 2739 | if (obbDir != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2740 | if (MORE_DEBUG) Log.i(TAG, "obb dir: " + obbDir.getAbsolutePath()); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2741 | File[] obbFiles = obbDir.listFiles(); |
| 2742 | if (obbFiles != null) { |
| 2743 | final String obbDirName = obbDir.getAbsolutePath(); |
| 2744 | for (File obb : obbFiles) { |
| 2745 | FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null, |
| 2746 | obbDirName, obb.getAbsolutePath(), output); |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2752 | private void finalizeBackup(OutputStream out) { |
| 2753 | try { |
| 2754 | // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes. |
| 2755 | byte[] eof = new byte[512 * 2]; // newly allocated == zero filled |
| 2756 | out.write(eof); |
| 2757 | } catch (IOException e) { |
| 2758 | Slog.w(TAG, "Error attempting to finalize backup stream"); |
| 2759 | } |
| 2760 | } |
| 2761 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2762 | private void writeAppManifest(PackageInfo pkg, File manifestFile, boolean withApk) |
| 2763 | throws IOException { |
| 2764 | // Manifest format. All data are strings ending in LF: |
| 2765 | // BACKUP_MANIFEST_VERSION, currently 1 |
| 2766 | // |
| 2767 | // Version 1: |
| 2768 | // package name |
| 2769 | // package's versionCode |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2770 | // platform versionCode |
| 2771 | // getInstallerPackageName() for this package (maybe empty) |
| 2772 | // boolean: "1" if archive includes .apk; any other string means not |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2773 | // number of signatures == N |
| 2774 | // N*: signature byte array in ascii format per Signature.toCharsString() |
| 2775 | StringBuilder builder = new StringBuilder(4096); |
| 2776 | StringBuilderPrinter printer = new StringBuilderPrinter(builder); |
| 2777 | |
| 2778 | printer.println(Integer.toString(BACKUP_MANIFEST_VERSION)); |
| 2779 | printer.println(pkg.packageName); |
| 2780 | printer.println(Integer.toString(pkg.versionCode)); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2781 | printer.println(Integer.toString(Build.VERSION.SDK_INT)); |
| 2782 | |
| 2783 | String installerName = mPackageManager.getInstallerPackageName(pkg.packageName); |
| 2784 | printer.println((installerName != null) ? installerName : ""); |
| 2785 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2786 | printer.println(withApk ? "1" : "0"); |
| 2787 | if (pkg.signatures == null) { |
| 2788 | printer.println("0"); |
| 2789 | } else { |
| 2790 | printer.println(Integer.toString(pkg.signatures.length)); |
| 2791 | for (Signature sig : pkg.signatures) { |
| 2792 | printer.println(sig.toCharsString()); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | FileOutputStream outstream = new FileOutputStream(manifestFile); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2797 | outstream.write(builder.toString().getBytes()); |
| 2798 | outstream.close(); |
| 2799 | } |
| 2800 | |
| 2801 | private void tearDown(PackageInfo pkg) { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2802 | if (pkg != null) { |
| 2803 | final ApplicationInfo app = pkg.applicationInfo; |
| 2804 | if (app != null) { |
| 2805 | try { |
| 2806 | // unbind and tidy up even on timeout or failure, just in case |
| 2807 | mActivityManager.unbindBackupAgent(app); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2808 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2809 | // The agent was running with a stub Application object, so shut it down. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2810 | if (app.uid != Process.SYSTEM_UID |
| 2811 | && app.uid != Process.PHONE_UID) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2812 | if (MORE_DEBUG) Slog.d(TAG, "Backup complete, killing host process"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2813 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 2814 | } else { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2815 | if (MORE_DEBUG) Slog.d(TAG, "Not killing after restore: " + app.processName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2816 | } |
| 2817 | } catch (RemoteException e) { |
| 2818 | Slog.d(TAG, "Lost app trying to shut down"); |
| 2819 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2820 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | // wrappers for observer use |
| 2825 | void sendStartBackup() { |
| 2826 | if (mObserver != null) { |
| 2827 | try { |
| 2828 | mObserver.onStartBackup(); |
| 2829 | } catch (RemoteException e) { |
| 2830 | Slog.w(TAG, "full backup observer went away: startBackup"); |
| 2831 | mObserver = null; |
| 2832 | } |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | void sendOnBackupPackage(String name) { |
| 2837 | if (mObserver != null) { |
| 2838 | try { |
| 2839 | // TODO: use a more user-friendly name string |
| 2840 | mObserver.onBackupPackage(name); |
| 2841 | } catch (RemoteException e) { |
| 2842 | Slog.w(TAG, "full backup observer went away: backupPackage"); |
| 2843 | mObserver = null; |
| 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | void sendEndBackup() { |
| 2849 | if (mObserver != null) { |
| 2850 | try { |
| 2851 | mObserver.onEndBackup(); |
| 2852 | } catch (RemoteException e) { |
| 2853 | Slog.w(TAG, "full backup observer went away: endBackup"); |
| 2854 | mObserver = null; |
| 2855 | } |
| 2856 | } |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2861 | // ----- Full restore from a file/socket ----- |
| 2862 | |
| 2863 | // Description of a file in the restore datastream |
| 2864 | static class FileMetadata { |
| 2865 | String packageName; // name of the owning app |
| 2866 | String installerPackageName; // name of the market-type app that installed the owner |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2867 | int type; // e.g. BackupAgent.TYPE_DIRECTORY |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2868 | String domain; // e.g. FullBackup.DATABASE_TREE_TOKEN |
| 2869 | String path; // subpath within the semantic domain |
| 2870 | long mode; // e.g. 0666 (actually int) |
| 2871 | long mtime; // last mod time, UTC time_t (actually int) |
| 2872 | long size; // bytes of content |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2873 | |
| 2874 | @Override |
| 2875 | public String toString() { |
| 2876 | StringBuilder sb = new StringBuilder(128); |
| 2877 | sb.append("FileMetadata{"); |
| 2878 | sb.append(packageName); sb.append(','); |
| 2879 | sb.append(type); sb.append(','); |
| 2880 | sb.append(domain); sb.append(':'); sb.append(path); sb.append(','); |
| 2881 | sb.append(size); |
| 2882 | sb.append('}'); |
| 2883 | return sb.toString(); |
| 2884 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | enum RestorePolicy { |
| 2888 | IGNORE, |
| 2889 | ACCEPT, |
| 2890 | ACCEPT_IF_APK |
| 2891 | } |
| 2892 | |
| 2893 | class PerformFullRestoreTask implements Runnable { |
| 2894 | ParcelFileDescriptor mInputFile; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2895 | String mCurrentPassword; |
| 2896 | String mDecryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2897 | IFullBackupRestoreObserver mObserver; |
| 2898 | AtomicBoolean mLatchObject; |
| 2899 | IBackupAgent mAgent; |
| 2900 | String mAgentPackage; |
| 2901 | ApplicationInfo mTargetApp; |
| 2902 | ParcelFileDescriptor[] mPipes = null; |
| 2903 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2904 | long mBytes; |
| 2905 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2906 | // possible handling states for a given package in the restore dataset |
| 2907 | final HashMap<String, RestorePolicy> mPackagePolicies |
| 2908 | = new HashMap<String, RestorePolicy>(); |
| 2909 | |
| 2910 | // installer package names for each encountered app, derived from the manifests |
| 2911 | final HashMap<String, String> mPackageInstallers = new HashMap<String, String>(); |
| 2912 | |
| 2913 | // Signatures for a given package found in its manifest file |
| 2914 | final HashMap<String, Signature[]> mManifestSignatures |
| 2915 | = new HashMap<String, Signature[]>(); |
| 2916 | |
| 2917 | // Packages we've already wiped data on when restoring their first file |
| 2918 | final HashSet<String> mClearedPackages = new HashSet<String>(); |
| 2919 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2920 | PerformFullRestoreTask(ParcelFileDescriptor fd, String curPassword, String decryptPassword, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2921 | IFullBackupRestoreObserver observer, AtomicBoolean latch) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2922 | mInputFile = fd; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2923 | mCurrentPassword = curPassword; |
| 2924 | mDecryptPassword = decryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2925 | mObserver = observer; |
| 2926 | mLatchObject = latch; |
| 2927 | mAgent = null; |
| 2928 | mAgentPackage = null; |
| 2929 | mTargetApp = null; |
| 2930 | |
| 2931 | // Which packages we've already wiped data on. We prepopulate this |
| 2932 | // with a whitelist of packages known to be unclearable. |
| 2933 | mClearedPackages.add("android"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2934 | mClearedPackages.add("com.android.providers.settings"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2935 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | class RestoreFileRunnable implements Runnable { |
| 2939 | IBackupAgent mAgent; |
| 2940 | FileMetadata mInfo; |
| 2941 | ParcelFileDescriptor mSocket; |
| 2942 | int mToken; |
| 2943 | |
| 2944 | RestoreFileRunnable(IBackupAgent agent, FileMetadata info, |
| 2945 | ParcelFileDescriptor socket, int token) throws IOException { |
| 2946 | mAgent = agent; |
| 2947 | mInfo = info; |
| 2948 | mToken = token; |
| 2949 | |
| 2950 | // This class is used strictly for process-local binder invocations. The |
| 2951 | // semantics of ParcelFileDescriptor differ in this case; in particular, we |
| 2952 | // do not automatically get a 'dup'ed descriptor that we can can continue |
| 2953 | // to use asynchronously from the caller. So, we make sure to dup it ourselves |
| 2954 | // before proceeding to do the restore. |
| 2955 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2956 | } |
| 2957 | |
| 2958 | @Override |
| 2959 | public void run() { |
| 2960 | try { |
| 2961 | mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type, |
| 2962 | mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime, |
| 2963 | mToken, mBackupManagerBinder); |
| 2964 | } catch (RemoteException e) { |
| 2965 | // never happens; this is used strictly for local binder calls |
| 2966 | } |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | @Override |
| 2971 | public void run() { |
| 2972 | Slog.i(TAG, "--- Performing full-dataset restore ---"); |
| 2973 | sendStartRestore(); |
| 2974 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2975 | // Are we able to restore shared-storage data? |
| 2976 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 2977 | mPackagePolicies.put(SHARED_BACKUP_AGENT_PACKAGE, RestorePolicy.ACCEPT); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2980 | FileInputStream rawInStream = null; |
| 2981 | DataInputStream rawDataIn = null; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2982 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2983 | if (hasBackupPassword()) { |
| 2984 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2985 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2986 | return; |
| 2987 | } |
| 2988 | } |
| 2989 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2990 | mBytes = 0; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2991 | byte[] buffer = new byte[32 * 1024]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2992 | rawInStream = new FileInputStream(mInputFile.getFileDescriptor()); |
| 2993 | rawDataIn = new DataInputStream(rawInStream); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2994 | |
| 2995 | // First, parse out the unencrypted/uncompressed header |
| 2996 | boolean compressed = false; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2997 | InputStream preCompressStream = rawInStream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2998 | final InputStream in; |
| 2999 | |
| 3000 | boolean okay = false; |
| 3001 | final int headerLen = BACKUP_FILE_HEADER_MAGIC.length(); |
| 3002 | byte[] streamHeader = new byte[headerLen]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3003 | rawDataIn.readFully(streamHeader); |
| 3004 | byte[] magicBytes = BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8"); |
| 3005 | if (Arrays.equals(magicBytes, streamHeader)) { |
| 3006 | // okay, header looks good. now parse out the rest of the fields. |
| 3007 | String s = readHeaderLine(rawInStream); |
| 3008 | if (Integer.parseInt(s) == BACKUP_FILE_VERSION) { |
| 3009 | // okay, it's a version we recognize |
| 3010 | s = readHeaderLine(rawInStream); |
| 3011 | compressed = (Integer.parseInt(s) != 0); |
| 3012 | s = readHeaderLine(rawInStream); |
| 3013 | if (s.equals("none")) { |
| 3014 | // no more header to parse; we're good to go |
| 3015 | okay = true; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 3016 | } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3017 | preCompressStream = decodeAesHeaderAndInitialize(s, rawInStream); |
| 3018 | if (preCompressStream != null) { |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3019 | okay = true; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3020 | } |
| 3021 | } else Slog.w(TAG, "Archive is encrypted but no password given"); |
| 3022 | } else Slog.w(TAG, "Wrong header version: " + s); |
| 3023 | } else Slog.w(TAG, "Didn't read the right header magic"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3024 | |
| 3025 | if (!okay) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3026 | Slog.w(TAG, "Invalid restore data; aborting."); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3027 | return; |
| 3028 | } |
| 3029 | |
| 3030 | // okay, use the right stream layer based on compression |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3031 | in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3032 | |
| 3033 | boolean didRestore; |
| 3034 | do { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 3035 | didRestore = restoreOneFile(in, buffer); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3036 | } while (didRestore); |
| 3037 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 3038 | if (MORE_DEBUG) Slog.v(TAG, "Done consuming input tarfile, total bytes=" + mBytes); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3039 | } catch (IOException e) { |
| 3040 | Slog.e(TAG, "Unable to read restore input"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3041 | } finally { |
| 3042 | tearDownPipes(); |
| 3043 | tearDownAgent(mTargetApp); |
| 3044 | |
| 3045 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3046 | if (rawDataIn != null) rawDataIn.close(); |
| 3047 | if (rawInStream != null) rawInStream.close(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3048 | mInputFile.close(); |
| 3049 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3050 | Slog.w(TAG, "Close of restore data pipe threw", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3051 | /* nothing we can do about this */ |
| 3052 | } |
| 3053 | synchronized (mCurrentOpLock) { |
| 3054 | mCurrentOperations.clear(); |
| 3055 | } |
| 3056 | synchronized (mLatchObject) { |
| 3057 | mLatchObject.set(true); |
| 3058 | mLatchObject.notifyAll(); |
| 3059 | } |
| 3060 | sendEndRestore(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 3061 | Slog.d(TAG, "Full restore pass complete."); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 3062 | mWakelock.release(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3066 | String readHeaderLine(InputStream in) throws IOException { |
| 3067 | int c; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3068 | StringBuilder buffer = new StringBuilder(80); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 3069 | while ((c = in.read()) >= 0) { |
| 3070 | if (c == '\n') break; // consume and discard the newlines |
| 3071 | buffer.append((char)c); |
| 3072 | } |
| 3073 | return buffer.toString(); |
| 3074 | } |
| 3075 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3076 | InputStream decodeAesHeaderAndInitialize(String encryptionName, InputStream rawInStream) { |
| 3077 | InputStream result = null; |
| 3078 | try { |
| 3079 | if (encryptionName.equals(ENCRYPTION_ALGORITHM_NAME)) { |
| 3080 | |
| 3081 | String userSaltHex = readHeaderLine(rawInStream); // 5 |
| 3082 | byte[] userSalt = hexToByteArray(userSaltHex); |
| 3083 | |
| 3084 | String ckSaltHex = readHeaderLine(rawInStream); // 6 |
| 3085 | byte[] ckSalt = hexToByteArray(ckSaltHex); |
| 3086 | |
| 3087 | int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7 |
| 3088 | String userIvHex = readHeaderLine(rawInStream); // 8 |
| 3089 | |
| 3090 | String masterKeyBlobHex = readHeaderLine(rawInStream); // 9 |
| 3091 | |
| 3092 | // decrypt the master key blob |
| 3093 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 3094 | SecretKey userKey = buildPasswordKey(mDecryptPassword, userSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3095 | rounds); |
| 3096 | byte[] IV = hexToByteArray(userIvHex); |
| 3097 | IvParameterSpec ivSpec = new IvParameterSpec(IV); |
| 3098 | c.init(Cipher.DECRYPT_MODE, |
| 3099 | new SecretKeySpec(userKey.getEncoded(), "AES"), |
| 3100 | ivSpec); |
| 3101 | byte[] mkCipher = hexToByteArray(masterKeyBlobHex); |
| 3102 | byte[] mkBlob = c.doFinal(mkCipher); |
| 3103 | |
| 3104 | // first, the master key IV |
| 3105 | int offset = 0; |
| 3106 | int len = mkBlob[offset++]; |
| 3107 | IV = Arrays.copyOfRange(mkBlob, offset, offset + len); |
| 3108 | offset += len; |
| 3109 | // then the master key itself |
| 3110 | len = mkBlob[offset++]; |
| 3111 | byte[] mk = Arrays.copyOfRange(mkBlob, |
| 3112 | offset, offset + len); |
| 3113 | offset += len; |
| 3114 | // and finally the master key checksum hash |
| 3115 | len = mkBlob[offset++]; |
| 3116 | byte[] mkChecksum = Arrays.copyOfRange(mkBlob, |
| 3117 | offset, offset + len); |
| 3118 | |
| 3119 | // now validate the decrypted master key against the checksum |
| 3120 | byte[] calculatedCk = makeKeyChecksum(mk, ckSalt, rounds); |
| 3121 | if (Arrays.equals(calculatedCk, mkChecksum)) { |
| 3122 | ivSpec = new IvParameterSpec(IV); |
| 3123 | c.init(Cipher.DECRYPT_MODE, |
| 3124 | new SecretKeySpec(mk, "AES"), |
| 3125 | ivSpec); |
| 3126 | // Only if all of the above worked properly will 'result' be assigned |
| 3127 | result = new CipherInputStream(rawInStream, c); |
| 3128 | } else Slog.w(TAG, "Incorrect password"); |
| 3129 | } else Slog.w(TAG, "Unsupported encryption method: " + encryptionName); |
| 3130 | } catch (InvalidAlgorithmParameterException e) { |
| 3131 | Slog.e(TAG, "Needed parameter spec unavailable!", e); |
| 3132 | } catch (BadPaddingException e) { |
| 3133 | // This case frequently occurs when the wrong password is used to decrypt |
| 3134 | // the master key. Use the identical "incorrect password" log text as is |
| 3135 | // used in the checksum failure log in order to avoid providing additional |
| 3136 | // information to an attacker. |
| 3137 | Slog.w(TAG, "Incorrect password"); |
| 3138 | } catch (IllegalBlockSizeException e) { |
| 3139 | Slog.w(TAG, "Invalid block size in master key"); |
| 3140 | } catch (NoSuchAlgorithmException e) { |
| 3141 | Slog.e(TAG, "Needed decryption algorithm unavailable!"); |
| 3142 | } catch (NoSuchPaddingException e) { |
| 3143 | Slog.e(TAG, "Needed padding mechanism unavailable!"); |
| 3144 | } catch (InvalidKeyException e) { |
| 3145 | Slog.w(TAG, "Illegal password; aborting"); |
| 3146 | } catch (NumberFormatException e) { |
| 3147 | Slog.w(TAG, "Can't parse restore data header"); |
| 3148 | } catch (IOException e) { |
| 3149 | Slog.w(TAG, "Can't read input header"); |
| 3150 | } |
| 3151 | |
| 3152 | return result; |
| 3153 | } |
| 3154 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3155 | boolean restoreOneFile(InputStream instream, byte[] buffer) { |
| 3156 | FileMetadata info; |
| 3157 | try { |
| 3158 | info = readTarHeaders(instream); |
| 3159 | if (info != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 3160 | if (MORE_DEBUG) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3161 | dumpFileMetadata(info); |
| 3162 | } |
| 3163 | |
| 3164 | final String pkg = info.packageName; |
| 3165 | if (!pkg.equals(mAgentPackage)) { |
| 3166 | // okay, change in package; set up our various |
| 3167 | // bookkeeping if we haven't seen it yet |
| 3168 | if (!mPackagePolicies.containsKey(pkg)) { |
| 3169 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3170 | } |
| 3171 | |
| 3172 | // Clean up the previous agent relationship if necessary, |
| 3173 | // and let the observer know we're considering a new app. |
| 3174 | if (mAgent != null) { |
| 3175 | if (DEBUG) Slog.d(TAG, "Saw new package; tearing down old one"); |
| 3176 | tearDownPipes(); |
| 3177 | tearDownAgent(mTargetApp); |
| 3178 | mTargetApp = null; |
| 3179 | mAgentPackage = null; |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | if (info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 3184 | mPackagePolicies.put(pkg, readAppManifest(info, instream)); |
| 3185 | mPackageInstallers.put(pkg, info.installerPackageName); |
| 3186 | // We've read only the manifest content itself at this point, |
| 3187 | // so consume the footer before looping around to the next |
| 3188 | // input file |
| 3189 | skipTarPadding(info.size, instream); |
| 3190 | sendOnRestorePackage(pkg); |
| 3191 | } else { |
| 3192 | // Non-manifest, so it's actual file data. Is this a package |
| 3193 | // we're ignoring? |
| 3194 | boolean okay = true; |
| 3195 | RestorePolicy policy = mPackagePolicies.get(pkg); |
| 3196 | switch (policy) { |
| 3197 | case IGNORE: |
| 3198 | okay = false; |
| 3199 | break; |
| 3200 | |
| 3201 | case ACCEPT_IF_APK: |
| 3202 | // If we're in accept-if-apk state, then the first file we |
| 3203 | // see MUST be the apk. |
| 3204 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 3205 | if (DEBUG) Slog.d(TAG, "APK file; installing"); |
| 3206 | // Try to install the app. |
| 3207 | String installerName = mPackageInstallers.get(pkg); |
| 3208 | okay = installApk(info, installerName, instream); |
| 3209 | // good to go; promote to ACCEPT |
| 3210 | mPackagePolicies.put(pkg, (okay) |
| 3211 | ? RestorePolicy.ACCEPT |
| 3212 | : RestorePolicy.IGNORE); |
| 3213 | // At this point we've consumed this file entry |
| 3214 | // ourselves, so just strip the tar footer and |
| 3215 | // go on to the next file in the input stream |
| 3216 | skipTarPadding(info.size, instream); |
| 3217 | return true; |
| 3218 | } else { |
| 3219 | // File data before (or without) the apk. We can't |
| 3220 | // handle it coherently in this case so ignore it. |
| 3221 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3222 | okay = false; |
| 3223 | } |
| 3224 | break; |
| 3225 | |
| 3226 | case ACCEPT: |
| 3227 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 3228 | if (DEBUG) Slog.d(TAG, "apk present but ACCEPT"); |
| 3229 | // we can take the data without the apk, so we |
| 3230 | // *want* to do so. skip the apk by declaring this |
| 3231 | // one file not-okay without changing the restore |
| 3232 | // policy for the package. |
| 3233 | okay = false; |
| 3234 | } |
| 3235 | break; |
| 3236 | |
| 3237 | default: |
| 3238 | // Something has gone dreadfully wrong when determining |
| 3239 | // the restore policy from the manifest. Ignore the |
| 3240 | // rest of this package's data. |
| 3241 | Slog.e(TAG, "Invalid policy from manifest"); |
| 3242 | okay = false; |
| 3243 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3244 | break; |
| 3245 | } |
| 3246 | |
| 3247 | // If the policy is satisfied, go ahead and set up to pipe the |
| 3248 | // data to the agent. |
| 3249 | if (DEBUG && okay && mAgent != null) { |
| 3250 | Slog.i(TAG, "Reusing existing agent instance"); |
| 3251 | } |
| 3252 | if (okay && mAgent == null) { |
| 3253 | if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg); |
| 3254 | |
| 3255 | try { |
| 3256 | mTargetApp = mPackageManager.getApplicationInfo(pkg, 0); |
| 3257 | |
| 3258 | // If we haven't sent any data to this app yet, we probably |
| 3259 | // need to clear it first. Check that. |
| 3260 | if (!mClearedPackages.contains(pkg)) { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3261 | // apps with their own backup agents are |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3262 | // responsible for coherently managing a full |
| 3263 | // restore. |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3264 | if (mTargetApp.backupAgentName == null) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3265 | if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore"); |
| 3266 | clearApplicationDataSynchronous(pkg); |
| 3267 | } else { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3268 | if (DEBUG) Slog.d(TAG, "backup agent (" |
| 3269 | + mTargetApp.backupAgentName + ") => no clear"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3270 | } |
| 3271 | mClearedPackages.add(pkg); |
| 3272 | } else { |
| 3273 | if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required"); |
| 3274 | } |
| 3275 | |
| 3276 | // All set; now set up the IPC and launch the agent |
| 3277 | setUpPipes(); |
| 3278 | mAgent = bindToAgentSynchronous(mTargetApp, |
| 3279 | IApplicationThread.BACKUP_MODE_RESTORE_FULL); |
| 3280 | mAgentPackage = pkg; |
| 3281 | } catch (IOException e) { |
| 3282 | // fall through to error handling |
| 3283 | } catch (NameNotFoundException e) { |
| 3284 | // fall through to error handling |
| 3285 | } |
| 3286 | |
| 3287 | if (mAgent == null) { |
| 3288 | if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg); |
| 3289 | okay = false; |
| 3290 | tearDownPipes(); |
| 3291 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | // Sanity check: make sure we never give data to the wrong app. This |
| 3296 | // should never happen but a little paranoia here won't go amiss. |
| 3297 | if (okay && !pkg.equals(mAgentPackage)) { |
| 3298 | Slog.e(TAG, "Restoring data for " + pkg |
| 3299 | + " but agent is for " + mAgentPackage); |
| 3300 | okay = false; |
| 3301 | } |
| 3302 | |
| 3303 | // At this point we have an agent ready to handle the full |
| 3304 | // restore data as well as a pipe for sending data to |
| 3305 | // that agent. Tell the agent to start reading from the |
| 3306 | // pipe. |
| 3307 | if (okay) { |
| 3308 | boolean agentSuccess = true; |
| 3309 | long toCopy = info.size; |
| 3310 | final int token = generateToken(); |
| 3311 | try { |
| 3312 | if (DEBUG) Slog.d(TAG, "Invoking agent to restore file " |
| 3313 | + info.path); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 3314 | prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL, null); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3315 | // fire up the app's agent listening on the socket. If |
| 3316 | // the agent is running in the system process we can't |
| 3317 | // just invoke it asynchronously, so we provide a thread |
| 3318 | // for it here. |
| 3319 | if (mTargetApp.processName.equals("system")) { |
| 3320 | Slog.d(TAG, "system process agent - spinning a thread"); |
| 3321 | RestoreFileRunnable runner = new RestoreFileRunnable( |
| 3322 | mAgent, info, mPipes[0], token); |
| 3323 | new Thread(runner).start(); |
| 3324 | } else { |
| 3325 | mAgent.doRestoreFile(mPipes[0], info.size, info.type, |
| 3326 | info.domain, info.path, info.mode, info.mtime, |
| 3327 | token, mBackupManagerBinder); |
| 3328 | } |
| 3329 | } catch (IOException e) { |
| 3330 | // couldn't dup the socket for a process-local restore |
| 3331 | Slog.d(TAG, "Couldn't establish restore"); |
| 3332 | agentSuccess = false; |
| 3333 | okay = false; |
| 3334 | } catch (RemoteException e) { |
| 3335 | // whoops, remote agent went away. We'll eat the content |
| 3336 | // ourselves, then, and not copy it over. |
| 3337 | Slog.e(TAG, "Agent crashed during full restore"); |
| 3338 | agentSuccess = false; |
| 3339 | okay = false; |
| 3340 | } |
| 3341 | |
| 3342 | // Copy over the data if the agent is still good |
| 3343 | if (okay) { |
| 3344 | boolean pipeOkay = true; |
| 3345 | FileOutputStream pipe = new FileOutputStream( |
| 3346 | mPipes[1].getFileDescriptor()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3347 | while (toCopy > 0) { |
| 3348 | int toRead = (toCopy > buffer.length) |
| 3349 | ? buffer.length : (int)toCopy; |
| 3350 | int nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3351 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3352 | if (nRead <= 0) break; |
| 3353 | toCopy -= nRead; |
| 3354 | |
| 3355 | // send it to the output pipe as long as things |
| 3356 | // are still good |
| 3357 | if (pipeOkay) { |
| 3358 | try { |
| 3359 | pipe.write(buffer, 0, nRead); |
| 3360 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3361 | Slog.e(TAG, "Failed to write to restore pipe", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3362 | pipeOkay = false; |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | // done sending that file! Now we just need to consume |
| 3368 | // the delta from info.size to the end of block. |
| 3369 | skipTarPadding(info.size, instream); |
| 3370 | |
| 3371 | // and now that we've sent it all, wait for the remote |
| 3372 | // side to acknowledge receipt |
| 3373 | agentSuccess = waitUntilOperationComplete(token); |
| 3374 | } |
| 3375 | |
| 3376 | // okay, if the remote end failed at any point, deal with |
| 3377 | // it by ignoring the rest of the restore on it |
| 3378 | if (!agentSuccess) { |
| 3379 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 3380 | tearDownPipes(); |
| 3381 | tearDownAgent(mTargetApp); |
| 3382 | mAgent = null; |
| 3383 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3384 | } |
| 3385 | } |
| 3386 | |
| 3387 | // Problems setting up the agent communication, or an already- |
| 3388 | // ignored package: skip to the next tar stream entry by |
| 3389 | // reading and discarding this file. |
| 3390 | if (!okay) { |
| 3391 | if (DEBUG) Slog.d(TAG, "[discarding file content]"); |
| 3392 | long bytesToConsume = (info.size + 511) & ~511; |
| 3393 | while (bytesToConsume > 0) { |
| 3394 | int toRead = (bytesToConsume > buffer.length) |
| 3395 | ? buffer.length : (int)bytesToConsume; |
| 3396 | long nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3397 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3398 | if (nRead <= 0) break; |
| 3399 | bytesToConsume -= nRead; |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | } catch (IOException e) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3405 | if (DEBUG) Slog.w(TAG, "io exception on restore socket read", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3406 | // treat as EOF |
| 3407 | info = null; |
| 3408 | } |
| 3409 | |
| 3410 | return (info != null); |
| 3411 | } |
| 3412 | |
| 3413 | void setUpPipes() throws IOException { |
| 3414 | mPipes = ParcelFileDescriptor.createPipe(); |
| 3415 | } |
| 3416 | |
| 3417 | void tearDownPipes() { |
| 3418 | if (mPipes != null) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3419 | try { |
| 3420 | mPipes[0].close(); |
| 3421 | mPipes[0] = null; |
| 3422 | mPipes[1].close(); |
| 3423 | mPipes[1] = null; |
| 3424 | } catch (IOException e) { |
| 3425 | Slog.w(TAG, "Couldn't close agent pipes", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3426 | } |
| 3427 | mPipes = null; |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | void tearDownAgent(ApplicationInfo app) { |
| 3432 | if (mAgent != null) { |
| 3433 | try { |
| 3434 | // unbind and tidy up even on timeout or failure, just in case |
| 3435 | mActivityManager.unbindBackupAgent(app); |
| 3436 | |
| 3437 | // The agent was running with a stub Application object, so shut it down. |
| 3438 | // !!! We hardcode the confirmation UI's package name here rather than use a |
| 3439 | // manifest flag! TODO something less direct. |
| 3440 | if (app.uid != Process.SYSTEM_UID |
| 3441 | && !app.packageName.equals("com.android.backupconfirm")) { |
| 3442 | if (DEBUG) Slog.d(TAG, "Killing host process"); |
| 3443 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 3444 | } else { |
| 3445 | if (DEBUG) Slog.d(TAG, "Not killing after full restore"); |
| 3446 | } |
| 3447 | } catch (RemoteException e) { |
| 3448 | Slog.d(TAG, "Lost app trying to shut down"); |
| 3449 | } |
| 3450 | mAgent = null; |
| 3451 | } |
| 3452 | } |
| 3453 | |
| 3454 | class RestoreInstallObserver extends IPackageInstallObserver.Stub { |
| 3455 | final AtomicBoolean mDone = new AtomicBoolean(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3456 | String mPackageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3457 | int mResult; |
| 3458 | |
| 3459 | public void reset() { |
| 3460 | synchronized (mDone) { |
| 3461 | mDone.set(false); |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | public void waitForCompletion() { |
| 3466 | synchronized (mDone) { |
| 3467 | while (mDone.get() == false) { |
| 3468 | try { |
| 3469 | mDone.wait(); |
| 3470 | } catch (InterruptedException e) { } |
| 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | int getResult() { |
| 3476 | return mResult; |
| 3477 | } |
| 3478 | |
| 3479 | @Override |
| 3480 | public void packageInstalled(String packageName, int returnCode) |
| 3481 | throws RemoteException { |
| 3482 | synchronized (mDone) { |
| 3483 | mResult = returnCode; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3484 | mPackageName = packageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3485 | mDone.set(true); |
| 3486 | mDone.notifyAll(); |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3490 | |
| 3491 | class RestoreDeleteObserver extends IPackageDeleteObserver.Stub { |
| 3492 | final AtomicBoolean mDone = new AtomicBoolean(); |
| 3493 | int mResult; |
| 3494 | |
| 3495 | public void reset() { |
| 3496 | synchronized (mDone) { |
| 3497 | mDone.set(false); |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | public void waitForCompletion() { |
| 3502 | synchronized (mDone) { |
| 3503 | while (mDone.get() == false) { |
| 3504 | try { |
| 3505 | mDone.wait(); |
| 3506 | } catch (InterruptedException e) { } |
| 3507 | } |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | @Override |
| 3512 | public void packageDeleted(String packageName, int returnCode) throws RemoteException { |
| 3513 | synchronized (mDone) { |
| 3514 | mResult = returnCode; |
| 3515 | mDone.set(true); |
| 3516 | mDone.notifyAll(); |
| 3517 | } |
| 3518 | } |
| 3519 | } |
| 3520 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3521 | final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3522 | final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3523 | |
| 3524 | boolean installApk(FileMetadata info, String installerPackage, InputStream instream) { |
| 3525 | boolean okay = true; |
| 3526 | |
| 3527 | if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName); |
| 3528 | |
| 3529 | // The file content is an .apk file. Copy it out to a staging location and |
| 3530 | // attempt to install it. |
| 3531 | File apkFile = new File(mDataDir, info.packageName); |
| 3532 | try { |
| 3533 | FileOutputStream apkStream = new FileOutputStream(apkFile); |
| 3534 | byte[] buffer = new byte[32 * 1024]; |
| 3535 | long size = info.size; |
| 3536 | while (size > 0) { |
| 3537 | long toRead = (buffer.length < size) ? buffer.length : size; |
| 3538 | int didRead = instream.read(buffer, 0, (int)toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3539 | if (didRead >= 0) mBytes += didRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3540 | apkStream.write(buffer, 0, didRead); |
| 3541 | size -= didRead; |
| 3542 | } |
| 3543 | apkStream.close(); |
| 3544 | |
| 3545 | // make sure the installer can read it |
| 3546 | apkFile.setReadable(true, false); |
| 3547 | |
| 3548 | // Now install it |
| 3549 | Uri packageUri = Uri.fromFile(apkFile); |
| 3550 | mInstallObserver.reset(); |
| 3551 | mPackageManager.installPackage(packageUri, mInstallObserver, |
| Christopher Tate | ab63aa8 | 2011-09-26 16:30:30 -0700 | [diff] [blame] | 3552 | PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB, |
| 3553 | installerPackage); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3554 | mInstallObserver.waitForCompletion(); |
| 3555 | |
| 3556 | if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) { |
| 3557 | // The only time we continue to accept install of data even if the |
| 3558 | // apk install failed is if we had already determined that we could |
| 3559 | // accept the data regardless. |
| 3560 | if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) { |
| 3561 | okay = false; |
| 3562 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3563 | } else { |
| 3564 | // Okay, the install succeeded. Make sure it was the right app. |
| 3565 | boolean uninstall = false; |
| 3566 | if (!mInstallObserver.mPackageName.equals(info.packageName)) { |
| 3567 | Slog.w(TAG, "Restore stream claimed to include apk for " |
| 3568 | + info.packageName + " but apk was really " |
| 3569 | + mInstallObserver.mPackageName); |
| 3570 | // delete the package we just put in place; it might be fraudulent |
| 3571 | okay = false; |
| 3572 | uninstall = true; |
| 3573 | } else { |
| 3574 | try { |
| 3575 | PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName, |
| 3576 | PackageManager.GET_SIGNATURES); |
| 3577 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 3578 | Slog.w(TAG, "Restore stream contains apk of package " |
| 3579 | + info.packageName + " but it disallows backup/restore"); |
| 3580 | okay = false; |
| 3581 | } else { |
| 3582 | // So far so good -- do the signatures match the manifest? |
| 3583 | Signature[] sigs = mManifestSignatures.get(info.packageName); |
| 3584 | if (!signaturesMatch(sigs, pkg)) { |
| 3585 | Slog.w(TAG, "Installed app " + info.packageName |
| 3586 | + " signatures do not match restore manifest"); |
| 3587 | okay = false; |
| 3588 | uninstall = true; |
| 3589 | } |
| 3590 | } |
| 3591 | } catch (NameNotFoundException e) { |
| 3592 | Slog.w(TAG, "Install of package " + info.packageName |
| 3593 | + " succeeded but now not found"); |
| 3594 | okay = false; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | // If we're not okay at this point, we need to delete the package |
| 3599 | // that we just installed. |
| 3600 | if (uninstall) { |
| 3601 | mDeleteObserver.reset(); |
| 3602 | mPackageManager.deletePackage(mInstallObserver.mPackageName, |
| 3603 | mDeleteObserver, 0); |
| 3604 | mDeleteObserver.waitForCompletion(); |
| 3605 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3606 | } |
| 3607 | } catch (IOException e) { |
| 3608 | Slog.e(TAG, "Unable to transcribe restored apk for install"); |
| 3609 | okay = false; |
| 3610 | } finally { |
| 3611 | apkFile.delete(); |
| 3612 | } |
| 3613 | |
| 3614 | return okay; |
| 3615 | } |
| 3616 | |
| 3617 | // Given an actual file content size, consume the post-content padding mandated |
| 3618 | // by the tar format. |
| 3619 | void skipTarPadding(long size, InputStream instream) throws IOException { |
| 3620 | long partial = (size + 512) % 512; |
| 3621 | if (partial > 0) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3622 | final int needed = 512 - (int)partial; |
| 3623 | byte[] buffer = new byte[needed]; |
| 3624 | if (readExactly(instream, buffer, 0, needed) == needed) { |
| 3625 | mBytes += needed; |
| 3626 | } else throw new IOException("Unexpected EOF in padding"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | // Returns a policy constant; takes a buffer arg to reduce memory churn |
| 3631 | RestorePolicy readAppManifest(FileMetadata info, InputStream instream) |
| 3632 | throws IOException { |
| 3633 | // Fail on suspiciously large manifest files |
| 3634 | if (info.size > 64 * 1024) { |
| 3635 | throw new IOException("Restore manifest too big; corrupt? size=" + info.size); |
| 3636 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3637 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3638 | byte[] buffer = new byte[(int) info.size]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3639 | if (readExactly(instream, buffer, 0, (int)info.size) == info.size) { |
| 3640 | mBytes += info.size; |
| 3641 | } else throw new IOException("Unexpected EOF in manifest"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3642 | |
| 3643 | RestorePolicy policy = RestorePolicy.IGNORE; |
| 3644 | String[] str = new String[1]; |
| 3645 | int offset = 0; |
| 3646 | |
| 3647 | try { |
| 3648 | offset = extractLine(buffer, offset, str); |
| 3649 | int version = Integer.parseInt(str[0]); |
| 3650 | if (version == BACKUP_MANIFEST_VERSION) { |
| 3651 | offset = extractLine(buffer, offset, str); |
| 3652 | String manifestPackage = str[0]; |
| 3653 | // TODO: handle <original-package> |
| 3654 | if (manifestPackage.equals(info.packageName)) { |
| 3655 | offset = extractLine(buffer, offset, str); |
| 3656 | version = Integer.parseInt(str[0]); // app version |
| 3657 | offset = extractLine(buffer, offset, str); |
| 3658 | int platformVersion = Integer.parseInt(str[0]); |
| 3659 | offset = extractLine(buffer, offset, str); |
| 3660 | info.installerPackageName = (str[0].length() > 0) ? str[0] : null; |
| 3661 | offset = extractLine(buffer, offset, str); |
| 3662 | boolean hasApk = str[0].equals("1"); |
| 3663 | offset = extractLine(buffer, offset, str); |
| 3664 | int numSigs = Integer.parseInt(str[0]); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3665 | if (numSigs > 0) { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3666 | Signature[] sigs = new Signature[numSigs]; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3667 | for (int i = 0; i < numSigs; i++) { |
| 3668 | offset = extractLine(buffer, offset, str); |
| 3669 | sigs[i] = new Signature(str[0]); |
| 3670 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3671 | mManifestSignatures.put(info.packageName, sigs); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3672 | |
| 3673 | // Okay, got the manifest info we need... |
| 3674 | try { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3675 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3676 | info.packageName, PackageManager.GET_SIGNATURES); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3677 | // Fall through to IGNORE if the app explicitly disallows backup |
| 3678 | final int flags = pkgInfo.applicationInfo.flags; |
| 3679 | if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) { |
| 3680 | // Verify signatures against any installed version; if they |
| 3681 | // don't match, then we fall though and ignore the data. The |
| 3682 | // signatureMatch() method explicitly ignores the signature |
| 3683 | // check for packages installed on the system partition, because |
| 3684 | // such packages are signed with the platform cert instead of |
| 3685 | // the app developer's cert, so they're different on every |
| 3686 | // device. |
| 3687 | if (signaturesMatch(sigs, pkgInfo)) { |
| 3688 | if (pkgInfo.versionCode >= version) { |
| 3689 | Slog.i(TAG, "Sig + version match; taking data"); |
| 3690 | policy = RestorePolicy.ACCEPT; |
| 3691 | } else { |
| 3692 | // The data is from a newer version of the app than |
| 3693 | // is presently installed. That means we can only |
| 3694 | // use it if the matching apk is also supplied. |
| 3695 | Slog.d(TAG, "Data version " + version |
| 3696 | + " is newer than installed version " |
| 3697 | + pkgInfo.versionCode + " - requiring apk"); |
| 3698 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3699 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3700 | } else { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3701 | Slog.w(TAG, "Restore manifest signatures do not match " |
| 3702 | + "installed application for " + info.packageName); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3703 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3704 | } else { |
| 3705 | if (DEBUG) Slog.i(TAG, "Restore manifest from " |
| 3706 | + info.packageName + " but allowBackup=false"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3707 | } |
| 3708 | } catch (NameNotFoundException e) { |
| 3709 | // Okay, the target app isn't installed. We can process |
| 3710 | // the restore properly only if the dataset provides the |
| 3711 | // apk file and we can successfully install it. |
| 3712 | if (DEBUG) Slog.i(TAG, "Package " + info.packageName |
| 3713 | + " not installed; requiring apk in dataset"); |
| 3714 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3715 | } |
| 3716 | |
| 3717 | if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) { |
| 3718 | Slog.i(TAG, "Cannot restore package " + info.packageName |
| 3719 | + " without the matching .apk"); |
| 3720 | } |
| 3721 | } else { |
| 3722 | Slog.i(TAG, "Missing signature on backed-up package " |
| 3723 | + info.packageName); |
| 3724 | } |
| 3725 | } else { |
| 3726 | Slog.i(TAG, "Expected package " + info.packageName |
| 3727 | + " but restore manifest claims " + manifestPackage); |
| 3728 | } |
| 3729 | } else { |
| 3730 | Slog.i(TAG, "Unknown restore manifest version " + version |
| 3731 | + " for package " + info.packageName); |
| 3732 | } |
| 3733 | } catch (NumberFormatException e) { |
| 3734 | Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName); |
| Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 3735 | } catch (IllegalArgumentException e) { |
| 3736 | Slog.w(TAG, e.getMessage()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | return policy; |
| 3740 | } |
| 3741 | |
| 3742 | // Builds a line from a byte buffer starting at 'offset', and returns |
| 3743 | // the index of the next unconsumed data in the buffer. |
| 3744 | int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException { |
| 3745 | final int end = buffer.length; |
| 3746 | if (offset >= end) throw new IOException("Incomplete data"); |
| 3747 | |
| 3748 | int pos; |
| 3749 | for (pos = offset; pos < end; pos++) { |
| 3750 | byte c = buffer[pos]; |
| 3751 | // at LF we declare end of line, and return the next char as the |
| 3752 | // starting point for the next time through |
| 3753 | if (c == '\n') { |
| 3754 | break; |
| 3755 | } |
| 3756 | } |
| 3757 | outStr[0] = new String(buffer, offset, pos - offset); |
| 3758 | pos++; // may be pointing an extra byte past the end but that's okay |
| 3759 | return pos; |
| 3760 | } |
| 3761 | |
| 3762 | void dumpFileMetadata(FileMetadata info) { |
| 3763 | if (DEBUG) { |
| 3764 | StringBuilder b = new StringBuilder(128); |
| 3765 | |
| 3766 | // mode string |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3767 | b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-'); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3768 | b.append(((info.mode & 0400) != 0) ? 'r' : '-'); |
| 3769 | b.append(((info.mode & 0200) != 0) ? 'w' : '-'); |
| 3770 | b.append(((info.mode & 0100) != 0) ? 'x' : '-'); |
| 3771 | b.append(((info.mode & 0040) != 0) ? 'r' : '-'); |
| 3772 | b.append(((info.mode & 0020) != 0) ? 'w' : '-'); |
| 3773 | b.append(((info.mode & 0010) != 0) ? 'x' : '-'); |
| 3774 | b.append(((info.mode & 0004) != 0) ? 'r' : '-'); |
| 3775 | b.append(((info.mode & 0002) != 0) ? 'w' : '-'); |
| 3776 | b.append(((info.mode & 0001) != 0) ? 'x' : '-'); |
| 3777 | b.append(String.format(" %9d ", info.size)); |
| 3778 | |
| 3779 | Date stamp = new Date(info.mtime); |
| 3780 | b.append(new SimpleDateFormat("MMM dd kk:mm:ss ").format(stamp)); |
| 3781 | |
| 3782 | b.append(info.packageName); |
| 3783 | b.append(" :: "); |
| 3784 | b.append(info.domain); |
| 3785 | b.append(" :: "); |
| 3786 | b.append(info.path); |
| 3787 | |
| 3788 | Slog.i(TAG, b.toString()); |
| 3789 | } |
| 3790 | } |
| 3791 | // Consume a tar file header block [sequence] and accumulate the relevant metadata |
| 3792 | FileMetadata readTarHeaders(InputStream instream) throws IOException { |
| 3793 | byte[] block = new byte[512]; |
| 3794 | FileMetadata info = null; |
| 3795 | |
| 3796 | boolean gotHeader = readTarHeader(instream, block); |
| 3797 | if (gotHeader) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3798 | try { |
| 3799 | // okay, presume we're okay, and extract the various metadata |
| 3800 | info = new FileMetadata(); |
| 3801 | info.size = extractRadix(block, 124, 12, 8); |
| 3802 | info.mtime = extractRadix(block, 136, 12, 8); |
| 3803 | info.mode = extractRadix(block, 100, 8, 8); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3804 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3805 | info.path = extractString(block, 345, 155); // prefix |
| 3806 | String path = extractString(block, 0, 100); |
| 3807 | if (path.length() > 0) { |
| 3808 | if (info.path.length() > 0) info.path += '/'; |
| 3809 | info.path += path; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3810 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3811 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3812 | // tar link indicator field: 1 byte at offset 156 in the header. |
| 3813 | int typeChar = block[156]; |
| 3814 | if (typeChar == 'x') { |
| 3815 | // pax extended header, so we need to read that |
| 3816 | gotHeader = readPaxExtendedHeader(instream, info); |
| 3817 | if (gotHeader) { |
| 3818 | // and after a pax extended header comes another real header -- read |
| 3819 | // that to find the real file type |
| 3820 | gotHeader = readTarHeader(instream, block); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3821 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3822 | if (!gotHeader) throw new IOException("Bad or missing pax header"); |
| 3823 | |
| 3824 | typeChar = block[156]; |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3825 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3826 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3827 | switch (typeChar) { |
| 3828 | case '0': info.type = BackupAgent.TYPE_FILE; break; |
| 3829 | case '5': { |
| 3830 | info.type = BackupAgent.TYPE_DIRECTORY; |
| 3831 | if (info.size != 0) { |
| 3832 | Slog.w(TAG, "Directory entry with nonzero size in header"); |
| 3833 | info.size = 0; |
| 3834 | } |
| 3835 | break; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3836 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3837 | case 0: { |
| 3838 | // presume EOF |
| 3839 | if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info); |
| 3840 | return null; |
| 3841 | } |
| 3842 | default: { |
| 3843 | Slog.e(TAG, "Unknown tar entity type: " + typeChar); |
| 3844 | throw new IOException("Unknown entity type " + typeChar); |
| 3845 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3846 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3847 | |
| 3848 | // Parse out the path |
| 3849 | // |
| 3850 | // first: apps/shared/unrecognized |
| 3851 | if (FullBackup.SHARED_PREFIX.regionMatches(0, |
| 3852 | info.path, 0, FullBackup.SHARED_PREFIX.length())) { |
| 3853 | // File in shared storage. !!! TODO: implement this. |
| 3854 | info.path = info.path.substring(FullBackup.SHARED_PREFIX.length()); |
| Christopher Tate | 73d7369 | 2012-01-20 17:11:31 -0800 | [diff] [blame] | 3855 | info.packageName = SHARED_BACKUP_AGENT_PACKAGE; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3856 | info.domain = FullBackup.SHARED_STORAGE_TOKEN; |
| 3857 | if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path); |
| 3858 | } else if (FullBackup.APPS_PREFIX.regionMatches(0, |
| 3859 | info.path, 0, FullBackup.APPS_PREFIX.length())) { |
| 3860 | // App content! Parse out the package name and domain |
| 3861 | |
| 3862 | // strip the apps/ prefix |
| 3863 | info.path = info.path.substring(FullBackup.APPS_PREFIX.length()); |
| 3864 | |
| 3865 | // extract the package name |
| 3866 | int slash = info.path.indexOf('/'); |
| 3867 | if (slash < 0) throw new IOException("Illegal semantic path in " + info.path); |
| 3868 | info.packageName = info.path.substring(0, slash); |
| 3869 | info.path = info.path.substring(slash+1); |
| 3870 | |
| 3871 | // if it's a manifest we're done, otherwise parse out the domains |
| 3872 | if (!info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 3873 | slash = info.path.indexOf('/'); |
| 3874 | if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path); |
| 3875 | info.domain = info.path.substring(0, slash); |
| 3876 | // validate that it's one of the domains we understand |
| 3877 | if (!info.domain.equals(FullBackup.APK_TREE_TOKEN) |
| 3878 | && !info.domain.equals(FullBackup.DATA_TREE_TOKEN) |
| 3879 | && !info.domain.equals(FullBackup.DATABASE_TREE_TOKEN) |
| 3880 | && !info.domain.equals(FullBackup.ROOT_TREE_TOKEN) |
| 3881 | && !info.domain.equals(FullBackup.SHAREDPREFS_TREE_TOKEN) |
| 3882 | && !info.domain.equals(FullBackup.OBB_TREE_TOKEN) |
| 3883 | && !info.domain.equals(FullBackup.CACHE_TREE_TOKEN)) { |
| 3884 | throw new IOException("Unrecognized domain " + info.domain); |
| 3885 | } |
| 3886 | |
| 3887 | info.path = info.path.substring(slash + 1); |
| 3888 | } |
| 3889 | } |
| 3890 | } catch (IOException e) { |
| 3891 | if (DEBUG) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3892 | Slog.e(TAG, "Parse error in header: " + e.getMessage()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3893 | HEXLOG(block); |
| 3894 | } |
| 3895 | throw e; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | return info; |
| 3899 | } |
| 3900 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3901 | private void HEXLOG(byte[] block) { |
| 3902 | int offset = 0; |
| 3903 | int todo = block.length; |
| 3904 | StringBuilder buf = new StringBuilder(64); |
| 3905 | while (todo > 0) { |
| 3906 | buf.append(String.format("%04x ", offset)); |
| 3907 | int numThisLine = (todo > 16) ? 16 : todo; |
| 3908 | for (int i = 0; i < numThisLine; i++) { |
| 3909 | buf.append(String.format("%02x ", block[offset+i])); |
| 3910 | } |
| 3911 | Slog.i("hexdump", buf.toString()); |
| 3912 | buf.setLength(0); |
| 3913 | todo -= numThisLine; |
| 3914 | offset += numThisLine; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3915 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3918 | // Read exactly the given number of bytes into a buffer at the stated offset. |
| 3919 | // Returns false if EOF is encountered before the requested number of bytes |
| 3920 | // could be read. |
| 3921 | int readExactly(InputStream in, byte[] buffer, int offset, int size) |
| 3922 | throws IOException { |
| 3923 | if (size <= 0) throw new IllegalArgumentException("size must be > 0"); |
| 3924 | |
| 3925 | int soFar = 0; |
| 3926 | while (soFar < size) { |
| 3927 | int nRead = in.read(buffer, offset + soFar, size - soFar); |
| 3928 | if (nRead <= 0) { |
| 3929 | if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar); |
| 3930 | break; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3931 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3932 | soFar += nRead; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3933 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3934 | return soFar; |
| 3935 | } |
| 3936 | |
| 3937 | boolean readTarHeader(InputStream instream, byte[] block) throws IOException { |
| 3938 | final int got = readExactly(instream, block, 0, 512); |
| 3939 | if (got == 0) return false; // Clean EOF |
| 3940 | if (got < 512) throw new IOException("Unable to read full block header"); |
| 3941 | mBytes += 512; |
| 3942 | return true; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | // overwrites 'info' fields based on the pax extended header |
| 3946 | boolean readPaxExtendedHeader(InputStream instream, FileMetadata info) |
| 3947 | throws IOException { |
| 3948 | // We should never see a pax extended header larger than this |
| 3949 | if (info.size > 32*1024) { |
| 3950 | Slog.w(TAG, "Suspiciously large pax header size " + info.size |
| 3951 | + " - aborting"); |
| 3952 | throw new IOException("Sanity failure: pax header size " + info.size); |
| 3953 | } |
| 3954 | |
| 3955 | // read whole blocks, not just the content size |
| 3956 | int numBlocks = (int)((info.size + 511) >> 9); |
| 3957 | byte[] data = new byte[numBlocks * 512]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3958 | if (readExactly(instream, data, 0, data.length) < data.length) { |
| 3959 | throw new IOException("Unable to read full pax header"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3960 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3961 | mBytes += data.length; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3962 | |
| 3963 | final int contentSize = (int) info.size; |
| 3964 | int offset = 0; |
| 3965 | do { |
| 3966 | // extract the line at 'offset' |
| 3967 | int eol = offset+1; |
| 3968 | while (eol < contentSize && data[eol] != ' ') eol++; |
| 3969 | if (eol >= contentSize) { |
| 3970 | // error: we just hit EOD looking for the end of the size field |
| 3971 | throw new IOException("Invalid pax data"); |
| 3972 | } |
| 3973 | // eol points to the space between the count and the key |
| 3974 | int linelen = (int) extractRadix(data, offset, eol - offset, 10); |
| 3975 | int key = eol + 1; // start of key=value |
| 3976 | eol = offset + linelen - 1; // trailing LF |
| 3977 | int value; |
| 3978 | for (value = key+1; data[value] != '=' && value <= eol; value++); |
| 3979 | if (value > eol) { |
| 3980 | throw new IOException("Invalid pax declaration"); |
| 3981 | } |
| 3982 | |
| 3983 | // pax requires that key/value strings be in UTF-8 |
| 3984 | String keyStr = new String(data, key, value-key, "UTF-8"); |
| 3985 | // -1 to strip the trailing LF |
| 3986 | String valStr = new String(data, value+1, eol-value-1, "UTF-8"); |
| 3987 | |
| 3988 | if ("path".equals(keyStr)) { |
| 3989 | info.path = valStr; |
| 3990 | } else if ("size".equals(keyStr)) { |
| 3991 | info.size = Long.parseLong(valStr); |
| 3992 | } else { |
| 3993 | if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key); |
| 3994 | } |
| 3995 | |
| 3996 | offset += linelen; |
| 3997 | } while (offset < contentSize); |
| 3998 | |
| 3999 | return true; |
| 4000 | } |
| 4001 | |
| 4002 | long extractRadix(byte[] data, int offset, int maxChars, int radix) |
| 4003 | throws IOException { |
| 4004 | long value = 0; |
| 4005 | final int end = offset + maxChars; |
| 4006 | for (int i = offset; i < end; i++) { |
| 4007 | final byte b = data[i]; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 4008 | // Numeric fields in tar can terminate with either NUL or SPC |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4009 | if (b == 0 || b == ' ') break; |
| 4010 | if (b < '0' || b > ('0' + radix - 1)) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4011 | throw new IOException("Invalid number in header: '" + (char)b + "' for radix " + radix); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4012 | } |
| 4013 | value = radix * value + (b - '0'); |
| 4014 | } |
| 4015 | return value; |
| 4016 | } |
| 4017 | |
| 4018 | String extractString(byte[] data, int offset, int maxChars) throws IOException { |
| 4019 | final int end = offset + maxChars; |
| 4020 | int eos = offset; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 4021 | // tar string fields terminate early with a NUL |
| 4022 | while (eos < end && data[eos] != 0) eos++; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4023 | return new String(data, offset, eos-offset, "US-ASCII"); |
| 4024 | } |
| 4025 | |
| 4026 | void sendStartRestore() { |
| 4027 | if (mObserver != null) { |
| 4028 | try { |
| 4029 | mObserver.onStartRestore(); |
| 4030 | } catch (RemoteException e) { |
| 4031 | Slog.w(TAG, "full restore observer went away: startRestore"); |
| 4032 | mObserver = null; |
| 4033 | } |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | void sendOnRestorePackage(String name) { |
| 4038 | if (mObserver != null) { |
| 4039 | try { |
| 4040 | // TODO: use a more user-friendly name string |
| 4041 | mObserver.onRestorePackage(name); |
| 4042 | } catch (RemoteException e) { |
| 4043 | Slog.w(TAG, "full restore observer went away: restorePackage"); |
| 4044 | mObserver = null; |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | void sendEndRestore() { |
| 4050 | if (mObserver != null) { |
| 4051 | try { |
| 4052 | mObserver.onEndRestore(); |
| 4053 | } catch (RemoteException e) { |
| 4054 | Slog.w(TAG, "full restore observer went away: endRestore"); |
| 4055 | mObserver = null; |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | } |
| 4060 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4061 | // ----- Restore handling ----- |
| 4062 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 4063 | private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) { |
| 4064 | // If the target resides on the system partition, we allow it to restore |
| 4065 | // data from the like-named package in a restore set even if the signatures |
| 4066 | // do not match. (Unlike general applications, those flashed to the system |
| 4067 | // partition will be signed with the device's platform certificate, so on |
| 4068 | // different phones the same system app will have different signatures.) |
| 4069 | if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4070 | if (DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check"); |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 4071 | return true; |
| 4072 | } |
| 4073 | |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 4074 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 4075 | // !!! TODO: is this the right policy? |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 4076 | Signature[] deviceSigs = target.signatures; |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4077 | if (MORE_DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 4078 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 4079 | if ((storedSigs == null || storedSigs.length == 0) |
| 4080 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 4081 | return true; |
| 4082 | } |
| 4083 | if (storedSigs == null || deviceSigs == null) { |
| 4084 | return false; |
| 4085 | } |
| 4086 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 4087 | // !!! TODO: this demands that every stored signature match one |
| 4088 | // that is present on device, and does not demand the converse. |
| 4089 | // Is this this right policy? |
| 4090 | int nStored = storedSigs.length; |
| 4091 | int nDevice = deviceSigs.length; |
| 4092 | |
| 4093 | for (int i=0; i < nStored; i++) { |
| 4094 | boolean match = false; |
| 4095 | for (int j=0; j < nDevice; j++) { |
| 4096 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 4097 | match = true; |
| 4098 | break; |
| 4099 | } |
| 4100 | } |
| 4101 | if (!match) { |
| 4102 | return false; |
| 4103 | } |
| 4104 | } |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4108 | enum RestoreState { |
| 4109 | INITIAL, |
| 4110 | DOWNLOAD_DATA, |
| 4111 | PM_METADATA, |
| 4112 | RUNNING_QUEUE, |
| 4113 | FINAL |
| 4114 | } |
| 4115 | |
| 4116 | class PerformRestoreTask implements BackupRestoreTask { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4117 | private IBackupTransport mTransport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4118 | private IRestoreObserver mObserver; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 4119 | private long mToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4120 | private PackageInfo mTargetPackage; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 4121 | private File mStateDir; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4122 | private int mPmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4123 | private boolean mNeedFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4124 | private HashSet<String> mFilterSet; |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4125 | private long mStartRealtime; |
| 4126 | private PackageManagerBackupAgent mPmAgent; |
| 4127 | private List<PackageInfo> mAgentPackages; |
| 4128 | private ArrayList<PackageInfo> mRestorePackages; |
| 4129 | private RestoreState mCurrentState; |
| 4130 | private int mCount; |
| 4131 | private boolean mFinished; |
| 4132 | private int mStatus; |
| 4133 | private File mBackupDataName; |
| 4134 | private File mNewStateName; |
| 4135 | private File mSavedStateName; |
| 4136 | private ParcelFileDescriptor mBackupData; |
| 4137 | private ParcelFileDescriptor mNewState; |
| 4138 | private PackageInfo mCurrentPackage; |
| 4139 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4140 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 4141 | class RestoreRequest { |
| 4142 | public PackageInfo app; |
| 4143 | public int storedAppVersion; |
| 4144 | |
| 4145 | RestoreRequest(PackageInfo _app, int _version) { |
| 4146 | app = _app; |
| 4147 | storedAppVersion = _version; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4151 | PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4152 | long restoreSetToken, PackageInfo targetPackage, int pmToken, |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4153 | boolean needFullBackup, String[] filterSet) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4154 | mCurrentState = RestoreState.INITIAL; |
| 4155 | mFinished = false; |
| 4156 | mPmAgent = null; |
| 4157 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4158 | mTransport = transport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4159 | mObserver = observer; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4160 | mToken = restoreSetToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4161 | mTargetPackage = targetPackage; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4162 | mPmToken = pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4163 | mNeedFullBackup = needFullBackup; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 4164 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4165 | if (filterSet != null) { |
| 4166 | mFilterSet = new HashSet<String>(); |
| 4167 | for (String pkg : filterSet) { |
| 4168 | mFilterSet.add(pkg); |
| 4169 | } |
| 4170 | } else { |
| 4171 | mFilterSet = null; |
| 4172 | } |
| 4173 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 4174 | try { |
| 4175 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 4176 | } catch (RemoteException e) { |
| 4177 | // can't happen; the transport is local |
| 4178 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4181 | // Execute one tick of whatever state machine the task implements |
| 4182 | @Override |
| 4183 | public void execute() { |
| 4184 | if (MORE_DEBUG) Slog.v(TAG, "*** Executing restore step: " + mCurrentState); |
| 4185 | switch (mCurrentState) { |
| 4186 | case INITIAL: |
| 4187 | beginRestore(); |
| 4188 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4189 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4190 | case DOWNLOAD_DATA: |
| 4191 | downloadRestoreData(); |
| 4192 | break; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4193 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4194 | case PM_METADATA: |
| 4195 | restorePmMetadata(); |
| 4196 | break; |
| 4197 | |
| 4198 | case RUNNING_QUEUE: |
| 4199 | restoreNextAgent(); |
| 4200 | break; |
| 4201 | |
| 4202 | case FINAL: |
| 4203 | if (!mFinished) finalizeRestore(); |
| 4204 | else { |
| 4205 | Slog.e(TAG, "Duplicate finish"); |
| 4206 | } |
| 4207 | mFinished = true; |
| 4208 | break; |
| 4209 | } |
| 4210 | } |
| 4211 | |
| 4212 | // Initialize and set up for the PM metadata restore, which comes first |
| 4213 | void beginRestore() { |
| 4214 | // Don't account time doing the restore as inactivity of the app |
| 4215 | // that has opened a restore session. |
| 4216 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4217 | |
| 4218 | // Assume error until we successfully init everything |
| 4219 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4220 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4221 | try { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4222 | // TODO: Log this before getAvailableRestoreSets, somehow |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4223 | EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 4224 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4225 | // Get the list of all packages which have backup enabled. |
| 4226 | // (Include the Package Manager metadata pseudo-package first.) |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4227 | mRestorePackages = new ArrayList<PackageInfo>(); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4228 | PackageInfo omPackage = new PackageInfo(); |
| 4229 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4230 | mRestorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4231 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4232 | mAgentPackages = allAgentPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4233 | if (mTargetPackage == null) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4234 | // if there's a filter set, strip out anything that isn't |
| 4235 | // present before proceeding |
| 4236 | if (mFilterSet != null) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4237 | for (int i = mAgentPackages.size() - 1; i >= 0; i--) { |
| 4238 | final PackageInfo pkg = mAgentPackages.get(i); |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4239 | if (! mFilterSet.contains(pkg.packageName)) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4240 | mAgentPackages.remove(i); |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4241 | } |
| 4242 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4243 | if (MORE_DEBUG) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4244 | Slog.i(TAG, "Post-filter package set for restore:"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4245 | for (PackageInfo p : mAgentPackages) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4246 | Slog.i(TAG, " " + p); |
| 4247 | } |
| 4248 | } |
| 4249 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4250 | mRestorePackages.addAll(mAgentPackages); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4251 | } else { |
| 4252 | // Just one package to attempt restore of |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4253 | mRestorePackages.add(mTargetPackage); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4254 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4255 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4256 | // let the observer know that we're running |
| 4257 | if (mObserver != null) { |
| 4258 | try { |
| 4259 | // !!! TODO: get an actual count from the transport after |
| 4260 | // its startRestore() runs? |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4261 | mObserver.restoreStarting(mRestorePackages.size()); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4262 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4263 | Slog.d(TAG, "Restore observer died at restoreStarting"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4264 | mObserver = null; |
| 4265 | } |
| 4266 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4267 | } catch (RemoteException e) { |
| 4268 | // Something has gone catastrophically wrong with the transport |
| 4269 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4270 | executeNextState(RestoreState.FINAL); |
| 4271 | return; |
| 4272 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4273 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4274 | mStatus = BackupConstants.TRANSPORT_OK; |
| 4275 | executeNextState(RestoreState.DOWNLOAD_DATA); |
| 4276 | } |
| 4277 | |
| 4278 | void downloadRestoreData() { |
| 4279 | // Note that the download phase can be very time consuming, but we're executing |
| 4280 | // it inline here on the looper. This is "okay" because it is not calling out to |
| 4281 | // third party code; the transport is "trusted," and so we assume it is being a |
| 4282 | // good citizen and timing out etc when appropriate. |
| 4283 | // |
| 4284 | // TODO: when appropriate, move the download off the looper and rearrange the |
| 4285 | // error handling around that. |
| 4286 | try { |
| 4287 | mStatus = mTransport.startRestore(mToken, |
| 4288 | mRestorePackages.toArray(new PackageInfo[0])); |
| 4289 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4290 | Slog.e(TAG, "Error starting restore operation"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4291 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4292 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4293 | return; |
| 4294 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4295 | } catch (RemoteException e) { |
| 4296 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4297 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4298 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4299 | executeNextState(RestoreState.FINAL); |
| 4300 | return; |
| 4301 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4302 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4303 | // Successful download of the data to be parceled out to the apps, so off we go. |
| 4304 | executeNextState(RestoreState.PM_METADATA); |
| 4305 | } |
| 4306 | |
| 4307 | void restorePmMetadata() { |
| 4308 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4309 | String packageName = mTransport.nextRestorePackage(); |
| 4310 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4311 | Slog.e(TAG, "Error getting first restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4312 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4313 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4314 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4315 | return; |
| 4316 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4317 | Slog.i(TAG, "No restore data available"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4318 | int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4319 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4320 | mStatus = BackupConstants.TRANSPORT_OK; |
| 4321 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4322 | return; |
| 4323 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4324 | Slog.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4325 | + "\", found only \"" + packageName + "\""); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4326 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4327 | "Package manager data missing"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4328 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4329 | return; |
| 4330 | } |
| 4331 | |
| 4332 | // Pull the Package Manager metadata from the restore set first |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4333 | PackageInfo omPackage = new PackageInfo(); |
| 4334 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 4335 | mPmAgent = new PackageManagerBackupAgent( |
| 4336 | mPackageManager, mAgentPackages); |
| 4337 | initiateOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(mPmAgent.onBind()), |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4338 | mNeedFullBackup); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4339 | // The PM agent called operationComplete() already, because our invocation |
| 4340 | // of it is process-local and therefore synchronous. That means that a |
| 4341 | // RUNNING_QUEUE message is already enqueued. Only if we're unable to |
| 4342 | // proceed with running the queue do we remove that pending message and |
| 4343 | // jump straight to the FINAL state. |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4344 | |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4345 | // Verify that the backup set includes metadata. If not, we can't do |
| 4346 | // signature/version verification etc, so we simply do not proceed with |
| 4347 | // the restore operation. |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4348 | if (!mPmAgent.hasMetadata()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4349 | Slog.e(TAG, "No restore metadata available, so not restoring settings"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4350 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4351 | "Package manager restore metadata missing"); |
| 4352 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4353 | mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this); |
| 4354 | executeNextState(RestoreState.FINAL); |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4355 | return; |
| 4356 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4357 | } catch (RemoteException e) { |
| 4358 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4359 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4360 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4361 | mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this); |
| 4362 | executeNextState(RestoreState.FINAL); |
| 4363 | return; |
| 4364 | } |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4365 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4366 | // Metadata is intact, so we can now run the restore queue. If we get here, |
| 4367 | // we have already enqueued the necessary next-step message on the looper. |
| 4368 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4369 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4370 | void restoreNextAgent() { |
| 4371 | try { |
| 4372 | String packageName = mTransport.nextRestorePackage(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4373 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4374 | if (packageName == null) { |
| 4375 | Slog.e(TAG, "Error getting next restore package"); |
| 4376 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4377 | executeNextState(RestoreState.FINAL); |
| 4378 | return; |
| 4379 | } else if (packageName.equals("")) { |
| 4380 | if (DEBUG) Slog.v(TAG, "No next package, finishing restore"); |
| 4381 | int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime); |
| 4382 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, mCount, millis); |
| 4383 | executeNextState(RestoreState.FINAL); |
| 4384 | return; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4385 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4386 | |
| 4387 | if (mObserver != null) { |
| 4388 | try { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4389 | mObserver.onUpdate(mCount, packageName); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4390 | } catch (RemoteException e) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4391 | Slog.d(TAG, "Restore observer died in onUpdate"); |
| 4392 | mObserver = null; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4393 | } |
| 4394 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4395 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4396 | Metadata metaInfo = mPmAgent.getRestoredMetadata(packageName); |
| 4397 | if (metaInfo == null) { |
| 4398 | Slog.e(TAG, "Missing metadata for " + packageName); |
| 4399 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4400 | "Package metadata missing"); |
| 4401 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4402 | return; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4403 | } |
| 4404 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4405 | PackageInfo packageInfo; |
| 4406 | try { |
| 4407 | int flags = PackageManager.GET_SIGNATURES; |
| 4408 | packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 4409 | } catch (NameNotFoundException e) { |
| 4410 | Slog.e(TAG, "Invalid package restoring data", e); |
| 4411 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4412 | "Package missing on device"); |
| 4413 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4414 | return; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4415 | } |
| 4416 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4417 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| 4418 | // Data is from a "newer" version of the app than we have currently |
| 4419 | // installed. If the app has not declared that it is prepared to |
| 4420 | // handle this case, we do not attempt the restore. |
| 4421 | if ((packageInfo.applicationInfo.flags |
| 4422 | & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) { |
| 4423 | String message = "Version " + metaInfo.versionCode |
| 4424 | + " > installed version " + packageInfo.versionCode; |
| 4425 | Slog.w(TAG, "Package " + packageName + ": " + message); |
| 4426 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 4427 | packageName, message); |
| 4428 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4429 | return; |
| 4430 | } else { |
| 4431 | if (DEBUG) Slog.v(TAG, "Version " + metaInfo.versionCode |
| 4432 | + " > installed " + packageInfo.versionCode |
| 4433 | + " but restoreAnyVersion"); |
| 4434 | } |
| 4435 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4436 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4437 | if (!signaturesMatch(metaInfo.signatures, packageInfo)) { |
| 4438 | Slog.w(TAG, "Signature mismatch restoring " + packageName); |
| 4439 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4440 | "Signature mismatch"); |
| 4441 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4442 | return; |
| 4443 | } |
| 4444 | |
| 4445 | if (DEBUG) Slog.v(TAG, "Package " + packageName |
| 4446 | + " restore version [" + metaInfo.versionCode |
| 4447 | + "] is compatible with installed version [" |
| 4448 | + packageInfo.versionCode + "]"); |
| 4449 | |
| 4450 | // Then set up and bind the agent |
| 4451 | IBackupAgent agent = bindToAgentSynchronous( |
| 4452 | packageInfo.applicationInfo, |
| 4453 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| 4454 | if (agent == null) { |
| 4455 | Slog.w(TAG, "Can't find backup agent for " + packageName); |
| 4456 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4457 | "Restore agent missing"); |
| 4458 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4459 | return; |
| 4460 | } |
| 4461 | |
| 4462 | // And then finally start the restore on this agent |
| 4463 | try { |
| 4464 | initiateOneRestore(packageInfo, metaInfo.versionCode, agent, mNeedFullBackup); |
| 4465 | ++mCount; |
| 4466 | } catch (Exception e) { |
| 4467 | Slog.e(TAG, "Error when attempting restore: " + e.toString()); |
| 4468 | agentErrorCleanup(); |
| 4469 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4470 | } |
| 4471 | } catch (RemoteException e) { |
| 4472 | Slog.e(TAG, "Unable to fetch restore data from transport"); |
| 4473 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4474 | executeNextState(RestoreState.FINAL); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4478 | void finalizeRestore() { |
| 4479 | if (MORE_DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver); |
| 4480 | |
| 4481 | try { |
| 4482 | mTransport.finishRestore(); |
| 4483 | } catch (RemoteException e) { |
| 4484 | Slog.e(TAG, "Error finishing restore", e); |
| 4485 | } |
| 4486 | |
| 4487 | if (mObserver != null) { |
| 4488 | try { |
| 4489 | mObserver.restoreFinished(mStatus); |
| 4490 | } catch (RemoteException e) { |
| 4491 | Slog.d(TAG, "Restore observer died at restoreFinished"); |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | // If this was a restoreAll operation, record that this was our |
| 4496 | // ancestral dataset, as well as the set of apps that are possibly |
| 4497 | // restoreable from the dataset |
| 4498 | if (mTargetPackage == null && mPmAgent != null) { |
| 4499 | mAncestralPackages = mPmAgent.getRestoredPackages(); |
| 4500 | mAncestralToken = mToken; |
| 4501 | writeRestoreTokens(); |
| 4502 | } |
| 4503 | |
| 4504 | // We must under all circumstances tell the Package Manager to |
| 4505 | // proceed with install notifications if it's waiting for us. |
| 4506 | if (mPmToken > 0) { |
| 4507 | if (MORE_DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken); |
| 4508 | try { |
| 4509 | mPackageManagerBinder.finishPackageInstall(mPmToken); |
| 4510 | } catch (RemoteException e) { /* can't happen */ } |
| 4511 | } |
| 4512 | |
| 4513 | // Furthermore we need to reset the session timeout clock |
| 4514 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4515 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, |
| 4516 | TIMEOUT_RESTORE_INTERVAL); |
| 4517 | |
| 4518 | // done; we can finally release the wakelock |
| 4519 | Slog.i(TAG, "Restore complete."); |
| 4520 | mWakelock.release(); |
| 4521 | } |
| 4522 | |
| 4523 | // Call asynchronously into the app, passing it the restore data. The next step |
| 4524 | // after this is always a callback, either operationComplete() or handleTimeout(). |
| 4525 | void initiateOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4526 | boolean needFullBackup) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4527 | mCurrentPackage = app; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4528 | final String packageName = app.packageName; |
| 4529 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4530 | if (DEBUG) Slog.d(TAG, "initiateOneRestore packageName=" + packageName); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4531 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4532 | // !!! TODO: get the dirs from the transport |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4533 | mBackupDataName = new File(mDataDir, packageName + ".restore"); |
| 4534 | mNewStateName = new File(mStateDir, packageName + ".new"); |
| 4535 | mSavedStateName = new File(mStateDir, packageName); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4536 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4537 | final int token = generateToken(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4538 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4539 | // Run the transport's restore pass |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4540 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4541 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4542 | ParcelFileDescriptor.MODE_CREATE | |
| 4543 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4544 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4545 | if (mTransport.getRestoreData(mBackupData) != BackupConstants.TRANSPORT_OK) { |
| Christopher Tate | 5f2f413 | 2011-09-26 13:10:38 -0700 | [diff] [blame] | 4546 | // Transport-level failure, so we wind everything up and |
| 4547 | // terminate the restore operation. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4548 | Slog.e(TAG, "Error getting restore data for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4549 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Christopher Tate | 5f2f413 | 2011-09-26 13:10:38 -0700 | [diff] [blame] | 4550 | mBackupData.close(); |
| 4551 | mBackupDataName.delete(); |
| 4552 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4553 | return; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | // Okay, we have the data. Now have the agent do the restore. |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4557 | mBackupData.close(); |
| 4558 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4559 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 4560 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4561 | mNewState = ParcelFileDescriptor.open(mNewStateName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4562 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4563 | ParcelFileDescriptor.MODE_CREATE | |
| 4564 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4565 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4566 | // Kick off the restore, checking for hung agents |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4567 | prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL, this); |
| 4568 | agent.doRestore(mBackupData, appVersionCode, mNewState, token, mBackupManagerBinder); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4569 | } catch (Exception e) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4570 | Slog.e(TAG, "Unable to call app for restore: " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4571 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString()); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4572 | agentErrorCleanup(); // clears any pending timeout messages as well |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4573 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4574 | // After a restore failure we go back to running the queue. If there |
| 4575 | // are no more packages to be restored that will be handled by the |
| 4576 | // next step. |
| 4577 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4578 | } |
| 4579 | } |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4580 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4581 | void agentErrorCleanup() { |
| 4582 | // If the agent fails restore, it might have put the app's data |
| 4583 | // into an incoherent state. For consistency we wipe its data |
| 4584 | // again in this case before continuing with normal teardown |
| 4585 | clearApplicationDataSynchronous(mCurrentPackage.packageName); |
| 4586 | agentCleanup(); |
| 4587 | } |
| 4588 | |
| 4589 | void agentCleanup() { |
| 4590 | mBackupDataName.delete(); |
| 4591 | try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {} |
| 4592 | try { if (mNewState != null) mNewState.close(); } catch (IOException e) {} |
| 4593 | mBackupData = mNewState = null; |
| 4594 | |
| 4595 | // if everything went okay, remember the recorded state now |
| 4596 | // |
| 4597 | // !!! TODO: the restored data should be migrated on the server |
| 4598 | // side into the current dataset. In that case the new state file |
| 4599 | // we just created would reflect the data already extant in the |
| 4600 | // backend, so there'd be nothing more to do. Until that happens, |
| 4601 | // however, we need to make sure that we record the data to the |
| 4602 | // current backend dataset. (Yes, this means shipping the data over |
| 4603 | // the wire in both directions. That's bad, but consistency comes |
| 4604 | // first, then efficiency.) Once we introduce server-side data |
| 4605 | // migration to the newly-restored device's dataset, we will change |
| 4606 | // the following from a discard of the newly-written state to the |
| 4607 | // "correct" operation of renaming into the canonical state blob. |
| 4608 | mNewStateName.delete(); // TODO: remove; see above comment |
| 4609 | //mNewStateName.renameTo(mSavedStateName); // TODO: replace with this |
| 4610 | |
| 4611 | // If this wasn't the PM pseudopackage, tear down the agent side |
| 4612 | if (mCurrentPackage.applicationInfo != null) { |
| 4613 | // unbind and tidy up even on timeout or failure |
| 4614 | try { |
| 4615 | mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo); |
| 4616 | |
| 4617 | // The agent was probably running with a stub Application object, |
| 4618 | // which isn't a valid run mode for the main app logic. Shut |
| 4619 | // down the app so that next time it's launched, it gets the |
| 4620 | // usual full initialization. Note that this is only done for |
| 4621 | // full-system restores: when a single app has requested a restore, |
| 4622 | // it is explicitly not killed following that operation. |
| 4623 | if (mTargetPackage == null && (mCurrentPackage.applicationInfo.flags |
| 4624 | & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) { |
| 4625 | if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of " |
| 4626 | + mCurrentPackage.applicationInfo.processName); |
| 4627 | mActivityManager.killApplicationProcess( |
| 4628 | mCurrentPackage.applicationInfo.processName, |
| 4629 | mCurrentPackage.applicationInfo.uid); |
| 4630 | } |
| 4631 | } catch (RemoteException e) { |
| 4632 | // can't happen; we run in the same process as the activity manager |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4633 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4634 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4635 | |
| 4636 | // The caller is responsible for reestablishing the state machine; our |
| 4637 | // responsibility here is to clear the decks for whatever comes next. |
| 4638 | mBackupHandler.removeMessages(MSG_TIMEOUT, this); |
| 4639 | synchronized (mCurrentOpLock) { |
| 4640 | mCurrentOperations.clear(); |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | // A call to agent.doRestore() has been positively acknowledged as complete |
| 4645 | @Override |
| 4646 | public void operationComplete() { |
| 4647 | int size = (int) mBackupDataName.length(); |
| 4648 | EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, mCurrentPackage.packageName, size); |
| 4649 | // Just go back to running the restore queue |
| 4650 | agentCleanup(); |
| 4651 | |
| 4652 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4653 | } |
| 4654 | |
| 4655 | // A call to agent.doRestore() has timed out |
| 4656 | @Override |
| 4657 | public void handleTimeout() { |
| 4658 | Slog.e(TAG, "Timeout restoring application " + mCurrentPackage.packageName); |
| 4659 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 4660 | mCurrentPackage.packageName, "restore timeout"); |
| 4661 | // Handle like an agent that threw on invocation: wipe it and go on to the next |
| 4662 | agentErrorCleanup(); |
| 4663 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4664 | } |
| 4665 | |
| 4666 | void executeNextState(RestoreState nextState) { |
| 4667 | if (MORE_DEBUG) Slog.i(TAG, " => executing next step on " |
| 4668 | + this + " nextState=" + nextState); |
| 4669 | mCurrentState = nextState; |
| 4670 | Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this); |
| 4671 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4675 | class PerformClearTask implements Runnable { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4676 | IBackupTransport mTransport; |
| 4677 | PackageInfo mPackage; |
| 4678 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4679 | PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4680 | mTransport = transport; |
| 4681 | mPackage = packageInfo; |
| 4682 | } |
| 4683 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4684 | public void run() { |
| 4685 | try { |
| 4686 | // Clear the on-device backup state to ensure a full backup next time |
| 4687 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 4688 | File stateFile = new File(stateDir, mPackage.packageName); |
| 4689 | stateFile.delete(); |
| 4690 | |
| 4691 | // Tell the transport to remove all the persistent storage for the app |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4692 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4693 | mTransport.clearBackupData(mPackage); |
| 4694 | } catch (RemoteException e) { |
| 4695 | // can't happen; the transport is local |
| Christopher Tate | 0abf6a0 | 2012-03-23 17:45:15 -0700 | [diff] [blame] | 4696 | } catch (Exception e) { |
| 4697 | Slog.e(TAG, "Transport threw attempting to clear data for " + mPackage); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4698 | } finally { |
| 4699 | try { |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4700 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4701 | mTransport.finishBackup(); |
| 4702 | } catch (RemoteException e) { |
| 4703 | // can't happen; the transport is local |
| 4704 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4705 | |
| 4706 | // Last but not least, release the cpu |
| 4707 | mWakelock.release(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4708 | } |
| 4709 | } |
| 4710 | } |
| 4711 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4712 | class PerformInitializeTask implements Runnable { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4713 | HashSet<String> mQueue; |
| 4714 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4715 | PerformInitializeTask(HashSet<String> transportNames) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4716 | mQueue = transportNames; |
| 4717 | } |
| 4718 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4719 | public void run() { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4720 | try { |
| 4721 | for (String transportName : mQueue) { |
| 4722 | IBackupTransport transport = getTransport(transportName); |
| 4723 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4724 | Slog.e(TAG, "Requested init for " + transportName + " but not found"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4725 | continue; |
| 4726 | } |
| 4727 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4728 | Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4729 | EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName()); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4730 | long startRealtime = SystemClock.elapsedRealtime(); |
| 4731 | int status = transport.initializeDevice(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4732 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4733 | if (status == BackupConstants.TRANSPORT_OK) { |
| 4734 | status = transport.finishBackup(); |
| 4735 | } |
| 4736 | |
| 4737 | // Okay, the wipe really happened. Clean up our local bookkeeping. |
| 4738 | if (status == BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4739 | Slog.i(TAG, "Device init successful"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4740 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4741 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4742 | resetBackupState(new File(mBaseStateDir, transport.transportDirName())); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4743 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4744 | synchronized (mQueueLock) { |
| 4745 | recordInitPendingLocked(false, transportName); |
| 4746 | } |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4747 | } else { |
| 4748 | // If this didn't work, requeue this one and try again |
| 4749 | // after a suitable interval |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4750 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4751 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4752 | synchronized (mQueueLock) { |
| 4753 | recordInitPendingLocked(true, transportName); |
| 4754 | } |
| 4755 | // do this via another alarm to make sure of the wakelock states |
| 4756 | long delay = transport.requestBackupTime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4757 | if (DEBUG) Slog.w(TAG, "init failed on " |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4758 | + transportName + " resched in " + delay); |
| 4759 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 4760 | System.currentTimeMillis() + delay, mRunInitIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4761 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4762 | } |
| 4763 | } catch (RemoteException e) { |
| 4764 | // can't happen; the transports are local |
| 4765 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4766 | Slog.e(TAG, "Unexpected error performing init", e); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4767 | } finally { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 4768 | // Done; release the wakelock |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4769 | mWakelock.release(); |
| 4770 | } |
| 4771 | } |
| 4772 | } |
| 4773 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4774 | private void dataChangedImpl(String packageName) { |
| 4775 | HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4776 | dataChangedImpl(packageName, targets); |
| 4777 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4778 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4779 | private void dataChangedImpl(String packageName, HashSet<ApplicationInfo> targets) { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4780 | // Record that we need a backup pass for the caller. Since multiple callers |
| 4781 | // may share a uid, we need to note all candidates within that uid and schedule |
| 4782 | // a backup pass for each of them. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4783 | EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4784 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4785 | if (targets == null) { |
| 4786 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4787 | + " uid=" + Binder.getCallingUid()); |
| 4788 | return; |
| 4789 | } |
| 4790 | |
| 4791 | synchronized (mQueueLock) { |
| 4792 | // Note that this client has made data changes that need to be backed up |
| 4793 | for (ApplicationInfo app : targets) { |
| 4794 | // validate the caller-supplied package name against the known set of |
| 4795 | // packages associated with this uid |
| 4796 | if (app.packageName.equals(packageName)) { |
| 4797 | // Add the caller to the set of pending backups. If there is |
| 4798 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4799 | BackupRequest req = new BackupRequest(packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 4800 | if (mPendingBackups.put(app.packageName, req) == null) { |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 4801 | if (DEBUG) Slog.d(TAG, "Now staging backup of " + packageName); |
| 4802 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4803 | // Journal this request in case of crash. The put() |
| 4804 | // operation returned null when this package was not already |
| 4805 | // in the set; we want to avoid touching the disk redundantly. |
| 4806 | writeToJournalLocked(packageName); |
| 4807 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4808 | if (MORE_DEBUG) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4809 | int numKeys = mPendingBackups.size(); |
| 4810 | Slog.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| 4811 | for (BackupRequest b : mPendingBackups.values()) { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4812 | Slog.d(TAG, " + " + b); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | } |
| 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | // Note: packageName is currently unused, but may be in the future |
| 4822 | private HashSet<ApplicationInfo> dataChangedTargets(String packageName) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4823 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4824 | // backup of its own data. |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4825 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4826 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4827 | synchronized (mBackupParticipants) { |
| 4828 | return mBackupParticipants.get(Binder.getCallingUid()); |
| 4829 | } |
| 4830 | } |
| 4831 | |
| 4832 | // a caller with full permission can ask to back up any participating app |
| 4833 | // !!! TODO: allow backup of ANY app? |
| 4834 | HashSet<ApplicationInfo> targets = new HashSet<ApplicationInfo>(); |
| 4835 | synchronized (mBackupParticipants) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4836 | int N = mBackupParticipants.size(); |
| 4837 | for (int i = 0; i < N; i++) { |
| 4838 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4839 | if (s != null) { |
| 4840 | targets.addAll(s); |
| 4841 | } |
| 4842 | } |
| 4843 | } |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4844 | return targets; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4845 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4846 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4847 | private void writeToJournalLocked(String str) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4848 | RandomAccessFile out = null; |
| 4849 | try { |
| 4850 | if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir); |
| 4851 | out = new RandomAccessFile(mJournal, "rws"); |
| 4852 | out.seek(out.length()); |
| 4853 | out.writeUTF(str); |
| 4854 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4855 | Slog.e(TAG, "Can't write " + str + " to backup journal", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4856 | mJournal = null; |
| 4857 | } finally { |
| 4858 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4862 | // ----- IBackupManager binder interface ----- |
| 4863 | |
| 4864 | public void dataChanged(final String packageName) { |
| 4865 | final HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4866 | if (targets == null) { |
| 4867 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4868 | + " uid=" + Binder.getCallingUid()); |
| 4869 | return; |
| 4870 | } |
| 4871 | |
| 4872 | mBackupHandler.post(new Runnable() { |
| 4873 | public void run() { |
| 4874 | dataChangedImpl(packageName, targets); |
| 4875 | } |
| 4876 | }); |
| 4877 | } |
| 4878 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4879 | // Clear the given package's backup data from the current transport |
| 4880 | public void clearBackupData(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4881 | if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4882 | PackageInfo info; |
| 4883 | try { |
| 4884 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 4885 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4886 | Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4887 | return; |
| 4888 | } |
| 4889 | |
| 4890 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4891 | // wipe of its own backed-up data. |
| 4892 | HashSet<ApplicationInfo> apps; |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 4893 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4894 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 4895 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 4896 | } else { |
| 4897 | // a caller with full permission can ask to back up any participating app |
| 4898 | // !!! TODO: allow data-clear of ANY app? |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4899 | if (DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4900 | apps = new HashSet<ApplicationInfo>(); |
| 4901 | int N = mBackupParticipants.size(); |
| 4902 | for (int i = 0; i < N; i++) { |
| 4903 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4904 | if (s != null) { |
| 4905 | apps.addAll(s); |
| 4906 | } |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | // now find the given package in the set of candidate apps |
| 4911 | for (ApplicationInfo app : apps) { |
| 4912 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4913 | if (DEBUG) Slog.v(TAG, "Found the app - running clear process"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4914 | // found it; fire off the clear request |
| 4915 | synchronized (mQueueLock) { |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4916 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4917 | mWakelock.acquire(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4918 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 4919 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 4920 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4921 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4922 | } |
| 4923 | break; |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 4928 | // Run a backup pass immediately for any applications that have declared |
| 4929 | // that they have pending updates. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4930 | public void backupNow() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4931 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4932 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4933 | if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4934 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4935 | // Because the alarms we are using can jitter, and we want an *immediate* |
| 4936 | // backup pass to happen, we restart the timer beginning with "next time," |
| 4937 | // then manually fire the backup trigger intent ourselves. |
| 4938 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4939 | try { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4940 | mRunBackupIntent.send(); |
| 4941 | } catch (PendingIntent.CanceledException e) { |
| 4942 | // should never happen |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4943 | Slog.e(TAG, "run-backup intent cancelled!"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4944 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4945 | } |
| 4946 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4947 | |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 4948 | boolean deviceIsProvisioned() { |
| 4949 | final ContentResolver resolver = mContext.getContentResolver(); |
| 4950 | return (Settings.Secure.getInt(resolver, Settings.Secure.DEVICE_PROVISIONED, 0) != 0); |
| 4951 | } |
| 4952 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4953 | // Run a *full* backup pass for the given package, writing the resulting data stream |
| 4954 | // to the supplied file descriptor. This method is synchronous and does not return |
| 4955 | // to the caller until the backup has been completed. |
| 4956 | public void fullBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeShared, |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 4957 | boolean doAllApps, boolean includeSystem, String[] pkgList) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4958 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 4959 | |
| 4960 | // Validate |
| 4961 | if (!doAllApps) { |
| 4962 | if (!includeShared) { |
| 4963 | // If we're backing up shared data (sdcard or equivalent), then we can run |
| 4964 | // without any supplied app names. Otherwise, we'd be doing no work, so |
| 4965 | // report the error. |
| 4966 | if (pkgList == null || pkgList.length == 0) { |
| 4967 | throw new IllegalArgumentException( |
| 4968 | "Backup requested but neither shared nor any apps named"); |
| 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4973 | long oldId = Binder.clearCallingIdentity(); |
| 4974 | try { |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 4975 | // Doesn't make sense to do a full backup prior to setup |
| 4976 | if (!deviceIsProvisioned()) { |
| 4977 | Slog.i(TAG, "Full backup not supported before setup"); |
| 4978 | return; |
| 4979 | } |
| 4980 | |
| 4981 | if (DEBUG) Slog.v(TAG, "Requesting full backup: apks=" + includeApks |
| 4982 | + " shared=" + includeShared + " all=" + doAllApps |
| 4983 | + " pkgs=" + pkgList); |
| 4984 | Slog.i(TAG, "Beginning full backup..."); |
| 4985 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4986 | FullBackupParams params = new FullBackupParams(fd, includeApks, includeShared, |
| Christopher Tate | 240c7d2 | 2011-10-03 18:13:44 -0700 | [diff] [blame] | 4987 | doAllApps, includeSystem, pkgList); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4988 | final int token = generateToken(); |
| 4989 | synchronized (mFullConfirmations) { |
| 4990 | mFullConfirmations.put(token, params); |
| 4991 | } |
| 4992 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4993 | // start up the confirmation UI |
| 4994 | if (DEBUG) Slog.d(TAG, "Starting backup confirmation UI, token=" + token); |
| 4995 | if (!startConfirmationUi(token, FullBackup.FULL_BACKUP_INTENT_ACTION)) { |
| 4996 | Slog.e(TAG, "Unable to launch full backup confirmation"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4997 | mFullConfirmations.delete(token); |
| 4998 | return; |
| 4999 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5000 | |
| 5001 | // make sure the screen is lit for the user interaction |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5002 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 5003 | |
| 5004 | // start the confirmation countdown |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5005 | startConfirmationTimeout(token, params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5006 | |
| 5007 | // wait for the backup to be performed |
| 5008 | if (DEBUG) Slog.d(TAG, "Waiting for full backup completion..."); |
| 5009 | waitForCompletion(params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5010 | } finally { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5011 | try { |
| 5012 | fd.close(); |
| 5013 | } catch (IOException e) { |
| 5014 | // just eat it |
| 5015 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5016 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 5017 | Slog.d(TAG, "Full backup processing complete."); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5018 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | public void fullRestore(ParcelFileDescriptor fd) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5022 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullRestore"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5023 | |
| 5024 | long oldId = Binder.clearCallingIdentity(); |
| 5025 | |
| 5026 | try { |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 5027 | // Check whether the device has been provisioned -- we don't handle |
| 5028 | // full restores prior to completing the setup process. |
| 5029 | if (!deviceIsProvisioned()) { |
| 5030 | Slog.i(TAG, "Full restore not permitted before setup"); |
| 5031 | return; |
| 5032 | } |
| 5033 | |
| 5034 | Slog.i(TAG, "Beginning full restore..."); |
| 5035 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5036 | FullRestoreParams params = new FullRestoreParams(fd); |
| 5037 | final int token = generateToken(); |
| 5038 | synchronized (mFullConfirmations) { |
| 5039 | mFullConfirmations.put(token, params); |
| 5040 | } |
| 5041 | |
| 5042 | // start up the confirmation UI |
| 5043 | if (DEBUG) Slog.d(TAG, "Starting restore confirmation UI, token=" + token); |
| 5044 | if (!startConfirmationUi(token, FullBackup.FULL_RESTORE_INTENT_ACTION)) { |
| 5045 | Slog.e(TAG, "Unable to launch full restore confirmation"); |
| 5046 | mFullConfirmations.delete(token); |
| 5047 | return; |
| 5048 | } |
| 5049 | |
| 5050 | // make sure the screen is lit for the user interaction |
| 5051 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 5052 | |
| 5053 | // start the confirmation countdown |
| 5054 | startConfirmationTimeout(token, params); |
| 5055 | |
| 5056 | // wait for the restore to be performed |
| 5057 | if (DEBUG) Slog.d(TAG, "Waiting for full restore completion..."); |
| 5058 | waitForCompletion(params); |
| 5059 | } finally { |
| 5060 | try { |
| 5061 | fd.close(); |
| 5062 | } catch (IOException e) { |
| 5063 | Slog.w(TAG, "Error trying to close fd after full restore: " + e); |
| 5064 | } |
| 5065 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame] | 5066 | Slog.i(TAG, "Full restore processing complete."); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | boolean startConfirmationUi(int token, String action) { |
| 5071 | try { |
| 5072 | Intent confIntent = new Intent(action); |
| 5073 | confIntent.setClassName("com.android.backupconfirm", |
| 5074 | "com.android.backupconfirm.BackupRestoreConfirmation"); |
| 5075 | confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token); |
| 5076 | confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 5077 | mContext.startActivity(confIntent); |
| 5078 | } catch (ActivityNotFoundException e) { |
| 5079 | return false; |
| 5080 | } |
| 5081 | return true; |
| 5082 | } |
| 5083 | |
| 5084 | void startConfirmationTimeout(int token, FullParams params) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5085 | if (MORE_DEBUG) Slog.d(TAG, "Posting conf timeout msg after " |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5086 | + TIMEOUT_FULL_CONFIRMATION + " millis"); |
| 5087 | Message msg = mBackupHandler.obtainMessage(MSG_FULL_CONFIRMATION_TIMEOUT, |
| 5088 | token, 0, params); |
| 5089 | mBackupHandler.sendMessageDelayed(msg, TIMEOUT_FULL_CONFIRMATION); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | void waitForCompletion(FullParams params) { |
| 5093 | synchronized (params.latch) { |
| 5094 | while (params.latch.get() == false) { |
| 5095 | try { |
| 5096 | params.latch.wait(); |
| 5097 | } catch (InterruptedException e) { /* never interrupted */ } |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | void signalFullBackupRestoreCompletion(FullParams params) { |
| 5103 | synchronized (params.latch) { |
| 5104 | params.latch.set(true); |
| 5105 | params.latch.notifyAll(); |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | // Confirm that the previously-requested full backup/restore operation can proceed. This |
| 5110 | // is used to require a user-facing disclosure about the operation. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5111 | @Override |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5112 | public void acknowledgeFullBackupOrRestore(int token, boolean allow, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 5113 | String curPassword, String encPpassword, IFullBackupRestoreObserver observer) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5114 | if (DEBUG) Slog.d(TAG, "acknowledgeFullBackupOrRestore : token=" + token |
| 5115 | + " allow=" + allow); |
| 5116 | |
| 5117 | // TODO: possibly require not just this signature-only permission, but even |
| 5118 | // require that the specific designated confirmation-UI app uid is the caller? |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5119 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "acknowledgeFullBackupOrRestore"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5120 | |
| 5121 | long oldId = Binder.clearCallingIdentity(); |
| 5122 | try { |
| 5123 | |
| 5124 | FullParams params; |
| 5125 | synchronized (mFullConfirmations) { |
| 5126 | params = mFullConfirmations.get(token); |
| 5127 | if (params != null) { |
| 5128 | mBackupHandler.removeMessages(MSG_FULL_CONFIRMATION_TIMEOUT, params); |
| 5129 | mFullConfirmations.delete(token); |
| 5130 | |
| 5131 | if (allow) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5132 | final int verb = params instanceof FullBackupParams |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5133 | ? MSG_RUN_FULL_BACKUP |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5134 | : MSG_RUN_FULL_RESTORE; |
| 5135 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 5136 | params.observer = observer; |
| 5137 | params.curPassword = curPassword; |
| Christopher Tate | 32418be | 2011-10-10 13:51:12 -0700 | [diff] [blame] | 5138 | |
| 5139 | boolean isEncrypted; |
| 5140 | try { |
| 5141 | isEncrypted = (mMountService.getEncryptionState() != MountService.ENCRYPTION_STATE_NONE); |
| 5142 | if (isEncrypted) Slog.w(TAG, "Device is encrypted; forcing enc password"); |
| 5143 | } catch (RemoteException e) { |
| 5144 | // couldn't contact the mount service; fail "safe" and assume encryption |
| 5145 | Slog.e(TAG, "Unable to contact mount service!"); |
| 5146 | isEncrypted = true; |
| 5147 | } |
| 5148 | params.encryptPassword = (isEncrypted) ? curPassword : encPpassword; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 5149 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 5150 | if (DEBUG) Slog.d(TAG, "Sending conf message with verb " + verb); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 5151 | mWakelock.acquire(); |
| 5152 | Message msg = mBackupHandler.obtainMessage(verb, params); |
| 5153 | mBackupHandler.sendMessage(msg); |
| 5154 | } else { |
| 5155 | Slog.w(TAG, "User rejected full backup/restore operation"); |
| 5156 | // indicate completion without having actually transferred any data |
| 5157 | signalFullBackupRestoreCompletion(params); |
| 5158 | } |
| 5159 | } else { |
| 5160 | Slog.w(TAG, "Attempted to ack full backup/restore with invalid token"); |
| 5161 | } |
| 5162 | } |
| 5163 | } finally { |
| 5164 | Binder.restoreCallingIdentity(oldId); |
| 5165 | } |
| 5166 | } |
| 5167 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5168 | // Enable/disable the backup service |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5169 | public void setBackupEnabled(boolean enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 5170 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5171 | "setBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5172 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5173 | Slog.i(TAG, "Backup enabled => " + enable); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 5174 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5175 | boolean wasEnabled = mEnabled; |
| 5176 | synchronized (this) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 5177 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 5178 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5179 | mEnabled = enable; |
| 5180 | } |
| 5181 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 5182 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5183 | if (enable && !wasEnabled && mProvisioned) { |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 5184 | // if we've just been enabled, start scheduling backup passes |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5185 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 5186 | } else if (!enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 5187 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5188 | if (DEBUG) Slog.i(TAG, "Opting out of backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 5189 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 5190 | mAlarmManager.cancel(mRunBackupIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 5191 | |
| 5192 | // This also constitutes an opt-out, so we wipe any data for |
| 5193 | // this device from the backend. We start that process with |
| 5194 | // an alarm in order to guarantee wakelock states. |
| 5195 | if (wasEnabled && mProvisioned) { |
| 5196 | // NOTE: we currently flush every registered transport, not just |
| 5197 | // the currently-active one. |
| 5198 | HashSet<String> allTransports; |
| 5199 | synchronized (mTransports) { |
| 5200 | allTransports = new HashSet<String>(mTransports.keySet()); |
| 5201 | } |
| 5202 | // build the set of transports for which we are posting an init |
| 5203 | for (String transport : allTransports) { |
| 5204 | recordInitPendingLocked(true, transport); |
| 5205 | } |
| 5206 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), |
| 5207 | mRunInitIntent); |
| 5208 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5209 | } |
| 5210 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 5211 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5212 | |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5213 | // Enable/disable automatic restore of app data at install time |
| 5214 | public void setAutoRestore(boolean doAutoRestore) { |
| 5215 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5216 | "setAutoRestore"); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5217 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5218 | Slog.i(TAG, "Auto restore => " + doAutoRestore); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5219 | |
| 5220 | synchronized (this) { |
| 5221 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 5222 | Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0); |
| 5223 | mAutoRestore = doAutoRestore; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5227 | // Mark the backup service as having been provisioned |
| 5228 | public void setBackupProvisioned(boolean available) { |
| 5229 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5230 | "setBackupProvisioned"); |
| 5231 | |
| 5232 | boolean wasProvisioned = mProvisioned; |
| 5233 | synchronized (this) { |
| 5234 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 5235 | Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0); |
| 5236 | mProvisioned = available; |
| 5237 | } |
| 5238 | |
| 5239 | synchronized (mQueueLock) { |
| 5240 | if (available && !wasProvisioned && mEnabled) { |
| 5241 | // we're now good to go, so start the backup alarms |
| 5242 | startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL); |
| 5243 | } else if (!available) { |
| 5244 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5245 | Slog.w(TAG, "Backup service no longer provisioned"); |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5246 | mAlarmManager.cancel(mRunBackupIntent); |
| 5247 | } |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | private void startBackupAlarmsLocked(long delayBeforeFirstBackup) { |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 5252 | // We used to use setInexactRepeating(), but that may be linked to |
| 5253 | // backups running at :00 more often than not, creating load spikes. |
| 5254 | // Schedule at an exact time for now, and also add a bit of "fuzz". |
| 5255 | |
| 5256 | Random random = new Random(); |
| 5257 | long when = System.currentTimeMillis() + delayBeforeFirstBackup + |
| 5258 | random.nextInt(FUZZ_MILLIS); |
| 5259 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, |
| 5260 | BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5261 | mNextBackupPass = when; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5264 | // Report whether the backup mechanism is currently enabled |
| 5265 | public boolean isBackupEnabled() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5266 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5267 | return mEnabled; // no need to synchronize just to read it |
| 5268 | } |
| 5269 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5270 | // Report the name of the currently active transport |
| 5271 | public String getCurrentTransport() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5272 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 5273 | "getCurrentTransport"); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5274 | if (MORE_DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5275 | return mCurrentTransport; |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5278 | // Report all known, available backup transports |
| 5279 | public String[] listAllTransports() { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 5280 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5281 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5282 | String[] list = null; |
| 5283 | ArrayList<String> known = new ArrayList<String>(); |
| 5284 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 5285 | if (entry.getValue() != null) { |
| 5286 | known.add(entry.getKey()); |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | if (known.size() > 0) { |
| 5291 | list = new String[known.size()]; |
| 5292 | known.toArray(list); |
| 5293 | } |
| 5294 | return list; |
| 5295 | } |
| 5296 | |
| 5297 | // Select which transport to use for the next backup operation. If the given |
| 5298 | // name is not one of the available transports, no action is taken and the method |
| 5299 | // returns null. |
| 5300 | public String selectBackupTransport(String transport) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5301 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5302 | |
| 5303 | synchronized (mTransports) { |
| 5304 | String prevTransport = null; |
| 5305 | if (mTransports.get(transport) != null) { |
| 5306 | prevTransport = mCurrentTransport; |
| 5307 | mCurrentTransport = transport; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 5308 | Settings.Secure.putString(mContext.getContentResolver(), |
| 5309 | Settings.Secure.BACKUP_TRANSPORT, transport); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5310 | Slog.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5311 | + " returning " + prevTransport); |
| 5312 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5313 | Slog.w(TAG, "Attempt to select unavailable transport " + transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5314 | } |
| 5315 | return prevTransport; |
| 5316 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5317 | } |
| 5318 | |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5319 | // Supply the configuration Intent for the given transport. If the name is not one |
| 5320 | // of the available transports, or if the transport does not supply any configuration |
| 5321 | // UI, the method returns null. |
| 5322 | public Intent getConfigurationIntent(String transportName) { |
| 5323 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5324 | "getConfigurationIntent"); |
| 5325 | |
| 5326 | synchronized (mTransports) { |
| 5327 | final IBackupTransport transport = mTransports.get(transportName); |
| 5328 | if (transport != null) { |
| 5329 | try { |
| 5330 | final Intent intent = transport.configurationIntent(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5331 | if (MORE_DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent " |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5332 | + intent); |
| 5333 | return intent; |
| 5334 | } catch (RemoteException e) { |
| 5335 | /* fall through to return null */ |
| 5336 | } |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | return null; |
| 5341 | } |
| 5342 | |
| 5343 | // Supply the configuration summary string for the given transport. If the name is |
| 5344 | // not one of the available transports, or if the transport does not supply any |
| 5345 | // summary / destination string, the method can return null. |
| 5346 | // |
| 5347 | // This string is used VERBATIM as the summary text of the relevant Settings item! |
| 5348 | public String getDestinationString(String transportName) { |
| 5349 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5350 | "getDestinationString"); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5351 | |
| 5352 | synchronized (mTransports) { |
| 5353 | final IBackupTransport transport = mTransports.get(transportName); |
| 5354 | if (transport != null) { |
| 5355 | try { |
| 5356 | final String text = transport.currentDestinationString(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5357 | if (MORE_DEBUG) Slog.d(TAG, "getDestinationString() returning " + text); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5358 | return text; |
| 5359 | } catch (RemoteException e) { |
| 5360 | /* fall through to return null */ |
| 5361 | } |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | return null; |
| 5366 | } |
| 5367 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5368 | // Callback: a requested backup agent has been instantiated. This should only |
| 5369 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5370 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5371 | synchronized(mAgentConnectLock) { |
| 5372 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5373 | Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5374 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 5375 | mConnectedAgent = agent; |
| 5376 | mConnecting = false; |
| 5377 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5378 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5379 | + " claiming agent connected"); |
| 5380 | } |
| 5381 | mAgentConnectLock.notifyAll(); |
| 5382 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 5386 | // 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] | 5387 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5388 | public void agentDisconnected(String packageName) { |
| 5389 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5390 | synchronized(mAgentConnectLock) { |
| 5391 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 5392 | mConnectedAgent = null; |
| 5393 | mConnecting = false; |
| 5394 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5395 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5396 | + " claiming agent disconnected"); |
| 5397 | } |
| 5398 | mAgentConnectLock.notifyAll(); |
| 5399 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5400 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5401 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5402 | // An application being installed will need a restore pass, then the Package Manager |
| 5403 | // will need to be told when the restore is finished. |
| 5404 | public void restoreAtInstall(String packageName, int token) { |
| 5405 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5406 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5407 | + " attemping install-time restore"); |
| 5408 | return; |
| 5409 | } |
| 5410 | |
| 5411 | long restoreSet = getAvailableRestoreToken(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5412 | if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5413 | + " token=" + Integer.toHexString(token)); |
| 5414 | |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 5415 | if (mAutoRestore && mProvisioned && restoreSet != 0) { |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5416 | // okay, we're going to attempt a restore of this package from this restore set. |
| 5417 | // The eventual message back into the Package Manager to run the post-install |
| 5418 | // steps for 'token' will be issued from the restore handling code. |
| 5419 | |
| 5420 | // We can use a synthetic PackageInfo here because: |
| 5421 | // 1. We know it's valid, since the Package Manager supplied the name |
| 5422 | // 2. Only the packageName field will be used by the restore code |
| 5423 | PackageInfo pkg = new PackageInfo(); |
| 5424 | pkg.packageName = packageName; |
| 5425 | |
| 5426 | mWakelock.acquire(); |
| 5427 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 5428 | msg.obj = new RestoreParams(getTransport(mCurrentTransport), null, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5429 | restoreSet, pkg, token, true); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5430 | mBackupHandler.sendMessage(msg); |
| 5431 | } else { |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 5432 | // Auto-restore disabled or no way to attempt a restore; just tell the Package |
| 5433 | // Manager to proceed with the post-install handling for this package. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5434 | if (DEBUG) Slog.v(TAG, "No restore set -- skipping restore"); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5435 | try { |
| 5436 | mPackageManagerBinder.finishPackageInstall(token); |
| 5437 | } catch (RemoteException e) { /* can't happen */ } |
| 5438 | } |
| 5439 | } |
| 5440 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 5441 | // Hand off a restore session |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5442 | public IRestoreSession beginRestoreSession(String packageName, String transport) { |
| 5443 | if (DEBUG) Slog.v(TAG, "beginRestoreSession: pkg=" + packageName |
| 5444 | + " transport=" + transport); |
| 5445 | |
| 5446 | boolean needPermission = true; |
| 5447 | if (transport == null) { |
| 5448 | transport = mCurrentTransport; |
| 5449 | |
| 5450 | if (packageName != null) { |
| 5451 | PackageInfo app = null; |
| 5452 | try { |
| 5453 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 5454 | } catch (NameNotFoundException nnf) { |
| 5455 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| 5456 | throw new IllegalArgumentException("Package " + packageName + " not found"); |
| 5457 | } |
| 5458 | |
| 5459 | if (app.applicationInfo.uid == Binder.getCallingUid()) { |
| 5460 | // So: using the current active transport, and the caller has asked |
| 5461 | // that its own package will be restored. In this narrow use case |
| 5462 | // we do not require the caller to hold the permission. |
| 5463 | needPermission = false; |
| 5464 | } |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | if (needPermission) { |
| 5469 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5470 | "beginRestoreSession"); |
| 5471 | } else { |
| 5472 | if (DEBUG) Slog.d(TAG, "restoring self on current transport; no permission needed"); |
| 5473 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5474 | |
| 5475 | synchronized(this) { |
| 5476 | if (mActiveRestoreSession != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5477 | Slog.d(TAG, "Restore session requested but one already active"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5478 | return null; |
| 5479 | } |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5480 | mActiveRestoreSession = new ActiveRestoreSession(packageName, transport); |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5481 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5482 | } |
| 5483 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 5484 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5485 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5486 | void clearRestoreSession(ActiveRestoreSession currentSession) { |
| 5487 | synchronized(this) { |
| 5488 | if (currentSession != mActiveRestoreSession) { |
| 5489 | Slog.e(TAG, "ending non-current restore session"); |
| 5490 | } else { |
| 5491 | if (DEBUG) Slog.v(TAG, "Clearing restore session and halting timeout"); |
| 5492 | mActiveRestoreSession = null; |
| 5493 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5498 | // Note that a currently-active backup agent has notified us that it has |
| 5499 | // completed the given outstanding asynchronous backup/restore operation. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5500 | @Override |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5501 | public void opComplete(int token) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5502 | if (MORE_DEBUG) Slog.v(TAG, "opComplete: " + Integer.toHexString(token)); |
| 5503 | Operation op = null; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5504 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5505 | op = mCurrentOperations.get(token); |
| 5506 | if (op != null) { |
| 5507 | op.state = OP_ACKNOWLEDGED; |
| 5508 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5509 | mCurrentOpLock.notifyAll(); |
| 5510 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5511 | |
| 5512 | // The completion callback, if any, is invoked on the handler |
| 5513 | if (op != null && op.callback != null) { |
| 5514 | Message msg = mBackupHandler.obtainMessage(MSG_OP_COMPLETE, op.callback); |
| 5515 | mBackupHandler.sendMessage(msg); |
| 5516 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5517 | } |
| 5518 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5519 | // ----- Restore session ----- |
| 5520 | |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 5521 | class ActiveRestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5522 | private static final String TAG = "RestoreSession"; |
| 5523 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5524 | private String mPackageName; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5525 | private IBackupTransport mRestoreTransport = null; |
| 5526 | RestoreSet[] mRestoreSets = null; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5527 | boolean mEnded = false; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5528 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5529 | ActiveRestoreSession(String packageName, String transport) { |
| 5530 | mPackageName = packageName; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5531 | mRestoreTransport = getTransport(transport); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | // --- Binder interface --- |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5535 | public synchronized int getAvailableRestoreSets(IRestoreObserver observer) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5536 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5537 | "getAvailableRestoreSets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5538 | if (observer == null) { |
| 5539 | throw new IllegalArgumentException("Observer must not be null"); |
| 5540 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5541 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5542 | if (mEnded) { |
| 5543 | throw new IllegalStateException("Restore session already ended"); |
| 5544 | } |
| 5545 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5546 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5547 | try { |
| Christopher Tate | 4338304 | 2009-07-13 15:17:13 -0700 | [diff] [blame] | 5548 | if (mRestoreTransport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5549 | Slog.w(TAG, "Null transport getting restore sets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5550 | return -1; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5551 | } |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5552 | // spin off the transport request to our service thread |
| 5553 | mWakelock.acquire(); |
| 5554 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS, |
| 5555 | new RestoreGetSetsParams(mRestoreTransport, this, observer)); |
| 5556 | mBackupHandler.sendMessage(msg); |
| 5557 | return 0; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5558 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5559 | Slog.e(TAG, "Error in getAvailableRestoreSets", e); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5560 | return -1; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5561 | } finally { |
| 5562 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5563 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5566 | public synchronized int restoreAll(long token, IRestoreObserver observer) { |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5567 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5568 | "performRestore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5569 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5570 | if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token) |
| Christopher Tate | f2c321a | 2009-08-10 15:43:36 -0700 | [diff] [blame] | 5571 | + " observer=" + observer); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 5572 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5573 | if (mEnded) { |
| 5574 | throw new IllegalStateException("Restore session already ended"); |
| 5575 | } |
| 5576 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5577 | if (mRestoreTransport == null || mRestoreSets == null) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5578 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 5579 | return -1; |
| 5580 | } |
| 5581 | |
| 5582 | if (mPackageName != null) { |
| 5583 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5584 | return -1; |
| 5585 | } |
| 5586 | |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5587 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5588 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 5589 | if (token == mRestoreSets[i].token) { |
| 5590 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5591 | mWakelock.acquire(); |
| 5592 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5593 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, true); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5594 | mBackupHandler.sendMessage(msg); |
| 5595 | Binder.restoreCallingIdentity(oldId); |
| 5596 | return 0; |
| 5597 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5598 | } |
| 5599 | } |
| Christopher Tate | 0e0b4ae | 2009-08-10 16:13:47 -0700 | [diff] [blame] | 5600 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5601 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5602 | return -1; |
| 5603 | } |
| 5604 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 5605 | public synchronized int restoreSome(long token, IRestoreObserver observer, |
| 5606 | String[] packages) { |
| 5607 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5608 | "performRestore"); |
| 5609 | |
| 5610 | if (DEBUG) { |
| 5611 | StringBuilder b = new StringBuilder(128); |
| 5612 | b.append("restoreSome token="); |
| 5613 | b.append(Long.toHexString(token)); |
| 5614 | b.append(" observer="); |
| 5615 | b.append(observer.toString()); |
| 5616 | b.append(" packages="); |
| 5617 | if (packages == null) { |
| 5618 | b.append("null"); |
| 5619 | } else { |
| 5620 | b.append('{'); |
| 5621 | boolean first = true; |
| 5622 | for (String s : packages) { |
| 5623 | if (!first) { |
| 5624 | b.append(", "); |
| 5625 | } else first = false; |
| 5626 | b.append(s); |
| 5627 | } |
| 5628 | b.append('}'); |
| 5629 | } |
| 5630 | Slog.d(TAG, b.toString()); |
| 5631 | } |
| 5632 | |
| 5633 | if (mEnded) { |
| 5634 | throw new IllegalStateException("Restore session already ended"); |
| 5635 | } |
| 5636 | |
| 5637 | if (mRestoreTransport == null || mRestoreSets == null) { |
| 5638 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 5639 | return -1; |
| 5640 | } |
| 5641 | |
| 5642 | if (mPackageName != null) { |
| 5643 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| 5644 | return -1; |
| 5645 | } |
| 5646 | |
| 5647 | synchronized (mQueueLock) { |
| 5648 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 5649 | if (token == mRestoreSets[i].token) { |
| 5650 | long oldId = Binder.clearCallingIdentity(); |
| 5651 | mWakelock.acquire(); |
| 5652 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 5653 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, |
| 5654 | packages, true); |
| 5655 | mBackupHandler.sendMessage(msg); |
| 5656 | Binder.restoreCallingIdentity(oldId); |
| 5657 | return 0; |
| 5658 | } |
| 5659 | } |
| 5660 | } |
| 5661 | |
| 5662 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| 5663 | return -1; |
| 5664 | } |
| 5665 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5666 | public synchronized int restorePackage(String packageName, IRestoreObserver observer) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5667 | if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5668 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5669 | if (mEnded) { |
| 5670 | throw new IllegalStateException("Restore session already ended"); |
| 5671 | } |
| 5672 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5673 | if (mPackageName != null) { |
| 5674 | if (! mPackageName.equals(packageName)) { |
| 5675 | Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName |
| 5676 | + " on session for package " + mPackageName); |
| 5677 | return -1; |
| 5678 | } |
| 5679 | } |
| 5680 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5681 | PackageInfo app = null; |
| 5682 | try { |
| 5683 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 5684 | } catch (NameNotFoundException nnf) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5685 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5686 | return -1; |
| 5687 | } |
| 5688 | |
| 5689 | // If the caller is not privileged and is not coming from the target |
| 5690 | // app's uid, throw a permission exception back to the caller. |
| 5691 | int perm = mContext.checkPermission(android.Manifest.permission.BACKUP, |
| 5692 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 5693 | if ((perm == PackageManager.PERMISSION_DENIED) && |
| 5694 | (app.applicationInfo.uid != Binder.getCallingUid())) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5695 | Slog.w(TAG, "restorePackage: bad packageName=" + packageName |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5696 | + " or calling uid=" + Binder.getCallingUid()); |
| 5697 | throw new SecurityException("No permission to restore other packages"); |
| 5698 | } |
| 5699 | |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5700 | // If the package has no backup agent, we obviously cannot proceed |
| 5701 | if (app.applicationInfo.backupAgentName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5702 | Slog.w(TAG, "Asked to restore package " + packageName + " with no agent"); |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5703 | return -1; |
| 5704 | } |
| 5705 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5706 | // So far so good; we're allowed to try to restore this package. Now |
| 5707 | // check whether there is data for it in the current dataset, falling back |
| 5708 | // to the ancestral dataset if not. |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5709 | long token = getAvailableRestoreToken(packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5710 | |
| 5711 | // If we didn't come up with a place to look -- no ancestral dataset and |
| 5712 | // the app has never been backed up from this device -- there's nothing |
| 5713 | // to do but return failure. |
| 5714 | if (token == 0) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5715 | 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] | 5716 | return -1; |
| 5717 | } |
| 5718 | |
| 5719 | // Ready to go: enqueue the restore request and claim success |
| 5720 | long oldId = Binder.clearCallingIdentity(); |
| 5721 | mWakelock.acquire(); |
| 5722 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5723 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, app, 0, false); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5724 | mBackupHandler.sendMessage(msg); |
| 5725 | Binder.restoreCallingIdentity(oldId); |
| 5726 | return 0; |
| 5727 | } |
| 5728 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5729 | // Posted to the handler to tear down a restore session in a cleanly synchronized way |
| 5730 | class EndRestoreRunnable implements Runnable { |
| 5731 | BackupManagerService mBackupManager; |
| 5732 | ActiveRestoreSession mSession; |
| 5733 | |
| 5734 | EndRestoreRunnable(BackupManagerService manager, ActiveRestoreSession session) { |
| 5735 | mBackupManager = manager; |
| 5736 | mSession = session; |
| 5737 | } |
| 5738 | |
| 5739 | public void run() { |
| 5740 | // clean up the session's bookkeeping |
| 5741 | synchronized (mSession) { |
| 5742 | try { |
| 5743 | if (mSession.mRestoreTransport != null) { |
| 5744 | mSession.mRestoreTransport.finishRestore(); |
| 5745 | } |
| 5746 | } catch (Exception e) { |
| 5747 | Slog.e(TAG, "Error in finishRestore", e); |
| 5748 | } finally { |
| 5749 | mSession.mRestoreTransport = null; |
| 5750 | mSession.mEnded = true; |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | // clean up the BackupManagerService side of the bookkeeping |
| 5755 | // and cancel any pending timeout message |
| 5756 | mBackupManager.clearRestoreSession(mSession); |
| 5757 | } |
| 5758 | } |
| 5759 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5760 | public synchronized void endRestoreSession() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5761 | if (DEBUG) Slog.d(TAG, "endRestoreSession"); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 5762 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5763 | if (mEnded) { |
| 5764 | throw new IllegalStateException("Restore session already ended"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5765 | } |
| 5766 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5767 | mBackupHandler.post(new EndRestoreRunnable(BackupManagerService.this, this)); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5771 | @Override |
| 5772 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5773 | long identityToken = Binder.clearCallingIdentity(); |
| 5774 | try { |
| 5775 | dumpInternal(pw); |
| 5776 | } finally { |
| 5777 | Binder.restoreCallingIdentity(identityToken); |
| 5778 | } |
| 5779 | } |
| 5780 | |
| 5781 | private void dumpInternal(PrintWriter pw) { |
| Christopher Tate | b8491bb | 2011-09-29 15:13:11 -0700 | [diff] [blame] | 5782 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 5783 | != PackageManager.PERMISSION_GRANTED) { |
| 5784 | pw.println("Permission Denial: can't dump Backup Manager service from from pid=" |
| 5785 | + Binder.getCallingPid() |
| 5786 | + ", uid=" + Binder.getCallingUid() |
| 5787 | + " without permission " |
| 5788 | + android.Manifest.permission.DUMP); |
| 5789 | return; |
| 5790 | } |
| 5791 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5792 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5793 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled") |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5794 | + " / " + (!mProvisioned ? "not " : "") + "provisioned / " |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 5795 | + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init"); |
| Christopher Tate | ae06ed9 | 2010-02-25 17:13:28 -0800 | [diff] [blame] | 5796 | pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled")); |
| Christopher Tate | 336a649 | 2011-10-05 16:05:43 -0700 | [diff] [blame] | 5797 | if (mBackupRunning) pw.println("Backup currently running"); |
| 5798 | pw.println("Last backup pass started: " + mLastBackupPass |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5799 | + " (now = " + System.currentTimeMillis() + ')'); |
| 5800 | pw.println(" next scheduled: " + mNextBackupPass); |
| 5801 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5802 | pw.println("Available transports:"); |
| 5803 | for (String t : listAllTransports()) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5804 | pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t); |
| 5805 | try { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5806 | IBackupTransport transport = getTransport(t); |
| 5807 | File dir = new File(mBaseStateDir, transport.transportDirName()); |
| 5808 | pw.println(" destination: " + transport.currentDestinationString()); |
| 5809 | pw.println(" intent: " + transport.configurationIntent()); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5810 | for (File f : dir.listFiles()) { |
| 5811 | pw.println(" " + f.getName() + " - " + f.length() + " state bytes"); |
| 5812 | } |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5813 | } catch (Exception e) { |
| 5814 | Slog.e(TAG, "Error in transport", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5815 | pw.println(" Error: " + e); |
| 5816 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5817 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5818 | |
| 5819 | pw.println("Pending init: " + mPendingInits.size()); |
| 5820 | for (String s : mPendingInits) { |
| 5821 | pw.println(" " + s); |
| 5822 | } |
| 5823 | |
| Christopher Tate | 6de74ff | 2012-01-17 15:20:32 -0800 | [diff] [blame] | 5824 | if (DEBUG_BACKUP_TRACE) { |
| 5825 | synchronized (mBackupTrace) { |
| 5826 | if (!mBackupTrace.isEmpty()) { |
| 5827 | pw.println("Most recent backup trace:"); |
| 5828 | for (String s : mBackupTrace) { |
| 5829 | pw.println(" " + s); |
| 5830 | } |
| 5831 | } |
| 5832 | } |
| 5833 | } |
| 5834 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5835 | int N = mBackupParticipants.size(); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5836 | pw.println("Participants:"); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5837 | for (int i=0; i<N; i++) { |
| 5838 | int uid = mBackupParticipants.keyAt(i); |
| 5839 | pw.print(" uid: "); |
| 5840 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5841 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 5842 | for (ApplicationInfo app: participants) { |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5843 | pw.println(" " + app.packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5844 | } |
| 5845 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5846 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5847 | pw.println("Ancestral packages: " |
| 5848 | + (mAncestralPackages == null ? "none" : mAncestralPackages.size())); |
| Christopher Tate | 5923c97 | 2010-04-04 17:45:35 -0700 | [diff] [blame] | 5849 | if (mAncestralPackages != null) { |
| 5850 | for (String pkg : mAncestralPackages) { |
| 5851 | pw.println(" " + pkg); |
| 5852 | } |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5853 | } |
| 5854 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 5855 | pw.println("Ever backed up: " + mEverStoredApps.size()); |
| 5856 | for (String pkg : mEverStoredApps) { |
| 5857 | pw.println(" " + pkg); |
| 5858 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5859 | |
| 5860 | pw.println("Pending backup: " + mPendingBackups.size()); |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 5861 | for (BackupRequest req : mPendingBackups.values()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5862 | pw.println(" " + req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5863 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5864 | } |
| 5865 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 5866 | } |