diff options
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/providersetup/activities/scanner/ScannerActivity.java | 74 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 2 |
2 files changed, 44 insertions, 32 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/scanner/ScannerActivity.java b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/scanner/ScannerActivity.java index 0878bd11..1d828d8c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/scanner/ScannerActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/providersetup/activities/scanner/ScannerActivity.java @@ -21,6 +21,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.camera.mlkit.vision.MlKitAnalyzer; import androidx.camera.view.LifecycleCameraController; +import androidx.camera.view.PreviewView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.res.ResourcesCompat; @@ -78,7 +79,7 @@ public class ScannerActivity extends AppCompatActivity { ActionBarTitle actionBarTitle = new ActionBarTitle(context); actionBarTitle.setTitleCaps(BuildConfig.actionbar_capitalize_title); - actionBarTitle.setTitle("Scan QR Code"); + actionBarTitle.setTitle(getString(R.string.scan_qr_code)); final Drawable upArrow = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_back, getTheme()); actionBar.setHomeAsUpIndicator(upArrow); @@ -98,15 +99,6 @@ public class ScannerActivity extends AppCompatActivity { } } - private boolean allPermissionsGranted() { - for (String permission : REQUIRED_PERMISSIONS) { - if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { - return false; - } - } - return true; - } - private void startCamera() { var cameraController = new LifecycleCameraController(getBaseContext()); var previewView = binding.previewView; @@ -114,34 +106,43 @@ public class ScannerActivity extends AppCompatActivity { var options = new BarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE).build(); barcodeScanner = BarcodeScanning.getClient(options); - cameraController.setImageAnalysisAnalyzer(ContextCompat.getMainExecutor(this), new MlKitAnalyzer(Collections.singletonList(barcodeScanner), COORDINATE_SYSTEM_VIEW_REFERENCED, ContextCompat.getMainExecutor(this), result -> { - var barcodeResults = result.getValue(barcodeScanner); - if ((barcodeResults == null) || (barcodeResults.size() == 0) || (barcodeResults.get(0) == null)) { - previewView.getOverlay().clear(); - previewView.setOnTouchListener((v, event) -> false); //no-op - return; - } - try { - Introducer introducer = Introducer.fromUrl(barcodeResults.get(0).getRawValue()); - if (introducer != null && introducer.validate()) { - setResult(RESULT_OK, new Intent().putExtra(INVITE_CODE, introducer)); - } else { - Toast.makeText(this, "Invalid introducer", Toast.LENGTH_SHORT).show(); - } - } catch (Exception e) { - Log.e(TAG, e.getMessage()); - Toast.makeText(this, "Invalid introducer", Toast.LENGTH_SHORT).show(); - } - previewView.setOnTouchListener((v, event) -> false); //no-op - previewView.getOverlay().clear(); + var mlKitAnalyzer = new MlKitAnalyzer( + Collections.singletonList(barcodeScanner), + COORDINATE_SYSTEM_VIEW_REFERENCED, + ContextCompat.getMainExecutor(this), + result -> handleQrResult(result, previewView) + ); - finish(); - })); + cameraController.setImageAnalysisAnalyzer(ContextCompat.getMainExecutor(this), mlKitAnalyzer); cameraController.bindToLifecycle(this); previewView.setController(cameraController); } + private void handleQrResult(MlKitAnalyzer.Result result, PreviewView previewView) { + var barcodeResults = result.getValue(barcodeScanner); + if ((barcodeResults == null) || (barcodeResults.isEmpty()) || (barcodeResults.get(0) == null)) { + previewView.getOverlay().clear(); + previewView.setOnTouchListener((v, event) -> false); //no-op + return; + } + try { + Introducer introducer = Introducer.fromUrl(barcodeResults.get(0).getRawValue()); + if (introducer.validate()) { + setResult(RESULT_OK, new Intent().putExtra(INVITE_CODE, introducer)); + } else { + Toast.makeText(this, R.string.invalid_code, Toast.LENGTH_SHORT).show(); + } + } catch (Exception e) { + Log.e(TAG, "Introducer error: " + e.getMessage()); + Toast.makeText(this, R.string.invalid_code, Toast.LENGTH_SHORT).show(); + } + previewView.setOnTouchListener((v, event) -> false); //no-op + previewView.getOverlay().clear(); + + finish(); + } + @Override protected void onDestroy() { super.onDestroy(); @@ -158,6 +159,15 @@ public class ScannerActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } + private boolean allPermissionsGranted() { + for (String permission : REQUIRED_PERMISSIONS) { + if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { + return false; + } + } + return true; + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c40f5819..0f284ddc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -247,4 +247,6 @@ <string name="validation_status_success">Good</string> <string name="validation_status_failure">Bad</string> <string name="enter_invite_code">Enter invite Code</string> + <string name="scan_qr_code">Scan QR Code</string> + <string name="invalid_code">Invalid code</string> </resources> |