replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / main / java / org / iotivity / service / ns / sample / provider / ProviderSample.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.content.Context;
26 import android.os.*;
27 import android.util.Log;
28 import android.widget.Toast;
29
30 import org.iotivity.base.ModeType;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcResourceHandle;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ServiceType;
36 import org.iotivity.service.ns.common.Message;
37 import org.iotivity.service.ns.provider.*;
38 import org.iotivity.service.ns.common.*;
39
40 import java.util.HashMap;
41
42 public class ProviderSample
43         implements ProviderService.OnConsumerSubscribedListener,
44         ProviderService.OnMessageSynchronizedListener {
45
46     private static final String      TAG                 = "NS_PROVIDER_SAMPLE";
47
48     private Context                  mContext            = null;
49     private OcResourceHandle         mResourceHandle;
50     private ProviderService          ioTNotification     = null;
51     private HashMap<String, Integer> msgMap;
52
53     private Handler                  mHandler            = null;
54
55     private static final int         CONSUMER_SUBSCRIBED = 1;
56     private static final int         MESSAGE_SYNC        = 2;
57
58     private static final int         SYNC_READ           = 0;
59     private static final int         SYNC_DISMISS        = 1;
60     private static final int         SYNC_UNREAD         = 2;
61     private boolean                  gAcceptor;
62     private Consumer                 gConsumer;
63
64     public ProviderSample(Context context) {
65         Log.i(TAG, "Create providerSample Instance");
66
67         this.msgMap = new HashMap<>();
68         this.mContext = context;
69         ioTNotification = ProviderService.getInstance();
70     }
71
72     public void setHandler(Handler handler) {
73         this.mHandler = handler;
74     }
75
76     private void configurePlatform() {
77
78         PlatformConfig platformConfig = new PlatformConfig(mContext,
79                 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0",
80                 0, // Uses randomly available port
81                 QualityOfService.LOW);
82
83         Log.i(TAG, "Configuring platform.");
84         OcPlatform.Configure(platformConfig);
85         try {
86             OcPlatform.stopPresence(); // Initialize OcPlatform
87         } catch (Exception e) {
88             Log.e(TAG, "Exception: stopping presence when configuration step: "
89                     + e);
90         }
91         Log.i(TAG, "Configuration done Successfully");
92     }
93
94     public void start(boolean policy) {
95         Log.i(TAG, "Start ProviderService -IN");
96         configurePlatform();
97         gAcceptor = policy;
98         try {
99             ioTNotification.start(this, this, policy, "Info", false);
100         } catch (Exception e) {
101             Log.e(TAG, "Exception: start : " + e);
102         }
103         Log.i(TAG, "Start ProviderService - OUT");
104     }
105
106     public void registerTopic() {
107         Log.i(TAG, "Register Topics -IN");
108         try {
109             ioTNotification.registerTopic("OCF_TOPIC1");
110             ioTNotification.registerTopic("OCF_TOPIC2");
111             ioTNotification.registerTopic("OCF_TOPIC3");
112             ioTNotification.registerTopic("OCF_TOPIC4");
113         } catch (Exception e) {
114             Log.e(TAG, "Exception: registerTopic  : "+ e);
115         }
116
117         Log.i(TAG, "Start ProviderService - OUT");
118     }
119
120     public void setTopic() {
121         Log.i(TAG, "Set Topic -IN");
122         if (gConsumer == null) {
123             return;
124         }
125         try {
126             gConsumer.setTopic("OCF_TOPIC1");
127             gConsumer.setTopic("OCF_TOPIC2");
128             gConsumer.setTopic("OCF_TOPIC3");
129             gConsumer.setTopic("OCF_TOPIC4");
130         } catch (Exception e) {
131             Log.e(TAG, "Exception: setTopic : " + e);
132         }
133
134         Log.i(TAG, "setTopic ProviderService - OUT");
135         return;
136     }
137
138     public void stop() {
139         Log.i(TAG, "Stop ProviderService - IN");
140         try {
141             OcPlatform.stopPresence();
142         } catch (Exception e) {
143             Log.e(TAG,"Exception: stopping presence when terminating NS server: "+ e);
144         }
145         try {
146             ioTNotification.stop();
147         } catch (Exception e) {
148             Log.e(TAG, "Exception: stop : " + e);
149         }
150
151         Log.i(TAG, "Stop ProviderService - OUT");
152     }
153
154     public Message createMessage() {
155         Log.i(TAG, "createMessage ProviderService - IN");
156         Message message = null;
157         try {
158             message = ioTNotification.createMessage();
159         } catch (Exception e) {
160             Log.e(TAG, "Exception: createMessage : " + e);
161         }
162         Log.i(TAG, "createMessage ProviderService - OUT");
163         return message;
164     }
165
166     public void sendMessage(Message notiMessage) {
167         Log.i(TAG, "SendMessage ProviderService - IN");
168
169         try {
170             ioTNotification.sendMessage(notiMessage);
171         } catch (Exception e) {
172             Log.e(TAG, "Exception: sendMessage : " + e);
173         }
174
175         Log.i(TAG, "SendMessage ProviderService - OUT");
176         mHandler.post(new Runnable() {
177             @Override
178             public void run() {
179                 Toast.makeText(mContext, "Notification sent",
180                         Toast.LENGTH_SHORT).show();
181             }
182         });
183     }
184
185     public void sendSyncInfo(long messageId, SyncInfo.SyncType syncType) {
186         Log.i(TAG, "SendSyncInfo ProviderService - IN");
187         if (msgMap.containsKey(messageId)) {
188             if (msgMap.get(messageId) == SYNC_UNREAD) {
189                 try {
190                     ioTNotification.sendSyncInfo(messageId, syncType);
191                     Log.i(TAG, "Notification Sync ");
192                 } catch (Exception e) {
193                     Log.e(TAG, "Exception: sendSyncInfo : " + e);
194                 }
195                 Log.i(TAG, "SendSyncInfo ProviderService - OUT");
196                 msgMap.put("ID: " + messageId, SYNC_READ);
197             }
198         }
199     }
200
201     public void enableRemoteService(String servAdd) {
202         Log.i(TAG, "EnableRemoteService ProviderService - IN");
203         try {
204             ioTNotification.enableRemoteService(servAdd);
205         } catch (Exception e) {
206             Log.e(TAG, "Exception: enableRemoteService : " + e);
207         }
208         Log.i(TAG, "EnableRemoteService ProviderService - OUT");
209         return;
210     }
211
212     public void disableRemoteService(String servAdd) {
213         Log.i(TAG, "DisableRemoteService ProviderService - IN");
214         try {
215             ioTNotification.disableRemoteService(servAdd);
216         } catch (Exception e) {
217             Log.e(TAG, "Exception: disableRemoteService : " + e);
218         }
219         Log.i(TAG, "DisableRemoteService ProviderService - OUT");
220         return;
221     }
222
223     public void subscribeMQService(String servAdd, String topicName) {
224         Log.i(TAG, "SubscribeMQService ProviderService - IN");
225         try {
226             ioTNotification.subscribeMQService(servAdd, topicName);
227         } catch (Exception e) {
228             Log.e(TAG, "Exception: subscribeMQService : " + e);
229         }
230         Log.i(TAG, "SubscribeMQService ProviderService - OUT");
231         return;
232     }
233
234     public void acceptSubscription(Consumer consumer, boolean accepted) {
235         Log.i(TAG, "AcceptSubscription ProviderService - IN");
236         try {
237             consumer.acceptSubscription(accepted);
238         } catch (Exception e) {
239             Log.e(TAG, "Exception: acceptSubscription : " + e);
240         }
241         Log.i(TAG, "AcceptSubscription ProviderService - OUT");
242     }
243
244     @Override
245     public void onConsumerSubscribed(Consumer consumer) {
246         Log.i(TAG, "onConsumerSubscribed - IN");
247         gConsumer = consumer;
248         acceptSubscription(consumer, true);
249         android.os.Message msg = mHandler.obtainMessage(CONSUMER_SUBSCRIBED,
250                 "Consumer Id: " + consumer.getConsumerId());
251         mHandler.sendMessage(msg);
252         Log.i(TAG, "onConsumerSubscribed - OUT");
253     }
254
255     @Override
256     public void onMessageSynchronized(SyncInfo syncInfo) {
257         Log.i(TAG,
258                 "Received SyncInfo with messageID: " + syncInfo.getMessageId());
259         android.os.Message msg = mHandler.obtainMessage(MESSAGE_SYNC,
260                 "Message Id: " + syncInfo.getMessageId());
261         mHandler.sendMessage(msg);
262     }
263
264     public HashMap<String, Integer> getMsgMap() {
265         return msgMap;
266     }
267 }