Skip to content

Commit

Permalink
Migrate from Kotlin synthetics to Jetpack view binding (#25063)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Feb 5, 2024
1 parent 4aabc27 commit 1814256
Show file tree
Hide file tree
Showing 22 changed files with 601 additions and 500 deletions.
8 changes: 7 additions & 1 deletion examples/android/CHIPTool/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-parcelize'

android {
compileSdkVersion 31
Expand Down Expand Up @@ -34,11 +34,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
viewBinding = true
}

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
Expand Down Expand Up @@ -103,6 +108,7 @@ dependencies {
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
}

repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,42 @@ 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.databinding.SelectActionFragmentBinding
import com.google.chip.chiptool.util.FragmentUtil
import kotlinx.android.synthetic.main.select_action_fragment.provisionThreadCredentialsBtn
import kotlinx.android.synthetic.main.select_action_fragment.provisionWiFiCredentialsBtn
import kotlinx.android.synthetic.main.select_action_fragment.view.*

/** Fragment to select from various options to interact with a CHIP device. */
class SelectActionFragment : Fragment() {
private var _binding: SelectActionFragmentBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.select_action_fragment, container, false).apply {
scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
provisionWiFiCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.handleProvisionWiFiCredentialsClicked() }
}
provisionThreadCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.handleProvisionThreadCredentialsClicked() }
}
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
sensorClustersBtn.setOnClickListener { getCallback()?.handleSensorClicked() }
multiAdminClusterBtn.setOnClickListener { getCallback()?.handleMultiAdminClicked() }
opCredClustersBtn.setOnClickListener { getCallback()?.handleOpCredClicked() }
basicClusterBtn.setOnClickListener { getCallback()?.handleBasicClicked() }
attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
clusterInteractionBtn.setOnClickListener { getCallback()?.handleClusterInteractionClicked() }
provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleProvisionCustomFlowClicked() }
wildcardBtn.setOnClickListener { getCallback()?.handleWildcardClicked() }
unpairDeviceBtn.setOnClickListener{ getCallback()?.handleUnpairDeviceClicked() }
_binding = SelectActionFragmentBinding.inflate(inflater, container, false)

binding.scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
binding.provisionWiFiCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.handleProvisionWiFiCredentialsClicked() }
}
binding.provisionThreadCredentialsBtn.apply {
isEnabled = hasLocationPermission()
setOnClickListener { getCallback()?.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() }

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -80,11 +82,11 @@ class SelectActionFragment : Fragment() {
}
}
if (granted) {
provisionWiFiCredentialsBtn.isEnabled = true
provisionThreadCredentialsBtn.isEnabled = true
binding.provisionWiFiCredentialsBtn.isEnabled = true
binding.provisionThreadCredentialsBtn.isEnabled = true
} else {
provisionWiFiCredentialsBtn.isEnabled = false
provisionThreadCredentialsBtn.isEnabled = false
binding.provisionWiFiCredentialsBtn.isEnabled = false
binding.provisionThreadCredentialsBtn.isEnabled = false

AlertDialog.Builder(requireContext())
.setTitle(R.string.location_permission_denied_title)
Expand All @@ -109,6 +111,11 @@ class SelectActionFragment : Fragment() {
permissionRequest.launch(permissions)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun hasLocationPermission(): Boolean {
val locationPermissionGranted =
ContextCompat.checkSelfPermission(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,38 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.attestation_test_fragment.view.*
import com.google.chip.chiptool.databinding.AttestationTestFragmentBinding

/** Fragment for launching external attestation apps */
class AttestationTestFragment : Fragment() {
private var _binding: AttestationTestFragmentBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.attestation_test_fragment, container, false).apply {
attestationText.text = context.getString(R.string.attestation_fetching_status)
val appIntent = AttestationAppLauncher.getAttestationIntent(requireContext())
): View {
_binding = AttestationTestFragmentBinding.inflate(inflater, container, false)

if (appIntent != null) {
AttestationAppLauncher
.getLauncher(this@AttestationTestFragment) { result ->
attestationText.text = result
}
.launch(appIntent)
} else {
attestationText.text = context.getString(R.string.attestation_app_not_found)
}
binding.attestationText.text = context!!.getString(R.string.attestation_fetching_status)
val appIntent = AttestationAppLauncher.getAttestationIntent(requireContext())
if (appIntent != null) {
AttestationAppLauncher
.getLauncher(this@AttestationTestFragment) { result ->
binding.attestationText.text = result
}
.launch(appIntent)
} else {
binding.attestationText.text = context!!.getString(R.string.attestation_app_not_found)
}

return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,39 @@ import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ChipIdLookup
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.AddressUpdateFragmentBinding
import com.google.chip.chiptool.util.DeviceIdUtil
import kotlinx.android.synthetic.main.address_update_fragment.deviceIdEd
import kotlinx.android.synthetic.main.address_update_fragment.fabricIdEd

/** Fragment for updating the address of a device given its fabric and node ID. */
class AddressUpdateFragment: Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

val deviceId: Long
get() = deviceIdEd.text.toString().toULong().toLong()
get() = binding.deviceIdEd.text.toString().toULong().toLong()

private var _binding: AddressUpdateFragmentBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.address_update_fragment, container, false).apply { }
_binding = AddressUpdateFragmentBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val compressedFabricId = deviceController.compressedFabricId
fabricIdEd.setText(compressedFabricId.toULong().toString(16).padStart(16, '0'))
deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
binding.fabricIdEd.setText(compressedFabricId.toULong().toString(16).padStart(16, '0'))
binding.deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.basic_client_fragment.attributeNameSpinner
import kotlinx.android.synthetic.main.basic_client_fragment.basicClusterCommandStatus
import kotlinx.android.synthetic.main.basic_client_fragment.locationEd
import kotlinx.android.synthetic.main.basic_client_fragment.nodeLabelEd
import kotlinx.android.synthetic.main.basic_client_fragment.view.attributeNameSpinner
import kotlinx.android.synthetic.main.basic_client_fragment.view.readAttributeBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocalConfigDisabledSwitch
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocationBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeNodeLabelBtn
import com.google.chip.chiptool.databinding.BasicClientFragmentBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand All @@ -35,34 +27,43 @@ class BasicClientFragment : Fragment() {

private lateinit var addressUpdateFragment: AddressUpdateFragment

private var _binding: BasicClientFragmentBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = BasicClientFragmentBinding.inflate(inflater, container, false)
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.basic_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment

writeNodeLabelBtn.setOnClickListener { scope.launch {
sendWriteNodeLabelAttribute()
nodeLabelEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}}
writeLocationBtn.setOnClickListener { scope.launch {
sendWriteLocationAttribute()
locationEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}}
writeLocalConfigDisabledSwitch.setOnCheckedChangeListener { _, isChecked ->
scope.launch { sendWriteLocalConfigDisabledAttribute(isChecked) }
}
makeAttributeList()
attributeNameSpinner.adapter = makeAttributeNamesAdapter()
readAttributeBtn.setOnClickListener { scope.launch { readAttributeButtonClick() }}
deviceController.setCompletionListener(ChipControllerCallback())

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment

binding.writeNodeLabelBtn.setOnClickListener { scope.launch {
sendWriteNodeLabelAttribute()
binding.nodeLabelEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}}
binding.writeLocationBtn.setOnClickListener { scope.launch {
sendWriteLocationAttribute()
binding.locationEd.onEditorAction(EditorInfo.IME_ACTION_DONE)
}}
binding.writeLocalConfigDisabledSwitch.setOnCheckedChangeListener { _, isChecked ->
scope.launch { sendWriteLocalConfigDisabledAttribute(isChecked) }
}
makeAttributeList()
binding.attributeNameSpinner.adapter = makeAttributeNamesAdapter()
binding.readAttributeBtn.setOnClickListener { scope.launch { readAttributeButtonClick() }}

return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
Expand Down Expand Up @@ -98,7 +99,7 @@ class BasicClientFragment : Fragment() {

private suspend fun readAttributeButtonClick() {
try {
readBasicClusters(attributeNameSpinner.selectedItemPosition)
readBasicClusters(binding.attributeNameSpinner.selectedItemPosition)
} catch (ex: Exception) {
showMessage("readBasicCluster failed: $ex")
}
Expand Down Expand Up @@ -244,7 +245,7 @@ class BasicClientFragment : Fragment() {
showMessage("Write NodeLabel failure $ex")
Log.e(TAG, "Write NodeLabel failure", ex)
}
}, nodeLabelEd.text.toString())
}, binding.nodeLabelEd.text.toString())
}

private suspend fun sendReadLocationAttribute() {
Expand All @@ -271,7 +272,7 @@ class BasicClientFragment : Fragment() {
showMessage("Write Location failure $ex")
Log.e(TAG, "Write Location failure", ex)
}
}, locationEd.text.toString())
}, binding.locationEd.text.toString())
}

private suspend fun sendReadHardwareVersionAttribute() {
Expand Down Expand Up @@ -457,7 +458,7 @@ class BasicClientFragment : Fragment() {

private fun showMessage(msg: String) {
requireActivity().runOnUiThread {
basicClusterCommandStatus.text = msg
binding.basicClusterCommandStatus.text = msg
}
}

Expand Down
Loading

0 comments on commit 1814256

Please sign in to comment.