Skip to content

Commit

Permalink
add internal request all files perm
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Feb 4, 2024
1 parent 62b31b8 commit 3853a46
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions app/src/main/java/com/geode/launcher/utils/GeodeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -371,32 +371,53 @@ object GeodeUtils {
return "com.geode.launcher.user" == uri.authority
}

private const val INTERNAL_PERMISSION_PREFIX = "geode.permission_internal"
private const val MANAGE_ALL_FILES = "${INTERNAL_PERMISSION_PREFIX}.MANAGE_ALL_FILES"

@JvmStatic
fun getPermissionStatus(permission: String): Boolean {
return ContextCompat.checkSelfPermission(
activity.get()?.applicationContext ?: return false,
permission
) == PackageManager.PERMISSION_GRANTED
val context = activity.get() ?: return false

return when (permission) {
MANAGE_ALL_FILES -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
Environment.isExternalStorageManager()
} else {
val permissions = listOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)

return permissions.all {
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
}
else -> ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
}
}

@JvmStatic
fun requestPermission(permission: String) {
if (getPermissionStatus(permission)) {
permissionCallback(true)
if (permission == MANAGE_ALL_FILES) {
// this function handles already having perms for us
checkForFilePermissions(onSuccess = {
permissionCallback(true)
}, onFailure = {
permissionCallback(false)
})

return
}

activity.get()?.run {
try {
requestPermissionLauncher.launch(permission)
} catch (e: ActivityNotFoundException) {
permissionCallback(false)
}

if (getPermissionStatus(permission)) {
permissionCallback(true)
return
}

permissionCallback(false)
try {
requestPermissionLauncher.launch(permission)
} catch (e: ActivityNotFoundException) {
permissionCallback(false)
}
}

private external fun permissionCallback(granted: Boolean)
Expand Down

0 comments on commit 3853a46

Please sign in to comment.