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.os.Bundle;
\r
27 import android.service.notification.NotificationListenerService;
\r
28 import android.service.notification.StatusBarNotification;
\r
29 import android.util.Log;
\r
30 import org.iotivity.service.ns.common.MediaContents;
\r
31 import org.iotivity.service.ns.common.Message;
\r
32 import java.util.ArrayList;
\r
34 public class NotiListener extends NotificationListenerService {
\r
36 private final String TAG = "NS_JNI_NOTI_LISTENER";
\r
37 private static ProviderSample mProviderSample = null;
\r
38 private MainActivity mActivity = null;
\r
39 ArrayList mBlackSourceList = new ArrayList<String>();
\r
41 public NotiListener() {
\r
43 Log.i(TAG, "Create NotiListener");
\r
46 public NotiListener(MainActivity activity) {
\r
48 Log.i(TAG, "Create NotiListener with MainActivity");
\r
50 this.mActivity = activity;
\r
51 this.mProviderSample = mActivity.getProviderSample();
\r
53 setBlackSourceList();
\r
55 if(mProviderSample == null) {
\r
56 Log.i(TAG, "Fail to get providerProxy instance");
\r
60 public void setBlackSourceList() {
\r
62 // set blacklist of app package name not to receive notification
\r
63 mBlackSourceList.add("android");
\r
64 mBlackSourceList.add("com.android.systemui");
\r
68 public void onNotificationPosted(StatusBarNotification sbn) {
\r
69 super.onNotificationPosted(sbn);
\r
71 Bundle bundle = sbn.getNotification().extras;
\r
72 String source = null;
\r
74 // prevent not to send notification
\r
75 for(int i = 0; i < mBlackSourceList.size(); ++i)
\r
77 if (sbn.getPackageName().equals(mBlackSourceList.get(i)))
\r
83 // filter exception case : Some notification are generated twice
\r
84 if(sbn.getId() > 10000 || sbn.getId() < 0)
\r
87 // Temporary protocol code to display ICON on consumer app.
\r
88 // For example, consumer app shows KAKAOTALK Icon when receiving Notification with SOURCE
\r
89 // that is set to KAKAO, otherwise it displays OCF Icon on current sample app.
\r
90 if(sbn.getPackageName().equals("com.kakao.talk"))
\r
95 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
\r
96 Log.i(TAG, "Noti. ID : " + sbn.getId());
\r
98 String id = Integer.toString(sbn.getId());
\r
99 String title = bundle.getString(Notification.EXTRA_TITLE, "");
\r
100 String body = bundle.getString(Notification.EXTRA_TEXT, "");
\r
102 Log.i(TAG, "onNotificationPosted .. ");
\r
103 Log.i(TAG, "source : " + source);
\r
104 Log.i(TAG, "Id : " + id);
\r
105 Log.i(TAG, "Title : " + title);
\r
106 Log.i(TAG, "Body : " + body);
\r
108 Message notiMessage = new Message(title,body,source);
\r
109 notiMessage.setTTL(10);
\r
110 notiMessage.setTime("12:10");
\r
111 MediaContents media = new MediaContents("daasd");
\r
112 notiMessage.setMediaContents(media);
\r
113 if (mProviderSample != null) {
\r
114 mProviderSample.SendMessage(notiMessage);
\r
116 Log.i(TAG, "providerExample is NULL");
\r
121 public void onNotificationRemoved(StatusBarNotification sbn) {
\r
122 super.onNotificationRemoved(sbn);
\r
124 Bundle bundle = sbn.getNotification().extras;
\r
126 if (sbn.getPackageName().equals("android"))
\r
129 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
\r
130 Log.i(TAG, "Noti. ID : " + sbn.getId());
\r
132 if(mProviderSample.getMsgMap().containsKey(sbn.getId()))
\r
134 if(mProviderSample.getMsgMap().get(sbn.getId()) == 2)
\r
136 org.iotivity.service.ns.common.SyncInfo.SyncType type = org.iotivity.service.ns.common.SyncInfo.SyncType.READ;
\r
137 mProviderSample.SendSyncInfo(1,type);
\r