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 which of the files from the
41     * application's data directory need to be backed up, write them to
42     * <code>data</code>, and fill in <code>newState</code> with the state as it
43     * exists now.
44     * <p>
45     * Implementing this method is much like implementing
46     * {@link BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
47     * onBackup()} &mdash; the method parameters are the same.  When this method is invoked the
48     * {@code oldState} descriptor points to the beginning of the state data
49     * written during this helper's previous backup operation, and the {@code newState}
50     * descriptor points to the file location at which the helper should write its
51     * new state after performing the backup operation.
52     * <p class="note">
53     * <strong>Note:</strong> The helper should not close or seek either the {@code oldState} or
54     * the {@code newState} file descriptors.</p>
55     *
56     * @param oldState An open, read-only {@link android.os.ParcelFileDescriptor} pointing to the
57     *            last backup state provided by the application. May be
58     *            <code>null</code>, in which case no prior state is being
59     *            provided and the application should perform a full backup.
60     * @param data An open, read/write {@link BackupDataOutput}
61     *            pointing to the backup data destination.
62     *            Typically the application will use backup helper classes to
63     *            write to this file.
64     * @param newState An open, read/write {@link android.os.ParcelFileDescriptor} pointing to an
65     *            empty file. The application should record the final backup
66     *            state here after writing the requested data to the <code>data</code>
67     *            output stream.
68     */
69    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
70            ParcelFileDescriptor newState);
71
72    /**
73     * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
74     * to restore a single entity from the restore data set.  This method will be
75     * called for each entity in the data set that belongs to this handler.
76     * <p class="note">
77     * <strong>Note:</strong> Do not close the <code>data</code> stream.  Do not read more than
78     * {@link android.app.backup.BackupDataInputStream#size() size()} bytes from
79     * <code>data</code>.</p>
80     *
81     * @param data An open {@link BackupDataInputStream} from which the backup data can be read.
82     */
83    public void restoreEntity(BackupDataInputStream data);
84
85    /**
86     * Called by {@link android.app.backup.BackupAgentHelper BackupAgentHelper}
87     * after a restore operation to write the backup state file corresponding to
88     * the data as processed by the helper.  The data written here will be
89     * available to the helper during the next call to its
90     * {@link #performBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
91     * performBackup()} method.
92     * <p>
93     * This method will be called even if the handler's
94     * {@link #restoreEntity(BackupDataInputStream) restoreEntity()} method was never invoked during
95     * the restore operation.
96     * <p class="note">
97     * <strong>Note:</strong> The helper should not close or seek the {@code newState}
98     * file descriptor.</p>
99     *
100     * @param newState A {@link android.os.ParcelFileDescriptor} to which the new state will be
101     * written.
102     */
103    public void writeNewStateDescription(ParcelFileDescriptor newState);
104}
105
106