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 ******************************************************************/
19 package org.iotivity.service.sample.client;
21 import android.app.Activity;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.util.Log;
26 import android.view.View;
27 import android.widget.ListView;
28 import android.widget.TextView;
30 import org.iotivity.service.RcsException;
31 import org.iotivity.service.client.RcsAddress;
32 import org.iotivity.service.client.RcsDiscoveryManager;
33 import org.iotivity.service.client.RcsRemoteResourceObject;
35 import java.lang.ref.WeakReference;
36 import java.util.ArrayList;
37 import java.util.List;
40 * It contains the discover resource API for Discovering Container Resource
43 public class ContainerClientActivity extends Activity implements
44 RcsDiscoveryManager.OnResourceDiscoveredListener {
45 public static final int MSG_ID_ATTRIBUTE_RECEIVED = 0;
46 public static final int MSG_ID_CACHEDATA_RECEIVED = 1;
48 private final String LOG_TAG = "[SampleClient] "
49 + this.getClass().getSimpleName();
50 private final String RESOURCE_DISCOVERY_URI_KEYWORD = "discomfort";
52 private Handler mHandler;
54 private ResourceListAdapter mResourceListViewAdapter;
56 private ListView mResourceListView;
57 private TextView mLogView;
59 private List<String> mFoundResourceUris;
60 private List<ResourceListItem> mFoundResources;
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.activity_container_client);
67 mFoundResourceUris = new ArrayList<String>();
68 mFoundResources = new ArrayList<ResourceListItem>();
70 mHandler = new ContainerClientHandler(this);
72 mResourceListViewAdapter = new ResourceListAdapter(this, R.layout.discovered_resource, mFoundResources);
73 mResourceListView = (ListView) findViewById(R.id.ResourceListView);
74 mResourceListView.setAdapter(mResourceListViewAdapter);
76 mLogView = (TextView) findViewById(R.id.log);
79 public void onDiscoverResourceClick(View v) {
81 RcsDiscoveryManager.getInstance().discoverResource(
82 RcsAddress.multicast(),
83 ContainerClientActivity.this);
85 mFoundResourceUris.clear();
86 mFoundResources.clear();
87 mResourceListViewAdapter.notifyDataSetChanged();
90 } catch (RcsException e) {
95 private boolean IsFoundResource(String resourceUri)
97 if (mFoundResourceUris.contains(resourceUri))
104 public void onResourceDiscovered(
105 final RcsRemoteResourceObject discoveredResource) {
107 runOnUiThread(new Runnable() {
110 String uri = discoveredResource.getUri();
111 Log.i(LOG_TAG, "onResourceDiscovered -- " + uri);
113 if (uri.contains(RESOURCE_DISCOVERY_URI_KEYWORD) && !IsFoundResource(uri)) {
114 mFoundResourceUris.add(uri);
115 mFoundResources.add(new ResourceListItem(uri, discoveredResource, ContainerClientActivity.this.getHandler()));
116 mResourceListViewAdapter.notifyDataSetChanged();
118 } catch (RcsException e) {
125 private void printLog(String message) {
126 Log.i(LOG_TAG, message);
127 mLogView.setText(message);
130 public Handler getHandler() {
134 private static class ContainerClientHandler extends Handler {
135 private WeakReference<ContainerClientActivity> mActivityRef;
136 private String logMsg;
138 private ContainerClientHandler(ContainerClientActivity activity) {
139 mActivityRef = new WeakReference<>(activity);
143 public void handleMessage(Message msg) {
144 super.handleMessage(msg);
146 ContainerClientActivity activity = mActivityRef.get();
147 if (activity == null) return;
150 case MSG_ID_ATTRIBUTE_RECEIVED:
151 logMsg = "-- ATTRIBUTE_RECEIVED\n";
152 logMsg += msg.obj.toString();
155 case MSG_ID_CACHEDATA_RECEIVED:
156 logMsg = "-- CACHEDATA_RECEIVED\n";
157 logMsg += msg.obj.toString();
160 activity.printLog(logMsg);