From fa2c7a07d816e1f7690ad6a1a7259c34ccdd095a Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Tue, 4 Dec 2018 11:33:58 -0600 Subject: [PATCH] Add camera permission to Manifest to be able to use camera in the app to read QR codes. - Add methods to SendTransactionFragment to request camera permission and start/stop the camera preview accordingly. --- app/src/main/AndroidManifest.xml | 2 + .../fragments/SendTransactionFragment.kt | 90 +++++++++++++++++-- app/src/main/res/drawable/ic_close.xml | 5 ++ .../res/layout/fragment_send_transaction.xml | 5 +- 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/drawable/ic_close.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 572ce2d..ca59e1b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" package="cy.agorise.bitsybitshareswallet"> + + , grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == REQUEST_CAMERA_PERMISSION) { + if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { + startCameraPreview() + } else { + // TODO extract string resource + Toast.makeText(context!!, "Camera permission is necessary to read QR codes.", Toast.LENGTH_SHORT).show() + } + return + } + } + + private fun startCameraPreview() { + cameraPreview.visibility = View.VISIBLE + fabOpenCamera.setImageResource(R.drawable.ic_close) + isCameraPreviewVisible = true + + // Configure QR scanner + cameraPreview.setFormats(listOf(BarcodeFormat.QR_CODE)) + cameraPreview.setAspectTolerance(0.5f) + cameraPreview.setAutoFocus(true) + cameraPreview.setLaserColor(R.color.colorAccent) + cameraPreview.setMaskColor(R.color.colorAccent) + cameraPreview.setResultHandler(this) + cameraPreview.startCamera() + } + + private fun stopCameraPreview() { + cameraPreview.visibility = View.INVISIBLE + fabOpenCamera.setImageResource(R.drawable.ic_camera) + isCameraPreviewVisible = false + cameraPreview.stopCamera() + } + + override fun handleResult(result: Result?) { + Toast.makeText(context!!, result!!.text, Toast.LENGTH_SHORT).show() + } + + override fun onResume() { + super.onResume() + if (isCameraPreviewVisible) + startCameraPreview() + } + + override fun onPause() { + super.onPause() + if (!isCameraPreviewVisible) + stopCameraPreview() + } } diff --git a/app/src/main/res/drawable/ic_close.xml b/app/src/main/res/drawable/ic_close.xml new file mode 100644 index 0000000..0c8775c --- /dev/null +++ b/app/src/main/res/drawable/ic_close.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_send_transaction.xml b/app/src/main/res/layout/fragment_send_transaction.xml index 4b5c111..74fe56b 100644 --- a/app/src/main/res/layout/fragment_send_transaction.xml +++ b/app/src/main/res/layout/fragment_send_transaction.xml @@ -42,6 +42,7 @@ android:id="@+id/tietTo" android:layout_width="match_parent" android:layout_height="wrap_content" + android:digits="abcdefghijklmnopqrstuvwxyz-0123456789" android:maxLines="1"/> @@ -135,7 +136,7 @@ app:layout_constraintEnd_toStartOf="@id/cameraVerticalGuideline"/>