Skip to content

Commit

Permalink
Process SelectActionFragment Button handling within SelectActionFragm…
Browse files Browse the repository at this point in the history
…ent (#25134)
  • Loading branch information
yufengwangca authored Feb 21, 2023
1 parent 99fb607 commit 35faed4
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,6 @@ class CHIPToolActivity :
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo!!, networkCredentials))
}

override fun handleScanQrCodeClicked() {
showFragment(BarcodeFragment.newInstance(), false)
}

override fun handleClusterInteractionClicked() {
showFragment(ClusterInteractionFragment.newInstance())
}

override fun handleWildcardClicked() {
showFragment(WildcardFragment.newInstance())
}

override fun handleOnOffClicked() {
showFragment(OnOffClientFragment.newInstance())
}

override fun handleSensorClicked() {
showFragment(SensorClientFragment.newInstance())
}

override fun handleMultiAdminClicked() {
showFragment(MultiAdminClientFragment.newInstance())
}

override fun handleOpCredClicked() {
showFragment(OpCredClientFragment.newInstance())
}

override fun handleBasicClicked() {
showFragment(BasicClientFragment.newInstance())
}

override fun handleAttestationTestClicked() {
showFragment(AttestationTestFragment.newInstance())
}

override fun handleReadFromLedgerClicked(deviceInfo: CHIPDeviceInfo) {
showFragment(CHIPLedgerDetailsFragment.newInstance(deviceInfo))
}
Expand All @@ -168,22 +132,8 @@ class CHIPToolActivity :
startActivity(redirectIntent)
}

override fun handleProvisionWiFiCredentialsClicked() {
networkType = ProvisionNetworkType.WIFI
showFragment(BarcodeFragment.newInstance(), false)
}

override fun handleProvisionThreadCredentialsClicked() {
networkType = ProvisionNetworkType.THREAD
showFragment(BarcodeFragment.newInstance(), false)
}

override fun handleProvisionCustomFlowClicked() {
showFragment(BarcodeFragment.newInstance(), false)
}

override fun handleUnpairDeviceClicked() {
showFragment(UnpairDeviceFragment.newInstance())
override fun SetNetworkType(type: ProvisionNetworkType) {
networkType = type
}

private fun showFragment(fragment: Fragment, showOnBack: Boolean = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
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.clusterinteraction.ClusterInteractionFragment
import com.google.chip.chiptool.databinding.SelectActionFragmentBinding
import com.google.chip.chiptool.provisioning.ProvisionNetworkType
import com.google.chip.chiptool.provisioning.UnpairDeviceFragment
import com.google.chip.chiptool.setuppayloadscanner.BarcodeFragment
import com.google.chip.chiptool.util.FragmentUtil

/** Fragment to select from various options to interact with a CHIP device. */
Expand All @@ -43,25 +49,27 @@ class SelectActionFragment : Fragment() {
): View {
_binding = SelectActionFragmentBinding.inflate(inflater, container, false)

binding.scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
binding.provisionWiFiCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.handleProvisionWiFiCredentialsClicked() }
setOnClickListener { handleProvisionWiFiCredentialsClicked() }
}

binding.provisionThreadCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.handleProvisionThreadCredentialsClicked() }
setOnClickListener { handleProvisionThreadCredentialsClicked() }
}
binding.onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
binding.sensorClustersBtn.setOnClickListener { getCallback()?.handleSensorClicked() }
binding.multiAdminClusterBtn.setOnClickListener { getCallback()?.handleMultiAdminClicked() }
binding.opCredClustersBtn.setOnClickListener { getCallback()?.handleOpCredClicked() }
binding.basicClusterBtn.setOnClickListener { getCallback()?.handleBasicClicked() }
binding.attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
binding.clusterInteractionBtn.setOnClickListener { getCallback()?.handleClusterInteractionClicked() }
binding.provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleProvisionCustomFlowClicked() }
binding.wildcardBtn.setOnClickListener { getCallback()?.handleWildcardClicked() }
binding.unpairDeviceBtn.setOnClickListener{ getCallback()?.handleUnpairDeviceClicked() }

binding.scanQrBtn.setOnClickListener { handleScanQrCodeClicked() }
binding.onOffClusterBtn.setOnClickListener { handleOnOffClicked() }
binding.sensorClustersBtn.setOnClickListener { handleSensorClicked() }
binding.multiAdminClusterBtn.setOnClickListener { handleMultiAdminClicked() }
binding.opCredClustersBtn.setOnClickListener { handleOpCredClicked() }
binding.basicClusterBtn.setOnClickListener { handleBasicClicked() }
binding.attestationTestBtn.setOnClickListener { handleAttestationTestClicked() }
binding.clusterInteractionBtn.setOnClickListener { handleClusterInteractionClicked() }
binding.provisionCustomFlowBtn.setOnClickListener{ handleProvisionCustomFlowClicked() }
binding.wildcardBtn.setOnClickListener { handleWildcardClicked() }
binding.unpairDeviceBtn.setOnClickListener{ handleUnpairDeviceClicked() }

