Update notificaion message synchronization.
authorch79.cho <ch79.cho@samsung.com>
Thu, 26 May 2016 08:37:31 +0000 (17:37 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 26 May 2016 09:49:51 +0000 (09:49 +0000)
A bug about message synchronization function is fixed.
This function provides for subscribers to receive sync data
which is sent by a subscriber.

Change-Id: Idfa0b242e1a17af7ec88eb43c7b5084d6ceae14d
Signed-off-by: ch79.cho <ch79.cho@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8351
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/notification/android/notification-service/src/main/java/org/iotivity/service/notification/IoTNotification.java
service/notification/android/notification-service/src/main/jni/notificationProvider.c
service/notification/android/notification-service/src/main/jni/notificationProvider.h
service/notification/examples/android/NotiProviderExample/app/src/main/java/com/sec/notiproviderexample/MainActivity.java
service/notification/examples/android/NotiProviderExample/app/src/main/java/com/sec/notiproviderexample/NotiListener.java
service/notification/examples/android/NotiProviderExample/app/src/main/java/com/sec/notiproviderexample/ProviderProxy.java
service/notification/examples/android/NotiProviderExample/app/src/main/res/layout/activity_main.xml

index 3bea94e..e5a1fe6 100644 (file)
@@ -29,7 +29,7 @@ public class IoTNotification
     }
 
     public interface NSSynchListner {
-        public void OnNSSynchronizedEvent(String messageId, int syncState);
+       public void OnNSSynchronizedEvent(String messageId, int syncState);
     }
 }
 
index 916aff9..de8d27d 100644 (file)
@@ -24,7 +24,7 @@
 \r
 #define  LOG_TAG   "JNI_NS_INTERFACE"\r
 #define  LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)\r
-#define  LOGE(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)\r
+#define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)\r
 \r
 static JavaVM *g_jvm = NULL;\r
 static jobject g_obj_subscriptionListener = NULL;\r
@@ -47,7 +47,6 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NS
     if (!jSubscriptionListener || !jSyncListener)\r
     {\r
         LOGI("Fail to set listeners");\r
-        //return (jint) NS_ERROR;\r
     }\r
 \r
     g_obj_subscriptionListener = (jobject) (*env)->NewGlobalRef(env, jSubscriptionListener);\r
@@ -104,7 +103,7 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NS
 JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NSProviderReadCheck(\r
         JNIEnv * env, jobject jObj, jobject jMsg)\r
 {\r
-    printf("JNI TEST - NSReasCheck\n");\r
+    LOGI("NSReasCheck");\r
     return 0;\r
 }\r
 \r
@@ -113,12 +112,12 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_notification_IoTNotification_NS
 {\r
     if (jAccepted)\r
     {\r
-        printf("JNI TEST - Accepted\n");\r
+        LOGI("Accepted");\r
         //NSAccept(consumer, true);\r
     }\r
     else\r
     {\r
-        printf("JNI TEST - Denied\n");\r
+        LOGI("Denied");\r
         //NSAccept(consumer, false);\r
     }\r
 \r
@@ -177,7 +176,7 @@ void NSSubscribeRequestCb(NSConsumer *consumer)
     return;\r
 }\r
 \r
