Added CreateMessage API in Android APIs
authornikhil.a <nikhil.a7@samsung.com>
Mon, 29 Aug 2016 12:49:21 +0000 (18:19 +0530)
committerUze Choi <uzchoi@samsung.com>
Fri, 2 Sep 2016 07:11:43 +0000 (07:11 +0000)
1) CreateMessage API added for ProviderService
2) Addition of JNI code for the respective API
3)Refactoring of code

Change-Id: I53c44c09580028b4ec0631b47f31c5b8abb47535
Signed-off-by: nikhil.a <nikhil.a7@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11053
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Abitha Shankar <abitha.s@samsung.com>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
(cherry picked from commit dfb5ff844c058fdbbafb530326e1935cae17673e)
Reviewed-on: https://gerrit.iotivity.org/gerrit/11277
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/android/notification-service/src/main/java/org/iotivity/service/ns/consumer/ConsumerService.java
service/notification/android/notification-service/src/main/java/org/iotivity/service/ns/provider/ProviderService.java
service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp
service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp
service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.h

index d48e7e18f23f32ccb34b07d466eca99a658c57f6..88a7e0173062222d818db0000a6dc42d9ab04e62 100755 (executable)
@@ -74,11 +74,6 @@ public class ConsumerService
         return instance;
     }
 
-    public ConsumerService()
-    {
-        Log.i (LOG_TAG, "ConsumerService()");
-    }
-
     public void Start(
         OnProviderDiscoveredListner onProviderDiscoveredListner,
         OnProviderChangedListener onProviderChangedListener
index 9b695ad76d9841ce67f641f81fa29180690aa3be..eb8f30a1ebae9b9fb5b3112d8d40bb376347d410 100644 (file)
@@ -84,6 +84,11 @@ public class ProviderService
         return;
     }
 
