BackupAgentHelper.java revision cc84c69726507a85116f5664e20e2ebfac76edbe
1/*
2 * Copyright (C) 2007 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 BackupAgent wrapper class that automatically manages
25 * heterogeneous data sets within the backup data, each identified by a unique
26 * key prefix. An application will typically extend this class in their own
27 * backup agent. Then, within the agent's onBackup() and onRestore() methods, it
28 * will call {@link #addHelper(String, BackupHelper)} one or more times to
29 * specify the data sets, then invoke super.onBackup() or super.onRestore() to
30 * have the BackupAgentHelper implementation process the data.
31 * <p>
32 * STOPSHIP: document!
33 */
34public class BackupAgentHelper extends BackupAgent {
35    static final String TAG = "BackupAgentHelper";
36
37    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();
38
39    /**
40     * Run the backup process on each of the configured handlers.
41     */
42    @Override
43    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
44             ParcelFileDescriptor newState) throws IOException {
45        mDispatcher.performBackup(oldState, data, newState);
46    }
47
48    /**
49     * Run the restore process on each of the configured handlers.
50     */
51    @Override
52    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
53            throws IOException {
54        mDispatcher.performRestore(data, appVersionCode, newState);
55    }
56
57    /** @hide */
58    public BackupHelperDispatcher getDispatcher() {
59        return mDispatcher;
60    }
61
62    /**
63     * Add a helper for a given data subset to the agent's configuration.  Each helper
64     * must have a prefix string that is unique within this backup agent's set of
65     * helpers.
66     *
67     * @param keyPrefix A string used to disambiguate the various helpers within this agent
68     * @param helper A backup/restore helper object to be invoked during backup and restore
69     *    operations.
70     */
71    public void addHelper(String keyPrefix, BackupHelper helper) {
72        mDispatcher.addHelper(keyPrefix, helper);
73    }
74}
75
76
77