Refactoring

This commit is contained in:
litetex 2021-11-23 20:11:36 +01:00
parent aa28a85747
commit 0d51eefbb9
2 changed files with 21 additions and 42 deletions

View file

@ -205,9 +205,6 @@ public final class VideoDetailFragment
private Player player; private Player player;
private final PlayerHolder playerHolder = PlayerHolder.getInstance(); private final PlayerHolder playerHolder = PlayerHolder.getInstance();
@Nullable
private VideoDetailPlayerCrasher videoDetailPlayerCrasher = null;
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Service management // Service management
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -600,13 +597,6 @@ public final class VideoDetailFragment
@Override @Override
public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) { public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState); super.onViewCreated(rootView, savedInstanceState);
if (DEBUG) {
this.videoDetailPlayerCrasher = new VideoDetailPlayerCrasher(
() -> this.getContext(),
() -> this.getLayoutInflater()
);
}
} }
@Override // called from onViewCreated in {@link BaseFragment#onViewCreated} @Override // called from onViewCreated in {@link BaseFragment#onViewCreated}
@ -667,7 +657,8 @@ public final class VideoDetailFragment
binding.detailControlsPlayWithKodi.setOnClickListener(this); binding.detailControlsPlayWithKodi.setOnClickListener(this);
if (DEBUG) { if (DEBUG) {
binding.detailControlsCrashThePlayer.setOnClickListener( binding.detailControlsCrashThePlayer.setOnClickListener(
v -> videoDetailPlayerCrasher.onCrashThePlayer(this.player)); v -> VideoDetailPlayerCrasher.onCrashThePlayer(this.player, getLayoutInflater())
);
} }
binding.overlayThumbnail.setOnClickListener(this); binding.overlayThumbnail.setOnClickListener(this);

View file

@ -22,6 +22,7 @@ import org.schabi.newpipe.player.Player;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -30,24 +31,18 @@ import java.util.function.Supplier;
/** /**
* Outsourced logic for crashing the player in the {@link VideoDetailFragment}. * 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) // This has to be <= 23 chars on devices running Android 7 or lower (API <= 25)
// or it fails with an IllegalArgumentException // or it fails with an IllegalArgumentException
// https://stackoverflow.com/a/54744028 // https://stackoverflow.com/a/54744028
private static final String TAG = "VideoDetPlayerCrasher"; private static final String TAG = "VideoDetPlayerCrasher";
@NonNull private static final Map<String, Supplier<ExoPlaybackException>> AVAILABLE_EXCEPTION_TYPES =
private final Supplier<Context> contextSupplier; getExceptionTypes();
@NonNull
private final Supplier<LayoutInflater> layoutInflaterSupplier;
public VideoDetailPlayerCrasher( private VideoDetailPlayerCrasher() {
@NonNull final Supplier<Context> contextSupplier, // No impls
@NonNull final Supplier<LayoutInflater> layoutInflaterSupplier
) {
this.contextSupplier = contextSupplier;
this.layoutInflaterSupplier = layoutInflaterSupplier;
} }
private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() { private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() {
@ -87,29 +82,22 @@ public class VideoDetailPlayerCrasher {
) )
); );
return exceptionTypes; return Collections.unmodifiableMap(exceptionTypes);
} }
private Context getContext() { private static Context getThemeWrapperContext(final Context context) {
return this.contextSupplier.get();
}
private LayoutInflater getLayoutInflater() {
return this.layoutInflaterSupplier.get();
}
private Context getThemeWrapperContext() {
return new ContextThemeWrapper( return new ContextThemeWrapper(
getContext(), context,
ThemeHelper.isLightThemeSelected(getContext()) ThemeHelper.isLightThemeSelected(context)
? R.style.LightTheme ? R.style.LightTheme
: R.style.DarkTheme); : 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)) { if (!isPlayerAvailable(player)) {
Log.d(TAG, "Player is not available"); 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(); .show();
return; return;
@ -117,13 +105,13 @@ public class VideoDetailPlayerCrasher {
// -- Build the dialog/UI -- // -- Build the dialog/UI --
final Context themeWrapperContext = getThemeWrapperContext(); final Context themeWrapperContext = getThemeWrapperContext(context);
final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext); final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext);
final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(getLayoutInflater()) final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(layoutInflater)
.list; .list;
final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext()) final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext(context))
.setTitle("Choose an exception") .setTitle("Choose an exception")
.setView(radioGroup) .setView(radioGroup)
.setCancelable(true) .setCancelable(true)
@ -131,7 +119,7 @@ public class VideoDetailPlayerCrasher {
.create(); .create();
for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry
: getExceptionTypes().entrySet()) { : AVAILABLE_EXCEPTION_TYPES.entrySet()) {
final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot(); final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot();
radioButton.setText(entry.getKey()); radioButton.setText(entry.getKey());
radioButton.setChecked(false); radioButton.setChecked(false);
@ -159,7 +147,7 @@ public class VideoDetailPlayerCrasher {
* @param player * @param player
* @param exception * @param exception
*/ */
private void tryCrashPlayerWith( private static void tryCrashPlayerWith(
@NonNull final Player player, @NonNull final Player player,
@NonNull final ExoPlaybackException exception @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; return player != null;
} }
} }