Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix handling manual code in CHIPTool #29255

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading