mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 23:07:26 -04:00
Fixed: If storage permission is permanently denied on Android 11 or below, the selected file via the file picker does not open.
* The storage permission dialog is now shown when the user clicks the "File Picker" button, prompting them to grant the permission first before selecting a ZIM file via the file picker. * If storage permission is denied or not granted, and the user opens a ZIM file by clicking on it in the file manager, the app will first prompt for permission. Once the permission is granted, the ZIM file will open in the reader. If the permission is not granted, the operation is canceled, and the selected ZIM file will not open.
This commit is contained in:
parent
952f039345
commit
daeec25ffd
@ -132,6 +132,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
|
|||||||
private var fragmentDestinationLibraryBinding: FragmentDestinationLibraryBinding? = null
|
private var fragmentDestinationLibraryBinding: FragmentDestinationLibraryBinding? = null
|
||||||
private var permissionDeniedLayoutShowing = false
|
private var permissionDeniedLayoutShowing = false
|
||||||
private var fileSelectListState: FileSelectListState? = null
|
private var fileSelectListState: FileSelectListState? = null
|
||||||
|
private var zimFileUri: Uri? = null
|
||||||
|
|
||||||
private val zimManageViewModel by lazy {
|
private val zimManageViewModel by lazy {
|
||||||
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
||||||
@ -371,7 +372,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
|
|||||||
fragmentDestinationLibraryBinding?.selectFile?.setOnClickListener {
|
fragmentDestinationLibraryBinding?.selectFile?.setOnClickListener {
|
||||||
if (!requireActivity().isManageExternalStoragePermissionGranted(sharedPreferenceUtil)) {
|
if (!requireActivity().isManageExternalStoragePermissionGranted(sharedPreferenceUtil)) {
|
||||||
showManageExternalStoragePermissionDialog()
|
showManageExternalStoragePermissionDialog()
|
||||||
} else {
|
} else if (requestExternalStorageWritePermission()) {
|
||||||
showFileChooser()
|
showFileChooser()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -435,7 +436,10 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
|
|||||||
parentFragmentManager
|
parentFragmentManager
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
getZimFileFromUri(uri)?.let(::navigateToReaderFragment)
|
zimFileUri = uri
|
||||||
|
if (requestExternalStorageWritePermission()) {
|
||||||
|
getZimFileFromUri(uri)?.let(::navigateToReaderFragment)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -510,6 +514,9 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
|
|||||||
storagePermissionLauncher = null
|
storagePermissionLauncher = null
|
||||||
copyMoveFileHandler?.dispose()
|
copyMoveFileHandler?.dispose()
|
||||||
copyMoveFileHandler = null
|
copyMoveFileHandler = null
|
||||||
|
readStoragePermissionLauncher?.unregister()
|
||||||
|
readStoragePermissionLauncher = null
|
||||||
|
zimFileUri = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sideEffects() = zimManageViewModel.sideEffects
|
private fun sideEffects() = zimManageViewModel.sideEffects
|
||||||
@ -710,4 +717,59 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
|
|||||||
private fun showWarningDialogForSplittedZimFile() {
|
private fun showWarningDialogForSplittedZimFile() {
|
||||||
dialogShower.show(KiwixDialog.ShowWarningAboutSplittedZimFile)
|
dialogShower.show(KiwixDialog.ShowWarningAboutSplittedZimFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var readStoragePermissionLauncher: ActivityResultLauncher<Array<String>>? =
|
||||||
|
registerForActivityResult(
|
||||||
|
ActivityResultContracts.RequestMultiplePermissions()
|
||||||
|
) { permissionResult ->
|
||||||
|
val isGranted =
|
||||||
|
permissionResult.entries.all(
|
||||||
|
Map.Entry<String, @kotlin.jvm.JvmSuppressWildcards Boolean>::value
|
||||||
|
)
|
||||||
|
if (isGranted) {
|
||||||
|
zimFileUri?.let {
|
||||||
|
// open the selected ZIM file in reader.
|
||||||
|
lifecycleScope.launch {
|
||||||
|
getZimFileFromUri(it)?.let(::navigateToReaderFragment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
|
/* shouldShowRequestPermissionRationale() returns false when:
|
||||||
|
* 1) User has previously checked on "Don't ask me again", and/or
|
||||||
|
* 2) Permission has been disabled on device
|
||||||
|
*/
|
||||||
|
requireActivity().toast(string.request_storage, Toast.LENGTH_LONG)
|
||||||
|
} else {
|
||||||
|
dialogShower.show(
|
||||||
|
KiwixDialog.ReadPermissionRequired,
|
||||||
|
requireActivity()::navigateToAppSettings
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("NestedBlockDepth")
|
||||||
|
private fun requestExternalStorageWritePermission(): Boolean {
|
||||||
|
var isPermissionGranted = false
|
||||||
|
if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove() &&
|
||||||
|
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
|
||||||
|
) {
|
||||||
|
if (requireActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
== PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
isPermissionGranted = true
|
||||||
|
} else {
|
||||||
|
readStoragePermissionLauncher?.launch(
|
||||||
|
arrayOf(
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
isPermissionGranted = true
|
||||||
|
}
|
||||||
|
return isPermissionGranted
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user