1b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato/* 2b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * Copyright (C) 2009 The Android Open Source Project 3b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * 4b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 5b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * you may not use this file except in compliance with the License. 6b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * You may obtain a copy of the License at 7b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * 8b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * http://www.apache.org/licenses/LICENSE-2.0 9b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * 10b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * Unless required by applicable law or agreed to in writing, software 11b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 12b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * See the License for the specific language governing permissions and 14b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato * limitations under the License. 15b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato */ 16b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 17b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onoratopackage android.backup; 18b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 19b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onoratoimport android.content.Context; 20b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onoratoimport android.os.ParcelFileDescriptor; 2106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratoimport android.util.Log; 22b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 2306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratoimport java.io.File; 24b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onoratoimport java.io.FileDescriptor; 25b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 26290bb011c5c1a9ba1f2116810b06cf52a9c36b3eJoe Onorato/** @hide */ 2706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratopublic class SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper { 2806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato private static final String TAG = "SharedPreferencesBackupHelper"; 29436344ae12c819f58306ceb94241a266141e1218Christopher Tate private static final boolean DEBUG = false; 3006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato 311cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato private Context mContext; 3206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato private String[] mPrefGroups; 331cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato 34dc355a90a3d9d34f66316928a53f61ac35ab4781Joe Onorato public SharedPreferencesBackupHelper(Context context, String... prefGroups) { 3506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato super(context); 361cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato 371cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato mContext = context; 3806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato mPrefGroups = prefGroups; 391cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato } 4006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato 4106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 4206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato ParcelFileDescriptor newState) { 431cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato Context context = mContext; 441cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato 45b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato // make filenames for the prefGroups 4606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato String[] prefGroups = mPrefGroups; 47b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato final int N = prefGroups.length; 48b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato String[] files = new String[N]; 49b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato for (int i=0; i<N; i++) { 501cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath(); 51b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato } 52b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 531cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato // go 5406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato performBackup_checked(oldState, data, newState, files, prefGroups); 5506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato } 5606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato 5706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato public void restoreEntity(BackupDataInputStream data) { 5806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato Context context = mContext; 5906290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato 6006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato String key = data.getKey(); 61436344ae12c819f58306ceb94241a266141e1218Christopher Tate if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size()); 62436344ae12c819f58306ceb94241a266141e1218Christopher Tate 6306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato if (isKeyInList(key, mPrefGroups)) { 6406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato File f = context.getSharedPrefsFile(key).getAbsoluteFile(); 6506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato writeFile(f, data); 6606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato } 67b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato } 68b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato} 69b1a7ffef3a0007b6991b8338460f6aac8cbb11e8Joe Onorato 70