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;
18
19import android.util.Log;
20
21import java.lang.reflect.Method;
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * Lazy loaded boolean system property.
27 *
28 * <p>Set with  <code>adb shell setprop <em>key</em> <em>value</em></code> where:
29 * Values 'n', 'no', '0', 'false' or 'off' are considered false.
30 * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
31 * (case sensitive). See <a href=
32 * "https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/SystemProperties.java"
33 * >android.os.SystemProperties.getBoolean</a>.
34 */
35public class BooleanSystemProperty {
36    private final static String TAG = "BooleanSystemProperty";
37    private final static boolean DEBUG = false;
38    private static final List<BooleanSystemProperty> ALL_PROPERTIES = new ArrayList<>();
39    private final boolean mDefaultValue;
40    private final String mKey;
41    private Boolean mValue = null;
42
43    /**
44     * Create a boolean system property.
45     *
46     * @param key the system property key.
47     * @param defaultValue the value to return if the property is undefined or empty.
48     */
49    public BooleanSystemProperty(String key, boolean defaultValue) {
50        mDefaultValue = defaultValue;
51        mKey = key;
52        ALL_PROPERTIES.add(this);
53    }
54
55    public static void resetAll() {
56        for (BooleanSystemProperty prop : ALL_PROPERTIES) {
57            prop.reset();
58        }
59    }
60
61    /**
62     * Gets system properties set by <code>adb shell setprop <em>key</em> <em>value</em></code>
63     *
64     * @param key the property key.
65     * @param defaultValue the value to return if the property is undefined or empty.
66     * @return the system property value or the default value.
67     */
68    private static boolean getBoolean(String key, boolean defaultValue) {
69        try {
70            final Class<?> systemProperties = Class.forName("android.os.SystemProperties");
71            final Method get = systemProperties.getMethod("getBoolean", String.class, Boolean.TYPE);
72            return (boolean) get.invoke(null, key, defaultValue);
73        } catch (Exception e) {
74            Log.e(TAG, "Error getting boolean for  " + key, e);
75            // This should never happen
76            return defaultValue;
77        }
78    }
79
80    /**
81     * Clears the cached value.  The next call to getValue will check {@code
82     * android.os.SystemProperties}.
83     */
84    public void reset() {
85        mValue = null;
86    }
87
88    /**
89     * Returns the value of the system property.
90     *
91     * <p>If the value is cached  get the value from {@code android.os.SystemProperties} with the
92     * default set in the constructor.
93     */
94    public boolean getValue() {
95        if (mValue == null) {
96            mValue = getBoolean(mKey, mDefaultValue);
97            if (DEBUG) Log.d(TAG, mKey + "=" + mValue);
98        }
99        return mValue;
100    }
101
102    @Override
103    public String toString() {
104        return "SystemProperty[" + mKey + "]=" + getValue();
105    }
106}
107