Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / examples / presenceclient / src / main / java / org / iotivity / base / examples / PresenceClient.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.base.examples;
23
24 import android.app.Activity;
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.CompoundButton;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
33 import android.widget.ToggleButton;
34
35 import org.iotivity.base.ModeType;
36 import org.iotivity.base.OcConnectivityType;
37 import org.iotivity.base.OcException;
38 import org.iotivity.base.OcPlatform;
39 import org.iotivity.base.OcPresenceHandle;
40 import org.iotivity.base.OcPresenceStatus;
41 import org.iotivity.base.OcResource;
42 import org.iotivity.base.PlatformConfig;
43 import org.iotivity.base.QualityOfService;
44 import org.iotivity.base.ServiceType;
45
46 import java.util.EnumSet;
47
48 /**
49  * A client example for presence notification
50  */
51 public class PresenceClient extends Activity implements
52         OcPlatform.OnResourceFoundListener,
53         OcPlatform.OnPresenceListener {
54     private final static String TAG = PresenceClient.class.getSimpleName();
55     private OcResource mResource;
56     private OcPresenceHandle mPresenceHandle;
57     private TextView mConsoleTextView;
58     private ScrollView mScrollView;
59
60     private void startPresenceClient() {
61         Context context = this;
62
63         PlatformConfig platformConfig = new PlatformConfig(
64                 context,
65                 ServiceType.IN_PROC,
66                 ModeType.CLIENT,
67                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
68                 0,         // Uses randomly available port
69                 QualityOfService.LOW
70         );
71
72         msg("Configuring platform.");
73         OcPlatform.Configure(platformConfig);
74
75         try {
76             msg("Finding Resource...");
77             OcPlatform.findResource("", OcPlatform.WELL_KNOWN_QUERY,
78                     EnumSet.of(OcConnectivityType.CT_DEFAULT), this);
79         } catch (OcException e) {
80             Log.e(TAG, e.toString());
81             msg("Failed to find resource(s). ");
82         }
83         printLine();
84         enableStartStopButton();
85     }
86
87     //******************************************************************************
88     // End of the OIC specific code
89     //******************************************************************************
90
91     @Override
92     public synchronized void onResourceFound(OcResource foundResource) {
93         String resourceUri = foundResource.getUri();
94         if (null != mResource || !resourceUri.equals("/a/light")) {
95             msg("Found resource, ignoring");
96             return;
97         }
98
99         msg("Found Resource");
100         String hostAddress = foundResource.getHost();
101         msg("\tResource URI : " + resourceUri);
102         msg("\tResource Host : " + hostAddress);
103         msg("\tResource Interfaces : ");
104         for (String resInterface : foundResource.getResourceInterfaces()) {
105             msg("\t\t" + resInterface);
106         }
107         msg("\tResource Type : ");
108         for (String resTypes : foundResource.getResourceTypes()) {
109             msg("\t\t" + resTypes);
110         }
111
112         try {
113             msg("Subscribing to unicast address:" + hostAddress);
114             OcPlatform.subscribePresence(hostAddress,
115                     EnumSet.of(OcConnectivityType.CT_DEFAULT), this);
116         } catch (OcException e) {
117             Log.e(TAG, e.toString());
118             msg("Failed to subscribe to unicast address:" + hostAddress);
119         }
120         mResource = foundResource;
121         printLine();
122     }
123
124     @Override
125     public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress) {
126         msg("Received presence notification from : " + hostAddress);
127         switch (ocPresenceStatus) {
128             case OK:
129                 msg("Nonce# " + nonce);
130                 break;
131             case STOPPED:
132                 msg("Presence Stopped");
133                 break;
134             case TIMEOUT:
135                 msg("Presence Timeout");
136                 break;
137             case DO_NOT_HANDLE:
138                 msg("Presence Do Not Handle");
139                 break;
140         }
141     }
142
143     private void stopPresenceClient() {
144         if (null != mPresenceHandle) {
145             try {
146                 msg("Unsubscribing from the presence notifications.");
147                 OcPlatform.unsubscribePresence(mPresenceHandle);
148                 mPresenceHandle = null;
149             } catch (OcException e) {
150                 Log.e(TAG, e.toString());
151                 msg("Failed to unsubscribe from the presence notifications" + e.toString());
152             }
153         }
154         mResource = null;
155         printLine();
156         enableStartStopButton();
157     }
158
159     @Override
160     protected void onCreate(Bundle savedInstanceState) {
161         super.onCreate(savedInstanceState);
162         setContentView(R.layout.activity_presence_client);
163
164         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
165         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
166         mScrollView = (ScrollView) findViewById(R.id.scrollView);
167         mScrollView.fullScroll(View.FOCUS_DOWN);
168         final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
169
170         if (null == savedInstanceState) {
171             toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
172                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
173                     toggleButton.setEnabled(false);
174                     if (isChecked) {
175                         new Thread(new Runnable() {
176                             public void run() {
177                                 startPresenceClient();
178                             }
179                         }).start();
180                     } else {
181                         new Thread(new Runnable() {
182                             public void run() {
183                                 stopPresenceClient();
184                             }
185                         }).start();
186                     }
187                 }
188             });
189         } else {
190             String consoleOutput = savedInstanceState.getString("consoleOutputString");
191             mConsoleTextView.setText(consoleOutput);
192             boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
193             toggleButton.setChecked(buttonCheked);
194         }
195     }
196
197     @Override
198     protected void onSaveInstanceState(Bundle outState) {
199         super.onSaveInstanceState(outState);
200         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
201         ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
202         outState.putBoolean("toggleButtonChecked", toggleButton.isChecked());
203     }
204
205     @Override
206     protected void onRestoreInstanceState(Bundle savedInstanceState) {
207         super.onRestoreInstanceState(savedInstanceState);
208
209         String consoleOutput = savedInstanceState.getString("consoleOutputString");
210         mConsoleTextView.setText(consoleOutput);
211
212         final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
213         boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked");
214         toggleButton.setChecked(buttonCheked);
215     }
216
217     private void msg(final String text) {
218         runOnUiThread(new Runnable() {
219             public void run() {
220                 mConsoleTextView.append("\n");
221                 mConsoleTextView.append(text);
222                 mScrollView.fullScroll(View.FOCUS_DOWN);
223             }
224         });
225         Log.i(TAG, text);
226     }
227
228     private void printLine() {
229         msg("------------------------------------------------------------------------");
230     }
231
232     private void enableStartStopButton() {
233         runOnUiThread(new Runnable() {
234             public void run() {
235                 ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
236                 toggleButton.setEnabled(true);
237             }
238         });
239     }
240 }