[IOT-1089] Change Android build system to accomodate both Android and Generic Java...
[contrib/iotivity.git] / java / examples-java / 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("lcv", "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         } catch (OcException e) {
142             Log.e(TAG, e.toString());
143             msg("Failed to read device info values.");
144         }
145
146         printLine();
147     }
148
149     //******************************************************************************
150     // End of the OIC specific code
151     //******************************************************************************
152
153     private final static String TAG = DeviceDiscoveryClient.class.getSimpleName();
154     private TextView mConsoleTextView;
155     private ScrollView mScrollView;
156
157     @Override
158     protected void onCreate(Bundle savedInstanceState) {
159         super.onCreate(savedInstanceState);
160         setContentView(R.layout.activity_device_discovery_client);
161
162         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
163         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
164         mScrollView = (ScrollView) findViewById(R.id.scrollView);
165         mScrollView.fullScroll(View.FOCUS_DOWN);
166         final Button button = (Button) findViewById(R.id.button);
167
168         if (null == savedInstanceState) {
169             button.setOnClickListener(new View.OnClickListener() {
170                 @Override
171                 public void onClick(View v) {
172                     button.setText("Re-start");
173                     button.setEnabled(false);
174                     new Thread(new Runnable() {
175                         public void run() {
176                             startDeviceDiscoveryClient();
177                         }
178                     }).start();
179                 }
180             });
181         } else {
182             String consoleOutput = savedInstanceState.getString("consoleOutputString");
183             mConsoleTextView.setText(consoleOutput);
184         }
185     }
186
187     @Override
188     protected void onSaveInstanceState(Bundle outState) {
189         super.onSaveInstanceState(outState);
190         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
191     }
192
193     @Override
194     protected void onRestoreInstanceState(Bundle savedInstanceState) {
195         super.onRestoreInstanceState(savedInstanceState);
196
197         String consoleOutput = savedInstanceState.getString("consoleOutputString");
198         mConsoleTextView.setText(consoleOutput);
199     }
200
201     private void enableStartButton() {
202         runOnUiThread(new Runnable() {
203             public void run() {
204                 Button button = (Button) findViewById(R.id.button);
205                 button.setEnabled(true);
206             }
207         });
208     }
209
210     private void sleep(int seconds) {
211         try {
212             Thread.sleep(seconds * 1000);
213         } catch (InterruptedException e) {
214             e.printStackTrace();
215             Log.e(TAG, e.toString());
216         }
217     }
218
219     private void msg(final String text) {
220         runOnUiThread(new Runnable() {
221             public void run() {
222                 mConsoleTextView.append("\n");
223                 mConsoleTextView.append(text);
224                 mScrollView.fullScroll(View.FOCUS_DOWN);
225             }
226         });
227         Log.i(TAG, text);
228     }
229
230     private void printLine() {
231         msg("------------------------------------------------------------------------");
232     }
233 }