Remove unused pkg dependancy
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiConsumerExample / app / src / main / java / org / iotivity / service / ns / sample / consumer / ConsumerSample.java
1 /******************************************************************
2  * Copyright 2016 Samsung Electronics All Rights Reserved.
3  * <p>
4  * <p>
5  * <p>
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * <p>
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * <p>
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ******************************************************************/
18
19 package org.iotivity.service.ns.sample.consumer;
20
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.util.Log;
25
26 import org.iotivity.base.ModeType;
27 import org.iotivity.base.OcPlatform;
28 import org.iotivity.base.PlatformConfig;
29 import org.iotivity.base.QualityOfService;
30 import org.iotivity.base.ServiceType;
31 import org.iotivity.service.ns.common.NSException;
32 import org.iotivity.service.ns.common.SyncInfo;
33 import org.iotivity.service.ns.common.TopicsList;
34 import org.iotivity.service.ns.common.Topic;
35 import org.iotivity.service.ns.consumer.ConsumerService;
36 import org.iotivity.service.ns.consumer.Provider;
37
38 public class ConsumerSample
39         implements ConsumerService.OnProviderDiscoveredListener,
40         Provider.OnProviderStateListener, Provider.OnMessageReceivedListener,
41         Provider.OnSyncInfoReceivedListener {
42     private static final String TAG                 = "NS_CONSUMER_SAMPLE";
43
44     private Context             mContext            = null;
45     private ConsumerService     consumerService     = null;
46     private boolean             mAcceptor           = true;
47     private Provider            mProvider           = null;
48
49     private Handler             mHandler            = null;
50
51     private static final int    PROVIDER_DISCOVERED = 1;
52     private static final int    STATE_CHANGED       = 2;
53     private static final int    MESSAGE_RECEIVED    = 3;
54     private static final int    SYNCINFO_RECEIVED   = 4;
55     private static final int    TOPICS_RECEIVED     = 5;
56
57     public ConsumerSample(Context context) {
58         Log.i(TAG, "Create consumer sample Instance");
59
60         this.mContext = context;
61         consumerService = new ConsumerService();
62     }
63
64     public void setHandler(Handler handler) {
65         this.mHandler = handler;
66     }
67
68     public boolean getAcceptor() {
69         return mAcceptor;
70     }
71
72     private void configurePlatform() {
73
74         PlatformConfig platformConfig = new PlatformConfig(mContext,
75                 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0",
76                 0, // Uses randomly available port
77                 QualityOfService.LOW);
78
79         Log.i(TAG, "Configuring platform.");
80         OcPlatform.Configure(platformConfig);
81         try {
82             OcPlatform.stopPresence(); // Initialize OcPlatform
83         } catch (Exception e) {
84             Log.e(TAG, "Exception: stopping presence when configuration step: "
85                     + e);
86         }
87         Log.i(TAG, "Configuration done Successfully");
88     }
89
90     public void startNotificationConsumer() {
91         configurePlatform();
92         try {
93             consumerService.start(this);
94         } catch (Exception e) {
95             Log.e(TAG, "Exception: startNotificationConsumer : " + e);
96         }
97     }
98
99     public void stopNotificationConsumer() {
100         try {
101             consumerService.stop();
102             mProvider = null;
103         } catch (Exception e) {
104             Log.e(TAG, "Exception: stopNotificationConsumer : " + e);
105         }
106     }
107
108     public void enableRemoteService(String serverAddress) {
109         try {
110             consumerService.enableRemoteService(serverAddress);
111         } catch (NSException e) {
112             Log.e(TAG, "NSException: enableRemoteService : " + e);
113         }
114     }
115
116     public void subscribeMQService(String servAdd, String topicName) {
117         Log.i(TAG, "SubscribeMQService  - IN");
118         try {
119             consumerService.subscribeMQService(servAdd, topicName);
120         } catch (Exception e) {
121             Log.e(TAG, "Exception: subscribeMQService : " + e);
122         }
123         Log.i(TAG, "SubscribeMQService  - OUT");
124         return;
125     }
126
127     public void rescanProvider() {
128         try {
129             consumerService.rescanProvider();
130         } catch (Exception e) {
131             Log.e(TAG, "Exception: rescanProvider : " + e);
132         }
133     }
134
135     public void subscribe() {
136         try {
137             mProvider.subscribe();
138             Log.i(TAG, "Notification consumer subscribe: " );
139         } catch (Exception e) {
140             Log.e(TAG, "Exception: subscribe : " + e);
141         }
142     }
143
144     public void unsubscribe() {
145         try {
146             mProvider.unsubscribe();
147             Log.i(TAG, "Notification consumer unsubscribe: ");
148         } catch (Exception e) {
149             Log.e(TAG, "Exception: unsubscribe : " + e);
150         }
151     }
152     public void getTopicsList() {
153         if (mProvider != null) {
154             try {
155                 TopicsList topicsList = mProvider.getTopicList();
156                 StringBuilder stringBuilder = new StringBuilder();
157                 stringBuilder.append("TopicList Received :");
158                 for (Topic topic : topicsList.getTopicsList()) {
159                     Log.i(TAG, "Topic Name : " + topic.getTopicName());
160                     Log.i(TAG, "Topic State : " + topic.getState());
161                     stringBuilder
162                             .append("\nTopic Name : " + topic.getTopicName());
163                     stringBuilder.append("\nTopic State : " + topic.getState());
164                 }
165                 Message msg = mHandler.obtainMessage(TOPICS_RECEIVED,
166                         stringBuilder.toString());
167                 mHandler.sendMessage(msg);
168             } catch (Exception e) {
169                 Log.e(TAG, "Exception: getTopicsList : " + e);
170             }
171         } else {
172             Log.e(TAG, "getTopicsList Provider NULL");
173         }
174     }
175
176     public void updateTopicList(TopicsList topicsList) {
177         if (mProvider != null) {
178             try {
179                 mProvider.updateTopicList(topicsList);
180             } catch (Exception e) {
181                 Log.e(TAG, "Exception: updateTopicList : " + e);
182             }
183         } else {
184             Log.e(TAG, "updateTopicList Provider NULL");
185         }
186     }
187
188     @Override
189     public void onProviderDiscovered(Provider provider) {
190         Log.i(TAG, "onProviderDiscovered");
191         if (provider == null) {
192             Log.e(TAG, "providerID is Null  ");
193             return;
194         }
195         mProvider = provider;
196         Log.i(TAG, "Provider ID: " + provider.getProviderId());
197         Message msg = mHandler.obtainMessage(PROVIDER_DISCOVERED,
198                 "Provider Discovered Id: " + provider.getProviderId());
199         mHandler.sendMessage(msg);
200         try {
201             Log.i(TAG, "setListeners to Discovered Provider");
202             provider.setListener(this, this, this);
203             Log.i(TAG, "setListeners done");
204             if (!provider.isSubscribed()) {
205                 Log.i(TAG, "Provider not subscribed. Acceptor is Consumer");
206                 mAcceptor = false;
207                 provider.subscribe();
208             } else {
209                 Log.i(TAG,
210                         "Provider is already subscribed. Acceptor is Provider");
211                 mAcceptor = true;
212             }
213         } catch (Exception e) {
214             Log.e(TAG, "Exception : " + e);
215         }
216     }
217
218     @Override
219     public void onProviderStateReceived(Provider.ProviderState state) {
220         Log.i(TAG, "onProviderStateReceived");
221
222         Log.i(TAG, "ProviderState Received : " + state);
223         Message msg = mHandler.obtainMessage(STATE_CHANGED,
224                 "ProviderState Received : " + state);
225         mHandler.sendMessage(msg);
226     }
227
228     @Override
229     public void onMessageReceived(
230             org.iotivity.service.ns.common.Message message) {
231         Log.i(TAG, "onMessageReceived");
232
233         Log.i(TAG, "Message Id: " + message.getMessageId());
234         Log.i(TAG, "Message title: " + message.getTitle());
235         Log.i(TAG, "Message Content: " + message.getContentText());
236         Log.i(TAG, "Message Topic: " + message.getTopic());
237         Log.i(TAG, "Message Source: " + message.getSourceName());
238
239         Message msg = mHandler.obtainMessage(MESSAGE_RECEIVED,
240                 "Message Id: " + message.getMessageId() + "\n"
241                         + "Message title: " + message.getTitle() + "\n"
242                         + "Message Content: " + message.getContentText() + "\n"
243                         + "Message Topic: " + message.getTopic() + "\n"
244                         + "Message Source: " + message.getSourceName());
245         mHandler.sendMessage(msg);
246         try {
247             Log.i(TAG, "send READ syncInfo");
248             mProvider.sendSyncInfo(message.getMessageId(),
249                     SyncInfo.SyncType.READ);
250         } catch (Exception e) {
251             Log.e(TAG, "Exception : " + e);
252         }
253     }
254
255     @Override
256     public void onSyncInfoReceived(SyncInfo sync) {
257         Log.i(TAG, "onSyncInfoReceived");
258
259         Log.i(TAG, "Sync Id: " + sync.getMessageId());
260         Log.i(TAG, "Sync STATE: " + sync.getState());
261         Message msg = mHandler.obtainMessage(SYNCINFO_RECEIVED,
262                 "Sync Id: " + sync.getMessageId() + "\n" + "Sync STATE: "
263                         + sync.getState());
264         mHandler.sendMessage(msg);
265     }
266 }