1 /* 2 * Copyright (C) 2017 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 package com.android.documentsui.prefs; 17 18 import android.content.Context; 19 import android.content.SharedPreferences; 20 import android.content.SharedPreferences.Editor; 21 import android.preference.PreferenceManager; 22 import android.support.annotation.VisibleForTesting; 23 24 import java.util.Map; 25 26 /** 27 * Class providing core logic for backup and restore of DocumentsUI preferences. 28 */ 29 final class PrefsBackupHelper { 30 31 private SharedPreferences mDefaultPreferences; 32 33 @VisibleForTesting PrefsBackupHelper(SharedPreferences overridePreferences)34 PrefsBackupHelper(SharedPreferences overridePreferences) { 35 mDefaultPreferences = overridePreferences; 36 } 37 PrefsBackupHelper(Context context)38 PrefsBackupHelper(Context context) { 39 mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(context); 40 } 41 42 /** 43 * Loads all applicable preferences to supplied backup file. 44 */ getBackupPreferences(SharedPreferences prefs)45 void getBackupPreferences(SharedPreferences prefs) { 46 Editor editor = prefs.edit(); 47 editor.clear(); 48 49 copyMatchingPreferences(mDefaultPreferences, editor); 50 editor.apply(); 51 } 52 53 /** 54 * Restores all applicable preferences from the supplied preferences file. 55 */ putBackupPreferences(SharedPreferences prefs)56 void putBackupPreferences(SharedPreferences prefs) { 57 Editor editor = mDefaultPreferences.edit(); 58 59 copyMatchingPreferences(prefs, editor); 60 editor.apply(); 61 } 62 copyMatchingPreferences(SharedPreferences source, Editor destination)63 private void copyMatchingPreferences(SharedPreferences source, Editor destination) { 64 for (Map.Entry<String, ?> preference : source.getAll().entrySet()) { 65 if (Preferences.shouldBackup(preference.getKey())) { 66 setPreference(destination, preference); 67 } 68 } 69 } 70 71 @SuppressWarnings("unchecked") 72 @VisibleForTesting setPreference(Editor target, final Map.Entry<String, ?> preference)73 void setPreference(Editor target, final Map.Entry<String, ?> preference) { 74 final String key = preference.getKey(); 75 final Object value = preference.getValue(); 76 // Only handle already know types. 77 if (value instanceof Integer) { 78 target.putInt(key, (Integer) value); 79 } else if (value instanceof Boolean) { 80 target.putBoolean(key, (Boolean) value); 81 } else { 82 throw new IllegalArgumentException("DocumentsUI backup: invalid preference " 83 + (value == null ? null : value.getClass())); 84 } 85 } 86 } 87