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