1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.backup;
18 
19 import android.os.ParcelFileDescriptor;
20 
21 /**
22  * Defines the calling interface that {@link BackupAgentHelper} uses
23  * when dispatching backup and restore operations to the installed helpers.
24  * Applications can define and install their own helpers as well as using those
25  * provided as part of the Android framework.
26  * <p>
27  * Although multiple helper objects may be installed simultaneously, each helper
28  * is responsible only for handling its own data, and will not see entities
29  * created by other components within the backup system.  Invocations of multiple
30  * helpers are performed sequentially by the {@link BackupAgentHelper}, with each
31  * helper given a chance to access its own saved state from within the state record
32  * produced during the previous backup operation.
33  *
34  * @see BackupAgentHelper
35  * @see FileBackupHelper
36  * @see SharedPreferencesBackupHelper
37  */
38 public interface BackupHelper {
39     /**
40      * Based on <code>oldState</code>, determine what application content
41      * needs to be backed up, write it to <code>data</code>, and fill in
42      * <code>newState</code> with the complete state as it exists now.
43      * <p>
44      * Implementing this method is much like implementing
45      * {@link BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
46      * onBackup()} &mdash; the method parameters are the same.  When this method is invoked the
47      * {@code oldState} descriptor points to the beginning of the state data
48      * written during this helper's previous backup operation, and the {@code newState}
49      * descriptor points to the file location at which the helper should write its
50      * new state after performing the backup operation.
51      * <p class="note">
52      * <strong>Note:</strong> The helper should not close or seek either the {@code oldState} or
53      * the {@code newState} file descriptors.</p>
54      *
55      * @param oldState An open, read-only {@link android.os.ParcelFileDescriptor} pointing to the
56      *            last backup state provided by the application. May be
57      *            <code>null</code>, in which case no prior state is being
58      *            provided and the application should perform a full backup.
59      * @param data An open, read/write {@link BackupDataOutput}
60      *            pointing to the backup data destination.
61      *            Typically the application will use backup helper classes to
62      *            write to this file.
63      * @param newState An open, read/write {@link android.os.ParcelFileDescriptor} pointing to an
64      *            empty file. The application should record the final backup
65      *            state here after writing the requested data to the <code>data</code>
66      *            output stream.
67      */
performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)68     public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
69             ParcelFileDescriptor newState);
70 
71     /**
72      * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
73      * to restore a single entity from the restore data set.  This method will be
74      * called for each entity in the data set that belongs to this handler.
75      * <p class="note">
76      * <strong>Note:</strong> Do not close the <code>data</code> stream.  Do not read more than
77      * {@link android.app.backup.BackupDataInputStream#size() size()} bytes from
78      * <code>data</code>.</p>
79      *
80      * @param data An open {@link BackupDataInputStream} from which the backup data can be read.
81      */
restoreEntity(BackupDataInputStream data)82     public void restoreEntity(BackupDataInputStream data);
83 
84     /**
85      * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
86      * after a restore operation to write the backup state file corresponding to
87      * the data as processed by the helper.  The data written here will be
88      * available to the helper during the next call to its
89      * {@link #performBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
90      * performBackup()} method.
91      * <p>
92      * This method will be called even if the handler's
93      * {@link #restoreEntity(BackupDataInputStream) restoreEntity()} method was never invoked during
94      * the restore operation.
95      * <p class="note">
96      * <strong>Note:</strong> The helper should not close or seek the {@code newState}
97      * file descriptor.</p>
98      *
99      * @param newState A {@link android.os.ParcelFileDescriptor} to which the new state will be
100      * written.
101      */
writeNewStateDescription(ParcelFileDescriptor newState)102     public void writeNewStateDescription(ParcelFileDescriptor newState);
103 }
104 
105