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 17 package android.backup; 18 19 import android.content.Context; 20 import android.os.ParcelFileDescriptor; 21 import android.util.Log; 22 23 import java.io.File; 24 import java.io.FileDescriptor; 25 26 /** @hide */ 27 public class FileBackupHelper extends FileBackupHelperBase implements BackupHelper { 28 private static final String TAG = "FileBackupHelper"; 29 private static final boolean DEBUG = false; 30 31 Context mContext; 32 File mFilesDir; 33 String[] mFiles; 34 FileBackupHelper(Context context, String... files)35 public FileBackupHelper(Context context, String... files) { 36 super(context); 37 38 mContext = context; 39 mFilesDir = context.getFilesDir(); 40 mFiles = files; 41 } 42 43 /** 44 * Based on oldState, determine which of the files from the application's data directory 45 * need to be backed up, write them to the data stream, and fill in newState with the 46 * state as it exists now. 47 */ performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)48 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 49 ParcelFileDescriptor newState) { 50 // file names 51 String[] files = mFiles; 52 File base = mContext.getFilesDir(); 53 final int N = files.length; 54 String[] fullPaths = new String[N]; 55 for (int i=0; i<N; i++) { 56 fullPaths[i] = (new File(base, files[i])).getAbsolutePath(); 57 } 58 59 // go 60 performBackup_checked(oldState, data, newState, fullPaths, files); 61 } 62 restoreEntity(BackupDataInputStream data)63 public void restoreEntity(BackupDataInputStream data) { 64 if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); 65 String key = data.getKey(); 66 if (isKeyInList(key, mFiles)) { 67 File f = new File(mFilesDir, key); 68 writeFile(f, data); 69 } 70 } 71 } 72 73