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
21import java.io.IOException;
22
23/**
24 * A convenient {@link BackupAgent} wrapper class that automatically manages
25 * heterogeneous data sets within the backup data, each identified by a unique
26 * key prefix.  When processing a backup or restore operation, the BackupAgentHelper
27 * dispatches to one or more installed {@link BackupHelper} objects, each
28 * of which is responsible for a defined subset of the data being processed.
29 * <p>
30 * An application will typically extend this class in its own
31 * backup agent. Then, within the agent's {@link BackupAgent#onCreate() onCreate()}
32 * method, it will call {@link #addHelper(String, BackupHelper) addHelper()} one or more times to
33 * install the handlers for each kind of data it wishes to manage within its backups.
34 * <p>
35 * The Android framework currently provides two predefined {@link BackupHelper} classes:</p>
36 * <ul><li>{@link FileBackupHelper} - Manages the backup and restore of entire files
37 * within an application's data directory hierarchy.</li>
38 * <li>{@link SharedPreferencesBackupHelper} - Manages the backup and restore of an
39 * application's {@link android.content.SharedPreferences} data.</li></ul>
40 * <p>
41 * An application can also implement its own helper classes to work within the
42 * {@link BackupAgentHelper} framework.  See the {@link BackupHelper} interface
43 * documentation for details.
44 *
45 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For more information about using BackupAgentHelper, read the
48 * <a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a> developer guide.</p>
49 * </div>
50 *
51 * @see BackupHelper
52 * @see FileBackupHelper
53 * @see SharedPreferencesBackupHelper
54 */
55public class BackupAgentHelper extends BackupAgent {
56    static final String TAG = "BackupAgentHelper";
57
58    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();
59
60    /**
61     * Run the backup process on each of the configured handlers.
62     */
63    @Override
64    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
65             ParcelFileDescriptor newState) throws IOException {
66        mDispatcher.performBackup(oldState, data, newState);
67    }
68
69    /**
70     * Run the restore process on each of the configured handlers.
71     */
72    @Override
73    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
74            throws IOException {
75        mDispatcher.performRestore(data, appVersionCode, newState);
76    }
77
78    /** @hide */
79    public BackupHelperDispatcher getDispatcher() {
80        return mDispatcher;
81    }
82
83    /**
84     * Add a helper for a given data subset to the agent's configuration.  Each helper
85     * must have a prefix string that is unique within this backup agent's set of
86     * helpers.
87     *
88     * @param keyPrefix A string used to disambiguate the various helpers within this agent
89     * @param helper A backup/restore helper object to be invoked during backup and restore
90     *    operations.
91     */
92    public void addHelper(String keyPrefix, BackupHelper helper) {
93        mDispatcher.addHelper(keyPrefix, helper);
94    }
95}
96
97
98