From 0d51eefbb9385883d2ebd3739fad38e72db26866 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Tue, 23 Nov 2021 20:11:36 +0100 Subject: [PATCH] Refactoring --- .../fragments/detail/VideoDetailFragment.java | 13 +---- .../detail/VideoDetailPlayerCrasher.java | 50 +++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 96aa43e7b..b54a04d6f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -205,9 +205,6 @@ public final class VideoDetailFragment private Player player; private final PlayerHolder playerHolder = PlayerHolder.getInstance(); - @Nullable - private VideoDetailPlayerCrasher videoDetailPlayerCrasher = null; - /*////////////////////////////////////////////////////////////////////////// // Service management //////////////////////////////////////////////////////////////////////////*/ @@ -600,13 +597,6 @@ public final class VideoDetailFragment @Override public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) { super.onViewCreated(rootView, savedInstanceState); - - if (DEBUG) { - this.videoDetailPlayerCrasher = new VideoDetailPlayerCrasher( - () -> this.getContext(), - () -> this.getLayoutInflater() - ); - } } @Override // called from onViewCreated in {@link BaseFragment#onViewCreated} @@ -667,7 +657,8 @@ public final class VideoDetailFragment binding.detailControlsPlayWithKodi.setOnClickListener(this); if (DEBUG) { binding.detailControlsCrashThePlayer.setOnClickListener( - v -> videoDetailPlayerCrasher.onCrashThePlayer(this.player)); + v -> VideoDetailPlayerCrasher.onCrashThePlayer(this.player, getLayoutInflater()) + ); } binding.overlayThumbnail.setOnClickListener(this); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java index a7c382f33..773a77f50 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java @@ -22,6 +22,7 @@ import org.schabi.newpipe.player.Player; import org.schabi.newpipe.util.ThemeHelper; import java.io.IOException; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.TimeoutException; @@ -30,24 +31,18 @@ import java.util.function.Supplier; /** * Outsourced logic for crashing the player in the {@link VideoDetailFragment}. */ -public class VideoDetailPlayerCrasher { +public final class VideoDetailPlayerCrasher { // This has to be <= 23 chars on devices running Android 7 or lower (API <= 25) // or it fails with an IllegalArgumentException // https://stackoverflow.com/a/54744028 private static final String TAG = "VideoDetPlayerCrasher"; - @NonNull - private final Supplier contextSupplier; - @NonNull - private final Supplier layoutInflaterSupplier; + private static final Map> AVAILABLE_EXCEPTION_TYPES = + getExceptionTypes(); - public VideoDetailPlayerCrasher( - @NonNull final Supplier contextSupplier, - @NonNull final Supplier layoutInflaterSupplier - ) { - this.contextSupplier = contextSupplier; - this.layoutInflaterSupplier = layoutInflaterSupplier; + private VideoDetailPlayerCrasher() { + // No impls } private static Map> getExceptionTypes() { @@ -87,29 +82,22 @@ public class VideoDetailPlayerCrasher { ) ); - return exceptionTypes; + return Collections.unmodifiableMap(exceptionTypes); } - private Context getContext() { - return this.contextSupplier.get(); - } - - private LayoutInflater getLayoutInflater() { - return this.layoutInflaterSupplier.get(); - } - - private Context getThemeWrapperContext() { + private static Context getThemeWrapperContext(final Context context) { return new ContextThemeWrapper( - getContext(), - ThemeHelper.isLightThemeSelected(getContext()) + context, + ThemeHelper.isLightThemeSelected(context) ? R.style.LightTheme : R.style.DarkTheme); } - public void onCrashThePlayer(final Player player) { + public static void onCrashThePlayer(final Player player, final LayoutInflater layoutInflater) { + final Context context = player.getContext(); if (!isPlayerAvailable(player)) { Log.d(TAG, "Player is not available"); - Toast.makeText(getContext(), "Player is not available", Toast.LENGTH_SHORT) + Toast.makeText(context, "Player is not available", Toast.LENGTH_SHORT) .show(); return; @@ -117,13 +105,13 @@ public class VideoDetailPlayerCrasher { // -- Build the dialog/UI -- - final Context themeWrapperContext = getThemeWrapperContext(); + final Context themeWrapperContext = getThemeWrapperContext(context); final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext); - final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(getLayoutInflater()) + final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(layoutInflater) .list; - final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext()) + final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext(context)) .setTitle("Choose an exception") .setView(radioGroup) .setCancelable(true) @@ -131,7 +119,7 @@ public class VideoDetailPlayerCrasher { .create(); for (final Map.Entry> entry - : getExceptionTypes().entrySet()) { + : AVAILABLE_EXCEPTION_TYPES.entrySet()) { final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot(); radioButton.setText(entry.getKey()); radioButton.setChecked(false); @@ -159,7 +147,7 @@ public class VideoDetailPlayerCrasher { * @param player * @param exception */ - private void tryCrashPlayerWith( + private static void tryCrashPlayerWith( @NonNull final Player player, @NonNull final ExoPlaybackException exception ) { @@ -173,7 +161,7 @@ public class VideoDetailPlayerCrasher { } } - private boolean isPlayerAvailable(final Player player) { + private static boolean isPlayerAvailable(final Player player) { return player != null; } }