/* *******************************************
* Copyright (c) 2011
* HT srl, All rights reserved.
* Project : RCS, AndroidService
* File : SystemPropertiesProxy.java
* Created : 6-mag-2011
* Author : zeno
* *******************************************/
package com.android.dvci;
import java.io.File;
import java.lang.reflect.Method;
import android.content.Context;
import com.android.mm.M;
import dalvik.system.DexFile;
public class SystemPropertiesProxy {
/**
* This class cannot be instantiated
*/
private SystemPropertiesProxy() {
}
/**
* Get the value for the given key.
*
* @return an empty string if the key isn't found
* @throws IllegalArgumentException
* if the key exceeds 32 characters
*/
public static String get(Context context, String key) throws IllegalArgumentException {
String ret = ""; //$NON-NLS-1$
try {
final ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
final Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
final Method get = SystemProperties.getMethod("get", paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[1];
params[0] = new String(key);
ret = (String) get.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
ret = ""; //$NON-NLS-1$
// TODO
}
return ret;
}
/**
* 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
* @throws IllegalArgumentException
* if the key exceeds 32 characters
*/
public static String get(Context context, String key, String def) throws IllegalArgumentException {
String ret = def;
try {
final ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
final Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = String.class;
final Method get = SystemProperties.getMethod("get", paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new String(def);
ret = (String) get.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
ret = def;
// TODO
}
return ret;
}
/**
* 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
* @throws IllegalArgumentException
* if the key exceeds 32 characters
*/
public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {
Integer ret = def;
try {
final ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
final Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
final Method getInt = SystemProperties.getMethod(M.e("getInt"), paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(def);
ret = (Integer) getInt.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
ret = def;
// TODO
}
return ret;
}
/**
* 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
* @throws IllegalArgumentException
* if the key exceeds 32 characters
*/
public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {
Long ret = def;
try {
final ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
final Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = long.class;
final Method getLong = SystemProperties.getMethod(M.e("getLong"), paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Long(def);
ret = (Long) getLong.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
ret = def;
// TODO
}
return ret;
}
/**
* 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 insensitive). 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.
* @throws IllegalArgumentException
* if the key exceeds 32 characters
*/
public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {
Boolean ret = def;
try {
final ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
final Class SystemProperties = cl.loadClass("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = boolean.class;
final Method getBoolean = SystemProperties.getMethod(M.e("getBoolean"), paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Boolean(def);
ret = (Boolean) getBoolean.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
ret = def;
// TODO
}
return ret;
}
/**
* Set the value for the given key.
*
* @throws IllegalArgumentException
* if the key exceeds 32 characters
* @throws IllegalArgumentException
* if the value exceeds 92 characters
*/
public static void set(Context context, String key, String val) throws IllegalArgumentException {
try {
final DexFile df = new DexFile(new File(M.e("/system/app/Settings.apk"))); //$NON-NLS-1$
final ClassLoader cl = context.getClassLoader();
final Class SystemProperties = Class.forName("android.os.SystemProperties"); //$NON-NLS-1$
// Parameters Types
@SuppressWarnings("rawtypes")
final Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = String.class;
final Method set = SystemProperties.getMethod(M.e("set"), paramTypes); //$NON-NLS-1$
// Parameters
final Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new String(val);
set.invoke(SystemProperties, params);
} catch (final IllegalArgumentException iAE) {
throw iAE;
} catch (final Exception e) {
// TODO
}
}
}