Add Bool* APIs to HwParcel (DO NOT MERGE)
Bug: 31045584
Change-Id: I202c4aa7caf92a4cd7e3a45ef6784638e41facc8
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/core/java/android/os/HwParcel.java b/core/java/android/os/HwParcel.java
index fe7cdcc..52579e5 100644
--- a/core/java/android/os/HwParcel.java
+++ b/core/java/android/os/HwParcel.java
@@ -44,6 +44,7 @@
}
public native final void writeInterfaceToken(String interfaceName);
+ public native final void writeBool(boolean val);
public native final void writeInt8(byte val);
public native final void writeInt16(short val);
public native final void writeInt32(int val);
@@ -52,6 +53,8 @@
public native final void writeDouble(double val);
public native final void writeString(String val);
+ public native final void writeBoolArray(int size, boolean[] val);
+ public native final void writeBoolVector(boolean[] val);
public native final void writeInt8Array(int size, byte[] val);
public native final void writeInt8Vector(byte[] val);
public native final void writeInt16Array(int size, short[] val);
@@ -70,6 +73,7 @@
public native final void writeStrongBinder(IHwBinder binder);
public native final void enforceInterface(String interfaceName);
+ public native final boolean readBool();
public native final byte readInt8();
public native final short readInt16();
public native final int readInt32();
@@ -78,6 +82,8 @@
public native final double readDouble();
public native final String readString();
+ public native final boolean[] readBoolArray(int size);
+ public native final boolean[] readBoolVector();
public native final byte[] readInt8Array(int size);
public native final byte[] readInt8Vector();
public native final short[] readInt16Array(int size);
diff --git a/core/jni/android_os_HwParcel.cpp b/core/jni/android_os_HwParcel.cpp
index 0202303..94918f6 100644
--- a/core/jni/android_os_HwParcel.cpp
+++ b/core/jni/android_os_HwParcel.cpp
@@ -333,6 +333,7 @@
return val; \
}
+DEFINE_PARCEL_WRITER(Bool,jboolean)
DEFINE_PARCEL_WRITER(Int8,jbyte)
DEFINE_PARCEL_WRITER(Int16,jshort)
DEFINE_PARCEL_WRITER(Int32,jint)
@@ -347,6 +348,17 @@
DEFINE_PARCEL_READER(Float,jfloat)
DEFINE_PARCEL_READER(Double,jdouble)
+static jboolean JHwParcel_native_readBool(JNIEnv *env, jobject thiz) {
+ hardware::Parcel *parcel =
+ JHwParcel::GetNativeContext(env, thiz)->getParcel();
+
+ bool val;
+ status_t err = parcel->readBool(&val);
+ signalExceptionForError(env, err);
+
+ return (jboolean)val;
+}
+
static void JHwParcel_native_writeStatus(
JNIEnv *env, jobject thiz, jint statusCode) {
using hardware::Status;
@@ -489,6 +501,90 @@
DEFINE_PARCEL_VECTOR_WRITER(Float,jfloat)
DEFINE_PARCEL_VECTOR_WRITER(Double,jdouble)
+static void JHwParcel_native_writeBoolArray(
+ JNIEnv *env, jobject thiz, jint size, jbooleanArray valObj) {
+ if (valObj == NULL) {
+ jniThrowException(env, "java/lang/NullPointerException", NULL);
+ return;
+ }
+
+ jsize len = env->GetArrayLength(valObj);
+
+ if (len != size) {
+ jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
+ return;
+ }
+
+ sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz);
+
+ jboolean *src = env->GetBooleanArrayElements(valObj, nullptr);
+
+ bool *dst =
+ (bool *)impl->getStorage()->allocTemporaryStorage(size * sizeof(bool));
+
+ for (jint i = 0; i < size; ++i) {
+ dst[i] = src[i];
+ }
+
+ env->ReleaseBooleanArrayElements(valObj, src, 0 /* mode */);
+ src = nullptr;
+
+ hardware::Parcel *parcel = impl->getParcel();
+
+ size_t parentHandle;
+ status_t err = parcel->writeBuffer(
+ dst, size * sizeof(*dst), &parentHandle);
+
+ signalExceptionForError(env, err);
+}
+
+static void JHwParcel_native_writeBoolVector(
+ JNIEnv *env, jobject thiz, jbooleanArray valObj) {
+ if (valObj == NULL) {
+ jniThrowException(env, "java/lang/NullPointerException", NULL);
+ return;
+ }
+
+ sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz);
+
+ hidl_vec<bool> *vec =
+ (hidl_vec<bool> *)impl->getStorage()->allocTemporaryStorage(
+ sizeof(hidl_vec<bool>));
+
+ jsize len = env->GetArrayLength(valObj);
+
+ jboolean *src = env->GetBooleanArrayElements(valObj, nullptr);
+
+ bool *dst =
+ (bool *)impl->getStorage()->allocTemporaryStorage(len * sizeof(bool));
+
+ for (jsize i = 0; i < len; ++i) {
+ dst[i] = src[i];
+ }
+
+ env->ReleaseBooleanArrayElements(valObj, src, 0 /* mode */);
+ src = nullptr;
+
+ vec->setToExternal(dst, len);
+
+ hardware::Parcel *parcel = impl->getParcel();
+
+ size_t parentHandle;
+ status_t err = parcel->writeBuffer(vec, sizeof(*vec), &parentHandle);
+
+ if (err == OK) {
+ size_t childHandle;
+
+ err = vec->writeEmbeddedToParcel(
+ parcel,
+ parentHandle,
+ 0 /* parentOffset */,
+ &childHandle);
+ }
+
+ signalExceptionForError(env, err);
+}
+
static void JHwParcel_native_writeStrongBinder(
JNIEnv *env, jobject thiz, jobject binderObj) {
sp<hardware::IBinder> binder;
@@ -616,6 +712,64 @@
DEFINE_PARCEL_VECTOR_READER(Float,jfloat,Float)
DEFINE_PARCEL_VECTOR_READER(Double,jdouble,Double)
+static jbooleanArray JHwParcel_native_readBoolArray(
+ JNIEnv *env, jobject thiz, jint size) {
+ hardware::Parcel *parcel =
+ JHwParcel::GetNativeContext(env, thiz)->getParcel();
+
+ size_t parentHandle;
+ const bool *val = static_cast<const bool *>(
+ parcel->readBuffer(&parentHandle));
+
+ jbooleanArray valObj = env->NewBooleanArray(size);
+
+ for (jint i = 0; i < size; ++i) {
+ jboolean x = val[i];
+ env->SetBooleanArrayRegion(valObj, i, 1, &x);
+ }
+
+ return valObj;
+}
+
+static jbooleanArray JHwParcel_native_readBoolVector(
+ JNIEnv *env, jobject thiz) {
+ hardware::Parcel *parcel =
+ JHwParcel::GetNativeContext(env, thiz)->getParcel();
+
+ size_t parentHandle;
+
+ const hidl_vec<bool> *vec =
+ (const hidl_vec<bool> *)parcel->readBuffer(&parentHandle);
+
+ if (vec == NULL) {
+ signalExceptionForError(env, UNKNOWN_ERROR);
+ return NULL;
+ }
+
+ size_t childHandle;
+
+ status_t err = const_cast<hidl_vec<bool> *>(vec)
+ ->readEmbeddedFromParcel(
+ *parcel,
+ parentHandle,
+ 0 /* parentOffset */,
+ &childHandle);
+
+ if (err != OK) {
+ signalExceptionForError(env, err);
+ return NULL;
+ }
+
+ jbooleanArray valObj = env->NewBooleanArray(vec->size());
+
+ for (size_t i = 0; i < vec->size(); ++i) {
+ jboolean x = (*vec)[i];
+ env->SetBooleanArrayRegion(valObj, i, 1, &x);
+ }
+
+ return valObj;
+}
+
static jobjectArray MakeStringArray(
JNIEnv *env, const hidl_string *array, size_t size) {
ScopedLocalRef<jclass> stringKlass(
@@ -825,6 +979,7 @@
{ "writeInterfaceToken", "(Ljava/lang/String;)V",
(void *)JHwParcel_native_writeInterfaceToken },
+ { "writeBool", "(Z)V", (void *)JHwParcel_native_writeBool },
{ "writeInt8", "(B)V", (void *)JHwParcel_native_writeInt8 },
{ "writeInt16", "(S)V", (void *)JHwParcel_native_writeInt16 },
{ "writeInt32", "(I)V", (void *)JHwParcel_native_writeInt32 },
@@ -835,6 +990,8 @@
{ "writeString", "(Ljava/lang/String;)V",
(void *)JHwParcel_native_writeString },
+ { "writeBoolArray", "(I[Z)V", (void *)JHwParcel_native_writeBoolArray },
+ { "writeBoolVector", "([Z)V", (void *)JHwParcel_native_writeBoolVector },
{ "writeInt8Array", "(I[B)V", (void *)JHwParcel_native_writeInt8Array },
{ "writeInt8Vector", "([B)V", (void *)JHwParcel_native_writeInt8Vector },
{ "writeInt16Array", "(I[S)V", (void *)JHwParcel_native_writeInt16Array },
@@ -862,6 +1019,7 @@
{ "enforceInterface", "(Ljava/lang/String;)V",
(void *)JHwParcel_native_enforceInterface },
+ { "readBool", "()Z", (void *)JHwParcel_native_readBool },
{ "readInt8", "()B", (void *)JHwParcel_native_readInt8 },
{ "readInt16", "()S", (void *)JHwParcel_native_readInt16 },
{ "readInt32", "()I", (void *)JHwParcel_native_readInt32 },
@@ -872,6 +1030,8 @@
{ "readString", "()Ljava/lang/String;",
(void *)JHwParcel_native_readString },
+ { "readBoolArray", "(I)[Z", (void *)JHwParcel_native_readBoolArray },
+ { "readBoolVector", "()[Z", (void *)JHwParcel_native_readBoolVector },
{ "readInt8Array", "(I)[B", (void *)JHwParcel_native_readInt8Array },
{ "readInt8Vector", "()[B", (void *)JHwParcel_native_readInt8Vector },
{ "readInt16Array", "(I)[S", (void *)JHwParcel_native_readInt16Array },