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