Skip to content

Commit 1067628

Browse files
yufengwangcapull[bot]
authored andcommitted
[Android] Use FragmentContainerView as the container to manage fragments (#25004)
1 parent 2770972 commit 1067628

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt

+15-25
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import android.widget.Toast
2828
import androidx.appcompat.app.AlertDialog
2929
import androidx.appcompat.app.AppCompatActivity
3030
import androidx.fragment.app.Fragment
31-
import com.google.chip.chiptool.NetworkCredentialsParcelable
3231
import chip.setuppayload.SetupPayload
3332
import chip.setuppayload.SetupPayloadParser
3433
import chip.setuppayload.SetupPayloadParser.UnrecognizedQrCodeException
@@ -71,7 +70,7 @@ class CHIPToolActivity :
7170
val fragment = SelectActionFragment.newInstance()
7271
supportFragmentManager
7372
.beginTransaction()
74-
.add(R.id.fragment_container, fragment, fragment.javaClass.simpleName)
73+
.add(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName)
7574
.commit()
7675
} else {
7776
networkType =
@@ -116,20 +115,6 @@ class CHIPToolActivity :
116115
showFragment(SelectActionFragment.newInstance(), false)
117116
}
118117

119-
override fun handleScanQrCodeClicked() {
120-
showFragment(BarcodeFragment.newInstance())
121-
}
122-
123-
override fun onProvisionWiFiCredentialsClicked() {
124-
networkType = ProvisionNetworkType.WIFI
125-
showFragment(BarcodeFragment.newInstance(), false)
126-
}
127-
128-
override fun onProvisionThreadCredentialsClicked() {
129-
networkType = ProvisionNetworkType.THREAD
130-
showFragment(BarcodeFragment.newInstance(), false)
131-
}
132-
133118
override fun onShowDeviceAddressInput() {
134119
showFragment(AddressCommissioningFragment.newInstance(), false)
135120
}
@@ -138,6 +123,10 @@ class CHIPToolActivity :
138123
showFragment(DeviceProvisioningFragment.newInstance(deviceInfo!!, networkCredentials))
139124
}
140125

126+
override fun handleScanQrCodeClicked() {
127+
showFragment(BarcodeFragment.newInstance(), false)
128+
}
129+
141130
override fun handleClusterInteractionClicked() {
142131
showFragment(ClusterInteractionFragment.newInstance())
143132
}
@@ -179,17 +168,18 @@ class CHIPToolActivity :
179168
startActivity(redirectIntent)
180169
}
181170

182-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
183-
super.onActivityResult(requestCode, resultCode, data)
171+
override fun handleProvisionWiFiCredentialsClicked() {
172+
networkType = ProvisionNetworkType.WIFI
173+
showFragment(BarcodeFragment.newInstance(), false)
174+
}
184175

185-
if (requestCode == REQUEST_CODE_COMMISSIONING) {
186-
// Simply ignore the commissioning result.
187-
// TODO: tracking commissioned devices.
188-
}
176+
override fun handleProvisionThreadCredentialsClicked() {
177+
networkType = ProvisionNetworkType.THREAD
178+
showFragment(BarcodeFragment.newInstance(), false)
189179
}
190180

