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 "jni_things_manager_util.h"
21 #include "JniOcResource.h"
23 std::vector<std::string> convertStringVector(JNIEnv *env, jobject jVectorString)
25 std::vector<std::string> vectorString;
27 jclass vectorClass = env->FindClass(TM_JAVA_VECTOR_CLASS_PATH);
30 LOGE("convertStringVector: Not able to find class %s", TM_JAVA_VECTOR_CLASS_PATH);
34 jmethodID size = env->GetMethodID(vectorClass, "size", "()I");
37 LOGE("convertStringVector: Failed to get method id for size");
41 jmethodID get = env->GetMethodID(vectorClass, "get", "(I)""Ljava/lang/Object;");
44 LOGE("convertStringVector: Failed to get method id for get");
48 jint jSize = env->CallIntMethod(jVectorString, size);
49 int sizeOfVector = jSize;
51 for (int index = 0; index < sizeOfVector; index++)
54 jstring jContactInfoObj = (jstring)env->CallObjectMethod(jVectorString, get, jIndex);
55 if (jContactInfoObj == NULL)
57 LOGE("convertStringVector: Failed on CallObjectMethod");
60 const char *buff = env->GetStringUTFChars(jContactInfoObj, 0);
63 std::string tempString = buff;
64 vectorString.push_back(tempString);
67 env->ReleaseStringUTFChars(jContactInfoObj, buff);
73 std::map<std::string, std::string> convertStringMap(JNIEnv *env, jobject jMapString)
75 // Get reference to java/util/Map class
76 jclass mapClass = env->GetObjectClass(jMapString);
77 jmethodID sizeMethodId = env->GetMethodID(mapClass, "size", "()I");
78 jmethodID entrySetMethodId = env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;");
80 // Get reference to java/util/Set class
81 jclass setClass = env->FindClass("java/util/Set");
82 jmethodID iteratorMethodId = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;");
84 // Get reference to java/util/Iterator class
85 jclass iteratorClass = env->FindClass("java/util/Iterator");
86 jmethodID hasNextMethodId = env->GetMethodID(iteratorClass, "hasNext", "()Z");
87 jmethodID nextMethodId = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
89 // Get reference to java/util/Map/Entry class
90 jclass mapEntryClass = env->FindClass("java/util/Map$Entry");
91 jmethodID getKeyMethodId = env->GetMethodID(mapEntryClass, "getKey", "()Ljava/lang/Object;");
92 jmethodID getValueMethodId = env->GetMethodID(mapEntryClass, "getValue", "()Ljava/lang/Object;");
94 std::map<std::string, std::string> mapString;
95 // Get entry set from map
96 jobject entrySet = env->CallObjectMethod(jMapString, entrySetMethodId);
99 LOGE("convertStringMap: Failed to get entrySet");
103 // Get iterator from set
104 jobject iterator = env->CallObjectMethod(entrySet, iteratorMethodId);
107 LOGE("convertStringMap: Failed to get iterator from set");
111 if (JNI_TRUE == env->CallBooleanMethod(iterator, hasNextMethodId))
113 jobject entry = env->CallObjectMethod(iterator, nextMethodId);
114 jstring jMapKeyStr = (jstring)env->CallObjectMethod(entry, getKeyMethodId);
115 jstring jMapValueStr = (jstring)env->CallObjectMethod(entry, getValueMethodId);
117 const char *key = env->GetStringUTFChars(jMapKeyStr, 0);
118 const char *value = env->GetStringUTFChars(jMapValueStr, 0);
120 if ((NULL != key) && (NULL != value))
122 std::string keyStr = key;
123 std::string valueStr = value;
124 mapString.insert(std::make_pair(keyStr, valueStr));
126 env->ReleaseStringUTFChars(jMapKeyStr, key);
127 env->ReleaseStringUTFChars(jMapValueStr, value);