From 297c20f5d34a49816fc2e954c156d0d3c49f19c9 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Sat, 15 Oct 2022 18:09:09 +0700 Subject: Fix duplicate download folders by persisting read permission (#1164) * Persist read permission ACTION_OPEN_DOCUMENT_TREE grants both read and write permissions, but they are granted only until device reboot unless app persists them. Once read permission is lost, app cannot check if folders exist in DownloadMediaService. But it can still create new folders because it has write permission. This results in duplicate folders. * Remove unnecessary FLAG_GRANT_WRITE_URI_PERMISSION This flag is ignored when used with ACTION_OPEN_DOCUMENT_TREE --- .../settings/AdvancedPreferenceFragment.java | 1 - .../settings/DownloadLocationPreferenceFragment.java | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java index 359a95ff..9f9534fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java @@ -276,7 +276,6 @@ public class AdvancedPreferenceFragment extends CustomFontPreferenceFragmentComp if (backupSettingsPreference != null) { backupSettingsPreference.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); - intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, SELECT_BACKUP_SETTINGS_DIRECTORY_REQUEST_CODE); return true; }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java index 8c717c5c..d036d8a6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/DownloadLocationPreferenceFragment.java @@ -50,7 +50,6 @@ public class DownloadLocationPreferenceFragment extends CustomFontPreferenceFrag nsfwDownloadLocationPreference.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); - intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, NSFW_DOWNLOAD_LOCATION_REQUEST_CODE); return true; }); @@ -63,7 +62,6 @@ public class DownloadLocationPreferenceFragment extends CustomFontPreferenceFrag imageDownloadLocationPreference.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); - intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, IMAGE_DOWNLOAD_LOCATION_REQUEST_CODE); return true; }); @@ -77,7 +75,6 @@ public class DownloadLocationPreferenceFragment extends CustomFontPreferenceFrag gifDownloadLocationPreference.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); - intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, GIF_DOWNLOAD_LOCATION_REQUEST_CODE); return true; }); @@ -91,7 +88,6 @@ public class DownloadLocationPreferenceFragment extends CustomFontPreferenceFrag videoDownloadLocationPreference.setOnPreferenceClickListener(preference -> { Intent intent = new Intent(ACTION_OPEN_DOCUMENT_TREE); - intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, VIDEO_DOWNLOAD_LOCATION_REQUEST_CODE); return true; }); @@ -103,25 +99,29 @@ public class DownloadLocationPreferenceFragment extends CustomFontPreferenceFrag super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { if (requestCode == IMAGE_DOWNLOAD_LOCATION_REQUEST_CODE) { - activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + activity.getContentResolver().takePersistableUriPermission(data.getData(), + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); sharedPreferences.edit().putString(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION, data.getDataString()).apply(); if (imageDownloadLocationPreference != null) { imageDownloadLocationPreference.setSummary(data.getDataString()); } } else if (requestCode == GIF_DOWNLOAD_LOCATION_REQUEST_CODE) { - activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + activity.getContentResolver().takePersistableUriPermission(data.getData(), + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); sharedPreferences.edit().putString(SharedPreferencesUtils.GIF_DOWNLOAD_LOCATION, data.getDataString()).apply(); if (gifDownloadLocationPreference != null) { gifDownloadLocationPreference.setSummary(data.getDataString()); } } else if (requestCode == VIDEO_DOWNLOAD_LOCATION_REQUEST_CODE) { - activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + activity.getContentResolver().takePersistableUriPermission(data.getData(), + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); sharedPreferences.edit().putString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, data.getDataString()).apply(); if (videoDownloadLocationPreference != null) { videoDownloadLocationPreference.setSummary(data.getDataString()); } } else if (requestCode == NSFW_DOWNLOAD_LOCATION_REQUEST_CODE) { - activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + activity.getContentResolver().takePersistableUriPermission(data.getData(), + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); sharedPreferences.edit().putString(SharedPreferencesUtils.NSFW_DOWNLOAD_LOCATION, data.getDataString()).apply(); if (nsfwDownloadLocationPreference != null) { nsfwDownloadLocationPreference.setSummary(data.getDataString()); -- cgit v1.2.3