-void NSSyncCb(NSProvider *provider, NSSync *sync)\r
+void NSSyncCb(NSSync *sync)\r
 {\r
     LOGI("Sync requested");\r
 \r
@@ -241,14 +240,14 @@ NSMessage * NSGetMessage(JNIEnv * env, jobject jMsg)
     jfieldID fid_id = (*env)->GetFieldID(env, cls, "id", "Ljava/lang/String;");\r
     if (fid_id == NULL)\r
     {\r
-        LOGI("Error: jfieldID for message id is null");\r
+        LOGE("Error: jfieldID for message id is null");\r
         return (jint) NS_ERROR;\r
     }\r
     jstring jmsgId = (*env)->GetObjectField(env, jMsg, fid_id);\r
     const char * messageId = (*env)->GetStringUTFChars(env, jmsgId, NULL);\r
     if (messageId == NULL)\r
     {\r
-        printf("Error: messageId is null\n");\r
+        LOGE("Error: messageId is null");\r
         return (jint) NS_ERROR;\r
     }\r
     LOGI("Message ID: %s\n", messageId);\r
@@ -264,7 +263,7 @@ NSMessage * NSGetMessage(JNIEnv * env, jobject jMsg)
     const char * messageTitle = (*env)->GetStringUTFChars(env, jmsgTitle, NULL);\r
     if (messageTitle == NULL)\r
     {\r
-        printf("Error: messageTitle is null\n");\r
+        LOGE("Error: messageTitle is null");\r
         return (jint) NS_ERROR;\r
     }\r
     LOGI("Message Title: %s\n", messageTitle);\r
@@ -280,7 +279,7 @@ NSMessage * NSGetMessage(JNIEnv * env, jobject jMsg)
     const char * messageBody = (*env)->GetStringUTFChars(env, jmsgBody, NULL);\r
     if (messageBody == NULL)\r
     {\r
-        printf("Error: messageBody is null\n");\r
+        LOGE("Error: messageBody is null");\r
         return (jint) NS_ERROR;\r
     }\r
     LOGI("Message Body: %s\n", messageBody);\r
@@ -296,7 +295,7 @@ NSMessage * NSGetMessage(JNIEnv * env, jobject jMsg)
     const char * messageSource = (*env)->GetStringUTFChars(env, jmsgSource, NULL);\r
     if (messageSource == NULL)\r
     {\r
-        printf("Error: messageSource is null\n");\r
+        LOGE("Error: messageSource is null");\r
         return (jint) NS_ERROR;\r
     }\r
     LOGI("Message Source: %s\n", messageSource);\r
index b462651..63827fd 100644 (file)
@@ -46,7 +46,7 @@ extern "C"
 \r
     void NSSubscribeRequestCb(NSConsumer*);\r
 \r
-    void NSSyncCb(NSProvider*, NSSync*);\r
+    void NSSyncCb(NSSync*);\r
 \r
     NSMessage * NSGetMessage(JNIEnv *, jobject);\r
 \r
index 29afcbb..fc9998e 100755 (executable)
@@ -34,12 +34,15 @@ import android.view.View;
 import android.widget.Button;\r
 import android.widget.EditText;\r
 import android.widget.TextView;\r
+import android.widget.Toast;\r
 \r
 public class MainActivity extends AppCompatActivity {\r
 \r
     private final String TAG = "NS_MAIN_ACTIVITY";\r
     private static final int MESSAGE_SUBSCRIPTION = 1;\r
     private static final int MESSAGE_SYNC = 2;\r
+    private static final int MESSAGE_NOTIFICATION = 3;\r
+\r
     private Button btnTitle;\r
     private Button btnBody;\r
     private Button btnSend;\r
@@ -66,20 +69,31 @@ public class MainActivity extends AppCompatActivity {
                 case MESSAGE_SUBSCRIPTION:\r
                     String subscriber = (String) msg.obj;\r
                     if(subscriber != null)\r
-                        TvLog.append("Recv-Sub(" + subCnt++ + ") " + subscriber + "\n");\r
+                        TvLog.append("Subscriber IP(" + ++subCnt + "): " + subscriber + "\n");\r
                     break;\r
 \r
                 case MESSAGE_SYNC:\r
                     String sync = (String) msg.obj;\r
                     if(sync != null)\r
-                        TvLog.append("Sync-Read(#" + sync + ")\n");\r
+                        TvLog.append("Sync-Read(Msg ID: " + sync + ")\n");\r
                     break;\r
+\r
                 default:\r
                     break;\r
             }\r
         }\r
     };\r
 \r
+    public void showToast(final String toast)\r
+    {\r
+        runOnUiThread(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();\r
+            }\r
+        });\r
+    }\r
+\r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
@@ -184,11 +198,9 @@ public class MainActivity extends AppCompatActivity {
                     NotificationManager notiMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\r
                     notiMgr.notify(notiId, notiBuilder.build());\r
 \r
-                    notiId++;\r
-\r
                     Log.i(TAG, "#" + notiId + " notified ..");\r
-                    TvLog.append("Send Notitication(#" + notiId + ")\n");\r
-\r
+                    TvLog.append("Send Notitication(Msg ID: " + notiId + ")\n");\r
+                    notiId++;\r
                 }\r
                 break;\r
 \r
index 2bdd016..4492799 100755 (executable)
@@ -27,12 +27,14 @@ import android.os.Bundle;
 import android.service.notification.NotificationListenerService;\r
 import android.service.notification.StatusBarNotification;\r
 import android.util.Log;\r
+import java.util.ArrayList;\r
 \r
 public class NotiListener extends NotificationListenerService {\r
 \r
     private final String TAG = "NS_JNI_NOTI_LISTENER";\r
     private static ProviderProxy mProviderProxy = null;\r
     private MainActivity mActivity = null;\r
+    ArrayList mBlackSourceList = new ArrayList<String>();\r
 \r
     public NotiListener() {\r
 \r
@@ -46,29 +48,57 @@ public class NotiListener extends NotificationListenerService {
         this.mActivity = activity;\r
         this.mProviderProxy = mActivity.getProviderProxy();\r
 \r
+        setBlackSourceList();\r
+\r
         if(mProviderProxy == null) {\r
             Log.i(TAG, "Fail to get providerProxy instance");\r
         }\r
     }\r
 \r
+    public void setBlackSourceList() {\r
+\r
+        // set blacklist of app package name not to receive notification\r
+        mBlackSourceList.add("android");\r
+        mBlackSourceList.add("com.android.systemui");\r
+    }\r
+\r
     @Override\r
     public void onNotificationPosted(StatusBarNotification sbn) {\r
         super.onNotificationPosted(sbn);\r
 \r
         Bundle bundle = sbn.getNotification().extras;\r
+        String source = null;\r
+\r
+        // prevent not to send notification\r
+        for(int i = 0; i < mBlackSourceList.size(); ++i)\r
+        {\r
+            if (sbn.getPackageName().equals(mBlackSourceList.get(i)))\r
+            {\r
+                return;\r
+            }\r
+        }\r
 \r
-        if (sbn.getPackageName().equals("android"))\r
+        // filter exception case : Some notification are generated twice\r
+        if(sbn.getId() > 10000 || sbn.getId() < 0)\r
             return;\r
 \r
+        // Temporary protocol code to display ICON on consumer app.\r
+        // For example, consumer app shows KAKAOTALK Icon when receiving Notification with SOURCE\r
+        // that is set to KAKAO, otherwise it displays OCF Icon on current sample app.\r
+        if(sbn.getPackageName().equals("com.kakao.talk"))\r
+            source = "KAKAO";\r
+        else\r
+            source = "OCF";\r
+\r
         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
 \r
         String id = Integer.toString(sbn.getId());\r
         String title = bundle.getString(Notification.EXTRA_TITLE, "");\r
         String body = bundle.getString(Notification.EXTRA_TEXT, "");\r
-        String source = "OCF";\r
 \r
         Log.i(TAG, "onNotificationPosted .. ");\r
+        Log.i(TAG, "source : " + source);\r
         Log.i(TAG, "Id : " + id);\r
         Log.i(TAG, "Title : " + title);\r
         Log.i(TAG, "Body : " + body);\r
@@ -92,6 +122,12 @@ public class NotiListener extends NotificationListenerService {
         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
 \r
-        mProviderProxy.readCheck(Integer.toString(sbn.getId()));\r
+        if(mProviderProxy.getMsgMap().containsKey(sbn.getId()))\r
+        {\r
+            if(mProviderProxy.getMsgMap().get(sbn.getId()) == 2)\r
+            {\r
+                mProviderProxy.readCheck(Integer.toString(sbn.getId()));\r
+            }\r
+        }\r
     }\r
 }\r
index 4e3f7a9..9fe6a26 100755 (executable)
 \r
 package com.sec.notiproviderexample;\r
 \r
+import android.app.NotificationManager;\r
 import android.content.Context;\r
 import android.os.Handler;\r
 import android.os.Message;\r
 import android.util.Log;\r
+import android.widget.Toast;\r
 \r
 import org.iotivity.base.ModeType;\r
 import org.iotivity.base.OcPlatform;\r
@@ -50,9 +52,14 @@ public class ProviderProxy
     private HashMap<String, Integer> msgMap;\r
 \r
     private Handler mHandler = null;\r
+\r
     private static final int MESSAGE_SUBSCRIPTION = 1;\r
     private static final int MESSAGE_SYNC = 2;\r
 \r
+    private static final int SYNC_READ = 0;\r
+    private static final int SYNC_DISMISS = 1;\r
+    private static final int SYNC_UNREAD = 2;\r
+\r
     public ProviderProxy(Context context) {\r
         Log.i(TAG, "Create providerProxy Instance");\r
 \r
@@ -110,14 +117,25 @@ public class ProviderProxy
         notiMessage.setTitle(title);\r
         notiMessage.setBody(body);\r
         notiMessage.setSource(source);\r
-        msgMap.put(id, 1);\r
+        msgMap.put(id, SYNC_UNREAD);\r
         ioTNotification.NSSendNotification(notiMessage);\r
+\r
+        mHandler.post(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                Toast.makeText(mContext, "Notification sent", Toast.LENGTH_SHORT).show();\r
+            }\r
+        });\r
     }\r
 \r
     public void readCheck(String messageId) {\r
         if(msgMap.containsKey(messageId)) {\r
-            NSMessage notiMessage = new NSMessage(messageId);\r
-            ioTNotification.NSProviderReadCheck(notiMessage);\r
+            if(msgMap.get(messageId) == SYNC_UNREAD)\r
+            {\r
+                NSMessage notiMessage = new NSMessage(messageId);\r
+                ioTNotification.NSProviderReadCheck(notiMessage);\r
+                msgMap.put(messageId, SYNC_READ);\r
+            }\r
         }\r
     }\r
 \r
