1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-06-21 04:39:59 +00:00

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;
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<Intent> 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;
}

View File

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

View File

@ -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) {

View File

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

View File

@ -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<Intent> 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) {

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;
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