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 RESOURCE_TYPE = "oic.r.temperature.sensor";
67 private static final String ATTR_KEY_TEMPERATURE = "Temperature";
69 private TextView mLogView;
70 private ListView mListView;
71 private Button mDiscoveryBtn;
73 private Handler mHandler;
74 private ArrayAdapter<Item> mItemAdapter;
76 private RcsDiscoveryManager.DiscoveryTask mDiscoveryTask;
77 private RcsRemoteResourceObject mResourceObj;
79 private OnResourceDiscoveredListener mOnResourceDiscoveredListener = new OnResourceDiscoveredListener() {
82 public void onResourceDiscovered(
83 RcsRemoteResourceObject foundResource) {
84 Log.i(LOG_TAG, "onResourceDiscovered");
86 mHandler.obtainMessage(MSG_ID_RESOURCE_DISCOVERED, foundResource)
91 private OnStateChangedListener mOnStateChangedListener = new OnStateChangedListener() {
94 public void onStateChanged(ResourceState resourceState) {
95 Log.i(LOG_TAG, "onStateChanged");
97 mHandler.obtainMessage(MSG_ID_PRINT_LOG,
98 "Current Resource State : " + resourceState);
102 private OnRemoteAttributesReceivedListener mOnRemoteAttributesReceivedListener = new OnRemoteAttributesReceivedListener() {
104 public void onAttributesReceived(RcsResourceAttributes attrs,
106 Log.i(LOG_TAG, "onAttributesReceived");
108 mHandler.obtainMessage(MSG_ID_ATTRIBUTE_RECEIVED, attrs)
113 private OnCacheUpdatedListener mOnCacheUpdatedListener = new OnCacheUpdatedListener() {
115 public void onCacheUpdated(RcsResourceAttributes attrs, int eCode) {
116 Log.i(LOG_TAG, "onCacheUpdated");
118 mHandler.obtainMessage(MSG_ID_ATTRIBUTE_RECEIVED, attrs)
123 private Item mStartMonitoring = new Item("1. Start Monitoring") {
125 public void execute() throws RcsException {
126 if (mResourceObj.isMonitoring()) {
127 printLog("Monitoring already started");
131 mResourceObj.startMonitoring(mOnStateChangedListener);
133 if (mResourceObj.isMonitoring()) {
134 printLog("Monitoring started successfully");
139 private Item mStopMonitoring = new Item("2. Stop Monitoring") {
141 public void execute() throws RcsException {
142 if (mResourceObj.isMonitoring()) {
144 mResourceObj.stopMonitoring();
146 if (!mResourceObj.isMonitoring()) {
147 printLog("Monitoring stopped successfully");
151 printLog("Monitoring not started");
156 private Item mGetRemoteAttributes = new Item("3. Get Remote Attributes") {
158 public void execute() throws RcsException {
160 .getRemoteAttributes(mOnRemoteAttributesReceivedListener);
164 private Item mSetRemoteAttributes = new Item("4. Set Remote Attributes") {
167 public void execute() throws RcsException {
168 showInputValueDialog();
172 private Item mStartCaching = new Item("5. Start Caching") {
174 public void execute() throws RcsException {
175 if (mResourceObj.isCaching()) {
176 printLog("Caching already started");
180 mResourceObj.startCaching(mOnCacheUpdatedListener);
182 if (mResourceObj.isCaching()) {
183 printLog("Caching started successfully");
189 private Item mGetCacheState = new Item("6. Get Cache State") {
191 public void execute() throws RcsException {
192 printLog("Cache State : " + mResourceObj.getCacheState());
196 private Item mGetCachedAttributes = new Item(
197 "7. Get All Cached Attributes") {
199 public void execute() throws RcsException {
200 printAttributes(mResourceObj.getCachedAttributes());
204 private Item mGetCachedAttribute = new Item("8. Get Cached Attribute") {
206 public void execute() throws RcsException {
207 printLog(ATTR_KEY_TEMPERATURE + " : " + mResourceObj
208 .getCachedAttribute(ATTR_KEY_TEMPERATURE).asInt());
212 private Item mStopCaching = new Item("9. Stop Caching") {
214 public void execute() throws RcsException {
215 if (mResourceObj.isCaching()) {
217 mResourceObj.stopCaching();
219 if (!mResourceObj.isCaching()) {
220 printLog("Caching stopped successfully");
222 printLog("Stopping caching unsuccessful");
226 printLog("Caching not started");
233 protected void onCreate(Bundle savedInstanceState) {
234 super.onCreate(savedInstanceState);
235 setContentView(R.layout.activity_resource_client);
237 mListView = (ListView) findViewById(R.id.list_menu);
238 mLogView = (TextView) findViewById(R.id.text_log);
239 mDiscoveryBtn = (Button) findViewById(R.id.btn_discovery);
241 mHandler = new ClientHandler(this);
247 protected void onDestroy() {
250 if (mDiscoveryTask != null) mDiscoveryTask.cancel();
251 if (mResourceObj != null) mResourceObj.destroy();
254 private void initMenuList() {
255 Item[] items = new Item[] { mStartMonitoring, mStopMonitoring,
256 mGetRemoteAttributes, mSetRemoteAttributes, mStartCaching,
257 mGetCacheState, mGetCachedAttributes, mGetCachedAttribute,
260 mItemAdapter = new ArrayAdapter<>(this,
261 android.R.layout.simple_list_item_1, items);
263 mListView.setAdapter(mItemAdapter);
265 mListView.setOnItemClickListener(this);
269 public void onItemClick(AdapterView<?> parent, View view, int position,
271 if (mResourceObj == null) {
272 showError("no discovered RemoteResourceObject");
277 mItemAdapter.getItem(position).execute();
278 } catch (RcsException e) {
283 public void onDiscoverResourceClick(View v) {
287 private void toggleDiscovery() {
288 if (mDiscoveryTask == null) {
290 mDiscoveryTask = RcsDiscoveryManager.getInstance()
291 .discoverResourceByType(RcsAddress.multicast(), RESOURCE_TYPE,
292 mOnResourceDiscoveredListener);
293 mDiscoveryBtn.setText(R.string.cancel_discovery);
295 mListView.setVisibility(View.INVISIBLE);
297 if (mResourceObj != null) {
298 mResourceObj.destroy();
301 } catch (RcsException e) {
305 mDiscoveryTask.cancel();
306 mDiscoveryTask = null;
308 mDiscoveryBtn.setText(R.string.discover_resource);
312 private void printAttributes(RcsResourceAttributes attributes) {
314 StringBuilder sb = new StringBuilder();
315 for (String key : attributes.keySet()) {
316 sb.append(key + " : " + attributes.get(key));
318 printLog(sb.toString());
319 } catch (Exception e) {
324 private void setRemoteResourceObject(
325 RcsRemoteResourceObject foundResource) {
326 if (mResourceObj != null) {
327 Log.w(LOG_TAG, "Another remote resource found...");
331 mResourceObj = foundResource;
333 mListView.setVisibility(View.VISIBLE);
337 printLog(resourceInfo(mResourceObj));
338 } catch (RcsException e) {
343 private void showInputValueDialog() {
344 final AlertDialog dialog = new AlertDialog.Builder(this)
345 .setTitle("Enter the Temperature Value")
346 .setView(R.layout.dialog_content_edit_text)
347 .setNegativeButton("Cancel", null).create();
349 dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
350 new DialogInterface.OnClickListener() {
352 public void onClick(DialogInterface dialogInterface,
355 EditText temperatureValue = (EditText) dialog
356 .findViewById(R.id.attributeValue);
359 RcsValue value = new RcsValue(Integer.parseInt(
360 temperatureValue.getText().toString()));
362 RcsResourceAttributes attrs = new RcsResourceAttributes();
363 attrs.put(ATTR_KEY_TEMPERATURE, value);
365 mResourceObj.setRemoteAttributes(attrs,
366 mOnRemoteAttributesReceivedListener);
367 } catch (NumberFormatException e) {
368 showError("Please enter the Integer Value");
369 } catch (RcsException e) {
377 private void showError(String msg) {
378 Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
382 private void showError(Exception e) {
383 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
384 Log.e(LOG_TAG, e.getMessage(), e);
387 private void printLog(String message) {
388 Log.i(LOG_TAG, message);
389 mLogView.setText(message);
392 private void printLog(Exception e) {
393 Log.i(LOG_TAG, e.getMessage(), e);
394 mLogView.setText(e.getMessage());
397 private String resourceInfo(RcsRemoteResourceObject resourceObject)
398 throws RcsException {
399 StringBuilder sb = new StringBuilder();
401 sb.append("URI : " + resourceObject.getUri() + "\n");
402 sb.append("Host : " + resourceObject.getAddress() + "\n");
403 for (String type : resourceObject.getTypes()) {
404 sb.append("resourceType : " + type + "\n");
407 for (String itf : resourceObject.getInterfaces()) {
408 sb.append("resourceInterfaces : " + itf + "\n");
411 sb.append("isObservable : " + resourceObject.isObservable() + "\n");
413 return sb.toString();
416 private static abstract class Item {
417 private final String mTitle;
419 protected Item(String title) {
424 public String toString() {
428 public abstract void execute() throws RcsException;
431 private static class ClientHandler extends Handler {
432 private WeakReference<ResourceClientActivity> mActivityRef;
434 private ClientHandler(ResourceClientActivity activity) {
435 mActivityRef = new WeakReference<>(activity);
439 public void handleMessage(Message msg) {
440 super.handleMessage(msg);
442 ResourceClientActivity activity = mActivityRef.get();
443 if (activity == null) return;
446 case MSG_ID_RESOURCE_DISCOVERED:
447 activity.setRemoteResourceObject(
448 (RcsRemoteResourceObject) msg.obj);
451 case MSG_ID_ATTRIBUTE_RECEIVED:
452 activity.printAttributes((RcsResourceAttributes) msg.obj);
455 case MSG_ID_PRINT_LOG:
456 activity.printLog(msg.obj.toString());