hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.rockchip.alexa.jacky.utils;
 
import java.lang.reflect.Method;
 
public class SystemProperties {
    private static Method setMethod = null;
    private static Method getMethod = null;
    private static Method getIntMethod = null;
    private static Method getLongMethod = null;
    private static Method getBooleanMethod = null;
 
    /**
     * Set the value for the given key.
     * 
     * @param key
     *            the key to setup
     * @param val
     *            a value to set
     * @return
     */
    public static void set(final String key, final String val) {
        try {
            if (setMethod == null) {
                setMethod = Class.forName("android.os.SystemProperties")
                        .getMethod("set", String.class, String.class);
            }
            setMethod.invoke(null, key, val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Get the value for the given key
     * 
     * @param key
     *            the key to lookup
     * @param def
     *            a default value to return
     * @return the key parsed as an integer, or def if the key isn't found or
     *         cannot be parsed
     */
    public static String get(final String key, final String def) {
        try {
            if (getMethod == null) {
                getMethod = Class.forName("android.os.SystemProperties")
                        .getMethod("get", String.class, String.class);
            }
            return (String) getMethod.invoke(null, key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
 
    /**
     * Get the value for the given key
     * 
     * @param key
     *            the key to lookup
     * @param def
     *            a default value to return
     * @return the key parsed as an integer, or def if the key isn't found or
     *         cannot be parsed
     */
    public static int getInt(final String key, final int def) {
        try {
            if (getIntMethod == null) {
                getIntMethod = Class.forName("android.os.SystemProperties")
                        .getMethod("getInt", String.class, Integer.class);
            }
            return (Integer) getIntMethod.invoke(null, key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
 
    /**
     * Get the value for the given key
     * 
     * @param key
     *            the key to lookup
     * @param def
     *            a default value to return
     * @return the key parsed as an long, or def if the key isn't found or
     *         cannot be parsed
     */
    public static long getLong(final String key, final long def) {
        try {
            if (getLongMethod == null) {
                getLongMethod = Class.forName("android.os.SystemProperties")
                        .getMethod("getLong", String.class, long.class);
            }
            return ((Long) getLongMethod.invoke(null, key, def)).longValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
 
    /**
     * Get the value for the given key
     * 
     * @param key
     *            the key to lookup
     * @param def
     *            a default value to return
     * @return the key parsed as an boolean, or def if the key isn't found or
     *         cannot be parsed
     */
    public static boolean getBoolean(final String key, final boolean def) {
        try {
            if (getBooleanMethod == null) {
                getBooleanMethod = Class.forName("android.os.SystemProperties")
                        .getMethod("getBoolean", String.class, boolean.class);
            }
            return (Boolean) getBooleanMethod.invoke(null, key, def);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return def;
    }
}