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