Skip to content

Commit 0d51eef

Browse files
committed
Refactoring
1 parent aa28a85 commit 0d51eef

2 files changed

Lines changed: 21 additions & 42 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,6 @@ public final class VideoDetailFragment
205205
private Player player;
206206
private final PlayerHolder playerHolder = PlayerHolder.getInstance();
207207

208-
@Nullable
209-
private VideoDetailPlayerCrasher videoDetailPlayerCrasher = null;
210-
211208
/*//////////////////////////////////////////////////////////////////////////
212209
// Service management
213210
//////////////////////////////////////////////////////////////////////////*/
@@ -600,13 +597,6 @@ private void toggleTitleAndSecondaryControls() {
600597
@Override
601598
public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) {
602599
super.onViewCreated(rootView, savedInstanceState);
603-
604-
if (DEBUG) {
605-
this.videoDetailPlayerCrasher = new VideoDetailPlayerCrasher(
606-
() -> this.getContext(),
607-
() -> this.getLayoutInflater()
608-
);
609-
}
610600
}
611601

612602
@Override // called from onViewCreated in {@link BaseFragment#onViewCreated}
@@ -667,7 +657,8 @@ protected void initListeners() {
667657
binding.detailControlsPlayWithKodi.setOnClickListener(this);
668658
if (DEBUG) {
669659
binding.detailControlsCrashThePlayer.setOnClickListener(
670-
v -> videoDetailPlayerCrasher.onCrashThePlayer(this.player));
660+
v -> VideoDetailPlayerCrasher.onCrashThePlayer(this.player, getLayoutInflater())
661+
);
671662
}
672663

673664
binding.overlayThumbnail.setOnClickListener(this);

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.schabi.newpipe.util.ThemeHelper;
2323

2424
import java.io.IOException;
25+
import java.util.Collections;
2526
import java.util.LinkedHashMap;
2627
import java.util.Map;
2728
import java.util.concurrent.TimeoutException;
@@ -30,24 +31,18 @@
3031
/**
3132
* Outsourced logic for crashing the player in the {@link VideoDetailFragment}.
3233
*/
33-
public class VideoDetailPlayerCrasher {
34+
public final class VideoDetailPlayerCrasher {
3435

3536
// This has to be <= 23 chars on devices running Android 7 or lower (API <= 25)
3637
// or it fails with an IllegalArgumentException
3738
// https://stackoverflow.com/a/54744028
3839
private static final String TAG = "VideoDetPlayerCrasher";
3940

40-
@NonNull
41-
private final Supplier<Context> contextSupplier;
42-
@NonNull
43-
private final Supplier<LayoutInflater> layoutInflaterSupplier;
41+
private static final Map<String, Supplier<ExoPlaybackException>> AVAILABLE_EXCEPTION_TYPES =
42+
getExceptionTypes();
4443

45-
public VideoDetailPlayerCrasher(
46-
@NonNull final Supplier<Context> contextSupplier,
47-
@NonNull final Supplier<LayoutInflater> layoutInflaterSupplier
48-
) {
49-
this.contextSupplier = contextSupplier;
50-
this.layoutInflaterSupplier = layoutInflaterSupplier;
44+
private VideoDetailPlayerCrasher() {
45+
// No impls
5146
}
5247

5348
private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() {
@@ -87,51 +82,44 @@ private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() {
8782
)
8883
);
8984

90-
return exceptionTypes;
91-
}
92-
93-
private Context getContext() {
94-
return this.contextSupplier.get();
95-
}
96-
97-
private LayoutInflater getLayoutInflater() {
98-
return this.layoutInflaterSupplier.get();
85+
return Collections.unmodifiableMap(exceptionTypes);
9986
}
10087

101-
private Context getThemeWrapperContext() {
88+
private static Context getThemeWrapperContext(final Context context) {
10289
return new ContextThemeWrapper(
103-
getContext(),
104-
ThemeHelper.isLightThemeSelected(getContext())
90+
context,
91+
ThemeHelper.isLightThemeSelected(context)
10592
? R.style.LightTheme
10693
: R.style.DarkTheme);
10794
}
10895

109-
public void onCrashThePlayer(final Player player) {
96+
public static void onCrashThePlayer(final Player player, final LayoutInflater layoutInflater) {
97+
final Context context = player.getContext();
11098
if (!isPlayerAvailable(player)) {
11199
Log.d(TAG, "Player is not available");
112-
Toast.makeText(getContext(), "Player is not available", Toast.LENGTH_SHORT)
100+
Toast.makeText(context, "Player is not available", Toast.LENGTH_SHORT)
113101
.show();
114102

115103
return;
116104
}
117105

118106
// -- Build the dialog/UI --
119107

120-
final Context themeWrapperContext = getThemeWrapperContext();
108+
final Context themeWrapperContext = getThemeWrapperContext(context);
121109

122110
final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext);
123-
final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(getLayoutInflater())
111+
final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(layoutInflater)
124112
.list;
125113

126-
final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext())
114+
final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext(context))
127115
.setTitle("Choose an exception")
128116
.setView(radioGroup)
129117
.setCancelable(true)
130118
.setNegativeButton(R.string.cancel, null)
131119
.create();
132120

133121
for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry
134-
: getExceptionTypes().entrySet()) {
122+
: AVAILABLE_EXCEPTION_TYPES.entrySet()) {
135123
final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot();
136124
radioButton.setText(entry.getKey());
137125
radioButton.setChecked(false);
@@ -159,7 +147,7 @@ public void onCrashThePlayer(final Player player) {
159147
* @param player
160148
* @param exception
161149
*/
162-
private void tryCrashPlayerWith(
150+
private static void tryCrashPlayerWith(
163151
@NonNull final Player player,
164152
@NonNull final ExoPlaybackException exception
165153
) {
@@ -173,7 +161,7 @@ private void tryCrashPlayerWith(
173161
}
174162
}
175163

176-
private boolean isPlayerAvailable(final Player player) {
164+
private static boolean isPlayerAvailable(final Player player) {
177165
return player != null;
178166
}
179167
}

0 commit comments

Comments
 (0)