+    public Message CreateMessage () throws NSException
+    {
+        return nativeCreateMessage();
+    }
+
     public int   EnableRemoteService(String servAdd) throws NSException
     {
         return nativeEnableRemoteService(servAdd);
@@ -113,6 +118,7 @@ public class ProviderService
     public native int  nativeStop();
     public native int  nativeSendMessage(Message message);
     public native void  nativeSendSyncInfo( long messageId , int type);
+    public native Message nativeCreateMessage();
     public native int  nativeEnableRemoteService(String servAdd);
     public native int  nativeDisableRemoteService(String servAdd);
     public native int  nativeAddTopic(String topicName);
index 88ed75616f3e96a71d2beb4bb69bcc349a2ead9f..b7396adf2b8bf94396fa758d8b70da2482481fb3 100755 (executable)
@@ -448,27 +448,27 @@ jobject getJavaMessageType(JNIEnv *env, OIC::Service::NSMessage::NSMessageType t
     {\r
         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_ALERT:\r
             {\r
-                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message,\r
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
                                           "ALERT", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
-                return env->GetStaticObjectField(g_cls_Message, fieldID);\r
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
             }\r
         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_NOTICE:\r
             {\r
-                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message,\r
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
                                           "NOTICE", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
-                return env->GetStaticObjectField(g_cls_Message, fieldID);\r
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
             }\r
         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_EVENT:\r
             {\r
-                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message,\r
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
                                           "EVENT", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
-                return env->GetStaticObjectField(g_cls_Message, fieldID);\r
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
             }\r
         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO:\r
             {\r
-                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message,\r
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
                                           "INFO", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
-                return env->GetStaticObjectField(g_cls_Message, fieldID);\r
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
             }\r
         default:\r
             return NULL;\r
index f8ee78958b381241c19761d10037fd970bdd5037..7b1f9b3d09e3eeeba9bc470cf253a339546c1f73 100644 (file)
@@ -286,6 +286,181 @@ OIC::Service::NSMessage *getNativeMessage(JNIEnv *env, jobject jMsg)
 
 }
 
+jobject getJavaMessageType(JNIEnv *env, OIC::Service::NSMessage::NSMessageType type)
+{
+    LOGD ("JNIProviderService: getJavaMessageType - IN");
+    switch (type)
+    {
+        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_ALERT:
+            {
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
+                                          "ALERT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);
+            }
+        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_NOTICE:
+            {
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
+                                          "NOTICE", "Lorg/iotivity/service/ns/common/Message$MessageType;");
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);
+            }
+        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_EVENT:
+            {
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
+                                          "EVENT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);
+            }
+        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO:
+            {
+                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
+                                          "INFO", "Lorg/iotivity/service/ns/common/Message$MessageType;");
+                return env->GetStaticObjectField(g_cls_Message_Type, fieldID);
+            }
+        default:
+            return NULL;
+    }
+    LOGD ("JNIProviderService: getJavaMessageType - OUT");
+    return NULL;
+}
+
+jobject getJavaMessage(JNIEnv *env, OIC::Service::NSMessage *message)
+{
+    LOGD ("JNIProviderService: getJavaMessage - IN");
+
+    LOGD ("id : %llu\n", message->getMessageId());
+    LOGD ("title : %s\n", message->getTitle().c_str());
+    LOGD ("content : %s\n", message->getContentText().c_str());
+    LOGD ("source : %s\n", message->getSourceName().c_str());
+
+    jlong jMessageId = (jlong) message->getMessageId();
+    jstring jProviderId = env->NewStringUTF(message->getProviderId().c_str());
+    jstring jTitle = env->NewStringUTF(message->getTitle().c_str());
+    jstring jContentText = env->NewStringUTF(message->getContentText().c_str());
+    jstring jSourceName = env->NewStringUTF(message->getSourceName().c_str());
+    jstring jTopic = env->NewStringUTF(message->getTopic().c_str());
+
+    jstring jTime = env->NewStringUTF(message->getTime().c_str());
+    jlong jTTL = (jlong) message->getTTL();
+
+    jlong pMessage = (long) message;
+
+    jclass cls_message = (jclass) (env->NewLocalRef(g_cls_Message));
+    if (!cls_message)
+    {
+        LOGE ("Failed to Get ObjectClass for Message");
+        return NULL ;
+    }
+    jmethodID mid_message = env->GetMethodID(
+                                cls_message, "<init>",
+                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
+    if (!mid_message)
+    {
+        LOGE ("Failed to Get MethodID for Message<init>");
+        return NULL;
+    }
+    jobject obj_message = env->NewObject(cls_message, mid_message,
+                                         jTitle, jContentText, jSourceName);
+    if (!obj_message)
+    {
+        LOGE ("Failed to Get Java Object for Message");
+        return NULL;
+    }
+    jfieldID fid_nativeHandle = env->GetFieldID(cls_message, "mNativeHandle", "J");
+    if (!fid_nativeHandle)
+    {
+        LOGE("Failed to get nativeHandle for Message");
+        return NULL;
+    }
+    env->SetLongField(obj_message, fid_nativeHandle, pMessage);
+
+    jfieldID fid_messageId = env->GetFieldID(cls_message, "mMessageId", "J");
+    if (!fid_messageId)
+    {
+        LOGE("Failed to get field MessageID for Message");
+        return NULL;
+    }
+    env->SetLongField(obj_message, fid_messageId, jMessageId);
+
+    jfieldID fid_providerId = env->GetFieldID(cls_message, "mProviderId", "Ljava/lang/String;");
+    if (!fid_providerId)
+    {
+        LOGE("Failed to get field ProviderID for Message");
+        return NULL;
+    }
+    env->SetObjectField(obj_message, fid_providerId, jProviderId);
+
+    jfieldID fid_time = env->GetFieldID(cls_message, "mTime", "Ljava/lang/String;");
+    if (!fid_time)
+    {
+        LOGE("Failed to get field Time for Message");
+        return NULL;
+    }
+    env->SetObjectField(obj_message, fid_time, jTime);
+
+    jfieldID fid_ttl = env->GetFieldID(cls_message, "mTTL", "J");
+    if (!fid_ttl)
+    {
+        LOGE("Failed to get field TTL for Message");
+        return NULL;
+    }
+    env->SetLongField(obj_message, fid_ttl, jTTL);
+
+    jfieldID fid_topic = env->GetFieldID(cls_message, "mTopic", "Ljava/lang/String;");
+    if (!fid_topic)
+    {
+        LOGE("Failed to get mTopic for Message");
+        return NULL;
+    }
+    env->SetObjectField(obj_message, fid_topic, jTopic);
+
+    OIC::Service::NSMediaContents *mediaCont = message->getMediaContents();
+    if (mediaCont != nullptr)
+    {
+        jstring jIconImage = env->NewStringUTF(mediaCont->getIconImage().c_str());
+        jclass cls_mediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));
+        if (!cls_mediaContents)
+        {
+            LOGE ("Failed to Get ObjectClass for MediaContents");
+            return NULL;
+        }
+        jmethodID mid_mediaContents = env->GetMethodID(
+                                          cls_mediaContents, "<init>", "(Ljava/lang/String;)V");
+        if (!mid_mediaContents)
+        {
+            LOGE ("Failed to Get MethodID for MediaContents<init>");
+            return NULL;
+        }
+        jobject obj_mediaContents = env->NewObject(cls_mediaContents, mid_mediaContents,
+                                    jIconImage);
+
+        jfieldID fid_mediaContents = env->GetFieldID(cls_message, "mMediaContents",
+                                     "Lorg/iotivity/service/ns/common/MediaContents;");
+        if (!fid_mediaContents)
+        {
+            LOGE("Failed to get field mediaContents for Message");
+            return NULL;
+        }
+        env->SetObjectField(obj_message, fid_mediaContents, obj_mediaContents);
+
+    }
+
+    jobject jType = getJavaMessageType(env, message->getType());
+    if (jType)
+    {
+        jfieldID fid_type = env->GetFieldID(cls_message, "mType",
+                                            "Lorg/iotivity/service/ns/common/Message$MessageType;");
+        if (!fid_type)
+        {
+            LOGE("Failed to get field Type for Message");
+            return NULL;
+        }
+        env->SetObjectField(obj_message, fid_type, jType);
+    }
+
+    env->DeleteLocalRef(cls_message);
+    LOGD ("JNIProviderService: getJavaMessage - OUT");
+    return obj_message;
+}
+
 jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsState)
 {
     LOGD("JNIProviderService: getJavaTopicState - IN");
@@ -559,9 +734,8 @@ void onSyncInfoListenerCb(OIC::Service::NSSyncInfo *sync)
 
 }
 
-JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStart(
-    JNIEnv *env, jobject jObj, jboolean jPolicy, jobject jSubscriptionListener,
-    jobject jSyncListener)
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStart
+(JNIEnv *env, jobject jObj, jboolean jPolicy, jobject jSubscriptionListener, jobject jSyncListener)
 {
     LOGD("JNIProviderService: nativeStart - IN");
     if (!jSubscriptionListener || !jSyncListener)
@@ -601,8 +775,8 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat
     return (jint) result;
 }
 
-JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStop(
-    JNIEnv *env, jobject jObj)
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStop
+(JNIEnv *env, jobject jObj)
 {
     LOGD("JNIProviderService: nativeStop - IN");
 
@@ -622,8 +796,8 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat
     return (jint) result;
 }
 
-JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendMessage(
-    JNIEnv *env, jobject jObj, jobject jMsg)
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendMessage
+(JNIEnv *env, jobject jObj, jobject jMsg)
 {
     LOGD("JNIProviderService: nativeSendMessage - IN");
     if (!jMsg)
@@ -648,8 +822,8 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat
     return (jint) result;
 }
 
-JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendSyncInfo(
-    JNIEnv *env, jobject jObj, jlong messageId , jint syncState)
+JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendSyncInfo
+(JNIEnv *env, jobject jObj, jlong messageId , jint syncState)
 {
     LOGD("JNIProviderService: nativeSendSyncInfo - IN");
     OIC::Service::NSProviderService::getInstance()->SendSyncInfo( messageId,
@@ -658,9 +832,29 @@ JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat
     return;
 }
 
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService(JNIEnv *env,
-        jobject jObj, jstring jstr)
+JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeCreateMessage
+(JNIEnv *env, jobject jObj)
+{
+    LOGD("JNIProviderService: nativeCreateMessage - IN");
+    OIC::Service::NSMessage* message =
+                      OIC::Service::NSProviderService::getInstance()->CreateMessage();
+    if(message == nullptr)
+    {
+        ThrowNSException(NS_ERROR, "Couldn't get Native Message");
+        return NULL;
+    }
+    jobject jMsg =  getJavaMessage(env, message);
+    if(!jMsg)
+    {
+        ThrowNSException(NS_ERROR, "Couldn't create Java Message");
+        return NULL;
+    }
+    LOGD("JNIProviderService: nativeCreateMessage - OUT");
+    return jMsg;
+}
+
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService
+(JNIEnv *env,jobject jObj, jstring jstr)
 {
     LOGD("JNIProviderService: nativeEnableRemoteService - IN");
     if (!jstr)
@@ -683,9 +877,8 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService(
     return (jint) result;
 }
 
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService(JNIEnv *env,
-        jobject jObj, jstring jstr)
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService
+(JNIEnv *env,jobject jObj, jstring jstr)
 {
     LOGD("JNIProviderService: nativeDisableRemoteService - IN");
     if (!jstr)
@@ -770,8 +963,8 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_
     return obj_topicList;
 }
 
-JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcceptSubscription(
-    JNIEnv *env,  jobject jObj, jobject jConsumer, jboolean jAccepted)
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcceptSubscription
+(JNIEnv *env,  jobject jObj, jobject jConsumer, jboolean jAccepted)
 {
     LOGD("JNIProviderService: nativeAcceptSubscription - IN");
 
index bc7779e67865fbacf3ae257d532c6a3ee7c2d702..ed7bdb6fdc755289984d76559ba2d8b95669a5a2 100644 (file)
@@ -59,13 +59,20 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat
 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendSyncInfo
 (JNIEnv *, jobject, jlong, jint);
 
+/*
+ * Class:     org_iotivity_service_ns_provider_ProviderService
+ * Method:    nativeCreateMessage
+ * Signature: ()Lorg/iotivity/service/ns/common/Message;
+ */
+JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeCreateMessage
+(JNIEnv *, jobject);
+
 /*
  * Class:     org_iotivity_service_ns_provider_ProviderService
  * Method:    nativeEnableRemoteService
  * Signature: (Ljava/lang/String;)I
  */
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService
 (JNIEnv *, jobject, jstring);
 
 /*
@@ -73,8 +80,7 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService
  * Method:    nativeDisableRemoteService
  * Signature: (Ljava/lang/String;)I
  */
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService
 (JNIEnv *, jobject, jstring);
 
 /*
@@ -82,8 +88,7 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService
  * Method:    nativeAddTopic
  * Signature: (Ljava/lang/String;)I
  */
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeAddTopic
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeAddTopic
 (JNIEnv *, jobject, jstring);
 
 /*
@@ -91,8 +96,7 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeAddTopic
  * Method:    nativeDeleteTopic
  * Signature: (Ljava/lang/String;)I
  */
-JNIEXPORT jint JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeDeleteTopic
+JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeDeleteTopic
 (JNIEnv *, jobject, jstring);
 
 /*
@@ -100,8 +104,7 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeDeleteTopic
  * Method:    nativeGetTopics
  * Signature: ()Lorg/iotivity/service/ns/common/TopicsList;
  */
-JNIEXPORT jobject JNICALL
-Java_org_iotivity_service_ns_provider_ProviderService_nativeGetTopics
+JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeGetTopics
 (JNIEnv *, jobject);
 
 /*