FileBackupHelper.java revision 4528186e0d65fc68ef0dd1941aa2ac8aefcd55a3
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.BackupHelperAgent} 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 * all the files in the list will be backed up. Note that this should only be 31 * used with small configuration files and not with large binary files. 32 * <p> 33 * Any files not present in the list of files during the restore procedure will 34 * be ignored. If files present in a previous version of an application are 35 * removed in subsequent versions, it is the responsibility of the developer to 36 * design a mechanism to remove those files. Otherwise files no longer needed 37 * will linger and consume space on the device. 38 * <p> 39 * STOPSHIP: document! [manages backup of a set of files; restore is totally 40 * opaque] 41 */ 42public class FileBackupHelper extends FileBackupHelperBase implements BackupHelper { 43 private static final String TAG = "FileBackupHelper"; 44 private static final boolean DEBUG = false; 45 46 Context mContext; 47 File mFilesDir; 48 String[] mFiles; 49 50 /** 51 * Construct a helper to manage backup/restore of entire files within the 52 * application's data directory hierarchy. 53 * 54 * @param context The backup agent's Context object 55 * @param files A list of the files to be backed up or restored. 56 */ 57 public FileBackupHelper(Context context, String... files) { 58 super(context); 59 60 mContext = context; 61 mFilesDir = context.getFilesDir(); 62 mFiles = files; 63 } 64 65 /** 66 * Based on <code>oldState</code>, determine which of the files from the 67 * application's data directory need to be backed up, write them to the data 68 * stream, and fill in <code>newState</code> with the state as it exists 69 * now. When <code>oldState</code> is <code>null</code>, all the files will 70 * be backed up. 71 * <p> 72 * This should be called from {@link android.app.backup.BackupHelperAgent} 73 * directly. See 74 * {@link android.app.backup.BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)} 75 * for a description of parameter meanings. 76 */ 77 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 78 ParcelFileDescriptor newState) { 79 // file names 80 String[] files = mFiles; 81 File base = mContext.getFilesDir(); 82 final int N = files.length; 83 String[] fullPaths = new String[N]; 84 for (int i=0; i<N; i++) { 85 fullPaths[i] = (new File(base, files[i])).getAbsolutePath(); 86 } 87 88 // go 89 performBackup_checked(oldState, data, newState, fullPaths, files); 90 } 91 92 /** 93 * Restore one record [representing a single file] from the restore dataset. 94 */ 95 public void restoreEntity(BackupDataInputStream data) { 96 if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); 97 String key = data.getKey(); 98 if (isKeyInList(key, mFiles)) { 99 File f = new File(mFilesDir, key); 100 writeFile(f, data); 101 } 102 } 103} 104 105