mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 04:33:54 -04:00
Remove Manage external storage permission and check ACTION_OPEN_DOCUMENT_TREE
This commit is contained in:
parent
9bb1788e8c
commit
bec0d1e2a5
@ -38,7 +38,6 @@ import androidx.appcompat.view.ActionMode
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
@ -58,13 +57,11 @@ import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.navigate
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
|
||||
import org.kiwix.kiwixmobile.core.extensions.toast
|
||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||
import org.kiwix.kiwixmobile.core.navigateToSettings
|
||||
import org.kiwix.kiwixmobile.core.utils.FILE_SELECT_CODE
|
||||
import org.kiwix.kiwixmobile.core.utils.LanguageUtils
|
||||
import org.kiwix.kiwixmobile.core.utils.REQUEST_STORAGE_PERMISSION
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
|
||||
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate
|
||||
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter
|
||||
@ -185,6 +182,11 @@ class LocalLibraryFragment : BaseFragment() {
|
||||
when (requestCode) {
|
||||
FILE_SELECT_CODE -> {
|
||||
data?.data?.let { uri ->
|
||||
val contentResolver = requireActivity().contentResolver
|
||||
|
||||
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
||||
getZimFileFromUri(uri)?.let(::navigateToReaderFragment)
|
||||
}
|
||||
}
|
||||
@ -302,18 +304,19 @@ class LocalLibraryFragment : BaseFragment() {
|
||||
// We already have permission!!
|
||||
requestFileSystemCheck()
|
||||
} else {
|
||||
if (sharedPreferenceUtil.manageExternalFilesPermissionDialog) {
|
||||
// We should only ask for first time, If the users wants to revoke settings
|
||||
// then they can directly toggle this feature from settings screen
|
||||
sharedPreferenceUtil.manageExternalFilesPermissionDialog = false
|
||||
// Show Dialog and Go to settings to give permission
|
||||
dialogShower.show(
|
||||
KiwixDialog.ManageExternalFilesPermissionDialog,
|
||||
{
|
||||
this.activity?.let(FragmentActivity::navigateToSettings)
|
||||
}
|
||||
)
|
||||
}
|
||||
// This code is for taking MANAGE_EXTERNAL_STORAGE permission
|
||||
// if (sharedPreferenceUtil.manageExternalFilesPermissionDialog) {
|
||||
// // We should only ask for first time, If the users wants to revoke settings
|
||||
// // then they can directly toggle this feature from settings screen
|
||||
// sharedPreferenceUtil.manageExternalFilesPermissionDialog = false
|
||||
// // Show Dialog and Go to settings to give permission
|
||||
// dialogShower.show(
|
||||
// KiwixDialog.ManageExternalFilesPermissionDialog,
|
||||
// {
|
||||
// this.activity?.let(FragmentActivity::navigateToSettings)
|
||||
// }
|
||||
// )
|
||||
// }
|
||||
}
|
||||
} else {
|
||||
requestFileSystemCheck()
|
||||
|
@ -99,6 +99,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
|
||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||
@Inject lateinit var bookUtils: BookUtils
|
||||
@Inject lateinit var availableSpaceCalculator: AvailableSpaceCalculator
|
||||
private val isInternalStorageSelected = false
|
||||
private val zimManageViewModel by lazy {
|
||||
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
|
||||
}
|
||||
@ -305,6 +306,12 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
|
||||
@SuppressLint("InflateParams")
|
||||
private fun storeDeviceInPreferences(storageDevice: StorageDevice) {
|
||||
if (storageDevice.isInternal) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
val view = LayoutInflater.from(activity).inflate(R.layout.select_folder_dialog, null)
|
||||
dialogShower.show(SelectFolder { view }, ::selectFolder)
|
||||
} else {
|
||||
setExternalStoragePath(storageDevice)
|
||||
}
|
||||
sharedPreferenceUtil.putPrefStorage(
|
||||
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
|
||||
)
|
||||
@ -347,8 +354,15 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
if (requestCode == REQUEST_SELECT_FOLDER_PERMISSION && resultCode == Activity.RESULT_OK) {
|
||||
data?.let {
|
||||
getPathFromUri(requireActivity(), data)?.let(sharedPreferenceUtil::putPrefStorage)
|
||||
sharedPreferenceUtil.putStoragePosition(EXTERNAL_SELECT_POSITION)
|
||||
getPathFromUri(
|
||||
requireActivity(), data,
|
||||
isInternalStorageSelected
|
||||
)?.let(sharedPreferenceUtil::putPrefStorage)
|
||||
if (isInternalStorageSelected) {
|
||||
sharedPreferenceUtil.putStoragePosition(INTERNAL_SELECT_POSITION)
|
||||
} else {
|
||||
sharedPreferenceUtil.putStoragePosition(EXTERNAL_SELECT_POSITION)
|
||||
}
|
||||
} ?: run {
|
||||
activity.toast(
|
||||
resources
|
||||
|
@ -86,6 +86,7 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
|
||||
protected NightModeConfig nightModeConfig;
|
||||
@Inject
|
||||
protected DialogShower alertDialogShower;
|
||||
private boolean isInternalStorageSelected = false;
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
@ -288,11 +289,22 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
|
||||
);
|
||||
|
||||
if (storageDevice.isInternal()) {
|
||||
sharedPreferenceUtil.putPrefStorage(
|
||||
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.getName()));
|
||||
isInternalStorageSelected = true;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
@SuppressLint("InflateParams") View view =
|
||||
LayoutInflater.from(getActivity()).inflate(R.layout.select_folder_dialog, null);
|
||||
alertDialogShower.show(new KiwixDialog.SelectFolder(() -> view), () -> {
|
||||
selectFolder();
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
} else {
|
||||
sharedPreferenceUtil.putPrefStorage(
|
||||
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.getName()));
|
||||
}
|
||||
findPreference(PREF_STORAGE).setTitle(getString(R.string.internal_storage));
|
||||
sharedPreferenceUtil.putStoragePosition(INTERNAL_SELECT_POSITION);
|
||||
} else {
|
||||
isInternalStorageSelected = false;
|
||||
if (sharedPreferenceUtil.isPlayStoreBuild()) {
|
||||
setExternalStoragePath(storageDevice);
|
||||
} else {
|
||||
@ -333,8 +345,14 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_SELECT_FOLDER_PERMISSION && resultCode == RESULT_OK) {
|
||||
sharedPreferenceUtil.putPrefStorage(
|
||||
FileUtils.getPathFromUri(requireActivity(), data));
|
||||
findPreference(PREF_STORAGE).setTitle(getString(R.string.external_storage));
|
||||
FileUtils.getPathFromUri(requireActivity(), data, isInternalStorageSelected));
|
||||
if (isInternalStorageSelected) {
|
||||
sharedPreferenceUtil.putStoragePosition(INTERNAL_SELECT_POSITION);
|
||||
findPreference(PREF_STORAGE).setTitle(getString(R.string.internal_storage));
|
||||
} else {
|
||||
sharedPreferenceUtil.putStoragePosition(EXTERNAL_SELECT_POSITION);
|
||||
findPreference(PREF_STORAGE).setTitle(getString(R.string.external_storage));
|
||||
}
|
||||
sharedPreferenceUtil.putStoragePosition(EXTERNAL_SELECT_POSITION);
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,11 @@ object FileUtils {
|
||||
.substringBefore(context.getString(R.string.android_directory_seperator))
|
||||
|
||||
@SuppressLint("WrongConstant")
|
||||
@JvmStatic fun getPathFromUri(activity: Activity, data: Intent): String? {
|
||||
@JvmStatic fun getPathFromUri(
|
||||
activity: Activity,
|
||||
data: Intent,
|
||||
isInternalStorageSelected: Boolean
|
||||
): String? {
|
||||
val uri: Uri? = data.data
|
||||
val takeFlags: Int = data.flags and (
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
@ -270,7 +274,8 @@ object FileUtils {
|
||||
val originalPath = file.substring(
|
||||
file.lastIndexOf(":") + 1
|
||||
)
|
||||
val path = "${activity.getExternalFilesDirs("")[1]}"
|
||||
val path =
|
||||
"${activity.getExternalFilesDirs("")[getStoragePosition(isInternalStorageSelected)]}"
|
||||
return@getPathFromUri path.substringBefore(
|
||||
activity.getString(R.string.android_directory_seperator)
|
||||
)
|
||||
@ -292,6 +297,9 @@ object FileUtils {
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getStoragePosition(isInternalStorageSelected: Boolean): Int =
|
||||
if (isInternalStorageSelected) 0 else 1
|
||||
|
||||
/**
|
||||
* Returns the file name from the url or src. In url it gets the file name from the last '/' and
|
||||
* if it contains '.'. If the url is null then it'll get the file name from the last '/'.
|
||||
|
Loading…
x
Reference in New Issue
Block a user