1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "simulator_resource_jni_util.h"
21 #include "simulator_common_jni.h"
23 extern SimulatorClassRefs gSimulatorClassRefs;
25 std::vector<int> convertIntegerVector(JNIEnv *env, jobject jVectorInt)
27 std::vector<int> vectorInt;
29 jclass vectorClass = env->FindClass("java/util/Vector");
35 jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
41 jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
47 jint jSize = env->CallIntMethod(jVectorInt, size);
48 int sizeOfVector = jSize;
50 for (int index = 0; index < sizeOfVector; index++)
53 jint jValue = env->CallIntMethod(jVectorInt, get, jIndex);
54 vectorInt.push_back((int)jValue);
60 std::vector<double> convertDoubleVector(JNIEnv *env, jobject jVectorDouble)
62 std::vector<double> vectorDouble;
64 jclass vectorClass = env->FindClass("java/util/Vector");
70 jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
76 jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
82 jint jSize = env->CallIntMethod(jVectorDouble, size);
83 int sizeOfVector = jSize;
85 for (int index = 0; index < sizeOfVector; index++)
88 jdouble jValue = env->CallDoubleMethod(jVectorDouble, get, jIndex);
89 vectorDouble.push_back((double)jValue);
95 std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString)
97 std::vector<std::string> vectorString;
99 jclass vectorClass = env->FindClass("java/util/Vector");
105 jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
111 jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
117 jint jSize = env->CallIntMethod(jVectorString, size);
118 int sizeOfVector = jSize;
120 for (int index = 0; index < sizeOfVector; index++)
123 jstring jContactInfoObj = (jstring)env->CallObjectMethod(jVectorString, get, jIndex);
124 if (jContactInfoObj == NULL)
128 const char *buff = env->GetStringUTFChars(jContactInfoObj, 0);
131 std::string tempString = buff;
132 vectorString.push_back(tempString);
135 env->ReleaseStringUTFChars(jContactInfoObj, buff);
141 void convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap,
142 std::map<std::string, std::string> &queryParams)
144 if (!hashMap) return;
146 jobject jEntrySet = env->CallObjectMethod(hashMap, gSimulatorClassRefs.classMapEntrySet);
147 jobject jIterator = env->CallObjectMethod(jEntrySet, gSimulatorClassRefs.classIteratorId);
148 if (!jEntrySet || !jIterator || env->ExceptionCheck()) return;
150 while (env->CallBooleanMethod(jIterator, gSimulatorClassRefs.classHasNextId))
152 jobject jEntry = env->CallObjectMethod(jIterator, gSimulatorClassRefs.classNextId);
154 jstring jKey = (jstring)env->CallObjectMethod(jEntry, gSimulatorClassRefs.classMapGetKey);
156 jstring jValue = (jstring)env->CallObjectMethod(jEntry, gSimulatorClassRefs.classMapGetValue);
159 queryParams.insert(std::make_pair(env->GetStringUTFChars(jKey, NULL),
160 env->GetStringUTFChars(jValue, NULL)));
162 if (env->ExceptionCheck()) return;
163 env->DeleteLocalRef(jEntry);
164 env->DeleteLocalRef(jKey);
165 env->DeleteLocalRef(jValue);
169 jobject convertHashMapToJavaMap(JNIEnv *env,
170 const std::map<std::string, uint8_t> &observersList)
172 if (observersList.empty())
177 jobject jObserverListMap = env->NewObject(gSimulatorClassRefs.classHashMap,
178 gSimulatorClassRefs.classHashMapCtor);
180 for (auto it = observersList.begin(); it != observersList.end(); ++it)
182 jstring key = (*env).NewStringUTF( (*it).first.c_str() );
183 jint value = (*it).second;
184 env->CallObjectMethod(jObserverListMap, gSimulatorClassRefs.classHashMapPut, key, value);
187 return jObserverListMap;
190 jobject convertStringVectorToJavaList(JNIEnv *env, std::vector<std::string> &vector)
192 jobject jList = env->NewObject(gSimulatorClassRefs.classLinkedList,
193 gSimulatorClassRefs.classLinkedListCtor);
194 if (!jList) return nullptr;
195 for (size_t i = 0; i < vector.size(); ++i)
197 jstring jStr = env->NewStringUTF(vector[i].c_str());
198 if (!jStr) return nullptr;
199 env->CallBooleanMethod(jList, gSimulatorClassRefs.classLinkedListAddObject, jStr);
200 if (env->ExceptionCheck()) return nullptr;
201 env->DeleteLocalRef(jStr);