1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

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?) { override fun onCreate(savedInstanceState: Bundle?) {
try { try {
DownloadUtils.init(this) DownloadUtils.init(this,
Utils.settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI))
} catch (e: ReselectDocumentTreeException) { } catch (e: ReselectDocumentTreeException) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val intent = Intent(this, DirectorySelectActivity::class.java) 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 { public class DownloadsPreferencesFragment extends BasePreferencesFragment {
private static final String TAG = DownloadsPreferencesFragment.class.getSimpleName(); private static final String TAG = DownloadsPreferencesFragment.class.getSimpleName();
private Preference dirPreference;
@Override @Override
void setupPreferenceScreen(final PreferenceScreen screen) { void setupPreferenceScreen(final PreferenceScreen screen) {
@ -53,26 +54,25 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
} }
private Preference getSaveToCustomFolderPreference(@NonNull final Context context) { private Preference getSaveToCustomFolderPreference(@NonNull final Context context) {
final Preference preference = new Preference(context); dirPreference = new Preference(context);
preference.setKey(PreferenceKeys.PREF_BARINSTA_DIR_URI); dirPreference.setIconSpaceReserved(false);
preference.setIconSpaceReserved(false); dirPreference.setTitle(R.string.barinsta_folder);
preference.setTitle(R.string.barinsta_folder); final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI);
preference.setSummaryProvider(p -> { if (TextUtils.isEmpty(currentValue)) dirPreference.setSummary("");
final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI); else {
if (TextUtils.isEmpty(currentValue)) return "";
String path; String path;
try { try {
path = URLDecoder.decode(currentValue, StandardCharsets.UTF_8.toString()); path = URLDecoder.decode(currentValue, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
path = currentValue; path = currentValue;
} }
return path; dirPreference.setSummary(path);
}); }
preference.setOnPreferenceClickListener(p -> { dirPreference.setOnPreferenceClickListener(p -> {
openDirectoryChooser(DownloadUtils.getRootDirUri()); openDirectoryChooser(DownloadUtils.getRootDirUri());
return true; return true;
}); });
return preference; return dirPreference;
} }
private void openDirectoryChooser(final Uri initialUri) { private void openDirectoryChooser(final Uri initialUri) {
@ -93,6 +93,13 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
AppExecutors.INSTANCE.getMainThread().execute(() -> { AppExecutors.INSTANCE.getMainThread().execute(() -> {
try { try {
Utils.setupSelectedDir(context, data); 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) { } catch (Exception e) {
// Should not come to this point. // Should not come to this point.
// If it does, we have to show this error to the user so that they can report it. // 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 WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String[] PERMS = new String[]{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 { public static void init(@NonNull final Context context,
final String barinstaDirUri = Utils.settingsHelper.getString(PREF_BARINSTA_DIR_URI); @Nullable final String barinstaDirUri) throws ReselectDocumentTreeException {
if (TextUtils.isEmpty(barinstaDirUri)) { if (TextUtils.isEmpty(barinstaDirUri)) {
throw new ReselectDocumentTreeException("folder path is null or empty"); throw new ReselectDocumentTreeException("folder path is null or empty");
} }
@ -88,6 +88,7 @@ public final class DownloadUtils {
root = null; root = null;
throw new ReselectDocumentTreeException(uri); throw new ReselectDocumentTreeException(uri);
} }
Utils.settingsHelper.putString(PREF_BARINSTA_DIR_URI, uri.toString());
} }
public static void destroy() { public static void destroy() {

View File

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