SharedPreferencesBackupHelper.java revision 4528186e0d65fc68ef0dd1941aa2ac8aefcd55a3
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
17package android.app.backup;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.os.ParcelFileDescriptor;
22import android.util.Log;
23
24import java.io.File;
25
26/**
27 * A helper class which can be used in conjunction with
28 * {@link android.app.backup.BackupHelperAgent} to manage the backup of
29 * {@link android.content.SharedPreferences}. Whenever backup is performed it
30 * will back up all named shared preferences which have changed since the last
31 * backup.
32 * <p>
33 * STOPSHIP: document!
34 */
35public class SharedPreferencesBackupHelper extends FileBackupHelperBase implements BackupHelper {
36    private static final String TAG = "SharedPreferencesBackupHelper";
37    private static final boolean DEBUG = false;
38
39    private Context mContext;
40    private String[] mPrefGroups;
41
42    /**
43     * Construct a helper for backing up and restoring the
44     * {@link android.content.SharedPreferences} under the given names.
45     *
46     * @param context
47     * @param prefGroups
48     */
49    public SharedPreferencesBackupHelper(Context context, String... prefGroups) {
50        super(context);
51
52        mContext = context;
53        mPrefGroups = prefGroups;
54    }
55
56    /**
57     * Backs up the configured SharedPreferences groups
58     */
59    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
60            ParcelFileDescriptor newState) {
61        Context context = mContext;
62
63        // make filenames for the prefGroups
64        String[] prefGroups = mPrefGroups;
65        final int N = prefGroups.length;
66        String[] files = new String[N];
67        for (int i=0; i<N; i++) {
68            files[i] = context.getSharedPrefsFile(prefGroups[i]).getAbsolutePath();
69        }
70
71        // go
72        performBackup_checked(oldState, data, newState, files, prefGroups);
73    }
74
75    /**
76     * Restores one entity from the restore data stream to its proper shared
77     * preferences file store.
78     */
79    public void restoreEntity(BackupDataInputStream data) {
80        Context context = mContext;
81
82        String key = data.getKey();
83        if (DEBUG) Log.d(TAG, "got entity '" + key + "' size=" + data.size());
84
85        if (isKeyInList(key, mPrefGroups)) {
86            File f = context.getSharedPrefsFile(key).getAbsoluteFile();
87            writeFile(f, data);
88        }
89    }
90}
91
92