Skip to content

Commit

Permalink
[Android] Support group message (#29244)
Browse files Browse the repository at this point in the history
* Implement Android Group message

* Add Android if_nameindex

* Add groupsetting API

* remove test code

* Restyled by whitespace

* Restyled by google-java-format

* Restyled by clang-format

* Implement Group Setting UI

* restyle kotlin

* Fix kotlin static code analysis

* Modify from comments.

* Restyled by whitespace

* Restyled by google-java-format

* Restyled by clang-format

* kotlin restyle

* Modify return value

* Update group id in commandpath

* Update kotlin codestyle

* Restyled by google-java-format

* Restyled by clang-format

* Update from comments

* Modify write path in group session

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Nov 18, 2023
1 parent c674ed7 commit 1189852
Show file tree
Hide file tree
Showing 32 changed files with 2,347 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.google.chip.chiptool.setuppayloadscanner.BarcodeFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceDetailsFragment
import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceInfo
import com.google.chip.chiptool.setuppayloadscanner.CHIPLedgerDetailsFragment
import com.google.chip.chiptool.util.DeviceIdUtil
import org.json.JSONObject

class CHIPToolActivity :
Expand Down Expand Up @@ -94,11 +95,12 @@ class CHIPToolActivity :
}
}

override fun onCommissioningComplete(code: Int) {
override fun onCommissioningComplete(code: Int, nodeId: Long) {
runOnUiThread {
Toast.makeText(this, getString(R.string.commissioning_completed, code), Toast.LENGTH_SHORT)
.show()
}
DeviceIdUtil.setCommissionedNodeId(this, nodeId)
ChipClient.getDeviceController(this).close()
showFragment(SelectActionFragment.newInstance(), false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.chip.chiptool.attestation.AttestationTestFragment
import com.google.chip.chiptool.clusterclient.*
import com.google.chip.chiptool.clusterclient.GroupSettingFragment
import com.google.chip.chiptool.clusterclient.clusterinteraction.ClusterInteractionFragment
import com.google.chip.chiptool.databinding.SelectActionFragmentBinding
import com.google.chip.chiptool.provisioning.ProvisionNetworkType
Expand Down Expand Up @@ -71,6 +72,7 @@ class SelectActionFragment : Fragment() {
binding.provisionCustomFlowBtn.setOnClickListener { handleProvisionCustomFlowClicked() }
binding.wildcardBtn.setOnClickListener { handleWildcardClicked() }
binding.unpairDeviceBtn.setOnClickListener { handleUnpairDeviceClicked() }
binding.groupSettingBtn.setOnClickListener { handleGroupSettingClicked() }

return binding.root
}
Expand Down Expand Up @@ -233,6 +235,10 @@ class SelectActionFragment : Fragment() {
showFragment(BarcodeFragment.newInstance(), false)
}

private fun handleGroupSettingClicked() {
showFragment(GroupSettingFragment.newInstance())
}

companion object {

@JvmStatic fun newInstance() = SelectActionFragment()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.google.chip.chiptool.clusterclient

import android.content.Context
import android.os.Bundle
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 androidx.fragment.app.Fragment
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
Expand Down Expand Up @@ -41,13 +45,70 @@ class AddressUpdateFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)

val compressedFabricId = deviceController.compressedFabricId
binding.fabricIdEd.setText(compressedFabricId.toULong().toString(16).padStart(16, '0'))
binding.deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
binding.fabricIdEd.setText(compressedFabricId.toULong().toString().padStart(16, '0'))
binding.deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString(16))
binding.epIdEd.setText(endpointId.toString())

updateDeviceIdSpinner()
}

fun updateDeviceIdSpinner() {
val deviceIdList = DeviceIdUtil.getCommissionedNodeId(requireContext())
binding.deviceIdSpinner.adapter =
ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, deviceIdList)
binding.deviceIdSpinner.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
binding.deviceIdEd.setText(deviceIdList[position].toULong(16).toString())
}

override fun onNothingSelected(parent: AdapterView<*>?) {
Log.d(TAG, "onNothingSelected")
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

suspend fun getDevicePointer(context: Context): Long {
return if (isGroupId()) {
deviceController.getGroupDevicePointer(getGroupId().toInt())
} else {
ChipClient.getConnectedDevicePointer(context, getNodeId().toLong())
}
}

fun isGroupId(): Boolean {
return isGroupNodeId(getNodeId())
}

fun getGroupId(): UInt {
return getGroupIdFromNodeId(getNodeId())
}

fun getNodeId(): ULong {
return binding.deviceIdEd.text.toString().toULong()
}

companion object {
private const val TAG = "AddressUpdateFragment"
// Refer from NodeId.h (src/lib/core/NodeId.h)
private const val MIN_GROUP_NODE_ID = 0xFFFF_FFFF_FFFF_0000UL
private const val MASK_GROUP_ID = 0x0000_0000_0000_FFFFUL

fun isGroupNodeId(nodeId: ULong): Boolean {
return nodeId >= MIN_GROUP_NODE_ID
}

fun getNodeIdFromGroupId(groupId: UInt): ULong {
return groupId.toULong() or MIN_GROUP_NODE_ID
}

fun getGroupIdFromNodeId(nodeId: ULong): UInt {
return (nodeId and MASK_GROUP_ID).toUInt()
}
}
}
Loading

0 comments on commit 1189852

Please sign in to comment.