Skip to content

Commit

Permalink
[Android] Fix handling manual code in CHIPTool (#29255)
Browse files Browse the repository at this point in the history
* [Android] Fix handling manual code in CHIPTool

* [Android] Fix handling manual code in CHIPTool

* Re-run kotlin formatter via scriptsd/helpers/kotlin-format.sh

---------

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 20, 2024
1 parent 5269f64 commit 5334735
Showing 1 changed file with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.fragment.app.Fragment
import chip.onboardingpayload.OnboardingPayload
import chip.onboardingpayload.OnboardingPayloadException
import chip.onboardingpayload.OnboardingPayloadParser
import chip.onboardingpayload.UnrecognizedQrCodeException
import com.google.chip.chiptool.R
Expand Down Expand Up @@ -139,9 +137,9 @@ class BarcodeFragment : Fragment() {

// workaround: can not use gms to scan the code in China, added a EditText to debug
binding.manualCodeBtn.setOnClickListener {
val qrCode = binding.manualCodeEditText.text.toString()
Log.d(TAG, "Submit Code:$qrCode")
handleInputQrCode(qrCode)
val code = binding.manualCodeEditText.text.toString()
Log.d(TAG, "Submit Code:$code")
handleInputCode(code)
}
}

Expand Down Expand Up @@ -178,39 +176,39 @@ class BarcodeFragment : Fragment() {
}
}

private fun handleInputQrCode(qrCode: String) {
lateinit var payload: OnboardingPayload
private fun handleInputCode(code: String) {
try {
payload = OnboardingPayloadParser().parseQrCode(qrCode)
} catch (ex: OnboardingPayloadException) {
try {
payload = OnboardingPayloadParser().parseManualPairingCode(qrCode)
} catch (ex: Exception) {
Log.e(TAG, "Unrecognized Manual Pairing Code", ex)
Toast.makeText(requireContext(), "Unrecognized Manual Pairing Code", Toast.LENGTH_SHORT)
.show()
}
val payload =
if (code.startsWith("MT:")) {
OnboardingPayloadParser().parseQrCode(code)
} else {
OnboardingPayloadParser().parseManualPairingCode(code)
}

FragmentUtil.getHost(this@BarcodeFragment, Callback::class.java)
?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload))
} catch (ex: UnrecognizedQrCodeException) {
Log.e(TAG, "Unrecognized QR Code", ex)
Log.e(TAG, "Unrecognized Code", ex)
Toast.makeText(requireContext(), "Unrecognized QR Code", Toast.LENGTH_SHORT).show()
} catch (ex: Exception) {
Log.e(TAG, "Exception, $ex")
Toast.makeText(requireContext(), "Occur Exception, $ex", Toast.LENGTH_SHORT).show()
}
FragmentUtil.getHost(this@BarcodeFragment, Callback::class.java)
?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload))
}

private fun handleScannedQrCode(barcode: Barcode) {
Handler(Looper.getMainLooper()).post {
lateinit var payload: OnboardingPayload
try {
payload =
val payload =
barcode.displayValue?.let { OnboardingPayloadParser().parseQrCode(it) } ?: return@post

FragmentUtil.getHost(this@BarcodeFragment, Callback::class.java)
?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload))
} catch (ex: UnrecognizedQrCodeException) {
Log.e(TAG, "Unrecognized QR Code", ex)
Toast.makeText(requireContext(), "Unrecognized QR Code", Toast.LENGTH_SHORT).show()
return@post
}
FragmentUtil.getHost(this@BarcodeFragment, Callback::class.java)
?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload))
}
}

Expand Down

0 comments on commit 5334735

Please sign in to comment.