Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / android / examples / devicediscoveryclient / src / main / java / org / iotivity / base / examples / DeviceDiscoveryClient.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.Button;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
33
34 import org.iotivity.base.ModeType;
35 import org.iotivity.base.OcConnectivityType;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcRepresentation;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ServiceType;
42
43 import java.util.EnumSet;
44 import java.util.HashMap;
45 import java.util.Map;
46
47 /**
48  * This sample demonstrates the device discovery feature.
49  * The client queries for the device related information stored by the server.
50  */
51 public class DeviceDiscoveryClient extends Activity implements
52         OcPlatform.OnDeviceFoundListener,
53         OcPlatform.OnPlatformFoundListener {
54     private void startDeviceDiscoveryClient() {
55         Context context = this;
56
57         PlatformConfig platformConfig = new PlatformConfig(
58                 context,
59                 ServiceType.IN_PROC,
60                 ModeType.CLIENT,
61                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
62                 0,         // Uses randomly available port
63                 QualityOfService.LOW
64         );
65
66         msg("Configuring platform.");
67         OcPlatform.Configure(platformConfig);
68         sleep(1);
69
70         try {
71             msg("Querying for platform information...");
72             OcPlatform.getPlatformInfo("",
73                     OcPlatform.WELL_KNOWN_PLATFORM_QUERY,
74                     EnumSet.of(OcConnectivityType.CT_DEFAULT),
75                     this);
76         } catch (OcException e) {
77             Log.e(TAG, e.toString());
78             msg("Failed to query for platform information");
79         }
80         sleep(2);
81
82         try {
83             msg("Querying for device information...");
84             OcPlatform.getDeviceInfo("",
85                     OcPlatform.WELL_KNOWN_DEVICE_QUERY,
86                     EnumSet.of(OcConnectivityType.CT_DEFAULT),
87                     this);
88         } catch (OcException e) {
89             Log.e(TAG, e.toString());
90             msg("Failed to query for device information");
91         }
92         sleep(2);
93
94         enableStartButton();
95         printLine();
96     }
97
98     private final static Map<String, String> PLATFORM_INFO_KEYS = new HashMap<String, String>() {{
99         put("pi", "Platform ID: ");
100         put("mnmn", "Manufacturer name: ");
101         put("mnml", "Manufacturer url: ");
102         put("mnmo", "Manufacturer Model No: ");
103         put("mndt", "Manufactured Date: ");
104         put("mnpv", "Manufacturer Platform Version: ");
105         put("mnos", "Manufacturer OS version: ");
106         put("mnhw", "Manufacturer hardware version: ");
107         put("mnfv", "Manufacturer firmware version: ");
108         put("mnsl", "Manufacturer support url: ");
109         put("st", "Manufacturer system time: ");
110     }};
111
112     @Override
113     public synchronized void onPlatformFound(OcRepresentation ocRepresentation) {
114         msg("Platform Information received:");
115         try {
116             for (String key : PLATFORM_INFO_KEYS.keySet()) {
117                 msg("\t" + PLATFORM_INFO_KEYS.get(key) + ocRepresentation.getValue(key));
118             }
119         } catch (OcException e) {
120             Log.e(TAG, e.toString());
121             msg("Failed to read platform info values.");
122         }
123
124         printLine();
125     }
126
127     private final static Map<String, String> DEVICE_INFO_KEYS = new HashMap<String, String>() {{
128         put("di", "Device ID: ");
129         put("n", "Device name: ");
130         put("icv", "Spec version url: ");
131         put("dmv", "Data Model: ");
132     }};
133
134     @Override
135     public synchronized void onDeviceFound(OcRepresentation ocRepresentation) {
136         msg("Device Information received:");
137         try {
138             for (String key : DEVICE_INFO_KEYS.keySet()) {
139                 msg("\t" + DEVICE_INFO_KEYS.get(key) + ocRepresentation.getValue(key));
140             }
141
142             msg("\tDevice types:");
143
144             for (String type : ocRepresentation.getResourceTypes()) {
145                 msg("\t\t" + type);
146             }
147         } catch (OcException e) {
148             Log.e(TAG, e.toString());
149             msg("Failed to read device info values.");
150         }
151
152         printLine();
153     }
154
155     //******************************************************************************
156     // End of the OIC specific code
157     //******************************************************************************
158
159     private final static String TAG = DeviceDiscoveryClient.class.getSimpleName();
160     private TextView mConsoleTextView;
161     private ScrollView mScrollView;
162
163     @Override
164     protected void onCreate(Bundle savedInstanceState) {
165         super.onCreate(savedInstanceState);
166         setContentView(R.layout.activity_device_discovery_client);
167
168         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
169         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
170         mScrollView = (ScrollView) findViewById(R.id.scrollView);
171         mScrollView.fullScroll(View.FOCUS_DOWN);
172         final Button button = (Button) findViewById(R.id.button);
173
174         if (null == savedInstanceState) {
175             button.setOnClickListener(new View.OnClickListener() {
176                 @Override
177                 public void onClick(View v) {
178                     button.setText("Re-start");
179                     button.setEnabled(false);
180                     new Thread(new Runnable() {
181                         public void run() {
182                             startDeviceDiscoveryClient();
183                         }
184                     }).start();
185                 }
186             });
187         } else {
188             String consoleOutput = savedInstanceState.getString("consoleOutputString");
189             mConsoleTextView.setText(consoleOutput);
190         }
191     }
192
193     @Override
194     protected void onSaveInstanceState(Bundle outState) {
195         super.onSaveInstanceState(outState);
196         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
197     }
198
199     @Override
200     protected void onRestoreInstanceState(Bundle savedInstanceState) {
201         super.onRestoreInstanceState(savedInstanceState);
202
203         String consoleOutput = savedInstanceState.getString("consoleOutputString");
204         mConsoleTextView.setText(consoleOutput);
205     }
206
207     private void enableStartButton() {
208         runOnUiThread(new Runnable() {
209             public void run() {
210                 Button button = (Button) findViewById(R.id.button);
211                 button.setEnabled(true);
212             }
213         });
214     }
215
216     private void sleep(int seconds) {
217         try {
218             Thread.sleep(seconds * 1000);
219         } catch (InterruptedException e) {
220             e.printStackTrace();
221             Log.e(TAG, e.toString());
222         }
223     }
224
225     private void msg(final String text) {
226         runOnUiThread(new Runnable() {
227             public void run() {
228                 mConsoleTextView.append("\n");
229                 mConsoleTextView.append(text);
230                 mScrollView.fullScroll(View.FOCUS_DOWN);
231             }
232         });
233         Log.i(TAG, text);
234     }
235
236     private void printLine() {
237         msg("------------------------------------------------------------------------");
238     }
239 }