Browse Source

validate uri before writing pref

also made pref summary up-to-date
renovate/org.robolectric-robolectric-4.x
Austin Huang 4 years ago
parent
commit
832641603a
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 3
      app/src/main/java/awais/instagrabber/activities/MainActivity.kt
  2. 27
      app/src/main/java/awais/instagrabber/fragments/settings/DownloadsPreferencesFragment.java
  3. 5
      app/src/main/java/awais/instagrabber/utils/DownloadUtils.java
  4. 3
      app/src/main/java/awais/instagrabber/utils/Utils.java

3
app/src/main/java/awais/instagrabber/activities/MainActivity.kt

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

27
app/src/main/java/awais/instagrabber/fragments/settings/DownloadsPreferencesFragment.java

@ -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);
preference.setKey(PreferenceKeys.PREF_BARINSTA_DIR_URI);
preference.setIconSpaceReserved(false);
preference.setTitle(R.string.barinsta_folder);
preference.setSummaryProvider(p -> {
dirPreference = new Preference(context);
dirPreference.setIconSpaceReserved(false);
dirPreference.setTitle(R.string.barinsta_folder);
final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI); final String currentValue = settingsHelper.getString(PreferenceKeys.PREF_BARINSTA_DIR_URI);
if (TextUtils.isEmpty(currentValue)) return "";
if (TextUtils.isEmpty(currentValue)) dirPreference.setSummary("");
else {
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;
});
preference.setOnPreferenceClickListener(p -> {
dirPreference.setSummary(path);
}
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.

5
app/src/main/java/awais/instagrabber/utils/DownloadUtils.java

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

3
app/src/main/java/awais/instagrabber/utils/Utils.java

@ -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

Loading…
Cancel
Save