|
| 1 | +package org.schabi.newpipe.fragments.detail; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.Log; |
| 5 | +import android.view.ContextThemeWrapper; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.RadioButton; |
| 9 | +import android.widget.RadioGroup; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import androidx.appcompat.app.AlertDialog; |
| 15 | + |
| 16 | +import com.google.android.exoplayer2.C; |
| 17 | +import com.google.android.exoplayer2.ExoPlaybackException; |
| 18 | + |
| 19 | +import org.schabi.newpipe.R; |
| 20 | +import org.schabi.newpipe.databinding.ListRadioIconItemBinding; |
| 21 | +import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; |
| 22 | +import org.schabi.newpipe.player.Player; |
| 23 | +import org.schabi.newpipe.util.ThemeHelper; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.function.Supplier; |
| 30 | + |
| 31 | +/** |
| 32 | + * Outsourced logic for crashing the player in the {@link VideoDetailFragment}. |
| 33 | + */ |
| 34 | +public final class VideoDetailPlayerCrasher { |
| 35 | + |
| 36 | + // This has to be <= 23 chars on devices running Android 7 or lower (API <= 25) |
| 37 | + // or it fails with an IllegalArgumentException |
| 38 | + // https://stackoverflow.com/a/54744028 |
| 39 | + private static final String TAG = "VideoDetPlayerCrasher"; |
| 40 | + |
| 41 | + private static final Map<String, Supplier<ExoPlaybackException>> AVAILABLE_EXCEPTION_TYPES = |
| 42 | + getExceptionTypes(); |
| 43 | + |
| 44 | + private VideoDetailPlayerCrasher() { |
| 45 | + // No impls |
| 46 | + } |
| 47 | + |
| 48 | + private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() { |
| 49 | + final String defaultMsg = "Dummy"; |
| 50 | + final Map<String, Supplier<ExoPlaybackException>> exceptionTypes = new LinkedHashMap<>(); |
| 51 | + exceptionTypes.put( |
| 52 | + "Source", |
| 53 | + () -> ExoPlaybackException.createForSource( |
| 54 | + new IOException(defaultMsg) |
| 55 | + ) |
| 56 | + ); |
| 57 | + exceptionTypes.put( |
| 58 | + "Renderer", |
| 59 | + () -> ExoPlaybackException.createForRenderer( |
| 60 | + new Exception(defaultMsg), |
| 61 | + "Dummy renderer", |
| 62 | + 0, |
| 63 | + null, |
| 64 | + C.FORMAT_HANDLED |
| 65 | + ) |
| 66 | + ); |
| 67 | + exceptionTypes.put( |
| 68 | + "Unexpected", |
| 69 | + () -> ExoPlaybackException.createForUnexpected( |
| 70 | + new RuntimeException(defaultMsg) |
| 71 | + ) |
| 72 | + ); |
| 73 | + exceptionTypes.put( |
| 74 | + "Remote", |
| 75 | + () -> ExoPlaybackException.createForRemote(defaultMsg) |
| 76 | + ); |
| 77 | + |
| 78 | + return Collections.unmodifiableMap(exceptionTypes); |
| 79 | + } |
| 80 | + |
| 81 | + private static Context getThemeWrapperContext(final Context context) { |
| 82 | + return new ContextThemeWrapper( |
| 83 | + context, |
| 84 | + ThemeHelper.isLightThemeSelected(context) |
| 85 | + ? R.style.LightTheme |
| 86 | + : R.style.DarkTheme); |
| 87 | + } |
| 88 | + |
| 89 | + public static void onCrashThePlayer( |
| 90 | + @NonNull final Context context, |
| 91 | + @Nullable final Player player, |
| 92 | + @NonNull final LayoutInflater layoutInflater |
| 93 | + ) { |
| 94 | + if (player == null) { |
| 95 | + Log.d(TAG, "Player is not available"); |
| 96 | + Toast.makeText(context, "Player is not available", Toast.LENGTH_SHORT) |
| 97 | + .show(); |
| 98 | + |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // -- Build the dialog/UI -- |
| 103 | + |
| 104 | + final Context themeWrapperContext = getThemeWrapperContext(context); |
| 105 | + |
| 106 | + final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext); |
| 107 | + final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(layoutInflater) |
| 108 | + .list; |
| 109 | + |
| 110 | + final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext(context)) |
| 111 | + .setTitle("Choose an exception") |
| 112 | + .setView(radioGroup) |
| 113 | + .setCancelable(true) |
| 114 | + .setNegativeButton(R.string.cancel, null) |
| 115 | + .create(); |
| 116 | + |
| 117 | + for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry |
| 118 | + : AVAILABLE_EXCEPTION_TYPES.entrySet()) { |
| 119 | + final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot(); |
| 120 | + radioButton.setText(entry.getKey()); |
| 121 | + radioButton.setChecked(false); |
| 122 | + radioButton.setLayoutParams( |
| 123 | + new RadioGroup.LayoutParams( |
| 124 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 125 | + ViewGroup.LayoutParams.WRAP_CONTENT |
| 126 | + ) |
| 127 | + ); |
| 128 | + radioButton.setOnClickListener(v -> { |
| 129 | + tryCrashPlayerWith(player, entry.getValue().get()); |
| 130 | + if (alertDialog != null) { |
| 131 | + alertDialog.cancel(); |
| 132 | + } |
| 133 | + }); |
| 134 | + radioGroup.addView(radioButton); |
| 135 | + } |
| 136 | + |
| 137 | + alertDialog.show(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Note that this method does not crash the underlying exoplayer directly (it's not possible). |
| 142 | + * It simply supplies a Exception to {@link Player#onPlayerError(ExoPlaybackException)}. |
| 143 | + * @param player |
| 144 | + * @param exception |
| 145 | + */ |
| 146 | + private static void tryCrashPlayerWith( |
| 147 | + @NonNull final Player player, |
| 148 | + @NonNull final ExoPlaybackException exception |
| 149 | + ) { |
| 150 | + Log.d(TAG, "Crashing the player using player.onPlayerError(ex)"); |
| 151 | + try { |
| 152 | + player.onPlayerError(exception); |
| 153 | + } catch (final Exception exPlayer) { |
| 154 | + Log.e(TAG, |
| 155 | + "Run into an exception while crashing the player:", |
| 156 | + exPlayer); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments