Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLiuZhuoCheng committed Nov 4, 2021
1 parent 993110b commit 9568f84
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 374 deletions.
17 changes: 17 additions & 0 deletions src/android/CHIPTool/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.google.chip.chiptool.clusterclient.clusterinteraction

import android.os.Bundle
import android.text.Layout
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.LinearLayout
Expand All @@ -17,24 +15,25 @@ import androidx.fragment.app.Fragment
import chip.clusterinfo.ClusterCommandCallback
import chip.clusterinfo.ClusterInfo
import chip.clusterinfo.CommandInfo
import chip.clusterinfo.CommandResponseInfo
import chip.clusterinfo.DelegatedClusterCallback
import chip.clusterinfo.ResponseValueInfo
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ClusterInfoMapping
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.callback_item.view.callbackData
import kotlinx.android.synthetic.main.callback_item.view.callbackName
import kotlinx.android.synthetic.main.callback_item.view.callbackType
import kotlinx.android.synthetic.main.cluster_callback_item.view.clusterCallbackDataTv
import kotlinx.android.synthetic.main.cluster_callback_item.view.clusterCallbackNameTv
import kotlinx.android.synthetic.main.cluster_callback_item.view.clusterCallbackTypeTv
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.callbackList
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.clusterAutoComplete
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.commandAutoComplete
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.clusterAutoCompleteTv
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.commandAutoCompleteTv
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.invokeCommand
import kotlinx.android.synthetic.main.cluster_detail_fragment.view.parameterList
import kotlinx.android.synthetic.main.parameter_item.view.parameterData
import kotlinx.android.synthetic.main.parameter_item.view.parameterName
import kotlinx.android.synthetic.main.parameter_item.view.parameterType
import kotlinx.android.synthetic.main.cluster_parameter_item.view.clusterParameterData
import kotlinx.android.synthetic.main.cluster_parameter_item.view.clusterParameterNameTv
import kotlinx.android.synthetic.main.cluster_parameter_item.view.clusterParameterTypeTv
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -49,44 +48,43 @@ class ClusterDetailFragment : Fragment() {
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var clusterMap: HashMap<String, ClusterInfo>
private lateinit var clusterMap: Map<String, ClusterInfo>
private lateinit var selectedClusterInfo: ClusterInfo
private lateinit var selectedCluster: ChipClusters.BaseChipCluster
private lateinit var selectedCommandCallback: DelegatedClusterCallback
private var commandArguments = HashMap<String, Any>()
private lateinit var selectedCommandInfo: CommandInfo
private var devicePtr = 0L
private var endpointId = 1
private var endpointId = 0

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
clusterMap =
checkNotNull(requireArguments().getSerializable(CLUSTER_MAP_INFO)) as HashMap<String, ClusterInfo>
clusterMap = ClusterInfoMapping().clusterMap
devicePtr = checkNotNull(requireArguments().getLong(DEVICE_PTR))
endpointId = checkNotNull(requireArguments().getInt(ENDPOINT_ID))
return inflater.inflate(R.layout.cluster_detail_fragment, container, false).apply {
deviceController.setCompletionListener(GenericChipDeviceListener())
commandAutoComplete.visibility = View.GONE
clusterAutoCompleteSetup(clusterAutoComplete, commandAutoComplete, parameterList)
commandAutoCompleteSetup(commandAutoComplete, inflater, parameterList, callbackList)
commandAutoCompleteTv.visibility = View.GONE
clusterAutoCompleteSetup(clusterAutoCompleteTv, commandAutoCompleteTv, parameterList)
commandAutoCompleteSetup(commandAutoCompleteTv, inflater, parameterList, callbackList)
invokeCommand.setOnClickListener {
val commandArguments = HashMap<String, Any>()
parameterList.forEach {
val type =
selectedCommandInfo.commandParameters[it.parameterName.text.toString()]!!.type!!
val data = castCorrectType(type, it.parameterData.text.toString())!!
commandArguments[it.parameterName.text.toString()] = data
}
selectedCommandInfo.commandParameters[it.clusterParameterNameTv.text.toString()]!!.type
val data = castStringToType(it.clusterParameterData.text.toString(), type)!!

commandArguments[it.clusterParameterNameTv.text.toString()] = data
}
selectedCommandInfo.getCommandFunction()
.invokeCommand(selectedCluster, selectedCommandCallback, commandArguments)
}
}
}

