blob: 6ec0969771c6729a721569927e458c2f372e30fc [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 /**
Michal Karpinski0df62ac2018-01-24 13:02:03 +0000255 * @deprecated Since Android P app can no longer request restoring of its backup.
256 *
Christopher Tate9c3cee92010-03-25 16:06:43 -0700257 * 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 Maind17da432010-04-29 21:42:58 -0700269 * @param observer The {@link RestoreObserver} to receive callbacks during the restore
270 * operation. This must not be null.
271 *
Christopher Tate9c3cee92010-03-25 16:06:43 -0700272 * @return Zero on success; nonzero on error.
273 */
Michal Karpinski0df62ac2018-01-24 13:02:03 +0000274 @Deprecated
Christopher Tate9c3cee92010-03-25 16:06:43 -0700275 public int requestRestore(RestoreObserver observer) {
Stefanotf4e237c2017-02-06 21:14:05 +0000276 return requestRestore(observer, null);
277 }
278
279 // system APIs start here
280
281 /**
Michal Karpinski0df62ac2018-01-24 13:02:03 +0000282 * @deprecated Since Android P app can no longer request restoring of its backup.
283 *
Stefanotf4e237c2017-02-06 21:14:05 +0000284 * 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 Karpinski0df62ac2018-01-24 13:02:03 +0000306 @Deprecated
Stefanotf4e237c2017-02-06 21:14:05 +0000307 @SystemApi
308 public int requestRestore(RestoreObserver observer, BackupManagerMonitor monitor) {
Michal Karpinski0df62ac2018-01-24 13:02:03 +0000309 Log.w(TAG, "requestRestore(): Since Android P app can no longer request restoring"
310 + " of its backup.");
311 return -1;
Christopher Tate9c3cee92010-03-25 16:06:43 -0700312 }
313
314 /**
Christopher Tatee28290e2010-02-16 15:22:26 -0800315 * Begin the process of restoring data from backup. See the
Christopher Tate45281862010-03-05 15:46:30 -0800316 * {@link android.app.backup.RestoreSession} class for documentation on that process.
Christopher Tate9c3cee92010-03-25 16:06:43 -0700317 * @hide
Christopher Tate9b3905c2009-06-08 15:24:01 -0700318 */
Christopher Tate2e597922014-08-07 14:53:06 -0700319 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600320 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tate80202c82010-01-25 19:37:47 -0800321 public RestoreSession beginRestoreSession() {
Christopher Tate80202c82010-01-25 19:37:47 -0800322 RestoreSession session = null;
Christopher Tatec8daa762009-07-06 19:04:57 -0700323 checkServiceBinder();
324 if (sService != null) {
Christopher Tate8a27f922009-06-26 11:49:18 -0700325 try {
Chris Tate44ab8452010-11-16 15:10:49 -0800326 // All packages, current transport
327 IRestoreSession binder = sService.beginRestoreSession(null, null);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700328 if (binder != null) {
329 session = new RestoreSession(mContext, binder);
330 }
Christopher Tate8a27f922009-06-26 11:49:18 -0700331 } catch (RemoteException e) {
Christopher Tated5cf7222014-07-29 16:53:09 -0700332 Log.e(TAG, "beginRestoreSession() couldn't connect");
Christopher Tate8a27f922009-06-26 11:49:18 -0700333 }
Christopher Tate9b3905c2009-06-08 15:24:01 -0700334 }
Christopher Tate80202c82010-01-25 19:37:47 -0800335 return session;
Christopher Tate9b3905c2009-06-08 15:24:01 -0700336 }
Christopher Tated5cf7222014-07-29 16:53:09 -0700337
Christopher Tated5cf7222014-07-29 16:53:09 -0700338 /**
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 Tated5cf7222014-07-29 16:53:09 -0700345 * @hide
346 */
347 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600348 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700349 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 Tated5cf7222014-07-29 16:53:09 -0700363 * @hide
364 */
365 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600366 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700367 public boolean isBackupEnabled() {
Christopher Tate9e079292014-09-09 15:10:56 -0700368 checkServiceBinder();
Christopher Tated5cf7222014-07-29 16:53:09 -0700369 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 Karpinski96e0be02018-01-16 16:14:54 +0000380 * 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 Tatea63246d2014-08-15 11:12:13 -0700403 * 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 Tatea63246d2014-08-15 11:12:13 -0700408 * @hide
409 */
410 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600411 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tatea63246d2014-08-15 11:12:13 -0700412 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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600424 * Identify the currently selected transport.
Christopher Tated5cf7222014-07-29 16:53:09 -0700425 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600431 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700432 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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600445 * Request a list of all available backup transports' names.
Christopher Tated5cf7222014-07-29 16:53:09 -0700446 *
447 * @hide
448 */
449 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600450 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700451 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 Rufinoab953332017-11-22 22:10:32 +0000464 * 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 Basarge865303f2017-01-13 14:48:56 +0000515 * Specify the current backup transport.
516 *
Christopher Tated5cf7222014-07-29 16:53:09 -0700517 * @param transport The name of the transport to select. This should be one
Shreyas Basarge865303f2017-01-13 14:48:56 +0000518 * of the names returned by {@link #listAllTransports()}. This is the String returned by
519 * {@link BackupTransport#name()} for the particular transport.
Christopher Tated5cf7222014-07-29 16:53:09 -0700520 * @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 Basarge865303f2017-01-13 14:48:56 +0000526 @Deprecated
Christopher Tated5cf7222014-07-29 16:53:09 -0700527 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600528 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700529 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 Basarge865303f2017-01-13 14:48:56 +0000542 * 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 Basarge865303f2017-01-13 14:48:56 +0000546 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600553 @RequiresPermission(android.Manifest.permission.BACKUP)
Shreyas Basarge865303f2017-01-13 14:48:56 +0000554 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 Tated5cf7222014-07-29 16:53:09 -0700569 * 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 Tated5cf7222014-07-29 16:53:09 -0700576 * @hide
577 */
578 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600579 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tated5cf7222014-07-29 16:53:09 -0700580 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 Tate511d02f2015-04-08 20:05:30 -0700590
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 Tate511d02f2015-04-08 20:05:30 -0700595 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600603 @RequiresPermission(android.Manifest.permission.BACKUP)
Christopher Tate511d02f2015-04-08 20:05:30 -0700604 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 Poromovfe06bf62015-12-15 16:26:23 +0100615
616 /**
Sergey Poromov94481962016-01-07 18:25:35 +0100617 * Ask the framework whether this app is eligible for backup.
618 *
Sergey Poromov94481962016-01-07 18:25:35 +0100619 * @param packageName The name of the package.
620 * @return Whether this app is eligible for backup.
621 *
622 * @hide
623 */
624 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600625 @RequiresPermission(android.Manifest.permission.BACKUP)
Sergey Poromov94481962016-01-07 18:25:35 +0100626 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 Poromovfe06bf62015-12-15 16:26:23 +0100639 * 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 Poromov899edf62016-02-24 17:21:59 +0100648 * operation. Could be {@code null}.
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100649 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600655 @RequiresPermission(android.Manifest.permission.BACKUP)
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100656 public int requestBackup(String[] packages, BackupObserver observer) {
Stefanotb1f573d2017-01-27 12:03:53 +0000657 return requestBackup(packages, observer, null, 0);
Shreyas Basarge38e74862017-01-11 17:15:58 +0000658 }
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}.
Stefanotb1f573d2017-01-27 12:03:53 +0000671 * @param monitor The {@link BackupManagerMonitorWrapper} to receive callbacks of important
672 * events during the backup operation. Could be {@code null}.
Shreyas Basarge38e74862017-01-11 17:15:58 +0000673 * @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 Sharkeyd86b8fe2017-06-02 17:36:26 -0600679 @RequiresPermission(android.Manifest.permission.BACKUP)
Stefanotb1f573d2017-01-27 12:03:53 +0000680 public int requestBackup(String[] packages, BackupObserver observer,
681 BackupManagerMonitor monitor, int flags) {
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100682 checkServiceBinder();
683 if (sService != null) {
684 try {
Sergey Poromov899edf62016-02-24 17:21:59 +0100685 BackupObserverWrapper observerWrapper = observer == null
686 ? null
687 : new BackupObserverWrapper(mContext, observer);
Stefanotb1f573d2017-01-27 12:03:53 +0000688 BackupManagerMonitorWrapper monitorWrapper = monitor == null
689 ? null
690 : new BackupManagerMonitorWrapper(monitor);
691 return sService.requestBackup(packages, observerWrapper, monitorWrapper, flags);
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100692 } catch (RemoteException e) {
693 Log.e(TAG, "requestBackup() couldn't connect");
694 }
695 }
696 return -1;
697 }
698
Shreyas Basargec3704422017-01-28 16:50:09 +0000699 /**
700 * Cancel all running backups. After this call returns, no currently running backups will
701 * interact with the selected transport.
702 *
Shreyas Basargec3704422017-01-28 16:50:09 +0000703 * @hide
704 */
705 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600706 @RequiresPermission(android.Manifest.permission.BACKUP)
Shreyas Basargec3704422017-01-28 16:50:09 +0000707 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 Poromovfe06bf62015-12-15 16:26:23 +0100718 /*
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 Poromovfe06bf62015-12-15 16:26:23 +0100723 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 Poromov4b51d992016-01-29 17:46:32 +0100766 mHandler.obtainMessage(MSG_RESULT, status, 0, currentPackage));
Sergey Poromovfe06bf62015-12-15 16:26:23 +0100767 }
768
769 @Override
770 public void backupFinished(int status) {
771 mHandler.sendMessage(
772 mHandler.obtainMessage(MSG_FINISHED, status, 0));
773 }
774 }
Shreyas Basarge865303f2017-01-13 14:48:56 +0000775
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 }
Stefanotb1f573d2017-01-27 12:03:53 +0000806
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 Tatea8bf8152009-04-30 11:36:21 -0700820}