Skip to content

Commit

Permalink
Tweak code scanner demo.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 456637238
Change-Id: Ic3ca0b3fb7219b33f6ef696d7cb56a57b4194739
  • Loading branch information
Google ML Kit authored and zhouyiself committed Jun 23, 2022
1 parent 5a30503 commit 18437dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public void onScanButtonClicked(View view) {
gmsBarcodeScanner
.startScan()
.addOnSuccessListener(barcode -> barcodeResultView.setText(getSuccessfulMessage(barcode)))
.addOnFailureListener(e -> barcodeResultView.setText(getErrorMessage((MlKitException) e)));
.addOnFailureListener(e -> barcodeResultView.setText(getErrorMessage((MlKitException) e)))
.addOnCanceledListener(
() -> barcodeResultView.setText(getString(R.string.error_scanner_cancelled)));
}

@Override
Expand Down Expand Up @@ -90,8 +92,6 @@ private String getSuccessfulMessage(Barcode barcode) {
@SuppressLint("SwitchIntDef")
private String getErrorMessage(MlKitException e) {
switch (e.getErrorCode()) {
case MlKitException.CODE_SCANNER_CANCELLED:
return getString(R.string.error_scanner_cancelled);
case MlKitException.CODE_SCANNER_CAMERA_PERMISSION_NOT_GRANTED:
return getString(R.string.error_camera_permission_not_granted);
case MlKitException.CODE_SCANNER_APP_NAME_UNAVAILABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class MainActivity : AppCompatActivity() {
.addOnFailureListener { e: Exception ->
barcodeResultView!!.text = getErrorMessage(e as MlKitException)
}
.addOnCanceledListener {
barcodeResultView!!.text = getString(R.string.error_scanner_cancelled)
}
}

override fun onSaveInstanceState(savedInstanceState: Bundle) {
Expand Down Expand Up @@ -85,7 +88,6 @@ class MainActivity : AppCompatActivity() {

private fun getErrorMessage(e: MlKitException): String {
return when (e.errorCode) {
MlKitException.CODE_SCANNER_CANCELLED -> getString(R.string.error_scanner_cancelled)
MlKitException.CODE_SCANNER_CAMERA_PERMISSION_NOT_GRANTED ->
getString(R.string.error_camera_permission_not_granted)
MlKitException.CODE_SCANNER_APP_NAME_UNAVAILABLE ->
Expand Down

0 comments on commit 18437dc

Please sign in to comment.