Refactoring + deduplicated code
This commit is contained in:
parent
273e0e7f06
commit
625ffba8b7
8 changed files with 134 additions and 112 deletions
|
|
@ -1,31 +0,0 @@
|
|||
package org.schabi.newpipe.streams.io;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
|
||||
/**
|
||||
* Helper for when no file-manager/activity was found.
|
||||
*/
|
||||
public final class NoFileManagerHelper {
|
||||
private NoFileManagerHelper() {
|
||||
// No impl
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows an alert dialog when no file-manager is found.
|
||||
* @param context Context
|
||||
*/
|
||||
public static void showActivityNotFoundAlert(final Context context) {
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.no_app_to_open_intent)
|
||||
.setMessage(
|
||||
context.getString(
|
||||
R.string.no_appropriate_file_manager_message,
|
||||
context.getString(R.string.downloads_storage_use_saf_title)))
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package org.schabi.newpipe.streams.io;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
|
||||
/**
|
||||
* Helper for when no file-manager/activity was found.
|
||||
*/
|
||||
public final class NoFileManagerSafeGuard {
|
||||
private NoFileManagerSafeGuard() {
|
||||
// No impl
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows an alert dialog when no file-manager is found.
|
||||
* @param context Context
|
||||
*/
|
||||
private static void showActivityNotFoundAlert(@NonNull final Context context) {
|
||||
if (context == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Unable to open no file manager alert dialog: Context is null");
|
||||
}
|
||||
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.no_app_to_open_intent)
|
||||
.setMessage(
|
||||
context.getString(
|
||||
R.string.no_appropriate_file_manager_message,
|
||||
context.getString(R.string.downloads_storage_use_saf_title)))
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches the file manager safely.
|
||||
*
|
||||
* If no file manager is found (which is normally only the case when the user uninstalled
|
||||
* the default file manager or the OS lacks one) an alert dialog shows up, asking the user
|
||||
* to fix the situation.
|
||||
*
|
||||
* @param activityResultLauncher see {@link ActivityResultLauncher#launch(Object)}
|
||||
* @param input see {@link ActivityResultLauncher#launch(Object)}
|
||||
* @param tag Tag used for logging
|
||||
* @param context Context
|
||||
* @param <I> see {@link ActivityResultLauncher#launch(Object)}
|
||||
*/
|
||||
public static <I> void launchSafe(
|
||||
final ActivityResultLauncher<I> activityResultLauncher,
|
||||
final I input,
|
||||
@NonNull final String tag,
|
||||
@NonNull final Context context
|
||||
) {
|
||||
try {
|
||||
activityResultLauncher.launch(input);
|
||||
} catch (final ActivityNotFoundException aex) {
|
||||
Log.w(tag, "Unable to launch file/directory picker", aex);
|
||||
NoFileManagerSafeGuard.showActivityNotFoundAlert(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue