--- /dev/null
+/******************************************************************
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************/
+
+package com.sec.noticonsumerexample;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+
+import org.iotivity.base.ModeType;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.PlatformConfig;
+import org.iotivity.base.QualityOfService;
+import org.iotivity.base.ServiceType;
+import org.iotivity.service.ns.common.NSException;
+import org.iotivity.service.ns.common.SyncInfo;
+import org.iotivity.service.ns.common.TopicsList;
+import org.iotivity.service.ns.common.Topic;
+import org.iotivity.service.ns.consumer.ConsumerService;
+import org.iotivity.service.ns.consumer.Provider;
+
+public class ConsumerSample
+ implements ConsumerService.OnProviderDiscoveredListener,
+ Provider.OnProviderStateListener,
+ Provider.OnMessageReceivedListner, Provider.OnSyncInfoReceivedListner
+{
+ private static final String TAG = "NS_CONSUMER_SAMPLE";
+
+ private Context mContext = null;
+ private ConsumerService consumerService = null;
+ private boolean mAcceptor = true;
+ private Provider mProvider = null;
+
+ private Handler mHandler = null;
+
+ private static final int PROVIDER_DISCOVERED = 1;
+ private static final int STATE_CHANGED = 2;
+ private static final int MESSAGE_RECEIVED = 3;
+ private static final int SYNCINFO_RECEIVED = 4;
+ private static final int TOPICS_RECEIVED = 5;
+
+ public ConsumerSample(Context context)
+ {
+ Log.i(TAG, "Create consumer sample Instance");
+
+ this.mContext = context;
+ consumerService = new ConsumerService();
+ }
+
+ public void setHandler(Handler handler)
+ {
+ this.mHandler = handler;
+ }
+
+ public boolean getAcceptor()
+ {
+ return mAcceptor;
+ }
+
+ private void configurePlatform()
+ {
+
+ PlatformConfig platformConfig = new PlatformConfig(
+ mContext,
+ ServiceType.IN_PROC,
+ ModeType.CLIENT_SERVER,
+ "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
+ 0, // Uses randomly available port
+ QualityOfService.LOW
+ );
+
+ Log.i(TAG, "Configuring platform.");
+ OcPlatform.Configure(platformConfig);
+ try
+ {
+ OcPlatform.stopPresence(); // Initialize OcPlatform
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: stopping presence when configuration step: " + e);
+ }
+ Log.i(TAG, "Configuration done Successfully");
+ }
+
+ public void startNotificationConsumer()
+ {
+ configurePlatform();
+ try
+ {
+ consumerService.start(this);
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: startNotificationConsumer : " + e);
+ }
+ }
+
+ public void stopNotificationConsumer()
+ {
+ try
+ {
+ consumerService.stop();
+ mProvider = null;
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: stopNotificationConsumer : " + e);
+ }
+ }
+
+ public void enableRemoteService(String serverAddress)
+ {
+ try
+ {
+ consumerService.enableRemoteService(serverAddress);
+ }
+ catch (NSException e)
+ {
+ Log.e(TAG, "NSException: enableRemoteService : " + e);
+ }
+ }
+
+ public void rescanProvider()
+ {
+ try
+ {
+ consumerService.rescanProvider();
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: rescanProvider : " + e);
+ }
+ }
+ public void getTopicsList()
+ {
+ if (mProvider != null)
+ {
+ try
+ {
+ TopicsList topicsList = mProvider.getTopicList();
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("TopicList Received :");
+ for(Topic topic : topicsList.getTopicsList())
+ {
+ Log.i(TAG, "Topic Name : " + topic.getTopicName() );
+ Log.i(TAG, "Topic State : " + topic.getState() );
+ stringBuilder.append("\nTopic Name : " + topic.getTopicName());
+ stringBuilder.append("\nTopic State : " + topic.getState());
+ }
+ Message msg = mHandler.obtainMessage(TOPICS_RECEIVED, stringBuilder.toString());
+ mHandler.sendMessage(msg);
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: getTopicsList : " + e);
+ }
+ }
+ else
+ {
+ Log.e(TAG, "getTopicsList Provider NULL");
+ }
+ }
+ public void updateTopicList(TopicsList topicsList)
+ {
+ if (mProvider != null)
+ {
+ try
+ {
+ mProvider.updateTopicList(topicsList);
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception: updateTopicList : " + e);
+ }
+ }
+ else
+ {
+ Log.e(TAG, "updateTopicList Provider NULL");
+ }
+ }
+ @Override
+ public void onProviderDiscovered(Provider provider)
+ {
+ Log.i(TAG, "onProviderDiscovered");
+ if (provider == null)
+ {
+ Log.e(TAG, "providerID is Null ");
+ return;
+ }
+ mProvider = provider;
+ Log.i(TAG, "Provider ID: " + provider.getProviderId() );
+ Message msg = mHandler.obtainMessage(PROVIDER_DISCOVERED,
+ "Provider Discovered Id: " + provider.getProviderId());
+ mHandler.sendMessage(msg);
+ try
+ {
+ Log.i(TAG, "setListeners to Discovered Provider");
+ provider.setListener(this, this, this);
+ Log.i(TAG, "setListeners done");
+ if (! provider.isSubscribed())
+ {
+ Log.i(TAG, "Provider not subscribed. Acceptor is Consumer");
+ mAcceptor = false;
+ provider.subscribe();
+ }
+ else
+ {
+ Log.i(TAG, "Provider is already subscribed. Acceptor is Provider");
+ mAcceptor = true;
+ }
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception : " + e);
+ }
+ }
+
+ @Override
+ public void onProviderStateReceived(Provider.ProviderState state)
+ {
+ Log.i(TAG, "onProviderStateReceived");
+
+ Log.i(TAG, "ProviderState Received : " + state );
+ Message msg = mHandler.obtainMessage(STATE_CHANGED, "ProviderState Received : " + state);
+ mHandler.sendMessage(msg);
+ }
+
+ @Override
+ public void onMessageReceived(org.iotivity.service.ns.common.Message message)
+ {
+ Log.i(TAG, "onMessageReceived");
+
+ Log.i(TAG, "Message Id: " + message.getMessageId());
+ Log.i(TAG, "Message title: " + message.getTitle());
+ Log.i(TAG, "Message Content: " + message.getContentText());
+ Log.i(TAG, "Message Topic: " + message.getTopic());
+ Log.i(TAG, "Message Source: " + message.getSourceName());
+
+ Message msg = mHandler.obtainMessage(MESSAGE_RECEIVED,
+ "Message Id: " + message.getMessageId() + "\n" +
+ "Message title: " + message.getTitle() + "\n" +
+ "Message Content: " + message.getContentText() + "\n" +
+ "Message Topic: " + message.getTopic() + "\n" +
+ "Message Source: " + message.getSourceName() );
+ mHandler.sendMessage(msg);
+ try
+ {
+ Log.i(TAG, "send READ syncInfo");
+ mProvider.sendSyncInfo(message.getMessageId(), SyncInfo.SyncType.READ);
+ }
+ catch (Exception e)
+ {
+ Log.e(TAG, "Exception : " + e);
+ }
+ }
+
+ @Override
+ public void onSyncInfoReceived(SyncInfo sync)
+ {
+ Log.i(TAG, "onSyncInfoReceived");
+
+ Log.i(TAG, "Sync Id: " + sync.getMessageId());
+ Log.i(TAG, "Sync STATE: " + sync.getState());
+ Message msg = mHandler.obtainMessage(SYNCINFO_RECEIVED,
+ "Sync Id: " + sync.getMessageId() + "\n" +
+ "Sync STATE: " + sync.getState());
+ mHandler.sendMessage(msg);
+ }
+}
--- /dev/null
+/*
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package com.sec.notiproviderexample;
+
+import android.content.Context;
+import android.os.*;
+import android.util.Log;
+import android.widget.Toast;
+
+import org.iotivity.base.ModeType;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.OcResourceHandle;
+import org.iotivity.base.PlatformConfig;
+import org.iotivity.base.QualityOfService;
+import org.iotivity.base.ServiceType;
+import org.iotivity.service.ns.common.Message;
+import org.iotivity.service.ns.provider.*;
+import org.iotivity.service.ns.common.*;
+
+
+import java.util.HashMap;
+
+public class ProviderSample
+ implements ProviderService.OnConsumerSubscribedListener, ProviderService.OnMessageSynchronizedListener{
+
+ private static final String TAG = "NS_PROVIDER_PROXY";
+
+ private Context mContext = null;
+ private OcResourceHandle mResourceHandle; //resource handle
+ private ProviderService ioTNotification = null;
+ private HashMap<String, Integer> msgMap;
+
+ private Handler mHandler = null;
+
+ private static final int CONSUMER_SUBSCRIBED = 1;
+ private static final int MESSAGE_SYNC = 2;
+
+ private static final int SYNC_READ = 0;
+ private static final int SYNC_DISMISS = 1;
+ private static final int SYNC_UNREAD = 2;
+ private boolean gAcceptor;
+ private Consumer gConsumer;
+
+ public ProviderSample(Context context) {
+ Log.i(TAG, "Create providerSample Instance");
+
+ this.msgMap = new HashMap<>();
+ this.mContext = context;
+ ioTNotification = ProviderService.getInstance();
+ }
+
+ public void setHandler(Handler handler)
+ {
+ this.mHandler = handler;
+ }
+
+ private void configurePlatform() {
+
+ PlatformConfig platformConfig = new PlatformConfig(
+ mContext,
+ ServiceType.IN_PROC,
+ ModeType.CLIENT_SERVER,
+ "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
+ 0, // Uses randomly available port
+ QualityOfService.LOW
+ );
+
+ Log.i(TAG, "Configuring platform.");
+ OcPlatform.Configure(platformConfig);
+ try {
+ OcPlatform.stopPresence(); // Initialize OcPlatform
+ } catch(Exception e) {
+ Log.e(TAG, "Exception: stopping presence when configuration step: " + e);
+ }
+ Log.i(TAG, "Configuration done Successfully");
+ }
+
+ public void Start(boolean policy)
+ {
+ Log.i(TAG, "Start ProviderService -IN");
+ configurePlatform();
+ gAcceptor = policy;
+ try{
+ int result = ioTNotification.start(this, this, policy, "Info", false);
+ Log.i(TAG, "Notification Start: " + result);
+ }
+ catch(Exception e){
+
+ }
+
+ Log.i(TAG, "Start ProviderService - OUT");
+ }
+
+ public void RegisterTopic()
+ {
+ Log.i(TAG, "Register Topics -IN");
+ try{
+ int result = ioTNotification.registerTopic("OCF_TOPIC1");
+ Log.i(TAG, " RegisterTopic: " + result);
+ result = ioTNotification.registerTopic("OCF_TOPIC2");
+ Log.i(TAG, " RegisterTopic: " + result);
+ result = ioTNotification.registerTopic("OCF_TOPIC3");
+ Log.i(TAG, " RegisterTopic: " + result);
+ result = ioTNotification.registerTopic("OCF_TOPIC4");
+ Log.i(TAG, " RegisterTopic: " + result);
+ }
+ catch(Exception e){
+
+ }
+
+ Log.i(TAG, "Start ProviderService - OUT");
+ }
+
+ public int SetTopic()
+ {
+ Log.i(TAG, "Set Topic -IN");
+ if(gConsumer == null){
+ return 0;
+ }
+ try{
+ int result = gConsumer.setTopic("OCF_TOPIC1");
+ Log.i(TAG, " Set Topic : " + result);
+ result = gConsumer.setTopic("OCF_TOPIC2");
+ Log.i(TAG, " Set Topic : " + result);
+ result = gConsumer.setTopic("OCF_TOPIC3");
+ Log.i(TAG, " Set Topic : " + result);
+ result = gConsumer.setTopic("OCF_TOPIC4");
+ Log.i(TAG, " Set Topic : " + result);
+ }
+ catch(Exception e){
+
+ }
+
+ Log.i(TAG, "Start ProviderService - OUT");
+ return 1;
+ }
+
+ public void Stop() {
+ Log.i(TAG, "Stop ProviderService - IN");
+ try {
+ OcPlatform.stopPresence();
+ } catch (Exception e) {
+ Log.e(TAG, "Exception: stopping presence when terminating NS server: " + e);
+ }
+ try{
+ int result = ioTNotification.stop();
+ Log.i(TAG, "Notification Stop: " + result);
+ }
+ catch(Exception e){
+
+ }
+
+ Log.i(TAG, "Stop ProviderService - OUT");
+ }
+
+
+ public void SendMessage(Message notiMessage) {
+ Log.i(TAG, "SendMessage ProviderService - IN");
+
+ try{
+ int result = ioTNotification.sendMessage(notiMessage);
+ Log.i(TAG, "Notification Send Message: " + result);
+ }
+ catch(Exception e){
+
+ }
+
+ Log.i(TAG, "SendMessage ProviderService - OUT");
+ mHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ Toast.makeText(mContext, "Notification sent", Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+ public void SendSyncInfo(long messageId, SyncInfo.SyncType syncType) {
+ Log.i(TAG, "SendSyncInfo ProviderService - IN");
+ if(msgMap.containsKey(messageId)) {
+ if(msgMap.get(messageId) == SYNC_UNREAD)
+ {
+ try{
+ ioTNotification.sendSyncInfo(messageId, syncType);
+ Log.i(TAG, "Notification Sync " );
+ }
+ catch(Exception e) {
+
+ }
+ Log.i(TAG, "SendSyncInfo ProviderService - OUT");
+ msgMap.put("ID: "+messageId, SYNC_READ);
+ }
+ }
+ }
+
+ public void EnableRemoteService(String servAdd) {
+ Log.i(TAG, "EnableRemoteService ProviderService - IN");
+ try{
+ int result = ioTNotification.enableRemoteService(servAdd);
+ Log.i(TAG, "Notification EnableRemoteService: "+ result );
+ }
+ catch(Exception e) {
+
+ }
+ Log.i(TAG, "EnableRemoteService ProviderService - OUT");
+ }
+
+ public void DisableRemoteService(String servAdd) {
+ Log.i(TAG, "DisableRemoteService ProviderService - IN");
+ try{
+ int result = ioTNotification.disableRemoteService(servAdd);
+ Log.i(TAG, "Notification DisableRemoteService: "+ result );
+ }
+ catch(Exception e) {
+
+ }
+ Log.i(TAG, "DisableRemoteService ProviderService - OUT");
+ }
+
+ public void AcceptSubscription(Consumer consumer, boolean accepted)
+ {
+ Log.i(TAG,"AcceptSubscription ProviderService - IN");
+ try{
+ int result = consumer.acceptSubscription(accepted);
+ Log.i(TAG, "Notification AcceptSubscription: "+result );
+ }
+ catch(Exception e) {
+
+ }
+ Log.i(TAG, "AcceptSubscription ProviderService - OUT");
+ }
+
+ @Override
+ public void onConsumerSubscribed(Consumer consumer) {
+ Log.i(TAG, "onConsumerSubscribed - IN");
+ gConsumer = consumer;
+ AcceptSubscription(consumer, true);
+ android.os.Message msg = mHandler.obtainMessage(CONSUMER_SUBSCRIBED,
+ "Consumer Id: " + consumer.getConsumerId() );
+ mHandler.sendMessage(msg);
+ Log.i(TAG, "onConsumerSubscribed - OUT");
+ }
+
+ @Override
+ public void onMessageSynchronized(SyncInfo syncInfo) {
+ Log.i(TAG, "Received SyncInfo with messageID: "+syncInfo.getMessageId());
+ android.os.Message msg = mHandler.obtainMessage(MESSAGE_SYNC,
+ "Message Id: " + syncInfo.getMessageId() );
+ mHandler.sendMessage(msg);
+ }
+
+ public HashMap<String, Integer> getMsgMap() {
+ return msgMap;
+ }
+}