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.util;
18 
19 import android.util.Log;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 
23 /**
24  * Proxy class that gives an access to a hidden API {@link android.os.SystemProperties#getBoolean}.
25  */
26 public class SystemPropertiesProxy {
27     private static final String TAG = "SystemPropertiesProxy";
28 
SystemPropertiesProxy()29     private SystemPropertiesProxy() {}
30 
getBoolean(String key, boolean def)31     public static boolean getBoolean(String key, boolean def) throws IllegalArgumentException {
32         try {
33             Class SystemPropertiesClass = Class.forName("android.os.SystemProperties");
34             Method getBooleanMethod =
35                     SystemPropertiesClass.getDeclaredMethod(
36                             "getBoolean", String.class, boolean.class);
37             getBooleanMethod.setAccessible(true);
38             return (boolean) getBooleanMethod.invoke(SystemPropertiesClass, key, def);
39         } catch (InvocationTargetException
40                 | IllegalAccessException
41                 | NoSuchMethodException
42                 | ClassNotFoundException e) {
43             Log.e(TAG, "Failed to invoke SystemProperties.getBoolean()", e);
44         }
45         return def;
46     }
47 
getInt(String key, int def)48     public static int getInt(String key, int def) throws IllegalArgumentException {
49         try {
50             Class SystemPropertiesClass = Class.forName("android.os.SystemProperties");
51             Method getIntMethod =
52                     SystemPropertiesClass.getDeclaredMethod("getInt", String.class, int.class);
53             getIntMethod.setAccessible(true);
54             return (int) getIntMethod.invoke(SystemPropertiesClass, key, def);
55         } catch (InvocationTargetException
56                 | IllegalAccessException
57                 | NoSuchMethodException
58                 | ClassNotFoundException e) {
59             Log.e(TAG, "Failed to invoke SystemProperties.getInt()", e);
60         }
61         return def;
62     }
63 
getString(String key, String def)64     public static String getString(String key, String def) throws IllegalArgumentException {
65         try {
66             Class SystemPropertiesClass = Class.forName("android.os.SystemProperties");
67             Method getIntMethod =
68                     SystemPropertiesClass.getDeclaredMethod("get", String.class, String.class);
69             getIntMethod.setAccessible(true);
70             return (String) getIntMethod.invoke(SystemPropertiesClass, key, def);
71         } catch (InvocationTargetException
72                 | IllegalAccessException
73                 | NoSuchMethodException
74                 | ClassNotFoundException e) {
75             Log.e(TAG, "Failed to invoke SystemProperties.get()", e);
76         }
77         return def;
78     }
79 }
80