private fun castCorrectType(type: Class<*>, data: String): Any? {

private fun castStringToType(data: String, type: Class<*>): Any? {
return when (type) {
Int::class.java -> data.toInt()
String::class.java -> data
Expand All @@ -95,7 +93,6 @@ class ClusterDetailFragment : Fragment() {
}
}


private fun showMessage(msg: String) {
requireActivity().runOnUiThread {
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
Expand Down Expand Up @@ -137,8 +134,8 @@ class ClusterDetailFragment : Fragment() {
selectedCommandInfo = selectedClusterInfo.commands[selectedCommand]!!
selectedCommandCallback = selectedCommandInfo.commandCallbackSupplier.get()
populateCommandParameter(inflater, parameterList)
selectedCommandCallback!!.setCallbackDelegate(object : ClusterCommandCallback {
override fun onSuccess(responseValues: Map<ResponseValueInfo, Any>) {
selectedCommandCallback.setCallbackDelegate(object : ClusterCommandCallback {
override fun onSuccess(responseValues: Map<CommandResponseInfo, Any>) {
showMessage("Command success")
// Populate UI based on response values. We know the types from CommandInfo.getCommandResponses().
requireActivity().runOnUiThread {
Expand All @@ -160,25 +157,25 @@ class ClusterDetailFragment : Fragment() {
}

private fun populateCommandParameter(inflater: LayoutInflater, parameterList: LinearLayout) {

selectedCommandInfo.commandParameters.forEach { (paramName, paramInfo) ->
val param = inflater.inflate(R.layout.parameter_item, null, false) as ConstraintLayout
param.parameterName.text = "${paramName}"
param.parameterType.text = "${paramInfo.type}"
val param = inflater.inflate(R.layout.cluster_parameter_item, null, false) as ConstraintLayout
param.clusterParameterNameTv.text = "${paramName}"
param.clusterParameterTypeTv.text = "${paramInfo.type}"
parameterList.addView(param)
}
}

private fun populateCallbackResult(
responseValues: Map<ResponseValueInfo, Any>,
responseValues: Map<CommandResponseInfo, Any>,
inflater: LayoutInflater,
callbackList: LinearLayout
) {
responseValues.forEach { (variableNameType, response) ->
val callback = inflater.inflate(R.layout.callback_item, null, false) as ConstraintLayout
callback.callbackName.text = variableNameType.name
callback.callbackData.text = response.toString()
callback.callbackType.text = variableNameType.type
val callback =
inflater.inflate(R.layout.cluster_callback_item, null, false) as ConstraintLayout
callback.clusterCallbackNameTv.text = variableNameType.name
callback.clusterCallbackDataTv.text = response.toString()
callback.clusterCallbackTypeTv.text = variableNameType.type
callbackList.addView(callback)
}
}
Expand All @@ -187,11 +184,11 @@ class ClusterDetailFragment : Fragment() {
clusterName: String
): ArrayAdapter<String> {
selectedClusterInfo = clusterMap[clusterName]!!
val commandNameList = constructHint(selectedClusterInfo.commands as HashMap<String, Any>)
val commandNameList = constructHint(selectedClusterInfo.commands)
return ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, commandNameList)
}

private fun constructHint(clusterMap: HashMap<String, *>): Array<String> {
private fun constructHint(clusterMap: Map<String, *>): Array<String> {
val clusterName = mutableListOf<String>()
for ((name, info) in clusterMap) {
clusterName.add(name)
Expand All @@ -206,19 +203,18 @@ class ClusterDetailFragment : Fragment() {

companion object {
private const val TAG = "ClusterDetailFragment"
private const val CLUSTER_MAP_INFO = "cluster_map_info"
private const val ENDPOINT_ID = "endpoint_id"
private const val DEVICE_PTR = "device_ptr"
fun newInstance(
clusterMap: HashMap<String, ClusterInfo>,
deviceId: Long
deviceId: Long,
endpointId: Int
): ClusterDetailFragment {
return ClusterDetailFragment().apply {
arguments = Bundle(1).apply {
putSerializable(CLUSTER_MAP_INFO, clusterMap)
arguments = Bundle(2).apply {
putLong(DEVICE_PTR, deviceId)
putInt(ENDPOINT_ID, endpointId)
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.google.chip.chiptool.clusterclient.clusterinteraction

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import chip.clusterinfo.ClusterInfo
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
Expand All @@ -17,7 +15,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import chip.devicecontroller.ClusterInfoMapping
import com.google.chip.chiptool.clusterclient.AddressUpdateFragment
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.endpointList
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.getEndpointListBtn
Expand All @@ -29,7 +26,6 @@ class ClusterInteractionFragment : Fragment() {

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var addressUpdateFragment: AddressUpdateFragment
private lateinit var clusterMap: Map<String, ClusterInfo>
private var devicePtr = 0L

override fun onCreateView(
Expand All @@ -48,10 +44,8 @@ class ClusterInteractionFragment : Fragment() {
endpointList.visibility = View.VISIBLE
}
}

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment
clusterMap = ClusterInfoMapping().clusterMap
var dataList: List<EndpointItem> = ArrayList()
// TODO: Dynamically retrieve endpoint information using descriptor cluster
// hardcode the endpoint for now
Expand Down Expand Up @@ -94,7 +88,7 @@ class ClusterInteractionFragment : Fragment() {
inner class EndpointListener : EndpointAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
Toast.makeText(requireContext(), "Item $position clicked", Toast.LENGTH_SHORT).show()
showFragment(ClusterDetailFragment.newInstance(clusterMap as HashMap<String, ClusterInfo>, devicePtr))
showFragment(ClusterDetailFragment.newInstance(devicePtr, position))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/callbackRow"
android:id="@+id/clusterCallbackRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_height="wrap_content">

<TextView
android:id="@+id/callbackName"
android:id="@+id/clusterCallbackNameTv"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:padding="16dp"
android:minWidth="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/clusterCallbackDataTv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/callbackData"
android:padding="16dp" />
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/callbackData"
android:id="@+id/clusterCallbackDataTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:singleLine="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/callbackType"
app:layout_constraintStart_toEndOf="@id/callbackName"
app:layout_constraintEnd_toStartOf="@id/clusterCallbackTypeTv"
app:layout_constraintStart_toEndOf="@id/clusterCallbackNameTv"
app:layout_constraintTop_toTopOf="parent" />


<TextView
android:id="@+id/callbackType"
android:id="@+id/clusterCallbackTypeTv"
android:textStyle="bold"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:singleLine="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/callbackData"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/clusterCallbackDataTv"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/clusterAutoComplete"
android:id="@+id/clusterAutoCompleteTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="Select a cluster"
android:completionHint="@string/select_a_cluster"
android:completionThreshold="0"
android:hint="Select a cluster"
android:minHeight="48dp"></AutoCompleteTextView>
android:hint="@string/select_a_cluster"
android:minHeight="48dp" />

<AutoCompleteTextView
android:id="@+id/commandAutoComplete"
android:id="@+id/commandAutoCompleteTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="Select a cluster"
android:completionHint="@string/select_a_command"
android:completionThreshold="0"
android:hint="Select a command"
android:minHeight="48dp"></AutoCompleteTextView>
android:hint="@string/select_a_command"
android:minHeight="48dp" />

<Button
android:id="@+id/invokeCommand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/invoke" />

<LinearLayout
android:id="@+id/parameterList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_below="@id/commandAutoComplete"
android:layout_below="@id/commandAutoCompleteTv"
android:orientation="vertical" />

<Button
android:id="@+id/invokeCommand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="Invoke" />

<LinearLayout
android:id="@+id/callbackList"
android:layout_width="match_parent"
Expand All @@ -47,5 +45,4 @@
android:layout_below="@+id/invokeCommand"
android:orientation="vertical" />


</LinearLayout>
Loading

0 comments on commit 9568f84

Please sign in to comment.