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.
This commit is contained in:
Severiano Jaramillo 2018-12-04 11:33:58 -06:00
parent 95404708a9
commit fa2c7a07d8
4 changed files with 95 additions and 7 deletions

View file

@ -3,6 +3,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="cy.agorise.bitsybitshareswallet">
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".utils.BitsyApplication"
android:allowBackup="true"

View file

@ -1,19 +1,99 @@
package cy.agorise.bitsybitshareswallet.fragments
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.zxing.BarcodeFormat
import com.google.zxing.Result
import cy.agorise.bitsybitshareswallet.R
import kotlinx.android.synthetic.main.fragment_send_transaction.*
import me.dm7.barcodescanner.zxing.ZXingScannerView
class SendTransactionFragment : Fragment() {
class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Camera Permission
private val REQUEST_CAMERA_PERMISSION = 1
private var isCameraPreviewVisible = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_send_transaction, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
verifyCameraPermission()
fabOpenCamera.setOnClickListener { if (isCameraPreviewVisible) stopCameraPreview() else verifyCameraPermission() }
}
private fun verifyCameraPermission() {
if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not already granted
requestPermissions(arrayOf(android.Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
} else {
// Permission is already granted
startCameraPreview()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, 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()
}
}

View file

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View file

@ -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"/>
</com.google.android.material.textfield.TextInputLayout>
@ -135,7 +136,7 @@
app:layout_constraintEnd_toStartOf="@id/cameraVerticalGuideline"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabCloseCamera"
android:id="@+id/fabOpenCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabCustomSize="32dp"
@ -145,7 +146,7 @@
app:srcCompat="@drawable/ic_camera" />
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="@+id/ivCamera"
android:id="@+id/cameraPreview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="w,3:4"