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
This commit is contained in:
Sergei Kozelko 2022-10-15 18:09:09 +07:00 committed by GitHub
parent 48dcf2293c
commit 297c20f5d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View File

@ -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;
});

View File

@ -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());