Skip to content

Commit

Permalink
[AN/STAFF] fix: 카메라 권한 요청(#77) (#80)
Browse files Browse the repository at this point in the history
* fix: 카메라 권한 요청 구현

* style: 문자열 리소스 name 수정

* refactor: 코틀린 컬렉션 api로 리펙토링
  • Loading branch information
EmilyCh0 authored Jul 20, 2023
1 parent d1058f8 commit a1e3878
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions android/festago-staff/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.festago.festagostaff.presentation.extension

import android.app.Activity
import android.widget.Toast

fun Activity.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.festago.festagostaff.presentation.ui.ticketvalidation

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_DENIED
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Bundle
import android.view.KeyEvent
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.festago.festagostaff.R
import com.festago.festagostaff.data.RetrofitClient
import com.festago.festagostaff.data.repository.TicketDefaultRepository
import com.festago.festagostaff.databinding.ActivityTicketValidationBinding
import com.festago.festagostaff.presentation.extension.showToast
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeCallback
import com.journeyapps.barcodescanner.DefaultDecoderFactory
Expand All @@ -28,7 +34,7 @@ class TicketValidationActivity : AppCompatActivity() {

private val barcodeCallback = BarcodeCallback { result ->
if (result.text == null) {
Toast.makeText(this, "No content", Toast.LENGTH_SHORT).show()
showToast(getString(R.string.ticket_validation_toast_no_content))
return@BarcodeCallback
}
if (!vm.isLatestCode(result.text)) {
Expand All @@ -40,6 +46,7 @@ class TicketValidationActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)

initBinding()
checkPermission(Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE)
}

private fun initBinding() {
Expand Down Expand Up @@ -69,7 +76,30 @@ class TicketValidationActivity : AppCompatActivity() {
return binding.dbvScanner.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
}

private fun checkPermission(permission: String, requestCode: Int) {
if (ContextCompat.checkSelfPermission(this, permission) == PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.getOrNull(0) == PERMISSION_GRANTED) {
showToast(getString(R.string.ticket_validation_toast_camera_permission_granted))
} else {
showToast(getString(R.string.ticket_validation_toast_camera_permission_denied))
}
}
}

companion object {
private const val CAMERA_PERMISSION_CODE = 111

fun getIntent(context: Context): Intent {
return Intent(context, TicketValidationActivity::class.java)
}
Expand Down
5 changes: 5 additions & 0 deletions android/festago-staff/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
<string name="app_name">festago-staff</string>
<string name="main_btn_check_ticket">티켓 검사</string>
<string name="main_label_staff">STAFF</string>

<!-- Strings related to ticket validation -->
<string name="ticket_validation_toast_camera_permission_granted">카메라 권한이 허용되었습니다.</string>
<string name="ticket_validation_toast_camera_permission_denied">카메라 권한을 거부했습니다.</string>
<string name="ticket_validation_toast_no_content">No content</string>
</resources>

0 comments on commit a1e3878

Please sign in to comment.