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