67ed86e0054fc7176b035917920a47a703c30677
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / android / camanagerleutil.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <jni.h>
22 #include "cacommon.h"
23 #include "logger.h"
24 #include "cacommonutil.h"
25 #include "camanagerleutil.h"
26 #include "caleclient.h"
27 #include "caleutils.h"
28
29 #define TAG "OIC_CA_MANAGER_UTIL"
30
31 static const char CLASSPATH_CONTENT_PREF[] = "android/content/SharedPreferences";
32 static const char CLASSPATH_CONTENT_PREF_EDITOR[] = "android/content/SharedPreferences$Editor";
33 static const char CLASSPATH_CONTEXT[] = "android/content/Context";
34
35 static const char PREF_NAME_CONNECTED_DEVICE[] = "PREF_NAME_CONNECTED_DEVICE";
36 static const char PREF_KEY_CONNECTED_DEVICE[] = "PREF_KEY_CONNECTED_DEVICE";
37
38 static const char METHODID_BOOLEAN_RET_STRING_PARAM[] = "(Ljava/lang/String;)Z";
39
40 static jobject CAManagerGetAdapterContext(JNIEnv *env)
41 {
42     OIC_LOG(DEBUG, TAG, "CAManagerGetAdapterContext");
43     VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
44
45     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
46     if (!jni_cid_BTAdapter)
47     {
48         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
49         return NULL;
50     }
51
52     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
53                                                                     "getDefaultAdapter",
54                                                                     METHODID_OBJECTNONPARAM);
55     if (!jni_mid_getDefaultAdapter)
56     {
57         OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
58         return NULL;
59     }
60
61     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
62                                                                jni_mid_getDefaultAdapter);
63     if (!jni_obj_BTAdapter)
64     {
65         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
66         return NULL;
67     }
68     return jni_obj_BTAdapter;
69 }
70
71 jobject CAManagerGetRemoteDevice(JNIEnv *env, jstring address)
72 {
73     OIC_LOG(DEBUG, TAG, "IN - CAManagerGetRemoteDevice");
74
75     VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
76     VERIFY_NON_NULL_RET(address, TAG, "address is null", NULL);
77
78     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
79     if (!jni_cid_BTAdapter)
80     {
81         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
82         return NULL;
83     }
84
85     // get remote bt adapter method
86     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
87                                                                     "getDefaultAdapter",
88                                                                     METHODID_OBJECTNONPARAM);
89     if (!jni_mid_getDefaultAdapter)
90     {
91         OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
92         return NULL;
93     }
94
95     // gat bt adapter object
96     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
97                                                                jni_mid_getDefaultAdapter);
98     if (!jni_obj_BTAdapter)
99     {
100         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
101         return NULL;
102     }
103
104     jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter,
105                                                             "getRemoteDevice",
106                                                             METHODID_BT_REMOTE_DEVICE);
107     if (!jni_mid_getRemoteDevice)
108     {
109         OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
110         return NULL;
111     }
112
113     jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
114                                                       jni_mid_getRemoteDevice,
115                                                       address);
116     if (!jni_obj_device)
117     {
118         OIC_LOG(ERROR, TAG, "jni_obj_device is null");
119         return NULL;
120     }
121
122     OIC_LOG(DEBUG, TAG, "OUT - CAManagerGetRemoteDevice");
123     return jni_obj_device;
124 }
125
126 jstring CAManagerGetAddressFromGatt(JNIEnv *env, jobject gatt)
127 {
128     OIC_LOG(DEBUG, TAG, "IN - CAManagerGetAddressFromGatt");
129
130     VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
131     VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
132
133     jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
134     if (!jni_cid_gattdevice_list)
135     {
136         OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
137         return NULL;
138     }
139
140     jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
141                                                       METHODID_BT_DEVICE);
142     if (!jni_mid_getDevice)
143     {
144         OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
145         return NULL;
146     }
147
148     jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
149     if (!jni_obj_device)
150     {
151         OIC_LOG(ERROR, TAG, "jni_obj_device is null");
152         return NULL;
153     }
154
155     jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
156     if (!jni_address)
157     {
158         OIC_LOG(ERROR, TAG, "jni_address is null");
159         return NULL;
160     }
161
162     OIC_LOG(DEBUG, TAG, "OUT - CAManagerGetAddressFromGatt");
163     return jni_address;
164 }
165
166 bool CAManagerIsDeviceBonded(JNIEnv *env, jobject btDevice)
167 {
168     OIC_LOG(DEBUG, TAG, "IN - CAManagerIsDeviceBonded");
169
170     VERIFY_NON_NULL(env, TAG, "env is null");
171     VERIFY_NON_NULL(btDevice, TAG, "btDevice is null");
172
173     jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
174     if (!jni_cid_BTDevice)
175     {
176         OIC_LOG(ERROR, TAG, "jni_cid_BTDevice is null");
177         return false;
178     }
179
180     jmethodID jni_mid_getBondState = (*env)->GetMethodID(env, jni_cid_BTDevice,
181                                                          "getBondState", "()I");
182     if (!jni_mid_getBondState)
183     {
184         OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
185         return false;
186     }
187
188     jint jni_bond_state = (*env)->CallIntMethod(env, btDevice, jni_mid_getBondState);
189     OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bond_state);
190
191     // BOND_BONDED - Constant value : 12 (0x0000000c)
192     jfieldID id_bonded = (*env)->GetStaticFieldID(env, jni_cid_BTDevice,
193                                                   "BOND_BONDED", "I");
194     if (!id_bonded)
195     {
196         OIC_LOG(ERROR, TAG, "id_bonded is null");
197         return false;;
198     }
199
200     jint jni_bonded_const = (*env)->GetStaticIntField(env, jni_cid_BTDevice, id_bonded);
201
202     if (jni_bond_state == jni_bonded_const)
203     {
204         OIC_LOG(INFO, TAG, "it is not bonded to each other");
205         return false;
206     }
207     OIC_LOG(DEBUG, TAG, "OUT - CAManagerIsDeviceBonded");
208     return true;
209 }
210
211 bool CAManagerControlAdapter(JNIEnv *env, bool control_flag)
212 {
213     OIC_LOG(DEBUG, TAG, "IN - CAManagerControlAdapter");
214
215     VERIFY_NON_NULL_RET(env, TAG, "env", false);
216
217     jobject jni_obj_BTAdapter = CAManagerGetAdapterContext(env);
218     if (!jni_obj_BTAdapter)
219     {
220         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
221         return false;
222     }
223
224     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
225     if (!jni_cid_BTAdapter)
226     {
227         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
228         return NULL;
229     }
230
231     OIC_LOG_V(DEBUG, TAG, "change adapter : %d", control_flag);
232     jmethodID jni_mid_control = NULL;
233     if (control_flag)
234     {
235         // enable()
236         jni_mid_control = (*env)->GetMethodID(env, jni_cid_BTAdapter, "enable", "()Z");
237     }
238     else
239     {
240         // disable()
241         jni_mid_control = (*env)->GetMethodID(env, jni_cid_BTAdapter, "disable", "()Z");
242     }
243
244     if (!jni_mid_control)
245     {
246         OIC_LOG(ERROR, TAG, "jni_mid_control is null");
247         return false;
248     }
249
250     OIC_LOG(DEBUG, TAG, "CALL API - Adapter Will be Changed");
251     jboolean jni_res = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_control);
252
253     return jni_res;
254 }
255
256 CAResult_t CAManagerReadRemoteRssi(JNIEnv *env, jobject bluetoothGatt)
257 {
258     OIC_LOG(DEBUG, TAG, "CAManagerReadRemoteRssi");
259
260     VERIFY_NON_NULL(env, TAG, "env is null");
261     VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
262
263     if (!CALEIsEnableBTAdapter(env))
264     {
265         OIC_LOG(INFO, TAG, "BT adapter is not enabled");
266         return CA_ADAPTER_NOT_ENABLED;
267     }
268
269     // get BluetoothGatt class
270     OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
271     jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
272     if (!jni_cid_BluetoothGatt)
273     {
274         OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
275         return CA_STATUS_FAILED;
276     }
277
278     OIC_LOG(DEBUG, TAG, "discovery gatt services method");
279     jmethodID jni_mid_readRemoteRssi = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
280                                                              "readRemoteRssi", "()Z");
281     if (!jni_mid_readRemoteRssi)
282     {
283         OIC_LOG(ERROR, TAG, "jni_mid_readRemoteRssi is null");
284         return CA_STATUS_FAILED;
285     }
286     // call disconnect gatt method
287     OIC_LOG(DEBUG, TAG, "CALL API - request readremoteRssi");
288     jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readRemoteRssi);
289     if (!ret)
290     {
291         OIC_LOG(ERROR, TAG, "readremoteRssi has not been called");
292         return CA_STATUS_FAILED;
293     }
294
295     return CA_STATUS_OK;
296 }
297
298 jobject CAManagerGetSharedPreference(JNIEnv *env, jobject context, jstring prefName)
299 {
300     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
301     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
302     VERIFY_NON_NULL_RET(prefName, TAG, "prefName", NULL);
303
304     jclass jni_cls_context = (*env)->FindClass(env, CLASSPATH_CONTEXT);
305     if (!jni_cls_context)
306     {
307         OIC_LOG(ERROR, TAG, "jni_cls_context is null");
308         return NULL;
309     }
310
311     // getSharedPreferences
312     jmethodID jni_mid_getPref = (*env)->GetMethodID(env, jni_cls_context, "getSharedPreferences",
313                                                     "(Ljava/lang/String;I)"
314                                                     "Landroid/content/SharedPreferences;");
315     if (!jni_mid_getPref)
316     {
317         OIC_LOG(ERROR, TAG, "jni_mid_getPref is null");
318         return NULL;
319     }
320
321     jobject jni_obj_sharedPref = (*env)->CallObjectMethod(env, context,
322                                                           jni_mid_getPref,
323                                                           prefName,
324                                                           0);
325     if (!jni_obj_sharedPref)
326     {
327         OIC_LOG(ERROR, TAG, "jni_obj_sharedPref is null");
328         return NULL;
329     }
330
331     return jni_obj_sharedPref;
332 }
333
334 jobject CAManagerGetPrefEditObject(JNIEnv *env, jobject context)
335 {
336     OIC_LOG(DEBUG, TAG, "IN-CAManagerGetPrefEditObject");
337     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
338     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
339
340     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
341     if (!jni_str_prefName)
342     {
343         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
344         return NULL;
345     }
346
347     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
348     if (!jni_str_prefKey)
349     {
350         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
351         return NULL;
352     }
353
354     // get SharedPreference
355     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
356     if (!jni_obj_pref)
357     {
358         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
359         return NULL;
360     }
361
362     // edit()
363     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
364     if (!jni_cls_pref)
365     {
366         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
367         return NULL;
368     }
369
370     jmethodID jni_mid_prefEdit = (*env)->GetMethodID(env, jni_cls_pref, "edit",
371                                                      "()Landroid/content/SharedPreferences$Editor;");
372     if (!jni_mid_prefEdit)
373     {
374         OIC_LOG(ERROR, TAG, "jni_mid_prefEdit is null");
375         return NULL;
376     }
377
378     jobject jni_obj_prefEdit = (*env)->CallObjectMethod(env, jni_obj_pref, jni_mid_prefEdit);
379     if (!jni_obj_prefEdit)
380     {
381         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
382         return NULL;
383     }
384
385     OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetPrefEditObject");
386     return jni_obj_prefEdit;
387 }
388
389 bool CAManagerUpdatePrefStringSet(JNIEnv *env, jobject context, jobject set)
390 {
391     OIC_LOG(DEBUG, TAG, "IN-CAManagerUpdatePrefStringSet");
392
393     VERIFY_NON_NULL_RET(env, TAG, "env", false);
394     VERIFY_NON_NULL_RET(context, TAG, "context", false);
395     VERIFY_NON_NULL_RET(set, TAG, "set", false);
396
397     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
398     if (!jni_str_prefKey)
399     {
400         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
401         return false;
402     }
403
404     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
405     if (!jni_obj_prefEdit)
406     {
407         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
408         return false;
409     }
410
411     // putString()
412     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
413     if (!jni_cls_edit)
414     {
415         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
416         return false;
417     }
418
419     // get putString method interface
420     jmethodID jni_mid_PrefPutStringSet = (*env)->GetMethodID(env, jni_cls_edit, "putStringSet",
421                                                              "(Ljava/lang/String;Ljava/util/Set;)"
422                                                              "Landroid/content/SharedPreferences"
423                                                              "$Editor;");
424     if (!jni_mid_PrefPutStringSet)
425     {
426         OIC_LOG(ERROR, TAG, "jni_mid_PrefPutStringSet is null");
427         return false;
428     }
429
430     jobject gSetString = (jobject)(*env)->NewGlobalRef(env, set);
431     if (!gSetString)
432     {
433         OIC_LOG(ERROR, TAG, "gAddress is null");
434         return false;
435     }
436
437     OIC_LOG(DEBUG, TAG, "CALL API - request putString for SharedPreferences.Editor");
438     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefPutStringSet,
439                              jni_str_prefKey, gSetString);
440
441     // get commit method interface
442     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
443                                                        "()Z");
444     if (!jni_mid_PrefCommit)
445     {
446         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
447         return false;
448     }
449
450     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
451
452     OIC_LOG(DEBUG, TAG, "OUT-CAManagerUpdatePrefStringSet");
453     return res;
454 }
455
456 jobject CAManagerGetPrefStringSet(JNIEnv *env, jobject context)
457 {
458     OIC_LOG(DEBUG, TAG, "CAManagerGetPrefStringSet");
459
460     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
461     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
462
463     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
464     if (!jni_str_prefName)
465     {
466         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
467         return NULL;
468     }
469
470     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
471     if (!jni_str_prefKey)
472     {
473         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
474         return NULL;
475     }
476
477     // get SharedPreference
478     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
479     if (!jni_obj_pref)
480     {
481         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
482         return NULL;
483     }
484
485     // contains(String key)
486     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
487     if (!jni_cls_pref)
488     {
489         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
490         return NULL;
491     }
492
493     jmethodID jni_mid_getStringSet = (*env)->GetMethodID(env, jni_cls_pref, "getStringSet",
494                                                          "(Ljava/lang/String;Ljava/util/Set;)"
495                                                          "Ljava/util/Set;");
496     if (!jni_mid_getStringSet)
497     {
498         OIC_LOG(ERROR, TAG, "jni_mid_getStringSet is null");
499         return NULL;
500     }
501
502     jobject jni_defSet = CAManagerCreateSetString(env);
503     if (!jni_defSet)
504     {
505         OIC_LOG(ERROR, TAG, "jni_defSet is null");
506         return NULL;
507     }
508
509     jobject value = (*env)->CallObjectMethod(env, jni_obj_pref,
510                                              jni_mid_getStringSet,
511                                              jni_str_prefKey,
512                                              jni_defSet);
513
514     return value;
515 }
516
517 bool CAManagerContainsPrefStringSet(JNIEnv *env, jobject context)
518 {
519     OIC_LOG(DEBUG, TAG, "CAManagerContainsPrefStringSet");
520
521     VERIFY_NON_NULL_RET(env, TAG, "env", false);
522     VERIFY_NON_NULL_RET(context, TAG, "context", false);
523
524     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
525     if (!jni_str_prefName)
526     {
527         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
528         return false;
529     }
530
531     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
532     if (!jni_str_prefKey)
533     {
534         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
535         return false;
536     }
537
538     // get SharedPreference
539     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
540     if (!jni_obj_pref)
541     {
542         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
543         return false;
544     }
545
546     // contains(String key)
547     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
548     if (!jni_cls_pref)
549     {
550         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
551         return false;
552     }
553
554     jmethodID jni_mid_prefContains = (*env)->GetMethodID(env, jni_cls_pref, "contains",
555                                                          METHODID_BOOLEAN_RET_STRING_PARAM);
556     if (!jni_mid_prefContains)
557     {
558         OIC_LOG(ERROR, TAG, "jni_mid_prefContains is null");
559         return false;
560     }
561
562     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_pref,
563                                              jni_mid_prefContains,
564                                              jni_str_prefKey);
565
566     OIC_LOG_V(DEBUG, TAG, "Preference - contains (%d)", res);
567     return res;
568 }
569
570 bool CAManagerRemovePrefStringSet(JNIEnv *env, jobject context)
571 {
572     OIC_LOG(DEBUG, TAG, "CAManagerRemovePrefStringSet");
573
574     VERIFY_NON_NULL_RET(env, TAG, "env", false);
575     VERIFY_NON_NULL_RET(context, TAG, "context", false);
576
577     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
578     if (!jni_str_prefKey)
579     {
580         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
581         return false;
582     }
583
584     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
585     if (!jni_obj_prefEdit)
586     {
587         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
588         return false;
589     }
590
591     // putString()
592     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
593     if (!jni_cls_edit)
594     {
595         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
596         return false;
597     }
598
599     // get remove method interface
600     jmethodID jni_mid_PrefRemove = (*env)->GetMethodID(env, jni_cls_edit, "remove",
601                                                        "(Ljava/lang/String;)"
602                                                        "Landroid/content/SharedPreferences"
603                                                        "$Editor;");
604     if (!jni_mid_PrefRemove)
605     {
606         OIC_LOG(ERROR, TAG, "jni_mid_PrefRemove is null");
607         return false;
608     }
609
610     OIC_LOG(DEBUG, TAG, "CALL API - request remove for SharedPreferences.Editor");
611     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefRemove, jni_str_prefKey);
612
613     // get commit method interface
614     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
615                                                        "()Z");
616     if (!jni_mid_PrefCommit)
617     {
618         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
619         return false;
620     }
621
622     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
623
624     OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddConnectedDeviceAddress");
625     return res;
626 }
627
628 bool CAManagerAddConnectedDeviceAddress(JNIEnv *env, jobject context,
629                                         jstring address, jobject set)
630 {
631     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddConnectedDeviceAddress");
632
633     VERIFY_NON_NULL_RET(env, TAG, "env", false);
634     VERIFY_NON_NULL_RET(context, TAG, "context", false);
635     VERIFY_NON_NULL_RET(address, TAG, "address", false);
636     VERIFY_NON_NULL_RET(set, TAG, "set", false);
637
638     if (CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
639     {
640         OIC_LOG(DEBUG, TAG, "it's already done");
641         return true;
642     }
643
644     if (!CAManagerCallFuncSetString(env, address, set, CM_ADD))
645     {
646         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
647         return false;
648     }
649
650     return CAManagerUpdatePrefStringSet(env, context, set);
651 }
652
653 bool CAManagerIsConnectedDeviceAddress(JNIEnv *env, jobject context,
654                                        jstring address, jobject set)
655 {
656     OIC_LOG(DEBUG, TAG, "CAManagerIsConnectedDeviceAddress");
657     VERIFY_NON_NULL_RET(env, TAG, "env", false);
658     VERIFY_NON_NULL_RET(context, TAG, "context", false);
659
660     return CAManagerCallFuncSetString(env, address, set, CM_CONTAINS);
661 }
662
663 jobject CAManagerGetConnectedDeviceAddress(JNIEnv *env, jobject context)
664 {
665     OIC_LOG(DEBUG, TAG, "CAManagerGetConnectedDeviceAddress");
666
667     if (!CAManagerContainsPrefStringSet(env, context))
668     {
669         OIC_LOG(DEBUG, TAG, "there is no set data");
670         return NULL;
671     }
672
673     return CAManagerGetPrefStringSet(env, context);
674 }
675
676 bool CAManagerRemoveConnectedDeviceAddress(JNIEnv *env, jobject context,
677                                            jstring address, jobject set)
678 {
679     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveConnectedDeviceAddress");
680
681     VERIFY_NON_NULL_RET(env, TAG, "env", false);
682     VERIFY_NON_NULL_RET(context, TAG, "context", false);
683     VERIFY_NON_NULL_RET(address, TAG, "address", false);
684     VERIFY_NON_NULL_RET(set, TAG, "set", false);
685
686     if (!CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
687     {
688         OIC_LOG(DEBUG, TAG, "it's already done");
689         return true;
690     }
691
692     if (!CAManagerCallFuncSetString(env, address, set, CM_REMOVE))
693     {
694         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
695         return false;
696     }
697
698     return CAManagerUpdatePrefStringSet(env, context, set);
699 }
700
701 jboolean CAManagerCheckBTAddress(JNIEnv *env, jstring address)
702 {
703     OIC_LOG(DEBUG, TAG, "CAManagerCheckBTAddress");
704
705     VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
706     VERIFY_NON_NULL_RET(address, TAG, "address is null", JNI_FALSE);
707
708     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
709     if (!jni_cid_BTAdapter)
710     {
711         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
712         return JNI_FALSE;
713     }
714
715     // get remote bt adapter method
716     jmethodID jni_mid_checkBTAddress =
717             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
718                                       "checkBluetoothAddress",
719                                       METHODID_BOOLEAN_RET_STRING_PARAM);
720     if (!jni_mid_checkBTAddress)
721     {
722         OIC_LOG(ERROR, TAG, "jni_mid_checkBTAddress is null");
723         return JNI_FALSE;
724     }
725
726     jboolean jni_obj_isAddress = (*env)->CallStaticBooleanMethod(env, jni_cid_BTAdapter,
727                                                                  jni_mid_checkBTAddress,
728                                                                  address);
729     return jni_obj_isAddress;
730 }
731
732 jobject CAManagerCreateSetString(JNIEnv *env)
733 {
734     OIC_LOG(DEBUG, TAG, "CAManagerCallFuncSetString");
735
736     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
737
738     jclass jclazzMap = (*env)->FindClass(env, "java/util/HashSet" );
739     if (!jclazzMap)
740     {
741         OIC_LOG(ERROR, TAG, "jclazzMap is null");
742         return NULL;
743     }
744
745     jmethodID jinitMap = (*env)->GetMethodID(env, jclazzMap, "<init>", "()V");
746     if (!jinitMap)
747     {
748         OIC_LOG(ERROR, TAG, "jinitMap is null");
749         return NULL;
750     }
751
752     jobject jpropertyMap = (*env)->NewObject(env, jclazzMap, jinitMap);
753     if (!jpropertyMap)
754     {
755         OIC_LOG(ERROR, TAG, "jpropertyMap is null");
756         return NULL;
757     }
758
759     return jpropertyMap;
760 }
761
762 bool CAManagerCallFuncSetString(JNIEnv *env, jstring address, jobject set,
763                                 CASetMethod_t method_type)
764 {
765     OIC_LOG(DEBUG, TAG, "CAManagerCallFuncSetString");
766     VERIFY_NON_NULL_RET(env, TAG, "env", false);
767     VERIFY_NON_NULL_RET(address, TAG, "address", false);
768     VERIFY_NON_NULL_RET(set, TAG, "set", false);
769
770     jclass jni_cls_set = (*env)->FindClass(env, "java/util/HashSet");
771     if (!jni_cls_set)
772     {
773         OIC_LOG(ERROR, TAG, "jni_cls_set is null");
774         return false;
775     }
776
777     jmethodID jni_mid_setMethod = NULL;
778     switch (method_type)
779     {
780         case CM_CONTAINS:
781             OIC_LOG(DEBUG, TAG, "java/util/HashSet.contains");
782             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "contains",
783                                                     "(Ljava/lang/Object;)Z");
784             break;
785         case CM_ADD:
786             OIC_LOG(DEBUG, TAG, "java/util/HashSet.add");
787             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "add",
788                                                     "(Ljava/lang/Object;)Z");
789             break;
790         case CM_REMOVE:
791             OIC_LOG(DEBUG, TAG, "java/util/HashSet.remove");
792             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "remove",
793                                                     "(Ljava/lang/Object;)Z");
794             break;
795         default:
796             break;
797     }
798     if (!jni_mid_setMethod)
799     {
800         OIC_LOG(ERROR, TAG, "jni_mid_setMethod is null");
801         return false;
802     }
803
804     jboolean res = (*env)->CallBooleanMethod(env, set, jni_mid_setMethod, address);
805     OIC_LOG_V(DEBUG, TAG, "method return result : %d", res);
806     return res;
807 }