2 //******************************************************************
\r
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 // you may not use this file except in compliance with the License.
\r
10 // You may obtain a copy of the License at
\r
12 // http://www.apache.org/licenses/LICENSE-2.0
\r
14 // Unless required by applicable law or agreed to in writing, software
\r
15 // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 // See the License for the specific language governing permissions and
\r
18 // limitations under the License.
\r
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
23 package com.sec.notiproviderexample;
\r
25 import android.app.Notification;
\r
26 import android.content.SyncInfo;
\r
27 import android.os.Bundle;
\r
28 import android.service.notification.NotificationListenerService;
\r
29 import android.service.notification.StatusBarNotification;
\r
30 import android.util.Log;
\r
31 import org.iotivity.service.ns.common.MediaContents;
\r
32 import org.iotivity.service.ns.common.Message;
\r
33 import java.util.ArrayList;
\r
35 public class NotiListener extends NotificationListenerService {
\r
37 private final String TAG = "NS_JNI_NOTI_LISTENER";
\r
38 private static ProviderProxy mProviderProxy = null;
\r
39 private MainActivity mActivity = null;
\r
40 ArrayList mBlackSourceList = new ArrayList<String>();
\r
42 public NotiListener() {
\r
44 Log.i(TAG, "Create NotiListener");
\r
47 public NotiListener(MainActivity activity) {
\r
49 Log.i(TAG, "Create NotiListener with MainActivity");
\r
51 this.mActivity = activity;
\r
52 this.mProviderProxy = mActivity.getProviderProxy();
\r
54 setBlackSourceList();
\r
56 if(mProviderProxy == null) {
\r
57 Log.i(TAG, "Fail to get providerProxy instance");
\r
61 public void setBlackSourceList() {
\r
63 // set blacklist of app package name not to receive notification
\r
64 mBlackSourceList.add("android");
\r
65 mBlackSourceList.add("com.android.systemui");
\r
69 public void onNotificationPosted(StatusBarNotification sbn) {
\r
70 super.onNotificationPosted(sbn);
\r
72 Bundle bundle = sbn.getNotification().extras;
\r
73 String source = null;
\r
75 // prevent not to send notification
\r
76 for(int i = 0; i < mBlackSourceList.size(); ++i)
\r
78 if (sbn.getPackageName().equals(mBlackSourceList.get(i)))
\r
84 // filter exception case : Some notification are generated twice
\r
85 if(sbn.getId() > 10000 || sbn.getId() < 0)
\r
88 // Temporary protocol code to display ICON on consumer app.
\r
89 // For example, consumer app shows KAKAOTALK Icon when receiving Notification with SOURCE
\r
90 // that is set to KAKAO, otherwise it displays OCF Icon on current sample app.
\r
91 if(sbn.getPackageName().equals("com.kakao.talk"))
\r
96 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
\r
97 Log.i(TAG, "Noti. ID : " + sbn.getId());
\r
99 String id = Integer.toString(sbn.getId());
\r
100 String title = bundle.getString(Notification.EXTRA_TITLE, "");
\r
101 String body = bundle.getString(Notification.EXTRA_TEXT, "");
\r
103 Log.i(TAG, "onNotificationPosted .. ");
\r
104 Log.i(TAG, "source : " + source);
\r
105 Log.i(TAG, "Id : " + id);
\r
106 Log.i(TAG, "Title : " + title);
\r
107 Log.i(TAG, "Body : " + body);
\r
109 Message notiMessage = new Message(title,body,source);
\r
110 notiMessage.setTTL(10);
\r
111 notiMessage.setTime("12:10");
\r
112 MediaContents media = new MediaContents("daasd");
\r
113 notiMessage.setMediaContents(media);
\r
114 if (mProviderProxy != null) {
\r
115 mProviderProxy.SendMessage(notiMessage);
\r
117 Log.i(TAG, "providerExample is NULL");
\r
122 public void onNotificationRemoved(StatusBarNotification sbn) {
\r
123 super.onNotificationRemoved(sbn);
\r
125 Bundle bundle = sbn.getNotification().extras;
\r
127 if (sbn.getPackageName().equals("android"))
\r
130 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
\r
131 Log.i(TAG, "Noti. ID : " + sbn.getId());
\r
133 if(mProviderProxy.getMsgMap().containsKey(sbn.getId()))
\r
135 if(mProviderProxy.getMsgMap().get(sbn.getId()) == 2)
\r
137 org.iotivity.service.ns.common.SyncInfo.SyncType type = org.iotivity.service.ns.common.SyncInfo.SyncType.READ;
\r
138 mProviderProxy.SendSyncInfo(1,type);
\r