1 //******************************************************************
3 // Copyright 2014 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.guiclient;
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;
37 import java.util.List;
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;
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.
50 * @see org.iotivity.guiclient.OcAttributeInfo
52 public class ExpandableResourceListAdapter extends BaseExpandableListAdapter {
54 * Hardcoded TAG... if project never uses proguard then
55 * MyOcClient.class.getName() is the better way.
57 private static final String TAG = "ExpandableResourceListAdapter";
59 private static final boolean LOCAL_LOGV = true; // set to false to compile out verbose logging
61 private List<OcResourceInfo> resourceList;
64 public ExpandableResourceListAdapter(List<OcResourceInfo> resourceList, Context ctx) {
65 this.resourceList = resourceList;
70 public Object getChild(int groupPosition, int childPosition) {
71 return resourceList.get(groupPosition).getAttributes().get(childPosition);
75 public int getChildrenCount(int groupPosition) {
76 return resourceList.get(groupPosition).getAttributes().size();
80 public long getChildId(int groupPosition, int childPosition) {
81 return resourceList.get(groupPosition).getAttributes().get(childPosition).hashCode();
85 public int getChildType(int groupPosition, int childPosition) {
86 return this.resourceList.get(groupPosition).getAttributes().get(childPosition)
91 public int getChildTypeCount() {
92 return OC_ATTRIBUTE_TYPE.values().length;
96 public View getChildView(final int groupPosition, int childPosition,
97 boolean isLastChild, View convertView, ViewGroup parent) {
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);
108 v = inflater.inflate(R.layout.attribute_layout_slider, parent, false);
111 case PLATFORM_LED_SWITCH:
112 v = inflater.inflate(R.layout.attribute_layout_on_off_switch, parent, false);
117 OcAttributeInfo attribute =
118 resourceList.get(groupPosition).getAttributes().get(childPosition);
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);
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);
134 this.renderLightDimmer(v, groupPosition, attribute);
137 this.renderLightSwitch(v, groupPosition, attribute);
139 case PLATFORM_LED_SWITCH:
140 this.renderPlatformLedSwitch(v, groupPosition, attribute);
142 case ROOM_TEMPERATURE_SENSOR_READING:
143 this.renderRoomTemperatureSensorReading(v, groupPosition, attribute);
151 public Object getGroup(int groupPosition) {
152 return resourceList.get(groupPosition);
156 public int getGroupCount() {
157 return resourceList.size();
161 public long getGroupId(int groupPosition) {
162 return resourceList.get(groupPosition).hashCode();
166 public View getGroupView(int groupPosition, boolean isExpanded,
167 View convertView, ViewGroup parent) {
169 View v = convertView;
172 LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
173 (Context.LAYOUT_INFLATER_SERVICE);
174 v = inflater.inflate(R.layout.resource_list_item_layout, parent, false);
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);
181 OcResourceInfo resource = resourceList.get(groupPosition);
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);
190 resourceIcon.setImageResource(R.drawable.light_icon);
193 resourceIcon.setImageResource(R.drawable.led_icon);
195 case ROOM_TEMPERATURE_SENSOR:
196 resourceIcon.setImageResource(R.drawable.thermometer_icon);
199 resourceIcon.setImageResource(R.drawable.iotivity_hex_icon);
207 public boolean hasStableIds() {
212 public boolean isChildSelectable(int groupPosition, int childPosition) {
217 * Type-specific layout render for Ambient Light Sensor reading attribute.
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);
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);
235 * Type-specific layout render for Light Dimmer attribute.
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);
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;
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;
257 public void onStartTrackingTouch(SeekBar seekBar) {}
259 public void onStopTrackingTouch(SeekBar seekBar) {
260 if (LOCAL_LOGV) Log.v(TAG, "onStopTrackingTouch()");
261 if(ctx instanceof MainActivity) {
263 ((MainActivity) ctx).setLightDimmerLevel(resourceList.get(groupPosition),
268 slider.setVisibility(View.VISIBLE);
272 * Type-specific layout render for Light Switch attribute.
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");
282 attributeValue.setText("on");
284 attributeValue.setVisibility(View.VISIBLE);
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() {
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) {
296 ((MainActivity) ctx).toggleLightSwitch(resourceList.get(groupPosition),
301 toggleSwitch.setVisibility(View.VISIBLE);
305 * Type-specific layout render for LED Switch attribute.
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");
315 attributeValue.setText("off");
317 attributeValue.setVisibility(View.VISIBLE);
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() {
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) {
329 ((MainActivity) ctx).toggleLedSwitch(resourceList.get(groupPosition),
334 toggleSwitch.setVisibility(View.VISIBLE);
338 * Type-specific layout render for Room Temperature Sensor Reading attribute.
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);
347 private String getAttributeLabelFromType(OC_ATTRIBUTE_TYPE type) {
349 case AMBIENT_LIGHT_SENSOR_READING:
350 return ctx.getString(R.string.ui_attribute_label_ambient_light_sensor_reading);
352 return ctx.getString(R.string.ui_attribute_label_light_dimmer);
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);
360 Log.w(TAG, "getAttributeLabelFromType(): unrecognized attribute type.");
365 private String getResourceLabelFromType(OcResourceInfo.OC_RESOURCE_TYPE type) {
366 if (LOCAL_LOGV) Log.v(TAG, "getResourceLabelFromType()");
369 case AMBIENT_LIGHT_SENSOR:
370 return ctx.getString(R.string.ui_resource_label_ambient_light_sensor);
372 return ctx.getString(R.string.ui_resource_label_light);
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);
378 Log.w(TAG, "getResourceLabelFromType(): unrecognized resource type.");