1/*
2 * Copyright (C) 2015 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 com.android.tv.common.feature;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.util.Log;
22import com.android.tv.common.SharedPreferencesUtils;
23
24/**
25 * Feature controlled by shared preferences.
26 */
27public final class SharedPreferencesFeature implements Feature {
28    private static final String TAG = "SharedPrefFeature";
29    private static final boolean DEBUG = false;
30
31    private final String mKey;
32    private boolean mEnabled;
33    private final boolean mDefaultValue;
34    private SharedPreferences mSharedPreferences;
35    private final Feature mBaseFeature;
36
37    /**
38     * Create SharedPreferences controlled feature.
39     *
40     * @param key the SharedPreferences key.
41     * @param defaultValue the value to return if the property is undefined or empty.
42     * @param baseFeature if {@code baseFeature} is turned off, this feature is always disabled.
43     */
44    public SharedPreferencesFeature(String key, boolean defaultValue, Feature baseFeature) {
45        mKey = key;
46        mDefaultValue = defaultValue;
47        mBaseFeature = baseFeature;
48    }
49
50    @Override
51    public boolean isEnabled(Context context) {
52        if (!mBaseFeature.isEnabled(context)) {
53            return false;
54        }
55        if (mSharedPreferences == null) {
56            mSharedPreferences = context.getSharedPreferences(
57                    SharedPreferencesUtils.SHARED_PREF_FEATURES, Context.MODE_PRIVATE);
58            mEnabled = mSharedPreferences.getBoolean(mKey, mDefaultValue);
59        }
60        if (DEBUG) Log.d(TAG, mKey + " is " + mEnabled);
61        return mEnabled;
62    }
63
64    @Override
65    public String toString() {
66        return "SharedPreferencesFeature:key=" + mKey + ",value=" + mEnabled;
67    }
68
69    public void setEnabled(Context context, boolean enable) {
70        if (DEBUG) Log.d(TAG, mKey + " is set to " + enable);
71        if (mSharedPreferences == null) {
72            mSharedPreferences = context.getSharedPreferences(
73                    SharedPreferencesUtils.SHARED_PREF_FEATURES, Context.MODE_PRIVATE);
74            mEnabled = enable;
75            mSharedPreferences.edit().putBoolean(mKey, enable).apply();
76        } else if (mEnabled != enable) {
77            mEnabled = enable;
78            mSharedPreferences.edit().putBoolean(mKey, enable).apply();
79        }
80
81    }
82}
83