Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/explore #22

Merged
merged 13 commits into from
Dec 26, 2024
Merged
2 changes: 1 addition & 1 deletion app/src/main/java/kr/co/seedocs/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.activity.enableEdgeToEdge
import kr.co.main.Main
import kr.co.ui.theme.SeeDocsTheme

class MainActivity : ComponentActivity() {
class MainActivity : PermissionActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
71 changes: 71 additions & 0 deletions app/src/main/java/kr/co/seedocs/PermissionActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package kr.co.seedocs

import android.Manifest
import android.app.AppOpsManager
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import androidx.activity.ComponentActivity
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

abstract class PermissionActivity : ComponentActivity() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추상화된 함수를 제공하지 않는다면 open class가 더 어울릴 것 같습니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

권한이 Storage permission으로 한정되어 있으므로 이름을 명확하게 하는것이 좋을 것 같아요.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어차피 캡슐화를 할 것이라면, 스토리지 권한만을 위한 로직의 책임을 가진 class로 만들고, 사용하는 곳에서 활용하도록 하는것도 좋을 것 같아요.


internal fun ComponentActivity.checkStoragePermission(): Boolean =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수들이 이미 ComponentActivity 내에 있으므로 확장함수로 구현하지 않아도 될 것 같아요.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
checkStoragePermissionApi30()
} else {
checkStoragePermissionApi19()
}

@RequiresApi(Build.VERSION_CODES.R)
private fun ComponentActivity.checkStoragePermissionApi30(): Boolean {
val appOps = getSystemService(AppOpsManager::class.java)
val mode = appOps.unsafeCheckOpNoThrow(
MANAGE_EXTERNAL_STORAGE_PERMISSION,
applicationInfo.uid,
packageName
)

return mode == AppOpsManager.MODE_ALLOWED
}

private fun ComponentActivity.checkStoragePermissionApi19(): Boolean {
val status =
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)

return status == PackageManager.PERMISSION_GRANTED
}

internal fun ComponentActivity.requestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestStoragePermissionApi30()
} else {
requestStoragePermissionApi19()
}
}

@RequiresApi(Build.VERSION_CODES.R)
private fun ComponentActivity.requestStoragePermissionApi30() {
val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)

startActivityForResult(intent, MANAGE_EXTERNAL_STORAGE_PERMISSION_REQUEST)
}

private fun ComponentActivity.requestStoragePermissionApi19() {
val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
ActivityCompat.requestPermissions(
this,
permissions,
READ_EXTERNAL_STORAGE_PERMISSION_REQUEST
)
}

private companion object {
private const val MANAGE_EXTERNAL_STORAGE_PERMISSION = "android:manage_external_storage"
private const val MANAGE_EXTERNAL_STORAGE_PERMISSION_REQUEST = 100
private const val READ_EXTERNAL_STORAGE_PERMISSION_REQUEST = 101
}
}
67 changes: 0 additions & 67 deletions app/src/main/java/kr/co/seedocs/PermissionUtil.kt

This file was deleted.

Loading