@@ -143,7 +161,26 @@ public class ProviderProxy
         Log.i(TAG, "Message Id: " + messageId);\r
         Log.i(TAG, "Sync state: " + syncState);\r
 \r
-        Message msg = mHandler.obtainMessage(MESSAGE_SYNC, messageId + "(" + syncState + ")");\r
+        Message msg = mHandler.obtainMessage(MESSAGE_SYNC, messageId + " / Sync State: " + syncState);\r
         mHandler.sendMessage(msg);\r
+\r
+        NotificationManager manager = (NotificationManager)mContext\r
+                .getSystemService(Context.NOTIFICATION_SERVICE);\r
+\r
+        if(messageId != null)\r
+            try\r
+            {\r
+                manager.cancel(Integer.valueOf(messageId));\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                Log.e(TAG, "Handle exception for invalid message id" + e);\r
+            }\r
+        else\r
+            Log.i(TAG, "message id is null");\r
+    }\r
+\r
+    public HashMap<String, Integer> getMsgMap() {\r
+        return msgMap;\r
     }\r
 }\r
index 0d90f05..2365c5c 100644 (file)
             android:layout_height="10dp">\r
         </LinearLayout>\r
 \r
-        <LinearLayout\r
+        <ScrollView\r
             android:layout_width="match_parent"\r
             android:layout_height="wrap_content"\r
-            android:paddingLeft="5dp"\r
-            android:paddingRight="5dp"\r
-            android:orientation="horizontal" >\r
+            android:fillViewport="true">\r
 \r
-            <TextView\r
-                android:layout_gravity="center_vertical|center_horizontal"\r
-                android:layout_height="200dp"\r
+            <LinearLayout\r
                 android:layout_width="match_parent"\r
-                android:scrollbars="vertical"\r
-                android:id="@+id/TvLog"\r
-                android:text="Log.."/>\r
-        </LinearLayout>\r
+                android:layout_height="150dp"\r
+                android:paddingLeft="5dp"\r
+                android:paddingRight="5dp"\r
+                android:orientation="horizontal" >\r
+\r
+                <TextView\r
+                    android:layout_gravity="center_vertical|center_horizontal"\r
+                    android:layout_height="150dp"\r
+                    android:layout_width="match_parent"\r
+                    android:scrollbars="vertical"\r
+                    android:id="@+id/TvLog"\r
+                    android:text="Log.."/>\r
+            </LinearLayout>\r
+\r
+        </ScrollView>\r
 \r
         <LinearLayout\r
             android:layout_width="match_parent"\r