1 /******************************************************************
2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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 package org.iotivity.service.sample.client;
20 import static org.iotivity.service.client.RcsRemoteResourceObject.OnCacheUpdatedListener;
21 import static org.iotivity.service.client.RcsRemoteResourceObject.OnStateChangedListener;
22 import static org.iotivity.service.client.RcsRemoteResourceObject.ResourceState;
24 import java.lang.ref.WeakReference;
26 import org.iotivity.service.RcsException;
27 import org.iotivity.service.RcsResourceAttributes;
28 import org.iotivity.service.RcsValue;
29 import org.iotivity.service.client.RcsAddress;
30 import org.iotivity.service.client.RcsDiscoveryManager;
31 import org.iotivity.service.client.RcsDiscoveryManager.OnResourceDiscoveredListener;
32 import org.iotivity.service.client.RcsRemoteResourceObject;
33 import org.iotivity.service.client.RcsRemoteResourceObject.OnRemoteAttributesReceivedListener;
35 import android.app.Activity;
36 import android.app.AlertDialog;
37 import android.content.DialogInterface;
38 import android.os.Bundle;
39 import android.os.Handler;
40 import android.os.Message;
41 import android.util.Log;
42 import android.view.View;
43 import android.widget.AdapterView;
44 import android.widget.AdapterView.OnItemClickListener;
45 import android.widget.ArrayAdapter;
46 import android.widget.Button;
47 import android.widget.EditText;
48 import android.widget.ListView;
49 import android.widget.TextView;
50 import android.widget.Toast;
53 * Activity for handling user's selection on UI for Resource Client APIs.
56 public class ResourceClientActivity extends Activity
57 implements OnItemClickListener {
59 private static final String LOG_TAG = ResourceClientActivity.class
62 private static final int MSG_ID_RESOURCE_DISCOVERED = 0;
63 private static final int MSG_ID_ATTRIBUTE_RECEIVED = 1;
64 private static final int MSG_ID_PRINT_LOG = 2;
66 private static final String ATTR_KEY_TEMPERATURE = "Temperature";
68 private TextView mLogView;
69 private ListView mListView;
70 private Button mDiscoveryBtn;
72 private Handler mHandler;
73 private ArrayAdapter<Item> mItemAdapter;
75 private RcsDiscoveryManager.DiscoveryTask mDiscoveryTask;
76 private RcsRemoteResourceObject mResourceObj;
78 private OnResourceDiscoveredListener mOnResourceDiscoveredListener = new OnResourceDiscoveredListener() {
81 public void onResourceDiscovered(
82 RcsRemoteResourceObject foundResource) {
83 Log.i(LOG_TAG, "onResourceDiscovered");
85 mHandler.obtainMessage(MSG_ID_RESOURCE_DISCOVERED, foundResource)
90 private OnStateChangedListener mOnStateChangedListener = new OnStateChangedListener() {
93 public void onStateChanged(ResourceState resourceState) {
94 Log.i(LOG_TAG, "onStateChanged");
96 mHandler.obtainMessage(MSG_ID_PRINT_LOG,
97 "Current Resource State : " + resourceState);
101 private OnRemoteAttributesReceivedListener mOnRemoteAttributesReceivedListener = new OnRemoteAttributesReceivedListener() {
103 public void onAttributesReceived(RcsResourceAttributes attrs,
105 Log.i(LOG_TAG, "onAttributesReceived");
107 mHandler.obtainMessage(MSG_ID_ATTRIBUTE_RECEIVED, attrs)
112 private OnCacheUpdatedListener mOnCacheUpdatedListener = new OnCacheUpdatedListener() {
114 public void onCacheUpdated(RcsResourceAttributes attrs) {
115 Log.i(LOG_TAG, "onCacheUpdated");
117 mHandler.obtainMessage(MSG_ID_ATTRIBUTE_RECEIVED, attrs)
122 private Item mStartMonitoring = new Item("1. Start Monitoring") {
124 public void execute() throws RcsException {
125 if (mResourceObj.isMonitoring()) {
126 printLog("Monitoring already started");
130 mResourceObj.startMonitoring(mOnStateChangedListener);
134 private Item mStopMonitoring = new Item("2. Stop Monitoring") {
136 public void execute() throws RcsException {
137 if (mResourceObj.isMonitoring()) {
138 mResourceObj.stopMonitoring();
139 printLog("Stopped Resource Monitoring");
141 printLog("Monitoring not started");
146 private Item mGetRemoteAttributes = new Item("3. Get Remote Attributes") {
148 public void execute() throws RcsException {
150 .getRemoteAttributes(mOnRemoteAttributesReceivedListener);
154 private Item mSetRemoteAttributes = new Item("4. Set Remote Attributes") {
157 public void execute() throws RcsException {
158 showInputValueDialog();
162 private Item mStartCaching = new Item("5. Start Caching") {
164 public void execute() throws RcsException {
165 if (mResourceObj.isCaching()) {
166 printLog("Caching already started");
170 mResourceObj.startCaching(mOnCacheUpdatedListener);
174 private Item mGetCacheState = new Item("6. Get Cache State") {
176 public void execute() throws RcsException {
177 printLog("Cache State : " + mResourceObj.getCacheState());
181 private Item mGetCachedAttributes = new Item(
182 "7. Get All Cached Attributes") {
184 public void execute() throws RcsException {
185 printAttributes(mResourceObj.getCachedAttributes());
189 private Item mGetCachedAttribute = new Item("8. Get Cached Attribute") {
191 public void execute() throws RcsException {
192 printLog(ATTR_KEY_TEMPERATURE + " : " + mResourceObj
193 .getCachedAttribute(ATTR_KEY_TEMPERATURE).asInt());
197 private Item mStopCaching = new Item("9. Stop Caching") {
199 public void execute() throws RcsException {
200 mResourceObj.stopCaching();
205 protected void onCreate(Bundle savedInstanceState) {
206 super.onCreate(savedInstanceState);
207 setContentView(R.layout.activity_resource_client);
209 mListView = (ListView) findViewById(R.id.list_menu);
210 mLogView = (TextView) findViewById(R.id.text_log);
211 mDiscoveryBtn = (Button) findViewById(R.id.btn_discovery);
213 mHandler = new ClientHandler(this);
219 protected void onDestroy() {
222 if (mDiscoveryTask != null)
223 mDiscoveryTask.cancel();
224 if (mResourceObj != null)
225 mResourceObj.destroy();
228 private void initMenuList() {
229 Item[] items = new Item[] { mStartMonitoring, mStopMonitoring,
230 mGetRemoteAttributes, mSetRemoteAttributes, mStartCaching,
231 mGetCacheState, mGetCachedAttributes, mGetCachedAttribute,
234 mItemAdapter = new ArrayAdapter<>(this,
235 android.R.layout.simple_list_item_1, items);
237 mListView.setAdapter(mItemAdapter);
239 mListView.setOnItemClickListener(this);
243 public void onItemClick(AdapterView<?> parent, View view, int position,
245 if (mResourceObj == null) {
246 showError("no discovered RemoteResourceObject");
251 mItemAdapter.getItem(position).execute();
252 } catch (RcsException e) {
257 public void onDiscoverResourceClick(View v) {
261 private void toggleDiscovery() {
262 if (mDiscoveryTask == null) {
264 mDiscoveryTask = RcsDiscoveryManager.getInstance()
265 .discoverResource(RcsAddress.multicast(),
266 mOnResourceDiscoveredListener);
267 mDiscoveryBtn.setText(R.string.cancel_discovery);
269 mListView.setVisibility(View.INVISIBLE);
271 if (mResourceObj != null) {
272 mResourceObj.destroy();
275 } catch (RcsException e) {
279 mDiscoveryTask.cancel();
280 mDiscoveryTask = null;
282 mDiscoveryBtn.setText(R.string.discover_resource);
286 private void printAttributes(RcsResourceAttributes attributes) {
288 StringBuilder sb = new StringBuilder();
289 for (String key : attributes.keySet()) {
290 sb.append(key + " : " + attributes.get(key));
292 printLog(sb.toString());
293 } catch (Exception e) {
298 private void setRemoteResourceObject(
299 RcsRemoteResourceObject foundResource) {
300 if (mResourceObj != null) {
301 Log.w(LOG_TAG, "Another remote resource found...");
305 mResourceObj = foundResource;
307 mListView.setVisibility(View.VISIBLE);
311 printLog(resourceInfo(mResourceObj));
312 } catch (RcsException e) {
317 private void showInputValueDialog() {
318 final AlertDialog dialog = new AlertDialog.Builder(this)
319 .setTitle("Enter the Temperature Value")
320 .setView(R.layout.dialog_content_edit_text)
321 .setNegativeButton("Cancel", null).create();
323 dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
324 new DialogInterface.OnClickListener() {
326 public void onClick(DialogInterface dialogInterface,
329 EditText temperatureValue = (EditText) dialog
330 .findViewById(R.id.attributeValue);
333 RcsValue value = new RcsValue(Integer.parseInt(
334 temperatureValue.getText().toString()));
336 RcsResourceAttributes attrs = new RcsResourceAttributes();
337 attrs.put(ATTR_KEY_TEMPERATURE, value);
339 mResourceObj.setRemoteAttributes(attrs,
340 mOnRemoteAttributesReceivedListener);
341 } catch (NumberFormatException e) {
342 showError("Please enter the Integer Value");
343 } catch (RcsException e) {
351 private void showError(String msg) {
352 Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
356 private void showError(Exception e) {
357 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
358 Log.e(LOG_TAG, e.getMessage(), e);
361 private void printLog(String message) {
362 Log.i(LOG_TAG, message);
363 mLogView.setText(message);
366 private void printLog(Exception e) {
367 Log.i(LOG_TAG, e.getMessage(), e);
368 mLogView.setText(e.getMessage());
371 private String resourceInfo(RcsRemoteResourceObject resourceObject)
372 throws RcsException {
373 StringBuilder sb = new StringBuilder();
375 sb.append("URI : " + resourceObject.getUri() + "\n");
376 sb.append("Host : " + resourceObject.getAddress() + "\n");
377 for (String type : resourceObject.getTypes()) {
378 sb.append("resourceType : " + type + "\n");
381 for (String itf : resourceObject.getInterfaces()) {
382 sb.append("resourceInterfaces : " + itf + "\n");
385 sb.append("isObservable : " + resourceObject.isObservable() + "\n");
387 return sb.toString();
390 private static abstract class Item {
391 private final String mTitle;
393 protected Item(String title) {
398 public String toString() {
402 public abstract void execute() throws RcsException;
405 private static class ClientHandler extends Handler {
406 private WeakReference<ResourceClientActivity> mActivityRef;
408 private ClientHandler(ResourceClientActivity activity) {
409 mActivityRef = new WeakReference<>(activity);
413 public void handleMessage(Message msg) {
414 super.handleMessage(msg);
416 ResourceClientActivity activity = mActivityRef.get();
417 if (activity == null)
421 case MSG_ID_RESOURCE_DISCOVERED:
422 activity.setRemoteResourceObject(
423 (RcsRemoteResourceObject) msg.obj);
426 case MSG_ID_ATTRIBUTE_RECEIVED:
427 activity.printAttributes((RcsResourceAttributes) msg.obj);
430 case MSG_ID_PRINT_LOG:
431 activity.printLog(msg.obj.toString());