2 //******************************************************************
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package com.sec.notiproviderexample;
25 import android.content.Context;
27 import android.util.Log;
28 import android.widget.Toast;
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.*;
41 import java.util.HashMap;
43 public class ProviderSample
44 implements ProviderService.OnConsumerSubscribedListener, ProviderService.OnMessageSynchronizedListener{
46 private static final String TAG = "NS_PROVIDER_PROXY";
48 private Context mContext = null;
49 private OcResourceHandle mResourceHandle; //resource handle
50 private ProviderService ioTNotification = null;
51 private HashMap<String, Integer> msgMap;
53 private Handler mHandler = null;
55 private static final int CONSUMER_SUBSCRIBED = 1;
56 private static final int MESSAGE_SYNC = 2;
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;
64 public ProviderSample(Context context) {
65 Log.i(TAG, "Create providerSample Instance");
67 this.msgMap = new HashMap<>();
68 this.mContext = context;
69 ioTNotification = ProviderService.getInstance();
72 public void setHandler(Handler handler)
74 this.mHandler = handler;
77 private void configurePlatform() {
79 PlatformConfig platformConfig = new PlatformConfig(
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
88 Log.i(TAG, "Configuring platform.");
89 OcPlatform.Configure(platformConfig);
91 OcPlatform.stopPresence(); // Initialize OcPlatform
92 } catch(Exception e) {
93 Log.e(TAG, "Exception: stopping presence when configuration step: " + e);
95 Log.i(TAG, "Configuration done Successfully");
98 public void Start(boolean policy)
100 Log.i(TAG, "Start ProviderService -IN");
104 int result = ioTNotification.start(this, this, policy, "Info", false);
105 Log.i(TAG, "Notification Start: " + result);
111 Log.i(TAG, "Start ProviderService - OUT");
114 public void RegisterTopic()
116 Log.i(TAG, "Register Topics -IN");
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);
131 Log.i(TAG, "Start ProviderService - OUT");
134 public int SetTopic()
136 Log.i(TAG, "Set Topic -IN");
137 if(gConsumer == null){
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);
154 Log.i(TAG, "Start ProviderService - OUT");
159 Log.i(TAG, "Stop ProviderService - IN");
161 OcPlatform.stopPresence();
162 } catch (Exception e) {
163 Log.e(TAG, "Exception: stopping presence when terminating NS server: " + e);
166 int result = ioTNotification.stop();
167 Log.i(TAG, "Notification Stop: " + result);
173 Log.i(TAG, "Stop ProviderService - OUT");
177 public void SendMessage(Message notiMessage) {
178 Log.i(TAG, "SendMessage ProviderService - IN");
181 int result = ioTNotification.sendMessage(notiMessage);
182 Log.i(TAG, "Notification Send Message: " + result);
188 Log.i(TAG, "SendMessage ProviderService - OUT");
189 mHandler.post(new Runnable() {
192 Toast.makeText(mContext, "Notification sent", Toast.LENGTH_SHORT).show();
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)
203 ioTNotification.sendSyncInfo(messageId, syncType);
204 Log.i(TAG, "Notification Sync " );
209 Log.i(TAG, "SendSyncInfo ProviderService - OUT");
210 msgMap.put("ID: "+messageId, SYNC_READ);
215 public void EnableRemoteService(String servAdd) {
216 Log.i(TAG, "EnableRemoteService ProviderService - IN");
218 int result = ioTNotification.enableRemoteService(servAdd);
219 Log.i(TAG, "Notification EnableRemoteService: "+ result );
224 Log.i(TAG, "EnableRemoteService ProviderService - OUT");
227 public void DisableRemoteService(String servAdd) {
228 Log.i(TAG, "DisableRemoteService ProviderService - IN");
230 int result = ioTNotification.disableRemoteService(servAdd);
231 Log.i(TAG, "Notification DisableRemoteService: "+ result );
236 Log.i(TAG, "DisableRemoteService ProviderService - OUT");
239 public void AcceptSubscription(Consumer consumer, boolean accepted)
241 Log.i(TAG,"AcceptSubscription ProviderService - IN");
243 int result = consumer.acceptSubscription(accepted);
244 Log.i(TAG, "Notification AcceptSubscription: "+result );
249 Log.i(TAG, "AcceptSubscription ProviderService - OUT");
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");
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);
271 public HashMap<String, Integer> getMsgMap() {