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