| 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; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 65 | import android.os.SystemClock; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 66 | import android.os.WorkSource; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 67 | import android.provider.Settings; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 68 | import android.util.EventLog; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 69 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 70 | import android.util.Slog; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 71 | import android.util.SparseArray; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 72 | import android.util.SparseIntArray; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 73 | import android.util.StringBuilderPrinter; |
| 74 | |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 75 | import com.android.internal.backup.BackupConstants; |
| 76 | import com.android.internal.backup.IBackupTransport; |
| 77 | import com.android.internal.backup.LocalTransport; |
| 78 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| 79 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 80 | import java.io.BufferedInputStream; |
| 81 | import java.io.BufferedOutputStream; |
| 82 | import java.io.ByteArrayOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 83 | import java.io.DataInputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 84 | import java.io.DataOutputStream; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 85 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 86 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 87 | import java.io.FileDescriptor; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 88 | import java.io.FileInputStream; |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 89 | import java.io.FileNotFoundException; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 90 | import java.io.FileOutputStream; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 91 | import java.io.IOException; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 92 | import java.io.InputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 93 | import java.io.OutputStream; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 94 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 95 | import java.io.RandomAccessFile; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 96 | import java.security.InvalidAlgorithmParameterException; |
| 97 | import java.security.InvalidKeyException; |
| 98 | import java.security.Key; |
| 99 | import java.security.NoSuchAlgorithmException; |
| 100 | import java.security.SecureRandom; |
| 101 | import java.security.spec.InvalidKeySpecException; |
| 102 | import java.security.spec.KeySpec; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 103 | import java.text.SimpleDateFormat; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 104 | import java.util.ArrayList; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 105 | import java.util.Arrays; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 106 | import java.util.Date; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 107 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 108 | import java.util.HashSet; |
| 109 | import java.util.List; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 110 | import java.util.Map; |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 111 | import java.util.Random; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 112 | import java.util.Set; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 113 | import java.util.concurrent.atomic.AtomicBoolean; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 114 | import java.util.zip.Deflater; |
| 115 | import java.util.zip.DeflaterOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 116 | import java.util.zip.InflaterInputStream; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 117 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 118 | import javax.crypto.BadPaddingException; |
| 119 | import javax.crypto.Cipher; |
| 120 | import javax.crypto.CipherInputStream; |
| 121 | import javax.crypto.CipherOutputStream; |
| 122 | import javax.crypto.IllegalBlockSizeException; |
| 123 | import javax.crypto.NoSuchPaddingException; |
| 124 | import javax.crypto.SecretKey; |
| 125 | import javax.crypto.SecretKeyFactory; |
| 126 | import javax.crypto.spec.IvParameterSpec; |
| 127 | import javax.crypto.spec.PBEKeySpec; |
| 128 | import javax.crypto.spec.SecretKeySpec; |
| 129 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 130 | class BackupManagerService extends IBackupManager.Stub { |
| 131 | private static final String TAG = "BackupManagerService"; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 132 | private static final boolean DEBUG = true; |
| Christopher Tate | b1543a9 | 2011-09-07 12:11:09 -0700 | [diff] [blame] | 133 | private static final boolean MORE_DEBUG = false; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 134 | |
| 135 | // Name and current contents version of the full-backup manifest file |
| 136 | static final String BACKUP_MANIFEST_FILENAME = "_manifest"; |
| 137 | static final int BACKUP_MANIFEST_VERSION = 1; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 138 | static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n"; |
| 139 | static final int BACKUP_FILE_VERSION = 1; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 140 | static final boolean COMPRESS_FULL_BACKUPS = true; // should be true in production |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 141 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 142 | // How often we perform a backup pass. Privileged external callers can |
| 143 | // trigger an immediate pass. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 144 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 145 | |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 146 | // Random variation in backup scheduling time to avoid server load spikes |
| 147 | private static final int FUZZ_MILLIS = 5 * 60 * 1000; |
| 148 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 149 | // The amount of time between the initial provisioning of the device and |
| 150 | // the first backup pass. |
| 151 | private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR; |
| 152 | |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 153 | private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN"; |
| 154 | private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT"; |
| 155 | private static final String RUN_CLEAR_ACTION = "android.app.backup.intent.CLEAR"; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 156 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 157 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 158 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 159 | private static final int MSG_RUN_CLEAR = 4; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 160 | private static final int MSG_RUN_INITIALIZE = 5; |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 161 | private static final int MSG_RUN_GET_RESTORE_SETS = 6; |
| 162 | private static final int MSG_TIMEOUT = 7; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 163 | private static final int MSG_RESTORE_TIMEOUT = 8; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 164 | private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9; |
| 165 | private static final int MSG_RUN_FULL_RESTORE = 10; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 166 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 167 | // backup task state machine tick |
| 168 | static final int MSG_BACKUP_RESTORE_STEP = 20; |
| 169 | static final int MSG_OP_COMPLETE = 21; |
| 170 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 171 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 172 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 173 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 174 | // Timeout intervals for agent backup & restore operations |
| 175 | static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 176 | static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 177 | static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 178 | static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000; |
| 179 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 180 | // User confirmation timeout for a full backup/restore operation. It's this long in |
| 181 | // order to give them time to enter the backup password. |
| 182 | static final long TIMEOUT_FULL_CONFIRMATION = 60 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 183 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 184 | private Context mContext; |
| 185 | private PackageManager mPackageManager; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 186 | IPackageManager mPackageManagerBinder; |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 187 | private IActivityManager mActivityManager; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 188 | private PowerManager mPowerManager; |
| 189 | private AlarmManager mAlarmManager; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 190 | IBackupManager mBackupManagerBinder; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 191 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 192 | boolean mEnabled; // access to this is synchronized on 'this' |
| 193 | boolean mProvisioned; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 194 | boolean mAutoRestore; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 195 | PowerManager.WakeLock mWakelock; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 196 | HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 197 | BackupHandler mBackupHandler; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 198 | PendingIntent mRunBackupIntent, mRunInitIntent; |
| 199 | BroadcastReceiver mRunBackupReceiver, mRunInitReceiver; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 200 | // 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] | 201 | final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 202 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 203 | // set of backup services that have pending changes |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 204 | class BackupRequest { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 205 | public String packageName; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 206 | |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 207 | BackupRequest(String pkgName) { |
| 208 | packageName = pkgName; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 209 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 210 | |
| 211 | public String toString() { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 212 | return "BackupRequest{pkg=" + packageName + "}"; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 213 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 214 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 215 | // Backups that we haven't started yet. Keys are package names. |
| 216 | HashMap<String,BackupRequest> mPendingBackups |
| 217 | = new HashMap<String,BackupRequest>(); |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 218 | |
| 219 | // Pseudoname that we use for the Package Manager metadata "package" |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 220 | static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 221 | |
| 222 | // locking around the pending-backup management |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 223 | final Object mQueueLock = new Object(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 224 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 225 | // The thread performing the sequence of queued backups binds to each app's agent |
| 226 | // in succession. Bind notifications are asynchronously delivered through the |
| 227 | // Activity Manager; use this lock object to signal when a requested binding has |
| 228 | // completed. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 229 | final Object mAgentConnectLock = new Object(); |
| 230 | IBackupAgent mConnectedAgent; |
| 231 | volatile boolean mConnecting; |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 232 | volatile long mLastBackupPass; |
| 233 | volatile long mNextBackupPass; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 234 | |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 235 | // A similar synchronization mechanism around clearing apps' data for restore |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 236 | final Object mClearDataLock = new Object(); |
| 237 | volatile boolean mClearingData; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 238 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 239 | // Transport bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 240 | final HashMap<String,IBackupTransport> mTransports |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 241 | = new HashMap<String,IBackupTransport>(); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 242 | String mCurrentTransport; |
| 243 | IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 244 | ActiveRestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 245 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 246 | class RestoreGetSetsParams { |
| 247 | public IBackupTransport transport; |
| 248 | public ActiveRestoreSession session; |
| 249 | public IRestoreObserver observer; |
| 250 | |
| 251 | RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session, |
| 252 | IRestoreObserver _observer) { |
| 253 | transport = _transport; |
| 254 | session = _session; |
| 255 | observer = _observer; |
| 256 | } |
| 257 | } |
| 258 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 259 | class RestoreParams { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 260 | public IBackupTransport transport; |
| 261 | public IRestoreObserver observer; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 262 | public long token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 263 | public PackageInfo pkgInfo; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 264 | 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] | 265 | public boolean needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 266 | public String[] filterSet; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 267 | |
| 268 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 269 | long _token, PackageInfo _pkg, int _pmToken, boolean _needFullBackup) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 270 | transport = _transport; |
| 271 | observer = _obs; |
| 272 | token = _token; |
| 273 | pkgInfo = _pkg; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 274 | pmToken = _pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 275 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 276 | filterSet = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 277 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 278 | |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 279 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 280 | boolean _needFullBackup) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 281 | transport = _transport; |
| 282 | observer = _obs; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 283 | token = _token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 284 | pkgInfo = null; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 285 | pmToken = 0; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 286 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 287 | filterSet = null; |
| 288 | } |
| 289 | |
| 290 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 291 | String[] _filterSet, boolean _needFullBackup) { |
| 292 | transport = _transport; |
| 293 | observer = _obs; |
| 294 | token = _token; |
| 295 | pkgInfo = null; |
| 296 | pmToken = 0; |
| 297 | needFullBackup = _needFullBackup; |
| 298 | filterSet = _filterSet; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 302 | class ClearParams { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 303 | public IBackupTransport transport; |
| 304 | public PackageInfo packageInfo; |
| 305 | |
| 306 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 307 | transport = _transport; |
| 308 | packageInfo = _info; |
| 309 | } |
| 310 | } |
| 311 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 312 | class FullParams { |
| 313 | public ParcelFileDescriptor fd; |
| 314 | public final AtomicBoolean latch; |
| 315 | public IFullBackupRestoreObserver observer; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 316 | public String curPassword; // filled in by the confirmation step |
| 317 | public String encryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 318 | |
| 319 | FullParams() { |
| 320 | latch = new AtomicBoolean(false); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | class FullBackupParams extends FullParams { |
| 325 | public boolean includeApks; |
| 326 | public boolean includeShared; |
| 327 | public boolean allApps; |
| 328 | public String[] packages; |
| 329 | |
| 330 | FullBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveShared, |
| 331 | boolean doAllApps, String[] pkgList) { |
| 332 | fd = output; |
| 333 | includeApks = saveApks; |
| 334 | includeShared = saveShared; |
| 335 | allApps = doAllApps; |
| 336 | packages = pkgList; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | class FullRestoreParams extends FullParams { |
| 341 | FullRestoreParams(ParcelFileDescriptor input) { |
| 342 | fd = input; |
| 343 | } |
| 344 | } |
| 345 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 346 | // Bookkeeping of in-flight operations for timeout etc. purposes. The operation |
| 347 | // token is the index of the entry in the pending-operations list. |
| 348 | static final int OP_PENDING = 0; |
| 349 | static final int OP_ACKNOWLEDGED = 1; |
| 350 | static final int OP_TIMEOUT = -1; |
| 351 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 352 | class Operation { |
| 353 | public int state; |
| 354 | public BackupRestoreTask callback; |
| 355 | |
| 356 | Operation(int initialState, BackupRestoreTask callbackObj) { |
| 357 | state = initialState; |
| 358 | callback = callbackObj; |
| 359 | } |
| 360 | } |
| 361 | final SparseArray<Operation> mCurrentOperations = new SparseArray<Operation>(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 362 | final Object mCurrentOpLock = new Object(); |
| 363 | final Random mTokenGenerator = new Random(); |
| 364 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 365 | final SparseArray<FullParams> mFullConfirmations = new SparseArray<FullParams>(); |
| 366 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 367 | // Where we keep our journal files and other bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 368 | File mBaseStateDir; |
| 369 | File mDataDir; |
| 370 | File mJournalDir; |
| 371 | File mJournal; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 372 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 373 | // Backup password, if any, and the file where it's saved. What is stored is not the |
| 374 | // password text itself; it's the result of a PBKDF2 hash with a randomly chosen (but |
| 375 | // persisted) salt. Validation is performed by running the challenge text through the |
| 376 | // same PBKDF2 cycle with the persisted salt; if the resulting derived key string matches |
| 377 | // the saved hash string, then the challenge text matches the originally supplied |
| 378 | // password text. |
| 379 | private final SecureRandom mRng = new SecureRandom(); |
| 380 | private String mPasswordHash; |
| 381 | private File mPasswordHashFile; |
| 382 | private byte[] mPasswordSalt; |
| 383 | |
| 384 | // Configuration of PBKDF2 that we use for generating pw hashes and intermediate keys |
| 385 | static final int PBKDF2_HASH_ROUNDS = 10000; |
| 386 | static final int PBKDF2_KEY_SIZE = 256; // bits |
| 387 | static final int PBKDF2_SALT_SIZE = 512; // bits |
| 388 | static final String ENCRYPTION_ALGORITHM_NAME = "AES-256"; |
| 389 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 390 | // Keep a log of all the apps we've ever backed up, and what the |
| 391 | // dataset tokens are for both the current backup dataset and |
| 392 | // the ancestral dataset. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 393 | private File mEverStored; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 394 | HashSet<String> mEverStoredApps = new HashSet<String>(); |
| 395 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 396 | 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] | 397 | File mTokenFile; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 398 | Set<String> mAncestralPackages = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 399 | long mAncestralToken = 0; |
| 400 | long mCurrentToken = 0; |
| 401 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 402 | // Persistently track the need to do a full init |
| 403 | static final String INIT_SENTINEL_FILE_NAME = "_need_init_"; |
| 404 | HashSet<String> mPendingInits = new HashSet<String>(); // transport names |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 405 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 406 | // Utility: build a new random integer token |
| 407 | int generateToken() { |
| 408 | int token; |
| 409 | do { |
| 410 | synchronized (mTokenGenerator) { |
| 411 | token = mTokenGenerator.nextInt(); |
| 412 | } |
| 413 | } while (token < 0); |
| 414 | return token; |
| 415 | } |
| 416 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 417 | // ----- Asynchronous backup/restore handler thread ----- |
| 418 | |
| 419 | private class BackupHandler extends Handler { |
| 420 | public BackupHandler(Looper looper) { |
| 421 | super(looper); |
| 422 | } |
| 423 | |
| 424 | public void handleMessage(Message msg) { |
| 425 | |
| 426 | switch (msg.what) { |
| 427 | case MSG_RUN_BACKUP: |
| 428 | { |
| 429 | mLastBackupPass = System.currentTimeMillis(); |
| 430 | mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL; |
| 431 | |
| 432 | IBackupTransport transport = getTransport(mCurrentTransport); |
| 433 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 434 | Slog.v(TAG, "Backup requested but no transport available"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 435 | mWakelock.release(); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | // snapshot the pending-backup set and work on that |
| 440 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 441 | File oldJournal = mJournal; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 442 | synchronized (mQueueLock) { |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 443 | // Do we have any work to do? Construct the work queue |
| 444 | // then release the synchronization lock to actually run |
| 445 | // the backup. |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 446 | if (mPendingBackups.size() > 0) { |
| 447 | for (BackupRequest b: mPendingBackups.values()) { |
| 448 | queue.add(b); |
| 449 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 450 | if (DEBUG) Slog.v(TAG, "clearing pending backups"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 451 | mPendingBackups.clear(); |
| 452 | |
| 453 | // Start a new backup-queue journal file too |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 454 | mJournal = null; |
| 455 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 458 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 459 | // At this point, we have started a new journal file, and the old |
| 460 | // file identity is being passed to the backup processing task. |
| 461 | // When it completes successfully, that old journal file will be |
| 462 | // deleted. If we crash prior to that, the old journal is parsed |
| 463 | // at next boot and the journaled requests fulfilled. |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 464 | if (queue.size() > 0) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 465 | // Spin up a backup state sequence and set it running |
| 466 | PerformBackupTask pbt = new PerformBackupTask(transport, queue, oldJournal); |
| 467 | Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt); |
| 468 | sendMessage(pbtMessage); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 469 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 470 | Slog.v(TAG, "Backup requested but nothing pending"); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 471 | mWakelock.release(); |
| 472 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 473 | break; |
| 474 | } |
| 475 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 476 | case MSG_BACKUP_RESTORE_STEP: |
| 477 | { |
| 478 | try { |
| 479 | BackupRestoreTask task = (BackupRestoreTask) msg.obj; |
| 480 | if (MORE_DEBUG) Slog.v(TAG, "Got next step for " + task + ", executing"); |
| 481 | task.execute(); |
| 482 | } catch (ClassCastException e) { |
| 483 | Slog.e(TAG, "Invalid backup task in flight, obj=" + msg.obj); |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | case MSG_OP_COMPLETE: |
| 489 | { |
| 490 | try { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 491 | BackupRestoreTask task = (BackupRestoreTask) msg.obj; |
| 492 | task.operationComplete(); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 493 | } catch (ClassCastException e) { |
| 494 | Slog.e(TAG, "Invalid completion in flight, obj=" + msg.obj); |
| 495 | } |
| 496 | break; |
| 497 | } |
| 498 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 499 | case MSG_RUN_FULL_BACKUP: |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 500 | { |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 501 | // TODO: refactor full backup to be a looper-based state machine |
| 502 | // similar to normal backup/restore. |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 503 | FullBackupParams params = (FullBackupParams)msg.obj; |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 504 | PerformFullBackupTask task = new PerformFullBackupTask(params.fd, |
| 505 | params.observer, params.includeApks, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 506 | params.includeShared, params.curPassword, params.encryptPassword, |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 507 | params.allApps, params.packages, params.latch); |
| 508 | (new Thread(task)).start(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 509 | break; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 510 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 511 | |
| 512 | case MSG_RUN_RESTORE: |
| 513 | { |
| 514 | RestoreParams params = (RestoreParams)msg.obj; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 515 | Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 516 | PerformRestoreTask task = new PerformRestoreTask( |
| 517 | params.transport, params.observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 518 | params.token, params.pkgInfo, params.pmToken, |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 519 | params.needFullBackup, params.filterSet); |
| 520 | Message restoreMsg = obtainMessage(MSG_BACKUP_RESTORE_STEP, task); |
| 521 | sendMessage(restoreMsg); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 525 | case MSG_RUN_FULL_RESTORE: |
| 526 | { |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 527 | // TODO: refactor full restore to be a looper-based state machine |
| 528 | // similar to normal backup/restore. |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 529 | FullRestoreParams params = (FullRestoreParams)msg.obj; |
| Christopher Tate | a28e854 | 2011-09-12 13:45:21 -0700 | [diff] [blame] | 530 | PerformFullRestoreTask task = new PerformFullRestoreTask(params.fd, |
| 531 | params.curPassword, params.encryptPassword, |
| 532 | params.observer, params.latch); |
| 533 | (new Thread(task)).start(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 537 | case MSG_RUN_CLEAR: |
| 538 | { |
| 539 | ClearParams params = (ClearParams)msg.obj; |
| 540 | (new PerformClearTask(params.transport, params.packageInfo)).run(); |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | case MSG_RUN_INITIALIZE: |
| 545 | { |
| 546 | HashSet<String> queue; |
| 547 | |
| 548 | // Snapshot the pending-init queue and work on that |
| 549 | synchronized (mQueueLock) { |
| 550 | queue = new HashSet<String>(mPendingInits); |
| 551 | mPendingInits.clear(); |
| 552 | } |
| 553 | |
| 554 | (new PerformInitializeTask(queue)).run(); |
| 555 | break; |
| 556 | } |
| 557 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 558 | case MSG_RUN_GET_RESTORE_SETS: |
| 559 | { |
| 560 | // Like other async operations, this is entered with the wakelock held |
| 561 | RestoreSet[] sets = null; |
| 562 | RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj; |
| 563 | try { |
| 564 | sets = params.transport.getAvailableRestoreSets(); |
| 565 | // cache the result in the active session |
| 566 | synchronized (params.session) { |
| 567 | params.session.mRestoreSets = sets; |
| 568 | } |
| 569 | if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 570 | } catch (Exception e) { |
| 571 | Slog.e(TAG, "Error from transport getting set list"); |
| 572 | } finally { |
| 573 | if (params.observer != null) { |
| 574 | try { |
| 575 | params.observer.restoreSetsAvailable(sets); |
| 576 | } catch (RemoteException re) { |
| 577 | Slog.e(TAG, "Unable to report listing to observer"); |
| 578 | } catch (Exception e) { |
| 579 | Slog.e(TAG, "Restore observer threw", e); |
| 580 | } |
| 581 | } |
| 582 | |
| Christopher Tate | 2a93509 | 2011-03-03 17:30:32 -0800 | [diff] [blame] | 583 | // Done: reset the session timeout clock |
| 584 | removeMessages(MSG_RESTORE_TIMEOUT); |
| 585 | sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| 586 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 587 | mWakelock.release(); |
| 588 | } |
| 589 | break; |
| 590 | } |
| 591 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 592 | case MSG_TIMEOUT: |
| 593 | { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 594 | handleTimeout(msg.arg1, msg.obj); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 595 | break; |
| 596 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 597 | |
| 598 | case MSG_RESTORE_TIMEOUT: |
| 599 | { |
| 600 | synchronized (BackupManagerService.this) { |
| 601 | if (mActiveRestoreSession != null) { |
| 602 | // Client app left the restore session dangling. We know that it |
| 603 | // can't be in the middle of an actual restore operation because |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 604 | // the timeout is suspended while a restore is in progress. Clean |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 605 | // up now. |
| 606 | Slog.w(TAG, "Restore session timed out; aborting"); |
| 607 | post(mActiveRestoreSession.new EndRestoreRunnable( |
| 608 | BackupManagerService.this, mActiveRestoreSession)); |
| 609 | } |
| 610 | } |
| 611 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 612 | |
| 613 | case MSG_FULL_CONFIRMATION_TIMEOUT: |
| 614 | { |
| 615 | synchronized (mFullConfirmations) { |
| 616 | FullParams params = mFullConfirmations.get(msg.arg1); |
| 617 | if (params != null) { |
| 618 | Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation"); |
| 619 | |
| 620 | // Release the waiter; timeout == completion |
| 621 | signalFullBackupRestoreCompletion(params); |
| 622 | |
| 623 | // Remove the token from the set |
| 624 | mFullConfirmations.delete(msg.arg1); |
| 625 | |
| 626 | // Report a timeout to the observer, if any |
| 627 | if (params.observer != null) { |
| 628 | try { |
| 629 | params.observer.onTimeout(); |
| 630 | } catch (RemoteException e) { |
| 631 | /* don't care if the app has gone away */ |
| 632 | } |
| 633 | } |
| 634 | } else { |
| 635 | Slog.d(TAG, "couldn't find params for token " + msg.arg1); |
| 636 | } |
| 637 | } |
| 638 | break; |
| 639 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // ----- Main service implementation ----- |
| 645 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 646 | public BackupManagerService(Context context) { |
| 647 | mContext = context; |
| 648 | mPackageManager = context.getPackageManager(); |
| Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 649 | mPackageManagerBinder = AppGlobals.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 650 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 651 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 652 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 653 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 654 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 655 | mBackupManagerBinder = asInterface(asBinder()); |
| 656 | |
| 657 | // spin up the backup/restore handler thread |
| 658 | mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 659 | mHandlerThread.start(); |
| 660 | mBackupHandler = new BackupHandler(mHandlerThread.getLooper()); |
| 661 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 662 | // Set up our bookkeeping |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 663 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 664 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 665 | mProvisioned = Settings.Secure.getInt(context.getContentResolver(), |
| Joe Onorato | ab9a2a5 | 2009-07-27 08:56:39 -0700 | [diff] [blame] | 666 | Settings.Secure.BACKUP_PROVISIONED, 0) != 0; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 667 | mAutoRestore = Settings.Secure.getInt(context.getContentResolver(), |
| Christopher Tate | 5035fda | 2010-02-25 18:01:14 -0800 | [diff] [blame] | 668 | Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 669 | // If Encrypted file systems is enabled or disabled, this call will return the |
| 670 | // correct directory. |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 671 | mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 672 | mBaseStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 673 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 674 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 675 | mPasswordHashFile = new File(mBaseStateDir, "pwhash"); |
| 676 | if (mPasswordHashFile.exists()) { |
| 677 | FileInputStream fin = null; |
| 678 | DataInputStream in = null; |
| 679 | try { |
| 680 | fin = new FileInputStream(mPasswordHashFile); |
| 681 | in = new DataInputStream(new BufferedInputStream(fin)); |
| 682 | // integer length of the salt array, followed by the salt, |
| 683 | // then the hex pw hash string |
| 684 | int saltLen = in.readInt(); |
| 685 | byte[] salt = new byte[saltLen]; |
| 686 | in.readFully(salt); |
| 687 | mPasswordHash = in.readUTF(); |
| 688 | mPasswordSalt = salt; |
| 689 | } catch (IOException e) { |
| 690 | Slog.e(TAG, "Unable to read saved backup pw hash"); |
| 691 | } finally { |
| 692 | try { |
| 693 | if (in != null) in.close(); |
| 694 | if (fin != null) fin.close(); |
| 695 | } catch (IOException e) { |
| 696 | Slog.w(TAG, "Unable to close streams"); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 701 | // Alarm receivers for scheduled backups & initialization operations |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 702 | mRunBackupReceiver = new RunBackupReceiver(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 703 | IntentFilter filter = new IntentFilter(); |
| 704 | filter.addAction(RUN_BACKUP_ACTION); |
| 705 | context.registerReceiver(mRunBackupReceiver, filter, |
| 706 | android.Manifest.permission.BACKUP, null); |
| 707 | |
| 708 | mRunInitReceiver = new RunInitializeReceiver(); |
| 709 | filter = new IntentFilter(); |
| 710 | filter.addAction(RUN_INITIALIZE_ACTION); |
| 711 | context.registerReceiver(mRunInitReceiver, filter, |
| 712 | android.Manifest.permission.BACKUP, null); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 713 | |
| 714 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 715 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 716 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 717 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 718 | Intent initIntent = new Intent(RUN_INITIALIZE_ACTION); |
| 719 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 720 | mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0); |
| 721 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 722 | // Set up the backup-request journaling |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 723 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 724 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 725 | mJournal = null; // will be created on first use |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 726 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 727 | // Set up the various sorts of package tracking we do |
| 728 | initPackageTracking(); |
| 729 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 730 | // Build our mapping of uid to backup client services. This implicitly |
| 731 | // schedules a backup pass on the Package Manager metadata the first |
| 732 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 733 | synchronized (mBackupParticipants) { |
| 734 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 737 | // Set up our transport options and initialize the default transport |
| 738 | // TODO: Have transports register themselves somehow? |
| 739 | // TODO: Don't create transports that we don't need to? |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 740 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 741 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 742 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 743 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 744 | mGoogleTransport = null; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 745 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 746 | Settings.Secure.BACKUP_TRANSPORT); |
| 747 | if ("".equals(mCurrentTransport)) { |
| 748 | mCurrentTransport = null; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 749 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 750 | if (DEBUG) Slog.v(TAG, "Starting with transport " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 751 | |
| 752 | // Attach to the Google backup transport. When this comes up, it will set |
| 753 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 754 | // to null. |
| Christopher Tate | a32504f | 2010-04-21 17:58:07 -0700 | [diff] [blame] | 755 | ComponentName transportComponent = new ComponentName("com.google.android.backup", |
| 756 | "com.google.android.backup.BackupTransportService"); |
| 757 | try { |
| 758 | // If there's something out there that is supposed to be the Google |
| 759 | // backup transport, make sure it's legitimately part of the OS build |
| 760 | // and not an app lying about its package name. |
| 761 | ApplicationInfo info = mPackageManager.getApplicationInfo( |
| 762 | transportComponent.getPackageName(), 0); |
| 763 | if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 764 | if (DEBUG) Slog.v(TAG, "Binding to Google transport"); |
| 765 | Intent intent = new Intent().setComponent(transportComponent); |
| 766 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| 767 | } else { |
| 768 | Slog.w(TAG, "Possible Google transport spoof: ignoring " + info); |
| 769 | } |
| 770 | } catch (PackageManager.NameNotFoundException nnf) { |
| 771 | // No such package? No binding. |
| 772 | if (DEBUG) Slog.v(TAG, "Google transport not present"); |
| 773 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 774 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 775 | // Now that we know about valid backup participants, parse any |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 776 | // leftover journal files into the pending backup set |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 777 | parseLeftoverJournals(); |
| 778 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 779 | // Power management |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 780 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 781 | |
| 782 | // Start the backup passes going |
| 783 | setBackupEnabled(areEnabled); |
| 784 | } |
| 785 | |
| 786 | private class RunBackupReceiver extends BroadcastReceiver { |
| 787 | public void onReceive(Context context, Intent intent) { |
| 788 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 789 | synchronized (mQueueLock) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 790 | if (mPendingInits.size() > 0) { |
| 791 | // If there are pending init operations, we process those |
| 792 | // and then settle into the usual periodic backup schedule. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 793 | if (DEBUG) Slog.v(TAG, "Init pending at scheduled backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 794 | try { |
| 795 | mAlarmManager.cancel(mRunInitIntent); |
| 796 | mRunInitIntent.send(); |
| 797 | } catch (PendingIntent.CanceledException ce) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 798 | Slog.e(TAG, "Run init intent cancelled"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 799 | // can't really do more than bail here |
| 800 | } |
| 801 | } else { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 802 | // Don't run backups now if we're disabled or not yet |
| 803 | // fully set up. |
| 804 | if (mEnabled && mProvisioned) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 805 | if (DEBUG) Slog.v(TAG, "Running a backup pass"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 806 | |
| 807 | // Acquire the wakelock and pass it to the backup thread. it will |
| 808 | // be released once backup concludes. |
| 809 | mWakelock.acquire(); |
| 810 | |
| 811 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 812 | mBackupHandler.sendMessage(msg); |
| 813 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 814 | Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | private class RunInitializeReceiver extends BroadcastReceiver { |
| 823 | public void onReceive(Context context, Intent intent) { |
| 824 | if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) { |
| 825 | synchronized (mQueueLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 826 | if (DEBUG) Slog.v(TAG, "Running a device init"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 827 | |
| 828 | // Acquire the wakelock and pass it to the init thread. it will |
| 829 | // be released once init concludes. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 830 | mWakelock.acquire(); |
| 831 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 832 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 833 | mBackupHandler.sendMessage(msg); |
| 834 | } |
| 835 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 836 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 837 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 838 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 839 | private void initPackageTracking() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 840 | if (DEBUG) Slog.v(TAG, "Initializing package tracking"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 841 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 842 | // Remember our ancestral dataset |
| 843 | mTokenFile = new File(mBaseStateDir, "ancestral"); |
| 844 | try { |
| 845 | RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 846 | int version = tf.readInt(); |
| 847 | if (version == CURRENT_ANCESTRAL_RECORD_VERSION) { |
| 848 | mAncestralToken = tf.readLong(); |
| 849 | mCurrentToken = tf.readLong(); |
| 850 | |
| 851 | int numPackages = tf.readInt(); |
| 852 | if (numPackages >= 0) { |
| 853 | mAncestralPackages = new HashSet<String>(); |
| 854 | for (int i = 0; i < numPackages; i++) { |
| 855 | String pkgName = tf.readUTF(); |
| 856 | mAncestralPackages.add(pkgName); |
| 857 | } |
| 858 | } |
| 859 | } |
| Brad Fitzpatrick | 725d8f0 | 2010-11-15 11:12:42 -0800 | [diff] [blame] | 860 | tf.close(); |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 861 | } catch (FileNotFoundException fnf) { |
| 862 | // Probably innocuous |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 863 | Slog.v(TAG, "No ancestral data"); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 864 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 865 | Slog.w(TAG, "Unable to read token file", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 868 | // Keep a log of what apps we've ever backed up. Because we might have |
| 869 | // rebooted in the middle of an operation that was removing something from |
| 870 | // this log, we sanity-check its contents here and reconstruct it. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 871 | mEverStored = new File(mBaseStateDir, "processed"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 872 | File tempProcessedFile = new File(mBaseStateDir, "processed.new"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 873 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 874 | // If we were in the middle of removing something from the ever-backed-up |
| 875 | // file, there might be a transient "processed.new" file still present. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 876 | // Ignore it -- we'll validate "processed" against the current package set. |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 877 | if (tempProcessedFile.exists()) { |
| 878 | tempProcessedFile.delete(); |
| 879 | } |
| 880 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 881 | // If there are previous contents, parse them out then start a new |
| 882 | // file to continue the recordkeeping. |
| 883 | if (mEverStored.exists()) { |
| 884 | RandomAccessFile temp = null; |
| 885 | RandomAccessFile in = null; |
| 886 | |
| 887 | try { |
| 888 | temp = new RandomAccessFile(tempProcessedFile, "rws"); |
| 889 | in = new RandomAccessFile(mEverStored, "r"); |
| 890 | |
| 891 | while (true) { |
| 892 | PackageInfo info; |
| 893 | String pkg = in.readUTF(); |
| 894 | try { |
| 895 | info = mPackageManager.getPackageInfo(pkg, 0); |
| 896 | mEverStoredApps.add(pkg); |
| 897 | temp.writeUTF(pkg); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 898 | if (MORE_DEBUG) Slog.v(TAG, " + " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 899 | } catch (NameNotFoundException e) { |
| 900 | // nope, this package was uninstalled; don't include it |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 901 | if (MORE_DEBUG) Slog.v(TAG, " - " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | } catch (EOFException e) { |
| 905 | // Once we've rewritten the backup history log, atomically replace the |
| 906 | // old one with the new one then reopen the file for continuing use. |
| 907 | if (!tempProcessedFile.renameTo(mEverStored)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 908 | Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 909 | } |
| 910 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 911 | Slog.e(TAG, "Error in processed file", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 912 | } finally { |
| 913 | try { if (temp != null) temp.close(); } catch (IOException e) {} |
| 914 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 915 | } |
| 916 | } |
| 917 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 918 | // Register for broadcasts about package install, etc., so we can |
| 919 | // update the provider list. |
| 920 | IntentFilter filter = new IntentFilter(); |
| 921 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 922 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 923 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 924 | filter.addDataScheme("package"); |
| 925 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 926 | // Register for events related to sdcard installation. |
| 927 | IntentFilter sdFilter = new IntentFilter(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 928 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 929 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 930 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 933 | private void parseLeftoverJournals() { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 934 | for (File f : mJournalDir.listFiles()) { |
| 935 | if (mJournal == null || f.compareTo(mJournal) != 0) { |
| 936 | // This isn't the current journal, so it must be a leftover. Read |
| 937 | // out the package names mentioned there and schedule them for |
| 938 | // backup. |
| 939 | RandomAccessFile in = null; |
| 940 | try { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 941 | Slog.i(TAG, "Found stale backup journal, scheduling"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 942 | in = new RandomAccessFile(f, "r"); |
| 943 | while (true) { |
| 944 | String packageName = in.readUTF(); |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 945 | Slog.i(TAG, " " + packageName); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 946 | dataChangedImpl(packageName); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 947 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 948 | } catch (EOFException e) { |
| 949 | // no more data; we're done |
| 950 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 951 | Slog.e(TAG, "Can't read " + f, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 952 | } finally { |
| 953 | // close/delete the file |
| 954 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 955 | f.delete(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 961 | private SecretKey buildPasswordKey(String pw, byte[] salt, int rounds) { |
| 962 | return buildCharArrayKey(pw.toCharArray(), salt, rounds); |
| 963 | } |
| 964 | |
| 965 | private SecretKey buildCharArrayKey(char[] pwArray, byte[] salt, int rounds) { |
| 966 | try { |
| 967 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 968 | KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE); |
| 969 | return keyFactory.generateSecret(ks); |
| 970 | } catch (InvalidKeySpecException e) { |
| 971 | Slog.e(TAG, "Invalid key spec for PBKDF2!"); |
| 972 | } catch (NoSuchAlgorithmException e) { |
| 973 | Slog.e(TAG, "PBKDF2 unavailable!"); |
| 974 | } |
| 975 | return null; |
| 976 | } |
| 977 | |
| 978 | private String buildPasswordHash(String pw, byte[] salt, int rounds) { |
| 979 | SecretKey key = buildPasswordKey(pw, salt, rounds); |
| 980 | if (key != null) { |
| 981 | return byteArrayToHex(key.getEncoded()); |
| 982 | } |
| 983 | return null; |
| 984 | } |
| 985 | |
| 986 | private String byteArrayToHex(byte[] data) { |
| 987 | StringBuilder buf = new StringBuilder(data.length * 2); |
| 988 | for (int i = 0; i < data.length; i++) { |
| 989 | buf.append(Byte.toHexString(data[i], true)); |
| 990 | } |
| 991 | return buf.toString(); |
| 992 | } |
| 993 | |
| 994 | private byte[] hexToByteArray(String digits) { |
| 995 | final int bytes = digits.length() / 2; |
| 996 | if (2*bytes != digits.length()) { |
| 997 | throw new IllegalArgumentException("Hex string must have an even number of digits"); |
| 998 | } |
| 999 | |
| 1000 | byte[] result = new byte[bytes]; |
| 1001 | for (int i = 0; i < digits.length(); i += 2) { |
| 1002 | result[i/2] = (byte) Integer.parseInt(digits.substring(i, i+2), 16); |
| 1003 | } |
| 1004 | return result; |
| 1005 | } |
| 1006 | |
| 1007 | private byte[] makeKeyChecksum(byte[] pwBytes, byte[] salt, int rounds) { |
| 1008 | char[] mkAsChar = new char[pwBytes.length]; |
| 1009 | for (int i = 0; i < pwBytes.length; i++) { |
| 1010 | mkAsChar[i] = (char) pwBytes[i]; |
| 1011 | } |
| 1012 | |
| 1013 | Key checksum = buildCharArrayKey(mkAsChar, salt, rounds); |
| 1014 | return checksum.getEncoded(); |
| 1015 | } |
| 1016 | |
| 1017 | // Used for generating random salts or passwords |
| 1018 | private byte[] randomBytes(int bits) { |
| 1019 | byte[] array = new byte[bits / 8]; |
| 1020 | mRng.nextBytes(array); |
| 1021 | return array; |
| 1022 | } |
| 1023 | |
| 1024 | // Backup password management |
| 1025 | boolean passwordMatchesSaved(String candidatePw, int rounds) { |
| 1026 | if (mPasswordHash == null) { |
| 1027 | // no current password case -- require that 'currentPw' be null or empty |
| 1028 | if (candidatePw == null || "".equals(candidatePw)) { |
| 1029 | return true; |
| 1030 | } // else the non-empty candidate does not match the empty stored pw |
| 1031 | } else { |
| 1032 | // hash the stated current pw and compare to the stored one |
| 1033 | if (candidatePw != null && candidatePw.length() > 0) { |
| 1034 | String currentPwHash = buildPasswordHash(candidatePw, mPasswordSalt, rounds); |
| 1035 | if (mPasswordHash.equalsIgnoreCase(currentPwHash)) { |
| 1036 | // candidate hash matches the stored hash -- the password matches |
| 1037 | return true; |
| 1038 | } |
| 1039 | } // else the stored pw is nonempty but the candidate is empty; no match |
| 1040 | } |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | @Override |
| 1045 | public boolean setBackupPassword(String currentPw, String newPw) { |
| 1046 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1047 | "setBackupPassword"); |
| 1048 | |
| 1049 | // If the supplied pw doesn't hash to the the saved one, fail |
| 1050 | if (!passwordMatchesSaved(currentPw, PBKDF2_HASH_ROUNDS)) { |
| 1051 | return false; |
| 1052 | } |
| 1053 | |
| 1054 | // Clearing the password is okay |
| 1055 | if (newPw == null || newPw.isEmpty()) { |
| 1056 | if (mPasswordHashFile.exists()) { |
| 1057 | if (!mPasswordHashFile.delete()) { |
| 1058 | // Unable to delete the old pw file, so fail |
| 1059 | Slog.e(TAG, "Unable to clear backup password"); |
| 1060 | return false; |
| 1061 | } |
| 1062 | } |
| 1063 | mPasswordHash = null; |
| 1064 | mPasswordSalt = null; |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | try { |
| 1069 | // Okay, build the hash of the new backup password |
| 1070 | byte[] salt = randomBytes(PBKDF2_SALT_SIZE); |
| 1071 | String newPwHash = buildPasswordHash(newPw, salt, PBKDF2_HASH_ROUNDS); |
| 1072 | |
| 1073 | OutputStream pwf = null, buffer = null; |
| 1074 | DataOutputStream out = null; |
| 1075 | try { |
| 1076 | pwf = new FileOutputStream(mPasswordHashFile); |
| 1077 | buffer = new BufferedOutputStream(pwf); |
| 1078 | out = new DataOutputStream(buffer); |
| 1079 | // integer length of the salt array, followed by the salt, |
| 1080 | // then the hex pw hash string |
| 1081 | out.writeInt(salt.length); |
| 1082 | out.write(salt); |
| 1083 | out.writeUTF(newPwHash); |
| 1084 | out.flush(); |
| 1085 | mPasswordHash = newPwHash; |
| 1086 | mPasswordSalt = salt; |
| 1087 | return true; |
| 1088 | } finally { |
| 1089 | if (out != null) out.close(); |
| 1090 | if (buffer != null) buffer.close(); |
| 1091 | if (pwf != null) pwf.close(); |
| 1092 | } |
| 1093 | } catch (IOException e) { |
| 1094 | Slog.e(TAG, "Unable to set backup password"); |
| 1095 | } |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | @Override |
| 1100 | public boolean hasBackupPassword() { |
| 1101 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1102 | "hasBackupPassword"); |
| 1103 | return (mPasswordHash != null && mPasswordHash.length() > 0); |
| 1104 | } |
| 1105 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1106 | // Maintain persistent state around whether need to do an initialize operation. |
| 1107 | // Must be called with the queue lock held. |
| 1108 | void recordInitPendingLocked(boolean isPending, String transportName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1109 | if (DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1110 | + " on transport " + transportName); |
| 1111 | try { |
| 1112 | IBackupTransport transport = getTransport(transportName); |
| 1113 | String transportDirName = transport.transportDirName(); |
| 1114 | File stateDir = new File(mBaseStateDir, transportDirName); |
| 1115 | File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1116 | |
| 1117 | if (isPending) { |
| 1118 | // We need an init before we can proceed with sending backup data. |
| 1119 | // Record that with an entry in our set of pending inits, as well as |
| 1120 | // journaling it via creation of a sentinel file. |
| 1121 | mPendingInits.add(transportName); |
| 1122 | try { |
| 1123 | (new FileOutputStream(initPendingFile)).close(); |
| 1124 | } catch (IOException ioe) { |
| 1125 | // Something is badly wrong with our permissions; just try to move on |
| 1126 | } |
| 1127 | } else { |
| 1128 | // No more initialization needed; wipe the journal and reset our state. |
| 1129 | initPendingFile.delete(); |
| 1130 | mPendingInits.remove(transportName); |
| 1131 | } |
| 1132 | } catch (RemoteException e) { |
| 1133 | // can't happen; the transport is local |
| 1134 | } |
| 1135 | } |
| 1136 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1137 | // Reset all of our bookkeeping, in response to having been told that |
| 1138 | // the backend data has been wiped [due to idle expiry, for example], |
| 1139 | // so we must re-upload all saved settings. |
| 1140 | void resetBackupState(File stateFileDir) { |
| 1141 | synchronized (mQueueLock) { |
| 1142 | // Wipe the "what we've ever backed up" tracking |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1143 | mEverStoredApps.clear(); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1144 | mEverStored.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1145 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1146 | mCurrentToken = 0; |
| 1147 | writeRestoreTokens(); |
| 1148 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1149 | // Remove all the state files |
| 1150 | for (File sf : stateFileDir.listFiles()) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1151 | // ... but don't touch the needs-init sentinel |
| 1152 | if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) { |
| 1153 | sf.delete(); |
| 1154 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1155 | } |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1156 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1157 | |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1158 | // Enqueue a new backup of every participant |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1159 | synchronized (mBackupParticipants) { |
| 1160 | int N = mBackupParticipants.size(); |
| 1161 | for (int i=0; i<N; i++) { |
| 1162 | int uid = mBackupParticipants.keyAt(i); |
| 1163 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 1164 | for (ApplicationInfo app: participants) { |
| 1165 | dataChangedImpl(app.packageName); |
| 1166 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1171 | // Add a transport to our set of available backends. If 'transport' is null, this |
| 1172 | // 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] | 1173 | private void registerTransport(String name, IBackupTransport transport) { |
| 1174 | synchronized (mTransports) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1175 | if (DEBUG) Slog.v(TAG, "Registering transport " + name + " = " + transport); |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1176 | if (transport != null) { |
| 1177 | mTransports.put(name, transport); |
| 1178 | } else { |
| 1179 | mTransports.remove(name); |
| Christopher Tate | b0dcaaf | 2010-01-29 16:27:04 -0800 | [diff] [blame] | 1180 | if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) { |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1181 | mCurrentTransport = null; |
| 1182 | } |
| 1183 | // Nothing further to do in the unregistration case |
| 1184 | return; |
| 1185 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1186 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1187 | |
| 1188 | // If the init sentinel file exists, we need to be sure to perform the init |
| 1189 | // as soon as practical. We also create the state directory at registration |
| 1190 | // time to ensure it's present from the outset. |
| 1191 | try { |
| 1192 | String transportName = transport.transportDirName(); |
| 1193 | File stateDir = new File(mBaseStateDir, transportName); |
| 1194 | stateDir.mkdirs(); |
| 1195 | |
| 1196 | File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1197 | if (initSentinel.exists()) { |
| 1198 | synchronized (mQueueLock) { |
| 1199 | mPendingInits.add(transportName); |
| 1200 | |
| 1201 | // TODO: pick a better starting time than now + 1 minute |
| 1202 | long delay = 1000 * 60; // one minute, in milliseconds |
| 1203 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 1204 | System.currentTimeMillis() + delay, mRunInitIntent); |
| 1205 | } |
| 1206 | } |
| 1207 | } catch (RemoteException e) { |
| 1208 | // can't happen, the transport is local |
| 1209 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1212 | // ----- Track installation/removal of packages ----- |
| 1213 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1214 | public void onReceive(Context context, Intent intent) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1215 | if (DEBUG) Slog.d(TAG, "Received broadcast " + intent); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1216 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1217 | String action = intent.getAction(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1218 | boolean replacing = false; |
| 1219 | boolean added = false; |
| 1220 | Bundle extras = intent.getExtras(); |
| 1221 | String pkgList[] = null; |
| 1222 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1223 | Intent.ACTION_PACKAGE_REMOVED.equals(action) || |
| 1224 | Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1225 | Uri uri = intent.getData(); |
| 1226 | if (uri == null) { |
| 1227 | return; |
| 1228 | } |
| 1229 | String pkgName = uri.getSchemeSpecificPart(); |
| 1230 | if (pkgName != null) { |
| 1231 | pkgList = new String[] { pkgName }; |
| 1232 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1233 | if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 1234 | // use the existing "add with replacement" logic |
| 1235 | if (MORE_DEBUG) Slog.d(TAG, "PACKAGE_REPLACED, updating package " + pkgName); |
| 1236 | added = replacing = true; |
| 1237 | } else { |
| 1238 | added = Intent.ACTION_PACKAGE_ADDED.equals(action); |
| 1239 | replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false); |
| 1240 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1241 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1242 | added = true; |
| 1243 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1244 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1245 | added = false; |
| 1246 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1247 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1248 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1249 | if (pkgList == null || pkgList.length == 0) { |
| 1250 | return; |
| 1251 | } |
| 1252 | if (added) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1253 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1254 | for (String pkgName : pkgList) { |
| 1255 | if (replacing) { |
| 1256 | // The package was just upgraded |
| 1257 | updatePackageParticipantsLocked(pkgName); |
| 1258 | } else { |
| 1259 | // The package was just added |
| 1260 | addPackageParticipantsLocked(pkgName); |
| 1261 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1262 | } |
| 1263 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1264 | } else { |
| 1265 | if (replacing) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1266 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 1267 | } else { |
| 1268 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1269 | for (String pkgName : pkgList) { |
| 1270 | removePackageParticipantsLocked(pkgName); |
| 1271 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | }; |
| 1277 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1278 | // ----- Track connection to GoogleBackupTransport service ----- |
| 1279 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 1280 | public void onServiceConnected(ComponentName name, IBinder service) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1281 | if (DEBUG) Slog.v(TAG, "Connected to Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1282 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1283 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | public void onServiceDisconnected(ComponentName name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1287 | if (DEBUG) Slog.v(TAG, "Disconnected from Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1288 | mGoogleTransport = null; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1289 | registerTransport(name.flattenToShortString(), null); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1290 | } |
| 1291 | }; |
| 1292 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1293 | // Add the backup agents in the given package to our set of known backup participants. |
| 1294 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1295 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1296 | // Look for apps that define the android:backupAgent attribute |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1297 | if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1298 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1299 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1302 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1303 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1304 | if (MORE_DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1305 | Slog.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1306 | for (PackageInfo p : targetPkgs) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1307 | Slog.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1308 | + " uid=" + p.applicationInfo.uid |
| 1309 | + " killAfterRestore=" |
| 1310 | + (((p.applicationInfo.flags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) ? "true" : "false") |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1311 | ); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1315 | for (PackageInfo pkg : targetPkgs) { |
| 1316 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 1317 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1318 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1319 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1320 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1321 | mBackupParticipants.put(uid, set); |
| 1322 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1323 | set.add(pkg.applicationInfo); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1324 | |
| 1325 | // If we've never seen this app before, schedule a backup for it |
| 1326 | if (!mEverStoredApps.contains(pkg.packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1327 | if (DEBUG) Slog.i(TAG, "New app " + pkg.packageName |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1328 | + " never backed up; scheduling"); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 1329 | dataChangedImpl(pkg.packageName); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1330 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1331 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1335 | // Remove the given package's entry from our known active set. If |
| 1336 | // 'packageName' is null, *all* participating apps will be removed. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1337 | void removePackageParticipantsLocked(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1338 | if (DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1339 | List<String> allApps = new ArrayList<String>(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1340 | if (packageName != null) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1341 | allApps.add(packageName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1342 | } else { |
| 1343 | // all apps with agents |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1344 | List<PackageInfo> knownPackages = allAgentPackages(); |
| 1345 | for (PackageInfo pkg : knownPackages) { |
| 1346 | allApps.add(pkg.packageName); |
| 1347 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1348 | } |
| 1349 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1352 | private void removePackageParticipantsLockedInner(String packageName, |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1353 | List<String> allPackageNames) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1354 | if (MORE_DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1355 | Slog.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1356 | + ") removing " + allPackageNames.size() + " entries"); |
| 1357 | for (String p : allPackageNames) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1358 | Slog.v(TAG, " - " + p); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1359 | } |
| 1360 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1361 | for (String pkg : allPackageNames) { |
| 1362 | if (packageName == null || pkg.equals(packageName)) { |
| 1363 | int uid = -1; |
| 1364 | try { |
| 1365 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1366 | uid = info.applicationInfo.uid; |
| 1367 | } catch (NameNotFoundException e) { |
| 1368 | // we don't know this package name, so just skip it for now |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1372 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1373 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1374 | // Find the existing entry with the same package name, and remove it. |
| 1375 | // We can't just remove(app) because the instances are different. |
| 1376 | for (ApplicationInfo entry: set) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1377 | if (entry.packageName.equals(pkg)) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1378 | if (MORE_DEBUG) Slog.v(TAG, " removing participant " + pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1379 | set.remove(entry); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1380 | removeEverBackedUp(pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1384 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1385 | mBackupParticipants.delete(uid); |
| 1386 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1392 | // Returns the set of all applications that define an android:backupAgent attribute |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1393 | List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1394 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1395 | int flags = PackageManager.GET_SIGNATURES; |
| 1396 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 1397 | int N = packages.size(); |
| 1398 | for (int a = N-1; a >= 0; a--) { |
| Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1399 | PackageInfo pkg = packages.get(a); |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1400 | try { |
| 1401 | ApplicationInfo app = pkg.applicationInfo; |
| 1402 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| Christopher Tate | a87240c | 2010-02-12 14:12:34 -0800 | [diff] [blame] | 1403 | || app.backupAgentName == null) { |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1404 | packages.remove(a); |
| 1405 | } |
| 1406 | else { |
| 1407 | // we will need the shared library path, so look that up and store it here |
| 1408 | app = mPackageManager.getApplicationInfo(pkg.packageName, |
| 1409 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 1410 | pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles; |
| 1411 | } |
| 1412 | } catch (NameNotFoundException e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1413 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1416 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1417 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1418 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1419 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 1420 | // action cannot be passed a null package name. |
| 1421 | void updatePackageParticipantsLocked(String packageName) { |
| 1422 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1423 | Slog.e(TAG, "updatePackageParticipants called with null package name"); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1424 | return; |
| 1425 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1426 | if (DEBUG) Slog.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1427 | |
| 1428 | // brute force but small code size |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1429 | List<PackageInfo> allApps = allAgentPackages(); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1430 | List<String> allAppNames = new ArrayList<String>(); |
| 1431 | for (PackageInfo pkg : allApps) { |
| 1432 | allAppNames.add(pkg.packageName); |
| 1433 | } |
| 1434 | removePackageParticipantsLockedInner(packageName, allAppNames); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1435 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1438 | // 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] | 1439 | // backed up at least once |
| 1440 | void logBackupComplete(String packageName) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1441 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return; |
| 1442 | |
| 1443 | synchronized (mEverStoredApps) { |
| 1444 | if (!mEverStoredApps.add(packageName)) return; |
| 1445 | |
| 1446 | RandomAccessFile out = null; |
| 1447 | try { |
| 1448 | out = new RandomAccessFile(mEverStored, "rws"); |
| 1449 | out.seek(out.length()); |
| 1450 | out.writeUTF(packageName); |
| 1451 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1452 | Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1453 | } finally { |
| 1454 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1459 | // Remove our awareness of having ever backed up the given package |
| 1460 | void removeEverBackedUp(String packageName) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1461 | if (DEBUG) Slog.v(TAG, "Removing backed-up knowledge of " + packageName); |
| 1462 | if (MORE_DEBUG) Slog.v(TAG, "New set:"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1463 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1464 | synchronized (mEverStoredApps) { |
| 1465 | // Rewrite the file and rename to overwrite. If we reboot in the middle, |
| 1466 | // we'll recognize on initialization time that the package no longer |
| 1467 | // exists and fix it up then. |
| 1468 | File tempKnownFile = new File(mBaseStateDir, "processed.new"); |
| 1469 | RandomAccessFile known = null; |
| 1470 | try { |
| 1471 | known = new RandomAccessFile(tempKnownFile, "rws"); |
| 1472 | mEverStoredApps.remove(packageName); |
| 1473 | for (String s : mEverStoredApps) { |
| 1474 | known.writeUTF(s); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1475 | if (MORE_DEBUG) Slog.v(TAG, " " + s); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1476 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1477 | known.close(); |
| 1478 | known = null; |
| 1479 | if (!tempKnownFile.renameTo(mEverStored)) { |
| 1480 | throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored); |
| 1481 | } |
| 1482 | } catch (IOException e) { |
| 1483 | // Bad: we couldn't create the new copy. For safety's sake we |
| 1484 | // abandon the whole process and remove all what's-backed-up |
| 1485 | // state entirely, meaning we'll force a backup pass for every |
| 1486 | // participant on the next boot or [re]install. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1487 | Slog.w(TAG, "Error rewriting " + mEverStored, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1488 | mEverStoredApps.clear(); |
| 1489 | tempKnownFile.delete(); |
| 1490 | mEverStored.delete(); |
| 1491 | } finally { |
| 1492 | try { if (known != null) known.close(); } catch (IOException e) {} |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1497 | // Persistently record the current and ancestral backup tokens as well |
| 1498 | // as the set of packages with data [supposedly] available in the |
| 1499 | // ancestral dataset. |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1500 | void writeRestoreTokens() { |
| 1501 | try { |
| 1502 | RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1503 | |
| 1504 | // First, the version number of this record, for futureproofing |
| 1505 | af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION); |
| 1506 | |
| 1507 | // Write the ancestral and current tokens |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1508 | af.writeLong(mAncestralToken); |
| 1509 | af.writeLong(mCurrentToken); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1510 | |
| 1511 | // Now write the set of ancestral packages |
| 1512 | if (mAncestralPackages == null) { |
| 1513 | af.writeInt(-1); |
| 1514 | } else { |
| 1515 | af.writeInt(mAncestralPackages.size()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1516 | if (DEBUG) Slog.v(TAG, "Ancestral packages: " + mAncestralPackages.size()); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1517 | for (String pkgName : mAncestralPackages) { |
| 1518 | af.writeUTF(pkgName); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1519 | if (MORE_DEBUG) Slog.v(TAG, " " + pkgName); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1520 | } |
| 1521 | } |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1522 | af.close(); |
| 1523 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1524 | Slog.w(TAG, "Unable to write token file:", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1528 | // Return the given transport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1529 | private IBackupTransport getTransport(String transportName) { |
| 1530 | synchronized (mTransports) { |
| 1531 | IBackupTransport transport = mTransports.get(transportName); |
| 1532 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1533 | Slog.w(TAG, "Requested unavailable transport: " + transportName); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1534 | } |
| 1535 | return transport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1536 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1539 | // fire off a backup agent, blocking until it attaches or times out |
| 1540 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 1541 | IBackupAgent agent = null; |
| 1542 | synchronized(mAgentConnectLock) { |
| 1543 | mConnecting = true; |
| 1544 | mConnectedAgent = null; |
| 1545 | try { |
| 1546 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1547 | Slog.d(TAG, "awaiting agent for " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1548 | |
| 1549 | // success; wait for the agent to arrive |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1550 | // only wait 10 seconds for the bind to happen |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1551 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1552 | while (mConnecting && mConnectedAgent == null |
| 1553 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1554 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1555 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1556 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1557 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1558 | return null; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | // if we timed out with no connect, abort and move on |
| 1563 | if (mConnecting == true) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1564 | Slog.w(TAG, "Timeout waiting for agent " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1565 | return null; |
| 1566 | } |
| 1567 | agent = mConnectedAgent; |
| 1568 | } |
| 1569 | } catch (RemoteException e) { |
| 1570 | // can't happen |
| 1571 | } |
| 1572 | } |
| 1573 | return agent; |
| 1574 | } |
| 1575 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1576 | // clear an application's data, blocking until the operation completes or times out |
| 1577 | void clearApplicationDataSynchronous(String packageName) { |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1578 | // Don't wipe packages marked allowClearUserData=false |
| 1579 | try { |
| 1580 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1581 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1582 | if (MORE_DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping " |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1583 | + packageName); |
| 1584 | return; |
| 1585 | } |
| 1586 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1587 | Slog.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1588 | return; |
| 1589 | } |
| 1590 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1591 | ClearDataObserver observer = new ClearDataObserver(); |
| 1592 | |
| 1593 | synchronized(mClearDataLock) { |
| 1594 | mClearingData = true; |
| Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1595 | try { |
| 1596 | mActivityManager.clearApplicationUserData(packageName, observer); |
| 1597 | } catch (RemoteException e) { |
| 1598 | // can't happen because the activity manager is in this process |
| 1599 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1600 | |
| 1601 | // only wait 10 seconds for the clear data to happen |
| 1602 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1603 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 1604 | try { |
| 1605 | mClearDataLock.wait(5000); |
| 1606 | } catch (InterruptedException e) { |
| 1607 | // won't happen, but still. |
| 1608 | mClearingData = false; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1615 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1616 | synchronized(mClearDataLock) { |
| 1617 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1618 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1623 | // Get the restore-set token for the best-available restore set for this package: |
| 1624 | // the active set if possible, else the ancestral one. Returns zero if none available. |
| 1625 | long getAvailableRestoreToken(String packageName) { |
| 1626 | long token = mAncestralToken; |
| 1627 | synchronized (mQueueLock) { |
| 1628 | if (mEverStoredApps.contains(packageName)) { |
| 1629 | token = mCurrentToken; |
| 1630 | } |
| 1631 | } |
| 1632 | return token; |
| 1633 | } |
| 1634 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1635 | // ----- |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1636 | // Interface and methods used by the asynchronous-with-timeout backup/restore operations |
| 1637 | |
| 1638 | interface BackupRestoreTask { |
| 1639 | // Execute one tick of whatever state machine the task implements |
| 1640 | void execute(); |
| 1641 | |
| 1642 | // An operation that wanted a callback has completed |
| 1643 | void operationComplete(); |
| 1644 | |
| 1645 | // An operation that wanted a callback has timed out |
| 1646 | void handleTimeout(); |
| 1647 | } |
| 1648 | |
| 1649 | void prepareOperationTimeout(int token, long interval, BackupRestoreTask callback) { |
| 1650 | if (MORE_DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token) |
| 1651 | + " interval=" + interval); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1652 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1653 | mCurrentOperations.put(token, new Operation(OP_PENDING, callback)); |
| 1654 | |
| 1655 | Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0, callback); |
| 1656 | mBackupHandler.sendMessageDelayed(msg, interval); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | // synchronous waiter case |
| 1661 | boolean waitUntilOperationComplete(int token) { |
| 1662 | if (MORE_DEBUG) Slog.i(TAG, "Blocking until operation complete for " |
| 1663 | + Integer.toHexString(token)); |
| 1664 | int finalState = OP_PENDING; |
| 1665 | Operation op = null; |
| 1666 | synchronized (mCurrentOpLock) { |
| 1667 | while (true) { |
| 1668 | op = mCurrentOperations.get(token); |
| 1669 | if (op == null) { |
| 1670 | // mysterious disappearance: treat as success with no callback |
| 1671 | break; |
| 1672 | } else { |
| 1673 | if (op.state == OP_PENDING) { |
| 1674 | try { |
| 1675 | mCurrentOpLock.wait(); |
| 1676 | } catch (InterruptedException e) {} |
| 1677 | // When the wait is notified we loop around and recheck the current state |
| 1678 | } else { |
| 1679 | // No longer pending; we're done |
| 1680 | finalState = op.state; |
| 1681 | break; |
| 1682 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1683 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1684 | } |
| 1685 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1686 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1687 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1688 | if (MORE_DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1689 | + " complete: finalState=" + finalState); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1690 | return finalState == OP_ACKNOWLEDGED; |
| 1691 | } |
| 1692 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1693 | void handleTimeout(int token, Object obj) { |
| 1694 | // Notify any synchronous waiters |
| 1695 | Operation op = null; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1696 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1697 | op = mCurrentOperations.get(token); |
| 1698 | if (MORE_DEBUG) { |
| 1699 | if (op == null) Slog.w(TAG, "Timeout of token " + Integer.toHexString(token) |
| 1700 | + " but no op found"); |
| 1701 | } |
| 1702 | int state = (op != null) ? op.state : OP_TIMEOUT; |
| 1703 | if (state == OP_PENDING) { |
| 1704 | if (DEBUG) Slog.v(TAG, "TIMEOUT: token=" + Integer.toHexString(token)); |
| 1705 | op.state = OP_TIMEOUT; |
| 1706 | mCurrentOperations.put(token, op); |
| 1707 | } |
| 1708 | mCurrentOpLock.notifyAll(); |
| 1709 | } |
| 1710 | |
| 1711 | // If there's a TimeoutHandler for this event, call it |
| 1712 | if (op != null && op.callback != null) { |
| 1713 | op.callback.handleTimeout(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1714 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1717 | // ----- Back up a set of applications via a worker thread ----- |
| 1718 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1719 | enum BackupState { |
| 1720 | INITIAL, |
| 1721 | RUNNING_QUEUE, |
| 1722 | FINAL |
| 1723 | } |
| 1724 | |
| 1725 | class PerformBackupTask implements BackupRestoreTask { |
| 1726 | private static final String TAG = "PerformBackupTask"; |
| 1727 | |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1728 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1729 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1730 | ArrayList<BackupRequest> mOriginalQueue; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1731 | File mStateDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1732 | File mJournal; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1733 | BackupState mCurrentState; |
| 1734 | |
| 1735 | // carried information about the current in-flight operation |
| 1736 | PackageInfo mCurrentPackage; |
| 1737 | File mSavedStateName; |
| 1738 | File mBackupDataName; |
| 1739 | File mNewStateName; |
| 1740 | ParcelFileDescriptor mSavedState; |
| 1741 | ParcelFileDescriptor mBackupData; |
| 1742 | ParcelFileDescriptor mNewState; |
| 1743 | int mStatus; |
| 1744 | boolean mFinished; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1745 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1746 | public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1747 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1748 | mTransport = transport; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1749 | mOriginalQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1750 | mJournal = journal; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1751 | |
| 1752 | try { |
| 1753 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1754 | } catch (RemoteException e) { |
| 1755 | // can't happen; the transport is local |
| 1756 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1757 | |
| 1758 | mCurrentState = BackupState.INITIAL; |
| 1759 | mFinished = false; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1762 | // Main entry point: perform one chunk of work, updating the state as appropriate |
| 1763 | // and reposting the next chunk to the primary backup handler thread. |
| 1764 | @Override |
| 1765 | public void execute() { |
| 1766 | switch (mCurrentState) { |
| 1767 | case INITIAL: |
| 1768 | beginBackup(); |
| 1769 | break; |
| 1770 | |
| 1771 | case RUNNING_QUEUE: |
| 1772 | invokeNextAgent(); |
| 1773 | break; |
| 1774 | |
| 1775 | case FINAL: |
| 1776 | if (!mFinished) finalizeBackup(); |
| 1777 | else { |
| 1778 | Slog.e(TAG, "Duplicate finish"); |
| 1779 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 1780 | mFinished = true; |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1781 | break; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | // We're starting a backup pass. Initialize the transport and send |
| 1786 | // the PM metadata blob if we haven't already. |
| 1787 | void beginBackup() { |
| 1788 | mStatus = BackupConstants.TRANSPORT_OK; |
| 1789 | |
| 1790 | // Sanity check: if the queue is empty we have no work to do. |
| 1791 | if (mOriginalQueue.isEmpty()) { |
| 1792 | Slog.w(TAG, "Backup begun with an empty queue - nothing to do."); |
| 1793 | return; |
| 1794 | } |
| 1795 | |
| 1796 | // We need to retain the original queue contents in case of transport |
| 1797 | // failure, but we want a working copy that we can manipulate along |
| 1798 | // the way. |
| 1799 | mQueue = (ArrayList<BackupRequest>) mOriginalQueue.clone(); |
| 1800 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1801 | if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1802 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1803 | File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1804 | try { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1805 | EventLog.writeEvent(EventLogTags.BACKUP_START, mTransport.transportDirName()); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1806 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1807 | // 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] | 1808 | if (mStatus == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1809 | Slog.i(TAG, "Initializing (wiping) backup state and transport storage"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1810 | resetBackupState(mStateDir); // Just to make sure. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1811 | mStatus = mTransport.initializeDevice(); |
| 1812 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1813 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1814 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1815 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1816 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1817 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1818 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1819 | |
| 1820 | // The package manager doesn't have a proper <application> etc, but since |
| 1821 | // it's running here in the system process we can just set up its agent |
| 1822 | // directly and use a synthetic BackupRequest. We always run this pass |
| 1823 | // because it's cheap and this way we guarantee that we don't get out of |
| 1824 | // step even if we're selecting among various transports at run time. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1825 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1826 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1827 | mPackageManager, allAgentPackages()); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1828 | mStatus = invokeAgentForBackup(PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1829 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport); |
| 1830 | } |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1831 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1832 | if (mStatus == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| 1833 | // The backend reports that our dataset has been wiped. Note this in |
| 1834 | // the event log; the no-success code below will reset the backup |
| 1835 | // state as well. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1836 | EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1837 | } |
| 1838 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1839 | Slog.e(TAG, "Error in backup thread", e); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1840 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1841 | } finally { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1842 | // If we've succeeded so far, invokeAgentForBackup() will have run the PM |
| 1843 | // metadata and its completion/timeout callback will continue the state |
| 1844 | // machine chain. If it failed that won't happen; we handle that now. |
| 1845 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 1846 | // if things went wrong at this point, we need to |
| 1847 | // restage everything and try again later. |
| 1848 | resetBackupState(mStateDir); // Just to make sure. |
| 1849 | executeNextState(BackupState.FINAL); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1850 | } |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1851 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1854 | // Transport has been initialized and the PM metadata submitted successfully |
| 1855 | // if that was warranted. Now we process the single next thing in the queue. |
| 1856 | void invokeNextAgent() { |
| 1857 | mStatus = BackupConstants.TRANSPORT_OK; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1858 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1859 | // Sanity check that we have work to do. If not, skip to the end where |
| 1860 | // we reestablish the wakelock invariants etc. |
| 1861 | if (mQueue.isEmpty()) { |
| 1862 | Slog.e(TAG, "Running queue but it's empty!"); |
| 1863 | executeNextState(BackupState.FINAL); |
| 1864 | return; |
| 1865 | } |
| 1866 | |
| 1867 | // pop the entry we're going to process on this step |
| 1868 | BackupRequest request = mQueue.get(0); |
| 1869 | mQueue.remove(0); |
| 1870 | |
| 1871 | Slog.d(TAG, "starting agent for backup of " + request); |
| 1872 | |
| 1873 | // Verify that the requested app exists; it might be something that |
| 1874 | // requested a backup but was then uninstalled. The request was |
| 1875 | // journalled and rather than tamper with the journal it's safer |
| 1876 | // to sanity-check here. This also gives us the classname of the |
| 1877 | // package's backup agent. |
| 1878 | try { |
| 1879 | mCurrentPackage = mPackageManager.getPackageInfo(request.packageName, |
| 1880 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1881 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1882 | IBackupAgent agent = null; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1883 | try { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1884 | mWakelock.setWorkSource(new WorkSource(mCurrentPackage.applicationInfo.uid)); |
| 1885 | agent = bindToAgentSynchronous(mCurrentPackage.applicationInfo, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1886 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1887 | if (agent != null) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1888 | mStatus = invokeAgentForBackup(request.packageName, agent, mTransport); |
| 1889 | // at this point we'll either get a completion callback from the |
| 1890 | // agent, or a timeout message on the main handler. either way, we're |
| 1891 | // done here as long as we're successful so far. |
| 1892 | } else { |
| 1893 | // Timeout waiting for the agent |
| 1894 | mStatus = BackupConstants.AGENT_ERROR; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1895 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1896 | } catch (SecurityException ex) { |
| 1897 | // Try for the next one. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1898 | Slog.d(TAG, "error in bind/backup", ex); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1899 | mStatus = BackupConstants.AGENT_ERROR; |
| 1900 | } |
| 1901 | } catch (NameNotFoundException e) { |
| 1902 | Slog.d(TAG, "Package does not exist; skipping"); |
| 1903 | } finally { |
| 1904 | mWakelock.setWorkSource(null); |
| 1905 | |
| 1906 | // If there was an agent error, no timeout/completion handling will occur. |
| 1907 | // That means we need to deal with the next state ourselves. |
| 1908 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 1909 | BackupState nextState = BackupState.RUNNING_QUEUE; |
| 1910 | |
| 1911 | // An agent-level failure means we reenqueue this one agent for |
| 1912 | // a later retry, but otherwise proceed normally. |
| 1913 | if (mStatus == BackupConstants.AGENT_ERROR) { |
| 1914 | if (MORE_DEBUG) Slog.i(TAG, "Agent failure for " + request.packageName |
| 1915 | + " - restaging"); |
| 1916 | dataChangedImpl(request.packageName); |
| 1917 | mStatus = BackupConstants.TRANSPORT_OK; |
| 1918 | if (mQueue.isEmpty()) nextState = BackupState.FINAL; |
| 1919 | } else if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 1920 | // Transport-level failure means we reenqueue everything |
| 1921 | revertAndEndBackup(); |
| 1922 | nextState = BackupState.FINAL; |
| 1923 | } |
| 1924 | |
| 1925 | executeNextState(nextState); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1929 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1930 | void finalizeBackup() { |
| 1931 | // Either backup was successful, in which case we of course do not need |
| 1932 | // this pass's journal any more; or it failed, in which case we just |
| 1933 | // re-enqueued all of these packages in the current active journal. |
| 1934 | // Either way, we no longer need this pass's journal. |
| 1935 | if (mJournal != null && !mJournal.delete()) { |
| 1936 | Slog.e(TAG, "Unable to remove backup journal file " + mJournal); |
| 1937 | } |
| 1938 | |
| 1939 | // If everything actually went through and this is the first time we've |
| 1940 | // done a backup, we can now record what the current backup dataset token |
| 1941 | // is. |
| 1942 | if ((mCurrentToken == 0) && (mStatus == BackupConstants.TRANSPORT_OK)) { |
| 1943 | try { |
| 1944 | mCurrentToken = mTransport.getCurrentRestoreSet(); |
| 1945 | } catch (RemoteException e) {} // can't happen |
| 1946 | writeRestoreTokens(); |
| 1947 | } |
| 1948 | |
| 1949 | // Set up the next backup pass |
| 1950 | if (mStatus == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| 1951 | backupNow(); |
| 1952 | } |
| 1953 | |
| 1954 | // Only once we're entirely finished do we release the wakelock |
| 1955 | Slog.i(TAG, "Backup pass finished."); |
| 1956 | mWakelock.release(); |
| 1957 | } |
| 1958 | |
| 1959 | // Invoke an agent's doBackup() and start a timeout message spinning on the main |
| 1960 | // handler in case it doesn't get back to us. |
| 1961 | int invokeAgentForBackup(String packageName, IBackupAgent agent, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1962 | IBackupTransport transport) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1963 | if (DEBUG) Slog.d(TAG, "processOneBackup doBackup() on " + packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1964 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1965 | mSavedStateName = new File(mStateDir, packageName); |
| 1966 | mBackupDataName = new File(mDataDir, packageName + ".data"); |
| 1967 | mNewStateName = new File(mStateDir, packageName + ".new"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1968 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1969 | mSavedState = null; |
| 1970 | mBackupData = null; |
| 1971 | mNewState = null; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1972 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1973 | final int token = generateToken(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1974 | try { |
| 1975 | // Look up the package info & signatures. This is first so that if it |
| 1976 | // throws an exception, there's no file setup yet that would need to |
| 1977 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1978 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1979 | // The metadata 'package' is synthetic; construct one and make |
| 1980 | // sure our global state is pointed at it |
| 1981 | mCurrentPackage = new PackageInfo(); |
| 1982 | mCurrentPackage.packageName = packageName; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1983 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1984 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1985 | // In a full backup, we pass a null ParcelFileDescriptor as |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1986 | // the saved-state "file". This is by definition an incremental, |
| 1987 | // so we build a saved state file to pass. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1988 | mSavedState = ParcelFileDescriptor.open(mSavedStateName, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1989 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 1990 | ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1991 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1992 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1993 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1994 | ParcelFileDescriptor.MODE_CREATE | |
| 1995 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1996 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 1997 | mNewState = ParcelFileDescriptor.open(mNewStateName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1998 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1999 | ParcelFileDescriptor.MODE_CREATE | |
| 2000 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2001 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 2002 | // Initiate the target's backup pass |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2003 | prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL, this); |
| 2004 | agent.doBackup(mSavedState, mBackupData, mNewState, token, mBackupManagerBinder); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2005 | } catch (Exception e) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2006 | Slog.e(TAG, "Error invoking for backup on " + packageName); |
| 2007 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, |
| 2008 | e.toString()); |
| 2009 | agentErrorCleanup(); |
| 2010 | return BackupConstants.AGENT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2013 | // At this point the agent is off and running. The next thing to happen will |
| 2014 | // either be a callback from the agent, at which point we'll process its data |
| 2015 | // for transport, or a timeout. Either way the next phase will happen in |
| 2016 | // response to the TimeoutHandler interface callbacks. |
| 2017 | return BackupConstants.TRANSPORT_OK; |
| 2018 | } |
| 2019 | |
| 2020 | @Override |
| 2021 | public void operationComplete() { |
| 2022 | // Okay, the agent successfully reported back to us. Spin the data off to the |
| 2023 | // transport and proceed with the next stage. |
| 2024 | if (MORE_DEBUG) Slog.v(TAG, "operationComplete(): sending data to transport for " |
| 2025 | + mCurrentPackage.packageName); |
| 2026 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 2027 | clearAgentState(); |
| 2028 | |
| 2029 | ParcelFileDescriptor backupData = null; |
| 2030 | mStatus = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2031 | try { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2032 | int size = (int) mBackupDataName.length(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2033 | if (size > 0) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2034 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| 2035 | backupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2036 | ParcelFileDescriptor.MODE_READ_ONLY); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2037 | mStatus = mTransport.performBackup(mCurrentPackage, backupData); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2038 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2039 | |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 2040 | // TODO - We call finishBackup() for each application backed up, because |
| 2041 | // we need to know now whether it succeeded or failed. Instead, we should |
| 2042 | // hold off on finishBackup() until the end, which implies holding off on |
| 2043 | // renaming *all* the output state files (see below) until that happens. |
| 2044 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2045 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| 2046 | mStatus = mTransport.finishBackup(); |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 2047 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2048 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2049 | if (DEBUG) Slog.i(TAG, "no backup data written; not calling transport"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | // After successful transport, delete the now-stale data |
| 2053 | // and juggle the files so that next time we supply the agent |
| 2054 | // with the new state file it just created. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2055 | if (mStatus == BackupConstants.TRANSPORT_OK) { |
| 2056 | mBackupDataName.delete(); |
| 2057 | mNewStateName.renameTo(mSavedStateName); |
| 2058 | EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, |
| 2059 | mCurrentPackage.packageName, size); |
| 2060 | logBackupComplete(mCurrentPackage.packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2061 | } else { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2062 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, |
| 2063 | mCurrentPackage.packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 2064 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2065 | } catch (Exception e) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2066 | Slog.e(TAG, "Transport error backing up " + mCurrentPackage.packageName, e); |
| 2067 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, |
| 2068 | mCurrentPackage.packageName); |
| 2069 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 2070 | } finally { |
| 2071 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2072 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 2073 | |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2074 | // If we encountered an error here it's a transport-level failure. That |
| 2075 | // means we need to halt everything and reschedule everything for next time. |
| 2076 | final BackupState nextState; |
| 2077 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| 2078 | revertAndEndBackup(); |
| 2079 | nextState = BackupState.FINAL; |
| 2080 | } else { |
| 2081 | // Success! Proceed with the next app if any, otherwise we're done. |
| 2082 | nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE; |
| 2083 | } |
| 2084 | |
| 2085 | executeNextState(nextState); |
| 2086 | } |
| 2087 | |
| 2088 | @Override |
| 2089 | public void handleTimeout() { |
| 2090 | // Whoops, the current agent timed out running doBackup(). Tidy up and restage |
| 2091 | // it for the next time we run a backup pass. |
| 2092 | // !!! TODO: keep track of failure counts per agent, and blacklist those which |
| 2093 | // fail repeatedly (i.e. have proved themselves to be buggy). |
| 2094 | Slog.e(TAG, "Timeout backing up " + mCurrentPackage.packageName); |
| 2095 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, mCurrentPackage.packageName, |
| 2096 | "timeout"); |
| 2097 | agentErrorCleanup(); |
| 2098 | dataChangedImpl(mCurrentPackage.packageName); |
| 2099 | } |
| 2100 | |
| 2101 | void revertAndEndBackup() { |
| 2102 | if (MORE_DEBUG) Slog.i(TAG, "Reverting backup queue - restaging everything"); |
| 2103 | for (BackupRequest request : mOriginalQueue) { |
| 2104 | dataChangedImpl(request.packageName); |
| 2105 | } |
| 2106 | // We also want to reset the backup schedule based on whatever |
| 2107 | // the transport suggests by way of retry/backoff time. |
| 2108 | restartBackupAlarm(); |
| 2109 | } |
| 2110 | |
| 2111 | void agentErrorCleanup() { |
| 2112 | mBackupDataName.delete(); |
| 2113 | mNewStateName.delete(); |
| 2114 | clearAgentState(); |
| 2115 | |
| 2116 | executeNextState(mQueue.isEmpty() ? BackupState.FINAL : BackupState.RUNNING_QUEUE); |
| 2117 | } |
| 2118 | |
| 2119 | // Cleanup common to both success and failure cases |
| 2120 | void clearAgentState() { |
| 2121 | try { if (mSavedState != null) mSavedState.close(); } catch (IOException e) {} |
| 2122 | try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {} |
| 2123 | try { if (mNewState != null) mNewState.close(); } catch (IOException e) {} |
| 2124 | mSavedState = mBackupData = mNewState = null; |
| 2125 | synchronized (mCurrentOpLock) { |
| 2126 | mCurrentOperations.clear(); |
| 2127 | } |
| 2128 | |
| 2129 | // If this was a pseudopackage there's no associated Activity Manager state |
| 2130 | if (mCurrentPackage.applicationInfo != null) { |
| 2131 | try { // unbind even on timeout, just in case |
| 2132 | mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo); |
| 2133 | } catch (RemoteException e) {} |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | void restartBackupAlarm() { |
| 2138 | synchronized (mQueueLock) { |
| 2139 | try { |
| 2140 | startBackupAlarmsLocked(mTransport.requestBackupTime()); |
| 2141 | } catch (RemoteException e) { /* cannot happen */ } |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | void executeNextState(BackupState nextState) { |
| 2146 | if (MORE_DEBUG) Slog.i(TAG, " => executing next step on " |
| 2147 | + this + " nextState=" + nextState); |
| 2148 | mCurrentState = nextState; |
| 2149 | Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this); |
| 2150 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 2151 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 2154 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2155 | // ----- Full backup to a file/socket ----- |
| 2156 | |
| 2157 | class PerformFullBackupTask implements Runnable { |
| 2158 | ParcelFileDescriptor mOutputFile; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2159 | DeflaterOutputStream mDeflater; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2160 | IFullBackupRestoreObserver mObserver; |
| 2161 | boolean mIncludeApks; |
| 2162 | boolean mIncludeShared; |
| 2163 | boolean mAllApps; |
| 2164 | String[] mPackages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2165 | String mCurrentPassword; |
| 2166 | String mEncryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2167 | AtomicBoolean mLatchObject; |
| 2168 | File mFilesDir; |
| 2169 | File mManifestFile; |
| 2170 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2171 | class FullBackupRunner implements Runnable { |
| 2172 | PackageInfo mPackage; |
| 2173 | IBackupAgent mAgent; |
| 2174 | ParcelFileDescriptor mPipe; |
| 2175 | int mToken; |
| 2176 | boolean mSendApk; |
| 2177 | |
| 2178 | FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe, |
| 2179 | int token, boolean sendApk) throws IOException { |
| 2180 | mPackage = pack; |
| 2181 | mAgent = agent; |
| 2182 | mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor()); |
| 2183 | mToken = token; |
| 2184 | mSendApk = sendApk; |
| 2185 | } |
| 2186 | |
| 2187 | @Override |
| 2188 | public void run() { |
| 2189 | try { |
| 2190 | BackupDataOutput output = new BackupDataOutput( |
| 2191 | mPipe.getFileDescriptor()); |
| 2192 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2193 | if (MORE_DEBUG) Slog.d(TAG, "Writing manifest for " + mPackage.packageName); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2194 | writeAppManifest(mPackage, mManifestFile, mSendApk); |
| 2195 | FullBackup.backupToTar(mPackage.packageName, null, null, |
| 2196 | mFilesDir.getAbsolutePath(), |
| 2197 | mManifestFile.getAbsolutePath(), |
| 2198 | output); |
| 2199 | |
| 2200 | if (mSendApk) { |
| 2201 | writeApkToBackup(mPackage, output); |
| 2202 | } |
| 2203 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2204 | if (DEBUG) Slog.d(TAG, "Calling doFullBackup() on " + mPackage.packageName); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2205 | prepareOperationTimeout(mToken, TIMEOUT_FULL_BACKUP_INTERVAL, null); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2206 | mAgent.doFullBackup(mPipe, mToken, mBackupManagerBinder); |
| 2207 | } catch (IOException e) { |
| 2208 | Slog.e(TAG, "Error running full backup for " + mPackage.packageName); |
| 2209 | } catch (RemoteException e) { |
| 2210 | Slog.e(TAG, "Remote agent vanished during full backup of " |
| 2211 | + mPackage.packageName); |
| 2212 | } finally { |
| 2213 | try { |
| 2214 | mPipe.close(); |
| 2215 | } catch (IOException e) {} |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2220 | PerformFullBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2221 | boolean includeApks, boolean includeShared, String curPassword, |
| 2222 | String encryptPassword, boolean doAllApps, String[] packages, |
| 2223 | AtomicBoolean latch) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2224 | mOutputFile = fd; |
| 2225 | mObserver = observer; |
| 2226 | mIncludeApks = includeApks; |
| 2227 | mIncludeShared = includeShared; |
| 2228 | mAllApps = doAllApps; |
| 2229 | mPackages = packages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2230 | mCurrentPassword = curPassword; |
| 2231 | // when backing up, if there is a current backup password, we require that |
| 2232 | // the user use a nonempty encryption password as well. if one is supplied |
| 2233 | // in the UI we use that, but if the UI was left empty we fall back to the |
| 2234 | // current backup password (which was supplied by the user as well). |
| 2235 | if (encryptPassword == null || "".equals(encryptPassword)) { |
| 2236 | mEncryptPassword = curPassword; |
| 2237 | } else { |
| 2238 | mEncryptPassword = encryptPassword; |
| 2239 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2240 | mLatchObject = latch; |
| 2241 | |
| 2242 | mFilesDir = new File("/data/system"); |
| 2243 | mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME); |
| 2244 | } |
| 2245 | |
| 2246 | @Override |
| 2247 | public void run() { |
| 2248 | final List<PackageInfo> packagesToBackup; |
| 2249 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2250 | Slog.i(TAG, "--- Performing full-dataset backup ---"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2251 | sendStartBackup(); |
| 2252 | |
| 2253 | // doAllApps supersedes the package set if any |
| 2254 | if (mAllApps) { |
| 2255 | packagesToBackup = mPackageManager.getInstalledPackages( |
| 2256 | PackageManager.GET_SIGNATURES); |
| 2257 | } else { |
| 2258 | packagesToBackup = new ArrayList<PackageInfo>(); |
| 2259 | for (String pkgName : mPackages) { |
| 2260 | try { |
| 2261 | packagesToBackup.add(mPackageManager.getPackageInfo(pkgName, |
| 2262 | PackageManager.GET_SIGNATURES)); |
| 2263 | } catch (NameNotFoundException e) { |
| 2264 | Slog.w(TAG, "Unknown package " + pkgName + ", skipping"); |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2269 | // Cull any packages that have indicated that backups are not permitted. |
| 2270 | for (int i = 0; i < packagesToBackup.size(); ) { |
| 2271 | PackageInfo info = packagesToBackup.get(i); |
| 2272 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 2273 | packagesToBackup.remove(i); |
| 2274 | } else { |
| 2275 | i++; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2279 | FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2280 | OutputStream out = null; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2281 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2282 | PackageInfo pkg = null; |
| 2283 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2284 | boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2285 | boolean compressing = COMPRESS_FULL_BACKUPS; |
| 2286 | OutputStream finalOutput = ofstream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2287 | |
| Christopher Tate | eef4ae4 | 2011-08-05 13:15:53 -0700 | [diff] [blame] | 2288 | // Verify that the given password matches the currently-active |
| 2289 | // backup password, if any |
| 2290 | if (hasBackupPassword()) { |
| 2291 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2292 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2293 | return; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2297 | // Write the global file header. All strings are UTF-8 encoded; lines end |
| 2298 | // with a '\n' byte. Actual backup data begins immediately following the |
| 2299 | // final '\n'. |
| 2300 | // |
| 2301 | // line 1: "ANDROID BACKUP" |
| 2302 | // line 2: backup file format version, currently "1" |
| 2303 | // line 3: compressed? "0" if not compressed, "1" if compressed. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2304 | // line 4: name of encryption algorithm [currently only "none" or "AES-256"] |
| 2305 | // |
| 2306 | // When line 4 is not "none", then additional header data follows: |
| 2307 | // |
| 2308 | // line 5: user password salt [hex] |
| 2309 | // line 6: master key checksum salt [hex] |
| 2310 | // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal] |
| 2311 | // line 8: IV of the user key [hex] |
| 2312 | // line 9: master key blob [hex] |
| 2313 | // IV of the master key, master key itself, master key checksum hash |
| 2314 | // |
| 2315 | // The master key checksum is the master key plus its checksum salt, run through |
| 2316 | // 10k rounds of PBKDF2. This is used to verify that the user has supplied the |
| 2317 | // correct password for decrypting the archive: the master key decrypted from |
| 2318 | // the archive using the user-supplied password is also run through PBKDF2 in |
| 2319 | // this way, and if the result does not match the checksum as stored in the |
| 2320 | // archive, then we know that the user-supplied password does not match the |
| 2321 | // archive's. |
| 2322 | StringBuilder headerbuf = new StringBuilder(1024); |
| 2323 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2324 | headerbuf.append(BACKUP_FILE_HEADER_MAGIC); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2325 | headerbuf.append(BACKUP_FILE_VERSION); // integer, no trailing \n |
| 2326 | headerbuf.append(compressing ? "\n1\n" : "\n0\n"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2327 | |
| 2328 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2329 | // Set up the encryption stage if appropriate, and emit the correct header |
| 2330 | if (encrypting) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2331 | finalOutput = emitAesBackupHeader(headerbuf, finalOutput); |
| 2332 | } else { |
| 2333 | headerbuf.append("none\n"); |
| 2334 | } |
| 2335 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2336 | byte[] header = headerbuf.toString().getBytes("UTF-8"); |
| 2337 | ofstream.write(header); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2338 | |
| 2339 | // Set up the compression stage feeding into the encryption stage (if any) |
| 2340 | if (compressing) { |
| 2341 | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); |
| 2342 | finalOutput = new DeflaterOutputStream(finalOutput, deflater, true); |
| 2343 | } |
| 2344 | |
| 2345 | out = finalOutput; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2346 | } catch (Exception e) { |
| 2347 | // Should never happen! |
| 2348 | Slog.e(TAG, "Unable to emit archive header", e); |
| 2349 | return; |
| 2350 | } |
| 2351 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2352 | // Now back up the app data via the agent mechanism |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2353 | int N = packagesToBackup.size(); |
| 2354 | for (int i = 0; i < N; i++) { |
| 2355 | pkg = packagesToBackup.get(i); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2356 | backupOnePackage(pkg, out); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2357 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2358 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2359 | // Shared storage if requested |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2360 | if (mIncludeShared) { |
| 2361 | backupSharedStorage(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2362 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2363 | |
| 2364 | // Done! |
| 2365 | finalizeBackup(out); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2366 | } catch (RemoteException e) { |
| 2367 | Slog.e(TAG, "App died during full backup"); |
| 2368 | } finally { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2369 | tearDown(pkg); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2370 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2371 | if (out != null) out.close(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2372 | mOutputFile.close(); |
| 2373 | } catch (IOException e) { |
| 2374 | /* nothing we can do about this */ |
| 2375 | } |
| 2376 | synchronized (mCurrentOpLock) { |
| 2377 | mCurrentOperations.clear(); |
| 2378 | } |
| 2379 | synchronized (mLatchObject) { |
| 2380 | mLatchObject.set(true); |
| 2381 | mLatchObject.notifyAll(); |
| 2382 | } |
| 2383 | sendEndBackup(); |
| 2384 | mWakelock.release(); |
| 2385 | if (DEBUG) Slog.d(TAG, "Full backup pass complete."); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2389 | private OutputStream emitAesBackupHeader(StringBuilder headerbuf, |
| 2390 | OutputStream ofstream) throws Exception { |
| 2391 | // User key will be used to encrypt the master key. |
| 2392 | byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2393 | SecretKey userKey = buildPasswordKey(mEncryptPassword, newUserSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2394 | PBKDF2_HASH_ROUNDS); |
| 2395 | |
| 2396 | // the master key is random for each backup |
| 2397 | byte[] masterPw = new byte[256 / 8]; |
| 2398 | mRng.nextBytes(masterPw); |
| 2399 | byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE); |
| 2400 | |
| 2401 | // primary encryption of the datastream with the random key |
| 2402 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2403 | SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES"); |
| 2404 | c.init(Cipher.ENCRYPT_MODE, masterKeySpec); |
| 2405 | OutputStream finalOutput = new CipherOutputStream(ofstream, c); |
| 2406 | |
| 2407 | // line 4: name of encryption algorithm |
| 2408 | headerbuf.append(ENCRYPTION_ALGORITHM_NAME); |
| 2409 | headerbuf.append('\n'); |
| 2410 | // line 5: user password salt [hex] |
| 2411 | headerbuf.append(byteArrayToHex(newUserSalt)); |
| 2412 | headerbuf.append('\n'); |
| 2413 | // line 6: master key checksum salt [hex] |
| 2414 | headerbuf.append(byteArrayToHex(checksumSalt)); |
| 2415 | headerbuf.append('\n'); |
| 2416 | // line 7: number of PBKDF2 rounds used [decimal] |
| 2417 | headerbuf.append(PBKDF2_HASH_ROUNDS); |
| 2418 | headerbuf.append('\n'); |
| 2419 | |
| 2420 | // line 8: IV of the user key [hex] |
| 2421 | Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2422 | mkC.init(Cipher.ENCRYPT_MODE, userKey); |
| 2423 | |
| 2424 | byte[] IV = mkC.getIV(); |
| 2425 | headerbuf.append(byteArrayToHex(IV)); |
| 2426 | headerbuf.append('\n'); |
| 2427 | |
| 2428 | // line 9: master IV + key blob, encrypted by the user key [hex]. Blob format: |
| 2429 | // [byte] IV length = Niv |
| 2430 | // [array of Niv bytes] IV itself |
| 2431 | // [byte] master key length = Nmk |
| 2432 | // [array of Nmk bytes] master key itself |
| 2433 | // [byte] MK checksum hash length = Nck |
| 2434 | // [array of Nck bytes] master key checksum hash |
| 2435 | // |
| 2436 | // The checksum is the (master key + checksum salt), run through the |
| 2437 | // stated number of PBKDF2 rounds |
| 2438 | IV = c.getIV(); |
| 2439 | byte[] mk = masterKeySpec.getEncoded(); |
| 2440 | byte[] checksum = makeKeyChecksum(masterKeySpec.getEncoded(), |
| 2441 | checksumSalt, PBKDF2_HASH_ROUNDS); |
| 2442 | |
| 2443 | ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length |
| 2444 | + checksum.length + 3); |
| 2445 | DataOutputStream mkOut = new DataOutputStream(blob); |
| 2446 | mkOut.writeByte(IV.length); |
| 2447 | mkOut.write(IV); |
| 2448 | mkOut.writeByte(mk.length); |
| 2449 | mkOut.write(mk); |
| 2450 | mkOut.writeByte(checksum.length); |
| 2451 | mkOut.write(checksum); |
| 2452 | mkOut.flush(); |
| 2453 | byte[] encryptedMk = mkC.doFinal(blob.toByteArray()); |
| 2454 | headerbuf.append(byteArrayToHex(encryptedMk)); |
| 2455 | headerbuf.append('\n'); |
| 2456 | |
| 2457 | return finalOutput; |
| 2458 | } |
| 2459 | |
| 2460 | private void backupOnePackage(PackageInfo pkg, OutputStream out) |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2461 | throws RemoteException { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2462 | Slog.d(TAG, "Binding to full backup agent : " + pkg.packageName); |
| 2463 | |
| 2464 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 2465 | IApplicationThread.BACKUP_MODE_FULL); |
| 2466 | if (agent != null) { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2467 | ParcelFileDescriptor[] pipes = null; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2468 | try { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2469 | pipes = ParcelFileDescriptor.createPipe(); |
| 2470 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2471 | ApplicationInfo app = pkg.applicationInfo; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2472 | final boolean sendApk = mIncludeApks |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2473 | && ((app.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0) |
| 2474 | && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 || |
| 2475 | (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); |
| 2476 | |
| 2477 | sendOnBackupPackage(pkg.packageName); |
| 2478 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2479 | final int token = generateToken(); |
| 2480 | FullBackupRunner runner = new FullBackupRunner(pkg, agent, pipes[1], |
| 2481 | token, sendApk); |
| 2482 | pipes[1].close(); // the runner has dup'd it |
| 2483 | pipes[1] = null; |
| 2484 | Thread t = new Thread(runner); |
| 2485 | t.start(); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2486 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2487 | // Now pull data from the app and stuff it into the compressor |
| 2488 | try { |
| 2489 | FileInputStream raw = new FileInputStream(pipes[0].getFileDescriptor()); |
| 2490 | DataInputStream in = new DataInputStream(raw); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2491 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2492 | byte[] buffer = new byte[16 * 1024]; |
| 2493 | int chunkTotal; |
| 2494 | while ((chunkTotal = in.readInt()) > 0) { |
| 2495 | while (chunkTotal > 0) { |
| 2496 | int toRead = (chunkTotal > buffer.length) |
| 2497 | ? buffer.length : chunkTotal; |
| 2498 | int nRead = in.read(buffer, 0, toRead); |
| 2499 | out.write(buffer, 0, nRead); |
| 2500 | chunkTotal -= nRead; |
| 2501 | } |
| 2502 | } |
| 2503 | } catch (IOException e) { |
| 2504 | Slog.i(TAG, "Caught exception reading from agent", e); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2507 | if (!waitUntilOperationComplete(token)) { |
| 2508 | Slog.e(TAG, "Full backup failed on package " + pkg.packageName); |
| 2509 | } else { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2510 | if (DEBUG) Slog.d(TAG, "Full package backup success: " + pkg.packageName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2511 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2512 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2513 | } catch (IOException e) { |
| 2514 | Slog.e(TAG, "Error backing up " + pkg.packageName, e); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2515 | } finally { |
| 2516 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2517 | // flush after every package |
| 2518 | out.flush(); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2519 | if (pipes != null) { |
| 2520 | if (pipes[0] != null) pipes[0].close(); |
| 2521 | if (pipes[1] != null) pipes[1].close(); |
| 2522 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2523 | } catch (IOException e) { |
| 2524 | Slog.w(TAG, "Error bringing down backup stack"); |
| 2525 | } |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2526 | } |
| 2527 | } else { |
| 2528 | Slog.w(TAG, "Unable to bind to full agent for " + pkg.packageName); |
| 2529 | } |
| 2530 | tearDown(pkg); |
| 2531 | } |
| 2532 | |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2533 | private void writeApkToBackup(PackageInfo pkg, BackupDataOutput output) { |
| 2534 | // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here |
| 2535 | final String appSourceDir = pkg.applicationInfo.sourceDir; |
| 2536 | final String apkDir = new File(appSourceDir).getParent(); |
| 2537 | FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null, |
| 2538 | apkDir, appSourceDir, output); |
| 2539 | |
| 2540 | // Save associated .obb content if it exists and we did save the apk |
| 2541 | // check for .obb and save those too |
| 2542 | final File obbDir = Environment.getExternalStorageAppObbDirectory(pkg.packageName); |
| 2543 | if (obbDir != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2544 | if (MORE_DEBUG) Log.i(TAG, "obb dir: " + obbDir.getAbsolutePath()); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2545 | File[] obbFiles = obbDir.listFiles(); |
| 2546 | if (obbFiles != null) { |
| 2547 | final String obbDirName = obbDir.getAbsolutePath(); |
| 2548 | for (File obb : obbFiles) { |
| 2549 | FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null, |
| 2550 | obbDirName, obb.getAbsolutePath(), output); |
| 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2556 | private void backupSharedStorage() throws RemoteException { |
| 2557 | PackageInfo pkg = null; |
| 2558 | try { |
| 2559 | pkg = mPackageManager.getPackageInfo("com.android.sharedstoragebackup", 0); |
| 2560 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 2561 | IApplicationThread.BACKUP_MODE_FULL); |
| 2562 | if (agent != null) { |
| 2563 | sendOnBackupPackage("Shared storage"); |
| 2564 | |
| 2565 | final int token = generateToken(); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 2566 | prepareOperationTimeout(token, TIMEOUT_SHARED_BACKUP_INTERVAL, null); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2567 | agent.doFullBackup(mOutputFile, token, mBackupManagerBinder); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2568 | if (!waitUntilOperationComplete(token)) { |
| 2569 | Slog.e(TAG, "Full backup failed on shared storage"); |
| 2570 | } else { |
| 2571 | if (DEBUG) Slog.d(TAG, "Full shared storage backup success"); |
| 2572 | } |
| 2573 | } else { |
| 2574 | Slog.e(TAG, "Could not bind to shared storage backup agent"); |
| 2575 | } |
| 2576 | } catch (NameNotFoundException e) { |
| 2577 | Slog.e(TAG, "Shared storage backup package not found"); |
| 2578 | } finally { |
| 2579 | tearDown(pkg); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2583 | private void finalizeBackup(OutputStream out) { |
| 2584 | try { |
| 2585 | // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes. |
| 2586 | byte[] eof = new byte[512 * 2]; // newly allocated == zero filled |
| 2587 | out.write(eof); |
| 2588 | } catch (IOException e) { |
| 2589 | Slog.w(TAG, "Error attempting to finalize backup stream"); |
| 2590 | } |
| 2591 | } |
| 2592 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2593 | private void writeAppManifest(PackageInfo pkg, File manifestFile, boolean withApk) |
| 2594 | throws IOException { |
| 2595 | // Manifest format. All data are strings ending in LF: |
| 2596 | // BACKUP_MANIFEST_VERSION, currently 1 |
| 2597 | // |
| 2598 | // Version 1: |
| 2599 | // package name |
| 2600 | // package's versionCode |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2601 | // platform versionCode |
| 2602 | // getInstallerPackageName() for this package (maybe empty) |
| 2603 | // boolean: "1" if archive includes .apk; any other string means not |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2604 | // number of signatures == N |
| 2605 | // N*: signature byte array in ascii format per Signature.toCharsString() |
| 2606 | StringBuilder builder = new StringBuilder(4096); |
| 2607 | StringBuilderPrinter printer = new StringBuilderPrinter(builder); |
| 2608 | |
| 2609 | printer.println(Integer.toString(BACKUP_MANIFEST_VERSION)); |
| 2610 | printer.println(pkg.packageName); |
| 2611 | printer.println(Integer.toString(pkg.versionCode)); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2612 | printer.println(Integer.toString(Build.VERSION.SDK_INT)); |
| 2613 | |
| 2614 | String installerName = mPackageManager.getInstallerPackageName(pkg.packageName); |
| 2615 | printer.println((installerName != null) ? installerName : ""); |
| 2616 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2617 | printer.println(withApk ? "1" : "0"); |
| 2618 | if (pkg.signatures == null) { |
| 2619 | printer.println("0"); |
| 2620 | } else { |
| 2621 | printer.println(Integer.toString(pkg.signatures.length)); |
| 2622 | for (Signature sig : pkg.signatures) { |
| 2623 | printer.println(sig.toCharsString()); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | FileOutputStream outstream = new FileOutputStream(manifestFile); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2628 | outstream.write(builder.toString().getBytes()); |
| 2629 | outstream.close(); |
| 2630 | } |
| 2631 | |
| 2632 | private void tearDown(PackageInfo pkg) { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2633 | if (pkg != null) { |
| 2634 | final ApplicationInfo app = pkg.applicationInfo; |
| 2635 | if (app != null) { |
| 2636 | try { |
| 2637 | // unbind and tidy up even on timeout or failure, just in case |
| 2638 | mActivityManager.unbindBackupAgent(app); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2639 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2640 | // 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] | 2641 | if (app.uid != Process.SYSTEM_UID |
| 2642 | && app.uid != Process.PHONE_UID) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2643 | if (MORE_DEBUG) Slog.d(TAG, "Backup complete, killing host process"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2644 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 2645 | } else { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2646 | if (MORE_DEBUG) Slog.d(TAG, "Not killing after restore: " + app.processName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2647 | } |
| 2648 | } catch (RemoteException e) { |
| 2649 | Slog.d(TAG, "Lost app trying to shut down"); |
| 2650 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2651 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | // wrappers for observer use |
| 2656 | void sendStartBackup() { |
| 2657 | if (mObserver != null) { |
| 2658 | try { |
| 2659 | mObserver.onStartBackup(); |
| 2660 | } catch (RemoteException e) { |
| 2661 | Slog.w(TAG, "full backup observer went away: startBackup"); |
| 2662 | mObserver = null; |
| 2663 | } |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | void sendOnBackupPackage(String name) { |
| 2668 | if (mObserver != null) { |
| 2669 | try { |
| 2670 | // TODO: use a more user-friendly name string |
| 2671 | mObserver.onBackupPackage(name); |
| 2672 | } catch (RemoteException e) { |
| 2673 | Slog.w(TAG, "full backup observer went away: backupPackage"); |
| 2674 | mObserver = null; |
| 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | void sendEndBackup() { |
| 2680 | if (mObserver != null) { |
| 2681 | try { |
| 2682 | mObserver.onEndBackup(); |
| 2683 | } catch (RemoteException e) { |
| 2684 | Slog.w(TAG, "full backup observer went away: endBackup"); |
| 2685 | mObserver = null; |
| 2686 | } |
| 2687 | } |
| 2688 | } |
| 2689 | } |
| 2690 | |
| 2691 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2692 | // ----- Full restore from a file/socket ----- |
| 2693 | |
| 2694 | // Description of a file in the restore datastream |
| 2695 | static class FileMetadata { |
| 2696 | String packageName; // name of the owning app |
| 2697 | String installerPackageName; // name of the market-type app that installed the owner |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2698 | int type; // e.g. BackupAgent.TYPE_DIRECTORY |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2699 | String domain; // e.g. FullBackup.DATABASE_TREE_TOKEN |
| 2700 | String path; // subpath within the semantic domain |
| 2701 | long mode; // e.g. 0666 (actually int) |
| 2702 | long mtime; // last mod time, UTC time_t (actually int) |
| 2703 | long size; // bytes of content |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2704 | |
| 2705 | @Override |
| 2706 | public String toString() { |
| 2707 | StringBuilder sb = new StringBuilder(128); |
| 2708 | sb.append("FileMetadata{"); |
| 2709 | sb.append(packageName); sb.append(','); |
| 2710 | sb.append(type); sb.append(','); |
| 2711 | sb.append(domain); sb.append(':'); sb.append(path); sb.append(','); |
| 2712 | sb.append(size); |
| 2713 | sb.append('}'); |
| 2714 | return sb.toString(); |
| 2715 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | enum RestorePolicy { |
| 2719 | IGNORE, |
| 2720 | ACCEPT, |
| 2721 | ACCEPT_IF_APK |
| 2722 | } |
| 2723 | |
| 2724 | class PerformFullRestoreTask implements Runnable { |
| 2725 | ParcelFileDescriptor mInputFile; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2726 | String mCurrentPassword; |
| 2727 | String mDecryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2728 | IFullBackupRestoreObserver mObserver; |
| 2729 | AtomicBoolean mLatchObject; |
| 2730 | IBackupAgent mAgent; |
| 2731 | String mAgentPackage; |
| 2732 | ApplicationInfo mTargetApp; |
| 2733 | ParcelFileDescriptor[] mPipes = null; |
| 2734 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2735 | long mBytes; |
| 2736 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2737 | // possible handling states for a given package in the restore dataset |
| 2738 | final HashMap<String, RestorePolicy> mPackagePolicies |
| 2739 | = new HashMap<String, RestorePolicy>(); |
| 2740 | |
| 2741 | // installer package names for each encountered app, derived from the manifests |
| 2742 | final HashMap<String, String> mPackageInstallers = new HashMap<String, String>(); |
| 2743 | |
| 2744 | // Signatures for a given package found in its manifest file |
| 2745 | final HashMap<String, Signature[]> mManifestSignatures |
| 2746 | = new HashMap<String, Signature[]>(); |
| 2747 | |
| 2748 | // Packages we've already wiped data on when restoring their first file |
| 2749 | final HashSet<String> mClearedPackages = new HashSet<String>(); |
| 2750 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2751 | PerformFullRestoreTask(ParcelFileDescriptor fd, String curPassword, String decryptPassword, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2752 | IFullBackupRestoreObserver observer, AtomicBoolean latch) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2753 | mInputFile = fd; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2754 | mCurrentPassword = curPassword; |
| 2755 | mDecryptPassword = decryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2756 | mObserver = observer; |
| 2757 | mLatchObject = latch; |
| 2758 | mAgent = null; |
| 2759 | mAgentPackage = null; |
| 2760 | mTargetApp = null; |
| 2761 | |
| 2762 | // Which packages we've already wiped data on. We prepopulate this |
| 2763 | // with a whitelist of packages known to be unclearable. |
| 2764 | mClearedPackages.add("android"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2765 | mClearedPackages.add("com.android.providers.settings"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2766 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | class RestoreFileRunnable implements Runnable { |
| 2770 | IBackupAgent mAgent; |
| 2771 | FileMetadata mInfo; |
| 2772 | ParcelFileDescriptor mSocket; |
| 2773 | int mToken; |
| 2774 | |
| 2775 | RestoreFileRunnable(IBackupAgent agent, FileMetadata info, |
| 2776 | ParcelFileDescriptor socket, int token) throws IOException { |
| 2777 | mAgent = agent; |
| 2778 | mInfo = info; |
| 2779 | mToken = token; |
| 2780 | |
| 2781 | // This class is used strictly for process-local binder invocations. The |
| 2782 | // semantics of ParcelFileDescriptor differ in this case; in particular, we |
| 2783 | // do not automatically get a 'dup'ed descriptor that we can can continue |
| 2784 | // to use asynchronously from the caller. So, we make sure to dup it ourselves |
| 2785 | // before proceeding to do the restore. |
| 2786 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2787 | } |
| 2788 | |
| 2789 | @Override |
| 2790 | public void run() { |
| 2791 | try { |
| 2792 | mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type, |
| 2793 | mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime, |
| 2794 | mToken, mBackupManagerBinder); |
| 2795 | } catch (RemoteException e) { |
| 2796 | // never happens; this is used strictly for local binder calls |
| 2797 | } |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | @Override |
| 2802 | public void run() { |
| 2803 | Slog.i(TAG, "--- Performing full-dataset restore ---"); |
| 2804 | sendStartRestore(); |
| 2805 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2806 | // Are we able to restore shared-storage data? |
| 2807 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| 2808 | mPackagePolicies.put("com.android.sharedstoragebackup", RestorePolicy.ACCEPT); |
| 2809 | } |
| 2810 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2811 | FileInputStream rawInStream = null; |
| 2812 | DataInputStream rawDataIn = null; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2813 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2814 | if (hasBackupPassword()) { |
| 2815 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2816 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2817 | return; |
| 2818 | } |
| 2819 | } |
| 2820 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2821 | mBytes = 0; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2822 | byte[] buffer = new byte[32 * 1024]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2823 | rawInStream = new FileInputStream(mInputFile.getFileDescriptor()); |
| 2824 | rawDataIn = new DataInputStream(rawInStream); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2825 | |
| 2826 | // First, parse out the unencrypted/uncompressed header |
| 2827 | boolean compressed = false; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2828 | InputStream preCompressStream = rawInStream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2829 | final InputStream in; |
| 2830 | |
| 2831 | boolean okay = false; |
| 2832 | final int headerLen = BACKUP_FILE_HEADER_MAGIC.length(); |
| 2833 | byte[] streamHeader = new byte[headerLen]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2834 | rawDataIn.readFully(streamHeader); |
| 2835 | byte[] magicBytes = BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8"); |
| 2836 | if (Arrays.equals(magicBytes, streamHeader)) { |
| 2837 | // okay, header looks good. now parse out the rest of the fields. |
| 2838 | String s = readHeaderLine(rawInStream); |
| 2839 | if (Integer.parseInt(s) == BACKUP_FILE_VERSION) { |
| 2840 | // okay, it's a version we recognize |
| 2841 | s = readHeaderLine(rawInStream); |
| 2842 | compressed = (Integer.parseInt(s) != 0); |
| 2843 | s = readHeaderLine(rawInStream); |
| 2844 | if (s.equals("none")) { |
| 2845 | // no more header to parse; we're good to go |
| 2846 | okay = true; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2847 | } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2848 | preCompressStream = decodeAesHeaderAndInitialize(s, rawInStream); |
| 2849 | if (preCompressStream != null) { |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2850 | okay = true; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2851 | } |
| 2852 | } else Slog.w(TAG, "Archive is encrypted but no password given"); |
| 2853 | } else Slog.w(TAG, "Wrong header version: " + s); |
| 2854 | } else Slog.w(TAG, "Didn't read the right header magic"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2855 | |
| 2856 | if (!okay) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2857 | Slog.w(TAG, "Invalid restore data; aborting."); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2858 | return; |
| 2859 | } |
| 2860 | |
| 2861 | // okay, use the right stream layer based on compression |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2862 | in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2863 | |
| 2864 | boolean didRestore; |
| 2865 | do { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2866 | didRestore = restoreOneFile(in, buffer); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2867 | } while (didRestore); |
| 2868 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2869 | 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] | 2870 | } catch (IOException e) { |
| 2871 | Slog.e(TAG, "Unable to read restore input"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2872 | } finally { |
| 2873 | tearDownPipes(); |
| 2874 | tearDownAgent(mTargetApp); |
| 2875 | |
| 2876 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2877 | if (rawDataIn != null) rawDataIn.close(); |
| 2878 | if (rawInStream != null) rawInStream.close(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2879 | mInputFile.close(); |
| 2880 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2881 | Slog.w(TAG, "Close of restore data pipe threw", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2882 | /* nothing we can do about this */ |
| 2883 | } |
| 2884 | synchronized (mCurrentOpLock) { |
| 2885 | mCurrentOperations.clear(); |
| 2886 | } |
| 2887 | synchronized (mLatchObject) { |
| 2888 | mLatchObject.set(true); |
| 2889 | mLatchObject.notifyAll(); |
| 2890 | } |
| 2891 | sendEndRestore(); |
| 2892 | mWakelock.release(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2893 | Slog.d(TAG, "Full restore pass complete."); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2897 | String readHeaderLine(InputStream in) throws IOException { |
| 2898 | int c; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2899 | StringBuilder buffer = new StringBuilder(80); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2900 | while ((c = in.read()) >= 0) { |
| 2901 | if (c == '\n') break; // consume and discard the newlines |
| 2902 | buffer.append((char)c); |
| 2903 | } |
| 2904 | return buffer.toString(); |
| 2905 | } |
| 2906 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2907 | InputStream decodeAesHeaderAndInitialize(String encryptionName, InputStream rawInStream) { |
| 2908 | InputStream result = null; |
| 2909 | try { |
| 2910 | if (encryptionName.equals(ENCRYPTION_ALGORITHM_NAME)) { |
| 2911 | |
| 2912 | String userSaltHex = readHeaderLine(rawInStream); // 5 |
| 2913 | byte[] userSalt = hexToByteArray(userSaltHex); |
| 2914 | |
| 2915 | String ckSaltHex = readHeaderLine(rawInStream); // 6 |
| 2916 | byte[] ckSalt = hexToByteArray(ckSaltHex); |
| 2917 | |
| 2918 | int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7 |
| 2919 | String userIvHex = readHeaderLine(rawInStream); // 8 |
| 2920 | |
| 2921 | String masterKeyBlobHex = readHeaderLine(rawInStream); // 9 |
| 2922 | |
| 2923 | // decrypt the master key blob |
| 2924 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2925 | SecretKey userKey = buildPasswordKey(mDecryptPassword, userSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2926 | rounds); |
| 2927 | byte[] IV = hexToByteArray(userIvHex); |
| 2928 | IvParameterSpec ivSpec = new IvParameterSpec(IV); |
| 2929 | c.init(Cipher.DECRYPT_MODE, |
| 2930 | new SecretKeySpec(userKey.getEncoded(), "AES"), |
| 2931 | ivSpec); |
| 2932 | byte[] mkCipher = hexToByteArray(masterKeyBlobHex); |
| 2933 | byte[] mkBlob = c.doFinal(mkCipher); |
| 2934 | |
| 2935 | // first, the master key IV |
| 2936 | int offset = 0; |
| 2937 | int len = mkBlob[offset++]; |
| 2938 | IV = Arrays.copyOfRange(mkBlob, offset, offset + len); |
| 2939 | offset += len; |
| 2940 | // then the master key itself |
| 2941 | len = mkBlob[offset++]; |
| 2942 | byte[] mk = Arrays.copyOfRange(mkBlob, |
| 2943 | offset, offset + len); |
| 2944 | offset += len; |
| 2945 | // and finally the master key checksum hash |
| 2946 | len = mkBlob[offset++]; |
| 2947 | byte[] mkChecksum = Arrays.copyOfRange(mkBlob, |
| 2948 | offset, offset + len); |
| 2949 | |
| 2950 | // now validate the decrypted master key against the checksum |
| 2951 | byte[] calculatedCk = makeKeyChecksum(mk, ckSalt, rounds); |
| 2952 | if (Arrays.equals(calculatedCk, mkChecksum)) { |
| 2953 | ivSpec = new IvParameterSpec(IV); |
| 2954 | c.init(Cipher.DECRYPT_MODE, |
| 2955 | new SecretKeySpec(mk, "AES"), |
| 2956 | ivSpec); |
| 2957 | // Only if all of the above worked properly will 'result' be assigned |
| 2958 | result = new CipherInputStream(rawInStream, c); |
| 2959 | } else Slog.w(TAG, "Incorrect password"); |
| 2960 | } else Slog.w(TAG, "Unsupported encryption method: " + encryptionName); |
| 2961 | } catch (InvalidAlgorithmParameterException e) { |
| 2962 | Slog.e(TAG, "Needed parameter spec unavailable!", e); |
| 2963 | } catch (BadPaddingException e) { |
| 2964 | // This case frequently occurs when the wrong password is used to decrypt |
| 2965 | // the master key. Use the identical "incorrect password" log text as is |
| 2966 | // used in the checksum failure log in order to avoid providing additional |
| 2967 | // information to an attacker. |
| 2968 | Slog.w(TAG, "Incorrect password"); |
| 2969 | } catch (IllegalBlockSizeException e) { |
| 2970 | Slog.w(TAG, "Invalid block size in master key"); |
| 2971 | } catch (NoSuchAlgorithmException e) { |
| 2972 | Slog.e(TAG, "Needed decryption algorithm unavailable!"); |
| 2973 | } catch (NoSuchPaddingException e) { |
| 2974 | Slog.e(TAG, "Needed padding mechanism unavailable!"); |
| 2975 | } catch (InvalidKeyException e) { |
| 2976 | Slog.w(TAG, "Illegal password; aborting"); |
| 2977 | } catch (NumberFormatException e) { |
| 2978 | Slog.w(TAG, "Can't parse restore data header"); |
| 2979 | } catch (IOException e) { |
| 2980 | Slog.w(TAG, "Can't read input header"); |
| 2981 | } |
| 2982 | |
| 2983 | return result; |
| 2984 | } |
| 2985 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2986 | boolean restoreOneFile(InputStream instream, byte[] buffer) { |
| 2987 | FileMetadata info; |
| 2988 | try { |
| 2989 | info = readTarHeaders(instream); |
| 2990 | if (info != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2991 | if (MORE_DEBUG) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2992 | dumpFileMetadata(info); |
| 2993 | } |
| 2994 | |
| 2995 | final String pkg = info.packageName; |
| 2996 | if (!pkg.equals(mAgentPackage)) { |
| 2997 | // okay, change in package; set up our various |
| 2998 | // bookkeeping if we haven't seen it yet |
| 2999 | if (!mPackagePolicies.containsKey(pkg)) { |
| 3000 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3001 | } |
| 3002 | |
| 3003 | // Clean up the previous agent relationship if necessary, |
| 3004 | // and let the observer know we're considering a new app. |
| 3005 | if (mAgent != null) { |
| 3006 | if (DEBUG) Slog.d(TAG, "Saw new package; tearing down old one"); |
| 3007 | tearDownPipes(); |
| 3008 | tearDownAgent(mTargetApp); |
| 3009 | mTargetApp = null; |
| 3010 | mAgentPackage = null; |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | if (info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 3015 | mPackagePolicies.put(pkg, readAppManifest(info, instream)); |
| 3016 | mPackageInstallers.put(pkg, info.installerPackageName); |
| 3017 | // We've read only the manifest content itself at this point, |
| 3018 | // so consume the footer before looping around to the next |
| 3019 | // input file |
| 3020 | skipTarPadding(info.size, instream); |
| 3021 | sendOnRestorePackage(pkg); |
| 3022 | } else { |
| 3023 | // Non-manifest, so it's actual file data. Is this a package |
| 3024 | // we're ignoring? |
| 3025 | boolean okay = true; |
| 3026 | RestorePolicy policy = mPackagePolicies.get(pkg); |
| 3027 | switch (policy) { |
| 3028 | case IGNORE: |
| 3029 | okay = false; |
| 3030 | break; |
| 3031 | |
| 3032 | case ACCEPT_IF_APK: |
| 3033 | // If we're in accept-if-apk state, then the first file we |
| 3034 | // see MUST be the apk. |
| 3035 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 3036 | if (DEBUG) Slog.d(TAG, "APK file; installing"); |
| 3037 | // Try to install the app. |
| 3038 | String installerName = mPackageInstallers.get(pkg); |
| 3039 | okay = installApk(info, installerName, instream); |
| 3040 | // good to go; promote to ACCEPT |
| 3041 | mPackagePolicies.put(pkg, (okay) |
| 3042 | ? RestorePolicy.ACCEPT |
| 3043 | : RestorePolicy.IGNORE); |
| 3044 | // At this point we've consumed this file entry |
| 3045 | // ourselves, so just strip the tar footer and |
| 3046 | // go on to the next file in the input stream |
| 3047 | skipTarPadding(info.size, instream); |
| 3048 | return true; |
| 3049 | } else { |
| 3050 | // File data before (or without) the apk. We can't |
| 3051 | // handle it coherently in this case so ignore it. |
| 3052 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3053 | okay = false; |
| 3054 | } |
| 3055 | break; |
| 3056 | |
| 3057 | case ACCEPT: |
| 3058 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 3059 | if (DEBUG) Slog.d(TAG, "apk present but ACCEPT"); |
| 3060 | // we can take the data without the apk, so we |
| 3061 | // *want* to do so. skip the apk by declaring this |
| 3062 | // one file not-okay without changing the restore |
| 3063 | // policy for the package. |
| 3064 | okay = false; |
| 3065 | } |
| 3066 | break; |
| 3067 | |
| 3068 | default: |
| 3069 | // Something has gone dreadfully wrong when determining |
| 3070 | // the restore policy from the manifest. Ignore the |
| 3071 | // rest of this package's data. |
| 3072 | Slog.e(TAG, "Invalid policy from manifest"); |
| 3073 | okay = false; |
| 3074 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3075 | break; |
| 3076 | } |
| 3077 | |
| 3078 | // If the policy is satisfied, go ahead and set up to pipe the |
| 3079 | // data to the agent. |
| 3080 | if (DEBUG && okay && mAgent != null) { |
| 3081 | Slog.i(TAG, "Reusing existing agent instance"); |
| 3082 | } |
| 3083 | if (okay && mAgent == null) { |
| 3084 | if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg); |
| 3085 | |
| 3086 | try { |
| 3087 | mTargetApp = mPackageManager.getApplicationInfo(pkg, 0); |
| 3088 | |
| 3089 | // If we haven't sent any data to this app yet, we probably |
| 3090 | // need to clear it first. Check that. |
| 3091 | if (!mClearedPackages.contains(pkg)) { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3092 | // apps with their own backup agents are |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3093 | // responsible for coherently managing a full |
| 3094 | // restore. |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3095 | if (mTargetApp.backupAgentName == null) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3096 | if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore"); |
| 3097 | clearApplicationDataSynchronous(pkg); |
| 3098 | } else { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3099 | if (DEBUG) Slog.d(TAG, "backup agent (" |
| 3100 | + mTargetApp.backupAgentName + ") => no clear"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3101 | } |
| 3102 | mClearedPackages.add(pkg); |
| 3103 | } else { |
| 3104 | if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required"); |
| 3105 | } |
| 3106 | |
| 3107 | // All set; now set up the IPC and launch the agent |
| 3108 | setUpPipes(); |
| 3109 | mAgent = bindToAgentSynchronous(mTargetApp, |
| 3110 | IApplicationThread.BACKUP_MODE_RESTORE_FULL); |
| 3111 | mAgentPackage = pkg; |
| 3112 | } catch (IOException e) { |
| 3113 | // fall through to error handling |
| 3114 | } catch (NameNotFoundException e) { |
| 3115 | // fall through to error handling |
| 3116 | } |
| 3117 | |
| 3118 | if (mAgent == null) { |
| 3119 | if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg); |
| 3120 | okay = false; |
| 3121 | tearDownPipes(); |
| 3122 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | // Sanity check: make sure we never give data to the wrong app. This |
| 3127 | // should never happen but a little paranoia here won't go amiss. |
| 3128 | if (okay && !pkg.equals(mAgentPackage)) { |
| 3129 | Slog.e(TAG, "Restoring data for " + pkg |
| 3130 | + " but agent is for " + mAgentPackage); |
| 3131 | okay = false; |
| 3132 | } |
| 3133 | |
| 3134 | // At this point we have an agent ready to handle the full |
| 3135 | // restore data as well as a pipe for sending data to |
| 3136 | // that agent. Tell the agent to start reading from the |
| 3137 | // pipe. |
| 3138 | if (okay) { |
| 3139 | boolean agentSuccess = true; |
| 3140 | long toCopy = info.size; |
| 3141 | final int token = generateToken(); |
| 3142 | try { |
| 3143 | if (DEBUG) Slog.d(TAG, "Invoking agent to restore file " |
| 3144 | + info.path); |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 3145 | prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL, null); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3146 | // fire up the app's agent listening on the socket. If |
| 3147 | // the agent is running in the system process we can't |
| 3148 | // just invoke it asynchronously, so we provide a thread |
| 3149 | // for it here. |
| 3150 | if (mTargetApp.processName.equals("system")) { |
| 3151 | Slog.d(TAG, "system process agent - spinning a thread"); |
| 3152 | RestoreFileRunnable runner = new RestoreFileRunnable( |
| 3153 | mAgent, info, mPipes[0], token); |
| 3154 | new Thread(runner).start(); |
| 3155 | } else { |
| 3156 | mAgent.doRestoreFile(mPipes[0], info.size, info.type, |
| 3157 | info.domain, info.path, info.mode, info.mtime, |
| 3158 | token, mBackupManagerBinder); |
| 3159 | } |
| 3160 | } catch (IOException e) { |
| 3161 | // couldn't dup the socket for a process-local restore |
| 3162 | Slog.d(TAG, "Couldn't establish restore"); |
| 3163 | agentSuccess = false; |
| 3164 | okay = false; |
| 3165 | } catch (RemoteException e) { |
| 3166 | // whoops, remote agent went away. We'll eat the content |
| 3167 | // ourselves, then, and not copy it over. |
| 3168 | Slog.e(TAG, "Agent crashed during full restore"); |
| 3169 | agentSuccess = false; |
| 3170 | okay = false; |
| 3171 | } |
| 3172 | |
| 3173 | // Copy over the data if the agent is still good |
| 3174 | if (okay) { |
| 3175 | boolean pipeOkay = true; |
| 3176 | FileOutputStream pipe = new FileOutputStream( |
| 3177 | mPipes[1].getFileDescriptor()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3178 | while (toCopy > 0) { |
| 3179 | int toRead = (toCopy > buffer.length) |
| 3180 | ? buffer.length : (int)toCopy; |
| 3181 | int nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3182 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3183 | if (nRead <= 0) break; |
| 3184 | toCopy -= nRead; |
| 3185 | |
| 3186 | // send it to the output pipe as long as things |
| 3187 | // are still good |
| 3188 | if (pipeOkay) { |
| 3189 | try { |
| 3190 | pipe.write(buffer, 0, nRead); |
| 3191 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3192 | Slog.e(TAG, "Failed to write to restore pipe", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3193 | pipeOkay = false; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | // done sending that file! Now we just need to consume |
| 3199 | // the delta from info.size to the end of block. |
| 3200 | skipTarPadding(info.size, instream); |
| 3201 | |
| 3202 | // and now that we've sent it all, wait for the remote |
| 3203 | // side to acknowledge receipt |
| 3204 | agentSuccess = waitUntilOperationComplete(token); |
| 3205 | } |
| 3206 | |
| 3207 | // okay, if the remote end failed at any point, deal with |
| 3208 | // it by ignoring the rest of the restore on it |
| 3209 | if (!agentSuccess) { |
| 3210 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 3211 | tearDownPipes(); |
| 3212 | tearDownAgent(mTargetApp); |
| 3213 | mAgent = null; |
| 3214 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | // Problems setting up the agent communication, or an already- |
| 3219 | // ignored package: skip to the next tar stream entry by |
| 3220 | // reading and discarding this file. |
| 3221 | if (!okay) { |
| 3222 | if (DEBUG) Slog.d(TAG, "[discarding file content]"); |
| 3223 | long bytesToConsume = (info.size + 511) & ~511; |
| 3224 | while (bytesToConsume > 0) { |
| 3225 | int toRead = (bytesToConsume > buffer.length) |
| 3226 | ? buffer.length : (int)bytesToConsume; |
| 3227 | long nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3228 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3229 | if (nRead <= 0) break; |
| 3230 | bytesToConsume -= nRead; |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | } catch (IOException e) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3236 | if (DEBUG) Slog.w(TAG, "io exception on restore socket read", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3237 | // treat as EOF |
| 3238 | info = null; |
| 3239 | } |
| 3240 | |
| 3241 | return (info != null); |
| 3242 | } |
| 3243 | |
| 3244 | void setUpPipes() throws IOException { |
| 3245 | mPipes = ParcelFileDescriptor.createPipe(); |
| 3246 | } |
| 3247 | |
| 3248 | void tearDownPipes() { |
| 3249 | if (mPipes != null) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3250 | try { |
| 3251 | mPipes[0].close(); |
| 3252 | mPipes[0] = null; |
| 3253 | mPipes[1].close(); |
| 3254 | mPipes[1] = null; |
| 3255 | } catch (IOException e) { |
| 3256 | Slog.w(TAG, "Couldn't close agent pipes", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3257 | } |
| 3258 | mPipes = null; |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | void tearDownAgent(ApplicationInfo app) { |
| 3263 | if (mAgent != null) { |
| 3264 | try { |
| 3265 | // unbind and tidy up even on timeout or failure, just in case |
| 3266 | mActivityManager.unbindBackupAgent(app); |
| 3267 | |
| 3268 | // The agent was running with a stub Application object, so shut it down. |
| 3269 | // !!! We hardcode the confirmation UI's package name here rather than use a |
| 3270 | // manifest flag! TODO something less direct. |
| 3271 | if (app.uid != Process.SYSTEM_UID |
| 3272 | && !app.packageName.equals("com.android.backupconfirm")) { |
| 3273 | if (DEBUG) Slog.d(TAG, "Killing host process"); |
| 3274 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 3275 | } else { |
| 3276 | if (DEBUG) Slog.d(TAG, "Not killing after full restore"); |
| 3277 | } |
| 3278 | } catch (RemoteException e) { |
| 3279 | Slog.d(TAG, "Lost app trying to shut down"); |
| 3280 | } |
| 3281 | mAgent = null; |
| 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | class RestoreInstallObserver extends IPackageInstallObserver.Stub { |
| 3286 | final AtomicBoolean mDone = new AtomicBoolean(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3287 | String mPackageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3288 | int mResult; |
| 3289 | |
| 3290 | public void reset() { |
| 3291 | synchronized (mDone) { |
| 3292 | mDone.set(false); |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | public void waitForCompletion() { |
| 3297 | synchronized (mDone) { |
| 3298 | while (mDone.get() == false) { |
| 3299 | try { |
| 3300 | mDone.wait(); |
| 3301 | } catch (InterruptedException e) { } |
| 3302 | } |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | int getResult() { |
| 3307 | return mResult; |
| 3308 | } |
| 3309 | |
| 3310 | @Override |
| 3311 | public void packageInstalled(String packageName, int returnCode) |
| 3312 | throws RemoteException { |
| 3313 | synchronized (mDone) { |
| 3314 | mResult = returnCode; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3315 | mPackageName = packageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3316 | mDone.set(true); |
| 3317 | mDone.notifyAll(); |
| 3318 | } |
| 3319 | } |
| 3320 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3321 | |
| 3322 | class RestoreDeleteObserver extends IPackageDeleteObserver.Stub { |
| 3323 | final AtomicBoolean mDone = new AtomicBoolean(); |
| 3324 | int mResult; |
| 3325 | |
| 3326 | public void reset() { |
| 3327 | synchronized (mDone) { |
| 3328 | mDone.set(false); |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | public void waitForCompletion() { |
| 3333 | synchronized (mDone) { |
| 3334 | while (mDone.get() == false) { |
| 3335 | try { |
| 3336 | mDone.wait(); |
| 3337 | } catch (InterruptedException e) { } |
| 3338 | } |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | @Override |
| 3343 | public void packageDeleted(String packageName, int returnCode) throws RemoteException { |
| 3344 | synchronized (mDone) { |
| 3345 | mResult = returnCode; |
| 3346 | mDone.set(true); |
| 3347 | mDone.notifyAll(); |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3352 | final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3353 | final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3354 | |
| 3355 | boolean installApk(FileMetadata info, String installerPackage, InputStream instream) { |
| 3356 | boolean okay = true; |
| 3357 | |
| 3358 | if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName); |
| 3359 | |
| 3360 | // The file content is an .apk file. Copy it out to a staging location and |
| 3361 | // attempt to install it. |
| 3362 | File apkFile = new File(mDataDir, info.packageName); |
| 3363 | try { |
| 3364 | FileOutputStream apkStream = new FileOutputStream(apkFile); |
| 3365 | byte[] buffer = new byte[32 * 1024]; |
| 3366 | long size = info.size; |
| 3367 | while (size > 0) { |
| 3368 | long toRead = (buffer.length < size) ? buffer.length : size; |
| 3369 | int didRead = instream.read(buffer, 0, (int)toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3370 | if (didRead >= 0) mBytes += didRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3371 | apkStream.write(buffer, 0, didRead); |
| 3372 | size -= didRead; |
| 3373 | } |
| 3374 | apkStream.close(); |
| 3375 | |
| 3376 | // make sure the installer can read it |
| 3377 | apkFile.setReadable(true, false); |
| 3378 | |
| 3379 | // Now install it |
| 3380 | Uri packageUri = Uri.fromFile(apkFile); |
| 3381 | mInstallObserver.reset(); |
| 3382 | mPackageManager.installPackage(packageUri, mInstallObserver, |
| 3383 | PackageManager.INSTALL_REPLACE_EXISTING, installerPackage); |
| 3384 | mInstallObserver.waitForCompletion(); |
| 3385 | |
| 3386 | if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) { |
| 3387 | // The only time we continue to accept install of data even if the |
| 3388 | // apk install failed is if we had already determined that we could |
| 3389 | // accept the data regardless. |
| 3390 | if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) { |
| 3391 | okay = false; |
| 3392 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3393 | } else { |
| 3394 | // Okay, the install succeeded. Make sure it was the right app. |
| 3395 | boolean uninstall = false; |
| 3396 | if (!mInstallObserver.mPackageName.equals(info.packageName)) { |
| 3397 | Slog.w(TAG, "Restore stream claimed to include apk for " |
| 3398 | + info.packageName + " but apk was really " |
| 3399 | + mInstallObserver.mPackageName); |
| 3400 | // delete the package we just put in place; it might be fraudulent |
| 3401 | okay = false; |
| 3402 | uninstall = true; |
| 3403 | } else { |
| 3404 | try { |
| 3405 | PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName, |
| 3406 | PackageManager.GET_SIGNATURES); |
| 3407 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 3408 | Slog.w(TAG, "Restore stream contains apk of package " |
| 3409 | + info.packageName + " but it disallows backup/restore"); |
| 3410 | okay = false; |
| 3411 | } else { |
| 3412 | // So far so good -- do the signatures match the manifest? |
| 3413 | Signature[] sigs = mManifestSignatures.get(info.packageName); |
| 3414 | if (!signaturesMatch(sigs, pkg)) { |
| 3415 | Slog.w(TAG, "Installed app " + info.packageName |
| 3416 | + " signatures do not match restore manifest"); |
| 3417 | okay = false; |
| 3418 | uninstall = true; |
| 3419 | } |
| 3420 | } |
| 3421 | } catch (NameNotFoundException e) { |
| 3422 | Slog.w(TAG, "Install of package " + info.packageName |
| 3423 | + " succeeded but now not found"); |
| 3424 | okay = false; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | // If we're not okay at this point, we need to delete the package |
| 3429 | // that we just installed. |
| 3430 | if (uninstall) { |
| 3431 | mDeleteObserver.reset(); |
| 3432 | mPackageManager.deletePackage(mInstallObserver.mPackageName, |
| 3433 | mDeleteObserver, 0); |
| 3434 | mDeleteObserver.waitForCompletion(); |
| 3435 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3436 | } |
| 3437 | } catch (IOException e) { |
| 3438 | Slog.e(TAG, "Unable to transcribe restored apk for install"); |
| 3439 | okay = false; |
| 3440 | } finally { |
| 3441 | apkFile.delete(); |
| 3442 | } |
| 3443 | |
| 3444 | return okay; |
| 3445 | } |
| 3446 | |
| 3447 | // Given an actual file content size, consume the post-content padding mandated |
| 3448 | // by the tar format. |
| 3449 | void skipTarPadding(long size, InputStream instream) throws IOException { |
| 3450 | long partial = (size + 512) % 512; |
| 3451 | if (partial > 0) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3452 | final int needed = 512 - (int)partial; |
| 3453 | byte[] buffer = new byte[needed]; |
| 3454 | if (readExactly(instream, buffer, 0, needed) == needed) { |
| 3455 | mBytes += needed; |
| 3456 | } else throw new IOException("Unexpected EOF in padding"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3457 | } |
| 3458 | } |
| 3459 | |
| 3460 | // Returns a policy constant; takes a buffer arg to reduce memory churn |
| 3461 | RestorePolicy readAppManifest(FileMetadata info, InputStream instream) |
| 3462 | throws IOException { |
| 3463 | // Fail on suspiciously large manifest files |
| 3464 | if (info.size > 64 * 1024) { |
| 3465 | throw new IOException("Restore manifest too big; corrupt? size=" + info.size); |
| 3466 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3467 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3468 | byte[] buffer = new byte[(int) info.size]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3469 | if (readExactly(instream, buffer, 0, (int)info.size) == info.size) { |
| 3470 | mBytes += info.size; |
| 3471 | } else throw new IOException("Unexpected EOF in manifest"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3472 | |
| 3473 | RestorePolicy policy = RestorePolicy.IGNORE; |
| 3474 | String[] str = new String[1]; |
| 3475 | int offset = 0; |
| 3476 | |
| 3477 | try { |
| 3478 | offset = extractLine(buffer, offset, str); |
| 3479 | int version = Integer.parseInt(str[0]); |
| 3480 | if (version == BACKUP_MANIFEST_VERSION) { |
| 3481 | offset = extractLine(buffer, offset, str); |
| 3482 | String manifestPackage = str[0]; |
| 3483 | // TODO: handle <original-package> |
| 3484 | if (manifestPackage.equals(info.packageName)) { |
| 3485 | offset = extractLine(buffer, offset, str); |
| 3486 | version = Integer.parseInt(str[0]); // app version |
| 3487 | offset = extractLine(buffer, offset, str); |
| 3488 | int platformVersion = Integer.parseInt(str[0]); |
| 3489 | offset = extractLine(buffer, offset, str); |
| 3490 | info.installerPackageName = (str[0].length() > 0) ? str[0] : null; |
| 3491 | offset = extractLine(buffer, offset, str); |
| 3492 | boolean hasApk = str[0].equals("1"); |
| 3493 | offset = extractLine(buffer, offset, str); |
| 3494 | int numSigs = Integer.parseInt(str[0]); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3495 | if (numSigs > 0) { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3496 | Signature[] sigs = new Signature[numSigs]; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3497 | for (int i = 0; i < numSigs; i++) { |
| 3498 | offset = extractLine(buffer, offset, str); |
| 3499 | sigs[i] = new Signature(str[0]); |
| 3500 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3501 | mManifestSignatures.put(info.packageName, sigs); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3502 | |
| 3503 | // Okay, got the manifest info we need... |
| 3504 | try { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3505 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3506 | info.packageName, PackageManager.GET_SIGNATURES); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3507 | // Fall through to IGNORE if the app explicitly disallows backup |
| 3508 | final int flags = pkgInfo.applicationInfo.flags; |
| 3509 | if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) { |
| 3510 | // Verify signatures against any installed version; if they |
| 3511 | // don't match, then we fall though and ignore the data. The |
| 3512 | // signatureMatch() method explicitly ignores the signature |
| 3513 | // check for packages installed on the system partition, because |
| 3514 | // such packages are signed with the platform cert instead of |
| 3515 | // the app developer's cert, so they're different on every |
| 3516 | // device. |
| 3517 | if (signaturesMatch(sigs, pkgInfo)) { |
| 3518 | if (pkgInfo.versionCode >= version) { |
| 3519 | Slog.i(TAG, "Sig + version match; taking data"); |
| 3520 | policy = RestorePolicy.ACCEPT; |
| 3521 | } else { |
| 3522 | // The data is from a newer version of the app than |
| 3523 | // is presently installed. That means we can only |
| 3524 | // use it if the matching apk is also supplied. |
| 3525 | Slog.d(TAG, "Data version " + version |
| 3526 | + " is newer than installed version " |
| 3527 | + pkgInfo.versionCode + " - requiring apk"); |
| 3528 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3529 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3530 | } else { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3531 | Slog.w(TAG, "Restore manifest signatures do not match " |
| 3532 | + "installed application for " + info.packageName); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3533 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3534 | } else { |
| 3535 | if (DEBUG) Slog.i(TAG, "Restore manifest from " |
| 3536 | + info.packageName + " but allowBackup=false"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3537 | } |
| 3538 | } catch (NameNotFoundException e) { |
| 3539 | // Okay, the target app isn't installed. We can process |
| 3540 | // the restore properly only if the dataset provides the |
| 3541 | // apk file and we can successfully install it. |
| 3542 | if (DEBUG) Slog.i(TAG, "Package " + info.packageName |
| 3543 | + " not installed; requiring apk in dataset"); |
| 3544 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3545 | } |
| 3546 | |
| 3547 | if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) { |
| 3548 | Slog.i(TAG, "Cannot restore package " + info.packageName |
| 3549 | + " without the matching .apk"); |
| 3550 | } |
| 3551 | } else { |
| 3552 | Slog.i(TAG, "Missing signature on backed-up package " |
| 3553 | + info.packageName); |
| 3554 | } |
| 3555 | } else { |
| 3556 | Slog.i(TAG, "Expected package " + info.packageName |
| 3557 | + " but restore manifest claims " + manifestPackage); |
| 3558 | } |
| 3559 | } else { |
| 3560 | Slog.i(TAG, "Unknown restore manifest version " + version |
| 3561 | + " for package " + info.packageName); |
| 3562 | } |
| 3563 | } catch (NumberFormatException e) { |
| 3564 | Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName); |
| Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 3565 | } catch (IllegalArgumentException e) { |
| 3566 | Slog.w(TAG, e.getMessage()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | return policy; |
| 3570 | } |
| 3571 | |
| 3572 | // Builds a line from a byte buffer starting at 'offset', and returns |
| 3573 | // the index of the next unconsumed data in the buffer. |
| 3574 | int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException { |
| 3575 | final int end = buffer.length; |
| 3576 | if (offset >= end) throw new IOException("Incomplete data"); |
| 3577 | |
| 3578 | int pos; |
| 3579 | for (pos = offset; pos < end; pos++) { |
| 3580 | byte c = buffer[pos]; |
| 3581 | // at LF we declare end of line, and return the next char as the |
| 3582 | // starting point for the next time through |
| 3583 | if (c == '\n') { |
| 3584 | break; |
| 3585 | } |
| 3586 | } |
| 3587 | outStr[0] = new String(buffer, offset, pos - offset); |
| 3588 | pos++; // may be pointing an extra byte past the end but that's okay |
| 3589 | return pos; |
| 3590 | } |
| 3591 | |
| 3592 | void dumpFileMetadata(FileMetadata info) { |
| 3593 | if (DEBUG) { |
| 3594 | StringBuilder b = new StringBuilder(128); |
| 3595 | |
| 3596 | // mode string |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3597 | b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-'); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3598 | b.append(((info.mode & 0400) != 0) ? 'r' : '-'); |
| 3599 | b.append(((info.mode & 0200) != 0) ? 'w' : '-'); |
| 3600 | b.append(((info.mode & 0100) != 0) ? 'x' : '-'); |
| 3601 | b.append(((info.mode & 0040) != 0) ? 'r' : '-'); |
| 3602 | b.append(((info.mode & 0020) != 0) ? 'w' : '-'); |
| 3603 | b.append(((info.mode & 0010) != 0) ? 'x' : '-'); |
| 3604 | b.append(((info.mode & 0004) != 0) ? 'r' : '-'); |
| 3605 | b.append(((info.mode & 0002) != 0) ? 'w' : '-'); |
| 3606 | b.append(((info.mode & 0001) != 0) ? 'x' : '-'); |
| 3607 | b.append(String.format(" %9d ", info.size)); |
| 3608 | |
| 3609 | Date stamp = new Date(info.mtime); |
| 3610 | b.append(new SimpleDateFormat("MMM dd kk:mm:ss ").format(stamp)); |
| 3611 | |
| 3612 | b.append(info.packageName); |
| 3613 | b.append(" :: "); |
| 3614 | b.append(info.domain); |
| 3615 | b.append(" :: "); |
| 3616 | b.append(info.path); |
| 3617 | |
| 3618 | Slog.i(TAG, b.toString()); |
| 3619 | } |
| 3620 | } |
| 3621 | // Consume a tar file header block [sequence] and accumulate the relevant metadata |
| 3622 | FileMetadata readTarHeaders(InputStream instream) throws IOException { |
| 3623 | byte[] block = new byte[512]; |
| 3624 | FileMetadata info = null; |
| 3625 | |
| 3626 | boolean gotHeader = readTarHeader(instream, block); |
| 3627 | if (gotHeader) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3628 | try { |
| 3629 | // okay, presume we're okay, and extract the various metadata |
| 3630 | info = new FileMetadata(); |
| 3631 | info.size = extractRadix(block, 124, 12, 8); |
| 3632 | info.mtime = extractRadix(block, 136, 12, 8); |
| 3633 | info.mode = extractRadix(block, 100, 8, 8); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3634 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3635 | info.path = extractString(block, 345, 155); // prefix |
| 3636 | String path = extractString(block, 0, 100); |
| 3637 | if (path.length() > 0) { |
| 3638 | if (info.path.length() > 0) info.path += '/'; |
| 3639 | info.path += path; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3640 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3641 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3642 | // tar link indicator field: 1 byte at offset 156 in the header. |
| 3643 | int typeChar = block[156]; |
| 3644 | if (typeChar == 'x') { |
| 3645 | // pax extended header, so we need to read that |
| 3646 | gotHeader = readPaxExtendedHeader(instream, info); |
| 3647 | if (gotHeader) { |
| 3648 | // and after a pax extended header comes another real header -- read |
| 3649 | // that to find the real file type |
| 3650 | gotHeader = readTarHeader(instream, block); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3651 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3652 | if (!gotHeader) throw new IOException("Bad or missing pax header"); |
| 3653 | |
| 3654 | typeChar = block[156]; |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3655 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3656 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3657 | switch (typeChar) { |
| 3658 | case '0': info.type = BackupAgent.TYPE_FILE; break; |
| 3659 | case '5': { |
| 3660 | info.type = BackupAgent.TYPE_DIRECTORY; |
| 3661 | if (info.size != 0) { |
| 3662 | Slog.w(TAG, "Directory entry with nonzero size in header"); |
| 3663 | info.size = 0; |
| 3664 | } |
| 3665 | break; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3666 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3667 | case 0: { |
| 3668 | // presume EOF |
| 3669 | if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info); |
| 3670 | return null; |
| 3671 | } |
| 3672 | default: { |
| 3673 | Slog.e(TAG, "Unknown tar entity type: " + typeChar); |
| 3674 | throw new IOException("Unknown entity type " + typeChar); |
| 3675 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3676 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3677 | |
| 3678 | // Parse out the path |
| 3679 | // |
| 3680 | // first: apps/shared/unrecognized |
| 3681 | if (FullBackup.SHARED_PREFIX.regionMatches(0, |
| 3682 | info.path, 0, FullBackup.SHARED_PREFIX.length())) { |
| 3683 | // File in shared storage. !!! TODO: implement this. |
| 3684 | info.path = info.path.substring(FullBackup.SHARED_PREFIX.length()); |
| 3685 | info.packageName = "com.android.sharedstoragebackup"; |
| 3686 | info.domain = FullBackup.SHARED_STORAGE_TOKEN; |
| 3687 | if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path); |
| 3688 | } else if (FullBackup.APPS_PREFIX.regionMatches(0, |
| 3689 | info.path, 0, FullBackup.APPS_PREFIX.length())) { |
| 3690 | // App content! Parse out the package name and domain |
| 3691 | |
| 3692 | // strip the apps/ prefix |
| 3693 | info.path = info.path.substring(FullBackup.APPS_PREFIX.length()); |
| 3694 | |
| 3695 | // extract the package name |
| 3696 | int slash = info.path.indexOf('/'); |
| 3697 | if (slash < 0) throw new IOException("Illegal semantic path in " + info.path); |
| 3698 | info.packageName = info.path.substring(0, slash); |
| 3699 | info.path = info.path.substring(slash+1); |
| 3700 | |
| 3701 | // if it's a manifest we're done, otherwise parse out the domains |
| 3702 | if (!info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 3703 | slash = info.path.indexOf('/'); |
| 3704 | if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path); |
| 3705 | info.domain = info.path.substring(0, slash); |
| 3706 | // validate that it's one of the domains we understand |
| 3707 | if (!info.domain.equals(FullBackup.APK_TREE_TOKEN) |
| 3708 | && !info.domain.equals(FullBackup.DATA_TREE_TOKEN) |
| 3709 | && !info.domain.equals(FullBackup.DATABASE_TREE_TOKEN) |
| 3710 | && !info.domain.equals(FullBackup.ROOT_TREE_TOKEN) |
| 3711 | && !info.domain.equals(FullBackup.SHAREDPREFS_TREE_TOKEN) |
| 3712 | && !info.domain.equals(FullBackup.OBB_TREE_TOKEN) |
| 3713 | && !info.domain.equals(FullBackup.CACHE_TREE_TOKEN)) { |
| 3714 | throw new IOException("Unrecognized domain " + info.domain); |
| 3715 | } |
| 3716 | |
| 3717 | info.path = info.path.substring(slash + 1); |
| 3718 | } |
| 3719 | } |
| 3720 | } catch (IOException e) { |
| 3721 | if (DEBUG) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3722 | Slog.e(TAG, "Parse error in header: " + e.getMessage()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3723 | HEXLOG(block); |
| 3724 | } |
| 3725 | throw e; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3726 | } |
| 3727 | } |
| 3728 | return info; |
| 3729 | } |
| 3730 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3731 | private void HEXLOG(byte[] block) { |
| 3732 | int offset = 0; |
| 3733 | int todo = block.length; |
| 3734 | StringBuilder buf = new StringBuilder(64); |
| 3735 | while (todo > 0) { |
| 3736 | buf.append(String.format("%04x ", offset)); |
| 3737 | int numThisLine = (todo > 16) ? 16 : todo; |
| 3738 | for (int i = 0; i < numThisLine; i++) { |
| 3739 | buf.append(String.format("%02x ", block[offset+i])); |
| 3740 | } |
| 3741 | Slog.i("hexdump", buf.toString()); |
| 3742 | buf.setLength(0); |
| 3743 | todo -= numThisLine; |
| 3744 | offset += numThisLine; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3745 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3748 | // Read exactly the given number of bytes into a buffer at the stated offset. |
| 3749 | // Returns false if EOF is encountered before the requested number of bytes |
| 3750 | // could be read. |
| 3751 | int readExactly(InputStream in, byte[] buffer, int offset, int size) |
| 3752 | throws IOException { |
| 3753 | if (size <= 0) throw new IllegalArgumentException("size must be > 0"); |
| 3754 | |
| 3755 | int soFar = 0; |
| 3756 | while (soFar < size) { |
| 3757 | int nRead = in.read(buffer, offset + soFar, size - soFar); |
| 3758 | if (nRead <= 0) { |
| 3759 | if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar); |
| 3760 | break; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3761 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3762 | soFar += nRead; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3763 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3764 | return soFar; |
| 3765 | } |
| 3766 | |
| 3767 | boolean readTarHeader(InputStream instream, byte[] block) throws IOException { |
| 3768 | final int got = readExactly(instream, block, 0, 512); |
| 3769 | if (got == 0) return false; // Clean EOF |
| 3770 | if (got < 512) throw new IOException("Unable to read full block header"); |
| 3771 | mBytes += 512; |
| 3772 | return true; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | // overwrites 'info' fields based on the pax extended header |
| 3776 | boolean readPaxExtendedHeader(InputStream instream, FileMetadata info) |
| 3777 | throws IOException { |
| 3778 | // We should never see a pax extended header larger than this |
| 3779 | if (info.size > 32*1024) { |
| 3780 | Slog.w(TAG, "Suspiciously large pax header size " + info.size |
| 3781 | + " - aborting"); |
| 3782 | throw new IOException("Sanity failure: pax header size " + info.size); |
| 3783 | } |
| 3784 | |
| 3785 | // read whole blocks, not just the content size |
| 3786 | int numBlocks = (int)((info.size + 511) >> 9); |
| 3787 | byte[] data = new byte[numBlocks * 512]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3788 | if (readExactly(instream, data, 0, data.length) < data.length) { |
| 3789 | throw new IOException("Unable to read full pax header"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3790 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3791 | mBytes += data.length; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3792 | |
| 3793 | final int contentSize = (int) info.size; |
| 3794 | int offset = 0; |
| 3795 | do { |
| 3796 | // extract the line at 'offset' |
| 3797 | int eol = offset+1; |
| 3798 | while (eol < contentSize && data[eol] != ' ') eol++; |
| 3799 | if (eol >= contentSize) { |
| 3800 | // error: we just hit EOD looking for the end of the size field |
| 3801 | throw new IOException("Invalid pax data"); |
| 3802 | } |
| 3803 | // eol points to the space between the count and the key |
| 3804 | int linelen = (int) extractRadix(data, offset, eol - offset, 10); |
| 3805 | int key = eol + 1; // start of key=value |
| 3806 | eol = offset + linelen - 1; // trailing LF |
| 3807 | int value; |
| 3808 | for (value = key+1; data[value] != '=' && value <= eol; value++); |
| 3809 | if (value > eol) { |
| 3810 | throw new IOException("Invalid pax declaration"); |
| 3811 | } |
| 3812 | |
| 3813 | // pax requires that key/value strings be in UTF-8 |
| 3814 | String keyStr = new String(data, key, value-key, "UTF-8"); |
| 3815 | // -1 to strip the trailing LF |
| 3816 | String valStr = new String(data, value+1, eol-value-1, "UTF-8"); |
| 3817 | |
| 3818 | if ("path".equals(keyStr)) { |
| 3819 | info.path = valStr; |
| 3820 | } else if ("size".equals(keyStr)) { |
| 3821 | info.size = Long.parseLong(valStr); |
| 3822 | } else { |
| 3823 | if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key); |
| 3824 | } |
| 3825 | |
| 3826 | offset += linelen; |
| 3827 | } while (offset < contentSize); |
| 3828 | |
| 3829 | return true; |
| 3830 | } |
| 3831 | |
| 3832 | long extractRadix(byte[] data, int offset, int maxChars, int radix) |
| 3833 | throws IOException { |
| 3834 | long value = 0; |
| 3835 | final int end = offset + maxChars; |
| 3836 | for (int i = offset; i < end; i++) { |
| 3837 | final byte b = data[i]; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 3838 | // Numeric fields in tar can terminate with either NUL or SPC |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3839 | if (b == 0 || b == ' ') break; |
| 3840 | if (b < '0' || b > ('0' + radix - 1)) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3841 | throw new IOException("Invalid number in header: '" + (char)b + "' for radix " + radix); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3842 | } |
| 3843 | value = radix * value + (b - '0'); |
| 3844 | } |
| 3845 | return value; |
| 3846 | } |
| 3847 | |
| 3848 | String extractString(byte[] data, int offset, int maxChars) throws IOException { |
| 3849 | final int end = offset + maxChars; |
| 3850 | int eos = offset; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 3851 | // tar string fields terminate early with a NUL |
| 3852 | while (eos < end && data[eos] != 0) eos++; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3853 | return new String(data, offset, eos-offset, "US-ASCII"); |
| 3854 | } |
| 3855 | |
| 3856 | void sendStartRestore() { |
| 3857 | if (mObserver != null) { |
| 3858 | try { |
| 3859 | mObserver.onStartRestore(); |
| 3860 | } catch (RemoteException e) { |
| 3861 | Slog.w(TAG, "full restore observer went away: startRestore"); |
| 3862 | mObserver = null; |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | void sendOnRestorePackage(String name) { |
| 3868 | if (mObserver != null) { |
| 3869 | try { |
| 3870 | // TODO: use a more user-friendly name string |
| 3871 | mObserver.onRestorePackage(name); |
| 3872 | } catch (RemoteException e) { |
| 3873 | Slog.w(TAG, "full restore observer went away: restorePackage"); |
| 3874 | mObserver = null; |
| 3875 | } |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | void sendEndRestore() { |
| 3880 | if (mObserver != null) { |
| 3881 | try { |
| 3882 | mObserver.onEndRestore(); |
| 3883 | } catch (RemoteException e) { |
| 3884 | Slog.w(TAG, "full restore observer went away: endRestore"); |
| 3885 | mObserver = null; |
| 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | } |
| 3890 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3891 | // ----- Restore handling ----- |
| 3892 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3893 | private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) { |
| 3894 | // If the target resides on the system partition, we allow it to restore |
| 3895 | // data from the like-named package in a restore set even if the signatures |
| 3896 | // do not match. (Unlike general applications, those flashed to the system |
| 3897 | // partition will be signed with the device's platform certificate, so on |
| 3898 | // different phones the same system app will have different signatures.) |
| 3899 | if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3900 | if (DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check"); |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3901 | return true; |
| 3902 | } |
| 3903 | |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 3904 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 3905 | // !!! TODO: is this the right policy? |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3906 | Signature[] deviceSigs = target.signatures; |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 3907 | if (MORE_DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 3908 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 3909 | if ((storedSigs == null || storedSigs.length == 0) |
| 3910 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 3911 | return true; |
| 3912 | } |
| 3913 | if (storedSigs == null || deviceSigs == null) { |
| 3914 | return false; |
| 3915 | } |
| 3916 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 3917 | // !!! TODO: this demands that every stored signature match one |
| 3918 | // that is present on device, and does not demand the converse. |
| 3919 | // Is this this right policy? |
| 3920 | int nStored = storedSigs.length; |
| 3921 | int nDevice = deviceSigs.length; |
| 3922 | |
| 3923 | for (int i=0; i < nStored; i++) { |
| 3924 | boolean match = false; |
| 3925 | for (int j=0; j < nDevice; j++) { |
| 3926 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 3927 | match = true; |
| 3928 | break; |
| 3929 | } |
| 3930 | } |
| 3931 | if (!match) { |
| 3932 | return false; |
| 3933 | } |
| 3934 | } |
| 3935 | return true; |
| 3936 | } |
| 3937 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 3938 | enum RestoreState { |
| 3939 | INITIAL, |
| 3940 | DOWNLOAD_DATA, |
| 3941 | PM_METADATA, |
| 3942 | RUNNING_QUEUE, |
| 3943 | FINAL |
| 3944 | } |
| 3945 | |
| 3946 | class PerformRestoreTask implements BackupRestoreTask { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3947 | private IBackupTransport mTransport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3948 | private IRestoreObserver mObserver; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 3949 | private long mToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3950 | private PackageInfo mTargetPackage; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3951 | private File mStateDir; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3952 | private int mPmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3953 | private boolean mNeedFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3954 | private HashSet<String> mFilterSet; |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 3955 | private long mStartRealtime; |
| 3956 | private PackageManagerBackupAgent mPmAgent; |
| 3957 | private List<PackageInfo> mAgentPackages; |
| 3958 | private ArrayList<PackageInfo> mRestorePackages; |
| 3959 | private RestoreState mCurrentState; |
| 3960 | private int mCount; |
| 3961 | private boolean mFinished; |
| 3962 | private int mStatus; |
| 3963 | private File mBackupDataName; |
| 3964 | private File mNewStateName; |
| 3965 | private File mSavedStateName; |
| 3966 | private ParcelFileDescriptor mBackupData; |
| 3967 | private ParcelFileDescriptor mNewState; |
| 3968 | private PackageInfo mCurrentPackage; |
| 3969 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3970 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 3971 | class RestoreRequest { |
| 3972 | public PackageInfo app; |
| 3973 | public int storedAppVersion; |
| 3974 | |
| 3975 | RestoreRequest(PackageInfo _app, int _version) { |
| 3976 | app = _app; |
| 3977 | storedAppVersion = _version; |
| 3978 | } |
| 3979 | } |
| 3980 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3981 | PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3982 | long restoreSetToken, PackageInfo targetPackage, int pmToken, |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3983 | boolean needFullBackup, String[] filterSet) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 3984 | mCurrentState = RestoreState.INITIAL; |
| 3985 | mFinished = false; |
| 3986 | mPmAgent = null; |
| 3987 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3988 | mTransport = transport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3989 | mObserver = observer; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 3990 | mToken = restoreSetToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3991 | mTargetPackage = targetPackage; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3992 | mPmToken = pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3993 | mNeedFullBackup = needFullBackup; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3994 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3995 | if (filterSet != null) { |
| 3996 | mFilterSet = new HashSet<String>(); |
| 3997 | for (String pkg : filterSet) { |
| 3998 | mFilterSet.add(pkg); |
| 3999 | } |
| 4000 | } else { |
| 4001 | mFilterSet = null; |
| 4002 | } |
| 4003 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 4004 | try { |
| 4005 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 4006 | } catch (RemoteException e) { |
| 4007 | // can't happen; the transport is local |
| 4008 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4011 | // Execute one tick of whatever state machine the task implements |
| 4012 | @Override |
| 4013 | public void execute() { |
| 4014 | if (MORE_DEBUG) Slog.v(TAG, "*** Executing restore step: " + mCurrentState); |
| 4015 | switch (mCurrentState) { |
| 4016 | case INITIAL: |
| 4017 | beginRestore(); |
| 4018 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4019 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4020 | case DOWNLOAD_DATA: |
| 4021 | downloadRestoreData(); |
| 4022 | break; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4023 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4024 | case PM_METADATA: |
| 4025 | restorePmMetadata(); |
| 4026 | break; |
| 4027 | |
| 4028 | case RUNNING_QUEUE: |
| 4029 | restoreNextAgent(); |
| 4030 | break; |
| 4031 | |
| 4032 | case FINAL: |
| 4033 | if (!mFinished) finalizeRestore(); |
| 4034 | else { |
| 4035 | Slog.e(TAG, "Duplicate finish"); |
| 4036 | } |
| 4037 | mFinished = true; |
| 4038 | break; |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | // Initialize and set up for the PM metadata restore, which comes first |
| 4043 | void beginRestore() { |
| 4044 | // Don't account time doing the restore as inactivity of the app |
| 4045 | // that has opened a restore session. |
| 4046 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4047 | |
| 4048 | // Assume error until we successfully init everything |
| 4049 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4050 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4051 | try { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4052 | // TODO: Log this before getAvailableRestoreSets, somehow |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4053 | EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 4054 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4055 | // Get the list of all packages which have backup enabled. |
| 4056 | // (Include the Package Manager metadata pseudo-package first.) |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4057 | mRestorePackages = new ArrayList<PackageInfo>(); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4058 | PackageInfo omPackage = new PackageInfo(); |
| 4059 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4060 | mRestorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4061 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4062 | mAgentPackages = allAgentPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4063 | if (mTargetPackage == null) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4064 | // if there's a filter set, strip out anything that isn't |
| 4065 | // present before proceeding |
| 4066 | if (mFilterSet != null) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4067 | for (int i = mAgentPackages.size() - 1; i >= 0; i--) { |
| 4068 | final PackageInfo pkg = mAgentPackages.get(i); |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4069 | if (! mFilterSet.contains(pkg.packageName)) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4070 | mAgentPackages.remove(i); |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4071 | } |
| 4072 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4073 | if (MORE_DEBUG) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4074 | Slog.i(TAG, "Post-filter package set for restore:"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4075 | for (PackageInfo p : mAgentPackages) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4076 | Slog.i(TAG, " " + p); |
| 4077 | } |
| 4078 | } |
| 4079 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4080 | mRestorePackages.addAll(mAgentPackages); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4081 | } else { |
| 4082 | // Just one package to attempt restore of |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4083 | mRestorePackages.add(mTargetPackage); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4084 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4085 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4086 | // let the observer know that we're running |
| 4087 | if (mObserver != null) { |
| 4088 | try { |
| 4089 | // !!! TODO: get an actual count from the transport after |
| 4090 | // its startRestore() runs? |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4091 | mObserver.restoreStarting(mRestorePackages.size()); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4092 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4093 | Slog.d(TAG, "Restore observer died at restoreStarting"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4094 | mObserver = null; |
| 4095 | } |
| 4096 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4097 | } catch (RemoteException e) { |
| 4098 | // Something has gone catastrophically wrong with the transport |
| 4099 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4100 | executeNextState(RestoreState.FINAL); |
| 4101 | return; |
| 4102 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4103 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4104 | mStatus = BackupConstants.TRANSPORT_OK; |
| 4105 | executeNextState(RestoreState.DOWNLOAD_DATA); |
| 4106 | } |
| 4107 | |
| 4108 | void downloadRestoreData() { |
| 4109 | // Note that the download phase can be very time consuming, but we're executing |
| 4110 | // it inline here on the looper. This is "okay" because it is not calling out to |
| 4111 | // third party code; the transport is "trusted," and so we assume it is being a |
| 4112 | // good citizen and timing out etc when appropriate. |
| 4113 | // |
| 4114 | // TODO: when appropriate, move the download off the looper and rearrange the |
| 4115 | // error handling around that. |
| 4116 | try { |
| 4117 | mStatus = mTransport.startRestore(mToken, |
| 4118 | mRestorePackages.toArray(new PackageInfo[0])); |
| 4119 | if (mStatus != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4120 | Slog.e(TAG, "Error starting restore operation"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4121 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4122 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4123 | return; |
| 4124 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4125 | } catch (RemoteException e) { |
| 4126 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4127 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4128 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4129 | executeNextState(RestoreState.FINAL); |
| 4130 | return; |
| 4131 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4132 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4133 | // Successful download of the data to be parceled out to the apps, so off we go. |
| 4134 | executeNextState(RestoreState.PM_METADATA); |
| 4135 | } |
| 4136 | |
| 4137 | void restorePmMetadata() { |
| 4138 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4139 | String packageName = mTransport.nextRestorePackage(); |
| 4140 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4141 | Slog.e(TAG, "Error getting first restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4142 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4143 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4144 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4145 | return; |
| 4146 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4147 | Slog.i(TAG, "No restore data available"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4148 | int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4149 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4150 | mStatus = BackupConstants.TRANSPORT_OK; |
| 4151 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4152 | return; |
| 4153 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4154 | Slog.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4155 | + "\", found only \"" + packageName + "\""); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4156 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4157 | "Package manager data missing"); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4158 | executeNextState(RestoreState.FINAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4159 | return; |
| 4160 | } |
| 4161 | |
| 4162 | // Pull the Package Manager metadata from the restore set first |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4163 | PackageInfo omPackage = new PackageInfo(); |
| 4164 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 4165 | mPmAgent = new PackageManagerBackupAgent( |
| 4166 | mPackageManager, mAgentPackages); |
| 4167 | initiateOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(mPmAgent.onBind()), |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4168 | mNeedFullBackup); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4169 | // The PM agent called operationComplete() already, because our invocation |
| 4170 | // of it is process-local and therefore synchronous. That means that a |
| 4171 | // RUNNING_QUEUE message is already enqueued. Only if we're unable to |
| 4172 | // proceed with running the queue do we remove that pending message and |
| 4173 | // jump straight to the FINAL state. |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4174 | |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4175 | // Verify that the backup set includes metadata. If not, we can't do |
| 4176 | // signature/version verification etc, so we simply do not proceed with |
| 4177 | // the restore operation. |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4178 | if (!mPmAgent.hasMetadata()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4179 | Slog.e(TAG, "No restore metadata available, so not restoring settings"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4180 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4181 | "Package manager restore metadata missing"); |
| 4182 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4183 | mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this); |
| 4184 | executeNextState(RestoreState.FINAL); |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4185 | return; |
| 4186 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4187 | } catch (RemoteException e) { |
| 4188 | Slog.e(TAG, "Error communicating with transport for restore"); |
| 4189 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4190 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4191 | mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this); |
| 4192 | executeNextState(RestoreState.FINAL); |
| 4193 | return; |
| 4194 | } |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 4195 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4196 | // Metadata is intact, so we can now run the restore queue. If we get here, |
| 4197 | // we have already enqueued the necessary next-step message on the looper. |
| 4198 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4199 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4200 | void restoreNextAgent() { |
| 4201 | try { |
| 4202 | String packageName = mTransport.nextRestorePackage(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4203 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4204 | if (packageName == null) { |
| 4205 | Slog.e(TAG, "Error getting next restore package"); |
| 4206 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 4207 | executeNextState(RestoreState.FINAL); |
| 4208 | return; |
| 4209 | } else if (packageName.equals("")) { |
| 4210 | if (DEBUG) Slog.v(TAG, "No next package, finishing restore"); |
| 4211 | int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime); |
| 4212 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, mCount, millis); |
| 4213 | executeNextState(RestoreState.FINAL); |
| 4214 | return; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4215 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4216 | |
| 4217 | if (mObserver != null) { |
| 4218 | try { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4219 | mObserver.onUpdate(mCount, packageName); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4220 | } catch (RemoteException e) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4221 | Slog.d(TAG, "Restore observer died in onUpdate"); |
| 4222 | mObserver = null; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 4223 | } |
| 4224 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4225 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4226 | Metadata metaInfo = mPmAgent.getRestoredMetadata(packageName); |
| 4227 | if (metaInfo == null) { |
| 4228 | Slog.e(TAG, "Missing metadata for " + packageName); |
| 4229 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4230 | "Package metadata missing"); |
| 4231 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4232 | return; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4233 | } |
| 4234 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4235 | PackageInfo packageInfo; |
| 4236 | try { |
| 4237 | int flags = PackageManager.GET_SIGNATURES; |
| 4238 | packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 4239 | } catch (NameNotFoundException e) { |
| 4240 | Slog.e(TAG, "Invalid package restoring data", e); |
| 4241 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4242 | "Package missing on device"); |
| 4243 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4244 | return; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4245 | } |
| 4246 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4247 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| 4248 | // Data is from a "newer" version of the app than we have currently |
| 4249 | // installed. If the app has not declared that it is prepared to |
| 4250 | // handle this case, we do not attempt the restore. |
| 4251 | if ((packageInfo.applicationInfo.flags |
| 4252 | & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) { |
| 4253 | String message = "Version " + metaInfo.versionCode |
| 4254 | + " > installed version " + packageInfo.versionCode; |
| 4255 | Slog.w(TAG, "Package " + packageName + ": " + message); |
| 4256 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 4257 | packageName, message); |
| 4258 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4259 | return; |
| 4260 | } else { |
| 4261 | if (DEBUG) Slog.v(TAG, "Version " + metaInfo.versionCode |
| 4262 | + " > installed " + packageInfo.versionCode |
| 4263 | + " but restoreAnyVersion"); |
| 4264 | } |
| 4265 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4266 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4267 | if (!signaturesMatch(metaInfo.signatures, packageInfo)) { |
| 4268 | Slog.w(TAG, "Signature mismatch restoring " + packageName); |
| 4269 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4270 | "Signature mismatch"); |
| 4271 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4272 | return; |
| 4273 | } |
| 4274 | |
| 4275 | if (DEBUG) Slog.v(TAG, "Package " + packageName |
| 4276 | + " restore version [" + metaInfo.versionCode |
| 4277 | + "] is compatible with installed version [" |
| 4278 | + packageInfo.versionCode + "]"); |
| 4279 | |
| 4280 | // Then set up and bind the agent |
| 4281 | IBackupAgent agent = bindToAgentSynchronous( |
| 4282 | packageInfo.applicationInfo, |
| 4283 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| 4284 | if (agent == null) { |
| 4285 | Slog.w(TAG, "Can't find backup agent for " + packageName); |
| 4286 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| 4287 | "Restore agent missing"); |
| 4288 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4289 | return; |
| 4290 | } |
| 4291 | |
| 4292 | // And then finally start the restore on this agent |
| 4293 | try { |
| 4294 | initiateOneRestore(packageInfo, metaInfo.versionCode, agent, mNeedFullBackup); |
| 4295 | ++mCount; |
| 4296 | } catch (Exception e) { |
| 4297 | Slog.e(TAG, "Error when attempting restore: " + e.toString()); |
| 4298 | agentErrorCleanup(); |
| 4299 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4300 | } |
| 4301 | } catch (RemoteException e) { |
| 4302 | Slog.e(TAG, "Unable to fetch restore data from transport"); |
| 4303 | mStatus = BackupConstants.TRANSPORT_ERROR; |
| 4304 | executeNextState(RestoreState.FINAL); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4305 | } |
| 4306 | } |
| 4307 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4308 | void finalizeRestore() { |
| 4309 | if (MORE_DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver); |
| 4310 | |
| 4311 | try { |
| 4312 | mTransport.finishRestore(); |
| 4313 | } catch (RemoteException e) { |
| 4314 | Slog.e(TAG, "Error finishing restore", e); |
| 4315 | } |
| 4316 | |
| 4317 | if (mObserver != null) { |
| 4318 | try { |
| 4319 | mObserver.restoreFinished(mStatus); |
| 4320 | } catch (RemoteException e) { |
| 4321 | Slog.d(TAG, "Restore observer died at restoreFinished"); |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | // If this was a restoreAll operation, record that this was our |
| 4326 | // ancestral dataset, as well as the set of apps that are possibly |
| 4327 | // restoreable from the dataset |
| 4328 | if (mTargetPackage == null && mPmAgent != null) { |
| 4329 | mAncestralPackages = mPmAgent.getRestoredPackages(); |
| 4330 | mAncestralToken = mToken; |
| 4331 | writeRestoreTokens(); |
| 4332 | } |
| 4333 | |
| 4334 | // We must under all circumstances tell the Package Manager to |
| 4335 | // proceed with install notifications if it's waiting for us. |
| 4336 | if (mPmToken > 0) { |
| 4337 | if (MORE_DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken); |
| 4338 | try { |
| 4339 | mPackageManagerBinder.finishPackageInstall(mPmToken); |
| 4340 | } catch (RemoteException e) { /* can't happen */ } |
| 4341 | } |
| 4342 | |
| 4343 | // Furthermore we need to reset the session timeout clock |
| 4344 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4345 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, |
| 4346 | TIMEOUT_RESTORE_INTERVAL); |
| 4347 | |
| 4348 | // done; we can finally release the wakelock |
| 4349 | Slog.i(TAG, "Restore complete."); |
| 4350 | mWakelock.release(); |
| 4351 | } |
| 4352 | |
| 4353 | // Call asynchronously into the app, passing it the restore data. The next step |
| 4354 | // after this is always a callback, either operationComplete() or handleTimeout(). |
| 4355 | void initiateOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4356 | boolean needFullBackup) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4357 | mCurrentPackage = app; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4358 | final String packageName = app.packageName; |
| 4359 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4360 | if (DEBUG) Slog.d(TAG, "initiateOneRestore packageName=" + packageName); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4361 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4362 | // !!! TODO: get the dirs from the transport |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4363 | mBackupDataName = new File(mDataDir, packageName + ".restore"); |
| 4364 | mNewStateName = new File(mStateDir, packageName + ".new"); |
| 4365 | mSavedStateName = new File(mStateDir, packageName); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4366 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4367 | final int token = generateToken(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4368 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4369 | // Run the transport's restore pass |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4370 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4371 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4372 | ParcelFileDescriptor.MODE_CREATE | |
| 4373 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4374 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4375 | if (mTransport.getRestoreData(mBackupData) != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4376 | Slog.e(TAG, "Error getting restore data for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4377 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4378 | return; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | // Okay, we have the data. Now have the agent do the restore. |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4382 | mBackupData.close(); |
| 4383 | mBackupData = ParcelFileDescriptor.open(mBackupDataName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4384 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 4385 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4386 | mNewState = ParcelFileDescriptor.open(mNewStateName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4387 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4388 | ParcelFileDescriptor.MODE_CREATE | |
| 4389 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4390 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4391 | // Kick off the restore, checking for hung agents |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4392 | prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL, this); |
| 4393 | agent.doRestore(mBackupData, appVersionCode, mNewState, token, mBackupManagerBinder); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4394 | } catch (Exception e) { |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4395 | Slog.e(TAG, "Unable to call app for restore: " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4396 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString()); |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4397 | agentErrorCleanup(); // clears any pending timeout messages as well |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4398 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4399 | // After a restore failure we go back to running the queue. If there |
| 4400 | // are no more packages to be restored that will be handled by the |
| 4401 | // next step. |
| 4402 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4403 | } |
| 4404 | } |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4405 | |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4406 | void agentErrorCleanup() { |
| 4407 | // If the agent fails restore, it might have put the app's data |
| 4408 | // into an incoherent state. For consistency we wipe its data |
| 4409 | // again in this case before continuing with normal teardown |
| 4410 | clearApplicationDataSynchronous(mCurrentPackage.packageName); |
| 4411 | agentCleanup(); |
| 4412 | } |
| 4413 | |
| 4414 | void agentCleanup() { |
| 4415 | mBackupDataName.delete(); |
| 4416 | try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {} |
| 4417 | try { if (mNewState != null) mNewState.close(); } catch (IOException e) {} |
| 4418 | mBackupData = mNewState = null; |
| 4419 | |
| 4420 | // if everything went okay, remember the recorded state now |
| 4421 | // |
| 4422 | // !!! TODO: the restored data should be migrated on the server |
| 4423 | // side into the current dataset. In that case the new state file |
| 4424 | // we just created would reflect the data already extant in the |
| 4425 | // backend, so there'd be nothing more to do. Until that happens, |
| 4426 | // however, we need to make sure that we record the data to the |
| 4427 | // current backend dataset. (Yes, this means shipping the data over |
| 4428 | // the wire in both directions. That's bad, but consistency comes |
| 4429 | // first, then efficiency.) Once we introduce server-side data |
| 4430 | // migration to the newly-restored device's dataset, we will change |
| 4431 | // the following from a discard of the newly-written state to the |
| 4432 | // "correct" operation of renaming into the canonical state blob. |
| 4433 | mNewStateName.delete(); // TODO: remove; see above comment |
| 4434 | //mNewStateName.renameTo(mSavedStateName); // TODO: replace with this |
| 4435 | |
| 4436 | // If this wasn't the PM pseudopackage, tear down the agent side |
| 4437 | if (mCurrentPackage.applicationInfo != null) { |
| 4438 | // unbind and tidy up even on timeout or failure |
| 4439 | try { |
| 4440 | mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo); |
| 4441 | |
| 4442 | // The agent was probably running with a stub Application object, |
| 4443 | // which isn't a valid run mode for the main app logic. Shut |
| 4444 | // down the app so that next time it's launched, it gets the |
| 4445 | // usual full initialization. Note that this is only done for |
| 4446 | // full-system restores: when a single app has requested a restore, |
| 4447 | // it is explicitly not killed following that operation. |
| 4448 | if (mTargetPackage == null && (mCurrentPackage.applicationInfo.flags |
| 4449 | & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) { |
| 4450 | if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of " |
| 4451 | + mCurrentPackage.applicationInfo.processName); |
| 4452 | mActivityManager.killApplicationProcess( |
| 4453 | mCurrentPackage.applicationInfo.processName, |
| 4454 | mCurrentPackage.applicationInfo.uid); |
| 4455 | } |
| 4456 | } catch (RemoteException e) { |
| 4457 | // 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] | 4458 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4459 | } |
| Christopher Tate | 2982d06 | 2011-09-06 20:35:24 -0700 | [diff] [blame] | 4460 | |
| 4461 | // The caller is responsible for reestablishing the state machine; our |
| 4462 | // responsibility here is to clear the decks for whatever comes next. |
| 4463 | mBackupHandler.removeMessages(MSG_TIMEOUT, this); |
| 4464 | synchronized (mCurrentOpLock) { |
| 4465 | mCurrentOperations.clear(); |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | // A call to agent.doRestore() has been positively acknowledged as complete |
| 4470 | @Override |
| 4471 | public void operationComplete() { |
| 4472 | int size = (int) mBackupDataName.length(); |
| 4473 | EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, mCurrentPackage.packageName, size); |
| 4474 | // Just go back to running the restore queue |
| 4475 | agentCleanup(); |
| 4476 | |
| 4477 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4478 | } |
| 4479 | |
| 4480 | // A call to agent.doRestore() has timed out |
| 4481 | @Override |
| 4482 | public void handleTimeout() { |
| 4483 | Slog.e(TAG, "Timeout restoring application " + mCurrentPackage.packageName); |
| 4484 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 4485 | mCurrentPackage.packageName, "restore timeout"); |
| 4486 | // Handle like an agent that threw on invocation: wipe it and go on to the next |
| 4487 | agentErrorCleanup(); |
| 4488 | executeNextState(RestoreState.RUNNING_QUEUE); |
| 4489 | } |
| 4490 | |
| 4491 | void executeNextState(RestoreState nextState) { |
| 4492 | if (MORE_DEBUG) Slog.i(TAG, " => executing next step on " |
| 4493 | + this + " nextState=" + nextState); |
| 4494 | mCurrentState = nextState; |
| 4495 | Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this); |
| 4496 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4497 | } |
| 4498 | } |
| 4499 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4500 | class PerformClearTask implements Runnable { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4501 | IBackupTransport mTransport; |
| 4502 | PackageInfo mPackage; |
| 4503 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4504 | PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4505 | mTransport = transport; |
| 4506 | mPackage = packageInfo; |
| 4507 | } |
| 4508 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4509 | public void run() { |
| 4510 | try { |
| 4511 | // Clear the on-device backup state to ensure a full backup next time |
| 4512 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 4513 | File stateFile = new File(stateDir, mPackage.packageName); |
| 4514 | stateFile.delete(); |
| 4515 | |
| 4516 | // Tell the transport to remove all the persistent storage for the app |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4517 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4518 | mTransport.clearBackupData(mPackage); |
| 4519 | } catch (RemoteException e) { |
| 4520 | // can't happen; the transport is local |
| 4521 | } finally { |
| 4522 | try { |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4523 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4524 | mTransport.finishBackup(); |
| 4525 | } catch (RemoteException e) { |
| 4526 | // can't happen; the transport is local |
| 4527 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4528 | |
| 4529 | // Last but not least, release the cpu |
| 4530 | mWakelock.release(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4531 | } |
| 4532 | } |
| 4533 | } |
| 4534 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4535 | class PerformInitializeTask implements Runnable { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4536 | HashSet<String> mQueue; |
| 4537 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4538 | PerformInitializeTask(HashSet<String> transportNames) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4539 | mQueue = transportNames; |
| 4540 | } |
| 4541 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4542 | public void run() { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4543 | try { |
| 4544 | for (String transportName : mQueue) { |
| 4545 | IBackupTransport transport = getTransport(transportName); |
| 4546 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4547 | Slog.e(TAG, "Requested init for " + transportName + " but not found"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4548 | continue; |
| 4549 | } |
| 4550 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4551 | Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4552 | EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName()); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4553 | long startRealtime = SystemClock.elapsedRealtime(); |
| 4554 | int status = transport.initializeDevice(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4555 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4556 | if (status == BackupConstants.TRANSPORT_OK) { |
| 4557 | status = transport.finishBackup(); |
| 4558 | } |
| 4559 | |
| 4560 | // Okay, the wipe really happened. Clean up our local bookkeeping. |
| 4561 | if (status == BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4562 | Slog.i(TAG, "Device init successful"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4563 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4564 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4565 | resetBackupState(new File(mBaseStateDir, transport.transportDirName())); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4566 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4567 | synchronized (mQueueLock) { |
| 4568 | recordInitPendingLocked(false, transportName); |
| 4569 | } |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4570 | } else { |
| 4571 | // If this didn't work, requeue this one and try again |
| 4572 | // after a suitable interval |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4573 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4574 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4575 | synchronized (mQueueLock) { |
| 4576 | recordInitPendingLocked(true, transportName); |
| 4577 | } |
| 4578 | // do this via another alarm to make sure of the wakelock states |
| 4579 | long delay = transport.requestBackupTime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4580 | if (DEBUG) Slog.w(TAG, "init failed on " |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4581 | + transportName + " resched in " + delay); |
| 4582 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 4583 | System.currentTimeMillis() + delay, mRunInitIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4584 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4585 | } |
| 4586 | } catch (RemoteException e) { |
| 4587 | // can't happen; the transports are local |
| 4588 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4589 | Slog.e(TAG, "Unexpected error performing init", e); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4590 | } finally { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 4591 | // Done; release the wakelock |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4592 | mWakelock.release(); |
| 4593 | } |
| 4594 | } |
| 4595 | } |
| 4596 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4597 | private void dataChangedImpl(String packageName) { |
| 4598 | HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4599 | dataChangedImpl(packageName, targets); |
| 4600 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4601 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4602 | private void dataChangedImpl(String packageName, HashSet<ApplicationInfo> targets) { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4603 | // Record that we need a backup pass for the caller. Since multiple callers |
| 4604 | // may share a uid, we need to note all candidates within that uid and schedule |
| 4605 | // a backup pass for each of them. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4606 | EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4607 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4608 | if (targets == null) { |
| 4609 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4610 | + " uid=" + Binder.getCallingUid()); |
| 4611 | return; |
| 4612 | } |
| 4613 | |
| 4614 | synchronized (mQueueLock) { |
| 4615 | // Note that this client has made data changes that need to be backed up |
| 4616 | for (ApplicationInfo app : targets) { |
| 4617 | // validate the caller-supplied package name against the known set of |
| 4618 | // packages associated with this uid |
| 4619 | if (app.packageName.equals(packageName)) { |
| 4620 | // Add the caller to the set of pending backups. If there is |
| 4621 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4622 | BackupRequest req = new BackupRequest(packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 4623 | if (mPendingBackups.put(app.packageName, req) == null) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4624 | // Journal this request in case of crash. The put() |
| 4625 | // operation returned null when this package was not already |
| 4626 | // in the set; we want to avoid touching the disk redundantly. |
| 4627 | writeToJournalLocked(packageName); |
| 4628 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4629 | if (MORE_DEBUG) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4630 | int numKeys = mPendingBackups.size(); |
| 4631 | Slog.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| 4632 | for (BackupRequest b : mPendingBackups.values()) { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4633 | Slog.d(TAG, " + " + b); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | // Note: packageName is currently unused, but may be in the future |
| 4643 | private HashSet<ApplicationInfo> dataChangedTargets(String packageName) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4644 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4645 | // backup of its own data. |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4646 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4647 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4648 | synchronized (mBackupParticipants) { |
| 4649 | return mBackupParticipants.get(Binder.getCallingUid()); |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | // a caller with full permission can ask to back up any participating app |
| 4654 | // !!! TODO: allow backup of ANY app? |
| 4655 | HashSet<ApplicationInfo> targets = new HashSet<ApplicationInfo>(); |
| 4656 | synchronized (mBackupParticipants) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4657 | int N = mBackupParticipants.size(); |
| 4658 | for (int i = 0; i < N; i++) { |
| 4659 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4660 | if (s != null) { |
| 4661 | targets.addAll(s); |
| 4662 | } |
| 4663 | } |
| 4664 | } |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4665 | return targets; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4666 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4667 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4668 | private void writeToJournalLocked(String str) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4669 | RandomAccessFile out = null; |
| 4670 | try { |
| 4671 | if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir); |
| 4672 | out = new RandomAccessFile(mJournal, "rws"); |
| 4673 | out.seek(out.length()); |
| 4674 | out.writeUTF(str); |
| 4675 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4676 | Slog.e(TAG, "Can't write " + str + " to backup journal", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4677 | mJournal = null; |
| 4678 | } finally { |
| 4679 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4683 | // ----- IBackupManager binder interface ----- |
| 4684 | |
| 4685 | public void dataChanged(final String packageName) { |
| 4686 | final HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4687 | if (targets == null) { |
| 4688 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4689 | + " uid=" + Binder.getCallingUid()); |
| 4690 | return; |
| 4691 | } |
| 4692 | |
| 4693 | mBackupHandler.post(new Runnable() { |
| 4694 | public void run() { |
| 4695 | dataChangedImpl(packageName, targets); |
| 4696 | } |
| 4697 | }); |
| 4698 | } |
| 4699 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4700 | // Clear the given package's backup data from the current transport |
| 4701 | public void clearBackupData(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4702 | if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4703 | PackageInfo info; |
| 4704 | try { |
| 4705 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 4706 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4707 | Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4708 | return; |
| 4709 | } |
| 4710 | |
| 4711 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4712 | // wipe of its own backed-up data. |
| 4713 | HashSet<ApplicationInfo> apps; |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 4714 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4715 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 4716 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 4717 | } else { |
| 4718 | // a caller with full permission can ask to back up any participating app |
| 4719 | // !!! TODO: allow data-clear of ANY app? |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4720 | if (DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4721 | apps = new HashSet<ApplicationInfo>(); |
| 4722 | int N = mBackupParticipants.size(); |
| 4723 | for (int i = 0; i < N; i++) { |
| 4724 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4725 | if (s != null) { |
| 4726 | apps.addAll(s); |
| 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | // now find the given package in the set of candidate apps |
| 4732 | for (ApplicationInfo app : apps) { |
| 4733 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4734 | if (DEBUG) Slog.v(TAG, "Found the app - running clear process"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4735 | // found it; fire off the clear request |
| 4736 | synchronized (mQueueLock) { |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4737 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4738 | mWakelock.acquire(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4739 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 4740 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 4741 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4742 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4743 | } |
| 4744 | break; |
| 4745 | } |
| 4746 | } |
| 4747 | } |
| 4748 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 4749 | // Run a backup pass immediately for any applications that have declared |
| 4750 | // that they have pending updates. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4751 | public void backupNow() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4752 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4753 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4754 | if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4755 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4756 | // Because the alarms we are using can jitter, and we want an *immediate* |
| 4757 | // backup pass to happen, we restart the timer beginning with "next time," |
| 4758 | // then manually fire the backup trigger intent ourselves. |
| 4759 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4760 | try { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4761 | mRunBackupIntent.send(); |
| 4762 | } catch (PendingIntent.CanceledException e) { |
| 4763 | // should never happen |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4764 | Slog.e(TAG, "run-backup intent cancelled!"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4765 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4766 | } |
| 4767 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4768 | |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame^] | 4769 | boolean deviceIsProvisioned() { |
| 4770 | final ContentResolver resolver = mContext.getContentResolver(); |
| 4771 | return (Settings.Secure.getInt(resolver, Settings.Secure.DEVICE_PROVISIONED, 0) != 0); |
| 4772 | } |
| 4773 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4774 | // Run a *full* backup pass for the given package, writing the resulting data stream |
| 4775 | // to the supplied file descriptor. This method is synchronous and does not return |
| 4776 | // to the caller until the backup has been completed. |
| 4777 | public void fullBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeShared, |
| 4778 | boolean doAllApps, String[] pkgList) { |
| 4779 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 4780 | |
| 4781 | // Validate |
| 4782 | if (!doAllApps) { |
| 4783 | if (!includeShared) { |
| 4784 | // If we're backing up shared data (sdcard or equivalent), then we can run |
| 4785 | // without any supplied app names. Otherwise, we'd be doing no work, so |
| 4786 | // report the error. |
| 4787 | if (pkgList == null || pkgList.length == 0) { |
| 4788 | throw new IllegalArgumentException( |
| 4789 | "Backup requested but neither shared nor any apps named"); |
| 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4794 | long oldId = Binder.clearCallingIdentity(); |
| 4795 | try { |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame^] | 4796 | // Doesn't make sense to do a full backup prior to setup |
| 4797 | if (!deviceIsProvisioned()) { |
| 4798 | Slog.i(TAG, "Full backup not supported before setup"); |
| 4799 | return; |
| 4800 | } |
| 4801 | |
| 4802 | if (DEBUG) Slog.v(TAG, "Requesting full backup: apks=" + includeApks |
| 4803 | + " shared=" + includeShared + " all=" + doAllApps |
| 4804 | + " pkgs=" + pkgList); |
| 4805 | Slog.i(TAG, "Beginning full backup..."); |
| 4806 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4807 | FullBackupParams params = new FullBackupParams(fd, includeApks, includeShared, |
| 4808 | doAllApps, pkgList); |
| 4809 | final int token = generateToken(); |
| 4810 | synchronized (mFullConfirmations) { |
| 4811 | mFullConfirmations.put(token, params); |
| 4812 | } |
| 4813 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4814 | // start up the confirmation UI |
| 4815 | if (DEBUG) Slog.d(TAG, "Starting backup confirmation UI, token=" + token); |
| 4816 | if (!startConfirmationUi(token, FullBackup.FULL_BACKUP_INTENT_ACTION)) { |
| 4817 | Slog.e(TAG, "Unable to launch full backup confirmation"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4818 | mFullConfirmations.delete(token); |
| 4819 | return; |
| 4820 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4821 | |
| 4822 | // make sure the screen is lit for the user interaction |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4823 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 4824 | |
| 4825 | // start the confirmation countdown |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4826 | startConfirmationTimeout(token, params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4827 | |
| 4828 | // wait for the backup to be performed |
| 4829 | if (DEBUG) Slog.d(TAG, "Waiting for full backup completion..."); |
| 4830 | waitForCompletion(params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4831 | } finally { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4832 | try { |
| 4833 | fd.close(); |
| 4834 | } catch (IOException e) { |
| 4835 | // just eat it |
| 4836 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4837 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame^] | 4838 | Slog.d(TAG, "Full backup processing complete."); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4839 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | public void fullRestore(ParcelFileDescriptor fd) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4843 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullRestore"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4844 | |
| 4845 | long oldId = Binder.clearCallingIdentity(); |
| 4846 | |
| 4847 | try { |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame^] | 4848 | // Check whether the device has been provisioned -- we don't handle |
| 4849 | // full restores prior to completing the setup process. |
| 4850 | if (!deviceIsProvisioned()) { |
| 4851 | Slog.i(TAG, "Full restore not permitted before setup"); |
| 4852 | return; |
| 4853 | } |
| 4854 | |
| 4855 | Slog.i(TAG, "Beginning full restore..."); |
| 4856 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4857 | FullRestoreParams params = new FullRestoreParams(fd); |
| 4858 | final int token = generateToken(); |
| 4859 | synchronized (mFullConfirmations) { |
| 4860 | mFullConfirmations.put(token, params); |
| 4861 | } |
| 4862 | |
| 4863 | // start up the confirmation UI |
| 4864 | if (DEBUG) Slog.d(TAG, "Starting restore confirmation UI, token=" + token); |
| 4865 | if (!startConfirmationUi(token, FullBackup.FULL_RESTORE_INTENT_ACTION)) { |
| 4866 | Slog.e(TAG, "Unable to launch full restore confirmation"); |
| 4867 | mFullConfirmations.delete(token); |
| 4868 | return; |
| 4869 | } |
| 4870 | |
| 4871 | // make sure the screen is lit for the user interaction |
| 4872 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 4873 | |
| 4874 | // start the confirmation countdown |
| 4875 | startConfirmationTimeout(token, params); |
| 4876 | |
| 4877 | // wait for the restore to be performed |
| 4878 | if (DEBUG) Slog.d(TAG, "Waiting for full restore completion..."); |
| 4879 | waitForCompletion(params); |
| 4880 | } finally { |
| 4881 | try { |
| 4882 | fd.close(); |
| 4883 | } catch (IOException e) { |
| 4884 | Slog.w(TAG, "Error trying to close fd after full restore: " + e); |
| 4885 | } |
| 4886 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | d2c0cd4 | 2011-09-15 15:51:29 -0700 | [diff] [blame^] | 4887 | Slog.i(TAG, "Full restore processing complete."); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | boolean startConfirmationUi(int token, String action) { |
| 4892 | try { |
| 4893 | Intent confIntent = new Intent(action); |
| 4894 | confIntent.setClassName("com.android.backupconfirm", |
| 4895 | "com.android.backupconfirm.BackupRestoreConfirmation"); |
| 4896 | confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token); |
| 4897 | confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4898 | mContext.startActivity(confIntent); |
| 4899 | } catch (ActivityNotFoundException e) { |
| 4900 | return false; |
| 4901 | } |
| 4902 | return true; |
| 4903 | } |
| 4904 | |
| 4905 | void startConfirmationTimeout(int token, FullParams params) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4906 | if (MORE_DEBUG) Slog.d(TAG, "Posting conf timeout msg after " |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4907 | + TIMEOUT_FULL_CONFIRMATION + " millis"); |
| 4908 | Message msg = mBackupHandler.obtainMessage(MSG_FULL_CONFIRMATION_TIMEOUT, |
| 4909 | token, 0, params); |
| 4910 | mBackupHandler.sendMessageDelayed(msg, TIMEOUT_FULL_CONFIRMATION); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | void waitForCompletion(FullParams params) { |
| 4914 | synchronized (params.latch) { |
| 4915 | while (params.latch.get() == false) { |
| 4916 | try { |
| 4917 | params.latch.wait(); |
| 4918 | } catch (InterruptedException e) { /* never interrupted */ } |
| 4919 | } |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | void signalFullBackupRestoreCompletion(FullParams params) { |
| 4924 | synchronized (params.latch) { |
| 4925 | params.latch.set(true); |
| 4926 | params.latch.notifyAll(); |
| 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | // Confirm that the previously-requested full backup/restore operation can proceed. This |
| 4931 | // is used to require a user-facing disclosure about the operation. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4932 | @Override |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4933 | public void acknowledgeFullBackupOrRestore(int token, boolean allow, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 4934 | String curPassword, String encPpassword, IFullBackupRestoreObserver observer) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4935 | if (DEBUG) Slog.d(TAG, "acknowledgeFullBackupOrRestore : token=" + token |
| 4936 | + " allow=" + allow); |
| 4937 | |
| 4938 | // TODO: possibly require not just this signature-only permission, but even |
| 4939 | // require that the specific designated confirmation-UI app uid is the caller? |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4940 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "acknowledgeFullBackupOrRestore"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4941 | |
| 4942 | long oldId = Binder.clearCallingIdentity(); |
| 4943 | try { |
| 4944 | |
| 4945 | FullParams params; |
| 4946 | synchronized (mFullConfirmations) { |
| 4947 | params = mFullConfirmations.get(token); |
| 4948 | if (params != null) { |
| 4949 | mBackupHandler.removeMessages(MSG_FULL_CONFIRMATION_TIMEOUT, params); |
| 4950 | mFullConfirmations.delete(token); |
| 4951 | |
| 4952 | if (allow) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4953 | final int verb = params instanceof FullBackupParams |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4954 | ? MSG_RUN_FULL_BACKUP |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4955 | : MSG_RUN_FULL_RESTORE; |
| 4956 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 4957 | params.observer = observer; |
| 4958 | params.curPassword = curPassword; |
| 4959 | params.encryptPassword = encPpassword; |
| 4960 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4961 | if (DEBUG) Slog.d(TAG, "Sending conf message with verb " + verb); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4962 | mWakelock.acquire(); |
| 4963 | Message msg = mBackupHandler.obtainMessage(verb, params); |
| 4964 | mBackupHandler.sendMessage(msg); |
| 4965 | } else { |
| 4966 | Slog.w(TAG, "User rejected full backup/restore operation"); |
| 4967 | // indicate completion without having actually transferred any data |
| 4968 | signalFullBackupRestoreCompletion(params); |
| 4969 | } |
| 4970 | } else { |
| 4971 | Slog.w(TAG, "Attempted to ack full backup/restore with invalid token"); |
| 4972 | } |
| 4973 | } |
| 4974 | } finally { |
| 4975 | Binder.restoreCallingIdentity(oldId); |
| 4976 | } |
| 4977 | } |
| 4978 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4979 | // Enable/disable the backup service |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4980 | public void setBackupEnabled(boolean enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4981 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4982 | "setBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4983 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4984 | Slog.i(TAG, "Backup enabled => " + enable); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4985 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4986 | boolean wasEnabled = mEnabled; |
| 4987 | synchronized (this) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4988 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 4989 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4990 | mEnabled = enable; |
| 4991 | } |
| 4992 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4993 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4994 | if (enable && !wasEnabled && mProvisioned) { |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4995 | // if we've just been enabled, start scheduling backup passes |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4996 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4997 | } else if (!enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4998 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4999 | if (DEBUG) Slog.i(TAG, "Opting out of backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 5000 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 5001 | mAlarmManager.cancel(mRunBackupIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 5002 | |
| 5003 | // This also constitutes an opt-out, so we wipe any data for |
| 5004 | // this device from the backend. We start that process with |
| 5005 | // an alarm in order to guarantee wakelock states. |
| 5006 | if (wasEnabled && mProvisioned) { |
| 5007 | // NOTE: we currently flush every registered transport, not just |
| 5008 | // the currently-active one. |
| 5009 | HashSet<String> allTransports; |
| 5010 | synchronized (mTransports) { |
| 5011 | allTransports = new HashSet<String>(mTransports.keySet()); |
| 5012 | } |
| 5013 | // build the set of transports for which we are posting an init |
| 5014 | for (String transport : allTransports) { |
| 5015 | recordInitPendingLocked(true, transport); |
| 5016 | } |
| 5017 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), |
| 5018 | mRunInitIntent); |
| 5019 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5020 | } |
| 5021 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 5022 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5023 | |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5024 | // Enable/disable automatic restore of app data at install time |
| 5025 | public void setAutoRestore(boolean doAutoRestore) { |
| 5026 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5027 | "setAutoRestore"); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5028 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5029 | Slog.i(TAG, "Auto restore => " + doAutoRestore); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 5030 | |
| 5031 | synchronized (this) { |
| 5032 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 5033 | Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0); |
| 5034 | mAutoRestore = doAutoRestore; |
| 5035 | } |
| 5036 | } |
| 5037 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5038 | // Mark the backup service as having been provisioned |
| 5039 | public void setBackupProvisioned(boolean available) { |
| 5040 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5041 | "setBackupProvisioned"); |
| 5042 | |
| 5043 | boolean wasProvisioned = mProvisioned; |
| 5044 | synchronized (this) { |
| 5045 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 5046 | Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0); |
| 5047 | mProvisioned = available; |
| 5048 | } |
| 5049 | |
| 5050 | synchronized (mQueueLock) { |
| 5051 | if (available && !wasProvisioned && mEnabled) { |
| 5052 | // we're now good to go, so start the backup alarms |
| 5053 | startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL); |
| 5054 | } else if (!available) { |
| 5055 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5056 | Slog.w(TAG, "Backup service no longer provisioned"); |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5057 | mAlarmManager.cancel(mRunBackupIntent); |
| 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | private void startBackupAlarmsLocked(long delayBeforeFirstBackup) { |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 5063 | // We used to use setInexactRepeating(), but that may be linked to |
| 5064 | // backups running at :00 more often than not, creating load spikes. |
| 5065 | // Schedule at an exact time for now, and also add a bit of "fuzz". |
| 5066 | |
| 5067 | Random random = new Random(); |
| 5068 | long when = System.currentTimeMillis() + delayBeforeFirstBackup + |
| 5069 | random.nextInt(FUZZ_MILLIS); |
| 5070 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, |
| 5071 | BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5072 | mNextBackupPass = when; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5075 | // Report whether the backup mechanism is currently enabled |
| 5076 | public boolean isBackupEnabled() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5077 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5078 | return mEnabled; // no need to synchronize just to read it |
| 5079 | } |
| 5080 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5081 | // Report the name of the currently active transport |
| 5082 | public String getCurrentTransport() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5083 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 5084 | "getCurrentTransport"); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5085 | if (MORE_DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5086 | return mCurrentTransport; |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 5087 | } |
| 5088 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5089 | // Report all known, available backup transports |
| 5090 | public String[] listAllTransports() { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 5091 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5092 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5093 | String[] list = null; |
| 5094 | ArrayList<String> known = new ArrayList<String>(); |
| 5095 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 5096 | if (entry.getValue() != null) { |
| 5097 | known.add(entry.getKey()); |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | if (known.size() > 0) { |
| 5102 | list = new String[known.size()]; |
| 5103 | known.toArray(list); |
| 5104 | } |
| 5105 | return list; |
| 5106 | } |
| 5107 | |
| 5108 | // Select which transport to use for the next backup operation. If the given |
| 5109 | // name is not one of the available transports, no action is taken and the method |
| 5110 | // returns null. |
| 5111 | public String selectBackupTransport(String transport) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5112 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5113 | |
| 5114 | synchronized (mTransports) { |
| 5115 | String prevTransport = null; |
| 5116 | if (mTransports.get(transport) != null) { |
| 5117 | prevTransport = mCurrentTransport; |
| 5118 | mCurrentTransport = transport; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 5119 | Settings.Secure.putString(mContext.getContentResolver(), |
| 5120 | Settings.Secure.BACKUP_TRANSPORT, transport); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5121 | Slog.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5122 | + " returning " + prevTransport); |
| 5123 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5124 | Slog.w(TAG, "Attempt to select unavailable transport " + transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5125 | } |
| 5126 | return prevTransport; |
| 5127 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5128 | } |
| 5129 | |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5130 | // Supply the configuration Intent for the given transport. If the name is not one |
| 5131 | // of the available transports, or if the transport does not supply any configuration |
| 5132 | // UI, the method returns null. |
| 5133 | public Intent getConfigurationIntent(String transportName) { |
| 5134 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5135 | "getConfigurationIntent"); |
| 5136 | |
| 5137 | synchronized (mTransports) { |
| 5138 | final IBackupTransport transport = mTransports.get(transportName); |
| 5139 | if (transport != null) { |
| 5140 | try { |
| 5141 | final Intent intent = transport.configurationIntent(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5142 | if (MORE_DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent " |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5143 | + intent); |
| 5144 | return intent; |
| 5145 | } catch (RemoteException e) { |
| 5146 | /* fall through to return null */ |
| 5147 | } |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | return null; |
| 5152 | } |
| 5153 | |
| 5154 | // Supply the configuration summary string for the given transport. If the name is |
| 5155 | // not one of the available transports, or if the transport does not supply any |
| 5156 | // summary / destination string, the method can return null. |
| 5157 | // |
| 5158 | // This string is used VERBATIM as the summary text of the relevant Settings item! |
| 5159 | public String getDestinationString(String transportName) { |
| 5160 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 5161 | "getDestinationString"); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5162 | |
| 5163 | synchronized (mTransports) { |
| 5164 | final IBackupTransport transport = mTransports.get(transportName); |
| 5165 | if (transport != null) { |
| 5166 | try { |
| 5167 | final String text = transport.currentDestinationString(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 5168 | if (MORE_DEBUG) Slog.d(TAG, "getDestinationString() returning " + text); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 5169 | return text; |
| 5170 | } catch (RemoteException e) { |
| 5171 | /* fall through to return null */ |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | return null; |
| 5177 | } |
| 5178 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5179 | // Callback: a requested backup agent has been instantiated. This should only |
| 5180 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5181 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5182 | synchronized(mAgentConnectLock) { |
| 5183 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5184 | Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5185 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 5186 | mConnectedAgent = agent; |
| 5187 | mConnecting = false; |
| 5188 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5189 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5190 | + " claiming agent connected"); |
| 5191 | } |
| 5192 | mAgentConnectLock.notifyAll(); |
| 5193 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 5197 | // 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] | 5198 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5199 | public void agentDisconnected(String packageName) { |
| 5200 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5201 | synchronized(mAgentConnectLock) { |
| 5202 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 5203 | mConnectedAgent = null; |
| 5204 | mConnecting = false; |
| 5205 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5206 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5207 | + " claiming agent disconnected"); |
| 5208 | } |
| 5209 | mAgentConnectLock.notifyAll(); |
| 5210 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5211 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5212 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5213 | // An application being installed will need a restore pass, then the Package Manager |
| 5214 | // will need to be told when the restore is finished. |
| 5215 | public void restoreAtInstall(String packageName, int token) { |
| 5216 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5217 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5218 | + " attemping install-time restore"); |
| 5219 | return; |
| 5220 | } |
| 5221 | |
| 5222 | long restoreSet = getAvailableRestoreToken(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5223 | if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5224 | + " token=" + Integer.toHexString(token)); |
| 5225 | |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 5226 | if (mAutoRestore && mProvisioned && restoreSet != 0) { |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5227 | // okay, we're going to attempt a restore of this package from this restore set. |
| 5228 | // The eventual message back into the Package Manager to run the post-install |
| 5229 | // steps for 'token' will be issued from the restore handling code. |
| 5230 | |
| 5231 | // We can use a synthetic PackageInfo here because: |
| 5232 | // 1. We know it's valid, since the Package Manager supplied the name |
| 5233 | // 2. Only the packageName field will be used by the restore code |
| 5234 | PackageInfo pkg = new PackageInfo(); |
| 5235 | pkg.packageName = packageName; |
| 5236 | |
| 5237 | mWakelock.acquire(); |
| 5238 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 5239 | msg.obj = new RestoreParams(getTransport(mCurrentTransport), null, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5240 | restoreSet, pkg, token, true); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5241 | mBackupHandler.sendMessage(msg); |
| 5242 | } else { |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 5243 | // Auto-restore disabled or no way to attempt a restore; just tell the Package |
| 5244 | // Manager to proceed with the post-install handling for this package. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5245 | if (DEBUG) Slog.v(TAG, "No restore set -- skipping restore"); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5246 | try { |
| 5247 | mPackageManagerBinder.finishPackageInstall(token); |
| 5248 | } catch (RemoteException e) { /* can't happen */ } |
| 5249 | } |
| 5250 | } |
| 5251 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 5252 | // Hand off a restore session |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5253 | public IRestoreSession beginRestoreSession(String packageName, String transport) { |
| 5254 | if (DEBUG) Slog.v(TAG, "beginRestoreSession: pkg=" + packageName |
| 5255 | + " transport=" + transport); |
| 5256 | |
| 5257 | boolean needPermission = true; |
| 5258 | if (transport == null) { |
| 5259 | transport = mCurrentTransport; |
| 5260 | |
| 5261 | if (packageName != null) { |
| 5262 | PackageInfo app = null; |
| 5263 | try { |
| 5264 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 5265 | } catch (NameNotFoundException nnf) { |
| 5266 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| 5267 | throw new IllegalArgumentException("Package " + packageName + " not found"); |
| 5268 | } |
| 5269 | |
| 5270 | if (app.applicationInfo.uid == Binder.getCallingUid()) { |
| 5271 | // So: using the current active transport, and the caller has asked |
| 5272 | // that its own package will be restored. In this narrow use case |
| 5273 | // we do not require the caller to hold the permission. |
| 5274 | needPermission = false; |
| 5275 | } |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | if (needPermission) { |
| 5280 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5281 | "beginRestoreSession"); |
| 5282 | } else { |
| 5283 | if (DEBUG) Slog.d(TAG, "restoring self on current transport; no permission needed"); |
| 5284 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5285 | |
| 5286 | synchronized(this) { |
| 5287 | if (mActiveRestoreSession != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5288 | Slog.d(TAG, "Restore session requested but one already active"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5289 | return null; |
| 5290 | } |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5291 | mActiveRestoreSession = new ActiveRestoreSession(packageName, transport); |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5292 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5293 | } |
| 5294 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 5295 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 5296 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5297 | void clearRestoreSession(ActiveRestoreSession currentSession) { |
| 5298 | synchronized(this) { |
| 5299 | if (currentSession != mActiveRestoreSession) { |
| 5300 | Slog.e(TAG, "ending non-current restore session"); |
| 5301 | } else { |
| 5302 | if (DEBUG) Slog.v(TAG, "Clearing restore session and halting timeout"); |
| 5303 | mActiveRestoreSession = null; |
| 5304 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5309 | // Note that a currently-active backup agent has notified us that it has |
| 5310 | // completed the given outstanding asynchronous backup/restore operation. |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5311 | @Override |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5312 | public void opComplete(int token) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5313 | if (MORE_DEBUG) Slog.v(TAG, "opComplete: " + Integer.toHexString(token)); |
| 5314 | Operation op = null; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5315 | synchronized (mCurrentOpLock) { |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5316 | op = mCurrentOperations.get(token); |
| 5317 | if (op != null) { |
| 5318 | op.state = OP_ACKNOWLEDGED; |
| 5319 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5320 | mCurrentOpLock.notifyAll(); |
| 5321 | } |
| Christopher Tate | 8e294d4 | 2011-08-31 20:37:12 -0700 | [diff] [blame] | 5322 | |
| 5323 | // The completion callback, if any, is invoked on the handler |
| 5324 | if (op != null && op.callback != null) { |
| 5325 | Message msg = mBackupHandler.obtainMessage(MSG_OP_COMPLETE, op.callback); |
| 5326 | mBackupHandler.sendMessage(msg); |
| 5327 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 5328 | } |
| 5329 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5330 | // ----- Restore session ----- |
| 5331 | |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 5332 | class ActiveRestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5333 | private static final String TAG = "RestoreSession"; |
| 5334 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5335 | private String mPackageName; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5336 | private IBackupTransport mRestoreTransport = null; |
| 5337 | RestoreSet[] mRestoreSets = null; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5338 | boolean mEnded = false; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5339 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5340 | ActiveRestoreSession(String packageName, String transport) { |
| 5341 | mPackageName = packageName; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5342 | mRestoreTransport = getTransport(transport); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | // --- Binder interface --- |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5346 | public synchronized int getAvailableRestoreSets(IRestoreObserver observer) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 5347 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5348 | "getAvailableRestoreSets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5349 | if (observer == null) { |
| 5350 | throw new IllegalArgumentException("Observer must not be null"); |
| 5351 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5352 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5353 | if (mEnded) { |
| 5354 | throw new IllegalStateException("Restore session already ended"); |
| 5355 | } |
| 5356 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5357 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5358 | try { |
| Christopher Tate | 4338304 | 2009-07-13 15:17:13 -0700 | [diff] [blame] | 5359 | if (mRestoreTransport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5360 | Slog.w(TAG, "Null transport getting restore sets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5361 | return -1; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5362 | } |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5363 | // spin off the transport request to our service thread |
| 5364 | mWakelock.acquire(); |
| 5365 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS, |
| 5366 | new RestoreGetSetsParams(mRestoreTransport, this, observer)); |
| 5367 | mBackupHandler.sendMessage(msg); |
| 5368 | return 0; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5369 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5370 | Slog.e(TAG, "Error in getAvailableRestoreSets", e); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 5371 | return -1; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5372 | } finally { |
| 5373 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 5374 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5375 | } |
| 5376 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5377 | public synchronized int restoreAll(long token, IRestoreObserver observer) { |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5378 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5379 | "performRestore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5380 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5381 | if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token) |
| Christopher Tate | f2c321a | 2009-08-10 15:43:36 -0700 | [diff] [blame] | 5382 | + " observer=" + observer); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 5383 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5384 | if (mEnded) { |
| 5385 | throw new IllegalStateException("Restore session already ended"); |
| 5386 | } |
| 5387 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5388 | if (mRestoreTransport == null || mRestoreSets == null) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5389 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 5390 | return -1; |
| 5391 | } |
| 5392 | |
| 5393 | if (mPackageName != null) { |
| 5394 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5395 | return -1; |
| 5396 | } |
| 5397 | |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5398 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5399 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 5400 | if (token == mRestoreSets[i].token) { |
| 5401 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5402 | mWakelock.acquire(); |
| 5403 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5404 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, true); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 5405 | mBackupHandler.sendMessage(msg); |
| 5406 | Binder.restoreCallingIdentity(oldId); |
| 5407 | return 0; |
| 5408 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 5409 | } |
| 5410 | } |
| Christopher Tate | 0e0b4ae | 2009-08-10 16:13:47 -0700 | [diff] [blame] | 5411 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5412 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5413 | return -1; |
| 5414 | } |
| 5415 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 5416 | public synchronized int restoreSome(long token, IRestoreObserver observer, |
| 5417 | String[] packages) { |
| 5418 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 5419 | "performRestore"); |
| 5420 | |
| 5421 | if (DEBUG) { |
| 5422 | StringBuilder b = new StringBuilder(128); |
| 5423 | b.append("restoreSome token="); |
| 5424 | b.append(Long.toHexString(token)); |
| 5425 | b.append(" observer="); |
| 5426 | b.append(observer.toString()); |
| 5427 | b.append(" packages="); |
| 5428 | if (packages == null) { |
| 5429 | b.append("null"); |
| 5430 | } else { |
| 5431 | b.append('{'); |
| 5432 | boolean first = true; |
| 5433 | for (String s : packages) { |
| 5434 | if (!first) { |
| 5435 | b.append(", "); |
| 5436 | } else first = false; |
| 5437 | b.append(s); |
| 5438 | } |
| 5439 | b.append('}'); |
| 5440 | } |
| 5441 | Slog.d(TAG, b.toString()); |
| 5442 | } |
| 5443 | |
| 5444 | if (mEnded) { |
| 5445 | throw new IllegalStateException("Restore session already ended"); |
| 5446 | } |
| 5447 | |
| 5448 | if (mRestoreTransport == null || mRestoreSets == null) { |
| 5449 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 5450 | return -1; |
| 5451 | } |
| 5452 | |
| 5453 | if (mPackageName != null) { |
| 5454 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| 5455 | return -1; |
| 5456 | } |
| 5457 | |
| 5458 | synchronized (mQueueLock) { |
| 5459 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 5460 | if (token == mRestoreSets[i].token) { |
| 5461 | long oldId = Binder.clearCallingIdentity(); |
| 5462 | mWakelock.acquire(); |
| 5463 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 5464 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, |
| 5465 | packages, true); |
| 5466 | mBackupHandler.sendMessage(msg); |
| 5467 | Binder.restoreCallingIdentity(oldId); |
| 5468 | return 0; |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| 5474 | return -1; |
| 5475 | } |
| 5476 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5477 | public synchronized int restorePackage(String packageName, IRestoreObserver observer) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5478 | if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5479 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5480 | if (mEnded) { |
| 5481 | throw new IllegalStateException("Restore session already ended"); |
| 5482 | } |
| 5483 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5484 | if (mPackageName != null) { |
| 5485 | if (! mPackageName.equals(packageName)) { |
| 5486 | Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName |
| 5487 | + " on session for package " + mPackageName); |
| 5488 | return -1; |
| 5489 | } |
| 5490 | } |
| 5491 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5492 | PackageInfo app = null; |
| 5493 | try { |
| 5494 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 5495 | } catch (NameNotFoundException nnf) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5496 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5497 | return -1; |
| 5498 | } |
| 5499 | |
| 5500 | // If the caller is not privileged and is not coming from the target |
| 5501 | // app's uid, throw a permission exception back to the caller. |
| 5502 | int perm = mContext.checkPermission(android.Manifest.permission.BACKUP, |
| 5503 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 5504 | if ((perm == PackageManager.PERMISSION_DENIED) && |
| 5505 | (app.applicationInfo.uid != Binder.getCallingUid())) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5506 | Slog.w(TAG, "restorePackage: bad packageName=" + packageName |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5507 | + " or calling uid=" + Binder.getCallingUid()); |
| 5508 | throw new SecurityException("No permission to restore other packages"); |
| 5509 | } |
| 5510 | |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5511 | // If the package has no backup agent, we obviously cannot proceed |
| 5512 | if (app.applicationInfo.backupAgentName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5513 | Slog.w(TAG, "Asked to restore package " + packageName + " with no agent"); |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5514 | return -1; |
| 5515 | } |
| 5516 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5517 | // So far so good; we're allowed to try to restore this package. Now |
| 5518 | // check whether there is data for it in the current dataset, falling back |
| 5519 | // to the ancestral dataset if not. |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5520 | long token = getAvailableRestoreToken(packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5521 | |
| 5522 | // If we didn't come up with a place to look -- no ancestral dataset and |
| 5523 | // the app has never been backed up from this device -- there's nothing |
| 5524 | // to do but return failure. |
| 5525 | if (token == 0) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5526 | 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] | 5527 | return -1; |
| 5528 | } |
| 5529 | |
| 5530 | // Ready to go: enqueue the restore request and claim success |
| 5531 | long oldId = Binder.clearCallingIdentity(); |
| 5532 | mWakelock.acquire(); |
| 5533 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5534 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, app, 0, false); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5535 | mBackupHandler.sendMessage(msg); |
| 5536 | Binder.restoreCallingIdentity(oldId); |
| 5537 | return 0; |
| 5538 | } |
| 5539 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5540 | // Posted to the handler to tear down a restore session in a cleanly synchronized way |
| 5541 | class EndRestoreRunnable implements Runnable { |
| 5542 | BackupManagerService mBackupManager; |
| 5543 | ActiveRestoreSession mSession; |
| 5544 | |
| 5545 | EndRestoreRunnable(BackupManagerService manager, ActiveRestoreSession session) { |
| 5546 | mBackupManager = manager; |
| 5547 | mSession = session; |
| 5548 | } |
| 5549 | |
| 5550 | public void run() { |
| 5551 | // clean up the session's bookkeeping |
| 5552 | synchronized (mSession) { |
| 5553 | try { |
| 5554 | if (mSession.mRestoreTransport != null) { |
| 5555 | mSession.mRestoreTransport.finishRestore(); |
| 5556 | } |
| 5557 | } catch (Exception e) { |
| 5558 | Slog.e(TAG, "Error in finishRestore", e); |
| 5559 | } finally { |
| 5560 | mSession.mRestoreTransport = null; |
| 5561 | mSession.mEnded = true; |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | // clean up the BackupManagerService side of the bookkeeping |
| 5566 | // and cancel any pending timeout message |
| 5567 | mBackupManager.clearRestoreSession(mSession); |
| 5568 | } |
| 5569 | } |
| 5570 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5571 | public synchronized void endRestoreSession() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5572 | if (DEBUG) Slog.d(TAG, "endRestoreSession"); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 5573 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5574 | if (mEnded) { |
| 5575 | throw new IllegalStateException("Restore session already ended"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5576 | } |
| 5577 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5578 | mBackupHandler.post(new EndRestoreRunnable(BackupManagerService.this, this)); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5582 | @Override |
| 5583 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5584 | long identityToken = Binder.clearCallingIdentity(); |
| 5585 | try { |
| 5586 | dumpInternal(pw); |
| 5587 | } finally { |
| 5588 | Binder.restoreCallingIdentity(identityToken); |
| 5589 | } |
| 5590 | } |
| 5591 | |
| 5592 | private void dumpInternal(PrintWriter pw) { |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5593 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5594 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled") |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5595 | + " / " + (!mProvisioned ? "not " : "") + "provisioned / " |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 5596 | + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init"); |
| Christopher Tate | ae06ed9 | 2010-02-25 17:13:28 -0800 | [diff] [blame] | 5597 | pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled")); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5598 | pw.println("Last backup pass: " + mLastBackupPass |
| 5599 | + " (now = " + System.currentTimeMillis() + ')'); |
| 5600 | pw.println(" next scheduled: " + mNextBackupPass); |
| 5601 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5602 | pw.println("Available transports:"); |
| 5603 | for (String t : listAllTransports()) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5604 | pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t); |
| 5605 | try { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5606 | IBackupTransport transport = getTransport(t); |
| 5607 | File dir = new File(mBaseStateDir, transport.transportDirName()); |
| 5608 | pw.println(" destination: " + transport.currentDestinationString()); |
| 5609 | pw.println(" intent: " + transport.configurationIntent()); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5610 | for (File f : dir.listFiles()) { |
| 5611 | pw.println(" " + f.getName() + " - " + f.length() + " state bytes"); |
| 5612 | } |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5613 | } catch (Exception e) { |
| 5614 | Slog.e(TAG, "Error in transport", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5615 | pw.println(" Error: " + e); |
| 5616 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5617 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5618 | |
| 5619 | pw.println("Pending init: " + mPendingInits.size()); |
| 5620 | for (String s : mPendingInits) { |
| 5621 | pw.println(" " + s); |
| 5622 | } |
| 5623 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5624 | int N = mBackupParticipants.size(); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5625 | pw.println("Participants:"); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5626 | for (int i=0; i<N; i++) { |
| 5627 | int uid = mBackupParticipants.keyAt(i); |
| 5628 | pw.print(" uid: "); |
| 5629 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5630 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 5631 | for (ApplicationInfo app: participants) { |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5632 | pw.println(" " + app.packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5633 | } |
| 5634 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5635 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5636 | pw.println("Ancestral packages: " |
| 5637 | + (mAncestralPackages == null ? "none" : mAncestralPackages.size())); |
| Christopher Tate | 5923c97 | 2010-04-04 17:45:35 -0700 | [diff] [blame] | 5638 | if (mAncestralPackages != null) { |
| 5639 | for (String pkg : mAncestralPackages) { |
| 5640 | pw.println(" " + pkg); |
| 5641 | } |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5642 | } |
| 5643 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 5644 | pw.println("Ever backed up: " + mEverStoredApps.size()); |
| 5645 | for (String pkg : mEverStoredApps) { |
| 5646 | pw.println(" " + pkg); |
| 5647 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5648 | |
| 5649 | pw.println("Pending backup: " + mPendingBackups.size()); |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 5650 | for (BackupRequest req : mPendingBackups.values()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5651 | pw.println(" " + req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5652 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5653 | } |
| 5654 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 5655 | } |