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>
}
public interface NSSynchListner {
- public void OnNSSynchronizedEvent(String messageId, int syncState);
+ public void OnNSSynchronizedEvent(String messageId, int syncState);
}
}
\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
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
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
{\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
return;\r
}\r
\r
-void NSSyncCb(NSProvider *provider, NSSync *sync)\r
+void NSSyncCb(NSSync *sync)\r
{\r
LOGI("Sync requested");\r
\r
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
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
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
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
\r
void NSSubscribeRequestCb(NSConsumer*);\r
\r
- void NSSyncCb(NSProvider*, NSSync*);\r
+ void NSSyncCb(NSSync*);\r
\r
NSMessage * NSGetMessage(JNIEnv *, jobject);\r
\r
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
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
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
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
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
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
\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
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
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
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
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