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
17package android.app.backup;
18
19import 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 */
38public 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.  It is essential that when reading the helper's
54     * saved state from the {@code oldState} file, no extra content is consumed beyond
55     * what was stored by this helper.  If more old state data is read, even accidentally,
56     * it will make it impossible for additional helpers that may be invoked after this one
57     * to properly reconstruct their prior state.</p>
58     *
59     * @param oldState An open, read-only {@link android.os.ParcelFileDescriptor} pointing to the
60     *            last backup state provided by the application. May be
61     *            <code>null</code>, in which case no prior state is being
62     *            provided and the application should perform a full backup.
63     * @param data An open, read/write {@link BackupDataOutput}
64     *            pointing to the backup data destination.
65     *            Typically the application will use backup helper classes to
66     *            write to this file.
67     * @param newState An open, read/write {@link android.os.ParcelFileDescriptor} pointing to an
68     *            empty file. The application should record the final backup
69     *            state here after writing the requested data to the <code>data</code>
70     *            output stream.
71     */
72    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
73            ParcelFileDescriptor newState);
74
75    /**
76     * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
77     * to restore a single entity from the restore data set.  This method will be
78     * called for each entity in the data set that belongs to this handler.
79     * <p class="note">
80     * <strong>Note:</strong> Do not close the <code>data</code> stream.  Do not read more than
81     * {@link android.app.backup.BackupDataInputStream#size() size()} bytes from
82     * <code>data</code>.</p>
83     *
84     * @param data An open {@link BackupDataInputStream} from which the backup data can be read.
85     */
86    public void restoreEntity(BackupDataInputStream data);
87
88    /**
89     * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
90     * after a restore operation to write the backup state file corresponding to
91     * the data as processed by the helper.  The data written here will be
92     * available to the helper during the next call to its
93     * {@link #performBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
94     * performBackup()} method.
95     * <p>
96     * This method will be called even if the handler's
97     * {@link #restoreEntity(BackupDataInputStream) restoreEntity()} method was never invoked during
98     * the restore operation.
99     * <p class="note">
100     * <strong>Note:</strong> The helper should not close or seek the {@code newState}
101     * file descriptor.</p>
102     *
103     * @param newState A {@link android.os.ParcelFileDescriptor} to which the new state will be
104     * written.
105     */
106    public void writeNewStateDescription(ParcelFileDescriptor newState);
107}
108
109