Skip to content

Commit 1467620

Browse files
JasonLiuZhuoChengpull[bot]
authored andcommitted
Enable complex data type UI (#11989)
* fix byte[] displaying and enable user to specify byte[] value * use encodeToByteArray and decodeToString * change variable naming
1 parent e587b4d commit 1467620

File tree

1 file changed

+35
-14
lines changed
  • src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction

1 file changed

+35
-14
lines changed

src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/ClusterDetailFragment.kt

+35-14
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class ClusterDetailFragment : Fragment() {
9595
return when (type) {
9696
Int::class.java -> data.toInt()
9797
Boolean::class.java -> data.toBoolean()
98+
ByteArray::class.java -> data.encodeToByteArray()
99+
Long::class.java -> data.toLong()
98100
else -> data
99101
}
100102
}
@@ -169,7 +171,14 @@ class ClusterDetailFragment : Fragment() {
169171
selectedInteractionInfo.commandParameters.forEach { (paramName, paramInfo) ->
170172
val param = inflater.inflate(R.layout.cluster_parameter_item, null, false) as ConstraintLayout
171173
param.clusterParameterNameTv.text = "${paramName}"
172-
param.clusterParameterTypeTv.text = "${paramInfo.type}"
174+
// byte[].class will be output as class [B, which is not readable, so dynamically change it
175+
// to Byte[]. If more custom logic is required, should add a className field in
176+
// commandParameterInfo
177+
if (paramInfo.type == ByteArray::class.java) {
178+
param.clusterParameterTypeTv.text = "Byte[]"
179+
} else {
180+
param.clusterParameterTypeTv.text = "${paramInfo.type}"
181+
}
173182
parameterList.addView(param)
174183
}
175184
}
@@ -181,14 +190,14 @@ class ClusterDetailFragment : Fragment() {
181190
) {
182191
responseValues.forEach { (variableNameType, response) ->
183192
if (response is List<*>) {
184-
createListReadAttributeView(response, inflater, callbackList, variableNameType)
193+
createListResponseView(response, inflater, callbackList, variableNameType)
185194
} else {
186-
createBasicReadAttributeView(response, inflater, callbackList, variableNameType)
195+
createBasicResponseView(response, inflater, callbackList, variableNameType)
187196
}
188197
}
189198
}
190199

191-
private fun createBasicReadAttributeView(
200+
private fun createBasicResponseView(
192201
response: Any,
193202
inflater: LayoutInflater,
194203
callbackList: LinearLayout,
@@ -197,12 +206,16 @@ class ClusterDetailFragment : Fragment() {
197206
val callbackItem =
198207
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
199208
callbackItem.clusterCallbackNameTv.text = variableNameType.name
200-
callbackItem.clusterCallbackDataTv.text = response.toString()
209+
callbackItem.clusterCallbackDataTv.text = if (response.javaClass == ByteArray::class.java) {
210+
(response as ByteArray).decodeToString()
211+
} else {
212+
response.toString()
213+
}
201214
callbackItem.clusterCallbackTypeTv.text = variableNameType.type
202215
callbackList.addView(callbackItem)
203216
}
204217

205-
private fun createListReadAttributeView(
218+
private fun createListResponseView(
206219
response: List<*>,
207220
inflater: LayoutInflater,
208221
callbackList: LinearLayout,
@@ -215,21 +228,29 @@ class ClusterDetailFragment : Fragment() {
215228
callbackList.addView(emptyCallback)
216229
} else {
217230
response.forEachIndexed { index, it ->
218-
val readAttributeCallbackItem =
231+
val attributeCallbackItem =
219232
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
220-
readAttributeCallbackItem.clusterCallbackNameTv.text = variableNameType.name + "[$index]"
221-
val objectString = it.toString()
222-
val callbackClassName = it!!.javaClass.toString().split('$').last()
223-
readAttributeCallbackItem.clusterCallbackDataTv.text = callbackClassName
224-
readAttributeCallbackItem.clusterCallbackDataTv.setOnClickListener {
233+
attributeCallbackItem.clusterCallbackNameTv.text = variableNameType.name + "[$index]"
234+
val objectString = if (it!!.javaClass == ByteArray::class.java) {
235+
(it as ByteArray).contentToString()
236+
} else {
237+
it.toString()
238+
}
239+
var callbackClassName = if (it!!.javaClass == ByteArray::class.java) {
240+
"Byte[]"
241+
} else {
242+
it!!.javaClass.toString().split('$').last()
243+
}
244+
attributeCallbackItem.clusterCallbackDataTv.text = callbackClassName
245+
attributeCallbackItem.clusterCallbackDataTv.setOnClickListener {
225246
AlertDialog.Builder(requireContext())
226247
.setTitle(callbackClassName)
227248
.setMessage(objectString)
228249
.create()
229250
.show()
230251
}
231-
readAttributeCallbackItem.clusterCallbackTypeTv.text = "List<$callbackClassName>"
232-
callbackList.addView(readAttributeCallbackItem)
252+
attributeCallbackItem.clusterCallbackTypeTv.text = "List<$callbackClassName>"
253+
callbackList.addView(attributeCallbackItem)
233254
}
234255
}
235256
}

0 commit comments

Comments
 (0)