From 4427209cde71df54b3251196b9985a58c9f9e6f5 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 21 Feb 2023 10:13:56 -0800 Subject: [PATCH] Process SelectActionFragment Button handling within SelectActionFragment (#25134) --- .../google/chip/chiptool/CHIPToolActivity.kt | 54 +----- .../chip/chiptool/SelectActionFragment.kt | 168 +++++++++++++----- 2 files changed, 130 insertions(+), 92 deletions(-) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt index 405fd892fd22dc..201e6a8912a388 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt @@ -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)) } @@ -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) { diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt index 9f1bbdaed7aa9c..38cc5d7fb5cec3 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt @@ -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. */ @@ -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 } @@ -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 {