From: ch79.cho Date: Mon, 23 May 2016 04:51:05 +0000 (+0900) Subject: Add callback interface and open source license term. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=09a886d63d82298d059873fd3e22bc928b8dbfc3;p=contrib%2Fiotivity.git Add callback interface and open source license term. The files for JNI interface are modified to offer callback function and add open source license term. Change-Id: I3f9453b37f274b9098117550e530a388b318ac02 Signed-off-by: ch79.cho Reviewed-on: https://gerrit.iotivity.org/gerrit/8269 Reviewed-by: Uze Choi Tested-by: Uze Choi --- diff --git a/service/notification/android/notification-service/src/main/java/org/iotivity/service/notification/IoTNotification.java b/service/notification/android/notification-service/src/main/java/org/iotivity/service/notification/IoTNotification.java index ca22906..3bea94e 100644 --- a/service/notification/android/notification-service/src/main/java/org/iotivity/service/notification/IoTNotification.java +++ b/service/notification/android/notification-service/src/main/java/org/iotivity/service/notification/IoTNotification.java @@ -1,36 +1,35 @@ -package org.iotivity.service.notification; - -import android.util.Log; -import org.iotivity.service.notification.NSMessage; -import org.iotivity.service.notification.NSConsumer; -import org.iotivity.service.notification.NSSync; - -public class IoTNotification { - public IoTNotification(){} - - static { - System.loadLibrary("notification_provider_jni"); - } - - public void onLogPrinted(String log) - { - Log.i("JNI_NS_CALLBACK", "[C->J] Log: " + log); - } - - public void onSubscribeRequest(String consumer) - { - Log.i("JNI_NS_CALLBACK", "[C->J] Consumer IP: " + consumer); - } - - public void onMessageSync(String sync) - { - Log.i("JNI_NS_CALLBACK", "[C->J] Sync Status" + sync); - } - - public native int NSStartProvider(boolean access); - public native int NSStopProvider(); - public native int NSSendNotification(NSMessage message); - public native int NSProviderReadCheck(NSMessage message); - public native int NSAccept(NSConsumer consumer, boolean accepted); -} - +package org.iotivity.service.notification; + +import android.util.Log; +import org.iotivity.service.notification.NSMessage; +import org.iotivity.service.notification.NSConsumer; +import org.iotivity.service.notification.NSSync; + +public class IoTNotification +{ + public IoTNotification() + { + } + + static + { + System.loadLibrary("notification_provider_jni"); + } + + public native int NSStartProvider(boolean access, + NSSubscriptionListner subscriptionListener, + NSSynchListner syncListener); + public native int NSStopProvider(); + public native int NSSendNotification(NSMessage message); + public native int NSProviderReadCheck(NSMessage message); + public native int NSAccept(NSConsumer consumer, boolean accepted); + + public interface NSSubscriptionListner { + public void OnNSSubscribedEvent(String consumerId); + } + + public interface NSSynchListner { + public void OnNSSynchronizedEvent(String messageId, int syncState); + } +} + diff --git a/service/notification/android/notification-service/src/main/jni/notificationProvider.c b/service/notification/android/notification-service/src/main/jni/notificationProvider.c index 94d598f..588a84c 100644 --- a/service/notification/android/notification-service/src/main/jni/notificationProvider.c +++ b/service/notification/android/notification-service/src/main/jni/notificationProvider.c @@ -1,6 +1,22 @@ +//****************************************************************** // -// Created by jaesick.shin on 2016-04-26. +// Copyright 2016 Samsung Electronics All Rights Reserved. // +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include #include @@ -11,7 +27,8 @@ #define LOGE(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) static JavaVM *g_jvm = NULL; -static jobject g_obj = NULL; +static jobject g_obj_subscriptionListener = NULL; +static jobject g_obj_syncListener = NULL; JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved) { @@ -22,119 +39,66 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved) } JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStartProvider( - JNIEnv * env, jobject jObj, jboolean jAccess) + JNIEnv * env, jobject jObj, jboolean jAccess, jobject jSubscriptionListener, + jobject jSyncListener) { - LOGI("JNI TEST - NSStartProvider..."); + LOGI("NSStartProvider..."); - g_obj = jObj; + if (!jSubscriptionListener || !jSyncListener) + { + LOGI("Fail to set listeners"); + //return (jint) NS_ERROR; + } - //JNIlogPrintedCallback(env, jObj, "NSStartProvider"); + g_obj_subscriptionListener = (jobject) (*env)->NewGlobalRef(env, jSubscriptionListener); + g_obj_syncListener = (jobject) (*env)->NewGlobalRef(env, jSyncListener); + // check access policy NSAccessPolicy access = NS_ACCEPTER_PROVIDER; - if(NSStartProvider(access, NSSubscribeRequestCb, NSSyncCb) == 0) - { - LOGI("JNI TEST - Success to start NSProvider service"); - } - else + + if (NSStartProvider(access, NSSubscribeRequestCb, NSSyncCb) != NS_OK) { - LOGE("JNI TEST - Fail to start NSProvider service"); + LOGE("Fail to start NSProvider service"); + return (jint) NS_ERROR; } - //NSStartProvider(access, NULL, NULL); - return 0; + return (jint) NS_OK; } JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStopProvider( JNIEnv * env, jobject jObj) { - LOGI("JNI TEST - NSStopProvider"); - return NSStopProvider(); + LOGI("NSStopProvider"); - return 0; + (*env)->DeleteGlobalRef(env, g_obj_subscriptionListener); + (*env)->DeleteGlobalRef(env, g_obj_syncListener); + + if (NSStopProvider() != NS_OK) + { + LOGE("Fail to stop NSProvider service"); + return (jint) NS_ERROR; + } + + return (jint) NS_OK; } JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSSendNotification( JNIEnv * env, jobject jObj, jobject jMsg) { - LOGI("JNI TEST - NSSendNotification"); - -// bool isAttached = false; -// //JNIEnv* env; -// jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); -// if (JNI_OK != res) -// { -// LOGI("AttachCurrentThread for JNIEnv pointer"); -// res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); -// -// if (JNI_OK != res) -// { -// LOGE("AttachCurrentThread has failed"); -// return -1; -// } -// isAttached = true; -// } - - jclass cls = (*env)->GetObjectClass(env, jMsg); - - // Message ID - jfieldID fid_id = (*env)->GetFieldID(env, cls, "id", "Ljava/lang/String;"); - if (fid_id == NULL) - { - LOGI("JNI TEST - Error: jfieldID for message id is null"); - return 0; - } - jstring jmsgId = (*env)->GetObjectField(env, jMsg, fid_id); - const char * messageId = (*env)->GetStringUTFChars(env, jmsgId, NULL); - if (messageId == NULL) - { - printf("JNI TEST - Error: messageId is null\n"); - return 0; - } - LOGI("JNI TEST - Message ID: %s\n", messageId); - JNIlogPrintedCallback(env, jObj, messageId); - - (*env)->ReleaseStringUTFChars(env, jmsgId, messageId); + LOGI("NSSendNotification"); - // Message Title - jfieldID fid_title = (*env)->GetFieldID(env, cls, "title", "Ljava/lang/String;"); - if (fid_title == NULL) + if (!jMsg) { - LOGE("JNI TEST - Error: jfieldID for message id is null"); - return 0; + LOGI("Fail to send notification - Message is null"); + return (jint) NS_ERROR; } - jstring jmsgTitle = (*env)->GetObjectField(env, jMsg, fid_title); - const char * messageTitle = (*env)->GetStringUTFChars(env, jmsgTitle, NULL); - if (messageTitle == NULL) - { - printf("JNI TEST - Error: messageTitle is null\n"); - return 0; - } - LOGI("JNI TEST - Message Title: %s\n", messageTitle); - (*env)->ReleaseStringUTFChars(env, jmsgTitle, messageTitle); - // Message Body - jfieldID fid_body = (*env)->GetFieldID(env, cls, "body", "Ljava/lang/String;"); - if (fid_body == NULL) - { - LOGE("JNI TEST - Error: jfieldID for message id is null"); - return 0; - } - jstring jmsgBody = (*env)->GetObjectField(env, jMsg, fid_body); - const char * messageBody = (*env)->GetStringUTFChars(env, jmsgBody, NULL); - if (messageBody == NULL) - { - printf("JNI TEST - Error: messageBody is null\n"); - return 0; - } - LOGI("JNI TEST - Message Body: %s\n", messageBody); - (*env)->ReleaseStringUTFChars(env, jmsgBody, messageBody); + NSMessage * nsMsg = NSGetMessage(env, jMsg); -// if (isAttached) -// { -// (*g_jvm)->DetachCurrentThread(g_jvm); -// } + LOGI("JNI TEST - NSSendNotification"); + NSSendNotification(nsMsg); - return 0; + return (jint) NS_OK; } JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSProviderReadCheck( @@ -161,149 +125,169 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NS return 0; } -void JNIlogPrintedCallback(JNIEnv * env, jobject jObj, const char * log) +void NSSubscribeRequestCb(NSConsumer *consumer) { - static jmethodID cb = NULL; - //jclass cls = (*env)->FindClass(env, "com/sec/notiproviderexample/ProviderExample"); - jclass cls = (*env)->GetObjectClass(env, jObj); - if (cls == NULL) - { - LOGE("JNI TEST - Error: cannot find callback class"); - } + LOGI("Subscription requested by consumer"); - if (cb == NULL) + JNIEnv * env; + jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); + if (JNI_OK != res) { - cb = (*env)->GetMethodID(env, cls, "onLogPrinted", "(Ljava/lang/String;)V"); - if (cb == NULL) + if (res == JNI_EDETACHED) { - LOGE("JNI TEST - Error: cannot get callback method"); + if ((*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL) != JNI_OK) + { + LOGE("Failed to get the environment"); + return; + } + else + { + LOGE("Success to get the environment"); + } + } + else + { + LOGE("Failed to get the environment using GetEnv()"); return; } - LOGI("JNI TEST - Get callback method - onLogPrinted"); - - jstring msg = (*env)->NewStringUTF(env, log); - LOGI("print: %s", log); - - (*env)->CallVoidMethod(env, jObj, cb, msg); } -} -void JNISubscribeRequestCallback(JNIEnv * env, jobject jObj, char * consumer) -{ - static jmethodID cb = NULL; - //jclass cls = (*env)->FindClass(env, "com/sec/notiproviderexample/ProviderExample"); - jclass cls = (*env)->GetObjectClass(env, jObj); - if (cls == NULL) + jclass cls = (*env)->GetObjectClass(env, g_obj_subscriptionListener); + if (!cls) { - LOGE("JNI TEST - Error: cannot find callback class"); + LOGE("Failed to Get ObjectClass"); + return; } - - if (cb == NULL) + jmethodID mid = (*env)->GetMethodID(env, cls, "OnNSSubscribedEvent", "(Ljava/lang/String;)V"); + if (!mid) { - cb = (*env)->GetMethodID(env, cls, "onSubscribeRequest", "(Ljava/lang/String;)V"); - if (cb == NULL) - { - LOGE("JNI TEST - Error: cannot get callback method"); - return; - } - LOGI("JNI TEST - Get callback method - onSubscribeRequest"); - - jstring msg = (*env)->NewStringUTF(env, consumer); - LOGI("print: %s", consumer); - - (*env)->CallVoidMethod(env, jObj, cb, msg); + LOGE("Failed to Get MethodID"); + return; } -} -void JNIMessageSyncCallback(JNIEnv * env, jobject jObj, char * message) -{ - static jmethodID cb = NULL; - //jclass cls = (*env)->FindClass(env, "com/sec/notiproviderexample/ProviderExample"); - jclass cls = (*env)->GetObjectClass(env, jObj); - if (cls == NULL) - { - LOGE("JNI TEST - Error: cannot find callback class"); - } + //(*env)->CallVoidMethod(env, g_obj_subscriptionListener, mid); - if (cb == NULL) - { - cb = (*env)->GetMethodID(env, cls, "onMessageSync", "(Ljava/lang/String;)V"); - if (cb == NULL) - { - LOGE("JNI TEST - Error: cannot get callback method"); - return; - } - LOGI("JNI TEST - Get callback method - onMessageSync"); + (*g_jvm)->DetachCurrentThread(g_jvm); - jstring msg = (*env)->NewStringUTF(env, message); - LOGI("print: %s", message); + NSAccept(consumer, true); - (*env)->CallVoidMethod(env, jObj, cb, msg); - } + return; } -void NSSubscribeRequestCb(NSConsumer *consumer) +void NSSyncCb(NSProvider *provider, NSSync *sync) { - LOGI("JNI TEST - consumer requested to subscribe"); - - char *cid = consumer->mId; - LOGI("JNI TEST - NS_ Consumer ID: %s\n", cid); + LOGI("Sync requested"); - bool isAttached = false; - JNIEnv* env; + JNIEnv * env; jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); if (JNI_OK != res) { - LOGI("AttachCurrentThread for JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) + if (JNI_EDETACHED) + { + if ((*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL) < 0) + { + LOGE("Failed to get the environment"); + return; + } + else + { + LOGE("Success to get the environment"); + } + } + else { - LOGE("AttachCurrentThread has failed"); + LOGE("Failed to get the environment using GetEnv()"); return; } - isAttached = true; } - // callback to app - JNISubscribeRequestCallback(env, g_obj, cid); + LOGI("Sync ID : %s\n", sync->mMessageId); + LOGI("Sync STATE : %d\n", sync->mState); + + jstring strMessageId = (*env)->NewStringUTF(env, sync->mMessageId); - if (isAttached) + jclass cls = (*env)->GetObjectClass(env, g_obj_syncListener); + if (!cls) { - (*g_jvm)->DetachCurrentThread(g_jvm); + LOGE("Failed to Get ObjectClass"); + return; + } + jmethodID mid = (*env)->GetMethodID(env, cls, "OnNSSynchronizedEvent", + "(Ljava/lang/String;I)V"); + if (!mid) + { + LOGE("Failed to Get MethodID"); + return; } - NSAccept(consumer, true); + //(*env)->CallVoidMethod(env, g_obj_syncListener, mid, strMessageId, (jint) sync->mState); + + (*g_jvm)->DetachCurrentThread(g_jvm); + + return; + } -void NSSyncCb(NSProvider *provider, NSSync *sync) +NSMessage * NSGetMessage(JNIEnv * env, jobject jMsg) { - LOGI("JNI TEST - sync requested"); - LOGI("JNI TEST - NS_ Sync MessageID: %s\n", sync->mMessageId); - LOGI("JNI TEST - NS_ Sync State: %d\n", sync->mState); + LOGI("NSGetMessage"); - bool isAttached = false; - JNIEnv* env; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - LOGI("AttachCurrentThread for JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); + jclass cls = (*env)->GetObjectClass(env, jMsg); - if (JNI_OK != res) - { - LOGE("AttachCurrentThread has failed"); - return; - } - isAttached = true; + // Message ID + jfieldID fid_id = (*env)->GetFieldID(env, cls, "id", "Ljava/lang/String;"); + if (fid_id == NULL) + { + LOGI("Error: jfieldID for message id is null"); + return (jint) NS_ERROR; + } + jstring jmsgId = (*env)->GetObjectField(env, jMsg, fid_id); + const char * messageId = (*env)->GetStringUTFChars(env, jmsgId, NULL); + if (messageId == NULL) + { + printf("Error: messageId is null\n"); + return (jint) NS_ERROR; } + LOGI("Message ID: %s\n", messageId); - // callback to app - JNIMessageSyncCallback(env, g_obj, sync->mMessageId); + // Message Title + jfieldID fid_title = (*env)->GetFieldID(env, cls, "title", "Ljava/lang/String;"); + if (fid_title == NULL) + { + LOGE("Error: jfieldID for message id is null"); + return (jint) NS_ERROR; + } + jstring jmsgTitle = (*env)->GetObjectField(env, jMsg, fid_title); + const char * messageTitle = (*env)->GetStringUTFChars(env, jmsgTitle, NULL); + if (messageTitle == NULL) + { + printf("Error: messageTitle is null\n"); + return (jint) NS_ERROR; + } + LOGI("Message Title: %s\n", messageTitle); - if (isAttached) + // Message Body + jfieldID fid_body = (*env)->GetFieldID(env, cls, "body", "Ljava/lang/String;"); + if (fid_body == NULL) { - (*g_jvm)->DetachCurrentThread(g_jvm); + LOGE("Error: jfieldID for message id is null"); + return (jint) NS_ERROR; } + jstring jmsgBody = (*env)->GetObjectField(env, jMsg, fid_body); + const char * messageBody = (*env)->GetStringUTFChars(env, jmsgBody, NULL); + if (messageBody == NULL) + { + printf("Error: messageBody is null\n"); + return (jint) NS_ERROR; + } + LOGI("Message Body: %s\n", messageBody); + + NSMessage * nsMsg = (NSMessage *) malloc(sizeof(NSMessage)); + + nsMsg->mId = strdup(messageId); + nsMsg->mTitle = strdup(messageTitle); + nsMsg->mContentText = strdup(messageBody); + + return nsMsg; } diff --git a/service/notification/android/notification-service/src/main/jni/notificationProvider.h b/service/notification/android/notification-service/src/main/jni/notificationProvider.h index 406bd5f..b462651 100644 --- a/service/notification/android/notification-service/src/main/jni/notificationProvider.h +++ b/service/notification/android/notification-service/src/main/jni/notificationProvider.h @@ -1,45 +1,54 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ +//****************************************************************** +// +// Copyright 2016 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + #include #include "NSProviderInterface.h" #include "NSCommon.h" -/* Header for class com_sec_notiproviderjni_IoTNotification */ #ifndef NOTIFICATION_JNI_H #define NOTIFICATION_JNI_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/* - * Class: com_sec_notiproviderjni_IoTNotification - * Method: stringFromJNI - * Signature: ()Ljava/lang/String; - */ - JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStartProvider - (JNIEnv *, jobject, jboolean); - - JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStopProvider - (JNIEnv *, jobject); - - JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSSendNotification - (JNIEnv *, jobject, jobject); - - JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSProviderReadCheck - (JNIEnv *, jobject, jobject); + JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStartProvider( + JNIEnv *, jobject, jboolean, jobject, jobject); - JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSAccept - (JNIEnv *, jobject, jobject, jboolean); + JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSStopProvider( + JNIEnv *, jobject); - void JNIlogPrintedCallback(JNIEnv*, jobject, const char*); + JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSSendNotification( + JNIEnv *, jobject, jobject); - void JNISubscribeRequestCallback(JNIEnv*, jobject, char*); + JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSProviderReadCheck( + JNIEnv *, jobject, jobject); - void JNIMessageSyncCallback(JNIEnv*, jobject, char*); + JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSAccept(JNIEnv *, + jobject, jobject, jboolean); - void NSSubscribeRequestCb(NSConsumer*); + void NSSubscribeRequestCb(NSConsumer*); - void NSSyncCb(NSProvider*, NSSync*); + void NSSyncCb(NSProvider*, NSSync*); + NSMessage * NSGetMessage(JNIEnv *, jobject); #ifdef __cplusplus }