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 | /** |
Michal Karpinski | 0df62ac | 2018-01-24 13:02:03 +0000 | [diff] [blame] | 255 | * @deprecated Since Android P app can no longer request restoring of its backup. |
| 256 | * |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 257 | * Restore the calling application from backup. The data will be restored from the |
| 258 | * current backup dataset if the application has stored data there, or from |
| 259 | * the dataset used during the last full device setup operation if the current |
| 260 | * backup dataset has no matching data. If no backup data exists for this application |
| 261 | * in either source, a nonzero value will be returned. |
| 262 | * |
| 263 | * <p>If this method returns zero (meaning success), the OS will attempt to retrieve |
| 264 | * a backed-up dataset from the remote transport, instantiate the application's |
| 265 | * backup agent, and pass the dataset to the agent's |
| 266 | * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} |
| 267 | * method. |
| 268 | * |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 269 | * @param observer The {@link RestoreObserver} to receive callbacks during the restore |
| 270 | * operation. This must not be null. |
| 271 | * |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 272 | * @return Zero on success; nonzero on error. |
| 273 | */ |
Michal Karpinski | 0df62ac | 2018-01-24 13:02:03 +0000 | [diff] [blame] | 274 | @Deprecated |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 275 | public int requestRestore(RestoreObserver observer) { |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 276 | return requestRestore(observer, null); |
| 277 | } |
| 278 | |
| 279 | // system APIs start here |
| 280 | |
| 281 | /** |
Michal Karpinski | 0df62ac | 2018-01-24 13:02:03 +0000 | [diff] [blame] | 282 | * @deprecated Since Android P app can no longer request restoring of its backup. |
| 283 | * |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 284 | * Restore the calling application from backup. The data will be restored from the |
| 285 | * current backup dataset if the application has stored data there, or from |
| 286 | * the dataset used during the last full device setup operation if the current |
| 287 | * backup dataset has no matching data. If no backup data exists for this application |
| 288 | * in either source, a nonzero value will be returned. |
| 289 | * |
| 290 | * <p>If this method returns zero (meaning success), the OS will attempt to retrieve |
| 291 | * a backed-up dataset from the remote transport, instantiate the application's |
| 292 | * backup agent, and pass the dataset to the agent's |
| 293 | * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} |
| 294 | * method. |
| 295 | * |
| 296 | * @param observer The {@link RestoreObserver} to receive callbacks during the restore |
| 297 | * operation. This must not be null. |
| 298 | * |
| 299 | * @param monitor the {@link BackupManagerMonitor} to receive callbacks during the restore |
| 300 | * operation. |
| 301 | * |
| 302 | * @return Zero on success; nonzero on error. |
| 303 | * |
| 304 | * @hide |
| 305 | */ |
Michal Karpinski | 0df62ac | 2018-01-24 13:02:03 +0000 | [diff] [blame] | 306 | @Deprecated |
Stefanot | f4e237c | 2017-02-06 21:14:05 +0000 | [diff] [blame] | 307 | @SystemApi |
| 308 | public int requestRestore(RestoreObserver observer, BackupManagerMonitor monitor) { |
Michal Karpinski | 0df62ac | 2018-01-24 13:02:03 +0000 | [diff] [blame] | 309 | Log.w(TAG, "requestRestore(): Since Android P app can no longer request restoring" |
| 310 | + " of its backup."); |
| 311 | return -1; |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 315 | * Begin the process of restoring data from backup. See the |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 316 | * {@link android.app.backup.RestoreSession} class for documentation on that process. |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 317 | * @hide |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 318 | */ |
Christopher Tate | 2e59792 | 2014-08-07 14:53:06 -0700 | [diff] [blame] | 319 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 320 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 321 | public RestoreSession beginRestoreSession() { |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 322 | RestoreSession session = null; |
Christopher Tate | c8daa76 | 2009-07-06 19:04:57 -0700 | [diff] [blame] | 323 | checkServiceBinder(); |
| 324 | if (sService != null) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 325 | try { |
Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 326 | // All packages, current transport |
| 327 | IRestoreSession binder = sService.beginRestoreSession(null, null); |
Christopher Tate | f5491fc | 2012-05-25 14:14:49 -0700 | [diff] [blame] | 328 | if (binder != null) { |
| 329 | session = new RestoreSession(mContext, binder); |
| 330 | } |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 331 | } catch (RemoteException e) { |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 332 | Log.e(TAG, "beginRestoreSession() couldn't connect"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 333 | } |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 334 | } |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 335 | return session; |
Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 336 | } |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 337 | |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 338 | /** |
| 339 | * Enable/disable the backup service entirely. When disabled, no backup |
| 340 | * or restore operations will take place. Data-changed notifications will |
| 341 | * still be observed and collected, however, so that changes made while the |
| 342 | * mechanism was disabled will still be backed up properly if it is enabled |
| 343 | * at some point in the future. |
| 344 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 345 | * @hide |
| 346 | */ |
| 347 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 348 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 349 | public void setBackupEnabled(boolean isEnabled) { |
| 350 | checkServiceBinder(); |
| 351 | if (sService != null) { |
| 352 | try { |
| 353 | sService.setBackupEnabled(isEnabled); |
| 354 | } catch (RemoteException e) { |
| 355 | Log.e(TAG, "setBackupEnabled() couldn't connect"); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Report whether the backup mechanism is currently enabled. |
| 362 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 363 | * @hide |
| 364 | */ |
| 365 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 366 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 367 | public boolean isBackupEnabled() { |
Christopher Tate | 9e07929 | 2014-09-09 15:10:56 -0700 | [diff] [blame] | 368 | checkServiceBinder(); |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 369 | if (sService != null) { |
| 370 | try { |
| 371 | return sService.isBackupEnabled(); |
| 372 | } catch (RemoteException e) { |
| 373 | Log.e(TAG, "isBackupEnabled() couldn't connect"); |
| 374 | } |
| 375 | } |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | /** |
Michal Karpinski | 96e0be0 | 2018-01-16 16:14:54 +0000 | [diff] [blame] | 380 | * Report whether the backup mechanism is currently active. |
| 381 | * When it is inactive, the device will not perform any backup operations, nor will it |
| 382 | * deliver data for restore, although clients can still safely call BackupManager methods. |
| 383 | * |
| 384 | * @hide |
| 385 | */ |
| 386 | @SystemApi |
| 387 | @RequiresPermission(android.Manifest.permission.BACKUP) |
| 388 | public boolean isBackupServiceActive(UserHandle user) { |
| 389 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, |
| 390 | "isBackupServiceActive"); |
| 391 | checkServiceBinder(); |
| 392 | if (sService != null) { |
| 393 | try { |
| 394 | return sService.isBackupServiceActive(user.getIdentifier()); |
| 395 | } catch (RemoteException e) { |
| 396 | Log.e(TAG, "isBackupEnabled() couldn't connect"); |
| 397 | } |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | /** |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 403 | * Enable/disable data restore at application install time. When enabled, app |
| 404 | * installation will include an attempt to fetch the app's historical data from |
| 405 | * the archival restore dataset (if any). When disabled, no such attempt will |
| 406 | * be made. |
| 407 | * |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 408 | * @hide |
| 409 | */ |
| 410 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 411 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | a63246d | 2014-08-15 11:12:13 -0700 | [diff] [blame] | 412 | public void setAutoRestore(boolean isEnabled) { |
| 413 | checkServiceBinder(); |
| 414 | if (sService != null) { |
| 415 | try { |
| 416 | sService.setAutoRestore(isEnabled); |
| 417 | } catch (RemoteException e) { |
| 418 | Log.e(TAG, "setAutoRestore() couldn't connect"); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 424 | * Identify the currently selected transport. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 425 | * @return The name of the currently active backup transport. In case of |
| 426 | * failure or if no transport is currently active, this method returns {@code null}. |
| 427 | * |
| 428 | * @hide |
| 429 | */ |
| 430 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 431 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 432 | public String getCurrentTransport() { |
| 433 | checkServiceBinder(); |
| 434 | if (sService != null) { |
| 435 | try { |
| 436 | return sService.getCurrentTransport(); |
| 437 | } catch (RemoteException e) { |
| 438 | Log.e(TAG, "getCurrentTransport() couldn't connect"); |
| 439 | } |
| 440 | } |
| 441 | return null; |
| 442 | } |
| 443 | |
| 444 | /** |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 445 | * Request a list of all available backup transports' names. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 446 | * |
| 447 | * @hide |
| 448 | */ |
| 449 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 450 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 451 | public String[] listAllTransports() { |
| 452 | checkServiceBinder(); |
| 453 | if (sService != null) { |
| 454 | try { |
| 455 | return sService.listAllTransports(); |
| 456 | } catch (RemoteException e) { |
| 457 | Log.e(TAG, "listAllTransports() couldn't connect"); |
| 458 | } |
| 459 | } |
| 460 | return null; |
| 461 | } |
| 462 | |
| 463 | /** |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 464 | * Update the attributes of the transport identified by {@code transportComponent}. If the |
| 465 | * specified transport has not been bound at least once (for registration), this call will be |
| 466 | * ignored. Only the host process of the transport can change its description, otherwise a |
| 467 | * {@link SecurityException} will be thrown. |
| 468 | * |
| 469 | * @param transportComponent The identity of the transport being described. |
| 470 | * @param name A {@link String} with the new name for the transport. This is NOT for |
| 471 | * identification. MUST NOT be {@code null}. |
| 472 | * @param configurationIntent An {@link Intent} that can be passed to |
| 473 | * {@link Context#startActivity} in order to launch the transport's configuration UI. It may |
| 474 | * be {@code null} if the transport does not offer any user-facing configuration UI. |
| 475 | * @param currentDestinationString A {@link String} describing the destination to which the |
| 476 | * transport is currently sending data. MUST NOT be {@code null}. |
| 477 | * @param dataManagementIntent An {@link Intent} that can be passed to |
| 478 | * {@link Context#startActivity} in order to launch the transport's data-management UI. It |
| 479 | * may be {@code null} if the transport does not offer any user-facing data |
| 480 | * management UI. |
| 481 | * @param dataManagementLabel A {@link String} to be used as the label for the transport's data |
| 482 | * management affordance. This MUST be {@code null} when dataManagementIntent is |
| 483 | * {@code null} and MUST NOT be {@code null} when dataManagementIntent is not {@code null}. |
| 484 | * @throws SecurityException If the UID of the calling process differs from the package UID of |
| 485 | * {@code transportComponent} or if the caller does NOT have BACKUP permission. |
| 486 | * |
| 487 | * @hide |
| 488 | */ |
| 489 | @SystemApi |
| 490 | @RequiresPermission(android.Manifest.permission.BACKUP) |
| 491 | public void updateTransportAttributes( |
| 492 | ComponentName transportComponent, |
| 493 | String name, |
| 494 | @Nullable Intent configurationIntent, |
| 495 | String currentDestinationString, |
| 496 | @Nullable Intent dataManagementIntent, |
| 497 | @Nullable String dataManagementLabel) { |
| 498 | checkServiceBinder(); |
| 499 | if (sService != null) { |
| 500 | try { |
| 501 | sService.updateTransportAttributes( |
| 502 | transportComponent, |
| 503 | name, |
| 504 | configurationIntent, |
| 505 | currentDestinationString, |
| 506 | dataManagementIntent, |
| 507 | dataManagementLabel); |
| 508 | } catch (RemoteException e) { |
| 509 | Log.e(TAG, "describeTransport() couldn't connect"); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /** |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 515 | * Specify the current backup transport. |
| 516 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 517 | * @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] | 518 | * of the names returned by {@link #listAllTransports()}. This is the String returned by |
| 519 | * {@link BackupTransport#name()} for the particular transport. |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 520 | * @return The name of the previously selected transport. If the given transport |
| 521 | * name is not one of the currently available transports, no change is made to |
| 522 | * the current transport setting and the method returns null. |
| 523 | * |
| 524 | * @hide |
| 525 | */ |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 526 | @Deprecated |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 527 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 528 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 529 | public String selectBackupTransport(String transport) { |
| 530 | checkServiceBinder(); |
| 531 | if (sService != null) { |
| 532 | try { |
| 533 | return sService.selectBackupTransport(transport); |
| 534 | } catch (RemoteException e) { |
| 535 | Log.e(TAG, "selectBackupTransport() couldn't connect"); |
| 536 | } |
| 537 | } |
| 538 | return null; |
| 539 | } |
| 540 | |
| 541 | /** |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 542 | * Specify the current backup transport and get notified when the transport is ready to be used. |
| 543 | * This method is async because BackupManager might need to bind to the specified transport |
| 544 | * which is in a separate process. |
| 545 | * |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 546 | * @param transport ComponentName of the service hosting the transport. This is different from |
| 547 | * the transport's name that is returned by {@link BackupTransport#name()}. |
| 548 | * @param listener A listener object to get a callback on the transport being selected. |
| 549 | * |
| 550 | * @hide |
| 551 | */ |
| 552 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 553 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 554 | public void selectBackupTransport(ComponentName transport, |
| 555 | SelectBackupTransportCallback listener) { |
| 556 | checkServiceBinder(); |
| 557 | if (sService != null) { |
| 558 | try { |
| 559 | SelectTransportListenerWrapper wrapper = listener == null ? |
| 560 | null : new SelectTransportListenerWrapper(mContext, listener); |
| 561 | sService.selectBackupTransportAsync(transport, wrapper); |
| 562 | } catch (RemoteException e) { |
| 563 | Log.e(TAG, "selectBackupTransportAsync() couldn't connect"); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 569 | * Schedule an immediate backup attempt for all pending key/value updates. This |
| 570 | * is primarily intended for transports to use when they detect a suitable |
| 571 | * opportunity for doing a backup pass. If there are no pending updates to |
| 572 | * be sent, no action will be taken. Even if some updates are pending, the |
| 573 | * transport will still be asked to confirm via the usual requestBackupTime() |
| 574 | * method. |
| 575 | * |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 576 | * @hide |
| 577 | */ |
| 578 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 579 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | d5cf722 | 2014-07-29 16:53:09 -0700 | [diff] [blame] | 580 | public void backupNow() { |
| 581 | checkServiceBinder(); |
| 582 | if (sService != null) { |
| 583 | try { |
| 584 | sService.backupNow(); |
| 585 | } catch (RemoteException e) { |
| 586 | Log.e(TAG, "backupNow() couldn't connect"); |
| 587 | } |
| 588 | } |
| 589 | } |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 590 | |
| 591 | /** |
| 592 | * Ask the framework which dataset, if any, the given package's data would be |
| 593 | * restored from if we were to install it right now. |
| 594 | * |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 595 | * @param packageName The name of the package whose most-suitable dataset we |
| 596 | * wish to look up |
| 597 | * @return The dataset token from which a restore should be attempted, or zero if |
| 598 | * no suitable data is available. |
| 599 | * |
| 600 | * @hide |
| 601 | */ |
| 602 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 603 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Christopher Tate | 511d02f | 2015-04-08 20:05:30 -0700 | [diff] [blame] | 604 | public long getAvailableRestoreToken(String packageName) { |
| 605 | checkServiceBinder(); |
| 606 | if (sService != null) { |
| 607 | try { |
| 608 | return sService.getAvailableRestoreToken(packageName); |
| 609 | } catch (RemoteException e) { |
| 610 | Log.e(TAG, "getAvailableRestoreToken() couldn't connect"); |
| 611 | } |
| 612 | } |
| 613 | return 0; |
| 614 | } |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 615 | |
| 616 | /** |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 617 | * Ask the framework whether this app is eligible for backup. |
| 618 | * |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 619 | * @param packageName The name of the package. |
| 620 | * @return Whether this app is eligible for backup. |
| 621 | * |
| 622 | * @hide |
| 623 | */ |
| 624 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 625 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Sergey Poromov | 9448196 | 2016-01-07 18:25:35 +0100 | [diff] [blame] | 626 | public boolean isAppEligibleForBackup(String packageName) { |
| 627 | checkServiceBinder(); |
| 628 | if (sService != null) { |
| 629 | try { |
| 630 | return sService.isAppEligibleForBackup(packageName); |
| 631 | } catch (RemoteException e) { |
| 632 | Log.e(TAG, "isAppEligibleForBackup(pkg) couldn't connect"); |
| 633 | } |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | /** |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 639 | * Request an immediate backup, providing an observer to which results of the backup operation |
| 640 | * will be published. The Android backup system will decide for each package whether it will |
| 641 | * be full app data backup or key/value-pair-based backup. |
| 642 | * |
| 643 | * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all |
| 644 | * provided packages using the remote transport. |
| 645 | * |
| 646 | * @param packages List of package names to backup. |
| 647 | * @param observer The {@link BackupObserver} to receive callbacks during the backup |
Sergey Poromov | 899edf6 | 2016-02-24 17:21:59 +0100 | [diff] [blame] | 648 | * operation. Could be {@code null}. |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 649 | * @return {@link BackupManager#SUCCESS} on success; nonzero on error. |
| 650 | * @exception IllegalArgumentException on null or empty {@code packages} param. |
| 651 | * |
| 652 | * @hide |
| 653 | */ |
| 654 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 655 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 656 | public int requestBackup(String[] packages, BackupObserver observer) { |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 657 | return requestBackup(packages, observer, null, 0); |
Shreyas Basarge | 38e7486 | 2017-01-11 17:15:58 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Request an immediate backup, providing an observer to which results of the backup operation |
| 662 | * will be published. The Android backup system will decide for each package whether it will |
| 663 | * be full app data backup or key/value-pair-based backup. |
| 664 | * |
| 665 | * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all |
| 666 | * provided packages using the remote transport. |
| 667 | * |
| 668 | * @param packages List of package names to backup. |
| 669 | * @param observer The {@link BackupObserver} to receive callbacks during the backup |
| 670 | * operation. Could be {@code null}. |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 671 | * @param monitor The {@link BackupManagerMonitorWrapper} to receive callbacks of important |
| 672 | * events during the backup operation. Could be {@code null}. |
Shreyas Basarge | 38e7486 | 2017-01-11 17:15:58 +0000 | [diff] [blame] | 673 | * @param flags {@link #FLAG_NON_INCREMENTAL_BACKUP}. |
| 674 | * @return {@link BackupManager#SUCCESS} on success; nonzero on error. |
| 675 | * @throws IllegalArgumentException on null or empty {@code packages} param. |
| 676 | * @hide |
| 677 | */ |
| 678 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 679 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 680 | public int requestBackup(String[] packages, BackupObserver observer, |
| 681 | BackupManagerMonitor monitor, int flags) { |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 682 | checkServiceBinder(); |
| 683 | if (sService != null) { |
| 684 | try { |
Sergey Poromov | 899edf6 | 2016-02-24 17:21:59 +0100 | [diff] [blame] | 685 | BackupObserverWrapper observerWrapper = observer == null |
| 686 | ? null |
| 687 | : new BackupObserverWrapper(mContext, observer); |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 688 | BackupManagerMonitorWrapper monitorWrapper = monitor == null |
| 689 | ? null |
| 690 | : new BackupManagerMonitorWrapper(monitor); |
| 691 | return sService.requestBackup(packages, observerWrapper, monitorWrapper, flags); |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 692 | } catch (RemoteException e) { |
| 693 | Log.e(TAG, "requestBackup() couldn't connect"); |
| 694 | } |
| 695 | } |
| 696 | return -1; |
| 697 | } |
| 698 | |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 699 | /** |
| 700 | * Cancel all running backups. After this call returns, no currently running backups will |
| 701 | * interact with the selected transport. |
| 702 | * |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 703 | * @hide |
| 704 | */ |
| 705 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 706 | @RequiresPermission(android.Manifest.permission.BACKUP) |
Shreyas Basarge | c370442 | 2017-01-28 16:50:09 +0000 | [diff] [blame] | 707 | public void cancelBackups() { |
| 708 | checkServiceBinder(); |
| 709 | if (sService != null) { |
| 710 | try { |
| 711 | sService.cancelBackups(); |
| 712 | } catch (RemoteException e) { |
| 713 | Log.e(TAG, "cancelBackups() couldn't connect."); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 718 | /* |
| 719 | * We wrap incoming binder calls with a private class implementation that |
| 720 | * redirects them into main-thread actions. This serializes the backup |
| 721 | * progress callbacks nicely within the usual main-thread lifecycle pattern. |
| 722 | */ |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 723 | private class BackupObserverWrapper extends IBackupObserver.Stub { |
| 724 | final Handler mHandler; |
| 725 | final BackupObserver mObserver; |
| 726 | |
| 727 | static final int MSG_UPDATE = 1; |
| 728 | static final int MSG_RESULT = 2; |
| 729 | static final int MSG_FINISHED = 3; |
| 730 | |
| 731 | BackupObserverWrapper(Context context, BackupObserver observer) { |
| 732 | mHandler = new Handler(context.getMainLooper()) { |
| 733 | @Override |
| 734 | public void handleMessage(Message msg) { |
| 735 | switch (msg.what) { |
| 736 | case MSG_UPDATE: |
| 737 | Pair<String, BackupProgress> obj = |
| 738 | (Pair<String, BackupProgress>) msg.obj; |
| 739 | mObserver.onUpdate(obj.first, obj.second); |
| 740 | break; |
| 741 | case MSG_RESULT: |
| 742 | mObserver.onResult((String)msg.obj, msg.arg1); |
| 743 | break; |
| 744 | case MSG_FINISHED: |
| 745 | mObserver.backupFinished(msg.arg1); |
| 746 | break; |
| 747 | default: |
| 748 | Log.w(TAG, "Unknown message: " + msg); |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | }; |
| 753 | mObserver = observer; |
| 754 | } |
| 755 | |
| 756 | // Binder calls into this object just enqueue on the main-thread handler |
| 757 | @Override |
| 758 | public void onUpdate(String currentPackage, BackupProgress backupProgress) { |
| 759 | mHandler.sendMessage( |
| 760 | mHandler.obtainMessage(MSG_UPDATE, Pair.create(currentPackage, backupProgress))); |
| 761 | } |
| 762 | |
| 763 | @Override |
| 764 | public void onResult(String currentPackage, int status) { |
| 765 | mHandler.sendMessage( |
Sergey Poromov | 4b51d99 | 2016-01-29 17:46:32 +0100 | [diff] [blame] | 766 | mHandler.obtainMessage(MSG_RESULT, status, 0, currentPackage)); |
Sergey Poromov | fe06bf6 | 2015-12-15 16:26:23 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | @Override |
| 770 | public void backupFinished(int status) { |
| 771 | mHandler.sendMessage( |
| 772 | mHandler.obtainMessage(MSG_FINISHED, status, 0)); |
| 773 | } |
| 774 | } |
Shreyas Basarge | 865303f | 2017-01-13 14:48:56 +0000 | [diff] [blame] | 775 | |
| 776 | private class SelectTransportListenerWrapper extends ISelectBackupTransportCallback.Stub { |
| 777 | |
| 778 | private final Handler mHandler; |
| 779 | private final SelectBackupTransportCallback mListener; |
| 780 | |
| 781 | SelectTransportListenerWrapper(Context context, SelectBackupTransportCallback listener) { |
| 782 | mHandler = new Handler(context.getMainLooper()); |
| 783 | mListener = listener; |
| 784 | } |
| 785 | |
| 786 | @Override |
| 787 | public void onSuccess(final String transportName) { |
| 788 | mHandler.post(new Runnable() { |
| 789 | @Override |
| 790 | public void run() { |
| 791 | mListener.onSuccess(transportName); |
| 792 | } |
| 793 | }); |
| 794 | } |
| 795 | |
| 796 | @Override |
| 797 | public void onFailure(final int reason) { |
| 798 | mHandler.post(new Runnable() { |
| 799 | @Override |
| 800 | public void run() { |
| 801 | mListener.onFailure(reason); |
| 802 | } |
| 803 | }); |
| 804 | } |
| 805 | } |
Stefanot | b1f573d | 2017-01-27 12:03:53 +0000 | [diff] [blame] | 806 | |
| 807 | private class BackupManagerMonitorWrapper extends IBackupManagerMonitor.Stub { |
| 808 | final BackupManagerMonitor mMonitor; |
| 809 | |
| 810 | BackupManagerMonitorWrapper(BackupManagerMonitor monitor) { |
| 811 | mMonitor = monitor; |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public void onEvent(final Bundle event) throws RemoteException { |
| 816 | mMonitor.onEvent(event); |
| 817 | } |
| 818 | } |
| 819 | |
Christopher Tate | a8bf815 | 2009-04-30 11:36:21 -0700 | [diff] [blame] | 820 | } |