Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.sokoil.multitransport.view.activity
- import android.Manifest
- import android.app.Activity
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.PointF
- import android.os.Bundle
- import android.view.View
- import android.widget.Toast
- import androidx.core.app.ActivityCompat
- import androidx.core.content.ContextCompat
- import butterknife.BindView
- import butterknife.ButterKnife
- import com.dlazaro66.qrcodereaderview.QRCodeReaderView
- import ru.sokoil.multitransport.R
- import ru.sokoil.multitransport.data.repository.RouteRepository
- import ru.sokoil.multitransport.view.base.BaseActivity
- import java.util.*
- class ScanQRActivity : BaseActivity(), QRCodeReaderView.OnQRCodeReadListener {
- @BindView(R.id.qrDecoderView) lateinit var qrCodeReaderView: QRCodeReaderView
- var lastMsgDate = Date()
- lateinit var routeRepository: RouteRepository
- companion object {
- fun newInstance(context: Activity, type: Int, intentCode: Int) {
- val intent = Intent(context, ScanQRActivity::class.java)
- intent.putExtra("type", type)
- context.startActivityForResult(intent, intentCode)
- }
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_qr_read)
- supportActionBar!!.setDisplayHomeAsUpEnabled(true)
- ButterKnife.bind(this)
- routeRepository = RouteRepository()
- if (checkPerm())
- setUpViews()
- }
- fun setUpViews() {
- qrCodeReaderView.visibility = View.VISIBLE
- qrCodeReaderView.setOnQRCodeReadListener(this)
- qrCodeReaderView.setQRDecodingEnabled(true)
- qrCodeReaderView.setAutofocusInterval(2000L)
- qrCodeReaderView.setTorchEnabled(true)
- qrCodeReaderView.setFrontCamera()
- qrCodeReaderView.setBackCamera()
- }
- override fun onQRCodeRead(text: String, points: Array<PointF>) {
- println("text = $text")
- try {
- val type = intent.extras!!.getInt("type")
- if(type == SCAN_QR_TYPE_STATION1 || type == SCAN_QR_TYPE_STATION2) {
- val stationId = text.toInt()
- val intent = Intent()
- intent.putExtra("st", type)
- intent.putExtra("code", stationId)
- setResult(Activity.RESULT_OK, intent)
- qrCodeReaderView.stopCamera()
- finish()
- } else if(type == INTENT_CODE_SCAN_TRANSPORT) {
- val intent = Intent()
- intent.putExtra("code", text)
- setResult(Activity.RESULT_OK, intent)
- qrCodeReaderView.stopCamera()
- finish()
- } else if(type == INTENT_CODE_SCAN_CITY) {
- val arr = text.replace("\\", "").split("|")
- if(arr.count() == 3) {
- val routeId = arr[0]
- val st1 = arr[1].toInt()
- val st2 = arr[2].toInt()
- val route = routeRepository.getRoutes(
- routeRepository.getStation(st1),
- routeRepository.getStation(st2)
- ).filter { it.routes.first().route.internalNumber.equals(routeId) }.first()
- val intent = Intent()
- intent.putExtra("st1", st1)
- intent.putExtra("st2", st2)
- intent.putExtra("route", route)
- setResult(Activity.RESULT_OK, intent)
- qrCodeReaderView.stopCamera()
- finish()
- }
- }
- } catch (e: Exception) {
- e.printStackTrace()
- if(lastMsgDate.time + 1000*5 <= Date().time) {
- Toast.makeText(this, "Неверный QR код", Toast.LENGTH_LONG).show()
- lastMsgDate = Date()
- }
- }
- }
- override fun onResume() {
- super.onResume()
- qrCodeReaderView.startCamera()
- }
- override fun onPause() {
- super.onPause()
- qrCodeReaderView.stopCamera()
- }
- internal fun checkPerm(): Boolean {
- if (ContextCompat.checkSelfPermission(
- this,
- Manifest.permission.CAMERA
- ) !== PackageManager.PERMISSION_GRANTED
- ) {
- if (ActivityCompat.shouldShowRequestPermissionRationale(
- this,
- Manifest.permission.CAMERA
- )
- ) {
- } else {
- ActivityCompat.requestPermissions(
- this,
- arrayOf<String>(Manifest.permission.CAMERA),
- 1
- )
- }
- return false
- } else {
- return true
- }
- }
- override fun onRequestPermissionsResult(
- requestCode: Int,
- permissions: Array<out String>,
- grantResults: IntArray
- ) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
- when (requestCode) {
- 1 -> {
- if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- setUpViews()
- } else {
- checkPerm()
- }
- return
- }
- }
- }
- }
- val INTENT_CODE_SCAN_STATION = 200
- val INTENT_CODE_SCAN_TRANSPORT = 201
- val INTENT_CODE_SCAN_CITY = 202
- val SCAN_QR_TYPE_STATION1 = 0
- val SCAN_QR_TYPE_STATION2 = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement