1 /* 2 * Copyright (C) 2016 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.preference.PreferenceManager; 21 import android.text.TextUtils; 22 23 /** 24 * Provides an interface (and runtime implementation) for preferences that are 25 * scoped (presumably to an activity). This eliminates the need to pass 26 * scoping values into {@link LocalPreferences}, as well as eliminates 27 * the static-coupling to {@link LocalPreferences} increasing testability. 28 */ 29 public interface ScopedPreferences { 30 31 static final String INCLUDE_DEVICE_ROOT = "includeDeviceRoot"; 32 static final String ENABLE_ARCHIVE_CREATION = "enableArchiveCreation-"; 33 34 boolean getShowDeviceRoot(); 35 void setShowDeviceRoot(boolean display); 36 37 /** 38 * @param scope An arbitrary string representitive of the scope 39 * for prefs that are set using this object. 40 */ 41 public static ScopedPreferences create(Context context, String scope) { 42 return new RuntimeScopedPreferences( 43 PreferenceManager.getDefaultSharedPreferences(context), scope); 44 } 45 46 static final class RuntimeScopedPreferences implements ScopedPreferences { 47 48 private SharedPreferences mSharedPrefs; 49 private String mScope; 50 51 private RuntimeScopedPreferences(SharedPreferences sharedPrefs, String scope) { 52 assert(!TextUtils.isEmpty(scope)); 53 54 mSharedPrefs = sharedPrefs; 55 mScope = scope; 56 } 57 58 @Override 59 public boolean getShowDeviceRoot() { 60 return mSharedPrefs.getBoolean(INCLUDE_DEVICE_ROOT, false); 61 } 62 63 @Override 64 public void setShowDeviceRoot(boolean display) { 65 mSharedPrefs.edit().putBoolean(INCLUDE_DEVICE_ROOT, display).apply(); 66 } 67 } 68 69 static boolean shouldBackup(String s) { 70 return INCLUDE_DEVICE_ROOT.equals(s); 71 } 72 } 73