From b2323859e5c243856a989fd34a9bb2ed4c4adf27 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Sun, 28 Nov 2021 14:07:45 +0100 Subject: [PATCH] Refactoring + deduplicated code --- .../newpipe/download/DownloadDialog.java | 28 ++++---- .../subscription/SubscriptionFragment.kt | 30 ++++----- .../SubscriptionsImportFragment.java | 20 +++--- .../settings/ContentSettingsFragment.java | 36 +++++----- .../settings/DownloadSettingsFragment.java | 15 ++--- .../streams/io/NoFileManagerHelper.java | 31 --------- .../streams/io/NoFileManagerSafeGuard.java | 67 +++++++++++++++++++ .../giga/ui/fragment/MissionsFragment.java | 19 +++--- 8 files changed, 134 insertions(+), 112 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerHelper.java create mode 100644 app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerSafeGuard.java diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 0386b890d..439efda20 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.download; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -54,7 +53,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.SubtitlesStream; import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.settings.NewPipeSettings; -import org.schabi.newpipe.streams.io.NoFileManagerHelper; +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredDirectoryHelper; import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.util.FilePickerActivityHelper; @@ -689,12 +688,12 @@ public class DownloadDialog extends DialogFragment } private void launchDirectoryPicker(final ActivityResultLauncher launcher) { - try { - launcher.launch(StoredDirectoryHelper.getPicker(context)); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch directory picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + launcher, + StoredDirectoryHelper.getPicker(context), + TAG, + context + ); } private void prepareSelectedDownload() { @@ -773,13 +772,12 @@ public class DownloadDialog extends DialogFragment initialPath = Uri.parse(initialSavePath.getAbsolutePath()); } - try { - requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context, - filenameTmp, mimeTmp, initialPath)); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch file picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + requestDownloadSaveAsLauncher, + StoredFileHelper.getNewPicker(context, filenameTmp, mimeTmp, initialPath), + TAG, + context + ); return; } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt index 264fb17e7..008228083 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt @@ -1,7 +1,6 @@ package org.schabi.newpipe.local.subscription import android.app.Activity -import android.content.ActivityNotFoundException import android.content.BroadcastReceiver import android.content.Context import android.content.DialogInterface @@ -9,7 +8,6 @@ import android.content.Intent import android.content.IntentFilter import android.os.Bundle import android.os.Parcelable -import android.util.Log import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater @@ -57,7 +55,7 @@ import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_MODE import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.PREVIOUS_EXPORT_MODE -import org.schabi.newpipe.streams.io.NoFileManagerHelper +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard import org.schabi.newpipe.streams.io.StoredFileHelper import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.OnClickGesture @@ -182,26 +180,24 @@ class SubscriptionFragment : BaseStateFragment() { } private fun onImportPreviousSelected() { - try { - requestImportLauncher.launch(StoredFileHelper.getPicker(activity, JSON_MIME_TYPE)) - } catch (aex: ActivityNotFoundException) { - Log.w(TAG, "Unable to launch file picker", aex) - NoFileManagerHelper.showActivityNotFoundAlert(context) - } + NoFileManagerSafeGuard.launchSafe( + requestImportLauncher, + StoredFileHelper.getPicker(activity, JSON_MIME_TYPE), + TAG, + requireContext() + ) } private fun onExportSelected() { val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date()) val exportName = "newpipe_subscriptions_$date.json" - try { - requestExportLauncher.launch( - StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null) - ) - } catch (aex: ActivityNotFoundException) { - Log.w(TAG, "Unable to launch file picker", aex) - NoFileManagerHelper.showActivityNotFoundAlert(context) - } + NoFileManagerSafeGuard.launchSafe( + requestExportLauncher, + StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null), + TAG, + requireContext() + ) } private fun openReorderDialog() { diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java index 112300f1f..4fbcfbec1 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java @@ -1,12 +1,10 @@ package org.schabi.newpipe.local.subscription; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.text.util.Linkify; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -32,7 +30,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; -import org.schabi.newpipe.streams.io.NoFileManagerHelper; +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ServiceHelper; @@ -178,14 +176,14 @@ public class SubscriptionsImportFragment extends BaseFragment { } public void onImportFile() { - try { - // leave */* mime type to support all services - // with different mime types and file extensions - requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity, "*/*")); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch file picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + requestImportFileLauncher, + // leave */* mime type to support all services + // with different mime types and file extensions + StoredFileHelper.getPicker(activity, "*/*"), + TAG, + getContext() + ); } private void requestImportFileResult(final ActivityResult result) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 0a5e50be0..1511a23ae 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.settings; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -26,7 +25,7 @@ import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.Localization; -import org.schabi.newpipe.streams.io.NoFileManagerHelper; +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PicassoHelper; @@ -75,29 +74,28 @@ public class ContentSettingsFragment extends BasePreferenceFragment { final Preference importDataPreference = requirePreference(R.string.import_data); importDataPreference.setOnPreferenceClickListener((Preference p) -> { - try { - requestImportPathLauncher.launch( - StoredFileHelper.getPicker(requireContext(), - ZIP_MIME_TYPE, getImportExportDataUri())); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch file picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + requestImportPathLauncher, + StoredFileHelper.getPicker(requireContext(), + ZIP_MIME_TYPE, getImportExportDataUri()), + TAG, + getContext() + ); + return true; }); final Preference exportDataPreference = requirePreference(R.string.export_data); exportDataPreference.setOnPreferenceClickListener((final Preference p) -> { + NoFileManagerSafeGuard.launchSafe( + requestExportPathLauncher, + StoredFileHelper.getNewPicker(requireContext(), + "NewPipeData-" + exportDateFormat.format(new Date()) + ".zip", + ZIP_MIME_TYPE, getImportExportDataUri()), + TAG, + getContext() + ); - try { - requestExportPathLauncher.launch( - StoredFileHelper.getNewPicker(requireContext(), - "NewPipeData-" + exportDateFormat.format(new Date()) + ".zip", - ZIP_MIME_TYPE, getImportExportDataUri())); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch file picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } return true; }); diff --git a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java index 45006bec4..681aee409 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.settings; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; @@ -22,7 +21,7 @@ import androidx.preference.SwitchPreferenceCompat; import com.nononsenseapps.filepicker.Utils; import org.schabi.newpipe.R; -import org.schabi.newpipe.streams.io.NoFileManagerHelper; +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredDirectoryHelper; import org.schabi.newpipe.util.FilePickerActivityHelper; @@ -216,12 +215,12 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { } private void launchDirectoryPicker(final ActivityResultLauncher launcher) { - try { - launcher.launch(StoredDirectoryHelper.getPicker(ctx)); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch directory picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + launcher, + StoredDirectoryHelper.getPicker(ctx), + TAG, + ctx + ); } private void requestDownloadVideoPathResult(final ActivityResult result) { diff --git a/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerHelper.java b/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerHelper.java deleted file mode 100644 index cd9119c08..000000000 --- a/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerHelper.java +++ /dev/null @@ -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(); - } -} diff --git a/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerSafeGuard.java b/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerSafeGuard.java new file mode 100644 index 000000000..1d14d1669 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerSafeGuard.java @@ -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 see {@link ActivityResultLauncher#launch(Object)} + */ + public static void launchSafe( + final ActivityResultLauncher 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); + } + } +} diff --git a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java index e08cc4dec..dda2d6dee 100644 --- a/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java +++ b/app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java @@ -1,7 +1,6 @@ package us.shandian.giga.ui.fragment; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -11,7 +10,6 @@ import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.IBinder; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -34,7 +32,7 @@ import com.nononsenseapps.filepicker.Utils; import org.schabi.newpipe.R; import org.schabi.newpipe.settings.NewPipeSettings; -import org.schabi.newpipe.streams.io.NoFileManagerHelper; +import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard; import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.util.FilePickerActivityHelper; @@ -261,14 +259,13 @@ public class MissionsFragment extends Fragment { initialPath = Uri.parse(initialSavePath.getAbsolutePath()); } - try { - requestDownloadSaveAsLauncher.launch( - StoredFileHelper.getNewPicker(mContext, mission.storage.getName(), - mission.storage.getType(), initialPath)); - } catch (final ActivityNotFoundException aex) { - Log.w(TAG, "Unable to launch file picker", aex); - NoFileManagerHelper.showActivityNotFoundAlert(getContext()); - } + NoFileManagerSafeGuard.launchSafe( + requestDownloadSaveAsLauncher, + StoredFileHelper.getNewPicker(mContext, mission.storage.getName(), + mission.storage.getType(), initialPath), + TAG, + mContext + ); } @Override