Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 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 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 17 | package android.app.backup; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 18 | |
Brad Fitzpatrick | 6f9d58a | 2010-09-06 22:17:12 -0700 | [diff] [blame] | 19 | import android.app.QueuedWork; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 21 | import android.content.SharedPreferences; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 22 | import android.os.ParcelFileDescriptor; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 24 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 25 | import java.io.File; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 26 | |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 27 | /** |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 28 | * A helper class that can be used in conjunction with |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 29 | * {@link android.app.backup.BackupAgentHelper} to manage the backup of |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 30 | * {@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 Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 32 | * backup operation. |
Kenny Root | 5a20ea1 | 2010-02-23 18:49:11 -0800 | [diff] [blame] | 33 | * <p> |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 34 | * To use this class, the application's backup agent class should extend |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 35 | * {@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 Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 38 | * framework. For example, an agent supporting backup and restore for |
| 39 | * an application with two groups of {@link android.content.SharedPreferences} |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 40 | * 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 Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 63 | * No further implementation is needed; the {@link BackupAgentHelper} mechanism automatically |
Christopher Tate | 4e14a82 | 2010-04-08 12:54:23 -0700 | [diff] [blame] | 64 | * 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 Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 68 | * callbacks to the SharedPreferencesBackupHelper as appropriate. |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 69 | */ |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 70 | public class SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper { |
| 71 | private static final String TAG = "SharedPreferencesBackupHelper"; |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 72 | private static final boolean DEBUG = false; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 73 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 74 | private Context mContext; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 75 | private String[] mPrefGroups; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 76 | |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Construct a helper for backing up and restoring the |
| 79 | * {@link android.content.SharedPreferences} under the given names. |
| 80 | * |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 81 | * @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 Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 84 | */ |
Joe Onorato | dc355a9 | 2009-06-26 14:45:25 -0400 | [diff] [blame] | 85 | public SharedPreferencesBackupHelper(Context context, String... prefGroups) { |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 86 | super(context); |
| 87 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 88 | mContext = context; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 89 | mPrefGroups = prefGroups; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 92 | /** |
Scott Main | d17da43 | 2010-04-29 21:42:58 -0700 | [diff] [blame] | 93 | * Backs up the configured {@link android.content.SharedPreferences} groups. |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 94 | */ |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 95 | public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 96 | ParcelFileDescriptor newState) { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 97 | Context context = mContext; |
Brad Fitzpatrick | 6f9d58a | 2010-09-06 22:17:12 -0700 | [diff] [blame] | 98 | |
| 99 | // If a SharedPreference has an outstanding write in flight, |
| 100 | // wait for it to finish flushing to disk. |
| 101 | QueuedWork.waitToFinish(); |
| 102 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 103 | // make filenames for the prefGroups |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 104 | String[] prefGroups = mPrefGroups; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 105 | final int N = prefGroups.length; |
| 106 | String[] files = new String[N]; |
| 107 | for (int i=0; i<N; i++) { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 108 | files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath(); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 111 | // go |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 112 | performBackup_checked(oldState, data, newState, files, prefGroups); |
| 113 | } |
| 114 | |
Christopher Tate | e28290e | 2010-02-16 15:22:26 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Restores one entity from the restore data stream to its proper shared |
| 117 | * preferences file store. |
| 118 | */ |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 119 | public void restoreEntity(BackupDataInputStream data) { |
| 120 | Context context = mContext; |
| 121 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 122 | String key = data.getKey(); |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 123 | if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size()); |
| 124 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 125 | if (isKeyInList(key, mPrefGroups)) { |
| 126 | File f = context.getSharedPrefsFile(key).getAbsoluteFile(); |
| 127 | writeFile(f, data); |
| 128 | } |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 129 | } |
| 130 | } |