1 /* 2 * Copyright 2023 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 androidx.input.motionprediction.common; 18 19 import static androidx.annotation.RestrictTo.Scope.LIBRARY; 20 21 import android.annotation.SuppressLint; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.RestrictTo; 25 26 import java.lang.reflect.Method; 27 28 /** 29 */ 30 @RestrictTo(LIBRARY) 31 public class SystemProperty { 32 private static final boolean BOOLEAN_PROPERTY_DEFAULT = false; 33 private static final int INT_PROPERTY_DEFAULT = 0; 34 SystemProperty()35 private SystemProperty() { 36 // This class is non-instantiable. 37 } 38 39 /** 40 * Reads a system property and returns its boolean value. 41 * 42 * @param name the name of the system property 43 * @return true if the property is set and true, false otherwise 44 */ getBoolean(@onNull String name)45 public static boolean getBoolean(@NonNull String name) { 46 try { 47 @SuppressLint("PrivateApi") 48 Class<?> systemProperties = Class.forName("android.os.SystemProperties"); 49 Method getMethod = systemProperties.getMethod( 50 "getBoolean", 51 String.class, 52 boolean.class); 53 @SuppressLint("BanUncheckedReflection") 54 Boolean result = (Boolean) getMethod.invoke( 55 systemProperties, 56 name, 57 BOOLEAN_PROPERTY_DEFAULT); 58 if (result != null) { 59 return result.booleanValue(); 60 } 61 } catch (Exception e) { 62 } 63 return BOOLEAN_PROPERTY_DEFAULT; 64 } 65 66 /** 67 * Reads a system property and returns its integer value. 68 * 69 * @param name the name of the system property 70 * @return the integer value of the property if defined, zero otherwise 71 */ getInt(@onNull String name)72 public static int getInt(@NonNull String name) { 73 try { 74 @SuppressLint("PrivateApi") 75 Class<?> systemProperties = Class.forName("android.os.SystemProperties"); 76 Method getMethod = systemProperties.getMethod( 77 "getInt", 78 String.class, 79 int.class); 80 @SuppressLint("BanUncheckedReflection") 81 Integer result = (Integer) getMethod.invoke( 82 systemProperties, 83 name, 84 INT_PROPERTY_DEFAULT); 85 if (result != null) { 86 return result.intValue(); 87 } 88 } catch (Exception e) { 89 } 90 return INT_PROPERTY_DEFAULT; 91 } 92 } 93