blob: 12f4483141f409cce5d5c524669edda501d57995 [file] [log] [blame]
Christopher Tatea8bf8152009-04-30 11:36:21 -07001/*
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 Tate45281862010-03-05 15:46:30 -080017package android.app.backup;
Christopher Tatea8bf8152009-04-30 11:36:21 -070018
Bernardo Rufinoab953332017-11-22 22:10:32 +000019import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.RequiresPermission;
Christopher Tated5cf7222014-07-29 16:53:09 -070021import android.annotation.SystemApi;
Shreyas Basarge865303f2017-01-13 14:48:56 +000022import android.content.ComponentName;
Christopher Tatea8bf8152009-04-30 11:36:21 -070023import android.content.Context;
Bernardo Rufinoab953332017-11-22 22:10:32 +000024import android.content.Intent;
Stefanotb1f573d2017-01-27 12:03:53 +000025import android.os.Bundle;
Sergey Poromovfe06bf62015-12-15 16:26:23 +010026import android.os.Handler;
27import android.os.Message;
Christopher Tatea8bf8152009-04-30 11:36:21 -070028import android.os.RemoteException;
29import android.os.ServiceManager;
Michal Karpinski96e0be02018-01-16 16:14:54 +000030import android.os.UserHandle;
Christopher Tatec8daa762009-07-06 19:04:57 -070031import android.util.Log;
Sergey Poromovfe06bf62015-12-15 16:26:23 +010032import android.util.Pair;
Christopher Tatea8bf8152009-04-30 11:36:21 -070033
34/**
Scott Maind17da432010-04-29 21:42:58 -070035 * 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 Root5a20ea12010-02-23 18:49:11 -080038 * <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 Maind17da432010-04-29 21:42:58 -070045 * 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 Tate45281862010-03-05 15:46:30 -080047 * documentation for {@link android.app.backup.BackupAgent} for a detailed description
Christopher Tate4e14a822010-04-08 12:54:23 -070048 * of how the operation then proceeds.
Kenny Root5a20ea12010-02-23 18:49:11 -080049 * <p>
Christopher Tate4e14a822010-04-08 12:54:23 -070050 * Several attributes affecting the operation of the backup and restore mechanism
Joe Fernandez61fd1e82011-10-26 13:39:11 -070051 * can be set on the <code>
52 * <a href="{@docRoot}guide/topics/manifest/application-element.html">&lt;application&gt;</a></code>
Scott Maind17da432010-04-29 21:42:58 -070053 * tag in your application's AndroidManifest.xml file.
54 *
Joe Fernandez61fd1e82011-10-26 13:39:11 -070055 * <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 Root5a20ea12010-02-23 18:49:11 -080060 * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup
61 * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent
Kenny Root5a20ea12010-02-23 18:49:11 -080062 * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore
Christopher Tate4e14a822010-04-08 12:54:23 -070063 * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion
Christopher Tatea8bf8152009-04-30 11:36:21 -070064 */
65public class BackupManager {
Christopher Tatec8daa762009-07-06 19:04:57 -070066 private static final String TAG = "BackupManager";
Christopher Tatea8bf8152009-04-30 11:36:21 -070067
Sergey Poromovfe06bf62015-12-15 16:26:23 +010068 // 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 Basargec3704422017-01-28 16:50:09 +000096 * The backup operation was cancelled.
97 *
98 * @hide
99 */
100 @SystemApi
101 public static final int ERROR_BACKUP_CANCELLED = -2003;
102
103 /**
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100104 * 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 Poromov872d3b62016-01-12 15:48:08 +0100127 * 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 Poromovfe06bf62015-12-15 16:26:23 +0100137 * 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 Tate38c9c302016-09-21 17:18:10 -0700145 /**
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 Basarge38e74862017-01-11 17:15:58 +0000153 /**
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 Basarge865303f2017-01-13 14:48:56 +0000174
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 Tatec8daa762009-07-06 19:04:57 -0700193 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 Tate043dadc2009-06-02 16:11:00 -0700202
203 /**
Christopher Tatea8bf8152009-04-30 11:36:21 -0700204 * Constructs a BackupManager object through which the application can
205 * communicate with the Android backup system.
Christopher Tatec114eb52009-04-30 12:40:19 -0700206 *
Christopher Tatea8bf8152009-04-30 11:36:21 -0700207 * @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 Tatec114eb52009-04-30 12:40:19 -0700211 public BackupManager(Context context) {
Christopher Tatea8bf8152009-04-30 11:36:21 -0700212 mContext = context;
Christopher Tatea8bf8152009-04-30 11:36:21 -0700213 }
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 Tate4e14a822010-04-08 12:54:23 -0700218 * {@link android.app.backup.BackupAgent} subclass will be scheduled when you
219 * call this method.
Christopher Tatea8bf8152009-04-30 11:36:21 -0700220 */
221 public void dataChanged() {
Christopher Tatec8daa762009-07-06 19:04:57 -0700222 checkServiceBinder();
223 if (sService != null) {
Christopher Tate8a27f922009-06-26 11:49:18 -0700224 try {
Christopher Tatec8daa762009-07-06 19:04:57 -0700225 sService.dataChanged(mContext.getPackageName());
Christopher Tate8a27f922009-06-26 11:49:18 -0700226 } catch (RemoteException e) {
Christopher Tatec8daa762009-07-06 19:04:57 -0700227 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 Tate4e14a822010-04-08 12:54:23 -0700234 * needs a backup pass. This can be useful in the case of groups of packages
235 * that share a uid.
236 * <p>
Christopher Tatec8daa762009-07-06 19:04:57 -0700237 * This method requires that the application hold the "android.permission.BACKUP"
Christopher Tate4e14a822010-04-08 12:54:23 -0700238 * permission if the package named in the argument does not run under the same uid
239 * as the caller.
Scott Maind17da432010-04-29 21:42:58 -0700240 *
241 * @param packageName The package name identifying the application to back up.
Christopher Tatec8daa762009-07-06 19:04:57 -0700242 */
243 public static void dataChanged(String packageName) {
244 checkServiceBinder();
245 if (sService != null) {
246 try {
247 sService.dataChanged(packageName);
248 } catch (RemoteException e) {
Christopher Tated5cf7222014-07-29 16:53:09 -0700249 Log.e(TAG, "dataChanged(pkg) couldn't connect");
Christopher Tate8a27f922009-06-26 11:49:18 -0700250 }
Christopher Tatea8bf8152009-04-30 11:36:21 -0700251 }
252 }
Christopher Tate9b3905c2009-06-08 15:24:01 -0700253
254 /**
Christopher Tate9c3cee92010-03-25 16:06:43 -0700255 * 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 Maind17da432010-04-29 21:42:58 -0700267 * @param observer The {@link RestoreObserver} to receive callbacks during the restore
268 * operation. This must not be null.
269 *
Christopher Tate9c3cee92010-03-25 16:06:43 -0700270 * @return Zero on success; nonzero on error.
271 */
272 public int requestRestore(RestoreObserver observer) {
Stefanotf4e237c2017-02-06 21:14:05 +0000273 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 Tate9c3cee92010-03-25 16:06:43 -0700303 int result = -1;
304 checkServiceBinder();
305 if (sService != null) {
306 RestoreSession session = null;
307 try {
Chris Tate44ab8452010-11-16 15:10:49 -0800308 IRestoreSession binder = sService.beginRestoreSession(mContext.getPackageName(),
Stefanotf4e237c2017-02-06 21:14:05 +0000309 null);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700310 if (binder != null) {
311 session = new RestoreSession(mContext, binder);
Stefanotf4e237c2017-02-06 21:14:05 +0000312 result = session.restorePackage(mContext.getPackageName(), observer, monitor);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700313 }
Christopher Tate9c3cee92010-03-25 16:06:43 -0700314 } catch (RemoteException e) {
Christopher Tated5cf7222014-07-29 16:53:09 -0700315 Log.e(TAG, "restoreSelf() unable to contact service");
Christopher Tate9c3cee92010-03-25 16:06:43 -0700316 } finally {
317 if (session != null) {
318 session.endRestoreSession();
319 }
320 }
321 }
322 return result;
323 }
324
325 /**
Christopher Tatee28290e2010-02-16 15:22:26 -0800326 * Begin the process of restoring data from backup. See the
Christopher Tate45281862010-03-05 15:46:30 -0800327 * {@link android.app.backup.RestoreSession} class for documentation on that process.
Christopher Tate9c3cee92010-03-25 16:06:43 -0700328 * @hide
Christopher Tate9b3905c2009-06-08 15:24:01 -0700329 */
Christopher Tate2e597922014-08-07 14:53:06 -0700330 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600331 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tate80202c82010-01-25 19:37:47 -0800332 public RestoreSession beginRestoreSession() {
Christopher Tate80202c82010-01-25 19:37:47 -0800333 RestoreSession session = null;
Christopher Tatec8daa762009-07-06 19:04:57 -0700334 checkServiceBinder();
335 if (sService != null) {
Christopher Tate8a27f922009-06-26 11:49:18 -0700336 try {
Chris Tate44ab8452010-11-16 15:10:49 -0800337 // All packages, current transport
338 IRestoreSession binder = sService.beginRestoreSession(null, null);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700339 if (binder != null) {
340 session = new RestoreSession(mContext, binder);
341 }
Christopher Tate8a27f922009-06-26 11:49:18 -0700342 } catch (RemoteException e) {
Christopher Tated5cf7222014-07-29 16:53:09 -0700343 Log.e(TAG, "beginRestoreSession() couldn't connect");
Christopher Tate8a27f922009-06-26 11:49:18 -0700344 }
Christopher Tate9b3905c2009-06-08 15:24:01 -0700345 }
Christopher Tate80202c82010-01-25 19:37:47 -0800346 return session;
Christopher Tate9b3905c2009-06-08 15:24:01 -0700347 }
Christopher Tated5cf7222014-07-29 16:53:09 -0700348
Christopher Tated5cf7222014-07-29 16:53:09 -0700349 /**
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 Tated5cf7222014-07-29 16:53:09 -0700356 * @hide
357 */
358 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600359 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700360 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 Tated5cf7222014-07-29 16:53:09 -0700374 * @hide
375 */
376 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600377 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700378 public boolean isBackupEnabled() {
Christopher Tate9e079292014-09-09 15:10:56 -0700379 checkServiceBinder();
Christopher Tated5cf7222014-07-29 16:53:09 -0700380 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 Karpinski96e0be02018-01-16 16:14:54 +0000391 * 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 Tatea63246d2014-08-15 11:12:13 -0700414 * 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 Tatea63246d2014-08-15 11:12:13 -0700419 * @hide
420 */
421 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600422 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tatea63246d2014-08-15 11:12:13 -0700423 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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600435 * Identify the currently selected transport.
Christopher Tated5cf7222014-07-29 16:53:09 -0700436 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600442 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700443 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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600456 * Request a list of all available backup transports' names.
Christopher Tated5cf7222014-07-29 16:53:09 -0700457 *
458 * @hide
459 */
460 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600461 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700462 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 Rufinoab953332017-11-22 22:10:32 +0000475 * 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 Basarge865303f2017-01-13 14:48:56 +0000526 * Specify the current backup transport.
527 *
Christopher Tated5cf7222014-07-29 16:53:09 -0700528 * @param transport The name of the transport to select. This should be one
Shreyas Basarge865303f2017-01-13 14:48:56 +0000529 * of the names returned by {@link #listAllTransports()}. This is the String returned by
530 * {@link BackupTransport#name()} for the particular transport.
Christopher Tated5cf7222014-07-29 16:53:09 -0700531 * @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 Basarge865303f2017-01-13 14:48:56 +0000537 @Deprecated
Christopher Tated5cf7222014-07-29 16:53:09 -0700538 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600539 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700540 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 Basarge865303f2017-01-13 14:48:56 +0000553 * 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 Basarge865303f2017-01-13 14:48:56 +0000557 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600564 @RequiresPermission(android.Manifest.permission.BACKUP)
Shreyas Basarge865303f2017-01-13 14:48:56 +0000565 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 Tated5cf7222014-07-29 16:53:09 -0700580 * 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 Tated5cf7222014-07-29 16:53:09 -0700587 * @hide
588 */
589 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600590 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700591 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 Tate511d02f2015-04-08 20:05:30 -0700601
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 Tate511d02f2015-04-08 20:05:30 -0700606 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600614 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tate511d02f2015-04-08 20:05:30 -0700615 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 Poromovfe06bf62015-12-15 16:26:23 +0100626
627 /**
Sergey Poromov94481962016-01-07 18:25:35 +0100628 * Ask the framework whether this app is eligible for backup.
629 *
Sergey Poromov94481962016-01-07 18:25:35 +0100630 * @param packageName The name of the package.
631 * @return Whether this app is eligible for backup.
632 *
633 * @hide
634 */
635 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600636 @RequiresPermission(android.Manifest.permission.BACKUP)
Sergey Poromov94481962016-01-07 18:25:35 +0100637 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 Poromovfe06bf62015-12-15 16:26:23 +0100650 * 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 Poromov899edf62016-02-24 17:21:59 +0100659 * operation. Could be {@code null}.
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100660 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600666 @RequiresPermission(android.Manifest.permission.BACKUP)
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100667 public int requestBackup(String[] packages, BackupObserver observer) {
Stefanotb1f573d2017-01-27 12:03:53 +0000668 return requestBackup(packages, observer, null, 0);
Shreyas Basarge38e74862017-01-11 17:15:58 +0000669 }
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}.
Stefanotb1f573d2017-01-27 12:03:53 +0000682 * @param monitor The {@link BackupManagerMonitorWrapper} to receive callbacks of important
683 * events during the backup operation. Could be {@code null}.
Shreyas Basarge38e74862017-01-11 17:15:58 +0000684 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600690 @RequiresPermission(android.Manifest.permission.BACKUP)
Stefanotb1f573d2017-01-27 12:03:53 +0000691 public int requestBackup(String[] packages, BackupObserver observer,
692 BackupManagerMonitor monitor, int flags) {
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100693 checkServiceBinder();
694 if (sService != null) {
695 try {
Sergey Poromov899edf62016-02-24 17:21:59 +0100696 BackupObserverWrapper observerWrapper = observer == null
697 ? null
698 : new BackupObserverWrapper(mContext, observer);
Stefanotb1f573d2017-01-27 12:03:53 +0000699 BackupManagerMonitorWrapper monitorWrapper = monitor == null
700 ? null
701 : new BackupManagerMonitorWrapper(monitor);
702 return sService.requestBackup(packages, observerWrapper, monitorWrapper, flags);
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100703 } catch (RemoteException e) {
704 Log.e(TAG, "requestBackup() couldn't connect");
705 }
706 }
707 return -1;
708 }
709
Shreyas Basargec3704422017-01-28 16:50:09 +0000710 /**
711 * Cancel all running backups. After this call returns, no currently running backups will
712 * interact with the selected transport.
713 *
Shreyas Basargec3704422017-01-28 16:50:09 +0000714 * @hide
715 */
716 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600717 @RequiresPermission(android.Manifest.permission.BACKUP)
Shreyas Basargec3704422017-01-28 16:50:09 +0000718 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 Poromovfe06bf62015-12-15 16:26:23 +0100729 /*
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 Poromovfe06bf62015-12-15 16:26:23 +0100734 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 Poromov4b51d992016-01-29 17:46:32 +0100777 mHandler.obtainMessage(MSG_RESULT, status, 0, currentPackage));
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100778 }
779
780 @Override
781 public void backupFinished(int status) {
782 mHandler.sendMessage(
783 mHandler.obtainMessage(MSG_FINISHED, status, 0));
784 }
785 }
Shreyas Basarge865303f2017-01-13 14:48:56 +0000786
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 }
Stefanotb1f573d2017-01-27 12:03:53 +0000817
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 Tatea8bf8152009-04-30 11:36:21 -0700831}