FileBackupHelper.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.content.Context; 20import android.os.ParcelFileDescriptor; 21import android.util.Log; 22 23import java.io.File; 24 25/** 26 * A helper class which can be used in conjunction with 27 * {@link android.app.backup.BackupAgentHelper} to manage the backup of a set of 28 * files. Whenever backup is performed, all files changed since the last backup 29 * will be saved in their entirety. During the first time the backup happens, 30 * every file in the list will be backed up. Note that this should only be 31 * used with small configuration files, not with large binary files. 32 * <p> 33 * During restore, if the helper encounters data for a file that was not 34 * specified when the FileBackupHelper object was constructed, that data 35 * will be ignored. 36 */ 37public class FileBackupHelper extends FileBackupHelperBase implements BackupHelper { 38 private static final String TAG = "FileBackupHelper"; 39 private static final boolean DEBUG = false; 40 41 Context mContext; 42 File mFilesDir; 43 String[] mFiles; 44 45 /** 46 * Construct a helper to manage backup/restore of entire files within the 47 * application's data directory hierarchy. 48 * 49 * @param context The backup agent's Context object 50 * @param files A list of the files to be backed up or restored. 51 */ 52 public FileBackupHelper(Context context, String... files) { 53 super(context); 54 55 mContext = context; 56 mFilesDir = context.getFilesDir(); 57 mFiles = files; 58 } 59 60 /** 61 * Based on <code>oldState</code>, determine which of the files from the 62 * application's data directory need to be backed up, write them to the data 63 * stream, and fill in <code>newState</code> with the state as it exists 64 * now. When <code>oldState</code> is <code>null</code>, all the files will 65 * be backed up. 66 * <p> 67 * This should only be called directly from within the {@link BackupAgentHelper} 68 * implementation. See 69 * {@link android.app.backup.BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)} 70 * for a description of parameter meanings. 71 */ 72 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 73 ParcelFileDescriptor newState) { 74 // file names 75 String[] files = mFiles; 76 File base = mContext.getFilesDir(); 77 final int N = files.length; 78 String[] fullPaths = new String[N]; 79 for (int i=0; i<N; i++) { 80 fullPaths[i] = (new File(base, files[i])).getAbsolutePath(); 81 } 82 83 // go 84 performBackup_checked(oldState, data, newState, fullPaths, files); 85 } 86 87 /** 88 * Restore one record [representing a single file] from the restore dataset. 89 * <p> 90 * This should only be called directly from within the {@link BackupAgentHelper} 91 * implementation. 92 */ 93 public void restoreEntity(BackupDataInputStream data) { 94 if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); 95 String key = data.getKey(); 96 if (isKeyInList(key, mFiles)) { 97 File f = new File(mFilesDir, key); 98 writeFile(f, data); 99 } 100 } 101} 102 103