1 /* 2 * Copyright (C) 2019 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.tv.common.dev; 17 18 import android.content.Context; 19 import android.content.SharedPreferences; 20 import android.support.annotation.Nullable; 21 import android.support.annotation.VisibleForTesting; 22 23 /** Preferences available to developers */ 24 public abstract class DeveloperPreference<T> { 25 26 private static final String PREFERENCE_FILE_NAME = 27 "com.android.tv.common.dev.DeveloperPreference"; 28 29 /** 30 * Create a boolean developer preference. 31 * 32 * @param key the developer setting key. 33 * @param defaultValue the value to return if the setting is undefined or empty. 34 */ create(String key, boolean defaultValue)35 public static DeveloperPreference<Boolean> create(String key, boolean defaultValue) { 36 return new DeveloperBooleanPreference(key, defaultValue); 37 } 38 39 @VisibleForTesting getPreferences(Context context)40 static final SharedPreferences getPreferences(Context context) { 41 return context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE); 42 } 43 44 /** 45 * Create a int developer preference. 46 * 47 * @param key the developer setting key. 48 * @param defaultValue the value to return if the setting is undefined or empty. 49 */ create(String key, int defaultValue)50 public static DeveloperPreference<Integer> create(String key, int defaultValue) { 51 return new DeveloperIntegerPreference(key, defaultValue); 52 } 53 54 final String mKey; 55 final T mDefaultValue; 56 private T mValue; 57 DeveloperPreference(String key, T defaultValue)58 private DeveloperPreference(String key, T defaultValue) { 59 mKey = key; 60 mValue = null; 61 mDefaultValue = defaultValue; 62 } 63 64 /** Set the value. */ set(Context context, T value)65 public final void set(Context context, T value) { 66 mValue = value; 67 storeValue(context, value); 68 } 69 storeValue(Context context, T value)70 protected abstract void storeValue(Context context, T value); 71 72 /** Get the current value, or the default if the value is not set. */ get(Context context)73 public final T get(Context context) { 74 mValue = getStoredValue(context); 75 return mValue; 76 } 77 78 /** Get the current value, or the default if the value is not set or context is null. */ getDefaultIfContextNull(@ullable Context context)79 public final T getDefaultIfContextNull(@Nullable Context context) { 80 return context == null ? mDefaultValue : getStoredValue(context); 81 } 82 getStoredValue(Context context)83 protected abstract T getStoredValue(Context context); 84 85 /** 86 * Clears the current value. 87 * 88 * <p>Future calls to {@link #get(Context)} will return the default value. 89 */ clear(Context context)90 public final void clear(Context context) { 91 getPreferences(context).edit().remove(mKey).apply(); 92 } 93 94 @Override toString()95 public final String toString() { 96 return "[" + mKey + "]=" + mValue + " Default value : " + mDefaultValue; 97 } 98 99 private static final class DeveloperBooleanPreference extends DeveloperPreference<Boolean> { 100 DeveloperBooleanPreference(String key, Boolean defaultValue)101 private DeveloperBooleanPreference(String key, Boolean defaultValue) { 102 super(key, defaultValue); 103 } 104 105 @Override storeValue(Context context, Boolean value)106 public void storeValue(Context context, Boolean value) { 107 getPreferences(context).edit().putBoolean(mKey, value).apply(); 108 } 109 110 @Override getStoredValue(Context context)111 public Boolean getStoredValue(Context context) { 112 return getPreferences(context).getBoolean(mKey, mDefaultValue); 113 } 114 } 115 116 private static final class DeveloperIntegerPreference extends DeveloperPreference<Integer> { 117 DeveloperIntegerPreference(String key, Integer defaultValue)118 private DeveloperIntegerPreference(String key, Integer defaultValue) { 119 super(key, defaultValue); 120 } 121 122 @Override storeValue(Context context, Integer value)123 protected void storeValue(Context context, Integer value) { 124 getPreferences(context).edit().putInt(mKey, value).apply(); 125 } 126 127 @Override getStoredValue(Context context)128 protected Integer getStoredValue(Context context) { 129 return getPreferences(context).getInt(mKey, mDefaultValue); 130 } 131 } 132 } 133