BackupAgentHelper.java revision cc84c69726507a85116f5664e20e2ebfac76edbe
106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato/*
206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * Copyright (C) 2007 The Android Open Source Project
306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato *
406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * you may not use this file except in compliance with the License.
606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * You may obtain a copy of the License at
706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato *
806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
906290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato *
1006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * Unless required by applicable law or agreed to in writing, software
1106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
1206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * See the License for the specific language governing permissions and
1406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato * limitations under the License.
1506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato */
1606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
174528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tatepackage android.app.backup;
1806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
1906290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratoimport android.os.ParcelFileDescriptor;
2006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
2106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratoimport java.io.IOException;
2206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
23e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate/**
245a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * A convenient BackupAgent wrapper class that automatically manages
255a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * heterogeneous data sets within the backup data, each identified by a unique
265a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * key prefix. An application will typically extend this class in their own
275a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * backup agent. Then, within the agent's onBackup() and onRestore() methods, it
285a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * will call {@link #addHelper(String, BackupHelper)} one or more times to
295a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * specify the data sets, then invoke super.onBackup() or super.onRestore() to
30cc84c69726507a85116f5664e20e2ebfac76edbeChristopher Tate * have the BackupAgentHelper implementation process the data.
315a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p>
32e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate * STOPSHIP: document!
33e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate */
34cc84c69726507a85116f5664e20e2ebfac76edbeChristopher Tatepublic class BackupAgentHelper extends BackupAgent {
35cc84c69726507a85116f5664e20e2ebfac76edbeChristopher Tate    static final String TAG = "BackupAgentHelper";
3606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
3706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();
3806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
39e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /**
40e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Run the backup process on each of the configured handlers.
41e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     */
4206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    @Override
4306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
444ababd922eac5931e0222862ff082dc29e012816Joe Onorato             ParcelFileDescriptor newState) throws IOException {
4506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato        mDispatcher.performBackup(oldState, data, newState);
4606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    }
4706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
48e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /**
49e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Run the restore process on each of the configured handlers.
50e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     */
5106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    @Override
525cbbf5652a78902ac3382dc4a3583bc5b0351027Christopher Tate    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
5306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato            throws IOException {
545cbbf5652a78902ac3382dc4a3583bc5b0351027Christopher Tate        mDispatcher.performRestore(data, appVersionCode, newState);
5506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    }
5606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
57e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /** @hide */
5806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    public BackupHelperDispatcher getDispatcher() {
5906290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato        return mDispatcher;
6006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    }
6106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
62e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /**
63e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Add a helper for a given data subset to the agent's configuration.  Each helper
64e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * must have a prefix string that is unique within this backup agent's set of
65e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * helpers.
66e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     *
67e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * @param keyPrefix A string used to disambiguate the various helpers within this agent
68e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * @param helper A backup/restore helper object to be invoked during backup and restore
69e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     *    operations.
70e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     */
7106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    public void addHelper(String keyPrefix, BackupHelper helper) {
7206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato        mDispatcher.addHelper(keyPrefix, helper);
7306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    }
7406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato}
7506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
7606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
77