Forget the download save path if the storage API is changed

This commit is contained in:
kapodamy 2019-04-15 21:31:33 -03:00
parent 1089de6321
commit 4b3eb2ece5
2 changed files with 51 additions and 46 deletions

View file

@ -2,6 +2,7 @@ package org.schabi.newpipe.settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
@ -22,6 +23,7 @@ import org.schabi.newpipe.util.FilePickerActivityHelper;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
@ -30,7 +32,7 @@ import us.shandian.giga.io.StoredDirectoryHelper;
public class DownloadSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_DOWNLOAD_VIDEO_PATH = 0x1235;
private static final int REQUEST_DOWNLOAD_AUDIO_PATH = 0x1236;
public static final boolean IGNORE_RELEASE_OLD_PATH = true;
public static final boolean IGNORE_RELEASE_ON_OLD_PATH = true;
private String DOWNLOAD_PATH_VIDEO_PREFERENCE;
private String DOWNLOAD_PATH_AUDIO_PREFERENCE;
@ -68,45 +70,21 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
if (javaIO == lastAPIJavaIO) return true;
lastAPIJavaIO = javaIO;
boolean res;
if (javaIO && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// forget save paths (if necessary)
res = forgetPath(DOWNLOAD_PATH_VIDEO_PREFERENCE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
boolean res = forgetPath(DOWNLOAD_PATH_VIDEO_PREFERENCE);
res |= forgetPath(DOWNLOAD_PATH_AUDIO_PREFERENCE);
} else {
res = hasInvalidPath(DOWNLOAD_PATH_VIDEO_PREFERENCE) || hasInvalidPath(DOWNLOAD_PATH_AUDIO_PREFERENCE);
}
if (res) {
Toast.makeText(ctx, R.string.download_pick_path, Toast.LENGTH_LONG).show();
Toast.makeText(ctx, R.string.download_pick_path, Toast.LENGTH_SHORT).show();
updatePreferencesSummary();
}
}
updatePathPickers(javaIO);
return true;
});
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean forgetPath(String prefKey) {
String path = defaultPreferences.getString(prefKey, "");
if (path == null || path.isEmpty()) return true;
if (path.startsWith("file://")) return false;
// forget SAF path (file:// is compatible with the SAF wrapper)
forgetSAFTree(getContext(), prefKey);
defaultPreferences.edit().putString(prefKey, "").apply();
return true;
}
private boolean hasInvalidPath(String prefKey) {
String value = defaultPreferences.getString(prefKey, null);
return value == null || value.isEmpty();
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.download_settings);
@ -137,6 +115,15 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
return;
}
if (rawUri.charAt(0) == File.separatorChar) {
target.setSummary(rawUri);
return;
}
if (rawUri.startsWith(ContentResolver.SCHEME_FILE)) {
target.setSummary(new File(URI.create(rawUri)).getPath());
return;
}
try {
rawUri = URLDecoder.decode(rawUri, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
@ -146,6 +133,24 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
target.setSummary(rawUri);
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private boolean forgetPath(String prefKey) {
String path = defaultPreferences.getString(prefKey, "");
if (path == null || path.isEmpty()) return true;
// forget SAF path if necessary
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
forgetSAFTree(getContext(), path);
defaultPreferences.edit().putString(prefKey, "").apply();
return true;
}
private boolean isFileUri(String path) {
return path.charAt(0) == File.separatorChar || path.startsWith(ContentResolver.SCHEME_FILE);
}
private void updatePathPickers(boolean useJavaIO) {
boolean enabled = useJavaIO || Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
prefPathVideo.setEnabled(enabled);
@ -159,15 +164,13 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
}
// FIXME: after releasing the old path, all downloads created on the folder becomes inaccessible
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void forgetSAFTree(Context ctx, String prefKey) {
if (IGNORE_RELEASE_OLD_PATH) {
private void forgetSAFTree(Context ctx, String oldPath) {
if (IGNORE_RELEASE_ON_OLD_PATH) {
return;
}
String oldPath = defaultPreferences.getString(prefKey, "");
if (oldPath == null || oldPath.isEmpty() || isFileUri(oldPath)) return;
if (oldPath != null && !oldPath.isEmpty() && oldPath.charAt(0) != File.separatorChar && !oldPath.startsWith("file://")) {
try {
Uri uri = Uri.parse(oldPath);
@ -179,7 +182,6 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
Log.e(TAG, "Error revoking old path permissions on " + oldPath, err);
}
}
}
private void showMessageDialog(@StringRes int title, @StringRes int message) {
AlertDialog.Builder msg = new AlertDialog.Builder(ctx);
@ -258,7 +260,7 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
final Context ctx = getContext();
if (ctx == null) throw new NullPointerException("getContext()");
forgetSAFTree(ctx, key);
forgetSAFTree(ctx, defaultPreferences.getString(key, ""));
try {
ctx.grantUriPermission(ctx.getPackageName(), uri, StoredDirectoryHelper.PERMISSION_FLAGS);

View file

@ -566,7 +566,10 @@ public class DownloadManagerService extends Service {
}
if (path == null || path.isEmpty()) {
return useJavaIO ? new StoredDirectoryHelper(new File(defaultPath).toURI(), tag) : null;
if (useJavaIO)
return new StoredDirectoryHelper(new File(defaultPath).toURI(), tag);
else
return null;
}
if (path.charAt(0) == File.separatorChar) {