summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorDaniel Colascione <dancol@google.com>2019-11-20 20:47:44 -0800
committerandroid-build-merger <android-build-merger@google.com>2019-11-20 20:47:44 -0800
commit1077797c14b514baca1202268fc4edd0e24e8e29 (patch)
tree7e579463b7b8e6e34ecac0b4769b7d1ae120e923 /core/java/android
parentf1b067c67106d70c17e84470975dc66e625ac8f8 (diff)
parent54543bb423f8d86fb87f357de12fb18089b26a58 (diff)
Merge changes from topic "property-handle"
am: 54543bb423 Change-Id: Id1ebdeca53c8705ce3cfab8db2e25cfebaa7f8a3
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/os/SystemProperties.java112
1 files changed, 99 insertions, 13 deletions
diff --git a/core/java/android/os/SystemProperties.java b/core/java/android/os/SystemProperties.java
index 8b0ffe155fc3..f08e1ff16999 100644
--- a/core/java/android/os/SystemProperties.java
+++ b/core/java/android/os/SystemProperties.java
@@ -26,6 +26,9 @@ import android.util.MutableInt;
import com.android.internal.annotations.GuardedBy;
+import dalvik.annotation.optimization.CriticalNative;
+import dalvik.annotation.optimization.FastNative;
+
import libcore.util.HexEncoding;
import java.nio.charset.StandardCharsets;
@@ -93,18 +96,43 @@ public class SystemProperties {
}
}
+ // The one-argument version of native_get used to be a regular native function. Nowadays,
+ // we use the two-argument form of native_get all the time, but we can't just delete the
+ // one-argument overload: apps use it via reflection, as the UnsupportedAppUsage annotation
+ // indicates. Let's just live with having a Java function with a very unusual name.
@UnsupportedAppUsage
- private static native String native_get(String key);
+ private static String native_get(String key) {
+ return native_get(key, "");
+ }
+
+ @FastNative
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static native String native_get(String key, String def);
+ @FastNative
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static native int native_get_int(String key, int def);
+ @FastNative
@UnsupportedAppUsage
private static native long native_get_long(String key, long def);
+ @FastNative
@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);
+
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private static native void native_add_change_callback();
private static native void native_report_sysprop_change();
@@ -229,25 +257,27 @@ public class SystemProperties {
@SuppressWarnings("unused") // Called from native code.
private static void callChangeCallbacks() {
+ ArrayList<Runnable> callbacks = null;
synchronized (sChangeCallbacks) {
//Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
if (sChangeCallbacks.size() == 0) {
return;
}
- ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
- final long token = Binder.clearCallingIdentity();
- try {
- for (int i = 0; i < callbacks.size(); i++) {
- try {
- callbacks.get(i).run();
- } catch (Throwable t) {
- Log.wtf(TAG, "Exception in SystemProperties change callback", t);
- // Ignore and try to go on.
- }
+ callbacks = new ArrayList<Runnable>(sChangeCallbacks);
+ }
+ final long token = Binder.clearCallingIdentity();
+ try {
+ for (int i = 0; i < callbacks.size(); i++) {
+ try {
+ callbacks.get(i).run();
+ } catch (Throwable t) {
+ // Ignore and try to go on. Don't use wtf here: that
+ // will cause the process to exit on some builds and break tests.
+ Log.e(TAG, "Exception in SystemProperties change callback", t);
}
- } finally {
- Binder.restoreCallingIdentity(token);
}
+ } finally {
+ Binder.restoreCallingIdentity(token);
}
}
@@ -284,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;
+ }
+ }
}