Remove unused pkg dependancy
[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.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
33 \r
34 public class NotiListener extends NotificationListenerService {\r
35 \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
40 \r
41     public NotiListener() {\r
42 \r
43         Log.i(TAG, "Create NotiListener");\r
44     }\r
45 \r
46     public NotiListener(MainActivity activity) {\r
47 \r
48         Log.i(TAG, "Create NotiListener with MainActivity");\r
49 \r
50         this.mActivity = activity;\r
51         this.mProviderSample = mActivity.getProviderSample();\r
52 \r
53         setBlackSourceList();\r
54 \r
55         if(mProviderSample == null) {\r
56             Log.i(TAG, "Fail to get providerProxy instance");\r
57         }\r
58     }\r
59 \r
60     public void setBlackSourceList() {\r
61 \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
65     }\r
66 \r
67     @Override\r
68     public void onNotificationPosted(StatusBarNotification sbn) {\r
69         super.onNotificationPosted(sbn);\r
70 \r
71         Bundle bundle = sbn.getNotification().extras;\r
72         String source = null;\r
73 \r
74         // prevent not to send notification\r
75         for(int i = 0; i < mBlackSourceList.size(); ++i)\r
76         {\r
77             if (sbn.getPackageName().equals(mBlackSourceList.get(i)))\r
78             {\r
79                 return;\r
80             }\r
81         }\r
82 \r
83         // filter exception case : Some notification are generated twice\r
84         if(sbn.getId() > 10000 || sbn.getId() < 0)\r
85             return;\r
86 \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
91             source = "KAKAO";\r
92         else\r
93             source = "OCF";\r
94 \r
95         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
96         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
97 \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
101 \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
107 \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
115         } else {\r
116             Log.i(TAG, "providerExample is NULL");\r
117         }\r
118     }\r
119 \r
120     @Override\r
121     public void onNotificationRemoved(StatusBarNotification sbn) {\r
122         super.onNotificationRemoved(sbn);\r
123 \r
124         Bundle bundle = sbn.getNotification().extras;\r
125 \r
126         if (sbn.getPackageName().equals("android"))\r
127             return;\r
128 \r
129         Log.i(TAG, "Noti. Package Name : " + sbn.getPackageName());\r
130         Log.i(TAG, "Noti. ID : " + sbn.getId());\r
131 \r
132         if(mProviderSample.getMsgMap().containsKey(sbn.getId()))\r
133         {\r
134             if(mProviderSample.getMsgMap().get(sbn.getId()) == 2)\r
135             {\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
138             }\r
139         }\r
140     }\r
141 }\r