forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement wildcard read/subscribe for Android (project-chip#14678)
- Loading branch information
Showing
29 changed files
with
17,507 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...oid/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.google.chip.chiptool.clusterclient | ||
|
||
import android.app.AlertDialog | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.EditText | ||
import androidx.appcompat.widget.MenuItemHoverListener | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import chip.devicecontroller.ChipDeviceController | ||
import chip.devicecontroller.ReportCallback | ||
import chip.devicecontroller.SubscriptionEstablishedCallback | ||
import chip.devicecontroller.model.ChipAttributePath | ||
import chip.devicecontroller.model.ChipPathId | ||
import com.google.chip.chiptool.ChipClient | ||
import com.google.chip.chiptool.R | ||
import kotlinx.android.synthetic.main.wildcard_fragment.attributeIdEd | ||
import kotlinx.android.synthetic.main.wildcard_fragment.clusterIdEd | ||
import kotlinx.android.synthetic.main.wildcard_fragment.endpointIdEd | ||
import kotlinx.android.synthetic.main.wildcard_fragment.outputTv | ||
import kotlinx.android.synthetic.main.wildcard_fragment.view.readBtn | ||
import kotlinx.android.synthetic.main.wildcard_fragment.view.subscribeBtn | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
class WildcardFragment : Fragment() { | ||
private val deviceController: ChipDeviceController | ||
get() = ChipClient.getDeviceController(requireContext()) | ||
|
||
private lateinit var scope: CoroutineScope | ||
|
||
private lateinit var addressUpdateFragment: AddressUpdateFragment | ||
|
||
private val reportCallback = object : ReportCallback { | ||
override fun onError(attributePath: ChipAttributePath, ex: Exception) { | ||
Log.i(TAG, "Report error for $attributePath: $ex") | ||
} | ||
|
||
override fun onReport(values: Map<ChipAttributePath, Any?>) { | ||
Log.i(TAG, "Received report with ${values.size} values") | ||
val builder = StringBuilder() | ||
values.forEach { builder.append("${it.key}: ${it.value}\n") } | ||
val output = builder.toString() | ||
|
||
Log.i(TAG, output) | ||
requireActivity().runOnUiThread { outputTv.text = output } | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
scope = viewLifecycleOwner.lifecycleScope | ||
return inflater.inflate(R.layout.wildcard_fragment, container, false).apply { | ||
subscribeBtn.setOnClickListener { scope.launch { showSubscribeDialog() } } | ||
readBtn.setOnClickListener { scope.launch { read() } } | ||
|
||
addressUpdateFragment = | ||
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment | ||
} | ||
} | ||
|
||
private suspend fun subscribe(minInterval: Int, maxInterval: Int) { | ||
val subscriptionEstablishedCallback = | ||
SubscriptionEstablishedCallback { Log.i(TAG, "Subscription to device established") } | ||
|
||
val endpointId = getChipPathIdForText(endpointIdEd.text.toString()) | ||
val clusterId = getChipPathIdForText(clusterIdEd.text.toString()) | ||
val attributeId = getChipPathIdForText(attributeIdEd.text.toString()) | ||
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId) | ||
|
||
deviceController.subscribeToPath(subscriptionEstablishedCallback, | ||
reportCallback, | ||
ChipClient.getConnectedDevicePointer(requireContext(), | ||
addressUpdateFragment.deviceId), | ||
attributePath, | ||
minInterval, | ||
maxInterval) | ||
} | ||
|
||
private suspend fun read() { | ||
val endpointId = getChipPathIdForText(endpointIdEd.text.toString()) | ||
val clusterId = getChipPathIdForText(clusterIdEd.text.toString()) | ||
val attributeId = getChipPathIdForText(attributeIdEd.text.toString()) | ||
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId) | ||
|
||
deviceController.readPath(reportCallback, | ||
ChipClient.getConnectedDevicePointer(requireContext(), | ||
addressUpdateFragment.deviceId), | ||
attributePath) | ||
} | ||
|
||
private fun showSubscribeDialog() { | ||
val dialogView = requireActivity().layoutInflater.inflate(R.layout.subscribe_dialog, null) | ||
val dialog = AlertDialog.Builder(requireContext()).apply { | ||
setView(dialogView) | ||
}.create() | ||
|
||
val minIntervalEd = dialogView.findViewById<EditText>(R.id.minIntervalEd) | ||
val maxIntervalEd = dialogView.findViewById<EditText>(R.id.maxIntervalEd) | ||
dialogView.findViewById<Button>(R.id.subscribeBtn).setOnClickListener { | ||
scope.launch { | ||
subscribe(minIntervalEd.text.toString().toInt(), maxIntervalEd.text.toString().toInt()) | ||
requireActivity().runOnUiThread { dialog.dismiss() } | ||
} | ||
} | ||
dialog.show() | ||
} | ||
|
||
private fun getChipPathIdForText(text: String): ChipPathId { | ||
return if (text.isEmpty()) ChipPathId.forWildcard() else ChipPathId.forId(text.toLong()) | ||
} | ||
|
||
companion object { | ||
private const val TAG = "WildcardFragment" | ||
|
||
fun newInstance(): WildcardFragment = WildcardFragment() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/android/CHIPTool/app/src/main/res/layout/wildcard_fragment.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingStart="16dp" | ||
android:paddingEnd="16dp" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/addressUpdateFragment" | ||
android:name="com.google.chip.chiptool.clusterclient.AddressUpdateFragment" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
|
||
<androidx.constraintlayout.helper.widget.Flow | ||
android:id="@+id/flow" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
app:constraint_referenced_ids="endpointIdLabel,endpointIdEd,clusterIdLabel,clusterIdEd,attributeIdLabel,attributeIdEd" | ||
app:flow_horizontalBias="0.0" | ||
app:flow_horizontalGap="8dp" | ||
app:flow_horizontalStyle="packed" | ||
app:flow_maxElementsWrap="2" | ||
app:flow_wrapMode="aligned" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/addressUpdateFragment" /> | ||
|
||
<TextView | ||
android:id="@+id/endpointIdLabel" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="8dp" | ||
android:textSize="16sp" | ||
android:text="@string/endpoint_id_label" /> | ||
|
||
<EditText | ||
android:id="@+id/endpointIdEd" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:inputType="text" | ||
android:hint="@string/wildcard_help_label" /> | ||
|
||
<TextView | ||
android:id="@+id/clusterIdLabel" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="8dp" | ||
android:textSize="16sp" | ||
android:text="@string/cluster_id_label" /> | ||
|
||
<EditText | ||
android:id="@+id/clusterIdEd" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:inputType="number" | ||
android:hint="@string/wildcard_help_label"/> | ||
|
||
<TextView | ||
android:id="@+id/attributeIdLabel" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="8dp" | ||
android:textSize="16sp" | ||
android:text="@string/attribute_id_label" /> | ||
|
||
<EditText | ||
android:id="@+id/attributeIdEd" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="16dp" | ||
android:inputType="number" | ||
android:hint="@string/wildcard_help_label"/> | ||
|
||
<Button | ||
android:id="@+id/readBtn" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="16dp" | ||
android:layout_marginTop="16dp" | ||
android:layout_marginBottom="8dp" | ||
android:layout_gravity="center" | ||
android:text="@string/wildcard_read_btn_text" | ||
android:textSize="16sp" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toStartOf="@id/subscribeBtn" | ||
app:layout_constraintTop_toBottomOf="@id/flow"/> | ||
|
||
<Button | ||
android:id="@+id/subscribeBtn" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="16dp" | ||
android:layout_marginTop="16dp" | ||
android:layout_marginBottom="8dp" | ||
android:layout_gravity="center" | ||
android:text="@string/wildcard_subscribe_btn_text" | ||
android:textSize="16sp" | ||
app:layout_constraintStart_toEndOf="@id/readBtn" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/flow"/> | ||
|
||
<ScrollView | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
android:fadeScrollbars="false" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/subscribeBtn"> | ||
<TextView | ||
android:id="@+id/outputTv" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:padding="16dp" | ||
android:minLines="4" | ||
android:singleLine="false" | ||
android:textSize="20sp" /> | ||
</ScrollView> | ||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.