2 //******************************************************************
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.service.ns.sample.provider;
25 import android.app.Notification;
26 import android.os.Bundle;
27 import android.service.notification.NotificationListenerService;
28 import android.service.notification.StatusBarNotification;
29 import android.util.Log;
30 import org.iotivity.service.ns.common.MediaContents;
31 import org.iotivity.service.ns.common.Message;
32 import java.util.ArrayList;
34 public class NotiListener extends NotificationListenerService {
36 private final String TAG = "NS_JNI_NOTI_LISTENER";
37 private static ProviderSample mProviderSample = null;
38 private MainActivity mActivity = null;
39 ArrayList mBlackSourceList = new ArrayList<String>();
41 public NotiListener() {
43 Log.i(TAG, "Create NotiListener");
46 public NotiListener(MainActivity activity) {
48 Log.i(TAG, "Create NotiListener with MainActivity");
50 this.mActivity = activity;
51 this.mProviderSample = mActivity.getProviderSample();
55 if (mProviderSample == null) {
56 Log.i(TAG, "Fail to get providerProxy instance");
60 public void setBlackSourceList() {
62 // set blacklist of app package name not to receive notification
63 mBlackSourceList.add("android");
64 mBlackSourceList.add("com.android.systemui");
68 public void onNotificationPosted(StatusBarNotification sbn) {
69 super.onNotificationPosted(sbn);
70 Log.i(TAG, "Notification posted By package" + sbn.getPackageName());
72 Bundle bundle = sbn.getNotification().extras;
75 // prevent not to send notification
76 for (int i = 0; i < mBlackSourceList.size(); ++i) {
77 if (sbn.getPackageName().equals(mBlackSourceList.get(i))) {
82 // filter exception case : Some notification are generated twice
83 if (sbn.getId() > 10000 || sbn.getId() < 0) {
87 // Temporary protocol code to display ICON on consumer app.
88 // For example, consumer app shows KAKAOTALK Icon when receiving
89 // Notification with SOURCE
90 // that is set to KAKAO, otherwise it displays OCF Icon on current
92 if (sbn.getPackageName().equals("com.kakao.talk")) {
99 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
100 Log.i(TAG, "Noti. ID : " + sbn.getId());
102 String id = Integer.toString(sbn.getId());
103 String title = bundle.getString(Notification.EXTRA_TITLE, "");
104 String body = bundle.getString(Notification.EXTRA_TEXT, "");
105 String topic = bundle.getString(Notification.EXTRA_SUB_TEXT, "");
107 Log.i(TAG, "onNotificationPosted .. ");
108 Log.i(TAG, "source : " + source);
109 Log.i(TAG, "Id : " + id);
110 Log.i(TAG, "Title : " + title);
111 Log.i(TAG, "Body : " + body);
113 Message notiMessage = mProviderSample.createMessage();
114 if(notiMessage == null)
116 Log.e(TAG, "CreateMessage Failed");
119 Log.i(TAG, "Message ID : " + notiMessage.getMessageId());
120 Log.i(TAG, "Provider ID : " + notiMessage.getProviderId());
121 notiMessage.setTitle(title);
122 notiMessage.setContentText(body);
123 notiMessage.setTopic(topic);
124 notiMessage.setSourceName(source);
125 notiMessage.setTTL(10);
126 notiMessage.setTime("12:10");
127 MediaContents media = new MediaContents("daasd");
128 notiMessage.setMediaContents(media);
129 if (mProviderSample != null) {
130 mProviderSample.sendMessage(notiMessage);
132 Log.i(TAG, "providerExample is NULL");
137 public void onNotificationRemoved(StatusBarNotification sbn) {
138 super.onNotificationRemoved(sbn);
140 Bundle bundle = sbn.getNotification().extras;
142 if (sbn.getPackageName().equals("android")) {
146 Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
147 Log.i(TAG, "Noti. ID : " + sbn.getId());
149 if (mProviderSample.getMsgMap().containsKey(sbn.getId())) {
150 if (mProviderSample.getMsgMap().get(sbn.getId()) == 2) {
151 org.iotivity.service.ns.common.SyncInfo.SyncType type = org.iotivity.service.ns.common.SyncInfo.SyncType.READ;
152 mProviderSample.sendSyncInfo(1, type);