flutter_barcode_reader/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScannerActivity.kt

158 lines
5.5 KiB
Kotlin

/*
* ************************************************************************
* *
* * AppTree Software Inc CONFIDENTIAL
* * __________________
* *
* * [2012] - [2016] AppTree Software Inc
* * All Rights Reserved.
* *
* * NOTICE: All information contained herein is, and remains
* * the property of AppTree Software Inc and its suppliers,
* * if any. The intellectual and technical concepts contained
* * herein are proprietary to AppTree Software Inc
* * and its suppliers and may be covered by U.S. and Foreign Patents,
* * patents in process, and are protected by trade secret or copyright law.
* * Dissemination of this information or reproduction of this material
* * is strictly forbidden unless prior written permission is obtained
* * from AppTree Software Inc.
*
*/
package com.apptreesoftware.barcodescan
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.view.Menu
import android.view.MenuItem
import com.google.zxing.Result
import me.dm7.barcodescanner.zxing.ZXingScannerView
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
//lateinit var pressLightSnackBar: Snackbar
companion object {
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
val TOGGLE_FLASH = 200
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = ""
scannerView = ZXingScannerView(this)
scannerView.setAutoFocus(true)
setContentView(scannerView)
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
// Int.MAX_VALUE)
// pressLightSnackBar.show()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (scannerView.flash) {
val item = menu.add(0,
TOGGLE_FLASH, 0, "Flash Off")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
} else {
val item = menu.add(0,
TOGGLE_FLASH, 0, "Flash On")
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
}
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == TOGGLE_FLASH) {
scannerView.flash = !scannerView.flash
this.invalidateOptionsMenu()
return true
}
return super.onOptionsItemSelected(item)
}
override fun onResume() {
super.onResume()
scannerView.setResultHandler(this)
if (!requestCameraAccessIfNecessary()) {
scannerView.startCamera()
}
}
override fun onPause() {
super.onPause()
scannerView.stopCamera()
}
override fun handleResult(result: Result?) {
val intent = Intent()
intent.putExtra("SCAN_RESULT", result.toString())
setResult(Activity.RESULT_OK, intent)
finish()
}
private fun requestCameraAccessIfNecessary(): Boolean {
val array = arrayOf(Manifest.permission.CAMERA)
if (ContextCompat
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
val snackbar = Snackbar.make(scannerView, "Grant access to the camera to enable barcode scanning.", Snackbar.LENGTH_INDEFINITE)
snackbar.setAction("OK") {
ActivityCompat.requestPermissions(this,
array,
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
}
snackbar.show()
} else {
ActivityCompat.requestPermissions(this,
array,
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
}
return true
}
return false
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
grantResults: IntArray) {
when (requestCode) {
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
if (PermissionUtil.verifyPermissions(grantResults)) {
scannerView.startCamera()
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
object PermissionUtil {
/**
* Check that all given permissions have been granted by verifying that each entry in the
* given array is of the value [PackageManager.PERMISSION_GRANTED].
* @see Activity.onRequestPermissionsResult
*/
fun verifyPermissions(grantResults: IntArray): Boolean {
// At least one result must be checked.
if (grantResults.size < 1) {
return false
}
// Verify that each required permission has been granted, otherwise return false.
for (result in grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false
}
}
return true
}
}