Refactoring + deduplicated code

This commit is contained in:
litetex 2021-11-28 14:07:45 +01:00
parent 68e7fcf8ee
commit b2323859e5
8 changed files with 134 additions and 112 deletions

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.download; package org.schabi.newpipe.download;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; 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.SubtitlesStream;
import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.settings.NewPipeSettings; 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.StoredDirectoryHelper;
import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilePickerActivityHelper;
@ -689,12 +688,12 @@ public class DownloadDialog extends DialogFragment
} }
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) { private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
try { NoFileManagerSafeGuard.launchSafe(
launcher.launch(StoredDirectoryHelper.getPicker(context)); launcher,
} catch (final ActivityNotFoundException aex) { StoredDirectoryHelper.getPicker(context),
Log.w(TAG, "Unable to launch directory picker", aex); TAG,
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); context
} );
} }
private void prepareSelectedDownload() { private void prepareSelectedDownload() {
@ -773,13 +772,12 @@ public class DownloadDialog extends DialogFragment
initialPath = Uri.parse(initialSavePath.getAbsolutePath()); initialPath = Uri.parse(initialSavePath.getAbsolutePath());
} }
try { NoFileManagerSafeGuard.launchSafe(
requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context, requestDownloadSaveAsLauncher,
filenameTmp, mimeTmp, initialPath)); StoredFileHelper.getNewPicker(context, filenameTmp, mimeTmp, initialPath),
} catch (final ActivityNotFoundException aex) { TAG,
Log.w(TAG, "Unable to launch file picker", aex); context
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); );
}
return; return;
} }

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.local.subscription package org.schabi.newpipe.local.subscription
import android.app.Activity import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
@ -9,7 +8,6 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater 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_MODE
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE 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.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.streams.io.StoredFileHelper
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.OnClickGesture import org.schabi.newpipe.util.OnClickGesture
@ -182,26 +180,24 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
} }
private fun onImportPreviousSelected() { private fun onImportPreviousSelected() {
try { NoFileManagerSafeGuard.launchSafe(
requestImportLauncher.launch(StoredFileHelper.getPicker(activity, JSON_MIME_TYPE)) requestImportLauncher,
} catch (aex: ActivityNotFoundException) { StoredFileHelper.getPicker(activity, JSON_MIME_TYPE),
Log.w(TAG, "Unable to launch file picker", aex) TAG,
NoFileManagerHelper.showActivityNotFoundAlert(context) requireContext()
} )
} }
private fun onExportSelected() { private fun onExportSelected() {
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date()) val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
val exportName = "newpipe_subscriptions_$date.json" val exportName = "newpipe_subscriptions_$date.json"
try { NoFileManagerSafeGuard.launchSafe(
requestExportLauncher.launch( requestExportLauncher,
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null) StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null),
) TAG,
} catch (aex: ActivityNotFoundException) { requireContext()
Log.w(TAG, "Unable to launch file picker", aex) )
NoFileManagerHelper.showActivityNotFoundAlert(context)
}
} }
private fun openReorderDialog() { private fun openReorderDialog() {

View File

@ -1,12 +1,10 @@
package org.schabi.newpipe.local.subscription; package org.schabi.newpipe.local.subscription;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService; 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.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.ServiceHelper;
@ -178,14 +176,14 @@ public class SubscriptionsImportFragment extends BaseFragment {
} }
public void onImportFile() { public void onImportFile() {
try { NoFileManagerSafeGuard.launchSafe(
// leave */* mime type to support all services requestImportFileLauncher,
// with different mime types and file extensions // leave */* mime type to support all services
requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity, "*/*")); // with different mime types and file extensions
} catch (final ActivityNotFoundException aex) { StoredFileHelper.getPicker(activity, "*/*"),
Log.w(TAG, "Unable to launch file picker", aex); TAG,
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); getContext()
} );
} }
private void requestImportFileResult(final ActivityResult result) { private void requestImportFileResult(final ActivityResult result) {

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.settings; package org.schabi.newpipe.settings;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; 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.NewPipe;
import org.schabi.newpipe.extractor.localization.ContentCountry; import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization; 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.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PicassoHelper; import org.schabi.newpipe.util.PicassoHelper;
@ -75,29 +74,28 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
final Preference importDataPreference = requirePreference(R.string.import_data); final Preference importDataPreference = requirePreference(R.string.import_data);
importDataPreference.setOnPreferenceClickListener((Preference p) -> { importDataPreference.setOnPreferenceClickListener((Preference p) -> {
try { NoFileManagerSafeGuard.launchSafe(
requestImportPathLauncher.launch( requestImportPathLauncher,
StoredFileHelper.getPicker(requireContext(), StoredFileHelper.getPicker(requireContext(),
ZIP_MIME_TYPE, getImportExportDataUri())); ZIP_MIME_TYPE, getImportExportDataUri()),
} catch (final ActivityNotFoundException aex) { TAG,
Log.w(TAG, "Unable to launch file picker", aex); getContext()
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); );
}
return true; return true;
}); });
final Preference exportDataPreference = requirePreference(R.string.export_data); final Preference exportDataPreference = requirePreference(R.string.export_data);
exportDataPreference.setOnPreferenceClickListener((final Preference p) -> { 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; return true;
}); });

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.settings; package org.schabi.newpipe.settings;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -22,7 +21,7 @@ import androidx.preference.SwitchPreferenceCompat;
import com.nononsenseapps.filepicker.Utils; import com.nononsenseapps.filepicker.Utils;
import org.schabi.newpipe.R; 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.streams.io.StoredDirectoryHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilePickerActivityHelper;
@ -216,12 +215,12 @@ public class DownloadSettingsFragment extends BasePreferenceFragment {
} }
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) { private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
try { NoFileManagerSafeGuard.launchSafe(
launcher.launch(StoredDirectoryHelper.getPicker(ctx)); launcher,
} catch (final ActivityNotFoundException aex) { StoredDirectoryHelper.getPicker(ctx),
Log.w(TAG, "Unable to launch directory picker", aex); TAG,
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); ctx
} );
} }
private void requestDownloadVideoPathResult(final ActivityResult result) { private void requestDownloadVideoPathResult(final ActivityResult result) {

View File

@ -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();
}
}

View File

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

View File

@ -1,7 +1,6 @@
package us.shandian.giga.ui.fragment; package us.shandian.giga.ui.fragment;
import android.app.Activity; import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -11,7 +10,6 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -34,7 +32,7 @@ import com.nononsenseapps.filepicker.Utils;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.NewPipeSettings; 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.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.FilePickerActivityHelper;
@ -261,14 +259,13 @@ public class MissionsFragment extends Fragment {
initialPath = Uri.parse(initialSavePath.getAbsolutePath()); initialPath = Uri.parse(initialSavePath.getAbsolutePath());
} }
try { NoFileManagerSafeGuard.launchSafe(
requestDownloadSaveAsLauncher.launch( requestDownloadSaveAsLauncher,
StoredFileHelper.getNewPicker(mContext, mission.storage.getName(), StoredFileHelper.getNewPicker(mContext, mission.storage.getName(),
mission.storage.getType(), initialPath)); mission.storage.getType(), initialPath),
} catch (final ActivityNotFoundException aex) { TAG,
Log.w(TAG, "Unable to launch file picker", aex); mContext
NoFileManagerHelper.showActivityNotFoundAlert(getContext()); );
}
} }
@Override @Override