validate uri before writing pref

also made pref summary up-to-date
This commit is contained in:
Austin Huang 2021-06-22 12:12:42 -04:00
parent d031998123
commit 832641603a
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
4 changed files with 24 additions and 16 deletions

View File

@ -111,7 +111,8 @@ class MainActivity : BaseLanguageActivity(), FragmentManager.OnBackStackChangedL
override fun onCreate(savedInstanceState: Bundle?) {
try {
DownloadUtils.init(this)
DownloadUtils.init(this,
Utils.settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI))
} catch (e: ReselectDocumentTreeException) {
super.onCreate(savedInstanceState)
val intent = Intent(this, DirectorySelectActivity::class.java)

View File

@ -34,6 +34,7 @@ import static awais.instagrabber.utils.Utils.settingsHelper;
public class DownloadsPreferencesFragment extends BasePreferencesFragment {
private static final String TAG = DownloadsPreferencesFragment.class.getSimpleName();
private Preference dirPreference;
@Override
void setupPreferenceScreen(final PreferenceScreen screen) {
@ -53,26 +54,25 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
}
private Preference getSaveToCustomFolderPreference(@NonNull final Context context) {
final Preference preference = new Preference(context);
preference.setKey(PreferenceKeys.PREF_BARINSTA_DIR_URI);
preference.setIconSpaceReserved(false);
preference.setTitle(R.string.barinsta_folder);
preference.setSummaryProvider(p -> {
final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI);
if (TextUtils.isEmpty(currentValue)) return "";
dirPreference = new Preference(context);
dirPreference.setIconSpaceReserved(false);
dirPreference.setTitle(R.string.barinsta_folder);
final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI);
if (TextUtils.isEmpty(currentValue)) dirPreference.setSummary("");
else {
String path;
try {
path = URLDecoder.decode(currentValue, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
path = currentValue;
}
return path;
});
preference.setOnPreferenceClickListener(p -> {
dirPreference.setSummary(path);
}
dirPreference.setOnPreferenceClickListener(p -> {
openDirectoryChooser(DownloadUtils.getRootDirUri());
return true;
});
return preference;
return dirPreference;
}
private void openDirectoryChooser(final Uri initialUri) {
@ -93,6 +93,13 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
AppExecutors.INSTANCE.getMainThread().execute(() -> {
try {
Utils.setupSelectedDir(context, data);
String path;
try {
path = URLDecoder.decode(data.getData().toString(), StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
path = data.getData().toString();
}
dirPreference.setSummary(path);
} catch (Exception e) {
// Should not come to this point.
// If it does, we have to show this error to the user so that they can report it.

View File

@ -63,8 +63,8 @@ public final class DownloadUtils {
public static final String WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String[] PERMS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
public static void init(@NonNull final Context context) throws ReselectDocumentTreeException {
final String barinstaDirUri = Utils.settingsHelper.getString(PREF_BARINSTA_DIR_URI);
public static void init(@NonNull final Context context,
@Nullable final String barinstaDirUri) throws ReselectDocumentTreeException {
if (TextUtils.isEmpty(barinstaDirUri)) {
throw new ReselectDocumentTreeException("folder path is null or empty");
}
@ -88,6 +88,7 @@ public final class DownloadUtils {
root = null;
throw new ReselectDocumentTreeException(uri);
}
Utils.settingsHelper.putString(PREF_BARINSTA_DIR_URI, uri.toString());
}
public static void destroy() {

View File

@ -592,9 +592,8 @@ public final class Utils {
if (dirUri == null) return;
final int takeFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.getContentResolver().takePersistableUriPermission(dirUri, takeFlags);
settingsHelper.putString(PREF_BARINSTA_DIR_URI, dirUri.toString());
// re-init DownloadUtils
DownloadUtils.init(context);
DownloadUtils.init(context, dirUri.toString());
}
@NonNull