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 * @see BackupHelper
46 * @see FileBackupHelper
47 * @see SharedPreferencesBackupHelper
48 */
49public class BackupAgentHelper extends BackupAgent {
50    static final String TAG = "BackupAgentHelper";
51
52    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();
53
54    /**
55     * Run the backup process on each of the configured handlers.
56     */
57    @Override
58    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
59             ParcelFileDescriptor newState) throws IOException {
60        mDispatcher.performBackup(oldState, data, newState);
61    }
62
63    /**
64     * Run the restore process on each of the configured handlers.
65     */
66    @Override
67    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
68            throws IOException {
69        mDispatcher.performRestore(data, appVersionCode, newState);
70    }
71
72    /** @hide */
73    public BackupHelperDispatcher getDispatcher() {
74        return mDispatcher;
75    }
76
77    /**
78     * Add a helper for a given data subset to the agent's configuration.  Each helper
79     * must have a prefix string that is unique within this backup agent's set of
80     * helpers.
81     *
82     * @param keyPrefix A string used to disambiguate the various helpers within this agent
83     * @param helper A backup/restore helper object to be invoked during backup and restore
84     *    operations.
85     */
86    public void addHelper(String keyPrefix, BackupHelper helper) {
87        mDispatcher.addHelper(keyPrefix, helper);
88    }
89}
90
91
92