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 SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper { 28 private static final String TAG = "SharedPreferencesBackupHelper"; 29 private static final boolean DEBUG = false; 30 31 private Context mContext; 32 private String[] mPrefGroups; 33 SharedPreferencesBackupHelper(Context context, String... prefGroups)34 public SharedPreferencesBackupHelper(Context context, String... prefGroups) { 35 super(context); 36 37 mContext = context; 38 mPrefGroups = prefGroups; 39 } 40 performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)41 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 42 ParcelFileDescriptor newState) { 43 Context context = mContext; 44 45 // make filenames for the prefGroups 46 String[] prefGroups = mPrefGroups; 47 final int N = prefGroups.length; 48 String[] files = new String[N]; 49 for (int i=0; i<N; i++) { 50 files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath(); 51 } 52 53 // go 54 performBackup_checked(oldState, data, newState, files, prefGroups); 55 } 56 restoreEntity(BackupDataInputStream data)57 public void restoreEntity(BackupDataInputStream data) { 58 Context context = mContext; 59 60 String key = data.getKey(); 61 if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size()); 62 63 if (isKeyInList(key, mPrefGroups)) { 64 File f = context.getSharedPrefsFile(key).getAbsoluteFile(); 65 writeFile(f, data); 66 } 67 } 68 } 69 70