Reimplement storing backup import/export path

#6319 and #6402 were reverted before adding SAF changes, and have been readded at the end of SAF changes
This commit is contained in:
Stypox 2021-06-06 22:15:32 +02:00
parent 5ffc667bea
commit 2a99e0e435
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
7 changed files with 155 additions and 55 deletions

View file

@ -448,7 +448,7 @@ public class StoredFileHelper implements Serializable {
return !str1.equals(str2);
}
public static Intent getPicker(final Context ctx) {
public static Intent getPicker(@NonNull final Context ctx) {
if (NewPipeSettings.useStorageAccessFramework(ctx)) {
return new Intent(Intent.ACTION_OPEN_DOCUMENT)
.putExtra("android.content.extra.SHOW_ADVANCED", true)
@ -466,10 +466,14 @@ public class StoredFileHelper implements Serializable {
}
}
public static Intent getPicker(@NonNull final Context ctx, @Nullable final Uri initialPath) {
return applyInitialPathToPickerIntent(ctx, getPicker(ctx), initialPath, null);
}
public static Intent getNewPicker(@NonNull final Context ctx,
@Nullable final String startPath,
@Nullable final String filename,
@NonNull final String mimeType) {
@NonNull final String mimeType,
@Nullable final Uri initialPath) {
final Intent i;
if (NewPipeSettings.useStorageAccessFramework(ctx)) {
i = new Intent(Intent.ACTION_CREATE_DOCUMENT)
@ -478,10 +482,6 @@ public class StoredFileHelper implements Serializable {
.addCategory(Intent.CATEGORY_OPENABLE)
.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| StoredDirectoryHelper.PERMISSION_FLAGS);
if (startPath != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
i.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Uri.parse(startPath));
}
if (filename != null) {
i.putExtra(Intent.EXTRA_TITLE, filename);
}
@ -492,21 +492,63 @@ public class StoredFileHelper implements Serializable {
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_EXISTING_FILE, true)
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
FilePickerActivityHelper.MODE_NEW_FILE);
if (startPath != null || filename != null) {
File fullStartPath;
if (startPath == null) {
fullStartPath = Environment.getExternalStorageDirectory();
} else {
fullStartPath = new File(startPath);
}
if (filename != null) {
fullStartPath = new File(fullStartPath, filename);
}
i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH,
fullStartPath.getAbsolutePath());
}
}
return i;
return applyInitialPathToPickerIntent(ctx, i, initialPath, filename);
}
private static Intent applyInitialPathToPickerIntent(@NonNull final Context ctx,
@NonNull final Intent intent,
@Nullable final Uri initialPath,
@Nullable final String filename) {
if (NewPipeSettings.useStorageAccessFramework(ctx)) {
if (initialPath == null) {
return intent; // nothing to do, no initial path provided
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialPath);
} else {
return intent; // can't set initial path on API < 26
}
} else {
if (initialPath == null && filename == null) {
return intent; // nothing to do, no initial path and no file name provided
}
File file;
if (initialPath == null) {
// The only way to set the previewed filename in non-SAF FilePicker is to set a
// starting path ending with that filename. So when the initialPath is null but
// filename isn't just default to the external storage directory.
file = Environment.getExternalStorageDirectory();
} else {
try {
file = Utils.getFileForUri(initialPath);
} catch (final Throwable ignored) {
// getFileForUri() can't decode paths to 'storage', fallback to this
file = new File(initialPath.toString());
}
}
// remove any filename at the end of the path (get the parent directory in that case)
if (!file.exists() || !file.isDirectory()) {
file = file.getParentFile();
if (file == null || !file.exists()) {
// default to the external storage directory in case of an invalid path
file = Environment.getExternalStorageDirectory();
}
// else: file is surely a directory
}
if (filename != null) {
// append a filename so that the non-SAF FilePicker shows it as the preview
file = new File(file, filename);
}
return intent
.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, file.getAbsolutePath());
}
}
}