replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / main / java / org / iotivity / service / ns / sample / provider / NotiListener.java
1 /*
2 //******************************************************************
3 //
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 //
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
11 //
12 //      http://www.apache.org/licenses/LICENSE-2.0
13 //
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.
19 //
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.service.ns.sample.provider;
24
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;
33
34 public class NotiListener extends NotificationListenerService {
35
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>();
40
41     public NotiListener() {
42
43         Log.i(TAG, "Create NotiListener");
44     }
45
46     public NotiListener(MainActivity activity) {
47
48         Log.i(TAG, "Create NotiListener with MainActivity");
49
50         this.mActivity = activity;
51         this.mProviderSample = mActivity.getProviderSample();
52
53         setBlackSourceList();
54
55         if (mProviderSample == null) {
56             Log.i(TAG, "Fail to get providerProxy instance");
57         }
58     }
59
60     public void setBlackSourceList() {
61
62         // set blacklist of app package name not to receive notification
63         mBlackSourceList.add("android");
64         mBlackSourceList.add("com.android.systemui");
65     }
66
67     @Override
68     public void onNotificationPosted(StatusBarNotification sbn) {
69         super.onNotificationPosted(sbn);
70         Log.i(TAG, "Notification posted By package" + sbn.getPackageName());
71
72         Bundle bundle = sbn.getNotification().extras;
73         String source = null;
74
75         // prevent not to send notification
76         for (int i = 0; i < mBlackSourceList.size(); ++i) {
77             if (sbn.getPackageName().equals(mBlackSourceList.get(i))) {
78                 return;
79             }
80         }
81
82         // filter exception case : Some notification are generated twice
83         if (sbn.getId() > 10000 || sbn.getId() < 0) {
84             return;
85         }
86
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
91         // sample app.
92         if (sbn.getPackageName().equals("com.kakao.talk")) {
93             source = "KAKAO";
94         }
95         else {
96             source = "OCF";
97         }
98
99         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
100         Log.i(TAG, "Noti. ID : " + sbn.getId());
101
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, "");
106
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);
112
113         Message notiMessage = mProviderSample.createMessage();
114         if(notiMessage == null)
115         {
116             Log.e(TAG, "CreateMessage Failed");
117             return;
118         }
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);
131         } else {
132             Log.i(TAG, "providerExample is NULL");
133         }
134     }
135
136     @Override
137     public void onNotificationRemoved(StatusBarNotification sbn) {
138         super.onNotificationRemoved(sbn);
139
140         Bundle bundle = sbn.getNotification().extras;
141
142         if (sbn.getPackageName().equals("android")) {
143             return;
144         }
145
146         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());
147         Log.i(TAG, "Noti. ID : " + sbn.getId());
148
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);
153             }
154         }
155     }
156 }