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