Notification ProviderService Java API Updation
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / main / java / com / sec / notiproviderexample / NotiListener.java
1 /*\r
2 //******************************************************************\r
3 //\r
4 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
5 //\r
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
7 //\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
11 //\r
12 //      http://www.apache.org/licenses/LICENSE-2.0\r
13 //\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
19 //\r
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
21  */\r
22 \r
23 package com.sec.notiproviderexample;\r
24 \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
34 \r
35 public class NotiListener extends NotificationListenerService {\r
36 \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
41 \r
42     public NotiListener() {\r
43 \r
44         Log.i(TAG, "Create NotiListener");\r
45     }\r
46 \r
47     public NotiListener(MainActivity activity) {\r
48 \r
49         Log.i(TAG, "Create NotiListener with MainActivity");\r
50 \r
51         this.mActivity = activity;\r
52         this.mProviderProxy = mActivity.getProviderProxy();\r
53 \r
54         setBlackSourceList();\r
55 \r
56         if(mProviderProxy == null) {\r
57             Log.i(TAG, "Fail to get providerProxy instance");\r
58         }\r
59     }\r
60 \r
61     public void setBlackSourceList() {\r
62 \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
66     }\r
67 \r
68     @Override\r
69     public void onNotificationPosted(StatusBarNotification sbn) {\r
70         super.onNotificationPosted(sbn);\r
71 \r
72         Bundle bundle = sbn.getNotification().extras;\r
73         String source = null;\r
74 \r
75         // prevent not to send notification\r
76         for(int i = 0; i < mBlackSourceList.size(); ++i)\r
77         {\r
78             if (sbn.getPackageName().equals(mBlackSourceList.get(i)))\r
79             {\r
80                 return;\r
81             }\r
82         }\r
83 \r
84         // filter exception case : Some notification are generated twice\r
85         if(sbn.getId() > 10000 || sbn.getId() < 0)\r
86             return;\r
87 \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
92             source = "KAKAO";\r
93         else\r
94             source = "OCF";\r
95 \r
96         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
97         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
98 \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
102 \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
108 \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
116         } else {\r
117             Log.i(TAG, "providerExample is NULL");\r
118         }\r
119     }\r
120 \r
121     @Override\r
122     public void onNotificationRemoved(StatusBarNotification sbn) {\r
123         super.onNotificationRemoved(sbn);\r
124 \r
125         Bundle bundle = sbn.getNotification().extras;\r
126 \r
127         if (sbn.getPackageName().equals("android"))\r
128             return;\r
129 \r
130         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
131         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
132 \r
133         if(mProviderProxy.getMsgMap().containsKey(sbn.getId()))\r
134         {\r
135             if(mProviderProxy.getMsgMap().get(sbn.getId()) == 2)\r
136             {\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
139             }\r
140         }\r
141     }\r
142 }\r