Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -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 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 17 | package android.app.backup; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 18 | |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 22 | import android.content.ComponentName; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 24 | import android.content.Intent; |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 25 | import android.os.Bundle; |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Message; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Michal Karpinski | 96e0be0 | 2018-01-16 16:14:54 +0000 | [diff] [blame] | 30 | import android.os.UserHandle; |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 32 | import android.util.Pair; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 35 | * The interface through which an application interacts with the Android backup service to |
| 36 | * request backup and restore operations. |
| 37 | * Applications instantiate it using the constructor and issue calls through that instance. |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 38 | * <p> |
| 39 | * When an application has made changes to data which should be backed up, a |
| 40 | * call to {@link #dataChanged()} will notify the backup service. The system |
| 41 | * will then schedule a backup operation to occur in the near future. Repeated |
| 42 | * calls to {@link #dataChanged()} have no further effect until the backup |
| 43 | * operation actually occurs. |
| 44 | * <p> |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 45 | * A backup or restore operation for your application begins when the system launches the |
| 46 | * {@link android.app.backup.BackupAgent} subclass you've declared in your manifest. See the |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 47 | * documentation for {@link android.app.backup.BackupAgent} for a detailed description |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 48 | * of how the operation then proceeds. |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 49 | * <p> |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 50 | * Several attributes affecting the operation of the backup and restore mechanism |
Joe Fernandez | 61fd1e8 | 2011-10-26 13:39:11 -0700 | [diff] [blame] | 51 | * can be set on the <code> |
| 52 | * <a href="{@docRoot}guide/topics/manifest/application-element.html"><application></a></code> |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 53 | * tag in your application's AndroidManifest.xml file. |
| 54 | * |
Joe Fernandez | 61fd1e8 | 2011-10-26 13:39:11 -0700 | [diff] [blame] | 55 | * <div class="special reference"> |
| 56 | * <h3>Developer Guides</h3> |
| 57 | * <p>For more information about using BackupManager, read the |
| 58 | * <a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a> developer guide.</p></div> |
| 59 | * |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 60 | * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup |
| 61 | * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 62 | * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 63 | * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 64 | */ |
| 65 | public class BackupManager { |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 66 | private static final String TAG = "BackupManager"; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 67 | |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 68 | // BackupObserver status codes |
| 69 | /** |
| 70 | * Indicates that backup succeeded. |
| 71 | * |
| 72 | * @hide |
| 73 | */ |
| 74 | @SystemApi |
| 75 | public static final int SUCCESS = 0; |
| 76 | |
| 77 | /** |
| 78 | * Indicates that backup is either not enabled at all or |
| 79 | * backup for the package was rejected by backup service |
| 80 | * or backup transport, |
| 81 | * |
| 82 | * @hide |
| 83 | */ |
| 84 | @SystemApi |
| 85 | public static final int ERROR_BACKUP_NOT_ALLOWED = -2001; |
| 86 | |
| 87 | /** |
| 88 | * The requested app is not installed on the device. |
| 89 | * |
| 90 | * @hide |
| 91 | */ |
| 92 | @SystemApi |
| 93 | public static final int ERROR_PACKAGE_NOT_FOUND = -2002; |
| 94 | |
| 95 | /** |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 96 | * The backup operation was cancelled. |
| 97 | * |
| 98 | * @hide |
| 99 | */ |
| 100 | @SystemApi |
| 101 | public static final int ERROR_BACKUP_CANCELLED = -2003; |
| 102 | |
| 103 | /** |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 104 | * The transport for some reason was not in a good state and |
| 105 | * aborted the entire backup request. This is a transient |
| 106 | * failure and should not be retried immediately. |
| 107 | * |
| 108 | * @hide |
| 109 | */ |
| 110 | @SystemApi |
| 111 | public static final int ERROR_TRANSPORT_ABORTED = BackupTransport.TRANSPORT_ERROR; |
| 112 | |
| 113 | /** |
| 114 | * Returned when the transport was unable to process the |
| 115 | * backup request for a given package, for example if the |
| 116 | * transport hit a transient network failure. The remaining |
| 117 | * packages provided to {@link #requestBackup(String[], BackupObserver)} |
| 118 | * will still be attempted. |
| 119 | * |
| 120 | * @hide |
| 121 | */ |
| 122 | @SystemApi |
| 123 | public static final int ERROR_TRANSPORT_PACKAGE_REJECTED = |
| 124 | BackupTransport.TRANSPORT_PACKAGE_REJECTED; |
| 125 | |
| 126 | /** |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 127 | * Returned when the transport reject the attempt to backup because |
| 128 | * backup data size exceeded current quota limit for this package. |
| 129 | * |
| 130 | * @hide |
| 131 | */ |
| 132 | @SystemApi |
| 133 | public static final int ERROR_TRANSPORT_QUOTA_EXCEEDED = |
| 134 | BackupTransport.TRANSPORT_QUOTA_EXCEEDED; |
| 135 | |
| 136 | /** |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 137 | * The {@link BackupAgent} for the requested package failed for some reason |
| 138 | * and didn't provide appropriate backup data. |
| 139 | * |
| 140 | * @hide |
| 141 | */ |
| 142 | @SystemApi |
| 143 | public static final int ERROR_AGENT_FAILURE = BackupTransport.AGENT_ERROR; |
| 144 | |
Christopher Tate | 38c9c30 | 2016-09-21 17:18:10 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Intent extra when any subsidiary backup-related UI is launched from Settings: does |
| 147 | * device policy or configuration permit backup operations to run at all? |
| 148 | * |
| 149 | * @hide |
| 150 | */ |
| 151 | public static final String EXTRA_BACKUP_SERVICES_AVAILABLE = "backup_services_available"; |
| 152 | |
Shreyas Basarge | 38e7486 | 2017-01-11 17:15:58 +0000 | [diff] [blame] | 153 | /** |
| 154 | * If this flag is passed to {@link #requestBackup(String[], BackupObserver, int)}, |
| 155 | * BackupManager will pass a blank old state to BackupAgents of requested packages. |
| 156 | * |
| 157 | * @hide |
| 158 | */ |
| 159 | @SystemApi |
| 160 | public static final int FLAG_NON_INCREMENTAL_BACKUP = 1; |
| 161 | |
| 162 | /** |
| 163 | * Use with {@link #requestBackup} to force backup of |
| 164 | * package meta data. Typically you do not need to explicitly request this be backed up as it is |
| 165 | * handled internally by the BackupManager. If you are requesting backups with |
| 166 | * FLAG_NON_INCREMENTAL, this package won't automatically be backed up and you have to |
| 167 | * explicitly request for its backup. |
| 168 | * |
| 169 | * @hide |
| 170 | */ |
| 171 | @SystemApi |
| 172 | public static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| 173 | |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * This error code is passed to {@link SelectBackupTransportCallback#onFailure(int)} |
| 177 | * if the requested transport is unavailable. |
| 178 | * |
| 179 | * @hide |
| 180 | */ |
| 181 | @SystemApi |
| 182 | public static final int ERROR_TRANSPORT_UNAVAILABLE = -1; |
| 183 | |
| 184 | /** |
| 185 | * This error code is passed to {@link SelectBackupTransportCallback#onFailure(int)} if the |
| 186 | * requested transport is not a valid BackupTransport. |
| 187 | * |
| 188 | * @hide |
| 189 | */ |
| 190 | @SystemApi |
| 191 | public static final int ERROR_TRANSPORT_INVALID = -2; |
| 192 | |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 193 | private Context mContext; |
| 194 | private static IBackupManager sService; |
| 195 | |
| 196 | private static void checkServiceBinder() { |
| 197 | if (sService == null) { |
| 198 | sService = IBackupManager.Stub.asInterface( |
| 199 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 200 | } |
| 201 | } |
Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 204 | * Constructs a BackupManager object through which the application can |
| 205 | * communicate with the Android backup system. |
Christopher Tate | c114eb5 | 2009-04-30 12:40:19 -0700 | [diff] [blame] | 206 | * |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 207 | * @param context The {@link android.content.Context} that was provided when |
| 208 | * one of your application's {@link android.app.Activity Activities} |
| 209 | * was created. |
| 210 | */ |
Christopher Tate | c114eb5 | 2009-04-30 12:40:19 -0700 | [diff] [blame] | 211 | public BackupManager(Context context) { |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 212 | mContext = context; |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Notifies the Android backup system that your application wishes to back up |
| 217 | * new changes to its data. A backup operation using your application's |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 218 | * {@link android.app.backup.BackupAgent} subclass will be scheduled when you |
| 219 | * call this method. |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 220 | */ |
| 221 | public void dataChanged() { |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 222 | checkServiceBinder(); |
| 223 | if (sService != null) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 224 | try { |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 225 | sService.dataChanged(mContext.getPackageName()); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 226 | } catch (RemoteException e) { |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 227 | Log.d(TAG, "dataChanged() couldn't connect"); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Convenience method for callers who need to indicate that some other package |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 234 | * needs a backup pass. This can be useful in the case of groups of packages |
| 235 | * that share a uid. |
| 236 | * <p> |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 237 | * This method requires that the application hold the "android.permission.BACKUP" |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 238 | * permission if the package named in the argument does not run under the same uid |
| 239 | * as the caller. |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 240 | * |
| 241 | * @param packageName The package name identifying the application to back up. |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 242 | */ |
| 243 | public static void dataChanged(String packageName) { |
| 244 | checkServiceBinder(); |
| 245 | if (sService != null) { |
| 246 | try { |
| 247 | sService.dataChanged(packageName); |
| 248 | } catch (RemoteException e) { |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 249 | Log.e(TAG, "dataChanged(pkg) couldn't connect"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 250 | } |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 253 | |
| 254 | /** |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 255 | * Restore the calling application from backup. The data will be restored from the |
| 256 | * current backup dataset if the application has stored data there, or from |
| 257 | * the dataset used during the last full device setup operation if the current |
| 258 | * backup dataset has no matching data. If no backup data exists for this application |
| 259 | * in either source, a nonzero value will be returned. |
| 260 | * |
| 261 | * <p>If this method returns zero (meaning success), the OS will attempt to retrieve |
| 262 | * a backed-up dataset from the remote transport, instantiate the application's |
| 263 | * backup agent, and pass the dataset to the agent's |
| 264 | * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} |
| 265 | * method. |
| 266 | * |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 267 | * @param observer The {@link RestoreObserver} to receive callbacks during the restore |
| 268 | * operation. This must not be null. |
| 269 | * |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 270 | * @return Zero on success; nonzero on error. |
| 271 | */ |
| 272 | public int requestRestore(RestoreObserver observer) { |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 273 | return requestRestore(observer, null); |
| 274 | } |
| 275 | |
| 276 | // system APIs start here |
| 277 | |
| 278 | /** |
| 279 | * Restore the calling application from backup. The data will be restored from the |
| 280 | * current backup dataset if the application has stored data there, or from |
| 281 | * the dataset used during the last full device setup operation if the current |
| 282 | * backup dataset has no matching data. If no backup data exists for this application |
| 283 | * in either source, a nonzero value will be returned. |
| 284 | * |
| 285 | * <p>If this method returns zero (meaning success), the OS will attempt to retrieve |
| 286 | * a backed-up dataset from the remote transport, instantiate the application's |
| 287 | * backup agent, and pass the dataset to the agent's |
| 288 | * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} |
| 289 | * method. |
| 290 | * |
| 291 | * @param observer The {@link RestoreObserver} to receive callbacks during the restore |
| 292 | * operation. This must not be null. |
| 293 | * |
| 294 | * @param monitor the {@link BackupManagerMonitor} to receive callbacks during the restore |
| 295 | * operation. |
| 296 | * |
| 297 | * @return Zero on success; nonzero on error. |
| 298 | * |
| 299 | * @hide |
| 300 | */ |
| 301 | @SystemApi |
| 302 | public int requestRestore(RestoreObserver observer, BackupManagerMonitor monitor) { |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 303 | int result = -1; |
| 304 | checkServiceBinder(); |
| 305 | if (sService != null) { |
| 306 | RestoreSession session = null; |
| 307 | try { |
Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 308 | IRestoreSession binder = sService.beginRestoreSession(mContext.getPackageName(), |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 309 | null); |
Christopher Tate | f5491fc | 2012-05-25 14:14:49 -0700 | [diff] [blame] | 310 | if (binder != null) { |
| 311 | session = new RestoreSession(mContext, binder); |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 312 | result = session.restorePackage(mContext.getPackageName(), observer, monitor); |
Christopher Tate | f5491fc | 2012-05-25 14:14:49 -0700 | [diff] [blame] | 313 | } |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 314 | } catch (RemoteException e) { |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 315 | Log.e(TAG, "restoreSelf() unable to contact service"); |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 316 | } finally { |
| 317 | if (session != null) { |
| 318 | session.endRestoreSession(); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | /** |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 326 | * Begin the process of restoring data from backup. See the |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 327 | * {@link android.app.backup.RestoreSession} class for documentation on that process. |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 328 | * @hide |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 329 | */ |
Christopher Tate | 2e59792 | 2014-08-07 14:53:06 -0700 | [diff] [blame] | 330 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 331 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 332 | public RestoreSession beginRestoreSession() { |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 333 | RestoreSession session = null; |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 334 | checkServiceBinder(); |
| 335 | if (sService != null) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 336 | try { |
Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 337 | // All packages, current transport |
| 338 | IRestoreSession binder = sService.beginRestoreSession(null, null); |
Christopher Tate | f5491fc | 2012-05-25 14:14:49 -0700 | [diff] [blame] | 339 | if (binder != null) { |
| 340 | session = new RestoreSession(mContext, binder); |
| 341 | } |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 342 | } catch (RemoteException e) { |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 343 | Log.e(TAG, "beginRestoreSession() couldn't connect"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 344 | } |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 345 | } |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 346 | return session; |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 347 | } |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 348 | |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Enable/disable the backup service entirely. When disabled, no backup |
| 351 | * or restore operations will take place. Data-changed notifications will |
| 352 | * still be observed and collected, however, so that changes made while the |
| 353 | * mechanism was disabled will still be backed up properly if it is enabled |
| 354 | * at some point in the future. |
| 355 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 356 | * @hide |
| 357 | */ |
| 358 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 359 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 360 | public void setBackupEnabled(boolean isEnabled) { |
| 361 | checkServiceBinder(); |
| 362 | if (sService != null) { |
| 363 | try { |
| 364 | sService.setBackupEnabled(isEnabled); |
| 365 | } catch (RemoteException e) { |
| 366 | Log.e(TAG, "setBackupEnabled() couldn't connect"); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Report whether the backup mechanism is currently enabled. |
| 373 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 374 | * @hide |
| 375 | */ |
| 376 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 377 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 378 | public boolean isBackupEnabled() { |
Christopher Tate | 9e07929 | 2014-09-09 15:10:56 -0700 | [diff] [blame] | 379 | checkServiceBinder(); |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 380 | if (sService != null) { |
| 381 | try { |
| 382 | return sService.isBackupEnabled(); |
| 383 | } catch (RemoteException e) { |
| 384 | Log.e(TAG, "isBackupEnabled() couldn't connect"); |
| 385 | } |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | /** |
Michal Karpinski | 96e0be0 | 2018-01-16 16:14:54 +0000 | [diff] [blame] | 391 | * Report whether the backup mechanism is currently active. |
| 392 | * When it is inactive, the device will not perform any backup operations, nor will it |
| 393 | * deliver data for restore, although clients can still safely call BackupManager methods. |
| 394 | * |
| 395 | * @hide |
| 396 | */ |
| 397 | @SystemApi |
| 398 | @RequiresPermission(android.Manifest.permission.BACKUP) |
| 399 | public boolean isBackupServiceActive(UserHandle user) { |
| 400 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, |
| 401 | "isBackupServiceActive"); |
| 402 | checkServiceBinder(); |
| 403 | if (sService != null) { |
| 404 | try { |
| 405 | return sService.isBackupServiceActive(user.getIdentifier()); |
| 406 | } catch (RemoteException e) { |
| 407 | Log.e(TAG, "isBackupEnabled() couldn't connect"); |
| 408 | } |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | /** |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 414 | * Enable/disable data restore at application install time. When enabled, app |
| 415 | * installation will include an attempt to fetch the app's historical data from |
| 416 | * the archival restore dataset (if any). When disabled, no such attempt will |
| 417 | * be made. |
| 418 | * |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 419 | * @hide |
| 420 | */ |
| 421 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 422 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 423 | public void setAutoRestore(boolean isEnabled) { |
| 424 | checkServiceBinder(); |
| 425 | if (sService != null) { |
| 426 | try { |
| 427 | sService.setAutoRestore(isEnabled); |
| 428 | } catch (RemoteException e) { |
| 429 | Log.e(TAG, "setAutoRestore() couldn't connect"); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 435 | * Identify the currently selected transport. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 436 | * @return The name of the currently active backup transport. In case of |
| 437 | * failure or if no transport is currently active, this method returns {@code null}. |
| 438 | * |
| 439 | * @hide |
| 440 | */ |
| 441 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 442 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 443 | public String getCurrentTransport() { |
| 444 | checkServiceBinder(); |
| 445 | if (sService != null) { |
| 446 | try { |
| 447 | return sService.getCurrentTransport(); |
| 448 | } catch (RemoteException e) { |
| 449 | Log.e(TAG, "getCurrentTransport() couldn't connect"); |
| 450 | } |
| 451 | } |
| 452 | return null; |
| 453 | } |
| 454 | |
| 455 | /** |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 456 | * Request a list of all available backup transports' names. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 457 | * |
| 458 | * @hide |
| 459 | */ |
| 460 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 461 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 462 | public String[] listAllTransports() { |
| 463 | checkServiceBinder(); |
| 464 | if (sService != null) { |
| 465 | try { |
| 466 | return sService.listAllTransports(); |
| 467 | } catch (RemoteException e) { |
| 468 | Log.e(TAG, "listAllTransports() couldn't connect"); |
| 469 | } |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | /** |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 475 | * Update the attributes of the transport identified by {@code transportComponent}. If the |
| 476 | * specified transport has not been bound at least once (for registration), this call will be |
| 477 | * ignored. Only the host process of the transport can change its description, otherwise a |
| 478 | * {@link SecurityException} will be thrown. |
| 479 | * |
| 480 | * @param transportComponent The identity of the transport being described. |
| 481 | * @param name A {@link String} with the new name for the transport. This is NOT for |
| 482 | * identification. MUST NOT be {@code null}. |
| 483 | * @param configurationIntent An {@link Intent} that can be passed to |
| 484 | * {@link Context#startActivity} in order to launch the transport's configuration UI. It may |
| 485 | * be {@code null} if the transport does not offer any user-facing configuration UI. |
| 486 | * @param currentDestinationString A {@link String} describing the destination to which the |
| 487 | * transport is currently sending data. MUST NOT be {@code null}. |
| 488 | * @param dataManagementIntent An {@link Intent} that can be passed to |
| 489 | * {@link Context#startActivity} in order to launch the transport's data-management UI. It |
| 490 | * may be {@code null} if the transport does not offer any user-facing data |
| 491 | * management UI. |
| 492 | * @param dataManagementLabel A {@link String} to be used as the label for the transport's data |
| 493 | * management affordance. This MUST be {@code null} when dataManagementIntent is |
| 494 | * {@code null} and MUST NOT be {@code null} when dataManagementIntent is not {@code null}. |
| 495 | * @throws SecurityException If the UID of the calling process differs from the package UID of |
| 496 | * {@code transportComponent} or if the caller does NOT have BACKUP permission. |
| 497 | * |
| 498 | * @hide |
| 499 | */ |
| 500 | @SystemApi |
| 501 | @RequiresPermission(android.Manifest.permission.BACKUP) |
| 502 | public void updateTransportAttributes( |
| 503 | ComponentName transportComponent, |
| 504 | String name, |
| 505 | @Nullable Intent configurationIntent, |
| 506 | String currentDestinationString, |
| 507 | @Nullable Intent dataManagementIntent, |
| 508 | @Nullable String dataManagementLabel) { |
| 509 | checkServiceBinder(); |
| 510 | if (sService != null) { |
| 511 | try { |
| 512 | sService.updateTransportAttributes( |
| 513 | transportComponent, |
| 514 | name, |
| 515 | configurationIntent, |
| 516 | currentDestinationString, |
| 517 | dataManagementIntent, |
| 518 | dataManagementLabel); |
| 519 | } catch (RemoteException e) { |
| 520 | Log.e(TAG, "describeTransport() couldn't connect"); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 526 | * Specify the current backup transport. |
| 527 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 528 | * @param transport The name of the transport to select. This should be one |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 529 | * of the names returned by {@link #listAllTransports()}. This is the String returned by |
| 530 | * {@link BackupTransport#name()} for the particular transport. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 531 | * @return The name of the previously selected transport. If the given transport |
| 532 | * name is not one of the currently available transports, no change is made to |
| 533 | * the current transport setting and the method returns null. |
| 534 | * |
| 535 | * @hide |
| 536 | */ |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 537 | @Deprecated |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 538 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 539 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 540 | public String selectBackupTransport(String transport) { |
| 541 | checkServiceBinder(); |
| 542 | if (sService != null) { |
| 543 | try { |
| 544 | return sService.selectBackupTransport(transport); |
| 545 | } catch (RemoteException e) { |
| 546 | Log.e(TAG, "selectBackupTransport() couldn't connect"); |
| 547 | } |
| 548 | } |
| 549 | return null; |
| 550 | } |
| 551 | |
| 552 | /** |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 553 | * Specify the current backup transport and get notified when the transport is ready to be used. |
| 554 | * This method is async because BackupManager might need to bind to the specified transport |
| 555 | * which is in a separate process. |
| 556 | * |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 557 | * @param transport ComponentName of the service hosting the transport. This is different from |
| 558 | * the transport's name that is returned by {@link BackupTransport#name()}. |
| 559 | * @param listener A listener object to get a callback on the transport being selected. |
| 560 | * |
| 561 | * @hide |
| 562 | */ |
| 563 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 564 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 565 | public void selectBackupTransport(ComponentName transport, |
| 566 | SelectBackupTransportCallback listener) { |
| 567 | checkServiceBinder(); |
| 568 | if (sService != null) { |
| 569 | try { |
| 570 | SelectTransportListenerWrapper wrapper = listener == null ? |
| 571 | null : new SelectTransportListenerWrapper(mContext, listener); |
| 572 | sService.selectBackupTransportAsync(transport, wrapper); |
| 573 | } catch (RemoteException e) { |
| 574 | Log.e(TAG, "selectBackupTransportAsync() couldn't connect"); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 580 | * Schedule an immediate backup attempt for all pending key/value updates. This |
| 581 | * is primarily intended for transports to use when they detect a suitable |
| 582 | * opportunity for doing a backup pass. If there are no pending updates to |
| 583 | * be sent, no action will be taken. Even if some updates are pending, the |
| 584 | * transport will still be asked to confirm via the usual requestBackupTime() |
| 585 | * method. |
| 586 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 587 | * @hide |
| 588 | */ |
| 589 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 590 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 591 | public void backupNow() { |
| 592 | checkServiceBinder(); |
| 593 | if (sService != null) { |
| 594 | try { |
| 595 | sService.backupNow(); |
| 596 | } catch (RemoteException e) { |
| 597 | Log.e(TAG, "backupNow() couldn't connect"); |
| 598 | } |
| 599 | } |
| 600 | } |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 601 | |
| 602 | /** |
| 603 | * Ask the framework which dataset, if any, the given package's data would be |
| 604 | * restored from if we were to install it right now. |
| 605 | * |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 606 | * @param packageName The name of the package whose most-suitable dataset we |
| 607 | * wish to look up |
| 608 | * @return The dataset token from which a restore should be attempted, or zero if |
| 609 | * no suitable data is available. |
| 610 | * |
| 611 | * @hide |
| 612 | */ |
| 613 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 614 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 615 | public long getAvailableRestoreToken(String packageName) { |
| 616 | checkServiceBinder(); |
| 617 | if (sService != null) { |
| 618 | try { |
| 619 | return sService.getAvailableRestoreToken(packageName); |
| 620 | } catch (RemoteException e) { |
| 621 | Log.e(TAG, "getAvailableRestoreToken() couldn't connect"); |
| 622 | } |
| 623 | } |
| 624 | return 0; |
| 625 | } |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 626 | |
| 627 | /** |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 628 | * Ask the framework whether this app is eligible for backup. |
| 629 | * |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 630 | * @param packageName The name of the package. |
| 631 | * @return Whether this app is eligible for backup. |
| 632 | * |
| 633 | * @hide |
| 634 | */ |
| 635 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 636 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 637 | public boolean isAppEligibleForBackup(String packageName) { |
| 638 | checkServiceBinder(); |
| 639 | if (sService != null) { |
| 640 | try { |
| 641 | return sService.isAppEligibleForBackup(packageName); |
| 642 | } catch (RemoteException e) { |
| 643 | Log.e(TAG, "isAppEligibleForBackup(pkg) couldn't connect"); |
| 644 | } |
| 645 | } |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | /** |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 650 | * Request an immediate backup, providing an observer to which results of the backup operation |
| 651 | * will be published. The Android backup system will decide for each package whether it will |
| 652 | * be full app data backup or key/value-pair-based backup. |
| 653 | * |
| 654 | * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all |
| 655 | * provided packages using the remote transport. |
| 656 | * |
| 657 | * @param packages List of package names to backup. |
| 658 | * @param observer The {@link BackupObserver} to receive callbacks during the backup |
Sergey Poromov | 899edf6 | 2016-02-24 17:21:59 +0100 | [diff] [blame] | 659 | * operation. Could be {@code null}. |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 660 | * @return {@link BackupManager#SUCCESS} on success; nonzero on error. |
| 661 | * @exception IllegalArgumentException on null or empty {@code packages} param. |
| 662 | * |
| 663 | * @hide |
| 664 | */ |
| 665 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 666 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 667 | public int requestBackup(String[] packages, BackupObserver observer) { |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 668 | return requestBackup(packages, observer, null, 0); |
Shreyas Basarge | 38e7486 | 2017-01-11 17:15:58 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Request an immediate backup, providing an observer to which results of the backup operation |
| 673 | * will be published. The Android backup system will decide for each package whether it will |
| 674 | * be full app data backup or key/value-pair-based backup. |
| 675 | * |
| 676 | * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all |
| 677 | * provided packages using the remote transport. |
| 678 | * |
| 679 | * @param packages List of package names to backup. |
| 680 | * @param observer The {@link BackupObserver} to receive callbacks during the backup |
| 681 | * operation. Could be {@code null}. |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 682 | * @param monitor The {@link BackupManagerMonitorWrapper} to receive callbacks of important |
| 683 | * events during the backup operation. Could be {@code null}. |
Shreyas Basarge | 38e7486 | 2017-01-11 17:15:58 +0000 | [diff] [blame] | 684 | * @param flags {@link #FLAG_NON_INCREMENTAL_BACKUP}. |
| 685 | * @return {@link BackupManager#SUCCESS} on success; nonzero on error. |
| 686 | * @throws IllegalArgumentException on null or empty {@code packages} param. |
| 687 | * @hide |
| 688 | */ |
| 689 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 690 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 691 | public int requestBackup(String[] packages, BackupObserver observer, |
| 692 | BackupManagerMonitor monitor, int flags) { |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 693 | checkServiceBinder(); |
| 694 | if (sService != null) { |
| 695 | try { |
Sergey Poromov | 899edf6 | 2016-02-24 17:21:59 +0100 | [diff] [blame] | 696 | BackupObserverWrapper observerWrapper = observer == null |
| 697 | ? null |
| 698 | : new BackupObserverWrapper(mContext, observer); |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 699 | BackupManagerMonitorWrapper monitorWrapper = monitor == null |
| 700 | ? null |
| 701 | : new BackupManagerMonitorWrapper(monitor); |
| 702 | return sService.requestBackup(packages, observerWrapper, monitorWrapper, flags); |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 703 | } catch (RemoteException e) { |
| 704 | Log.e(TAG, "requestBackup() couldn't connect"); |
| 705 | } |
| 706 | } |
| 707 | return -1; |
| 708 | } |
| 709 | |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 710 | /** |
| 711 | * Cancel all running backups. After this call returns, no currently running backups will |
| 712 | * interact with the selected transport. |
| 713 | * |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 714 | * @hide |
| 715 | */ |
| 716 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 717 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 718 | public void cancelBackups() { |
| 719 | checkServiceBinder(); |
| 720 | if (sService != null) { |
| 721 | try { |
| 722 | sService.cancelBackups(); |
| 723 | } catch (RemoteException e) { |
| 724 | Log.e(TAG, "cancelBackups() couldn't connect."); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 729 | /* |
| 730 | * We wrap incoming binder calls with a private class implementation that |
| 731 | * redirects them into main-thread actions. This serializes the backup |
| 732 | * progress callbacks nicely within the usual main-thread lifecycle pattern. |
| 733 | */ |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 734 | private class BackupObserverWrapper extends IBackupObserver.Stub { |
| 735 | final Handler mHandler; |
| 736 | final BackupObserver mObserver; |
| 737 | |
| 738 | static final int MSG_UPDATE = 1; |
| 739 | static final int MSG_RESULT = 2; |
| 740 | static final int MSG_FINISHED = 3; |
| 741 | |
| 742 | BackupObserverWrapper(Context context, BackupObserver observer) { |
| 743 | mHandler = new Handler(context.getMainLooper()) { |
| 744 | @Override |
| 745 | public void handleMessage(Message msg) { |
| 746 | switch (msg.what) { |
| 747 | case MSG_UPDATE: |
| 748 | Pair<String, BackupProgress> obj = |
| 749 | (Pair<String, BackupProgress>) msg.obj; |
| 750 | mObserver.onUpdate(obj.first, obj.second); |
| 751 | break; |
| 752 | case MSG_RESULT: |
| 753 | mObserver.onResult((String)msg.obj, msg.arg1); |
| 754 | break; |
| 755 | case MSG_FINISHED: |
| 756 | mObserver.backupFinished(msg.arg1); |
| 757 | break; |
| 758 | default: |
| 759 | Log.w(TAG, "Unknown message: " + msg); |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | }; |
| 764 | mObserver = observer; |
| 765 | } |
| 766 | |
| 767 | // Binder calls into this object just enqueue on the main-thread handler |
| 768 | @Override |
| 769 | public void onUpdate(String currentPackage, BackupProgress backupProgress) { |
| 770 | mHandler.sendMessage( |
| 771 | mHandler.obtainMessage(MSG_UPDATE, Pair.create(currentPackage, backupProgress))); |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public void onResult(String currentPackage, int status) { |
| 776 | mHandler.sendMessage( |
Sergey Poromov | 4b51d99 | 2016-01-29 17:46:32 +0100 | [diff] [blame] | 777 | mHandler.obtainMessage(MSG_RESULT, status, 0, currentPackage)); |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public void backupFinished(int status) { |
| 782 | mHandler.sendMessage( |
| 783 | mHandler.obtainMessage(MSG_FINISHED, status, 0)); |
| 784 | } |
| 785 | } |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 786 | |
| 787 | private class SelectTransportListenerWrapper extends ISelectBackupTransportCallback.Stub { |
| 788 | |
| 789 | private final Handler mHandler; |
| 790 | private final SelectBackupTransportCallback mListener; |
| 791 | |
| 792 | SelectTransportListenerWrapper(Context context, SelectBackupTransportCallback listener) { |
| 793 | mHandler = new Handler(context.getMainLooper()); |
| 794 | mListener = listener; |
| 795 | } |
| 796 | |
| 797 | @Override |
| 798 | public void onSuccess(final String transportName) { |
| 799 | mHandler.post(new Runnable() { |
| 800 | @Override |
| 801 | public void run() { |
| 802 | mListener.onSuccess(transportName); |
| 803 | } |
| 804 | }); |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public void onFailure(final int reason) { |
| 809 | mHandler.post(new Runnable() { |
| 810 | @Override |
| 811 | public void run() { |
| 812 | mListener.onFailure(reason); |
| 813 | } |
| 814 | }); |
| 815 | } |
| 816 | } |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 817 | |
| 818 | private class BackupManagerMonitorWrapper extends IBackupManagerMonitor.Stub { |
| 819 | final BackupManagerMonitor mMonitor; |
| 820 | |
| 821 | BackupManagerMonitorWrapper(BackupManagerMonitor monitor) { |
| 822 | mMonitor = monitor; |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | public void onEvent(final Bundle event) throws RemoteException { |
| 827 | mMonitor.onEvent(event); |
| 828 | } |
| 829 | } |
| 830 | |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 831 | } |