191-
override fun handleCustomFlowClicked() {
192-
showFragment(BarcodeFragment.newInstance())
181+
override fun handleProvisionCustomFlowClicked() {
182+
showFragment(BarcodeFragment.newInstance(), false)
193183
}
194184

195185
override fun handleUnpairDeviceClicked() {
@@ -199,7 +189,7 @@ class CHIPToolActivity :
199189
private fun showFragment(fragment: Fragment, showOnBack: Boolean = true) {
200190
val fragmentTransaction = supportFragmentManager
201191
.beginTransaction()
202-
.replace(R.id.fragment_container, fragment, fragment.javaClass.simpleName)
192+
.replace(R.id.nav_host_fragment, fragment, fragment.javaClass.simpleName)
203193

204194
if (showOnBack) {
205195
fragmentTransaction.addToBackStack(null)

examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class SelectActionFragment : Fragment() {
4545
scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
4646
provisionWiFiCredentialsBtn.apply {
4747
isEnabled = hasLocationPermission()
48-
setOnClickListener { getCallback()?.onProvisionWiFiCredentialsClicked() }
48+
setOnClickListener { getCallback()?.handleProvisionWiFiCredentialsClicked() }
4949
}
5050
provisionThreadCredentialsBtn.apply {
5151
isEnabled = hasLocationPermission()
52-
setOnClickListener { getCallback()?.onProvisionThreadCredentialsClicked() }
52+
setOnClickListener { getCallback()?.handleProvisionThreadCredentialsClicked() }
5353
}
5454
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
5555
sensorClustersBtn.setOnClickListener { getCallback()?.handleSensorClicked() }
@@ -58,7 +58,7 @@ class SelectActionFragment : Fragment() {
5858
basicClusterBtn.setOnClickListener { getCallback()?.handleBasicClicked() }
5959
attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
6060
clusterInteractionBtn.setOnClickListener { getCallback()?.handleClusterInteractionClicked() }
61-
provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleCustomFlowClicked() }
61+
provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleProvisionCustomFlowClicked() }
6262
wildcardBtn.setOnClickListener { getCallback()?.handleWildcardClicked() }
6363
unpairDeviceBtn.setOnClickListener{ getCallback()?.handleUnpairDeviceClicked() }
6464
}
@@ -134,10 +134,6 @@ class SelectActionFragment : Fragment() {
134134
interface Callback {
135135
/** Notifies listener of Scan QR code button click. */
136136
fun handleScanQrCodeClicked()
137-
/** Notifies listener of provision-WiFi-credentials button click. */
138-
fun onProvisionWiFiCredentialsClicked()
139-
/** Notifies listener of provision-Thread-credentials button click. */
140-
fun onProvisionThreadCredentialsClicked()
141137
/** Notifies listener of Light On/Off & Level Cluster button click. */
142138
fun handleOnOffClicked()
143139
/** Notifies listener of Sensor Clusters button click. */
@@ -156,8 +152,12 @@ class SelectActionFragment : Fragment() {
156152
fun handleClusterInteractionClicked()
157153
/** Notifies listener of wildcard button click. */
158154
fun handleWildcardClicked()
155+
/** Notifies listener of provision-WiFi-credentials button click. */
156+
fun handleProvisionWiFiCredentialsClicked()
157+
/** Notifies listener of provision-Thread-credentials button click. */
158+
fun handleProvisionThreadCredentialsClicked()
159159
/** Notifies listener of provision-custom-flow button click. */
160-
fun handleCustomFlowClicked()
160+
fun handleProvisionCustomFlowClicked()
161161
/** Notifies listener of unpair button click. */
162162
fun handleUnpairDeviceClicked()
163163
}

examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import kotlinx.coroutines.CoroutineScope
4545
import kotlinx.coroutines.ExperimentalCoroutinesApi
4646
import kotlinx.coroutines.Runnable
4747
import kotlinx.coroutines.launch
48-
import java.lang.IllegalArgumentException
4948

5049
@ExperimentalCoroutinesApi
5150
class DeviceProvisioningFragment : Fragment() {
@@ -74,7 +73,7 @@ class DeviceProvisioningFragment : Fragment() {
7473
scope = viewLifecycleOwner.lifecycleScope
7574
deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO))
7675

77-
return inflater.inflate(R.layout.single_fragment_container, container, false).apply {
76+
return inflater.inflate(R.layout.barcode_fragment, container, false).apply {
7877
if (savedInstanceState == null) {
7978
if (deviceInfo.ipAddress != null) {
8079
pairDeviceWithAddress()

examples/android/CHIPTool/app/src/main/res/layout/single_fragment_container.xml

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
android:id="@+id/fragment_container"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
35
android:layout_width="match_parent"
4-
android:layout_height="match_parent"/>
6+
android:layout_height="match_parent"
7+
tools:context=".CHIPToolActivity">
8+
9+
<androidx.fragment.app.FragmentContainerView
10+
android:id="@+id/nav_host_fragment"
11+
android:name="androidx.navigation.fragment.NavHostFragment"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent" />
14+
15+
</FrameLayout>

0 commit comments

Comments
 (0)