Skip to content

Commit

Permalink
feat: 권한 관련 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
briandr97 committed Nov 6, 2023
1 parent 086cf88 commit 30b1f09
Showing 1 changed file with 41 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,47 @@ class UploadActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
setImage(requireNotNull(imageUri) { "imageUri가 null입니다" })
}

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions(),
) { permission: Map<String, Boolean> ->
if (checkStorageRequest(permission)) return@registerForActivityResult
checkLocationRequest(permission)
}
private val storagePermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val isGranted: Boolean = permissions.values.all { it }
if (!isGranted) {
showStoragePermissionSnackBar()
return@registerForActivityResult
}
openCamera()
}

private val locationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val isGranted: Boolean = permissions.values.all { true }
if (!isGranted) {
showLocationPermissionSnackBar()
return@registerForActivityResult
}
setCoordinate()
}

private val isStoragePermissionGranted: Boolean
get() = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
storagePermissionsBelow28.all { checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }
} else {
true
}

private val locationSettingLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
setCoordinate()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityUploadBinding.inflate(layoutInflater)
setContentView(binding.root)

initViewModel()
subscribe()
registerAnalytics(this.lifecycle)
setCoordinate()
setClickListeners()
locationPermissionLauncher.launch(locationPermissions)
registerAnalytics(this.lifecycle)
}

private fun initViewModel() {
Expand Down Expand Up @@ -136,32 +155,6 @@ class UploadActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
view.visibility = status
}

private fun checkStorageRequest(permission: Map<String, Boolean>): Boolean {
val keys = permission.entries.map { it.key }
val isStorageRequest = storagePermissions.any { keys.contains(it) }
if (isStorageRequest) {
if (permission.entries.map { it.value }.contains(false)) {
showStoragePermissionSnackBar()
} else {
openCamera()
}
return true
}
return false
}

private fun checkLocationRequest(permission: Map<String, Boolean>) {
val keys = permission.entries.map { it.key }
val isLocationRequest = locationPermissions.any { keys.contains(it) }
if (isLocationRequest) {
if (permission.entries.map { it.value }.contains(false)) {
showLocationPermissionSnackBar()
return
}
setCoordinate()
}
}

private fun showStoragePermissionSnackBar() {
binding.root.showSnackbarWithEvent(
message = getString(R.string.snackbar_storage_message),
Expand Down Expand Up @@ -210,16 +203,15 @@ class UploadActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
}

private fun setCoordinate() {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, createCancellationToken())
.addOnSuccessListener { location ->
location.let { viewModel.setCoordinate(getCoordinate(location)) }
}
.addOnFailureListener { }
} else {
requestPermissionLauncher.launch(locationPermissions)
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
showLocationPermissionSnackBar()
return
}
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, createCancellationToken())
.addOnSuccessListener { location ->
location.let { viewModel.setCoordinate(getCoordinate(location)) }
}.addOnFailureListener { }
}

private fun createCancellationToken(): CancellationToken {
Expand All @@ -246,10 +238,11 @@ class UploadActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
}

private fun openCameraWithPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return openCamera()
if (!isStoragePermissionGranted) {
storagePermissionLauncher.launch(storagePermissionsBelow28)
return
}
requestPermissionLauncher.launch(storagePermissions)
openCamera()
}

private fun openCamera() {
Expand All @@ -275,7 +268,7 @@ class UploadActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
companion object {
private const val PRIORITY_HIGH_ACCURACY = 100
private const val RESIZE = 500
private val storagePermissions = arrayOf(
private val storagePermissionsBelow28 = arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
)
Expand Down

0 comments on commit 30b1f09

Please sign in to comment.