return binding.root
}
Expand Down Expand Up @@ -134,39 +142,119 @@ class SelectActionFragment : Fragment() {
return locationPermissionGranted && bleScanPermissionGranted
}


private fun getCallback() = FragmentUtil.getHost(this, Callback::class.java)

/** Interface for notifying the host. */
interface Callback {
/** Notifies listener of Scan QR code button click. */
fun handleScanQrCodeClicked()
/** Notifies listener of Light On/Off & Level Cluster button click. */
fun handleOnOffClicked()
/** Notifies listener of Sensor Clusters button click. */
fun handleSensorClicked()
/** Notifies listener of Multi-admin Clusters button click. */
fun handleMultiAdminClicked()
/** Notifies listener of Operational Credentials Clusters button click. */
fun handleOpCredClicked()
/** Notifies listener of Basic Clusters button click. */
fun handleBasicClicked()
/** Notifies listener of attestation command button clicked. */
fun handleAttestationTestClicked()
/** Notifies listener of a click to manually input the CHIP device address. */
fun onShowDeviceAddressInput()
/** Notifies listener of cluster interaction button click. */
fun handleClusterInteractionClicked()
/** Notifies listener of wildcard button click. */
fun handleWildcardClicked()
/** Notifies listener of provision-WiFi-credentials button click. */
fun handleProvisionWiFiCredentialsClicked()
/** Notifies listener of provision-Thread-credentials button click. */
fun handleProvisionThreadCredentialsClicked()
/** Notifies listener of provision-custom-flow button click. */
fun handleProvisionCustomFlowClicked()
/** Notifies listener of unpair button click. */
fun handleUnpairDeviceClicked()

fun SetNetworkType(type: ProvisionNetworkType)
}

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

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

fragmentTransaction.commit()
}

/**
* Notifies listener of Scan QR code button click.
*/
private fun handleScanQrCodeClicked() {
showFragment(BarcodeFragment.newInstance(), false)
}

/**
* Notifies listener of Light On/Off & Level Cluster button click.
*/
private fun handleOnOffClicked() {
showFragment(OnOffClientFragment.newInstance())
}

/**
* Notifies listener of Sensor Clusters button click.
*/
private fun handleSensorClicked() {
showFragment(SensorClientFragment.newInstance())
}

/**
* Notifies listener of Multi-admin Clusters button click.
*/
private fun handleMultiAdminClicked() {
showFragment(MultiAdminClientFragment.newInstance())
}

/**
* Notifies listener of Operational Credentials Clusters button click.
*/
private fun handleOpCredClicked() {
showFragment(OpCredClientFragment.newInstance())
}

/**
* Notifies listener of Basic Clusters button click.
*/
private fun handleBasicClicked() {
showFragment(BasicClientFragment.newInstance())
}

/**
* Notifies listener of attestation command button clicked.
*/
private fun handleAttestationTestClicked() {
showFragment(AttestationTestFragment.newInstance())
}

/**
* Notifies listener of cluster interaction button click.
*/
private fun handleClusterInteractionClicked() {
showFragment(ClusterInteractionFragment.newInstance())
}

/**
* Notifies listener of wildcard button click.
*/
private fun handleWildcardClicked() {
showFragment(WildcardFragment.newInstance())
}

/**
* Notifies listener of unpair button click.
*/
private fun handleUnpairDeviceClicked() {
showFragment(UnpairDeviceFragment.newInstance())
}

/**
* Notifies listener of provision-WiFi-credentials button click.
*/
private fun handleProvisionWiFiCredentialsClicked() {
getCallback()?.SetNetworkType(ProvisionNetworkType.WIFI)
showFragment(BarcodeFragment.newInstance(), false)
}

/**
* Notifies listener of provision-Thread-credentials button click.
*/
private fun handleProvisionThreadCredentialsClicked() {
getCallback()?.SetNetworkType(ProvisionNetworkType.THREAD)
showFragment(BarcodeFragment.newInstance(), false)
}

/**
* Notifies listener of provision-custom-flow button click.
*/
private fun handleProvisionCustomFlowClicked() {
showFragment(BarcodeFragment.newInstance(), false)
}

companion object {
Expand Down

0 comments on commit 35faed4

Please sign in to comment.