cmsdk: Add persistent properties API

* Add support for reading and writing values from/to persistent
   storage. Requires the MANAGE_PERSISTENT_STORAGE permission, which
   should not be available for general use by applications.

Change-Id: I8a793396d207f23fcda851c172372f2073778eec
This commit is contained in:
Steve Kondik
2015-08-31 18:43:51 -07:00
committed by Gerrit Code Review
parent 087f2b536d
commit 7cef6f6945
8 changed files with 303 additions and 1 deletions

View File

@@ -23,7 +23,9 @@ import android.util.Log;
import cyanogenmod.app.CMContextConstants;
import java.io.UnsupportedEncodingException;
import java.lang.IllegalArgumentException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
@@ -112,6 +114,11 @@ public final class CMHardwareManager {
*/
public static final int FEATURE_DISPLAY_MODES = 0x2000;
/**
* Persistent storage
*/
public static final int FEATURE_PERSISTENT_STORAGE = 0x4000;
private static final List<Integer> BOOLEAN_FEATURES = Arrays.asList(
FEATURE_ADAPTIVE_BACKLIGHT,
FEATURE_COLOR_ENHANCEMENT,
@@ -412,6 +419,132 @@ public final class CMHardwareManager {
return false;
}
/**
* Write a string to persistent storage, which persists thru factory reset
*
* @param key String identifier for this item
* @param value The UTF-8 encoded string to store
* @return true on success
*/
public boolean writePersistentString(String key, String value) {
try {
if (checkService()) {
return getService().writePersistentBytes(key,
value == null ? null : value.getBytes("UTF-8"));
}
} catch (RemoteException e) {
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage(), e);
}
return false;
}
/**
* Write an integer to persistent storage, which persists thru factory reset
*
* @param key String identifier for this item
* @param value The integer to store
* @return true on success
*/
public boolean writePersistentInt(String key, int value) {
try {
if (checkService()) {
return getService().writePersistentBytes(key,
ByteBuffer.allocate(4).putInt(value).array());
}
} catch (RemoteException e) {
}
return false;
}
/**
* Write a byte array to persistent storage, which persists thru factory reset
*
* @param key String identifier for this item
* @param value The byte array to store, up to 4096 bytes
* @return true on success
*/
public boolean writePersistentBytes(String key, byte[] value) {
try {
if (checkService()) {
return getService().writePersistentBytes(key, value);
}
} catch (RemoteException e) {
}
return false;
}
/**
* Read a string from persistent storage
*
* @param key String identifier for this item
* @return the stored UTF-8 encoded string, null if not found
*/
public String readPersistentString(String key) {
try {
if (checkService()) {
byte[] bytes = getService().readPersistentBytes(key);
if (bytes != null) {
return new String(bytes, "UTF-8");
}
}
} catch (RemoteException e) {
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
/**
* Read an integer from persistent storage
*
* @param key String identifier for this item
* @return the stored integer, zero if not found
*/
public int readPersistentInt(String key) {
try {
if (checkService()) {
byte[] bytes = getService().readPersistentBytes(key);
if (bytes != null) {
return ByteBuffer.wrap(bytes).getInt();
}
}
} catch (RemoteException e) {
}
return 0;
}
/**
* Read a byte array from persistent storage
*
* @param key String identifier for this item
* @return the stored byte array, null if not found
*/
public byte[] readPersistentBytes(String key) {
try {
if (checkService()) {
return getService().readPersistentBytes(key);
}
} catch (RemoteException e) {
}
return null;
}
/** Delete an object from persistent storage
*
* @param key String identifier for this item
* @return true if an item was deleted
*/
public boolean deletePersistentObject(String key) {
try {
if (checkService()) {
return getService().writePersistentBytes(key, null);
}
} catch (RemoteException e) {
}
return false;
}
/**
* {@hide}
*/

View File

@@ -47,4 +47,7 @@ interface ICMHardwareService {
DisplayMode getCurrentDisplayMode();
DisplayMode getDefaultDisplayMode();
boolean setDisplayMode(in DisplayMode mode, boolean makeDefault);
boolean writePersistentBytes(String key, in byte[] bytes);
byte[] readPersistentBytes(String key);
}