1 /*
2  * Copyright (C) 2018 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.dialer.oem;
18 
19 import android.support.annotation.Nullable;
20 import com.android.dialer.common.LogUtil;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 
24 /**
25  * Hacky way to call the hidden SystemProperties class API. Needed to get the real value of
26  * ro.carrier and some other values.
27  */
28 class SystemPropertiesAccessor {
29   private Method systemPropertiesGetMethod;
30 
31   @SuppressWarnings("PrivateApi")
get(String name)32   public String get(String name) {
33     Method systemPropertiesGetMethod = getSystemPropertiesGetMethod();
34     if (systemPropertiesGetMethod == null) {
35       return null;
36     }
37 
38     try {
39       return (String) systemPropertiesGetMethod.invoke(null, name);
40     } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
41       LogUtil.e("SystemPropertiesAccessor.get", "unable to invoke system method", e);
42       return null;
43     }
44   }
45 
46   @SuppressWarnings("PrivateApi")
getSystemPropertiesGetMethod()47   private @Nullable Method getSystemPropertiesGetMethod() {
48     if (systemPropertiesGetMethod != null) {
49       return systemPropertiesGetMethod;
50     }
51 
52     try {
53       Class<?> systemPropertiesClass = Class.forName("android.os.SystemProperties");
54       if (systemPropertiesClass == null) {
55         return null;
56       }
57       systemPropertiesGetMethod = systemPropertiesClass.getMethod("get", String.class);
58       return systemPropertiesGetMethod;
59     } catch (ClassNotFoundException | NoSuchMethodException e) {
60       LogUtil.e("SystemPropertiesAccessor.get", "unable to access system class", e);
61       return null;
62     }
63   }
64 }
65