From 1948849f8632818a298467eb20fdfa068a20f7af Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Sun, 12 Feb 2023 09:08:59 -0800 Subject: [PATCH] [Android] Use FragmentContainerView as the container to manage fragments --- .../google/chip/chiptool/CHIPToolActivity.kt | 42 ++++++++----------- .../chip/chiptool/SelectActionFragment.kt | 16 +++---- .../DeviceProvisioningFragment.kt | 3 +- .../res/layout/single_fragment_container.xml | 6 --- .../app/src/main/res/layout/top_activity.xml | 15 ++++++- 5 files changed, 40 insertions(+), 42 deletions(-) delete mode 100644 examples/android/CHIPTool/app/src/main/res/layout/single_fragment_container.xml 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 e7005477deb898..b24efdf002206d 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 @@ -28,6 +28,9 @@ import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment +import androidx.navigation.NavController +import androidx.navigation.fragment.NavHostFragment +import androidx.navigation.ui.setupActionBarWithNavController import com.google.chip.chiptool.NetworkCredentialsParcelable import chip.setuppayload.SetupPayload import chip.setuppayload.SetupPayloadParser @@ -71,7 +74,7 @@ class CHIPToolActivity : val fragment = SelectActionFragment.newInstance() supportFragmentManager .beginTransaction() - .add(R.id.fragment_container, fragment, fragment.javaClass.simpleName) + .add(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName) .commit() } else { networkType = @@ -116,20 +119,6 @@ class CHIPToolActivity : showFragment(SelectActionFragment.newInstance(), false) } - override fun handleScanQrCodeClicked() { - showFragment(BarcodeFragment.newInstance()) - } - - override fun onProvisionWiFiCredentialsClicked() { - networkType = ProvisionNetworkType.WIFI - showFragment(BarcodeFragment.newInstance(), false) - } - - override fun onProvisionThreadCredentialsClicked() { - networkType = ProvisionNetworkType.THREAD - showFragment(BarcodeFragment.newInstance(), false) - } - override fun onShowDeviceAddressInput() { showFragment(AddressCommissioningFragment.newInstance(), false) } @@ -138,6 +127,10 @@ class CHIPToolActivity : showFragment(DeviceProvisioningFragment.newInstance(deviceInfo!!, networkCredentials)) } + override fun handleScanQrCodeClicked() { + showFragment(BarcodeFragment.newInstance(), false) + } + override fun handleClusterInteractionClicked() { showFragment(ClusterInteractionFragment.newInstance()) } @@ -179,17 +172,18 @@ class CHIPToolActivity : startActivity(redirectIntent) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) + override fun handleProvisionWiFiCredentialsClicked() { + networkType = ProvisionNetworkType.WIFI + showFragment(BarcodeFragment.newInstance(), false) + } - if (requestCode == REQUEST_CODE_COMMISSIONING) { - // Simply ignore the commissioning result. - // TODO: tracking commissioned devices. - } + override fun handleProvisionThreadCredentialsClicked() { + networkType = ProvisionNetworkType.THREAD + showFragment(BarcodeFragment.newInstance(), false) } - override fun handleCustomFlowClicked() { - showFragment(BarcodeFragment.newInstance()) + override fun handleProvisionCustomFlowClicked() { + showFragment(BarcodeFragment.newInstance(), false) } override fun handleUnpairDeviceClicked() { @@ -199,7 +193,7 @@ class CHIPToolActivity : private fun showFragment(fragment: Fragment, showOnBack: Boolean = true) { val fragmentTransaction = supportFragmentManager .beginTransaction() - .replace(R.id.fragment_container, fragment, fragment.javaClass.simpleName) + .replace(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName) if (showOnBack) { fragmentTransaction.addToBackStack(null) 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 4816469c187f61..f21b650ac6dc8b 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 @@ -45,11 +45,11 @@ class SelectActionFragment : Fragment() { scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() } provisionWiFiCredentialsBtn.apply { isEnabled = hasLocationPermission() - setOnClickListener { getCallback()?.onProvisionWiFiCredentialsClicked() } + setOnClickListener { getCallback()?.handleProvisionWiFiCredentialsClicked() } } provisionThreadCredentialsBtn.apply { isEnabled = hasLocationPermission() - setOnClickListener { getCallback()?.onProvisionThreadCredentialsClicked() } + setOnClickListener { getCallback()?.handleProvisionThreadCredentialsClicked() } } onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() } sensorClustersBtn.setOnClickListener { getCallback()?.handleSensorClicked() } @@ -58,7 +58,7 @@ class SelectActionFragment : Fragment() { basicClusterBtn.setOnClickListener { getCallback()?.handleBasicClicked() } attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() } clusterInteractionBtn.setOnClickListener { getCallback()?.handleClusterInteractionClicked() } - provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleCustomFlowClicked() } + provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleProvisionCustomFlowClicked() } wildcardBtn.setOnClickListener { getCallback()?.handleWildcardClicked() } unpairDeviceBtn.setOnClickListener{ getCallback()?.handleUnpairDeviceClicked() } } @@ -134,10 +134,6 @@ class SelectActionFragment : Fragment() { interface Callback { /** Notifies listener of Scan QR code button click. */ fun handleScanQrCodeClicked() - /** Notifies listener of provision-WiFi-credentials button click. */ - fun onProvisionWiFiCredentialsClicked() - /** Notifies listener of provision-Thread-credentials button click. */ - fun onProvisionThreadCredentialsClicked() /** Notifies listener of Light On/Off & Level Cluster button click. */ fun handleOnOffClicked() /** Notifies listener of Sensor Clusters button click. */ @@ -156,8 +152,12 @@ class SelectActionFragment : Fragment() { 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 handleCustomFlowClicked() + fun handleProvisionCustomFlowClicked() /** Notifies listener of unpair button click. */ fun handleUnpairDeviceClicked() } diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt index 9c540ee4be7fa1..3a7fe9a460bf78 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt @@ -45,7 +45,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Runnable import kotlinx.coroutines.launch -import java.lang.IllegalArgumentException @ExperimentalCoroutinesApi class DeviceProvisioningFragment : Fragment() { @@ -74,7 +73,7 @@ class DeviceProvisioningFragment : Fragment() { scope = viewLifecycleOwner.lifecycleScope deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO)) - return inflater.inflate(R.layout.single_fragment_container, container, false).apply { + return inflater.inflate(R.layout.barcode_fragment, container, false).apply { if (savedInstanceState == null) { if (deviceInfo.ipAddress != null) { pairDeviceWithAddress() diff --git a/examples/android/CHIPTool/app/src/main/res/layout/single_fragment_container.xml b/examples/android/CHIPTool/app/src/main/res/layout/single_fragment_container.xml deleted file mode 100644 index 0543d016d73b2e..00000000000000 --- a/examples/android/CHIPTool/app/src/main/res/layout/single_fragment_container.xml +++ /dev/null @@ -1,6 +0,0 @@ - - \ No newline at end of file diff --git a/examples/android/CHIPTool/app/src/main/res/layout/top_activity.xml b/examples/android/CHIPTool/app/src/main/res/layout/top_activity.xml index 563f5d7c570a3c..041d9695d3d1d3 100644 --- a/examples/android/CHIPTool/app/src/main/res/layout/top_activity.xml +++ b/examples/android/CHIPTool/app/src/main/res/layout/top_activity.xml @@ -1,4 +1,15 @@ + + android:layout_height="match_parent" + tools:context=".CHIPToolActivity"> + + + + \ No newline at end of file