summaryrefslogtreecommitdiff
path: root/core/java/android/os/SystemProperties.java
diff options
context:
space:
mode:
authorDaniel Colascione <dancol@google.com>2019-11-14 13:22:31 -0800
committerDaniel Colascione <dancol@google.com>2019-11-19 13:28:56 -0800
commit6e2cff7fa1b32b5ea57ad619e6e6d0dcd6ae8bfe (patch)
treec6b6a046b597fb4a651cbcff88edcf2ffea2ee40 /core/java/android/os/SystemProperties.java
parent5e8ba5f05a047259d79a2f2487dc6053a21ae2ec (diff)
Add new SystemProperties.Handle interface
This new interface allows Java code to look up property values without paying for the name->prop_info mapping and, in the case of looking up scalars, without doing any allocation. Bug: 140788621 Test: added tests Test: atest FrameworksCoreSystemPropertiesTests Change-Id: I46d12f62499e9e124fe9add588376d724b364d5d
Diffstat (limited to 'core/java/android/os/SystemProperties.java')
-rw-r--r--core/java/android/os/SystemProperties.java69
1 files changed, 68 insertions, 1 deletions
diff --git a/core/java/android/os/SystemProperties.java b/core/java/android/os/SystemProperties.java
index 2b4f6f867e03..f08e1ff16999 100644
--- a/core/java/android/os/SystemProperties.java
+++ b/core/java/android/os/SystemProperties.java
@@ -26,6 +26,7 @@ import android.util.MutableInt;
import com.android.internal.annotations.GuardedBy;
+import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;
import libcore.util.HexEncoding;
@@ -37,7 +38,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
-
/**
* Gives access to the system properties store. The system properties
* store contains a list of string key-value pairs.
@@ -118,6 +118,17 @@ public class SystemProperties {
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static native boolean native_get_boolean(String key, boolean def);
+ @FastNative
+ private static native long native_find(String name);
+ @FastNative
+ private static native String native_get(long handle);
+ @CriticalNative
+ private static native int native_get_int(long handle, int def);
+ @CriticalNative
+ private static native long native_get_long(long handle, long def);
+ @CriticalNative
+ private static native boolean native_get_boolean(long handle, boolean def);
+
// _NOT_ FastNative: native_set performs IPC and can block
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static native void native_set(String key, String def);
@@ -303,4 +314,60 @@ public class SystemProperties {
@UnsupportedAppUsage
private SystemProperties() {
}
+
+ /**
+ * Look up a property location by name.
+ * @name name of the property
+ * @return property handle or {@code null} if property isn't set
+ * @hide
+ */
+ @Nullable public static Handle find(@NonNull String name) {
+ long nativeHandle = native_find(name);
+ if (nativeHandle == 0) {
+ return null;
+ }
+ return new Handle(nativeHandle);
+ }
+
+ /**
+ * Handle to a pre-located property. Looking up a property handle in advance allows
+ * for optimal repeated lookup of a single property.
+ * @hide
+ */
+ public static final class Handle {
+
+ private final long mNativeHandle;
+
+ /**
+ * @return Value of the property
+ */
+ @NonNull public String get() {
+ return native_get(mNativeHandle);
+ }
+ /**
+ * @param def default value
+ * @return value or {@code def} on parse error
+ */
+ public int getInt(int def) {
+ return native_get_int(mNativeHandle, def);
+ }
+ /**
+ * @param def default value
+ * @return value or {@code def} on parse error
+ */
+ public long getLong(long def) {
+ return native_get_long(mNativeHandle, def);
+ }
+ /**
+ * @param def default value
+ * @return value or {@code def} on parse error
+ */
+ public boolean getBoolean(boolean def) {
+ return native_get_boolean(mNativeHandle, def);
+ }
+
+ private Handle(long nativeHandle) {
+ mNativeHandle = nativeHandle;
+ }
+ }
}