From: Abitha Shankar Date: Mon, 19 Sep 2016 06:08:10 +0000 (+0530) Subject: Added UI modifications and API testing code for Notification Service Consumer Sample. X-Git-Tag: 1.3.0~1022^2^2~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8d133246295580dd2ac92436a2b5787917204f6;p=platform%2Fupstream%2Fiotivity.git Added UI modifications and API testing code for Notification Service Consumer Sample. patch 2 : updated the file name from review comments patch 3 : removed dependencies from application Change-Id: Ie52324bc8f0601b73483ec066093356d86641293 Signed-off-by: Abitha Shankar Reviewed-on: https://gerrit.iotivity.org/gerrit/11911 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka Reviewed-by: Uze Choi --- diff --git a/service/notification/examples/android/NotiConsumerExample/app/build.gradle b/service/notification/examples/android/NotiConsumerExample/app/build.gradle index 9023618..c7db51d 100755 --- a/service/notification/examples/android/NotiConsumerExample/app/build.gradle +++ b/service/notification/examples/android/NotiConsumerExample/app/build.gradle @@ -16,6 +16,4 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.1' - compile project(':iotivity-base-armeabi-release') - compile project(':iotivity-armeabi-notification-service-release') } diff --git a/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerProxy.java b/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerSample.java similarity index 87% rename from service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerProxy.java rename to service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerSample.java index 175356d..13f1908 100755 --- a/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerProxy.java +++ b/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/ConsumerSample.java @@ -1,278 +1,285 @@ -/****************************************************************** - * 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.noticonsumerexample; - -import android.app.NotificationManager; -import android.content.Context; -import android.os.Handler; -import android.os.Message; -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.NSException; -import org.iotivity.service.ns.common.SyncInfo; -import org.iotivity.service.ns.common.TopicsList; -import org.iotivity.service.ns.consumer.ConsumerService; -import org.iotivity.service.ns.consumer.Provider; - -import java.util.HashMap; - -public class ConsumerProxy - implements ConsumerService.OnProviderDiscoveredListener, - Provider.OnProviderStateListener, - Provider.OnMessageReceivedListner, Provider.OnSyncInfoReceivedListner -{ - private static final String TAG = "NS_CONSUMER_PROXY"; - - private Context mContext = null; - private ConsumerService consumerService = null; - private boolean mAcceptor = false; - 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 ConsumerProxy(Context context) - { - Log.i(TAG, "Create consumerProxy 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 (Exception e) - { - Log.e(TAG, "Exception: 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(); - } - 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, "Provider state: " + state ); - Message msg = mHandler.obtainMessage(STATE_CHANGED, "Provider state: " + 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); - } -} +/****************************************************************** + * 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.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); + } +} diff --git a/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/MainActivity.java b/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/MainActivity.java index 7d9e463..d79c440 100755 --- a/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/MainActivity.java +++ b/service/notification/examples/android/NotiConsumerExample/app/src/main/java/com/sec/noticonsumerexample/MainActivity.java @@ -18,9 +18,6 @@ package com.sec.noticonsumerexample; -import android.app.Notification; -import android.app.NotificationManager; -import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -28,11 +25,11 @@ import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; -import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import org.iotivity.service.ns.common.TopicsList; +import org.iotivity.service.ns.common.Topic; public class MainActivity extends AppCompatActivity { @@ -44,12 +41,12 @@ public class MainActivity extends AppCompatActivity private Button btnEnableRemoteService; private Button btnGetTopicList; private Button btnUpdateTopicList; + private Button btnClearLog; private static TextView TvLog; private boolean isStarted = false; - private String consumerId; - private ConsumerProxy mConsumerProxy = null; + private ConsumerSample mConsumerSample = null; private static final int PROVIDER_DISCOVERED = 1; private static final int STATE_CHANGED = 2; @@ -100,6 +97,15 @@ public class MainActivity extends AppCompatActivity } break; } + case TOPICS_RECEIVED: + { + String topicList = (String) msg.obj; + if (topicList != null) + { + TvLog.append( topicList + "\n"); + } + break; + } default: break; } @@ -130,6 +136,7 @@ public class MainActivity extends AppCompatActivity btnEnableRemoteService = (Button) findViewById(R.id.BtnEnableRemoteService); btnGetTopicList = (Button) findViewById(R.id.BtnGetTopicList); btnUpdateTopicList = (Button) findViewById(R.id.BtnUpdateTopicList); + btnClearLog = (Button) findViewById(R.id.BtnClearLog); TvLog = (TextView) findViewById(R.id.TvLog); @@ -139,24 +146,20 @@ public class MainActivity extends AppCompatActivity btnEnableRemoteService.setOnClickListener(mClickListener); btnGetTopicList.setOnClickListener(mClickListener); btnUpdateTopicList.setOnClickListener(mClickListener); + btnClearLog.setOnClickListener(mClickListener); - mConsumerProxy = new ConsumerProxy(getApplicationContext()); - mConsumerProxy.setHandler(mHandler); + mConsumerSample = new ConsumerSample(getApplicationContext()); + mConsumerSample.setHandler(mHandler); } @Override protected void onDestroy() { if (isStarted) - mConsumerProxy.stopNotificationConsumer(); + mConsumerSample.stopNotificationConsumer(); super.onDestroy(); } - public ConsumerProxy getProviderProxy() - { - return mConsumerProxy; - } - Button.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) @@ -165,84 +168,103 @@ public class MainActivity extends AppCompatActivity { case R.id.BtnStart: { - if (isStarted == false) + if (!isStarted) { Log.i(TAG, "Start NS Consumer Service"); TvLog.setText("Start NS-Consumer\n"); - mConsumerProxy.startNotificationConsumer(); + mConsumerSample.startNotificationConsumer(); isStarted = true; } else { - Log.e(TAG, "NS Consumer Service had already started"); + Log.e(TAG, "NS Consumer Service has already started"); + showToast("Error : Service has already started"); } } break; case R.id.BtnStop: { - if (isStarted == false) + if (!isStarted) { Log.e(TAG, "Fail to stop service. Service has not been started"); + showToast("Error : Service has not been started"); break; } TvLog.append("Stop NS-Consumer\n"); - mConsumerProxy.stopNotificationConsumer(); + mConsumerSample.stopNotificationConsumer(); isStarted = false; } break; case R.id.BtnRescan: { - if (isStarted == false) + if (!isStarted) { Log.e(TAG, "Fail to rescan. Service has not been started"); + showToast("Error : Service has not been started"); break; } - TvLog.append("Rescan NS-Consumer\n"); - mConsumerProxy.rescanProvider(); + mConsumerSample.rescanProvider(); } break; case R.id.BtnEnableRemoteService: { - if (isStarted == false) + if (!isStarted) { Log.e(TAG, "Fail to Enable RemoteService. Service has not been started"); + showToast("Error : Service has not been started"); break; } TvLog.append("EnableRemoteService NS-Consumer\n"); //TODO: Update to read the serverAddress from UI String serverAddress = new String(); - mConsumerProxy.enableRemoteService(serverAddress); + mConsumerSample.enableRemoteService(serverAddress); } break; case R.id.BtnGetTopicList: { - if (isStarted == false) + if (!isStarted) { Log.e(TAG, "Fail to GetTopicList. Service has not been started"); + showToast("Error : Service has not been started"); break; } TvLog.append("GetTopicList NS-Consumer\n"); - mConsumerProxy.getTopicsList(); + mConsumerSample.getTopicsList(); } break; case R.id.BtnUpdateTopicList: { - if (isStarted == false) + if (!isStarted) { Log.e(TAG, "Fail to UpdateTopicList. Service has not been started"); + showToast("Error : Service has not been started"); + break; + } + if(mConsumerSample.getAcceptor()) + { + Log.e(TAG, "Operation Not Allowed. ProviderService Acceptor is not Consumer"); + showToast("Operation Not Allowed. ProviderService Acceptor is not Consumer"); break; } TvLog.append("UpdateTopicList NS-Consumer\n"); - //TODO: Update to read the TopicsList from UI - TopicsList topicsList = new TopicsList(); - mConsumerProxy.updateTopicList(topicsList); + TopicsList topicList = new TopicsList(); + topicList.addTopic("OCF_TOPIC1", Topic.TopicState.SUBSCRIBED); + topicList.addTopic("OCF_TOPIC2", Topic.TopicState.SUBSCRIBED); + topicList.addTopic("OCF_TOPIC3", Topic.TopicState.UNSUBSCRIBED); + + mConsumerSample.updateTopicList(topicList); } break; + case R.id.BtnClearLog: + { + TvLog.setText(""); + } + break; } } }; diff --git a/service/notification/examples/android/NotiConsumerExample/app/src/main/res/layout/activity_main.xml b/service/notification/examples/android/NotiConsumerExample/app/src/main/res/layout/activity_main.xml index 2a93adc..b924bc2 100755 --- a/service/notification/examples/android/NotiConsumerExample/app/src/main/res/layout/activity_main.xml +++ b/service/notification/examples/android/NotiConsumerExample/app/src/main/res/layout/activity_main.xml @@ -12,8 +12,8 @@ +