Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / examples / guiclient / src / main / java / org / iotivity / guiclient / ExpandableResourceListAdapter.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package org.iotivity.guiclient;
22
23 import android.content.Context;
24 import android.media.Image;
25 import android.util.Log;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.BaseExpandableListAdapter;
30 import android.widget.CompoundButton;
31 import android.widget.ImageView;
32 import android.widget.ProgressBar;
33 import android.widget.SeekBar;
34 import android.widget.Switch;
35 import android.widget.TextView;
36
37 import java.util.List;
38
39 import static org.iotivity.guiclient.OcAttributeInfo.OC_ATTRIBUTE_TYPE;
40 import static org.iotivity.guiclient.OcProtocolStrings.AMBIENT_LIGHT_RESOURCE_URI;
41 import static org.iotivity.guiclient.OcProtocolStrings.LIGHT_RESOURCE_URI;
42 import static org.iotivity.guiclient.OcProtocolStrings.PLATFORM_LED_RESOURCE_URI;
43 import static org.iotivity.guiclient.OcProtocolStrings.ROOM_TEMPERATURE_RESOURCE_URI;
44
45 /**
46  * ExpandableResourceListAdapter knows how to render an ExpandableListView, using a
47  * List of OcResourceInfo objects as the parents of the ExpandableListView,
48  * and OcAttributeInfo objects as the children.
49  *
50  * @see org.iotivity.guiclient.OcAttributeInfo
51  */
52 public class ExpandableResourceListAdapter extends BaseExpandableListAdapter {
53     /**
54      * Hardcoded TAG... if project never uses proguard then
55      * MyOcClient.class.getName() is the better way.
56      */
57     private static final String TAG = "ExpandableResourceListAdapter";
58
59     private static final boolean LOCAL_LOGV = true; // set to false to compile out verbose logging
60
61     private List<OcResourceInfo> resourceList;
62     private Context ctx;
63
64     public ExpandableResourceListAdapter(List<OcResourceInfo> resourceList, Context ctx) {
65         this.resourceList = resourceList;
66         this.ctx = ctx;
67     }
68
69     @Override
70     public Object getChild(int groupPosition, int childPosition) {
71         return resourceList.get(groupPosition).getAttributes().get(childPosition);
72     }
73
74     @Override
75     public int getChildrenCount(int groupPosition) {
76         return resourceList.get(groupPosition).getAttributes().size();
77     }
78
79     @Override
80     public long getChildId(int groupPosition, int childPosition) {
81         return resourceList.get(groupPosition).getAttributes().get(childPosition).hashCode();
82     }
83
84     @Override
85     public int getChildType(int groupPosition, int childPosition) {
86         return this.resourceList.get(groupPosition).getAttributes().get(childPosition)
87                 .getType().ordinal();
88     }
89
90     @Override
91     public int getChildTypeCount() {
92         return OC_ATTRIBUTE_TYPE.values().length;
93     }
94
95     @Override
96     public View getChildView(final int groupPosition, int childPosition,
97                              boolean isLastChild, View convertView, ViewGroup parent) {
98         View v = convertView;
99         if (v == null) {
100             LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
101                     (Context.LAYOUT_INFLATER_SERVICE);
102             switch(OC_ATTRIBUTE_TYPE.fromInt(getChildType(groupPosition, childPosition))) {
103                 case AMBIENT_LIGHT_SENSOR_READING:
104                 case ROOM_TEMPERATURE_SENSOR_READING:
105                     v = inflater.inflate(R.layout.attribute_layout_progress_bar, parent, false);
106                     break;
107                 case LIGHT_DIMMER:
108                     v = inflater.inflate(R.layout.attribute_layout_slider, parent, false);
109                     break;
110                 case LIGHT_SWITCH:
111                 case PLATFORM_LED_SWITCH:
112                     v = inflater.inflate(R.layout.attribute_layout_on_off_switch, parent, false);
113                     break;
114             }
115         }
116
117         OcAttributeInfo attribute =
118                 resourceList.get(groupPosition).getAttributes().get(childPosition);
119
120         // All attribute icons and names are currently treated the same so we handle them outside
121         // the type-specific inflater functions
122         ImageView attributeIcon = (ImageView) v.findViewById(R.id.attribute_icon_id);
123         attributeIcon.setVisibility(View.VISIBLE);
124         TextView attributeName = (TextView) v.findViewById(R.id.attribute_name_id);
125         attributeName.setText(getAttributeLabelFromType(attribute.getType()));
126         attributeName.setVisibility(View.VISIBLE);
127
128         // Now inflate the rest of the layout in a type-specific way
129         switch(attribute.getType()){
130             case AMBIENT_LIGHT_SENSOR_READING:
131                 this.renderAmbientLightSensorReading(v, groupPosition, attribute);
132                 break;
133             case LIGHT_DIMMER:
134                 this.renderLightDimmer(v, groupPosition, attribute);
135                 break;
136             case LIGHT_SWITCH:
137                 this.renderLightSwitch(v, groupPosition, attribute);
138                 break;
139             case PLATFORM_LED_SWITCH:
140                 this.renderPlatformLedSwitch(v, groupPosition, attribute);
141                 break;
142             case ROOM_TEMPERATURE_SENSOR_READING:
143                 this.renderRoomTemperatureSensorReading(v, groupPosition, attribute);
144                 break;
145         }
146
147         return v;
148     }
149
150     @Override
151     public Object getGroup(int groupPosition) {
152         return resourceList.get(groupPosition);
153     }
154
155     @Override
156     public int getGroupCount() {
157         return resourceList.size();
158     }
159
160     @Override
161     public long getGroupId(int groupPosition) {
162         return resourceList.get(groupPosition).hashCode();
163     }
164
165     @Override
166     public View getGroupView(int groupPosition, boolean isExpanded,
167                              View convertView, ViewGroup parent) {
168
169         View v = convertView;
170
171         if (v == null) {
172             LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
173                     (Context.LAYOUT_INFLATER_SERVICE);
174             v = inflater.inflate(R.layout.resource_list_item_layout, parent, false);
175         }
176
177         TextView resourceName = (TextView) v.findViewById(R.id.resource_name_id);
178         TextView resourceDescription = (TextView) v.findViewById(R.id.resource_description_id);
179         ImageView resourceIcon = (ImageView) v.findViewById(R.id.resource_icon_id);
180
181         OcResourceInfo resource = resourceList.get(groupPosition);
182
183         resourceName.setText(this.getResourceLabelFromType(resource.getType()));
184         resourceDescription.setText(resource.getHost()+resource.getUri());
185         switch (resource.getType()) {
186             case AMBIENT_LIGHT_SENSOR:
187                 resourceIcon.setImageResource(R.drawable.iotivity_hex_icon);
188                 break;
189             case LIGHT:
190                 resourceIcon.setImageResource(R.drawable.light_icon);
191                 break;
192             case PLATFORM_LED:
193                 resourceIcon.setImageResource(R.drawable.led_icon);
194                 break;
195             case ROOM_TEMPERATURE_SENSOR:
196                 resourceIcon.setImageResource(R.drawable.thermometer_icon);
197                 break;
198             default:
199                 resourceIcon.setImageResource(R.drawable.iotivity_hex_icon);
200                 break;
201         }
202
203         return v;
204     }
205
206     @Override
207     public boolean hasStableIds() {
208         return true;
209     }
210
211     @Override
212     public boolean isChildSelectable(int groupPosition, int childPosition) {
213         return true;
214     }
215
216     /**
217      * Type-specific layout render for Ambient Light Sensor reading attribute.
218      */
219     private void renderAmbientLightSensorReading(final View view,
220                                                   final int groupPosition,
221                                                   final OcAttributeInfo attribute) {
222         // Render attributeValue
223         TextView attributeValue = (TextView) view.findViewById(R.id.attribute_value_id);
224         attributeValue.setText(String.valueOf(attribute.getValueInt()));
225         attributeValue.setVisibility(View.VISIBLE);
226
227         // Render progressBar
228         ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.attribute_progress_bar);
229         progressBar.setMax(100); // display as percent from 0-100
230         progressBar.setProgress(attribute.getValueAsPercentOfMax());
231         progressBar.setVisibility(View.VISIBLE);
232     }
233
234     /**
235      * Type-specific layout render for Light Dimmer attribute.
236      */
237     private void renderLightDimmer(final View view,
238                                     final int groupPosition,
239                                     final OcAttributeInfo attribute) {
240         // Render attributeValue
241         TextView attributeValue = (TextView) view.findViewById(R.id.attribute_value_id);
242         attributeValue.setText(String.valueOf(attribute.getValueInt()));
243         attributeValue.setVisibility(View.VISIBLE);
244
245         // Render SeekBar
246         SeekBar slider = (SeekBar) view.findViewById(R.id.attribute_slider);
247         slider.setMax(attribute.getValueMax());
248         slider.setProgress(attribute.getValueInt());
249         slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
250             private int mSliderVal;
251             @Override
252             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
253                 if (LOCAL_LOGV) Log.v(TAG, String.format("onProgressChanged(%s)", progress));
254                 this.mSliderVal = progress;
255             }
256             @Override
257             public void onStartTrackingTouch(SeekBar seekBar) {}
258             @Override
259             public void onStopTrackingTouch(SeekBar seekBar) {
260                 if (LOCAL_LOGV) Log.v(TAG, "onStopTrackingTouch()");
261                 if(ctx instanceof MainActivity) {
262                     // call MainActivity
263                     ((MainActivity) ctx).setLightDimmerLevel(resourceList.get(groupPosition),
264                             this.mSliderVal);
265                 }
266             }
267         });
268         slider.setVisibility(View.VISIBLE);
269     }
270
271     /**
272      * Type-specific layout render for Light Switch attribute.
273      */
274     private void renderLightSwitch(final View view,
275                                     final int groupPosition,
276                                     final OcAttributeInfo attribute) {
277         // Render attributeValue
278         TextView attributeValue = (TextView) view.findViewById(R.id.attribute_value_id);
279         if(false == attribute.getValueBool()) {
280             attributeValue.setText("off");
281         } else {
282             attributeValue.setText("on");
283         }
284         attributeValue.setVisibility(View.VISIBLE);
285
286         // Render Switch
287         Switch toggleSwitch = (Switch) view.findViewById(R.id.attribute_switch);
288         toggleSwitch.setText(this.ctx.getString(R.string.oc_light_switch_toggle_text));
289         toggleSwitch.setChecked(attribute.getValueBool());
290         toggleSwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
291             @Override
292             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
293                 if (LOCAL_LOGV) Log.v(TAG, String.format("onCheckedChanged(%s)", isChecked));
294                 if(ctx instanceof MainActivity) {
295                     // call MainActivity
296                     ((MainActivity) ctx).toggleLightSwitch(resourceList.get(groupPosition),
297                             isChecked);
298                 }
299             }
300         });
301         toggleSwitch.setVisibility(View.VISIBLE);
302     }
303
304     /**
305      * Type-specific layout render for LED Switch attribute.
306      */
307     private void renderPlatformLedSwitch(final View view,
308                                           final int groupPosition,
309                                           final OcAttributeInfo attribute) {
310         // Render attributeValue
311         TextView attributeValue = (TextView) view.findViewById(R.id.attribute_value_id);
312         if(1 == attribute.getValueInt()) {
313             attributeValue.setText("on");
314         } else {
315             attributeValue.setText("off");
316         }
317         attributeValue.setVisibility(View.VISIBLE);
318
319         // Render Switch
320         Switch toggleSwitch = (Switch) view.findViewById(R.id.attribute_switch);
321         toggleSwitch.setText(this.ctx.getString(R.string.oc_led_switch_toggle_text));
322         toggleSwitch.setChecked(1 == attribute.getValueInt());
323         toggleSwitch.setOnCheckedChangeListener( new Switch.OnCheckedChangeListener() {
324             @Override
325             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
326                 if (LOCAL_LOGV) Log.v(TAG, String.format("onCheckedChanged(%s)", isChecked));
327                 if(ctx instanceof MainActivity) {
328                     // call MainActivity
329                     ((MainActivity) ctx).toggleLedSwitch(resourceList.get(groupPosition),
330                             isChecked);
331                 }
332             }
333         });
334         toggleSwitch.setVisibility(View.VISIBLE);
335     }
336
337     /**
338      * Type-specific layout render for Room Temperature Sensor Reading attribute.
339      */
340     private void renderRoomTemperatureSensorReading(final View view,
341                                                      final int groupPosition,
342                                                      final OcAttributeInfo attribute) {
343         // this happens to have the same behavior as ambient light sensor, so just re-use
344         this.renderAmbientLightSensorReading(view, groupPosition, attribute);
345     }
346
347     private String getAttributeLabelFromType(OC_ATTRIBUTE_TYPE type) {
348         switch (type) {
349             case AMBIENT_LIGHT_SENSOR_READING:
350                 return ctx.getString(R.string.ui_attribute_label_ambient_light_sensor_reading);
351             case LIGHT_DIMMER:
352                 return ctx.getString(R.string.ui_attribute_label_light_dimmer);
353             case LIGHT_SWITCH:
354                 return ctx.getString(R.string.ui_attribute_label_light_switch);
355             case PLATFORM_LED_SWITCH:
356                 return ctx.getString(R.string.ui_attribute_label_led_switch);
357             case ROOM_TEMPERATURE_SENSOR_READING:
358                 return ctx.getString(R.string.ui_attribute_label_room_temperature_sensor_reading);
359             default:
360                 Log.w(TAG, "getAttributeLabelFromType(): unrecognized attribute type.");
361                 return "Attribute:";
362         }
363     }
364
365     private String getResourceLabelFromType(OcResourceInfo.OC_RESOURCE_TYPE type) {
366         if (LOCAL_LOGV) Log.v(TAG, "getResourceLabelFromType()");
367
368         switch(type) {
369             case AMBIENT_LIGHT_SENSOR:
370                 return ctx.getString(R.string.ui_resource_label_ambient_light_sensor);
371             case LIGHT:
372                 return ctx.getString(R.string.ui_resource_label_light);
373             case PLATFORM_LED:
374                 return ctx.getString(R.string.ui_resource_label_platform_led);
375             case ROOM_TEMPERATURE_SENSOR:
376                 return ctx.getString(R.string.ui_resource_label_room_temperature_sensor);
377             default:
378                 Log.w(TAG, "getResourceLabelFromType(): unrecognized resource type.");
379                 return "Resource:";
380         }
381     }
382 }