1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "JNIManager.h"
18
19 #include <mutex>
20 #include <vector>
21
22 #include "jni.h"
23
sendResultsToJava(std::map<std::string,std::string> data)24 void JNIManager::sendResultsToJava(std::map<std::string, std::string> data) {
25 JNIManager& manager = getInstance();
26 JNIEnv* env = manager.AttachCurrentThread();
27 jclass stringClass = env->FindClass("java/lang/String");
28 jmethodID sendMethod = env->GetMethodID(manager.getMainActivityClass(), "sendResultsToJava",
29 "([Ljava/lang/String;[Ljava/lang/String;)V");
30
31 jobjectArray namesOut = env->NewObjectArray(data.size(), stringClass, 0);
32 jobjectArray valuesOut = env->NewObjectArray(data.size(), stringClass, 0);
33
34 int index = 0;
35 for (auto&& item : data) {
36 env->SetObjectArrayElement(namesOut, index, env->NewStringUTF(item.first.c_str()));
37 env->SetObjectArrayElement(valuesOut, index, env->NewStringUTF(item.second.c_str()));
38 ++index;
39 }
40
41 env->CallVoidMethod(manager.app_->activity->clazz, sendMethod, namesOut, valuesOut);
42 manager.DetachCurrentThread();
43 }
44
sendConfigToNative(JNIEnv * env,jobject,jobjectArray data)45 void JNIManager::sendConfigToNative(JNIEnv* env, jobject, jobjectArray data) {
46 std::lock_guard lock(getInstance().mutex_);
47 size_t length = env->GetArrayLength(data);
48 std::vector<std::string> out{};
49 for (int i = 0; i < length; ++i) {
50 jstring str = static_cast<jstring>(env->GetObjectArrayElement(data, i));
51 const char* rawStr = env->GetStringUTFChars(str, 0);
52 out.push_back({rawStr});
53 env->ReleaseStringUTFChars(str, rawStr);
54 }
55 getInstance().testNames_.set_value(out);
56 }
57
getTestNames()58 std::vector<std::string> JNIManager::getTestNames() {
59 return testNames_.get_future().get();
60 }
61
JNI_OnLoad(JavaVM * vm,void *)62 JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
63 JNIEnv* env;
64 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
65 return JNI_ERR;
66 }
67
68 static jclass deviceActivityClass =
69 env->FindClass("android/adpf/hintsession/app/ADPFHintSessionDeviceActivity");
70 JNIManager::getInstance().setMainActivityClassGlobalRef(
71 reinterpret_cast<jclass>(env->NewGlobalRef(deviceActivityClass)));
72
73 if (deviceActivityClass == nullptr) return JNI_ERR;
74
75 static const JNINativeMethod methods[] = {
76 {"sendConfigToNative", "([Ljava/lang/String;)V",
77 reinterpret_cast<void*>(JNIManager::sendConfigToNative)},
78
79 };
80
81 int rc = env->RegisterNatives(deviceActivityClass, methods,
82 sizeof(methods) / sizeof(JNINativeMethod));
83 if (rc != JNI_OK) return rc;
84
85 return JNI_VERSION_1_6;
86 }
87