blob: 213bd31698de513c20511b33d8a04d894d68ec49 [file] [log] [blame]
Joe Onoratob1a7ffe2009-05-06 18:06:21 -07001/*
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
Christopher Tate45281862010-03-05 15:46:30 -080017package android.app.backup;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070018
Brad Fitzpatrick6f9d58a2010-09-06 22:17:12 -070019import android.app.QueuedWork;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070020import android.content.Context;
Kenny Root5a20ea12010-02-23 18:49:11 -080021import android.content.SharedPreferences;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070022import android.os.ParcelFileDescriptor;
Joe Onorato06290a42009-06-18 20:10:37 -070023import android.util.Log;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070024
Joe Onorato06290a42009-06-18 20:10:37 -070025import java.io.File;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070026
Christopher Tatee28290e2010-02-16 15:22:26 -080027/**
Scott Maind17da432010-04-29 21:42:58 -070028 * A helper class that can be used in conjunction with
Christopher Tatecc84c692010-03-29 14:54:02 -070029 * {@link android.app.backup.BackupAgentHelper} to manage the backup of
Scott Maind17da432010-04-29 21:42:58 -070030 * {@link android.content.SharedPreferences}. Whenever a backup is performed, it
31 * will back up all named shared preferences that have changed since the last
Christopher Tate4e14a822010-04-08 12:54:23 -070032 * backup operation.
Kenny Root5a20ea12010-02-23 18:49:11 -080033 * <p>
Scott Maind17da432010-04-29 21:42:58 -070034 * To use this class, the application's backup agent class should extend
Christopher Tate4e14a822010-04-08 12:54:23 -070035 * {@link android.app.backup.BackupAgentHelper}. Then, in the agent's
36 * {@link BackupAgent#onCreate()} method, an instance of this class should be
37 * allocated and installed as a backup/restore handler within the BackupAgentHelper
Scott Maind17da432010-04-29 21:42:58 -070038 * framework. For example, an agent supporting backup and restore for
39 * an application with two groups of {@link android.content.SharedPreferences}
Christopher Tate4e14a822010-04-08 12:54:23 -070040 * data might look something like this:
41 * <pre>
42 * import android.app.backup.BackupAgentHelper;
43 * import android.app.backup.SharedPreferencesBackupHelper;
44 *
45 * public class MyBackupAgent extends BackupAgentHelper {
46 * // The names of the SharedPreferences groups that the application maintains. These
47 * // are the same strings that are passed to {@link Context#getSharedPreferences(String, int)}.
48 * static final String PREFS_DISPLAY = "displayprefs";
49 * static final String PREFS_SCORES = "highscores";
50 *
51 * // An arbitrary string used within the BackupAgentHelper implementation to
52 * // identify the SharedPreferenceBackupHelper's data.
53 * static final String MY_PREFS_BACKUP_KEY = "myprefs";
54 *
55 * // Simply allocate a helper and install it
56 * void onCreate() {
57 * SharedPreferencesBackupHelper helper =
58 * new SharedPreferencesBackupHelper(this, PREFS_DISPLAY, PREFS_SCORES);
59 * addHelper(MY_PREFS_BACKUP_KEY, helper);
60 * }
61 * }</pre>
62 * <p>
Scott Maind17da432010-04-29 21:42:58 -070063 * No further implementation is needed; the {@link BackupAgentHelper} mechanism automatically
Christopher Tate4e14a822010-04-08 12:54:23 -070064 * dispatches the
65 * {@link BackupAgent#onBackup(android.os.ParcelFileDescriptor, BackupDataOutput, android.os.ParcelFileDescriptor) BackupAgent.onBackup()}
66 * and
67 * {@link BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) BackupAgent.onRestore()}
Scott Maind17da432010-04-29 21:42:58 -070068 * callbacks to the SharedPreferencesBackupHelper as appropriate.
Christopher Tatee28290e2010-02-16 15:22:26 -080069 */
Joe Onorato06290a42009-06-18 20:10:37 -070070public class SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper {
71 private static final String TAG = "SharedPreferencesBackupHelper";
Christopher Tate436344a2009-09-30 16:17:37 -070072 private static final boolean DEBUG = false;
Joe Onorato06290a42009-06-18 20:10:37 -070073
Joe Onorato1cf58742009-06-12 11:06:24 -070074 private Context mContext;
Joe Onorato06290a42009-06-18 20:10:37 -070075 private String[] mPrefGroups;
Joe Onorato1cf58742009-06-12 11:06:24 -070076
Christopher Tatee28290e2010-02-16 15:22:26 -080077 /**
78 * Construct a helper for backing up and restoring the
79 * {@link android.content.SharedPreferences} under the given names.
80 *
Scott Maind17da432010-04-29 21:42:58 -070081 * @param context The application {@link android.content.Context}
82 * @param prefGroups The names of each {@link android.content.SharedPreferences} file to
83 * back up
Christopher Tatee28290e2010-02-16 15:22:26 -080084 */
Joe Onoratodc355a92009-06-26 14:45:25 -040085 public SharedPreferencesBackupHelper(Context context, String... prefGroups) {
Joe Onorato06290a42009-06-18 20:10:37 -070086 super(context);
87
Joe Onorato1cf58742009-06-12 11:06:24 -070088 mContext = context;
Joe Onorato06290a42009-06-18 20:10:37 -070089 mPrefGroups = prefGroups;
Joe Onorato1cf58742009-06-12 11:06:24 -070090 }
91
Christopher Tatee28290e2010-02-16 15:22:26 -080092 /**
Scott Maind17da432010-04-29 21:42:58 -070093 * Backs up the configured {@link android.content.SharedPreferences} groups.
Christopher Tatee28290e2010-02-16 15:22:26 -080094 */
Joe Onorato06290a42009-06-18 20:10:37 -070095 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
96 ParcelFileDescriptor newState) {
Joe Onorato1cf58742009-06-12 11:06:24 -070097 Context context = mContext;
Brad Fitzpatrick6f9d58a2010-09-06 22:17:12 -070098
99 // If a SharedPreference has an outstanding write in flight,
100 // wait for it to finish flushing to disk.
101 QueuedWork.waitToFinish();
102
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700103 // make filenames for the prefGroups
Joe Onorato06290a42009-06-18 20:10:37 -0700104 String[] prefGroups = mPrefGroups;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700105 final int N = prefGroups.length;
106 String[] files = new String[N];
107 for (int i=0; i<N; i++) {
Joe Onorato1cf58742009-06-12 11:06:24 -0700108 files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath();
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700109 }
110
Joe Onorato1cf58742009-06-12 11:06:24 -0700111 // go
Joe Onorato06290a42009-06-18 20:10:37 -0700112 performBackup_checked(oldState, data, newState, files, prefGroups);
113 }
114
Christopher Tatee28290e2010-02-16 15:22:26 -0800115 /**
116 * Restores one entity from the restore data stream to its proper shared
117 * preferences file store.
118 */
Joe Onorato06290a42009-06-18 20:10:37 -0700119 public void restoreEntity(BackupDataInputStream data) {
120 Context context = mContext;
121
Joe Onorato06290a42009-06-18 20:10:37 -0700122 String key = data.getKey();
Christopher Tate436344a2009-09-30 16:17:37 -0700123 if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size());
124
Joe Onorato06290a42009-06-18 20:10:37 -0700125 if (isKeyInList(key, mPrefGroups)) {
126 File f = context.getSharedPrefsFile(key).getAbsoluteFile();
127 writeFile(f, data);
128 }
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700129 }
130}