86a005fb74e99f06bde89c70d95ecf88019d3fc5
[platform/upstream/iotivity.git] / service / resource-container / examples / android / RCSampleClientApp / app / src / main / java / org / iotivity / service / sample / client / ContainerClientActivity.java
1 /******************************************************************
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  * <p>
4  * <p>
5  * <p>
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
9  * <p>
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * <p>
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
19 package org.iotivity.service.sample.client;
20
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;
29
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;
34
35 import java.lang.ref.WeakReference;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * It contains the discover resource API for Discovering Container Resource
41  */
42
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;
47
48     private final String LOG_TAG = "[SampleClient] "
49                                          + this.getClass().getSimpleName();
50     private final String RESOURCE_DISCOVERY_URI_KEYWORD = "discomfort";
51
52     private Handler    mHandler;
53
54     private ResourceListAdapter mResourceListViewAdapter;
55
56     private ListView mResourceListView;
57     private TextView     mLogView;
58
59     private List<String> mFoundResourceUris;
60     private List<ResourceListItem> mFoundResources;
61
62     @Override
63     protected void onCreate(Bundle savedInstanceState) {
64         super.onCreate(savedInstanceState);
65         setContentView(R.layout.activity_container_client);
66
67         mFoundResourceUris = new ArrayList<String>();
68         mFoundResources = new ArrayList<ResourceListItem>();
69
70         mHandler = new ContainerClientHandler(this);
71
72         mResourceListViewAdapter = new ResourceListAdapter(this, R.layout.discovered_resource, mFoundResources);
73         mResourceListView = (ListView) findViewById(R.id.ResourceListView);
74         mResourceListView.setAdapter(mResourceListViewAdapter);
75
76         mLogView = (TextView) findViewById(R.id.log);
77     }
78
79     public void onDiscoverResourceClick(View v) {
80         try {
81             RcsDiscoveryManager.getInstance().discoverResource(
82                     RcsAddress.multicast(),
83                     ContainerClientActivity.this);
84
85             mFoundResourceUris.clear();
86             mFoundResources.clear();
87             mResourceListViewAdapter.notifyDataSetChanged();
88
89             mLogView.setText("");
90         } catch (RcsException e) {
91             e.printStackTrace();
92         }
93     }
94
95     private boolean IsFoundResource(String resourceUri)
96     {
97         if (mFoundResourceUris.contains(resourceUri))
98             return true;
99
100         return false;
101     }
102
103     @Override
104     public void onResourceDiscovered(
105             final RcsRemoteResourceObject discoveredResource) {
106
107         runOnUiThread(new Runnable() {
108             public void run() {
109                 try {
110                     String uri = discoveredResource.getUri();
111                     Log.i(LOG_TAG, "onResourceDiscovered -- " + uri);
112
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();
117                     }
118                 } catch (RcsException e) {
119                     e.printStackTrace();
120                 }
121             }
122         });
123     }
124
125     private void printLog(String message) {
126         Log.i(LOG_TAG, message);
127         mLogView.setText(message);
128     }
129
130     public Handler getHandler() {
131         return mHandler;
132     }
133
134     private static class ContainerClientHandler extends Handler {
135         private WeakReference<ContainerClientActivity> mActivityRef;
136         private String logMsg;
137
138         private ContainerClientHandler(ContainerClientActivity activity) {
139             mActivityRef = new WeakReference<>(activity);
140         }
141
142         @Override
143         public void handleMessage(Message msg) {
144             super.handleMessage(msg);
145
146             ContainerClientActivity activity = mActivityRef.get();
147             if (activity == null) return;
148
149             switch (msg.what) {
150                 case MSG_ID_ATTRIBUTE_RECEIVED:
151                     logMsg = "-- ATTRIBUTE_RECEIVED\n";
152                     logMsg += msg.obj.toString();
153                     break;
154
155                 case MSG_ID_CACHEDATA_RECEIVED:
156                     logMsg = "-- CACHEDATA_RECEIVED\n";
157                     logMsg += msg.obj.toString();
158                     break;
159             }
160             activity.printLog(logMsg);
161         }
162     }
163 }