Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display endpoint list #10942

Merged
merged 38 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
68ecd7e
basic code generation template, but not able to import to tool app
JasonLiuZhuoCheng Oct 14, 2021
dfbd736
change package
JasonLiuZhuoCheng Oct 14, 2021
8abefbd
no error code generation
JasonLiuZhuoCheng Oct 14, 2021
45fcc82
new design solution
JasonLiuZhuoCheng Oct 15, 2021
a45351b
need to wait for cast-helper
JasonLiuZhuoCheng Oct 16, 2021
e7dda33
callback generation done
JasonLiuZhuoCheng Oct 16, 2021
b75f47b
on/off commands working
JasonLiuZhuoCheng Oct 18, 2021
5998f2b
revert .idea changes
JasonLiuZhuoCheng Oct 18, 2021
134b0b4
one more .idea change revert
JasonLiuZhuoCheng Oct 18, 2021
0ffbf5b
remove outline.java
JasonLiuZhuoCheng Oct 18, 2021
da0685d
Restyled by whitespace
restyled-commits Oct 18, 2021
969be5f
Restyled by google-java-format
restyled-commits Oct 18, 2021
7fd470c
Restyled by gn
restyled-commits Oct 18, 2021
d40904f
Restyled by google-java-format
restyled-commits Oct 18, 2021
4969465
fix merge
JasonLiuZhuoCheng Oct 18, 2021
401fac5
merge conflict
JasonLiuZhuoCheng Oct 18, 2021
433e622
fix comments
JasonLiuZhuoCheng Oct 19, 2021
4f10303
Restyled by gn
restyled-commits Oct 19, 2021
4f0aab5
resolve type, nullable and format comments
JasonLiuZhuoCheng Oct 20, 2021
a7b467d
resolve build issues
JasonLiuZhuoCheng Oct 20, 2021
61779a6
Restyled by gn
restyled-commits Oct 20, 2021
985cca7
update zap generated file
JasonLiuZhuoCheng Oct 20, 2021
c2a424a
add descriptive documentation on each new class
JasonLiuZhuoCheng Oct 20, 2021
56417fd
Restyled by google-java-format
restyled-commits Oct 20, 2021
19ac32a
modify description of each new added class
JasonLiuZhuoCheng Oct 21, 2021
e8e545d
Restyled by google-java-format
restyled-commits Oct 21, 2021
2b9236d
add . at the end of class description
JasonLiuZhuoCheng Oct 21, 2021
a5c4a03
Restyled by google-java-format
restyled-commits Oct 21, 2021
fe8f793
add recycler view to display endpoint
JasonLiuZhuoCheng Oct 25, 2021
9a14b21
recycler view for endpoint display and test cluster detail screen done
JasonLiuZhuoCheng Oct 26, 2021
2a93632
change number of hard-coded enpoint to 2
JasonLiuZhuoCheng Oct 26, 2021
2f839b7
endpoint first draft done
JasonLiuZhuoCheng Oct 28, 2021
898409a
resolve conflict
JasonLiuZhuoCheng Oct 28, 2021
7a86db8
remove duplicate todo
JasonLiuZhuoCheng Oct 28, 2021
132a828
fix comments
JasonLiuZhuoCheng Oct 28, 2021
dc1f042
Restyled by whitespace
restyled-commits Oct 28, 2021
e5ff0d5
resolve comments
JasonLiuZhuoCheng Oct 28, 2021
1c859a5
fix comments
JasonLiuZhuoCheng Oct 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import chip.setuppayload.SetupPayload
import chip.setuppayload.SetupPayloadParser
import chip.setuppayload.SetupPayloadParser.UnrecognizedQrCodeException
import com.google.chip.chiptool.attestation.AttestationTestFragment
import com.google.chip.chiptool.clusterclient.ClusterInteractionFragment
import com.google.chip.chiptool.clusterclient.clusterinteraction.ClusterInteractionFragment
import com.google.chip.chiptool.clusterclient.MultiAdminClientFragment
import com.google.chip.chiptool.clusterclient.OpCredClientFragment
import com.google.chip.chiptool.clusterclient.BasicClientFragment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.google.chip.chiptool.clusterclient.clusterinteraction

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel

/**
* ClusterDetailFragment allows user to pick cluster, command, specify parameters and see
* the callback result.
*/
class ClusterDetailFragment : Fragment(){
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.cluster_detail_fragment, container, false).apply {
deviceController.setCompletionListener(GenericChipDeviceListener())
}
}

private fun showMessage(msg: String) {
requireActivity().runOnUiThread {
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

companion object {
private const val TAG = "ClusterDetailFragment"
fun newInstance(): ClusterDetailFragment = ClusterDetailFragment()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.google.chip.chiptool.clusterclient
package com.google.chip.chiptool.clusterclient.clusterinteraction

import android.os.Bundle
import android.util.Log
Expand All @@ -7,9 +7,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import chip.clusterinfo.ClusterCommandCallback
import androidx.recyclerview.widget.LinearLayoutManager
import chip.clusterinfo.ClusterInfo
import chip.clusterinfo.CommandInfo
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
Expand All @@ -19,8 +18,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import chip.devicecontroller.ClusterInfoMapping
import java.lang.Exception
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.getClusterMappingBtn
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
import kotlinx.coroutines.launch

class ClusterInteractionFragment : Fragment() {
Expand All @@ -38,41 +38,25 @@ class ClusterInteractionFragment : Fragment() {
): View {
return inflater.inflate(R.layout.cluster_interaction_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())
addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment
clusterMap = ClusterInfoMapping().clusterMap;
getClusterMappingBtn.setOnClickListener { scope.launch { getClusterMapping() } }
}
}

private suspend fun getClusterMapping() {
// In real code: "OnOff" would be selected by the user.
val methodSelected = "onOff"
showMessage(methodSelected + " is initialized")
val selectedClusterInfo = clusterMap[methodSelected]!!
val devicePtr =
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId)
val endpointId = 1
val selectedCluster = selectedClusterInfo.createClusterFunction.create(devicePtr, endpointId)
// Imagine user wants to execute the command "OffWithEffect", pass the string here
val selectedCommandInfo: CommandInfo = selectedClusterInfo.commands["on"]!!

var selectedCommandCallback = selectedCommandInfo.commandCallbackSupplier.get()
selectedCommandCallback?.setCallbackDelegate(object : ClusterCommandCallback {
override fun onSuccess(responseValues: List<Any>) {
showMessage("Command success")
// Populate UI based on response values. We know the types from CommandInfo.getCommandResponses().
responseValues.forEach { Log.d(TAG, it.toString()) }
getEndpointListBtn.setOnClickListener {
scope.launch {
showMessage("Retrieving endpoints")
endpointList.visibility = View.VISIBLE
}
}

override fun onFailure(exception: Exception) {
showMessage("Command failed")
Log.e(TAG, exception.toString())
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
for (i in 0 until 2) {
dataList += EndpointItem(i)
}
})

var commandArguments: HashMap<String, Any> = HashMap<String, Any>()
selectedCommandInfo.getCommandFunction().invokeCommand(selectedCluster, selectedCommandCallback, commandArguments)
endpointList.adapter = EndpointAdapter(dataList, EndpointListener())
endpointList.layoutManager = LinearLayoutManager(requireContext())
}
}

private fun showMessage(msg: String) {
Expand Down Expand Up @@ -109,4 +93,23 @@ class ClusterInteractionFragment : Fragment() {
private const val TAG = "ClusterInteractionFragment"
fun newInstance(): ClusterInteractionFragment = ClusterInteractionFragment()
}

private fun showFragment(fragment: Fragment, showOnBack: Boolean = true) {
val fragmentTransaction = requireActivity().supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment, fragment.javaClass.simpleName)

if (showOnBack) {
fragmentTransaction.addToBackStack(null)
}

fragmentTransaction.commit()
}

inner class EndpointListener : EndpointAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
Toast.makeText(requireContext(), "Item $position clicked", Toast.LENGTH_SHORT).show()
showFragment(ClusterDetailFragment.newInstance())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.google.chip.chiptool.clusterclient.clusterinteraction

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.chip.chiptool.R

/**
* EndpointAdapter implements the endpointList(RecycleView) Adapter and associates different
* endpoint with the same onClick function provided in [ClusterInteractionFragment.EndpointListener]
*/
class EndpointAdapter(
private val endpointList: List<EndpointItem>,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<EndpointAdapter.EndpointViewHolder>() {

inner class EndpointViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener {
val endpointId: TextView = itemView.findViewById(R.id.endpointTv)

init {
itemView.setOnClickListener(this)
}

override fun onClick(endpointItem: View) {
val position = this.adapterPosition
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position)
}
}
}

interface OnItemClickListener {
fun onItemClick(position: Int)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EndpointViewHolder {
val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.endpoint_item, parent, false)
return EndpointViewHolder(itemView)
}

override fun onBindViewHolder(holder: EndpointViewHolder, position: Int) {
holder.endpointId.text = endpointList[position].endpointId.toString()
}

override fun getItemCount(): Int {
return endpointList.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.google.chip.chiptool.clusterclient.clusterinteraction

data class EndpointItem(val endpointId: Int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
android:layout_height="match_parent">

<Button
android:id="@+id/getClusterMappingBtn"
android:id="@+id/getEndpointListBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="132dp"
android:layout_marginStart="16dp"
android:text="Button"
android:text="@string/retrieve_endpoint_list"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
Expand All @@ -26,10 +26,9 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/endpointList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="180dp"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addressUpdateFragment" />
app:layout_constraintTop_toBottomOf="@+id/getEndpointListBtn" />

</androidx.constraintlayout.widget.ConstraintLayout>
30 changes: 30 additions & 0 deletions src/android/CHIPTool/app/src/main/res/layout/endpoint_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:layout_margin="4dp">

<TextView
android:id="@+id/endpointTv"
android:textStyle="bold"
android:width="100dp"
android:height="70dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="@android:color/black"
android:textSize="18sp"
android:background="@color/cardview_shadow_start_color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
4 changes: 2 additions & 2 deletions src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<string name="op_cred_client_btn_text">Operational Credentials cluster</string>
<string name="op_cred_client_read_supported_fabric_btn_text">Read Supported Fabric count</string>
<string name="op_cred_client_read_commissioned_fabric_btn_text">Read Commissioned Fabric count</string>

<string name="basic_cluster_btn_text">Basic cluster</string>
<string name="basic_cluster_read_user_label_btn_text">Read User Label</string>
<string name="basic_cluster_write_user_label_btn_text">Write User Label</string>
Expand All @@ -123,5 +123,5 @@
<string name="dcl_response_key">result</string>
<string name="dcl_custom_flow_url_key">custom</string>
<string name="custom_flow_return_url"></string>

<string name="retrieve_endpoint_list">Retrieve Endpoint List</string>
</resources>
4 changes: 2 additions & 2 deletions src/controller/java/templates/ClusterInfo-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class ClusterInfoMapping {
Map<String, CommandInfo> {{asLowerCamelCase name}}ClusterCommandInfoMap = new LinkedHashMap<>();
{{#chip_cluster_commands}}
Map<String, CommandParameterInfo> {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParams = new LinkedHashMap<String, CommandParameterInfo>();
// PLEASE UPDATE LATER: fill out parameter types
{{! TODO: fill out parameter types }}
{{#if (zcl_command_arguments_count this.id)}}
{{#if hasSpecificResponse}}
CommandParameterInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParameterInfo = new CommandParameterInfo("{{asUpperCamelCase ../name}}", ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase responseName}}Callback.class);
Expand Down Expand Up @@ -150,4 +150,4 @@ public class ClusterInfoMapping {
}
}

{{/if}}
{{/if}}
Loading