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