Skip to content

Commit

Permalink
[Android] Use FragmentContainerView as the container to manage fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Feb 13, 2023
1 parent bbfd5d6 commit 1948849
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
}
Expand All @@ -138,6 +127,10 @@ class CHIPToolActivity :
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo!!, networkCredentials))
}

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

override fun handleClusterInteractionClicked() {
showFragment(ClusterInteractionFragment.newInstance())
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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() }
}
Expand Down Expand Up @@ -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. */
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down

This file was deleted.

15 changes: 13 additions & 2 deletions examples/android/CHIPTool/app/src/main/res/layout/top_activity.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
tools:context=".CHIPToolActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

0 comments on commit 1948849

Please sign in to comment.