Skip to content

Commit

Permalink
Issues boostcampwm-2022#287 feat: Permission 을 위해 설정화면으로 이동하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
audxo112 committed Mar 1, 2023
1 parent 80c12fa commit ea4b93c
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package com.lighthouse.core.android.utils.permission

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import com.lighthouse.core.android.utils.permission.core.PermissionManager

class LocationPermissionManager(activity: Activity) : PermissionManager(activity) {
class LocationPermissionManager(context: Context) : PermissionManager(context) {

override val additionalPermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
override val additionalPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
emptyArray()
}

override val permissions =
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)

override fun settingIntent(): Intent {
return Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", context.packageName, null)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.lighthouse.core.android.utils.permission

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Build
import com.lighthouse.core.android.utils.permission.core.PermissionManager

class StoragePermissionManager(activity: Activity) : PermissionManager(activity) {
class StoragePermissionManager(context: Context) : PermissionManager(context) {

override val permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
arrayOf(Manifest.permission.READ_MEDIA_IMAGES)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@ package com.lighthouse.core.android.utils.permission.core

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import kotlinx.coroutines.flow.MutableStateFlow

abstract class PermissionManager(
private val activity: Activity
protected val context: Context
) {
abstract val permissions: Array<String>
protected abstract val permissions: Array<String>

open val additionalPermission = emptyArray<String>()
protected open val additionalPermissions = emptyArray<String>()

val basicPermission
get() = permissions.firstOrNull() ?: ""

val permissionFlow by lazy {
MutableStateFlow(isGrant)
fun getPermissions(type: PermissionType = PermissionType.Basic): Array<String> {
return when (type) {
PermissionType.All -> permissions + additionalPermissions
PermissionType.Basic -> permissions
PermissionType.Additional -> additionalPermissions
}.copyOf()
}

val isGrant
get() = permissions.all { permission ->
activity.checkPermission(permission)
fun isGrant(type: PermissionType = PermissionType.Basic): Boolean {
val list = when (type) {
PermissionType.All -> permissions + additionalPermissions
PermissionType.Basic -> permissions
PermissionType.Additional -> additionalPermissions
}
return list.all {
context.checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED
}
}

val permissionState
get() = when {
(permissions + additionalPermission).all { activity.checkPermission(it) } -> {
BeepPermissionState.AllAllowedPermission
}
permissions.all { activity.checkPermission(it) } -> {
BeepPermissionState.PartiallyAllowedPermission
}
else -> {
BeepPermissionState.NotAllowedPermission
fun getManyTimesRejectedPermission(type: PermissionType = PermissionType.Basic): String? {
val activity = context as? Activity ?: return null
val permissions = getPermissions(type)
for (permission in permissions) {
if (activity.shouldShowRequestPermissionRationale(permission)) {
return permission
}
}
}
return null
}

fun Context.checkPermission(permission: String) =
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
open fun settingIntent(): Intent {
return Intent()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lighthouse.core.android.utils.permission.core

enum class PermissionType {
All,
Basic,
Additional
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.lighthouse.core.android.utils.permission.StoragePermissionManager
import com.lighthouse.core.android.utils.permission.core.permissions
import com.lighthouse.features.common.binding.viewBindings
import com.lighthouse.features.common.ext.repeatOnStarted
import com.lighthouse.features.main.R
Expand All @@ -30,8 +28,6 @@ class MainContainerFragment : Fragment(R.layout.fragment_main_container) {

private val mainNavigationViewModel: MainNavigationViewModel by viewModels()

private val storagePermission: StoragePermissionManager by permissions()

private lateinit var navController: NavController

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
Expand All @@ -14,6 +15,7 @@ import com.google.android.material.snackbar.Snackbar
import com.lighthouse.auth.google.model.GoogleAuthEvent
import com.lighthouse.auth.google.repository.GoogleClient
import com.lighthouse.auth.google.ui.GoogleAuthViewModel
import com.lighthouse.core.android.utils.permission.LocationPermissionManager
import com.lighthouse.core.android.utils.resource.UIText
import com.lighthouse.features.common.binding.viewBindings
import com.lighthouse.features.common.dialog.progress.ProgressDialog
Expand Down Expand Up @@ -78,6 +80,28 @@ class SettingFragment : Fragment(R.layout.fragment_setting) {
}
}

private val locationPermissionSettingLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val isGrant = locationPermissionManager.isGrant()
if (!isGrant) {
showSnackBar(R.string.error_permission_not_allowed)
}
viewModel.setLocationEnable(isGrant)
}

private val locationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { results ->
val isGrant = results.all { it.value }
if (!isGrant) {
showSnackBar(R.string.error_permission_not_allowed)
}
viewModel.setLocationEnable(isGrant)
}

private val locationPermissionManager by lazy {
LocationPermissionManager(requireActivity())
}

override fun onAttach(context: Context) {
super.onAttach(context)

Expand All @@ -93,9 +117,14 @@ class SettingFragment : Fragment(R.layout.fragment_setting) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setUpLocationPermission()
setUpSettingMenu()
setUpGoogleAuthEvent()
setUpSignInLoading()
setUpSettingMenu()
}

private fun setUpLocationPermission() {
viewModel.setLocationEnable(locationPermissionManager.isGrant())
}

private fun setUpSettingMenu() {
Expand Down Expand Up @@ -141,7 +170,7 @@ class SettingFragment : Fragment(R.layout.fragment_setting) {
SettingMenu.SECURITY ->
appNavigationViewModel.navigate(AppNavigationItem.Security)

SettingMenu.LOCATION -> location()
SettingMenu.LOCATION -> gotoLocation()
SettingMenu.SIGN_IN -> signIn()
SettingMenu.SIGN_OUT -> signOut()
SettingMenu.WITHDRAWAL -> withdrawal()
Expand All @@ -161,7 +190,15 @@ class SettingFragment : Fragment(R.layout.fragment_setting) {
}
}

private fun location() {
private fun gotoLocation() {
if (
locationPermissionManager.isGrant() ||
locationPermissionManager.getManyTimesRejectedPermission() != null
) {
locationPermissionSettingLauncher.launch(locationPermissionManager.settingIntent())
} else {
locationPermissionLauncher.launch(locationPermissionManager.getPermissions())
}
}

private fun signIn() {
Expand Down Expand Up @@ -202,6 +239,10 @@ class SettingFragment : Fragment(R.layout.fragment_setting) {
Snackbar.make(binding.root, string, Snackbar.LENGTH_SHORT).show()
}

private fun showSnackBar(@StringRes resId: Int) {
Snackbar.make(binding.root, getString(resId), Snackbar.LENGTH_SHORT).show()
}

private fun showSnackBar(uiText: UIText) {
showSnackBar(uiText.asString(requireContext()).toString())
}
Expand Down
1 change: 1 addition & 0 deletions features/ui-setting/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="config_location_permission">위치 정보 권한</string>
<string name="location_permission_allowed">허용됨</string>
<string name="location_permission_not_allowed">허용 안 함</string>
<string name="error_permission_not_allowed">권한을 허용하지 않았습니다.</string>

<string name="subtitle_user">사용자</string>
<string name="user_sign_out">로그아웃</string>
Expand Down

0 comments on commit ea4b93c

Please sign in to comment.