Created
February 17, 2016 06:58
-
-
Save joinAero/9da3ec46cef137e599c8 to your computer and use it in GitHub Desktop.
Android - Helper for accessing os properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cc.cubone.turbo.core.os; | |
import android.os.Environment; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Collection; | |
import java.util.Enumeration; | |
import java.util.Map; | |
import java.util.Properties; | |
import java.util.Set; | |
public class BuildProperties { | |
private final Properties properties; | |
private BuildProperties() throws IOException { | |
InputStream in = new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")); | |
properties = new Properties(); | |
properties.load(in); | |
in.close(); | |
} | |
public static BuildProperties newInstance() throws IOException { | |
return new BuildProperties(); | |
} | |
public boolean containsKey(final Object key) { | |
return properties.containsKey(key); | |
} | |
public boolean containsValue(final Object value) { | |
return properties.containsValue(value); | |
} | |
public Set<Map.Entry<Object, Object>> entrySet() { | |
return properties.entrySet(); | |
} | |
public String getProperty(final String name) { | |
return properties.getProperty(name); | |
} | |
public String getProperty(final String name, final String defaultValue) { | |
return properties.getProperty(name, defaultValue); | |
} | |
public boolean isEmpty() { | |
return properties.isEmpty(); | |
} | |
public Enumeration<Object> keys() { | |
return properties.keys(); | |
} | |
public Set<Object> keySet() { | |
return properties.keySet(); | |
} | |
public int size() { | |
return properties.size(); | |
} | |
public Collection<Object> values() { | |
return properties.values(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2015 Jared Rummler | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package cc.cubone.turbo.core.os; | |
/** | |
* Gives access to the system properties store. The system properties store contains a list of | |
* string key-value pairs. | |
* | |
* @see <a href="http://stackoverflow.com/questions/2641111/where-is-android-os-systemproperties"> | |
* Where is android.os.SystemProperties</a> | |
*/ | |
public class SystemProperties { | |
private static final Class<?> SP = getSystemPropertiesClass(); | |
private SystemProperties() { | |
throw new AssertionError(); | |
} | |
/** | |
* Get the value for the given key. | |
*/ | |
public static String get(String key) { | |
try { | |
return (String) SP.getMethod("get", String.class).invoke(null, key); | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
/** | |
* Get the value for the given key. | |
* | |
* @return if the key isn't found, return def if it isn't null, or an empty string otherwise | |
*/ | |
public static String get(String key, String def) { | |
try { | |
return (String) SP.getMethod("get", String.class, String.class).invoke(null, key, def); | |
} catch (Exception e) { | |
return def; | |
} | |
} | |
/** | |
* Get the value for the given key, returned as a boolean. Values 'n', 'no', '0', 'false' or | |
* 'off' are considered false. Values 'y', 'yes', '1', 'true' or 'on' are considered true. (case | |
* sensitive). If the key does not exist, or has any other value, then the default result is | |
* returned. | |
* | |
* @param key the key to lookup | |
* @param def a default value to return | |
* @return the key parsed as a boolean, or def if the key isn't found or is not able to be | |
* parsed as a boolean. | |
*/ | |
public static boolean getBoolean(String key, boolean def) { | |
try { | |
return (Boolean) SP.getMethod("getBoolean", String.class, boolean.class) | |
.invoke(null, key, def); | |
} catch (Exception e) { | |
return def; | |
} | |
} | |
/** | |
* Get the value for the given key, and return as an integer. | |
* | |
* @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(String key, int def) { | |
try { | |
return (Integer) SP.getMethod("getInt", String.class, int.class).invoke(null, key, def); | |
} catch (Exception e) { | |
return def; | |
} | |
} | |
/** | |
* Get the value for the given key, and return as a long. | |
* | |
* @param key the key to lookup | |
* @param def a default value to return | |
* @return the key parsed as a long, or def if the key isn't found or cannot be parsed | |
*/ | |
public static long getLong(String key, long def) { | |
try { | |
return (Long) SP.getMethod("getLong", String.class, long.class).invoke(null, key, def); | |
} catch (Exception e) { | |
return def; | |
} | |
} | |
/** | |
* Set the value for the given key. | |
* <p> | |
* <b>NOTE:</b> app processes do not have the privilege to write system properties. You will | |
* need to be the system or root user to set a system property. | |
* | |
* @throws IllegalArgumentException if the key exceeds 32 characters | |
* @throws IllegalArgumentException if the value exceeds 92 characters | |
*/ | |
public static void set(String key, String val) { | |
try { | |
SP.getMethod("set", String.class, String.class).invoke(null, key, val); | |
} catch (Exception ignored) { | |
} | |
} | |
private static Class<?> getSystemPropertiesClass() { | |
try { | |
return Class.forName("android.os.SystemProperties"); | |
} catch (ClassNotFoundException shouldNotHappen) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment