1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.android.car.kitchensink.property; 18 19 import static java.lang.Integer.toHexString; 20 21 import android.car.hardware.CarPropertyConfig; 22 import android.car.hardware.CarPropertyValue; 23 import android.car.hardware.property.CarPropertyManager; 24 import android.car.hardware.property.CarPropertyManager.CarPropertyEventCallback; 25 import android.content.Context; 26 import android.util.Log; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.BaseAdapter; 31 import android.widget.ListAdapter; 32 import android.widget.ScrollView; 33 import android.widget.TextView; 34 import android.widget.ToggleButton; 35 36 import com.google.android.car.kitchensink.R; 37 38 import java.util.List; 39 40 class PropertyListAdapter extends BaseAdapter implements ListAdapter { 41 private static final int DEFAULT_RATE = 1; 42 private static final String TAG = "PropertyListAdapter"; 43 private final Context mContext; 44 private final PropertyListEventListener mListener; 45 private final CarPropertyManager mMgr; 46 private final List<PropertyInfo> mPropInfo; 47 private final TextView mTvEventLog; 48 PropertyListAdapter(List<PropertyInfo> propInfo, CarPropertyManager mgr, TextView eventLog, ScrollView svEventLog, Context context)49 PropertyListAdapter(List<PropertyInfo> propInfo, CarPropertyManager mgr, TextView eventLog, 50 ScrollView svEventLog, Context context) { 51 mContext = context; 52 mListener = new PropertyListEventListener(eventLog, svEventLog); 53 mMgr = mgr; 54 mPropInfo = propInfo; 55 mTvEventLog = eventLog; 56 } 57 58 @Override getCount()59 public int getCount() { 60 return mPropInfo.size(); 61 } 62 63 @Override getItem(int pos)64 public Object getItem(int pos) { 65 return mPropInfo.get(pos); 66 } 67 68 @Override getItemId(int pos)69 public long getItemId(int pos) { 70 return mPropInfo.get(pos).mPropId; 71 } 72 73 @Override getView(final int position, View convertView, ViewGroup parent)74 public View getView(final int position, View convertView, ViewGroup parent) { 75 View view = convertView; 76 if (view == null) { 77 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( 78 Context.LAYOUT_INFLATER_SERVICE); 79 view = inflater.inflate(R.layout.property_list_item, null); 80 } 81 82 //Handle TextView and display string from your list 83 TextView listItemText = (TextView) view.findViewById(R.id.tvPropertyName); 84 listItemText.setText(mPropInfo.get(position).toString()); 85 86 //Handle buttons and add onClickListeners 87 ToggleButton btn = (ToggleButton) view.findViewById(R.id.tbRegisterPropertyBtn); 88 89 btn.setOnClickListener(new View.OnClickListener(){ 90 @Override 91 public void onClick(View v) { 92 CarPropertyConfig c = mPropInfo.get(position).mConfig; 93 int propId = c.getPropertyId(); 94 try { 95 if (btn.isChecked()) { 96 mMgr.registerCallback(mListener, propId, DEFAULT_RATE); 97 } else { 98 mMgr.unregisterCallback(mListener, propId); 99 } 100 } catch (Exception e) { 101 Log.e(TAG, "Unhandled exception: ", e); 102 } 103 } 104 }); 105 return view; 106 } 107 108 109 private static class PropertyListEventListener implements CarPropertyEventCallback { 110 private int mNumEvents; 111 private ScrollView mScrollView; 112 private TextView mTvLogEvent; 113 PropertyListEventListener(TextView logEvent, ScrollView scrollView)114 PropertyListEventListener(TextView logEvent, ScrollView scrollView) { 115 mScrollView = scrollView; 116 mTvLogEvent = logEvent; 117 } 118 119 @Override onChangeEvent(CarPropertyValue value)120 public void onChangeEvent(CarPropertyValue value) { 121 mNumEvents++; 122 mTvLogEvent.append("Event " + mNumEvents + ":" 123 + " time=" + value.getTimestamp() 124 + " propId=0x" + toHexString(value.getPropertyId()) 125 + " areaId=0x" + toHexString(value.getAreaId()) 126 + " status=" + value.getStatus() 127 + " value=" + value.getValue() 128 + "\n"); 129 scrollToBottom(); 130 } 131 132 @Override onErrorEvent(int propId, int areaId)133 public void onErrorEvent(int propId, int areaId) { 134 mTvLogEvent.append("Received error event propId=0x" + toHexString(propId) 135 + ", areaId=0x" + toHexString(areaId)); 136 scrollToBottom(); 137 } 138 scrollToBottom()139 private void scrollToBottom() { 140 mScrollView.post(new Runnable() { 141 public void run() { 142 mScrollView.fullScroll(View.FOCUS_DOWN); 143 //mScrollView.smoothScrollTo(0, mTextStatus.getBottom()); 144 } 145 }); 146 } 147 148 } 149 } 150