1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.backup;
18 
19 import android.annotation.Nullable;
20 import android.annotation.SystemApi;
21 import android.content.Intent;
22 import android.content.pm.PackageInfo;
23 import android.os.IBinder;
24 import android.os.ParcelFileDescriptor;
25 import android.os.RemoteException;
26 
27 import com.android.internal.backup.IBackupTransport;
28 
29 /**
30  * Concrete class that provides a stable-API bridge between IBackupTransport
31  * and its implementations.
32  *
33  * @hide
34  */
35 @SystemApi
36 public class BackupTransport {
37     // Zero return always means things are okay.  If returned from
38     // getNextFullRestoreDataChunk(), it means that no data could be delivered at
39     // this time, but the restore is still running and the caller should simply
40     // retry.
41     public static final int TRANSPORT_OK = 0;
42 
43     // -1 is special; it is used in getNextFullRestoreDataChunk() to indicate that
44     // we've delivered the entire data stream for the current restore target.
45     public static final int NO_MORE_DATA = -1;
46 
47     // Result codes that indicate real errors are negative and not -1
48     public static final int TRANSPORT_ERROR = -1000;
49     public static final int TRANSPORT_NOT_INITIALIZED = -1001;
50     public static final int TRANSPORT_PACKAGE_REJECTED = -1002;
51     public static final int AGENT_ERROR = -1003;
52     public static final int AGENT_UNKNOWN = -1004;
53     public static final int TRANSPORT_QUOTA_EXCEEDED = -1005;
54 
55     /**
56      * Indicates that the transport cannot accept a diff backup for this package.
57      *
58      * <p>Backup manager should clear its state for this package and immediately retry a
59      * non-incremental backup. This might be used if the transport no longer has data for this
60      * package in its backing store.
61      *
62      * <p>This is only valid when backup manager called {@link
63      * #performBackup(PackageInfo, ParcelFileDescriptor, int)} with {@link #FLAG_INCREMENTAL}.
64      */
65     public static final int TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED = -1006;
66 
67     // Indicates that operation was initiated by user, not a scheduled one.
68     // Transport should ignore its own moratoriums for call with this flag set.
69     public static final int FLAG_USER_INITIATED = 1;
70 
71     /**
72      * For key value backup, indicates that the backup data is a diff from a previous backup. The
73      * transport must apply this diff to an existing backup to build the new backup set.
74      *
75      * @see #performBackup(PackageInfo, ParcelFileDescriptor, int)
76      */
77     public static final int FLAG_INCREMENTAL = 1 << 1;
78 
79     /**
80      * For key value backup, indicates that the backup data is a complete set, not a diff from a
81      * previous backup. The transport should clear any previous backup when storing this backup.
82      *
83      * @see #performBackup(PackageInfo, ParcelFileDescriptor, int)
84      */
85     public static final int FLAG_NON_INCREMENTAL = 1 << 2;
86 
87     /**
88      * Used as a boolean extra in the binding intent of transports. We pass {@code true} to
89      * notify transports that the current connection is used for registering the transport.
90      */
91     public static final String EXTRA_TRANSPORT_REGISTRATION =
92             "android.app.backup.extra.TRANSPORT_REGISTRATION";
93 
94     IBackupTransport mBinderImpl = new TransportImpl();
95 
getBinder()96     public IBinder getBinder() {
97         return mBinderImpl.asBinder();
98     }
99 
100     // ------------------------------------------------------------------------------------
101     // Transport self-description and general configuration interfaces
102     //
103 
104     /**
105      * Ask the transport for the name under which it should be registered.  This will
106      * typically be its host service's component name, but need not be.
107      */
name()108     public String name() {
109         throw new UnsupportedOperationException("Transport name() not implemented");
110     }
111 
112     /**
113      * Ask the transport for an Intent that can be used to launch any internal
114      * configuration Activity that it wishes to present.  For example, the transport
115      * may offer a UI for allowing the user to supply login credentials for the
116      * transport's off-device backend.
117      *
118      * <p>If the transport does not supply any user-facing configuration UI, it should
119      * return {@code null} from this method.
120      *
121      * @return An Intent that can be passed to Context.startActivity() in order to
122      *         launch the transport's configuration UI.  This method will return {@code null}
123      *         if the transport does not offer any user-facing configuration UI.
124      */
configurationIntent()125     public Intent configurationIntent() {
126         return null;
127     }
128 
129     /**
130      * On demand, supply a one-line string that can be shown to the user that
131      * describes the current backend destination.  For example, a transport that
132      * can potentially associate backup data with arbitrary user accounts should
133      * include the name of the currently-active account here.
134      *
135      * @return A string describing the destination to which the transport is currently
136      *         sending data.  This method should not return null.
137      */
currentDestinationString()138     public String currentDestinationString() {
139         throw new UnsupportedOperationException(
140                 "Transport currentDestinationString() not implemented");
141     }
142 
143     /**
144      * Ask the transport for an Intent that can be used to launch a more detailed
145      * secondary data management activity.  For example, the configuration intent might
146      * be one for allowing the user to select which account they wish to associate
147      * their backups with, and the management intent might be one which presents a
148      * UI for managing the data on the backend.
149      *
150      * <p>In the Settings UI, the configuration intent will typically be invoked
151      * when the user taps on the preferences item labeled with the current
152      * destination string, and the management intent will be placed in an overflow
153      * menu labelled with the management label string.
154      *
155      * <p>If the transport does not supply any user-facing data management
156      * UI, then it should return {@code null} from this method.
157      *
158      * @return An intent that can be passed to Context.startActivity() in order to
159      *         launch the transport's data-management UI.  This method will return
160      *         {@code null} if the transport does not offer any user-facing data
161      *         management UI.
162      */
dataManagementIntent()163     public Intent dataManagementIntent() {
164         return null;
165     }
166 
167     /**
168      * On demand, supply a short string that can be shown to the user as the label on an overflow
169      * menu item used to invoke the data management UI.
170      *
171      * @return A string to be used as the label for the transport's data management affordance. If
172      *     the transport supplies a data management intent, this method must not return {@code
173      *     null}.
174      * @deprecated Since Android Q, please use the variant {@link #dataManagementIntentLabel()}
175      *     instead.
176      */
177     @Deprecated
178     @Nullable
dataManagementLabel()179     public String dataManagementLabel() {
180         throw new UnsupportedOperationException(
181                 "Transport dataManagementLabel() not implemented");
182     }
183 
184     /**
185      * On demand, supply a short CharSequence that can be shown to the user as the label on an
186      * overflow menu item used to invoke the data management UI.
187      *
188      * @return A CharSequence to be used as the label for the transport's data management
189      *     affordance. If the transport supplies a data management intent, this method must not
190      *     return {@code null}.
191      */
192     @Nullable
dataManagementIntentLabel()193     public CharSequence dataManagementIntentLabel() {
194         return dataManagementLabel();
195     }
196 
197     /**
198      * Ask the transport where, on local device storage, to keep backup state blobs.
199      * This is per-transport so that mock transports used for testing can coexist with
200      * "live" backup services without interfering with the live bookkeeping.  The
201      * returned string should be a name that is expected to be unambiguous among all
202      * available backup transports; the name of the class implementing the transport
203      * is a good choice.
204      *
205      * @return A unique name, suitable for use as a file or directory name, that the
206      *         Backup Manager could use to disambiguate state files associated with
207      *         different backup transports.
208      */
transportDirName()209     public String transportDirName() {
210         throw new UnsupportedOperationException(
211                 "Transport transportDirName() not implemented");
212     }
213 
214     // ------------------------------------------------------------------------------------
215     // Device-level operations common to both key/value and full-data storage
216 
217     /**
218      * Initialize the server side storage for this device, erasing all stored data.
219      * The transport may send the request immediately, or may buffer it.  After
220      * this is called, {@link #finishBackup} will be called to ensure the request
221      * is sent and received successfully.
222      *
223      * <p>If the transport returns anything other than TRANSPORT_OK from this method,
224      * the OS will halt the current initialize operation and schedule a retry in the
225      * near future.  Even if the transport is in a state such that attempting to
226      * "initialize" the backend storage is meaningless -- for example, if there is
227      * no current live dataset at all, or there is no authenticated account under which
228      * to store the data remotely -- the transport should return TRANSPORT_OK here
229      * and treat the initializeDevice() / finishBackup() pair as a graceful no-op.
230      *
231      * @return One of {@link BackupTransport#TRANSPORT_OK} (OK so far) or
232      *   {@link BackupTransport#TRANSPORT_ERROR} (to retry following network error
233      *   or other failure).
234      */
initializeDevice()235     public int initializeDevice() {
236         return BackupTransport.TRANSPORT_ERROR;
237     }
238 
239     /**
240      * Erase the given application's data from the backup destination.  This clears
241      * out the given package's data from the current backup set, making it as though
242      * the app had never yet been backed up.  After this is called, {@link finishBackup}
243      * must be called to ensure that the operation is recorded successfully.
244      *
245      * @return the same error codes as {@link #performBackup}.
246      */
clearBackupData(PackageInfo packageInfo)247     public int clearBackupData(PackageInfo packageInfo) {
248         return BackupTransport.TRANSPORT_ERROR;
249     }
250 
251     /**
252      * Finish sending application data to the backup destination.  This must be
253      * called after {@link #performBackup}, {@link #performFullBackup}, or {@link clearBackupData}
254      * to ensure that all data is sent and the operation properly finalized.  Only when this
255      * method returns true can a backup be assumed to have succeeded.
256      *
257      * @return the same error codes as {@link #performBackup} or {@link #performFullBackup}.
258      */
finishBackup()259     public int finishBackup() {
260         return BackupTransport.TRANSPORT_ERROR;
261     }
262 
263     // ------------------------------------------------------------------------------------
264     // Key/value incremental backup support interfaces
265 
266     /**
267      * Verify that this is a suitable time for a key/value backup pass.  This should return zero
268      * if a backup is reasonable right now, some positive value otherwise.  This method
269      * will be called outside of the {@link #performBackup}/{@link #finishBackup} pair.
270      *
271      * <p>If this is not a suitable time for a backup, the transport should return a
272      * backoff delay, in milliseconds, after which the Backup Manager should try again.
273      *
274      * @return Zero if this is a suitable time for a backup pass, or a positive time delay
275      *   in milliseconds to suggest deferring the backup pass for a while.
276      */
requestBackupTime()277     public long requestBackupTime() {
278         return 0;
279     }
280 
281     /**
282      * Send one application's key/value data update to the backup destination.  The
283      * transport may send the data immediately, or may buffer it.  If this method returns
284      * {@link #TRANSPORT_OK}, {@link #finishBackup} will then be called to ensure the data
285      * is sent and recorded successfully.
286      *
287      * If the backup data is a diff against the previous backup then the flag {@link
288      * BackupTransport#FLAG_INCREMENTAL} will be set. Otherwise, if the data is a complete backup
289      * set then {@link BackupTransport#FLAG_NON_INCREMENTAL} will be set. Before P neither flag will
290      * be set regardless of whether the backup is incremental or not.
291      *
292      * <p>If {@link BackupTransport#FLAG_INCREMENTAL} is set and the transport does not have data
293      * for this package in its storage backend then it cannot apply the incremental diff. Thus it
294      * should return {@link BackupTransport#TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED} to indicate
295      * that backup manager should delete its state and retry the package as a non-incremental
296      * backup. Before P, or if this is a non-incremental backup, then this return code is equivalent
297      * to {@link BackupTransport#TRANSPORT_ERROR}.
298      *
299      * @param packageInfo The identity of the application whose data is being backed up.
300      *   This specifically includes the signature list for the package.
301      * @param inFd Descriptor of file with data that resulted from invoking the application's
302      *   BackupService.doBackup() method.  This may be a pipe rather than a file on
303      *   persistent media, so it may not be seekable.
304      * @param flags a combination of {@link BackupTransport#FLAG_USER_INITIATED}, {@link
305      *   BackupTransport#FLAG_NON_INCREMENTAL}, {@link BackupTransport#FLAG_INCREMENTAL}, or 0.
306      * @return one of {@link BackupTransport#TRANSPORT_OK} (OK so far),
307      *  {@link BackupTransport#TRANSPORT_PACKAGE_REJECTED} (to suppress backup of this
308      *  specific package, but allow others to proceed),
309      *  {@link BackupTransport#TRANSPORT_ERROR} (on network error or other failure), {@link
310      *  BackupTransport#TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED} (if the transport cannot accept
311      *  an incremental backup for this package), or {@link
312      *  BackupTransport#TRANSPORT_NOT_INITIALIZED} (if the backend dataset has become lost due to
313      *  inactivity purge or some other reason and needs re-initializing)
314      */
performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd, int flags)315     public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd, int flags) {
316         return performBackup(packageInfo, inFd);
317     }
318 
319     /**
320      * Legacy version of {@link #performBackup(PackageInfo, ParcelFileDescriptor, int)} that
321      * doesn't use flags parameter.
322      */
performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd)323     public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd) {
324         return BackupTransport.TRANSPORT_ERROR;
325     }
326 
327     // ------------------------------------------------------------------------------------
328     // Key/value dataset restore interfaces
329 
330     /**
331      * Get the set of all backups currently available over this transport.
332      *
333      * @return Descriptions of the set of restore images available for this device,
334      *   or null if an error occurred (the attempt should be rescheduled).
335      **/
getAvailableRestoreSets()336     public RestoreSet[] getAvailableRestoreSets() {
337         return null;
338     }
339 
340     /**
341      * Get the identifying token of the backup set currently being stored from
342      * this device.  This is used in the case of applications wishing to restore
343      * their last-known-good data.
344      *
345      * @return A token that can be passed to {@link #startRestore}, or 0 if there
346      *   is no backup set available corresponding to the current device state.
347      */
getCurrentRestoreSet()348     public long getCurrentRestoreSet() {
349         return 0;
350     }
351 
352     /**
353      * Start restoring application data from backup.  After calling this function,
354      * alternate calls to {@link #nextRestorePackage} and {@link #nextRestoreData}
355      * to walk through the actual application data.
356      *
357      * @param token A backup token as returned by {@link #getAvailableRestoreSets}
358      *   or {@link #getCurrentRestoreSet}.
359      * @param packages List of applications to restore (if data is available).
360      *   Application data will be restored in the order given.
361      * @return One of {@link BackupTransport#TRANSPORT_OK} (OK so far, call
362      *   {@link #nextRestorePackage}) or {@link BackupTransport#TRANSPORT_ERROR}
363      *   (an error occurred, the restore should be aborted and rescheduled).
364      */
startRestore(long token, PackageInfo[] packages)365     public int startRestore(long token, PackageInfo[] packages) {
366         return BackupTransport.TRANSPORT_ERROR;
367     }
368 
369     /**
370      * Get the package name of the next application with data in the backup store, plus
371      * a description of the structure of the restored archive: either TYPE_KEY_VALUE for
372      * an original-API key/value dataset, or TYPE_FULL_STREAM for a tarball-type archive stream.
373      *
374      * <p>If the package name in the returned RestoreDescription object is the singleton
375      * {@link RestoreDescription#NO_MORE_PACKAGES}, it indicates that no further data is available
376      * in the current restore session: all packages described in startRestore() have been
377      * processed.
378      *
379      * <p>If this method returns {@code null}, it means that a transport-level error has
380      * occurred and the entire restore operation should be abandoned.
381      *
382      * <p class="note">The OS may call {@link #nextRestorePackage()} multiple times
383      * before calling either {@link #getRestoreData(ParcelFileDescriptor) getRestoreData()}
384      * or {@link #getNextFullRestoreDataChunk(ParcelFileDescriptor) getNextFullRestoreDataChunk()}.
385      * It does this when it has determined that it needs to skip restore of one or more
386      * packages.  The transport should not actually transfer any restore data for
387      * the given package in response to {@link #nextRestorePackage()}, but rather wait
388      * for an explicit request before doing so.
389      *
390      * @return A RestoreDescription object containing the name of one of the packages
391      *   supplied to {@link #startRestore} plus an indicator of the data type of that
392      *   restore data; or {@link RestoreDescription#NO_MORE_PACKAGES} to indicate that
393      *   no more packages can be restored in this session; or {@code null} to indicate
394      *   a transport-level error.
395      */
nextRestorePackage()396     public RestoreDescription nextRestorePackage() {
397         return null;
398     }
399 
400     /**
401      * Get the data for the application returned by {@link #nextRestorePackage}, if that
402      * method reported {@link RestoreDescription#TYPE_KEY_VALUE} as its delivery type.
403      * If the package has only TYPE_FULL_STREAM data, then this method will return an
404      * error.
405      *
406      * @param data An open, writable file into which the key/value backup data should be stored.
407      * @return the same error codes as {@link #startRestore}.
408      */
getRestoreData(ParcelFileDescriptor outFd)409     public int getRestoreData(ParcelFileDescriptor outFd) {
410         return BackupTransport.TRANSPORT_ERROR;
411     }
412 
413     /**
414      * End a restore session (aborting any in-process data transfer as necessary),
415      * freeing any resources and connections used during the restore process.
416      */
finishRestore()417     public void finishRestore() {
418         throw new UnsupportedOperationException(
419                 "Transport finishRestore() not implemented");
420     }
421 
422     // ------------------------------------------------------------------------------------
423     // Full backup interfaces
424 
425     /**
426      * Verify that this is a suitable time for a full-data backup pass.  This should return zero
427      * if a backup is reasonable right now, some positive value otherwise.  This method
428      * will be called outside of the {@link #performFullBackup}/{@link #finishBackup} pair.
429      *
430      * <p>If this is not a suitable time for a backup, the transport should return a
431      * backoff delay, in milliseconds, after which the Backup Manager should try again.
432      *
433      * @return Zero if this is a suitable time for a backup pass, or a positive time delay
434      *   in milliseconds to suggest deferring the backup pass for a while.
435      *
436      * @see #requestBackupTime()
437      */
requestFullBackupTime()438     public long requestFullBackupTime() {
439         return 0;
440     }
441 
442     /**
443      * Begin the process of sending an application's full-data archive to the backend.
444      * The description of the package whose data will be delivered is provided, as well as
445      * the socket file descriptor on which the transport will receive the data itself.
446      *
447      * <p>If the package is not eligible for backup, the transport should return
448      * {@link BackupTransport#TRANSPORT_PACKAGE_REJECTED}.  In this case the system will
449      * simply proceed with the next candidate if any, or finish the full backup operation
450      * if all apps have been processed.
451      *
452      * <p>After the transport returns {@link BackupTransport#TRANSPORT_OK} from this
453      * method, the OS will proceed to call {@link #sendBackupData()} one or more times
454      * to deliver the application's data as a streamed tarball.  The transport should not
455      * read() from the socket except as instructed to via the {@link #sendBackupData(int)}
456      * method.
457      *
458      * <p>After all data has been delivered to the transport, the system will call
459      * {@link #finishBackup()}.  At this point the transport should commit the data to
460      * its datastore, if appropriate, and close the socket that had been provided in
461      * {@link #performFullBackup(PackageInfo, ParcelFileDescriptor)}.
462      *
463      * <p class="note">If the transport returns TRANSPORT_OK from this method, then the
464      * OS will always provide a matching call to {@link #finishBackup()} even if sending
465      * data via {@link #sendBackupData(int)} failed at some point.
466      *
467      * @param targetPackage The package whose data is to follow.
468      * @param socket The socket file descriptor through which the data will be provided.
469      *    If the transport returns {@link #TRANSPORT_PACKAGE_REJECTED} here, it must still
470      *    close this file descriptor now; otherwise it should be cached for use during
471      *    succeeding calls to {@link #sendBackupData(int)}, and closed in response to
472      *    {@link #finishBackup()}.
473      * @param flags {@link BackupTransport#FLAG_USER_INITIATED} or 0.
474      * @return TRANSPORT_PACKAGE_REJECTED to indicate that the stated application is not
475      *    to be backed up; TRANSPORT_OK to indicate that the OS may proceed with delivering
476      *    backup data; TRANSPORT_ERROR to indicate a fatal error condition that precludes
477      *    performing a backup at this time.
478      */
performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket, int flags)479     public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket,
480             int flags) {
481         return performFullBackup(targetPackage, socket);
482     }
483 
484     /**
485      * Legacy version of {@link #performFullBackup(PackageInfo, ParcelFileDescriptor, int)} that
486      * doesn't use flags parameter.
487      */
performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket)488     public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) {
489         return BackupTransport.TRANSPORT_PACKAGE_REJECTED;
490     }
491 
492     /**
493      * Called after {@link #performFullBackup} to make sure that the transport is willing to
494      * handle a full-data backup operation of the specified size on the current package.
495      * If the transport returns anything other than TRANSPORT_OK, the package's backup
496      * operation will be skipped (and {@link #finishBackup() invoked} with no data for that
497      * package being passed to {@link #sendBackupData}.
498      *
499      * <p class="note">The platform does no size-based rejection of full backup attempts on
500      * its own: it is always the responsibility of the transport to implement its own policy.
501      * In particular, even if the preflighted payload size is zero, the platform will still call
502      * this method and will proceed to back up an archive metadata header with no file content
503      * if this method returns TRANSPORT_OK.  To avoid storing such payloads the transport
504      * must recognize this case and return TRANSPORT_PACKAGE_REJECTED.
505      *
506      * Added in {@link android.os.Build.VERSION_CODES#M}.
507      *
508      * @param size The estimated size of the full-data payload for this app.  This includes
509      *         manifest and archive format overhead, but is not guaranteed to be precise.
510      * @return TRANSPORT_OK if the platform is to proceed with the full-data backup,
511      *         TRANSPORT_PACKAGE_REJECTED if the proposed payload size is too large for
512      *         the transport to handle, or TRANSPORT_ERROR to indicate a fatal error
513      *         condition that means the platform cannot perform a backup at this time.
514      */
checkFullBackupSize(long size)515     public int checkFullBackupSize(long size) {
516         return BackupTransport.TRANSPORT_OK;
517     }
518 
519     /**
520      * Tells the transport to read {@code numBytes} bytes of data from the socket file
521      * descriptor provided in the {@link #performFullBackup(PackageInfo, ParcelFileDescriptor)}
522      * call, and deliver those bytes to the datastore.
523      *
524      * @param numBytes The number of bytes of tarball data available to be read from the
525      *    socket.
526      * @return TRANSPORT_OK on successful processing of the data; TRANSPORT_ERROR to
527      *    indicate a fatal error situation.  If an error is returned, the system will
528      *    call finishBackup() and stop attempting backups until after a backoff and retry
529      *    interval.
530      */
sendBackupData(int numBytes)531     public int sendBackupData(int numBytes) {
532         return BackupTransport.TRANSPORT_ERROR;
533     }
534 
535     /**
536      * Tells the transport to cancel the currently-ongoing full backup operation.  This
537      * will happen between {@link #performFullBackup()} and {@link #finishBackup()}
538      * if the OS needs to abort the backup operation for any reason, such as a crash in
539      * the application undergoing backup.
540      *
541      * <p>When it receives this call, the transport should discard any partial archive
542      * that it has stored so far.  If possible it should also roll back to the previous
543      * known-good archive in its datastore.
544      *
545      * <p>If the transport receives this callback, it will <em>not</em> receive a
546      * call to {@link #finishBackup()}.  It needs to tear down any ongoing backup state
547      * here.
548      */
cancelFullBackup()549     public void cancelFullBackup() {
550         throw new UnsupportedOperationException(
551                 "Transport cancelFullBackup() not implemented");
552     }
553 
554     /**
555      * Ask the transport whether this app is eligible for backup.
556      *
557      * @param targetPackage The identity of the application.
558      * @param isFullBackup If set, transport should check if app is eligible for full data backup,
559      *   otherwise to check if eligible for key-value backup.
560      * @return Whether this app is eligible for backup.
561      */
isAppEligibleForBackup(PackageInfo targetPackage, boolean isFullBackup)562     public boolean isAppEligibleForBackup(PackageInfo targetPackage, boolean isFullBackup) {
563         return true;
564     }
565 
566     /**
567      * Ask the transport about current quota for backup size of the package.
568      *
569      * @param packageName ID of package to provide the quota.
570      * @param isFullBackup If set, transport should return limit for full data backup, otherwise
571      *                     for key-value backup.
572      * @return Current limit on backup size in bytes.
573      */
getBackupQuota(String packageName, boolean isFullBackup)574     public long getBackupQuota(String packageName, boolean isFullBackup) {
575         return Long.MAX_VALUE;
576     }
577 
578     // ------------------------------------------------------------------------------------
579     // Full restore interfaces
580 
581     /**
582      * Ask the transport to provide data for the "current" package being restored.  This
583      * is the package that was just reported by {@link #nextRestorePackage()} as having
584      * {@link RestoreDescription#TYPE_FULL_STREAM} data.
585      *
586      * The transport writes some data to the socket supplied to this call, and returns
587      * the number of bytes written.  The system will then read that many bytes and
588      * stream them to the application's agent for restore, then will call this method again
589      * to receive the next chunk of the archive.  This sequence will be repeated until the
590      * transport returns zero indicating that all of the package's data has been delivered
591      * (or returns a negative value indicating some sort of hard error condition at the
592      * transport level).
593      *
594      * <p>After this method returns zero, the system will then call
595      * {@link #nextRestorePackage()} to begin the restore process for the next
596      * application, and the sequence begins again.
597      *
598      * <p>The transport should always close this socket when returning from this method.
599      * Do not cache this socket across multiple calls or you may leak file descriptors.
600      *
601      * @param socket The file descriptor that the transport will use for delivering the
602      *    streamed archive.  The transport must close this socket in all cases when returning
603      *    from this method.
604      * @return {@link #NO_MORE_DATA} when no more data for the current package is available.
605      *    A positive value indicates the presence of that many bytes to be delivered to the app.
606      *    A value of zero indicates that no data was deliverable at this time, but the restore
607      *    is still running and the caller should retry.  {@link #TRANSPORT_PACKAGE_REJECTED}
608      *    means that the current package's restore operation should be aborted, but that
609      *    the transport itself is still in a good state and so a multiple-package restore
610      *    sequence can still be continued.  Any other negative return value is treated as a
611      *    fatal error condition that aborts all further restore operations on the current dataset.
612      */
getNextFullRestoreDataChunk(ParcelFileDescriptor socket)613     public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) {
614         return 0;
615     }
616 
617     /**
618      * If the OS encounters an error while processing {@link RestoreDescription#TYPE_FULL_STREAM}
619      * data for restore, it will invoke this method to tell the transport that it should
620      * abandon the data download for the current package.  The OS will then either call
621      * {@link #nextRestorePackage()} again to move on to restoring the next package in the
622      * set being iterated over, or will call {@link #finishRestore()} to shut down the restore
623      * operation.
624      *
625      * @return {@link #TRANSPORT_OK} if the transport was successful in shutting down the
626      *    current stream cleanly, or {@link #TRANSPORT_ERROR} to indicate a serious
627      *    transport-level failure.  If the transport reports an error here, the entire restore
628      *    operation will immediately be finished with no further attempts to restore app data.
629      */
abortFullRestore()630     public int abortFullRestore() {
631         return BackupTransport.TRANSPORT_OK;
632     }
633 
634     /**
635      * Returns flags with additional information about the transport, which is accessible to the
636      * {@link android.app.backup.BackupAgent}. This allows the agent to decide what to do based on
637      * properties of the transport.
638      */
getTransportFlags()639     public int getTransportFlags() {
640         return 0;
641     }
642 
643     /**
644      * Bridge between the actual IBackupTransport implementation and the stable API.  If the
645      * binder interface needs to change, we use this layer to translate so that we can
646      * (if appropriate) decouple those framework-side changes from the BackupTransport
647      * implementations.
648      */
649     class TransportImpl extends IBackupTransport.Stub {
650 
651         @Override
name()652         public String name() throws RemoteException {
653             return BackupTransport.this.name();
654         }
655 
656         @Override
configurationIntent()657         public Intent configurationIntent() throws RemoteException {
658             return BackupTransport.this.configurationIntent();
659         }
660 
661         @Override
currentDestinationString()662         public String currentDestinationString() throws RemoteException {
663             return BackupTransport.this.currentDestinationString();
664         }
665 
666         @Override
dataManagementIntent()667         public Intent dataManagementIntent() {
668             return BackupTransport.this.dataManagementIntent();
669         }
670 
671         @Override
dataManagementIntentLabel()672         public CharSequence dataManagementIntentLabel() {
673             return BackupTransport.this.dataManagementIntentLabel();
674         }
675 
676         @Override
transportDirName()677         public String transportDirName() throws RemoteException {
678             return BackupTransport.this.transportDirName();
679         }
680 
681         @Override
requestBackupTime()682         public long requestBackupTime() throws RemoteException {
683             return BackupTransport.this.requestBackupTime();
684         }
685 
686         @Override
initializeDevice()687         public int initializeDevice() throws RemoteException {
688             return BackupTransport.this.initializeDevice();
689         }
690 
691         @Override
performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd, int flags)692         public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor inFd, int flags)
693                 throws RemoteException {
694             return BackupTransport.this.performBackup(packageInfo, inFd, flags);
695         }
696 
697         @Override
clearBackupData(PackageInfo packageInfo)698         public int clearBackupData(PackageInfo packageInfo) throws RemoteException {
699             return BackupTransport.this.clearBackupData(packageInfo);
700         }
701 
702         @Override
finishBackup()703         public int finishBackup() throws RemoteException {
704             return BackupTransport.this.finishBackup();
705         }
706 
707         @Override
getAvailableRestoreSets()708         public RestoreSet[] getAvailableRestoreSets() throws RemoteException {
709             return BackupTransport.this.getAvailableRestoreSets();
710         }
711 
712         @Override
getCurrentRestoreSet()713         public long getCurrentRestoreSet() throws RemoteException {
714             return BackupTransport.this.getCurrentRestoreSet();
715         }
716 
717         @Override
startRestore(long token, PackageInfo[] packages)718         public int startRestore(long token, PackageInfo[] packages) throws RemoteException {
719             return BackupTransport.this.startRestore(token, packages);
720         }
721 
722         @Override
nextRestorePackage()723         public RestoreDescription nextRestorePackage() throws RemoteException {
724             return BackupTransport.this.nextRestorePackage();
725         }
726 
727         @Override
getRestoreData(ParcelFileDescriptor outFd)728         public int getRestoreData(ParcelFileDescriptor outFd) throws RemoteException {
729             return BackupTransport.this.getRestoreData(outFd);
730         }
731 
732         @Override
finishRestore()733         public void finishRestore() throws RemoteException {
734             BackupTransport.this.finishRestore();
735         }
736 
737         @Override
requestFullBackupTime()738         public long requestFullBackupTime() throws RemoteException {
739             return BackupTransport.this.requestFullBackupTime();
740         }
741 
742         @Override
performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket, int flags)743         public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket,
744                 int flags) throws RemoteException {
745             return BackupTransport.this.performFullBackup(targetPackage, socket, flags);
746         }
747 
748         @Override
checkFullBackupSize(long size)749         public int checkFullBackupSize(long size) {
750             return BackupTransport.this.checkFullBackupSize(size);
751         }
752 
753         @Override
sendBackupData(int numBytes)754         public int sendBackupData(int numBytes) throws RemoteException {
755             return BackupTransport.this.sendBackupData(numBytes);
756         }
757 
758         @Override
cancelFullBackup()759         public void cancelFullBackup() throws RemoteException {
760             BackupTransport.this.cancelFullBackup();
761         }
762 
763         @Override
isAppEligibleForBackup(PackageInfo targetPackage, boolean isFullBackup)764         public boolean isAppEligibleForBackup(PackageInfo targetPackage, boolean isFullBackup)
765                 throws RemoteException {
766             return BackupTransport.this.isAppEligibleForBackup(targetPackage, isFullBackup);
767         }
768 
769         @Override
getBackupQuota(String packageName, boolean isFullBackup)770         public long getBackupQuota(String packageName, boolean isFullBackup) {
771             return BackupTransport.this.getBackupQuota(packageName, isFullBackup);
772         }
773 
774         @Override
getTransportFlags()775         public int getTransportFlags() {
776             return BackupTransport.this.getTransportFlags();
777         }
778 
779         @Override
getNextFullRestoreDataChunk(ParcelFileDescriptor socket)780         public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) {
781             return BackupTransport.this.getNextFullRestoreDataChunk(socket);
782         }
783 
784         @Override
abortFullRestore()785         public int abortFullRestore() {
786             return BackupTransport.this.abortFullRestore();
787         }
788     }
789 }
790