• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 android.os;
18 
19 
20 /**
21  * Gives access to the system properties store.  The system properties
22  * store contains a list of string key-value pairs.
23  *
24  * {@hide}
25  */
26 public class SystemProperties
27 {
28     public static final int PROP_NAME_MAX = 31;
29     public static final int PROP_VALUE_MAX = 91;
30 
native_get(String key)31     private static native String native_get(String key);
native_get(String key, String def)32     private static native String native_get(String key, String def);
native_get_int(String key, int def)33     private static native int native_get_int(String key, int def);
native_get_long(String key, long def)34     private static native long native_get_long(String key, long def);
native_get_boolean(String key, boolean def)35     private static native boolean native_get_boolean(String key, boolean def);
native_set(String key, String def)36     private static native void native_set(String key, String def);
37 
38     /**
39      * Get the value for the given key.
40      * @return an empty string if the key isn't found
41      * @throws IllegalArgumentException if the key exceeds 32 characters
42      */
get(String key)43     public static String get(String key) {
44         if (key.length() > PROP_NAME_MAX) {
45             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
46         }
47         return native_get(key);
48     }
49 
50     /**
51      * Get the value for the given key.
52      * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
53      * @throws IllegalArgumentException if the key exceeds 32 characters
54      */
get(String key, String def)55     public static String get(String key, String def) {
56         if (key.length() > PROP_NAME_MAX) {
57             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
58         }
59         return native_get(key, def);
60     }
61 
62     /**
63      * Get the value for the given key, and return as an integer.
64      * @param key the key to lookup
65      * @param def a default value to return
66      * @return the key parsed as an integer, or def if the key isn't found or
67      *         cannot be parsed
68      * @throws IllegalArgumentException if the key exceeds 32 characters
69      */
getInt(String key, int def)70     public static int getInt(String key, int def) {
71         if (key.length() > PROP_NAME_MAX) {
72             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
73         }
74         return native_get_int(key, def);
75     }
76 
77     /**
78      * Get the value for the given key, and return as a long.
79      * @param key the key to lookup
80      * @param def a default value to return
81      * @return the key parsed as a long, or def if the key isn't found or
82      *         cannot be parsed
83      * @throws IllegalArgumentException if the key exceeds 32 characters
84      */
getLong(String key, long def)85     public static long getLong(String key, long def) {
86         if (key.length() > PROP_NAME_MAX) {
87             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
88         }
89         return native_get_long(key, def);
90     }
91 
92     /**
93      * Get the value for the given key, returned as a boolean.
94      * Values 'n', 'no', '0', 'false' or 'off' are considered false.
95      * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
96      * (case insensitive).
97      * If the key does not exist, or has any other value, then the default
98      * result is returned.
99      * @param key the key to lookup
100      * @param def a default value to return
101      * @return the key parsed as a boolean, or def if the key isn't found or is
102      *         not able to be parsed as a boolean.
103      * @throws IllegalArgumentException if the key exceeds 32 characters
104      */
getBoolean(String key, boolean def)105     public static boolean getBoolean(String key, boolean def) {
106         if (key.length() > PROP_NAME_MAX) {
107             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
108         }
109         return native_get_boolean(key, def);
110     }
111 
112     /**
113      * Set the value for the given key.
114      * @throws IllegalArgumentException if the key exceeds 32 characters
115      * @throws IllegalArgumentException if the value exceeds 92 characters
116      */
set(String key, String val)117     public static void set(String key, String val) {
118         if (key.length() > PROP_NAME_MAX) {
119             throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
120         }
121         if (val != null && val.length() > PROP_VALUE_MAX) {
122             throw new IllegalArgumentException("val.length > " +
123                 PROP_VALUE_MAX);
124         }
125         native_set(key, val);
126     }
127 }
128