| 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; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 21 | import android.app.IActivityManager; |
| 22 | import android.app.IApplicationThread; |
| 23 | import android.app.IBackupAgent; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 24 | import android.app.PendingIntent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 29 | import android.content.IntentFilter; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 30 | import android.content.ServiceConnection; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 32 | import android.content.pm.IPackageDataObserver; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager.NameNotFoundException; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 36 | import android.content.pm.Signature; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 37 | import android.net.Uri; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 39 | import android.os.Binder; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 40 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 41 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.IBinder; |
| 44 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 45 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 46 | import android.os.PowerManager; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 47 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.util.Log; |
| 50 | import android.util.SparseArray; |
| 51 | |
| 52 | import android.backup.IBackupManager; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 53 | import android.backup.IRestoreObserver; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 54 | import android.backup.IRestoreSession; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 55 | import android.backup.RestoreSet; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 56 | |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 57 | import com.android.internal.backup.LocalTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 58 | import com.android.internal.backup.IBackupTransport; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 59 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 60 | import com.android.server.PackageManagerBackupAgent; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 61 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 62 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 63 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 64 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 65 | import java.io.FileDescriptor; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 66 | import java.io.IOException; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 67 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 68 | import java.io.RandomAccessFile; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 69 | import java.lang.String; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 70 | import java.util.ArrayList; |
| 71 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 74 | import java.util.Map; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 75 | |
| 76 | class BackupManagerService extends IBackupManager.Stub { |
| 77 | private static final String TAG = "BackupManagerService"; |
| 78 | private static final boolean DEBUG = true; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 79 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 80 | // How often we perform a backup pass. Privileged external callers can |
| 81 | // trigger an immediate pass. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 82 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 83 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 84 | private static final String RUN_BACKUP_ACTION = "_backup_run_"; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 85 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 86 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 87 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 88 | private static final int MSG_RUN_CLEAR = 4; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 89 | |
| 90 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 91 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 92 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 93 | private Context mContext; |
| 94 | private PackageManager mPackageManager; |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 95 | private IActivityManager mActivityManager; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 96 | private PowerManager mPowerManager; |
| 97 | private AlarmManager mAlarmManager; |
| 98 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 99 | private boolean mEnabled; // access to this is synchronized on 'this' |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 100 | private PowerManager.WakeLock mWakelock; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 101 | private final BackupHandler mBackupHandler = new BackupHandler(); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 102 | private PendingIntent mRunBackupIntent; |
| 103 | private BroadcastReceiver mRunBackupReceiver; |
| 104 | private IntentFilter mRunBackupFilter; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 105 | // map UIDs to the set of backup client services within that UID's app set |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 106 | private final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 107 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 108 | // set of backup services that have pending changes |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 109 | private class BackupRequest { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 110 | public ApplicationInfo appInfo; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 111 | public boolean fullBackup; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 112 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 113 | BackupRequest(ApplicationInfo app, boolean isFull) { |
| 114 | appInfo = app; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 115 | fullBackup = isFull; |
| 116 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 117 | |
| 118 | public String toString() { |
| 119 | return "BackupRequest{app=" + appInfo + " full=" + fullBackup + "}"; |
| 120 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 121 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 122 | // Backups that we haven't started yet. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 123 | private HashMap<ApplicationInfo,BackupRequest> mPendingBackups |
| 124 | = new HashMap<ApplicationInfo,BackupRequest>(); |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 125 | |
| 126 | // Pseudoname that we use for the Package Manager metadata "package" |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 127 | private static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 128 | |
| 129 | // locking around the pending-backup management |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 130 | private final Object mQueueLock = new Object(); |
| 131 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 132 | // The thread performing the sequence of queued backups binds to each app's agent |
| 133 | // in succession. Bind notifications are asynchronously delivered through the |
| 134 | // Activity Manager; use this lock object to signal when a requested binding has |
| 135 | // completed. |
| 136 | private final Object mAgentConnectLock = new Object(); |
| 137 | private IBackupAgent mConnectedAgent; |
| 138 | private volatile boolean mConnecting; |
| 139 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 140 | // A similar synchronicity mechanism around clearing apps' data for restore |
| 141 | private final Object mClearDataLock = new Object(); |
| 142 | private volatile boolean mClearingData; |
| 143 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 144 | // Transport bookkeeping |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 145 | private final HashMap<String,IBackupTransport> mTransports |
| 146 | = new HashMap<String,IBackupTransport>(); |
| 147 | private String mCurrentTransport; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 148 | private IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 149 | private RestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 150 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 151 | private class RestoreParams { |
| 152 | public IBackupTransport transport; |
| 153 | public IRestoreObserver observer; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 154 | public long token; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 155 | |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 156 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 157 | transport = _transport; |
| 158 | observer = _obs; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 159 | token = _token; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 163 | private class ClearParams { |
| 164 | public IBackupTransport transport; |
| 165 | public PackageInfo packageInfo; |
| 166 | |
| 167 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 168 | transport = _transport; |
| 169 | packageInfo = _info; |
| 170 | } |
| 171 | } |
| 172 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 173 | // Where we keep our journal files and other bookkeeping |
| 174 | private File mBaseStateDir; |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 175 | private File mDataDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 176 | private File mJournalDir; |
| 177 | private File mJournal; |
| 178 | private RandomAccessFile mJournalStream; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 179 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 180 | public BackupManagerService(Context context) { |
| 181 | mContext = context; |
| 182 | mPackageManager = context.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 183 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 184 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 185 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 186 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 187 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 188 | // Set up our bookkeeping |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 189 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 190 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 191 | mBaseStateDir = new File(Environment.getDataDirectory(), "backup"); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 192 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 193 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 194 | mRunBackupReceiver = new RunBackupReceiver(); |
| 195 | mRunBackupFilter = new IntentFilter(); |
| 196 | mRunBackupFilter.addAction(RUN_BACKUP_ACTION); |
| 197 | context.registerReceiver(mRunBackupReceiver, mRunBackupFilter); |
| 198 | |
| 199 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
| 200 | // !!! TODO: restrict delivery to our receiver; the naive setClass() doesn't seem to work |
| 201 | //backupIntent.setClass(context, mRunBackupReceiver.getClass()); |
| 202 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 203 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 204 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 205 | // Set up the backup-request journaling |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 206 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 207 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 208 | makeJournalLocked(); // okay because no other threads are running yet |
| 209 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 210 | // Build our mapping of uid to backup client services. This implicitly |
| 211 | // schedules a backup pass on the Package Manager metadata the first |
| 212 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 213 | synchronized (mBackupParticipants) { |
| 214 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 217 | // Set up our transport options and initialize the default transport |
| 218 | // TODO: Have transports register themselves somehow? |
| 219 | // TODO: Don't create transports that we don't need to? |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 220 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 221 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 222 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 223 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 224 | mGoogleTransport = null; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 225 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 226 | Settings.Secure.BACKUP_TRANSPORT); |
| 227 | if ("".equals(mCurrentTransport)) { |
| 228 | mCurrentTransport = null; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 229 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 230 | if (DEBUG) Log.v(TAG, "Starting with transport " + mCurrentTransport); |
| 231 | |
| 232 | // Attach to the Google backup transport. When this comes up, it will set |
| 233 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 234 | // to null. |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 235 | Intent intent = new Intent().setComponent(new ComponentName( |
| 236 | "com.google.android.backup", |
| 237 | "com.google.android.backup.BackupTransportService")); |
| 238 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 239 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 240 | // Now that we know about valid backup participants, parse any |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 241 | // leftover journal files into the pending backup set |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 242 | parseLeftoverJournals(); |
| 243 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 244 | // Register for broadcasts about package install, etc., so we can |
| 245 | // update the provider list. |
| 246 | IntentFilter filter = new IntentFilter(); |
| 247 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 248 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 249 | filter.addDataScheme("package"); |
| 250 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 251 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 252 | // Power management |
| 253 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backup"); |
| 254 | |
| 255 | // Start the backup passes going |
| 256 | setBackupEnabled(areEnabled); |
| 257 | } |
| 258 | |
| 259 | private class RunBackupReceiver extends BroadcastReceiver { |
| 260 | public void onReceive(Context context, Intent intent) { |
| 261 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
| 262 | if (DEBUG) Log.v(TAG, "Running a backup pass"); |
| 263 | |
| 264 | synchronized (mQueueLock) { |
| 265 | // acquire a wakelock and pass it to the backup thread. it will |
| 266 | // be released once backup concludes. |
| 267 | mWakelock.acquire(); |
| 268 | |
| 269 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 270 | mBackupHandler.sendMessage(msg); |
| 271 | } |
| 272 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 273 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 274 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 275 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 276 | private void makeJournalLocked() { |
| 277 | try { |
| 278 | mJournal = File.createTempFile("journal", null, mJournalDir); |
| 279 | mJournalStream = new RandomAccessFile(mJournal, "rwd"); |
| 280 | } catch (IOException e) { |
| 281 | Log.e(TAG, "Unable to write backup journals"); |
| 282 | mJournal = null; |
| 283 | mJournalStream = null; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | private void parseLeftoverJournals() { |
| 288 | if (mJournal != null) { |
| 289 | File[] allJournals = mJournalDir.listFiles(); |
| 290 | for (File f : allJournals) { |
| 291 | if (f.compareTo(mJournal) != 0) { |
| 292 | // This isn't the current journal, so it must be a leftover. Read |
| 293 | // out the package names mentioned there and schedule them for |
| 294 | // backup. |
| 295 | try { |
| 296 | Log.i(TAG, "Found stale backup journal, scheduling:"); |
| 297 | RandomAccessFile in = new RandomAccessFile(f, "r"); |
| 298 | while (true) { |
| 299 | String packageName = in.readUTF(); |
| 300 | Log.i(TAG, " + " + packageName); |
| 301 | dataChanged(packageName); |
| 302 | } |
| 303 | } catch (EOFException e) { |
| 304 | // no more data; we're done |
| 305 | } catch (Exception e) { |
| 306 | // can't read it or other error; just skip it |
| 307 | } finally { |
| 308 | // close/delete the file |
| 309 | f.delete(); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 316 | // Add a transport to our set of available backends |
| 317 | private void registerTransport(String name, IBackupTransport transport) { |
| 318 | synchronized (mTransports) { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 319 | if (DEBUG) Log.v(TAG, "Registering transport " + name + " = " + transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 320 | mTransports.put(name, transport); |
| 321 | } |
| 322 | } |
| 323 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 324 | // ----- Track installation/removal of packages ----- |
| 325 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 326 | public void onReceive(Context context, Intent intent) { |
| 327 | if (DEBUG) Log.d(TAG, "Received broadcast " + intent); |
| 328 | |
| 329 | Uri uri = intent.getData(); |
| 330 | if (uri == null) { |
| 331 | return; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 332 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 333 | String pkgName = uri.getSchemeSpecificPart(); |
| 334 | if (pkgName == null) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | String action = intent.getAction(); |
| 339 | if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 340 | synchronized (mBackupParticipants) { |
| 341 | Bundle extras = intent.getExtras(); |
| 342 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 343 | // The package was just upgraded |
| 344 | updatePackageParticipantsLocked(pkgName); |
| 345 | } else { |
| 346 | // The package was just added |
| 347 | addPackageParticipantsLocked(pkgName); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 352 | Bundle extras = intent.getExtras(); |
| 353 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 354 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 355 | } else { |
| 356 | synchronized (mBackupParticipants) { |
| 357 | removePackageParticipantsLocked(pkgName); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | }; |
| 363 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 364 | // ----- Track connection to GoogleBackupTransport service ----- |
| 365 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 366 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 367 | if (DEBUG) Log.v(TAG, "Connected to Google transport"); |
| 368 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 369 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | public void onServiceDisconnected(ComponentName name) { |
| 373 | if (DEBUG) Log.v(TAG, "Disconnected from Google transport"); |
| 374 | mGoogleTransport = null; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 375 | registerTransport(name.flattenToShortString(), null); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 376 | } |
| 377 | }; |
| 378 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 379 | // ----- Run the actual backup process asynchronously ----- |
| 380 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 381 | private class BackupHandler extends Handler { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 382 | public void handleMessage(Message msg) { |
| 383 | |
| 384 | switch (msg.what) { |
| 385 | case MSG_RUN_BACKUP: |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 386 | { |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 387 | IBackupTransport transport = getTransport(mCurrentTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 388 | if (transport == null) { |
| 389 | Log.v(TAG, "Backup requested but no transport available"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 390 | mWakelock.release(); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 394 | // snapshot the pending-backup set and work on that |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 395 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 396 | File oldJournal = mJournal; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 397 | synchronized (mQueueLock) { |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 398 | // Do we have any work to do? |
| 399 | if (mPendingBackups.size() > 0) { |
| 400 | for (BackupRequest b: mPendingBackups.values()) { |
| 401 | queue.add(b); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 402 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 403 | Log.v(TAG, "clearing pending backups"); |
| 404 | mPendingBackups.clear(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 405 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 406 | // Start a new backup-queue journal file too |
| 407 | if (mJournalStream != null) { |
| 408 | try { |
| 409 | mJournalStream.close(); |
| 410 | } catch (IOException e) { |
| 411 | // don't need to do anything |
| 412 | } |
| 413 | makeJournalLocked(); |
| 414 | } |
| 415 | |
| 416 | // At this point, we have started a new journal file, and the old |
| 417 | // file identity is being passed to the backup processing thread. |
| 418 | // When it completes successfully, that old journal file will be |
| 419 | // deleted. If we crash prior to that, the old journal is parsed |
| 420 | // at next boot and the journaled requests fulfilled. |
| 421 | (new PerformBackupThread(transport, queue, oldJournal)).start(); |
| 422 | } else { |
| 423 | Log.v(TAG, "Backup requested but nothing pending"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 424 | mWakelock.release(); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 425 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 426 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 427 | break; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 428 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 429 | |
| 430 | case MSG_RUN_FULL_BACKUP: |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 431 | break; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 432 | |
| 433 | case MSG_RUN_RESTORE: |
| 434 | { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 435 | RestoreParams params = (RestoreParams)msg.obj; |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 436 | Log.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 437 | (new PerformRestoreThread(params.transport, params.observer, |
| 438 | params.token)).start(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 439 | break; |
| 440 | } |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 441 | |
| 442 | case MSG_RUN_CLEAR: |
| 443 | { |
| 444 | ClearParams params = (ClearParams)msg.obj; |
| 445 | (new PerformClearThread(params.transport, params.packageInfo)).start(); |
| 446 | break; |
| 447 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 448 | } |
| 449 | } |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 452 | // Add the backup agents in the given package to our set of known backup participants. |
| 453 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 454 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 455 | // Look for apps that define the android:backupAgent attribute |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 456 | if (DEBUG) Log.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 457 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 458 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 461 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 462 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 463 | if (DEBUG) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 464 | Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| 465 | for (PackageInfo p : targetPkgs) { |
| Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 466 | Log.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName |
| 467 | + " uid=" + p.applicationInfo.uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 471 | for (PackageInfo pkg : targetPkgs) { |
| 472 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 473 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 474 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 475 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 476 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 477 | mBackupParticipants.put(uid, set); |
| 478 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 479 | set.add(pkg.applicationInfo); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 480 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 484 | // Remove the given package's entry from our known active set. If |
| 485 | // 'packageName' is null, *all* participating apps will be removed. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 486 | void removePackageParticipantsLocked(String packageName) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 487 | if (DEBUG) Log.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 488 | List<PackageInfo> allApps = null; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 489 | if (packageName != null) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 490 | allApps = new ArrayList<PackageInfo>(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 491 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 492 | int flags = PackageManager.GET_SIGNATURES; |
| 493 | allApps.add(mPackageManager.getPackageInfo(packageName, flags)); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 494 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 495 | // just skip it (???) |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 496 | } |
| 497 | } else { |
| 498 | // all apps with agents |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 499 | allApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 500 | } |
| 501 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 504 | private void removePackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 505 | List<PackageInfo> agents) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 506 | if (DEBUG) { |
| 507 | Log.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| 508 | + ") removing " + agents.size() + " entries"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 509 | for (PackageInfo p : agents) { |
| 510 | Log.v(TAG, " - " + p); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 513 | for (PackageInfo pkg : agents) { |
| 514 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 515 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 516 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 517 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 518 | // Find the existing entry with the same package name, and remove it. |
| 519 | // We can't just remove(app) because the instances are different. |
| 520 | for (ApplicationInfo entry: set) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 521 | if (entry.packageName.equals(pkg.packageName)) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 522 | set.remove(entry); |
| 523 | break; |
| 524 | } |
| 525 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 526 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 527 | mBackupParticipants.delete(uid); |
| 528 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 534 | // Returns the set of all applications that define an android:backupAgent attribute |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 535 | private List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 536 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 537 | int flags = PackageManager.GET_SIGNATURES; |
| 538 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 539 | int N = packages.size(); |
| 540 | for (int a = N-1; a >= 0; a--) { |
| 541 | ApplicationInfo app = packages.get(a).applicationInfo; |
| 542 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| 543 | || app.backupAgentName == null) { |
| 544 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 547 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 548 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 549 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 550 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 551 | // action cannot be passed a null package name. |
| 552 | void updatePackageParticipantsLocked(String packageName) { |
| 553 | if (packageName == null) { |
| 554 | Log.e(TAG, "updatePackageParticipants called with null package name"); |
| 555 | return; |
| 556 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 557 | if (DEBUG) Log.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 558 | |
| 559 | // brute force but small code size |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 560 | List<PackageInfo> allApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 561 | removePackageParticipantsLockedInner(packageName, allApps); |
| 562 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 565 | // Return the given transport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 566 | private IBackupTransport getTransport(String transportName) { |
| 567 | synchronized (mTransports) { |
| 568 | IBackupTransport transport = mTransports.get(transportName); |
| 569 | if (transport == null) { |
| 570 | Log.w(TAG, "Requested unavailable transport: " + transportName); |
| 571 | } |
| 572 | return transport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 573 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 576 | // fire off a backup agent, blocking until it attaches or times out |
| 577 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 578 | IBackupAgent agent = null; |
| 579 | synchronized(mAgentConnectLock) { |
| 580 | mConnecting = true; |
| 581 | mConnectedAgent = null; |
| 582 | try { |
| 583 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| 584 | Log.d(TAG, "awaiting agent for " + app); |
| 585 | |
| 586 | // success; wait for the agent to arrive |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 587 | // only wait 10 seconds for the clear data to happen |
| 588 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 589 | while (mConnecting && mConnectedAgent == null |
| 590 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 591 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 592 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 593 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 594 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 595 | return null; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // if we timed out with no connect, abort and move on |
| 600 | if (mConnecting == true) { |
| 601 | Log.w(TAG, "Timeout waiting for agent " + app); |
| 602 | return null; |
| 603 | } |
| 604 | agent = mConnectedAgent; |
| 605 | } |
| 606 | } catch (RemoteException e) { |
| 607 | // can't happen |
| 608 | } |
| 609 | } |
| 610 | return agent; |
| 611 | } |
| 612 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 613 | // clear an application's data, blocking until the operation completes or times out |
| 614 | void clearApplicationDataSynchronous(String packageName) { |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 615 | // Don't wipe packages marked allowClearUserData=false |
| 616 | try { |
| 617 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 618 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| 619 | if (DEBUG) Log.i(TAG, "allowClearUserData=false so not wiping " |
| 620 | + packageName); |
| 621 | return; |
| 622 | } |
| 623 | } catch (NameNotFoundException e) { |
| 624 | Log.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| 625 | return; |
| 626 | } |
| 627 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 628 | ClearDataObserver observer = new ClearDataObserver(); |
| 629 | |
| 630 | synchronized(mClearDataLock) { |
| 631 | mClearingData = true; |
| 632 | mPackageManager.clearApplicationUserData(packageName, observer); |
| 633 | |
| 634 | // only wait 10 seconds for the clear data to happen |
| 635 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 636 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 637 | try { |
| 638 | mClearDataLock.wait(5000); |
| 639 | } catch (InterruptedException e) { |
| 640 | // won't happen, but still. |
| 641 | mClearingData = false; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| 648 | public void onRemoveCompleted(String packageName, boolean succeeded) |
| 649 | throws android.os.RemoteException { |
| 650 | synchronized(mClearDataLock) { |
| 651 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 652 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 657 | // ----- Back up a set of applications via a worker thread ----- |
| 658 | |
| 659 | class PerformBackupThread extends Thread { |
| 660 | private static final String TAG = "PerformBackupThread"; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 661 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 662 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 663 | File mStateDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 664 | File mJournal; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 665 | |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 666 | public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 667 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 668 | mTransport = transport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 669 | mQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 670 | mJournal = journal; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 671 | |
| 672 | try { |
| 673 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 674 | } catch (RemoteException e) { |
| 675 | // can't happen; the transport is local |
| 676 | } |
| 677 | mStateDir.mkdirs(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public void run() { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 682 | if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| 683 | |
| Christopher Tate | 7958834 | 2009-06-30 16:11:49 -0700 | [diff] [blame] | 684 | // Backups run at background priority |
| 685 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 686 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 687 | // The package manager doesn't have a proper <application> etc, but since |
| 688 | // it's running here in the system process we can just set up its agent |
| 689 | // directly and use a synthetic BackupRequest. We always run this pass |
| 690 | // because it's cheap and this way we guarantee that we don't get out of |
| 691 | // step even if we're selecting among various transports at run time. |
| 692 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 693 | mPackageManager, allAgentPackages()); |
| 694 | BackupRequest pmRequest = new BackupRequest(new ApplicationInfo(), false); |
| 695 | pmRequest.appInfo.packageName = PACKAGE_MANAGER_SENTINEL; |
| 696 | processOneBackup(pmRequest, |
| 697 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), |
| 698 | mTransport); |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 699 | |
| 700 | // Now run all the backups in our queue |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 701 | doQueuedBackups(mTransport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 702 | |
| 703 | // Finally, tear down the transport |
| 704 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 705 | if (!mTransport.finishBackup()) { |
| 706 | // STOPSHIP TODO: handle errors |
| 707 | Log.e(TAG, "Backup failure in finishBackup()"); |
| 708 | } |
| 709 | } catch (RemoteException e) { |
| 710 | Log.e(TAG, "Error in finishBackup()", e); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 711 | } |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 712 | |
| 713 | if (!mJournal.delete()) { |
| 714 | Log.e(TAG, "Unable to remove backup journal file " + mJournal.getAbsolutePath()); |
| 715 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 716 | |
| 717 | // Only once we're entirely finished do we release the wakelock |
| 718 | mWakelock.release(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | private void doQueuedBackups(IBackupTransport transport) { |
| 722 | for (BackupRequest request : mQueue) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 723 | Log.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 724 | |
| 725 | IBackupAgent agent = null; |
| 726 | int mode = (request.fullBackup) |
| 727 | ? IApplicationThread.BACKUP_MODE_FULL |
| 728 | : IApplicationThread.BACKUP_MODE_INCREMENTAL; |
| 729 | try { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 730 | agent = bindToAgentSynchronous(request.appInfo, mode); |
| 731 | if (agent != null) { |
| 732 | processOneBackup(request, agent, transport); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 733 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 734 | |
| 735 | // unbind even on timeout, just in case |
| 736 | mActivityManager.unbindBackupAgent(request.appInfo); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 737 | } catch (SecurityException ex) { |
| 738 | // Try for the next one. |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 739 | Log.d(TAG, "error in bind/backup", ex); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 740 | } catch (RemoteException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 741 | Log.v(TAG, "bind/backup threw"); |
| 742 | e.printStackTrace(); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 743 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 744 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 747 | |
| 748 | void processOneBackup(BackupRequest request, IBackupAgent agent, IBackupTransport transport) { |
| 749 | final String packageName = request.appInfo.packageName; |
| 750 | Log.d(TAG, "processOneBackup doBackup() on " + packageName); |
| 751 | |
| 752 | try { |
| 753 | // Look up the package info & signatures. This is first so that if it |
| 754 | // throws an exception, there's no file setup yet that would need to |
| 755 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 756 | PackageInfo packInfo; |
| 757 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 758 | // The metadata 'package' is synthetic |
| 759 | packInfo = new PackageInfo(); |
| 760 | packInfo.packageName = packageName; |
| 761 | } else { |
| 762 | packInfo = mPackageManager.getPackageInfo(packageName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 763 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 764 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 765 | |
| 766 | // !!! TODO: get the state file dir from the transport |
| 767 | File savedStateName = new File(mStateDir, packageName); |
| 768 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 769 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 770 | |
| 771 | // In a full backup, we pass a null ParcelFileDescriptor as |
| 772 | // the saved-state "file" |
| 773 | ParcelFileDescriptor savedState = (request.fullBackup) ? null |
| 774 | : ParcelFileDescriptor.open(savedStateName, |
| 775 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 776 | ParcelFileDescriptor.MODE_CREATE); |
| 777 | |
| 778 | backupDataName.delete(); |
| 779 | ParcelFileDescriptor backupData = |
| 780 | ParcelFileDescriptor.open(backupDataName, |
| 781 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 782 | ParcelFileDescriptor.MODE_CREATE); |
| 783 | |
| 784 | newStateName.delete(); |
| 785 | ParcelFileDescriptor newState = |
| 786 | ParcelFileDescriptor.open(newStateName, |
| 787 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 788 | ParcelFileDescriptor.MODE_CREATE); |
| 789 | |
| 790 | // Run the target's backup pass |
| 791 | boolean success = false; |
| 792 | try { |
| 793 | agent.doBackup(savedState, backupData, newState); |
| 794 | success = true; |
| 795 | } finally { |
| 796 | if (savedState != null) { |
| 797 | savedState.close(); |
| 798 | } |
| 799 | backupData.close(); |
| 800 | newState.close(); |
| 801 | } |
| 802 | |
| 803 | // Now propagate the newly-backed-up data to the transport |
| 804 | if (success) { |
| 805 | if (DEBUG) Log.v(TAG, "doBackup() success; calling transport"); |
| 806 | backupData = |
| 807 | ParcelFileDescriptor.open(backupDataName, ParcelFileDescriptor.MODE_READ_ONLY); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 808 | if (!transport.performBackup(packInfo, backupData)) { |
| 809 | // STOPSHIP TODO: handle errors |
| 810 | Log.e(TAG, "Backup failure in performBackup()"); |
| 811 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 812 | |
| 813 | // !!! TODO: After successful transport, delete the now-stale data |
| 814 | // and juggle the files so that next time the new state is passed |
| 815 | //backupDataName.delete(); |
| 816 | newStateName.renameTo(savedStateName); |
| 817 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 818 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 819 | Log.e(TAG, "Error backing up " + packageName, e); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 824 | |
| 825 | // ----- Restore handling ----- |
| 826 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 827 | private boolean signaturesMatch(Signature[] storedSigs, Signature[] deviceSigs) { |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 828 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 829 | // !!! TODO: is this the right policy? |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 830 | if (DEBUG) Log.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| 831 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 832 | if ((storedSigs == null || storedSigs.length == 0) |
| 833 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 834 | return true; |
| 835 | } |
| 836 | if (storedSigs == null || deviceSigs == null) { |
| 837 | return false; |
| 838 | } |
| 839 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 840 | // !!! TODO: this demands that every stored signature match one |
| 841 | // that is present on device, and does not demand the converse. |
| 842 | // Is this this right policy? |
| 843 | int nStored = storedSigs.length; |
| 844 | int nDevice = deviceSigs.length; |
| 845 | |
| 846 | for (int i=0; i < nStored; i++) { |
| 847 | boolean match = false; |
| 848 | for (int j=0; j < nDevice; j++) { |
| 849 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 850 | match = true; |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | if (!match) { |
| 855 | return false; |
| 856 | } |
| 857 | } |
| 858 | return true; |
| 859 | } |
| 860 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 861 | class PerformRestoreThread extends Thread { |
| 862 | private IBackupTransport mTransport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 863 | private IRestoreObserver mObserver; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 864 | private long mToken; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 865 | private RestoreSet mImage; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 866 | private File mStateDir; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 867 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 868 | class RestoreRequest { |
| 869 | public PackageInfo app; |
| 870 | public int storedAppVersion; |
| 871 | |
| 872 | RestoreRequest(PackageInfo _app, int _version) { |
| 873 | app = _app; |
| 874 | storedAppVersion = _version; |
| 875 | } |
| 876 | } |
| 877 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 878 | PerformRestoreThread(IBackupTransport transport, IRestoreObserver observer, |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 879 | long restoreSetToken) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 880 | mTransport = transport; |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 881 | Log.d(TAG, "PerformRestoreThread mObserver=" + mObserver); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 882 | mObserver = observer; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 883 | mToken = restoreSetToken; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 884 | |
| 885 | try { |
| 886 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 887 | } catch (RemoteException e) { |
| 888 | // can't happen; the transport is local |
| 889 | } |
| 890 | mStateDir.mkdirs(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | @Override |
| 894 | public void run() { |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 895 | if (DEBUG) Log.v(TAG, "Beginning restore process mTransport=" + mTransport |
| 896 | + " mObserver=" + mObserver + " mToken=" + mToken); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 897 | /** |
| 898 | * Restore sequence: |
| 899 | * |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 900 | * 1. get the restore set description for our identity |
| 901 | * 2. for each app in the restore set: |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 902 | * 3.a. if it's restorable on this device, add it to the restore queue |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 903 | * 3. for each app in the restore queue: |
| 904 | * 3.a. clear the app data |
| 905 | * 3.b. get the restore data for the app from the transport |
| 906 | * 3.c. launch the backup agent for the app |
| 907 | * 3.d. agent.doRestore() with the data from the server |
| 908 | * 3.e. unbind the agent [and kill the app?] |
| 909 | * 4. shut down the transport |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 910 | */ |
| 911 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 912 | int error = -1; // assume error |
| 913 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 914 | // build the set of apps to restore |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 915 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 916 | RestoreSet[] images = mTransport.getAvailableRestoreSets(); |
| 917 | if (images == null) { |
| 918 | // STOPSHIP TODO: Handle the failure somehow? |
| 919 | Log.e(TAG, "Error getting restore sets"); |
| 920 | return; |
| 921 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 922 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 923 | if (images.length == 0) { |
| 924 | Log.i(TAG, "No restore sets available"); |
| 925 | return; |
| 926 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 927 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 928 | mImage = images[0]; |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 929 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 930 | // Get the list of all packages which have backup enabled. |
| 931 | // (Include the Package Manager metadata pseudo-package first.) |
| 932 | ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>(); |
| 933 | PackageInfo omPackage = new PackageInfo(); |
| 934 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 935 | restorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 936 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 937 | List<PackageInfo> agentPackages = allAgentPackages(); |
| 938 | restorePackages.addAll(agentPackages); |
| 939 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 940 | // let the observer know that we're running |
| 941 | if (mObserver != null) { |
| 942 | try { |
| 943 | // !!! TODO: get an actual count from the transport after |
| 944 | // its startRestore() runs? |
| 945 | mObserver.restoreStarting(restorePackages.size()); |
| 946 | } catch (RemoteException e) { |
| 947 | Log.d(TAG, "Restore observer died at restoreStarting"); |
| 948 | mObserver = null; |
| 949 | } |
| 950 | } |
| 951 | |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 952 | if (!mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0]))) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 953 | // STOPSHIP TODO: Handle the failure somehow? |
| 954 | Log.e(TAG, "Error starting restore operation"); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | String packageName = mTransport.nextRestorePackage(); |
| 959 | if (packageName == null) { |
| 960 | // STOPSHIP TODO: Handle the failure somehow? |
| 961 | Log.e(TAG, "Error getting first restore package"); |
| 962 | return; |
| 963 | } else if (packageName.equals("")) { |
| 964 | Log.i(TAG, "No restore data available"); |
| 965 | return; |
| 966 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 967 | Log.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| 968 | + "\", found only \"" + packageName + "\""); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | // Pull the Package Manager metadata from the restore set first |
| 973 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 974 | mPackageManager, agentPackages); |
| 975 | processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind())); |
| 976 | |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 977 | // Verify that the backup set includes metadata. If not, we can't do |
| 978 | // signature/version verification etc, so we simply do not proceed with |
| 979 | // the restore operation. |
| 980 | Metadata pmMeta = pmAgent.getRestoredMetadata(packageName); |
| 981 | if (pmMeta == null) { |
| 982 | Log.i(TAG, "No restore metadata available, so not restoring settings"); |
| 983 | return; |
| 984 | } |
| 985 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 986 | int count = 0; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 987 | for (;;) { |
| 988 | packageName = mTransport.nextRestorePackage(); |
| 989 | if (packageName == null) { |
| 990 | // STOPSHIP TODO: Handle the failure somehow? |
| 991 | Log.e(TAG, "Error getting next restore package"); |
| 992 | return; |
| 993 | } else if (packageName.equals("")) { |
| 994 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 995 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 996 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 997 | if (mObserver != null) { |
| 998 | ++count; |
| 999 | try { |
| 1000 | mObserver.onUpdate(count); |
| 1001 | } catch (RemoteException e) { |
| 1002 | Log.d(TAG, "Restore observer died in onUpdate"); |
| 1003 | mObserver = null; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1007 | Metadata metaInfo = pmAgent.getRestoredMetadata(packageName); |
| 1008 | if (metaInfo == null) { |
| 1009 | Log.e(TAG, "Missing metadata for " + packageName); |
| 1010 | continue; |
| 1011 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1012 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1013 | int flags = PackageManager.GET_SIGNATURES; |
| 1014 | PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 1015 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| 1016 | Log.w(TAG, "Package " + packageName |
| 1017 | + " restore version [" + metaInfo.versionCode |
| 1018 | + "] is too new for installed version [" |
| 1019 | + packageInfo.versionCode + "]"); |
| 1020 | continue; |
| 1021 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1022 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1023 | if (!signaturesMatch(metaInfo.signatures, packageInfo.signatures)) { |
| 1024 | Log.w(TAG, "Signature mismatch restoring " + packageName); |
| 1025 | continue; |
| 1026 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1027 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1028 | if (DEBUG) Log.v(TAG, "Package " + packageName |
| 1029 | + " restore version [" + metaInfo.versionCode |
| 1030 | + "] is compatible with installed version [" |
| 1031 | + packageInfo.versionCode + "]"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1032 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1033 | // Now perform the actual restore |
| 1034 | clearApplicationDataSynchronous(packageName); |
| 1035 | IBackupAgent agent = bindToAgentSynchronous( |
| 1036 | packageInfo.applicationInfo, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1037 | IApplicationThread.BACKUP_MODE_RESTORE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1038 | if (agent == null) { |
| 1039 | Log.w(TAG, "Can't find backup agent for " + packageName); |
| 1040 | continue; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1043 | try { |
| 1044 | processOneRestore(packageInfo, metaInfo.versionCode, agent); |
| 1045 | } finally { |
| 1046 | // unbind even on timeout or failure, just in case |
| 1047 | mActivityManager.unbindBackupAgent(packageInfo.applicationInfo); |
| 1048 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1049 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1050 | |
| 1051 | // if we get this far, report success to the observer |
| 1052 | error = 0; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1053 | } catch (NameNotFoundException e) { |
| 1054 | // STOPSHIP TODO: Handle the failure somehow? |
| 1055 | Log.e(TAG, "Invalid paackage restoring data", e); |
| 1056 | } catch (RemoteException e) { |
| 1057 | // STOPSHIP TODO: Handle the failure somehow? |
| 1058 | Log.e(TAG, "Error restoring data", e); |
| 1059 | } finally { |
| 1060 | try { |
| 1061 | mTransport.finishRestore(); |
| 1062 | } catch (RemoteException e) { |
| 1063 | Log.e(TAG, "Error finishing restore", e); |
| 1064 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1065 | |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1066 | Log.d(TAG, "finishing restore mObserver=" + mObserver); |
| 1067 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1068 | if (mObserver != null) { |
| 1069 | try { |
| 1070 | mObserver.restoreFinished(error); |
| 1071 | } catch (RemoteException e) { |
| 1072 | Log.d(TAG, "Restore observer died at restoreFinished"); |
| 1073 | } |
| 1074 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1075 | |
| 1076 | // done; we can finally release the wakelock |
| 1077 | mWakelock.release(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1081 | // Do the guts of a restore of one application, using mTransport.getRestoreData(). |
| 1082 | void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1083 | // !!! TODO: actually run the restore through mTransport |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1084 | final String packageName = app.packageName; |
| 1085 | |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1086 | Log.d(TAG, "processOneRestore packageName=" + packageName); |
| 1087 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1088 | // !!! TODO: get the dirs from the transport |
| 1089 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
| 1090 | backupDataName.delete(); |
| 1091 | try { |
| 1092 | ParcelFileDescriptor backupData = |
| 1093 | ParcelFileDescriptor.open(backupDataName, |
| 1094 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1095 | ParcelFileDescriptor.MODE_CREATE); |
| 1096 | |
| 1097 | // Run the transport's restore pass |
| 1098 | // Run the target's backup pass |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1099 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1100 | if (!mTransport.getRestoreData(backupData)) { |
| 1101 | // STOPSHIP TODO: Handle this error somehow? |
| 1102 | Log.e(TAG, "Error getting restore data for " + packageName); |
| 1103 | return; |
| 1104 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1105 | } finally { |
| 1106 | backupData.close(); |
| 1107 | } |
| 1108 | |
| 1109 | // Okay, we have the data. Now have the agent do the restore. |
| 1110 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 1111 | ParcelFileDescriptor newState = |
| 1112 | ParcelFileDescriptor.open(newStateName, |
| 1113 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1114 | ParcelFileDescriptor.MODE_CREATE); |
| 1115 | |
| 1116 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1117 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 1118 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1119 | try { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1120 | agent.doRestore(backupData, appVersionCode, newState); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1121 | } finally { |
| 1122 | newState.close(); |
| 1123 | backupData.close(); |
| 1124 | } |
| 1125 | |
| 1126 | // if everything went okay, remember the recorded state now |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1127 | File savedStateName = new File(mStateDir, packageName); |
| 1128 | newStateName.renameTo(savedStateName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1129 | } catch (Exception e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1130 | Log.e(TAG, "Error restoring data for " + packageName, e); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1131 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1135 | class PerformClearThread extends Thread { |
| 1136 | IBackupTransport mTransport; |
| 1137 | PackageInfo mPackage; |
| 1138 | |
| 1139 | PerformClearThread(IBackupTransport transport, PackageInfo packageInfo) { |
| 1140 | mTransport = transport; |
| 1141 | mPackage = packageInfo; |
| 1142 | } |
| 1143 | |
| 1144 | @Override |
| 1145 | public void run() { |
| 1146 | try { |
| 1147 | // Clear the on-device backup state to ensure a full backup next time |
| 1148 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 1149 | File stateFile = new File(stateDir, mPackage.packageName); |
| 1150 | stateFile.delete(); |
| 1151 | |
| 1152 | // Tell the transport to remove all the persistent storage for the app |
| 1153 | mTransport.clearBackupData(mPackage); |
| 1154 | } catch (RemoteException e) { |
| 1155 | // can't happen; the transport is local |
| 1156 | } finally { |
| 1157 | try { |
| 1158 | mTransport.finishBackup(); |
| 1159 | } catch (RemoteException e) { |
| 1160 | // can't happen; the transport is local |
| 1161 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1162 | |
| 1163 | // Last but not least, release the cpu |
| 1164 | mWakelock.release(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1169 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1170 | // ----- IBackupManager binder interface ----- |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1171 | |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 1172 | public void dataChanged(String packageName) throws RemoteException { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1173 | // Record that we need a backup pass for the caller. Since multiple callers |
| 1174 | // may share a uid, we need to note all candidates within that uid and schedule |
| 1175 | // a backup pass for each of them. |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1176 | |
| 1177 | Log.d(TAG, "dataChanged packageName=" + packageName); |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 1178 | |
| 1179 | // If the caller does not hold the BACKUP permission, it can only request a |
| 1180 | // backup of its own data. |
| 1181 | HashSet<ApplicationInfo> targets; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1182 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 1183 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 1184 | targets = mBackupParticipants.get(Binder.getCallingUid()); |
| 1185 | } else { |
| 1186 | // a caller with full permission can ask to back up any participating app |
| 1187 | // !!! TODO: allow backup of ANY app? |
| 1188 | if (DEBUG) Log.v(TAG, "Privileged caller, allowing backup of other apps"); |
| 1189 | targets = new HashSet<ApplicationInfo>(); |
| 1190 | int N = mBackupParticipants.size(); |
| 1191 | for (int i = 0; i < N; i++) { |
| 1192 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 1193 | if (s != null) { |
| 1194 | targets.addAll(s); |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1198 | if (targets != null) { |
| 1199 | synchronized (mQueueLock) { |
| 1200 | // Note that this client has made data changes that need to be backed up |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1201 | for (ApplicationInfo app : targets) { |
| Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 1202 | // validate the caller-supplied package name against the known set of |
| 1203 | // packages associated with this uid |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1204 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1205 | // Add the caller to the set of pending backups. If there is |
| 1206 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1207 | BackupRequest req = new BackupRequest(app, false); |
| 1208 | mPendingBackups.put(app, req); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1209 | |
| 1210 | // Journal this request in case of crash |
| 1211 | writeToJournalLocked(packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1215 | if (DEBUG) { |
| 1216 | int numKeys = mPendingBackups.size(); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1217 | Log.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1218 | for (BackupRequest b : mPendingBackups.values()) { |
| 1219 | Log.d(TAG, " + " + b + " agent=" + b.appInfo.backupAgentName); |
| 1220 | } |
| 1221 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1222 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1223 | } else { |
| 1224 | Log.w(TAG, "dataChanged but no participant pkg " + packageName); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1227 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1228 | private void writeToJournalLocked(String str) { |
| 1229 | if (mJournalStream != null) { |
| 1230 | try { |
| 1231 | mJournalStream.writeUTF(str); |
| 1232 | } catch (IOException e) { |
| 1233 | Log.e(TAG, "Error writing to backup journal"); |
| 1234 | mJournalStream = null; |
| 1235 | mJournal = null; |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1240 | // Clear the given package's backup data from the current transport |
| 1241 | public void clearBackupData(String packageName) { |
| 1242 | if (DEBUG) Log.v(TAG, "clearBackupData() of " + packageName); |
| 1243 | PackageInfo info; |
| 1244 | try { |
| 1245 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 1246 | } catch (NameNotFoundException e) { |
| 1247 | Log.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | // If the caller does not hold the BACKUP permission, it can only request a |
| 1252 | // wipe of its own backed-up data. |
| 1253 | HashSet<ApplicationInfo> apps; |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 1254 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1255 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 1256 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 1257 | } else { |
| 1258 | // a caller with full permission can ask to back up any participating app |
| 1259 | // !!! TODO: allow data-clear of ANY app? |
| 1260 | if (DEBUG) Log.v(TAG, "Privileged caller, allowing clear of other apps"); |
| 1261 | apps = new HashSet<ApplicationInfo>(); |
| 1262 | int N = mBackupParticipants.size(); |
| 1263 | for (int i = 0; i < N; i++) { |
| 1264 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 1265 | if (s != null) { |
| 1266 | apps.addAll(s); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | // now find the given package in the set of candidate apps |
| 1272 | for (ApplicationInfo app : apps) { |
| 1273 | if (app.packageName.equals(packageName)) { |
| 1274 | if (DEBUG) Log.v(TAG, "Found the app - running clear process"); |
| 1275 | // found it; fire off the clear request |
| 1276 | synchronized (mQueueLock) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1277 | mWakelock.acquire(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 1278 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 1279 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 1280 | mBackupHandler.sendMessage(msg); |
| 1281 | } |
| 1282 | break; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1287 | // Run a backup pass immediately for any applications that have declared |
| 1288 | // that they have pending updates. |
| 1289 | public void backupNow() throws RemoteException { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1290 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "backupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1291 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1292 | if (DEBUG) Log.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1293 | synchronized (mQueueLock) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1294 | try { |
| 1295 | if (DEBUG) Log.v(TAG, "sending immediate backup broadcast"); |
| 1296 | mRunBackupIntent.send(); |
| 1297 | } catch (PendingIntent.CanceledException e) { |
| 1298 | // should never happen |
| 1299 | Log.e(TAG, "run-backup intent cancelled!"); |
| 1300 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1303 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1304 | // Enable/disable the backup transport |
| 1305 | public void setBackupEnabled(boolean enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1306 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1307 | "setBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1308 | |
| 1309 | boolean wasEnabled = mEnabled; |
| 1310 | synchronized (this) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1311 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 1312 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1313 | mEnabled = enable; |
| 1314 | } |
| 1315 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 1316 | synchronized (mQueueLock) { |
| 1317 | if (enable && !wasEnabled) { |
| 1318 | // if we've just been enabled, start scheduling backup passes |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1319 | long when = System.currentTimeMillis() + BACKUP_INTERVAL; |
| 1320 | mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, when, |
| 1321 | BACKUP_INTERVAL, mRunBackupIntent); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 1322 | } else if (!enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1323 | // No longer enabled, so stop running backups |
| 1324 | mAlarmManager.cancel(mRunBackupIntent); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 1327 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1328 | |
| 1329 | // Report whether the backup mechanism is currently enabled |
| 1330 | public boolean isBackupEnabled() { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1331 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1332 | return mEnabled; // no need to synchronize just to read it |
| 1333 | } |
| 1334 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1335 | // Report the name of the currently active transport |
| 1336 | public String getCurrentTransport() { |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 1337 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, |
| 1338 | "getCurrentTransport"); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1339 | Log.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1340 | return mCurrentTransport; |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1343 | // Report all known, available backup transports |
| 1344 | public String[] listAllTransports() { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 1345 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1346 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1347 | String[] list = null; |
| 1348 | ArrayList<String> known = new ArrayList<String>(); |
| 1349 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 1350 | if (entry.getValue() != null) { |
| 1351 | known.add(entry.getKey()); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | if (known.size() > 0) { |
| 1356 | list = new String[known.size()]; |
| 1357 | known.toArray(list); |
| 1358 | } |
| 1359 | return list; |
| 1360 | } |
| 1361 | |
| 1362 | // Select which transport to use for the next backup operation. If the given |
| 1363 | // name is not one of the available transports, no action is taken and the method |
| 1364 | // returns null. |
| 1365 | public String selectBackupTransport(String transport) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1366 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1367 | |
| 1368 | synchronized (mTransports) { |
| 1369 | String prevTransport = null; |
| 1370 | if (mTransports.get(transport) != null) { |
| 1371 | prevTransport = mCurrentTransport; |
| 1372 | mCurrentTransport = transport; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1373 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1374 | Settings.Secure.BACKUP_TRANSPORT, transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1375 | Log.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| 1376 | + " returning " + prevTransport); |
| 1377 | } else { |
| 1378 | Log.w(TAG, "Attempt to select unavailable transport " + transport); |
| 1379 | } |
| 1380 | return prevTransport; |
| 1381 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | // Callback: a requested backup agent has been instantiated. This should only |
| 1385 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1386 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1387 | synchronized(mAgentConnectLock) { |
| 1388 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1389 | Log.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| 1390 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 1391 | mConnectedAgent = agent; |
| 1392 | mConnecting = false; |
| 1393 | } else { |
| 1394 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 1395 | + " claiming agent connected"); |
| 1396 | } |
| 1397 | mAgentConnectLock.notifyAll(); |
| 1398 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 1402 | // 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] | 1403 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1404 | public void agentDisconnected(String packageName) { |
| 1405 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1406 | synchronized(mAgentConnectLock) { |
| 1407 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1408 | mConnectedAgent = null; |
| 1409 | mConnecting = false; |
| 1410 | } else { |
| 1411 | Log.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| 1412 | + " claiming agent disconnected"); |
| 1413 | } |
| 1414 | mAgentConnectLock.notifyAll(); |
| 1415 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1416 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1417 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1418 | // Hand off a restore session |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1419 | public IRestoreSession beginRestoreSession(String transport) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1420 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "beginRestoreSession"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1421 | |
| 1422 | synchronized(this) { |
| 1423 | if (mActiveRestoreSession != null) { |
| 1424 | Log.d(TAG, "Restore session requested but one already active"); |
| 1425 | return null; |
| 1426 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1427 | mActiveRestoreSession = new RestoreSession(transport); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1428 | } |
| 1429 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1430 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1431 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1432 | // ----- Restore session ----- |
| 1433 | |
| 1434 | class RestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1435 | private static final String TAG = "RestoreSession"; |
| 1436 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1437 | private IBackupTransport mRestoreTransport = null; |
| 1438 | RestoreSet[] mRestoreSets = null; |
| 1439 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1440 | RestoreSession(String transport) { |
| 1441 | mRestoreTransport = getTransport(transport); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | // --- Binder interface --- |
| 1445 | public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1446 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1447 | "getAvailableRestoreSets"); |
| 1448 | |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1449 | try { |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1450 | synchronized(this) { |
| 1451 | if (mRestoreSets == null) { |
| 1452 | mRestoreSets = mRestoreTransport.getAvailableRestoreSets(); |
| 1453 | } |
| 1454 | return mRestoreSets; |
| 1455 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1456 | } catch (RuntimeException e) { |
| 1457 | Log.d(TAG, "getAvailableRestoreSets exception"); |
| 1458 | e.printStackTrace(); |
| 1459 | throw e; |
| 1460 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 1463 | public int performRestore(long token, IRestoreObserver observer) |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1464 | throws android.os.RemoteException { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1465 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "performRestore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1466 | |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1467 | Log.d(TAG, "performRestore token=" + token + " observer=" + observer); |
| 1468 | |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1469 | if (mRestoreSets != null) { |
| 1470 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 1471 | if (token == mRestoreSets[i].token) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 1472 | mWakelock.acquire(); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 1473 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 1474 | msg.obj = new RestoreParams(mRestoreTransport, observer, token); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1475 | mBackupHandler.sendMessage(msg); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1479 | } else { |
| 1480 | if (DEBUG) Log.v(TAG, "No current restore set, not doing restore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1481 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1482 | return -1; |
| 1483 | } |
| 1484 | |
| 1485 | public void endRestoreSession() throws android.os.RemoteException { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 1486 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 1487 | "endRestoreSession"); |
| 1488 | |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 1489 | Log.d(TAG, "endRestoreSession"); |
| 1490 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1491 | mRestoreTransport.finishRestore(); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1492 | mRestoreTransport = null; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1493 | synchronized(BackupManagerService.this) { |
| 1494 | if (BackupManagerService.this.mActiveRestoreSession == this) { |
| 1495 | BackupManagerService.this.mActiveRestoreSession = null; |
| 1496 | } else { |
| 1497 | Log.e(TAG, "ending non-current restore session"); |
| 1498 | } |
| 1499 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1503 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1504 | @Override |
| 1505 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1506 | synchronized (mQueueLock) { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 1507 | long oldId = Binder.clearCallingIdentity(); |
| 1508 | |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 1509 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled")); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1510 | pw.println("Available transports:"); |
| 1511 | for (String t : listAllTransports()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1512 | String pad = (t.equals(mCurrentTransport)) ? " * " : " "; |
| 1513 | pw.println(pad + t); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1514 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1515 | int N = mBackupParticipants.size(); |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 1516 | pw.println("Participants: " + N); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1517 | for (int i=0; i<N; i++) { |
| 1518 | int uid = mBackupParticipants.keyAt(i); |
| 1519 | pw.print(" uid: "); |
| 1520 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1521 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 1522 | for (ApplicationInfo app: participants) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1523 | pw.println(" " + app.toString()); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1524 | } |
| 1525 | } |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 1526 | pw.println("Pending: " + mPendingBackups.size()); |
| 1527 | for (BackupRequest req : mPendingBackups.values()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 1528 | pw.println(" " + req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1529 | } |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 1530 | |
| 1531 | Binder.restoreCallingIdentity(oldId); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1532 | } |
| 1533 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1534 | } |