Remove unused pkg dependancy
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiConsumerExample / app / src / main / java / com / sec / noticonsumerexample / 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 com.sec.noticonsumerexample;
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,
41     Provider.OnMessageReceivedListner, Provider.OnSyncInfoReceivedListner
42 {
43     private static final String TAG = "NS_CONSUMER_SAMPLE";
44
45     private Context mContext = null;
46     private ConsumerService consumerService = null;
47     private boolean mAcceptor = true;
48     private Provider mProvider = null;
49
50     private Handler mHandler = null;
51
52     private static final int PROVIDER_DISCOVERED = 1;
53     private static final int STATE_CHANGED = 2;
54     private static final int MESSAGE_RECEIVED = 3;
55     private static final int SYNCINFO_RECEIVED = 4;
56     private static final int TOPICS_RECEIVED = 5;
57
58     public ConsumerSample(Context context)
59     {
60         Log.i(TAG, "Create consumer sample Instance");
61
62         this.mContext = context;
63         consumerService = new ConsumerService();
64     }
65
66     public void setHandler(Handler handler)
67     {
68         this.mHandler = handler;
69     }
70
71     public boolean getAcceptor()
72     {
73         return mAcceptor;
74     }
75
76     private void configurePlatform()
77     {
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         {
92             OcPlatform.stopPresence(); // Initialize OcPlatform
93         }
94         catch (Exception e)
95         {
96             Log.e(TAG, "Exception: stopping presence when configuration step: " + e);
97         }
98         Log.i(TAG, "Configuration done Successfully");
99     }
100
101     public void startNotificationConsumer()
102     {
103         configurePlatform();
104         try
105         {
106             consumerService.start(this);
107         }
108         catch (Exception e)
109         {
110             Log.e(TAG, "Exception: startNotificationConsumer : " + e);
111         }
112     }
113
114     public void stopNotificationConsumer()
115     {
116         try
117         {
118             consumerService.stop();
119             mProvider = null;
120         }
121         catch (Exception e)
122         {
123             Log.e(TAG, "Exception: stopNotificationConsumer : " + e);
124         }
125     }
126
127     public void enableRemoteService(String serverAddress)
128     {
129         try
130         {
131             consumerService.enableRemoteService(serverAddress);
132         }
133         catch (NSException e)
134         {
135             Log.e(TAG, "NSException: enableRemoteService : " + e);
136         }
137     }
138
139     public void rescanProvider()
140     {
141         try
142         {
143             consumerService.rescanProvider();
144         }
145         catch (Exception e)
146         {
147             Log.e(TAG, "Exception: rescanProvider : " + e);
148         }
149     }
150     public void getTopicsList()
151     {
152         if (mProvider != null)
153         {
154             try
155             {
156                 TopicsList topicsList = mProvider.getTopicList();
157                 StringBuilder stringBuilder = new StringBuilder();
158                 stringBuilder.append("TopicList Received :");
159                 for(Topic topic : topicsList.getTopicsList())
160                 {
161                     Log.i(TAG, "Topic Name : " + topic.getTopicName() );
162                     Log.i(TAG, "Topic State : " + topic.getState() );
163                     stringBuilder.append("\nTopic Name : " + topic.getTopicName());
164                     stringBuilder.append("\nTopic State : " + topic.getState());
165                 }
166                 Message msg = mHandler.obtainMessage(TOPICS_RECEIVED, stringBuilder.toString());
167                 mHandler.sendMessage(msg);
168             }
169             catch (Exception e)
170             {
171                 Log.e(TAG, "Exception: getTopicsList : " + e);
172             }
173         }
174         else
175         {
176             Log.e(TAG, "getTopicsList Provider NULL");
177         }
178     }
179     public void updateTopicList(TopicsList topicsList)
180     {
181         if (mProvider != null)
182         {
183             try
184             {
185                 mProvider.updateTopicList(topicsList);
186             }
187             catch (Exception e)
188             {
189                 Log.e(TAG, "Exception: updateTopicList : " + e);
190             }
191         }
192         else
193         {
194             Log.e(TAG, "updateTopicList Provider NULL");
195         }
196     }
197     @Override
198     public void onProviderDiscovered(Provider provider)
199     {
200         Log.i(TAG, "onProviderDiscovered");
201         if (provider == null)
202         {
203             Log.e(TAG, "providerID is Null  ");
204             return;
205         }
206         mProvider = provider;
207         Log.i(TAG, "Provider ID: " + provider.getProviderId() );
208         Message msg = mHandler.obtainMessage(PROVIDER_DISCOVERED,
209                                              "Provider Discovered Id: " + provider.getProviderId());
210         mHandler.sendMessage(msg);
211         try
212         {
213             Log.i(TAG, "setListeners to Discovered Provider");
214             provider.setListener(this, this, this);
215             Log.i(TAG, "setListeners done");
216             if (! provider.isSubscribed())
217             {
218                 Log.i(TAG, "Provider not subscribed. Acceptor is Consumer");
219                 mAcceptor = false;
220                 provider.subscribe();
221             }
222             else
223             {
224                 Log.i(TAG, "Provider is already subscribed. Acceptor is Provider");
225                 mAcceptor = true;
226             }
227         }
228         catch (Exception e)
229         {
230             Log.e(TAG, "Exception : " + e);
231         }
232     }
233
234     @Override
235     public void onProviderStateReceived(Provider.ProviderState state)
236     {
237         Log.i(TAG, "onProviderStateReceived");
238
239         Log.i(TAG, "ProviderState Received : " + state );
240         Message msg = mHandler.obtainMessage(STATE_CHANGED, "ProviderState Received : " + state);
241         mHandler.sendMessage(msg);
242     }
243
244     @Override
245     public void onMessageReceived(org.iotivity.service.ns.common.Message message)
246     {
247         Log.i(TAG, "onMessageReceived");
248
249         Log.i(TAG, "Message Id: " + message.getMessageId());
250         Log.i(TAG, "Message title: " + message.getTitle());
251         Log.i(TAG, "Message Content: " + message.getContentText());
252         Log.i(TAG, "Message Topic: " + message.getTopic());
253         Log.i(TAG, "Message Source: " + message.getSourceName());
254
255         Message msg = mHandler.obtainMessage(MESSAGE_RECEIVED,
256                                              "Message Id: " + message.getMessageId() + "\n" +
257                                              "Message title: " + message.getTitle() + "\n" +
258                                              "Message Content: " + message.getContentText() + "\n" +
259                                              "Message Topic: " + message.getTopic() + "\n" +
260                                              "Message Source: " + message.getSourceName() );
261         mHandler.sendMessage(msg);
262         try
263         {
264             Log.i(TAG, "send READ syncInfo");
265             mProvider.sendSyncInfo(message.getMessageId(), SyncInfo.SyncType.READ);
266         }
267         catch (Exception e)
268         {
269             Log.e(TAG, "Exception : " + e);
270         }
271     }
272
273     @Override
274     public void onSyncInfoReceived(SyncInfo sync)
275     {
276         Log.i(TAG, "onSyncInfoReceived");
277
278         Log.i(TAG, "Sync Id: " + sync.getMessageId());
279         Log.i(TAG, "Sync STATE: " + sync.getState());
280         Message msg = mHandler.obtainMessage(SYNCINFO_RECEIVED,
281                                              "Sync Id: " + sync.getMessageId() + "\n" +
282                                              "Sync STATE: " + sync.getState());
283         mHandler.sendMessage(msg);
284     }
285 }