From dfd45da52abfe840177b3cab9da1370d6e6169ce Mon Sep 17 00:00:00 2001 From: harshilpadsala88 Date: Mon, 13 May 2024 15:52:14 +0530 Subject: [PATCH] Updated SmartStorage Repo --- .gitignore | 1 + .../com/ss/smart_storage/PermissionManager.kt | 47 ++++++++----------- .../java/com/ss/smart_storage/SmartStorage.kt | 8 ---- .../ss/smart_storage/util/PermissionStatus.kt | 2 +- 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index d14dddc..7664125 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.iml .gradle .idea /local.properties diff --git a/smart-storage/src/main/java/com/ss/smart_storage/PermissionManager.kt b/smart-storage/src/main/java/com/ss/smart_storage/PermissionManager.kt index 787ce5f..924d1df 100644 --- a/smart-storage/src/main/java/com/ss/smart_storage/PermissionManager.kt +++ b/smart-storage/src/main/java/com/ss/smart_storage/PermissionManager.kt @@ -3,8 +3,12 @@ package com.ss.smart_storage import android.Manifest import android.content.pm.PackageManager import android.os.Build +import android.os.Build.VERSION_CODES.M +import android.os.Build.VERSION_CODES.Q +import android.os.Environment import androidx.activity.ComponentActivity import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import com.ss.smart_storage.util.PermissionStatus import com.ss.smart_storage.util.SmartDirectory @@ -55,27 +59,21 @@ class PermissionManager( private fun checkOsForPermissions(location: String) { when { - isVersionInBetween(Build.VERSION_CODES.M, Build.VERSION_CODES.Q) -> { - checkIfPermissionGranted( - onPermissionGranted = { - onPermissionGranted(PermissionStatus.ACCEPTED) - }) + Build.VERSION.SDK_INT in M..Q -> { + checkIfPermissionGranted(onPermissionGranted = { + onPermissionGranted(PermissionStatus.ACCEPTED) + }) } - isVersionInBetween( - Build.VERSION_CODES.R, Build.VERSION_CODES.S - ) && location == SmartDirectory.EXTERNAL_PUBLIC -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - checkIfManagePermissionGranted( - onPermissionGranted = { granted -> - if(granted) onPermissionGranted(PermissionStatus.ACCEPTED) - else onPermissionGranted(PermissionStatus.REDIRECT_TO_SETTINGS) - }) - } + + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) && location == SmartDirectory.EXTERNAL_PUBLIC -> { + + checkIfManagePermissionGranted(onPermissionGranted = { granted -> + if (granted) onPermissionGranted(PermissionStatus.ACCEPTED) + else onPermissionGranted(PermissionStatus.REDIRECT_TO_SETTINGS) + }) } - Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && location == SmartDirectory.EXTERNAL_PUBLIC -> { - onPermissionGranted(PermissionStatus.NOT_AVAILABLE) - } + else -> { onPermissionGranted(PermissionStatus.ACCEPTED) } @@ -94,10 +92,9 @@ class PermissionManager( } } - private fun checkIfManagePermissionGranted( onPermissionGranted: (Boolean) -> Unit) { - if (ContextCompat.checkSelfPermission( - activity, Manifest.permission.MANAGE_EXTERNAL_STORAGE , - ) == PackageManager.PERMISSION_GRANTED + @RequiresApi(Build.VERSION_CODES.R) + private fun checkIfManagePermissionGranted(onPermissionGranted: (Boolean) -> Unit) { + if (Environment.isExternalStorageManager() ) { onPermissionGranted(true) } else { @@ -107,10 +104,4 @@ class PermissionManager( - - private fun isVersionInBetween(min: Int, max: Int): Boolean { - return Build.VERSION.SDK_INT in min..max - } - - } \ No newline at end of file diff --git a/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt b/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt index ff74e71..36ff32d 100644 --- a/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt +++ b/smart-storage/src/main/java/com/ss/smart_storage/SmartStorage.kt @@ -54,14 +54,6 @@ class SmartStorage(private val activity: ComponentActivity) { launchBaseDirectoryPicker() } - PermissionStatus.NOT_AVAILABLE -> { - Toast.makeText( - activity, - activity.getString(R.string.feature_not_available), - Toast.LENGTH_SHORT - ).show() - } - PermissionStatus.REDIRECT_TO_SETTINGS -> { requestFullStorageAccess() } diff --git a/smart-storage/src/main/java/com/ss/smart_storage/util/PermissionStatus.kt b/smart-storage/src/main/java/com/ss/smart_storage/util/PermissionStatus.kt index a45461c..60b8a87 100644 --- a/smart-storage/src/main/java/com/ss/smart_storage/util/PermissionStatus.kt +++ b/smart-storage/src/main/java/com/ss/smart_storage/util/PermissionStatus.kt @@ -1,5 +1,5 @@ package com.ss.smart_storage.util enum class PermissionStatus { - ACCEPTED, DENIED, NOT_APPLICABLE, NOT_NEEDED, NOT_AVAILABLE, REDIRECT_TO_SETTINGS + ACCEPTED, DENIED, NOT_APPLICABLE, NOT_NEEDED, REDIRECT_TO_SETTINGS } \ No newline at end of file