Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.54 KB | None | 0 0
  1. package ru.sokoil.multitransport.view.activity
  2.  
  3. import android.Manifest
  4. import android.app.Activity
  5. import android.content.Intent
  6. import android.content.pm.PackageManager
  7. import android.graphics.PointF
  8. import android.os.Bundle
  9. import android.view.View
  10. import android.widget.Toast
  11. import androidx.core.app.ActivityCompat
  12. import androidx.core.content.ContextCompat
  13. import butterknife.BindView
  14. import butterknife.ButterKnife
  15. import com.dlazaro66.qrcodereaderview.QRCodeReaderView
  16. import ru.sokoil.multitransport.R
  17. import ru.sokoil.multitransport.data.repository.RouteRepository
  18. import ru.sokoil.multitransport.view.base.BaseActivity
  19. import java.util.*
  20.  
  21.  
  22. class ScanQRActivity : BaseActivity(), QRCodeReaderView.OnQRCodeReadListener {
  23.  
  24.     @BindView(R.id.qrDecoderView) lateinit var qrCodeReaderView: QRCodeReaderView
  25.     var lastMsgDate = Date()
  26.     lateinit var routeRepository: RouteRepository
  27.  
  28.     companion object {
  29.         fun newInstance(context: Activity, type: Int, intentCode: Int) {
  30.             val intent = Intent(context, ScanQRActivity::class.java)
  31.             intent.putExtra("type", type)
  32.             context.startActivityForResult(intent, intentCode)
  33.         }
  34.     }
  35.  
  36.     override fun onCreate(savedInstanceState: Bundle?) {
  37.         super.onCreate(savedInstanceState)
  38.         setContentView(R.layout.activity_qr_read)
  39.         supportActionBar!!.setDisplayHomeAsUpEnabled(true)
  40.         ButterKnife.bind(this)
  41.         routeRepository = RouteRepository()
  42.         if (checkPerm())
  43.             setUpViews()
  44.     }
  45.  
  46.     fun setUpViews() {
  47.         qrCodeReaderView.visibility = View.VISIBLE
  48.         qrCodeReaderView.setOnQRCodeReadListener(this)
  49.         qrCodeReaderView.setQRDecodingEnabled(true)
  50.         qrCodeReaderView.setAutofocusInterval(2000L)
  51.         qrCodeReaderView.setTorchEnabled(true)
  52.         qrCodeReaderView.setFrontCamera()
  53.         qrCodeReaderView.setBackCamera()
  54.     }
  55.  
  56.     override fun onQRCodeRead(text: String, points: Array<PointF>) {
  57.         println("text = $text")
  58.         try {
  59.             val type = intent.extras!!.getInt("type")
  60.             if(type == SCAN_QR_TYPE_STATION1 || type == SCAN_QR_TYPE_STATION2) {
  61.                 val stationId = text.toInt()
  62.                 val intent = Intent()
  63.                 intent.putExtra("st", type)
  64.                 intent.putExtra("code", stationId)
  65.                 setResult(Activity.RESULT_OK, intent)
  66.                 qrCodeReaderView.stopCamera()
  67.                 finish()
  68.             } else if(type == INTENT_CODE_SCAN_TRANSPORT) {
  69.                 val intent = Intent()
  70.                 intent.putExtra("code", text)
  71.                 setResult(Activity.RESULT_OK, intent)
  72.                 qrCodeReaderView.stopCamera()
  73.                 finish()
  74.             } else if(type == INTENT_CODE_SCAN_CITY) {
  75.                 val arr = text.replace("\\", "").split("|")
  76.                 if(arr.count() == 3) {
  77.                     val routeId = arr[0]
  78.                     val st1 = arr[1].toInt()
  79.                     val st2 = arr[2].toInt()
  80.                     val route = routeRepository.getRoutes(
  81.                         routeRepository.getStation(st1),
  82.                         routeRepository.getStation(st2)
  83.                     ).filter { it.routes.first().route.internalNumber.equals(routeId) }.first()
  84.                     val intent = Intent()
  85.                     intent.putExtra("st1", st1)
  86.                     intent.putExtra("st2", st2)
  87.                     intent.putExtra("route", route)
  88.                     setResult(Activity.RESULT_OK, intent)
  89.                     qrCodeReaderView.stopCamera()
  90.                     finish()
  91.                 }
  92.             }
  93.         } catch (e: Exception) {
  94.             e.printStackTrace()
  95.             if(lastMsgDate.time + 1000*5  <= Date().time) {
  96.                 Toast.makeText(this, "Неверный QR код", Toast.LENGTH_LONG).show()
  97.                 lastMsgDate = Date()
  98.             }
  99.         }
  100.     }
  101.  
  102.     override fun onResume() {
  103.         super.onResume()
  104.         qrCodeReaderView.startCamera()
  105.     }
  106.  
  107.     override fun onPause() {
  108.         super.onPause()
  109.         qrCodeReaderView.stopCamera()
  110.     }
  111.  
  112.     internal fun checkPerm(): Boolean {
  113.         if (ContextCompat.checkSelfPermission(
  114.                 this,
  115.                 Manifest.permission.CAMERA
  116.             ) !== PackageManager.PERMISSION_GRANTED
  117.         ) {
  118.             if (ActivityCompat.shouldShowRequestPermissionRationale(
  119.                     this,
  120.                     Manifest.permission.CAMERA
  121.                 )
  122.             ) {
  123.  
  124.             } else {
  125.                 ActivityCompat.requestPermissions(
  126.                     this,
  127.                     arrayOf<String>(Manifest.permission.CAMERA),
  128.                     1
  129.                 )
  130.             }
  131.             return false
  132.         } else {
  133.             return true
  134.         }
  135.     }
  136.  
  137.     override fun onRequestPermissionsResult(
  138.         requestCode: Int,
  139.         permissions: Array<out String>,
  140.         grantResults: IntArray
  141.     ) {
  142.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  143.         when (requestCode) {
  144.             1 -> {
  145.                 if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  146.                     setUpViews()
  147.                 } else {
  148.                     checkPerm()
  149.                 }
  150.                 return
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156. val INTENT_CODE_SCAN_STATION = 200
  157. val INTENT_CODE_SCAN_TRANSPORT = 201
  158. val INTENT_CODE_SCAN_CITY = 202
  159.  
  160. val SCAN_QR_TYPE_STATION1 = 0
  161. val SCAN_QR_TYPE_STATION2 = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement