2222import org .schabi .newpipe .util .ThemeHelper ;
2323
2424import java .io .IOException ;
25+ import java .util .Collections ;
2526import java .util .LinkedHashMap ;
2627import java .util .Map ;
2728import java .util .concurrent .TimeoutException ;
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