|
| 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.appcompat.app.AlertDialog; |
| 14 | + |
| 15 | +import com.google.android.exoplayer2.ExoPlaybackException; |
| 16 | +import com.google.android.exoplayer2.RendererCapabilities; |
| 17 | + |
| 18 | +import org.schabi.newpipe.R; |
| 19 | +import org.schabi.newpipe.databinding.ListRadioIconItemBinding; |
| 20 | +import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; |
| 21 | +import org.schabi.newpipe.player.Player; |
| 22 | +import org.schabi.newpipe.util.ThemeHelper; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.LinkedHashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.concurrent.TimeoutException; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +public class VideoDetailPlayerCrasher { |
| 31 | + |
| 32 | + private static final String TAG = "VideoDetPlayerCrasher"; |
| 33 | + |
| 34 | + @NonNull |
| 35 | + private final Supplier<Context> contextSupplier; |
| 36 | + @NonNull |
| 37 | + private final Supplier<LayoutInflater> layoutInflaterSupplier; |
| 38 | + |
| 39 | + public VideoDetailPlayerCrasher( |
| 40 | + @NonNull final Supplier<Context> contextSupplier, |
| 41 | + @NonNull final Supplier<LayoutInflater> layoutInflaterSupplier |
| 42 | + ) { |
| 43 | + this.contextSupplier = contextSupplier; |
| 44 | + this.layoutInflaterSupplier = layoutInflaterSupplier; |
| 45 | + } |
| 46 | + |
| 47 | + private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() { |
| 48 | + final String defaultMsg = "Dummy"; |
| 49 | + final Map<String, Supplier<ExoPlaybackException>> exceptionTypes = new LinkedHashMap<>(); |
| 50 | + exceptionTypes.put( |
| 51 | + "Source", |
| 52 | + () -> ExoPlaybackException.createForSource( |
| 53 | + new IOException(defaultMsg) |
| 54 | + ) |
| 55 | + ); |
| 56 | + exceptionTypes.put( |
| 57 | + "Renderer", |
| 58 | + () -> ExoPlaybackException.createForRenderer( |
| 59 | + new Exception(defaultMsg), |
| 60 | + "Dummy renderer", |
| 61 | + 0, |
| 62 | + null, |
| 63 | + RendererCapabilities.FORMAT_HANDLED |
| 64 | + ) |
| 65 | + ); |
| 66 | + exceptionTypes.put( |
| 67 | + "Unexpected", |
| 68 | + () -> ExoPlaybackException.createForUnexpected( |
| 69 | + new RuntimeException(defaultMsg) |
| 70 | + ) |
| 71 | + ); |
| 72 | + exceptionTypes.put( |
| 73 | + "Remote", |
| 74 | + () -> ExoPlaybackException.createForRemote(defaultMsg) |
| 75 | + ); |
| 76 | + exceptionTypes.put( |
| 77 | + "Timeout", |
| 78 | + () -> ExoPlaybackException.createForTimeout( |
| 79 | + new TimeoutException(defaultMsg), |
| 80 | + ExoPlaybackException.TIMEOUT_OPERATION_UNDEFINED |
| 81 | + ) |
| 82 | + ); |
| 83 | + |
| 84 | + return exceptionTypes; |
| 85 | + } |
| 86 | + |
| 87 | + private Context getContext() { |
| 88 | + return this.contextSupplier.get(); |
| 89 | + } |
| 90 | + |
| 91 | + private LayoutInflater getLayoutInflater() { |
| 92 | + return this.layoutInflaterSupplier.get(); |
| 93 | + } |
| 94 | + |
| 95 | + private Context getThemeWrapperContext() { |
| 96 | + return new ContextThemeWrapper( |
| 97 | + getContext(), |
| 98 | + ThemeHelper.isLightThemeSelected(getContext()) |
| 99 | + ? R.style.LightTheme |
| 100 | + : R.style.DarkTheme); |
| 101 | + } |
| 102 | + |
| 103 | + public void onCrashThePlayer(final Player player) { |
| 104 | + if (!isPlayerAvailable(player)) { |
| 105 | + Log.d(TAG, "Player is not available"); |
| 106 | + Toast.makeText(getContext(), "Player is not available", Toast.LENGTH_SHORT) |
| 107 | + .show(); |
| 108 | + |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + final Context themeWrapperContext = getThemeWrapperContext(); |
| 113 | + |
| 114 | + final LayoutInflater inflater = LayoutInflater.from(themeWrapperContext); |
| 115 | + final RadioGroup radioGroup = SingleChoiceDialogViewBinding.inflate(getLayoutInflater()) |
| 116 | + .list; |
| 117 | + |
| 118 | + final AlertDialog alertDialog = new AlertDialog.Builder(getThemeWrapperContext()) |
| 119 | + .setTitle("Choose an exception") |
| 120 | + .setView(radioGroup) |
| 121 | + .setCancelable(true) |
| 122 | + .setNegativeButton(R.string.cancel, null) |
| 123 | + .create(); |
| 124 | + |
| 125 | + for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry |
| 126 | + : getExceptionTypes().entrySet()) { |
| 127 | + final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot(); |
| 128 | + radioButton.setText(entry.getKey()); |
| 129 | + radioButton.setChecked(false); |
| 130 | + radioButton.setLayoutParams( |
| 131 | + new RadioGroup.LayoutParams( |
| 132 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 133 | + ViewGroup.LayoutParams.WRAP_CONTENT |
| 134 | + ) |
| 135 | + ); |
| 136 | + radioButton.setOnClickListener(v -> { |
| 137 | + tryCrashPlayerWith(player, entry.getValue().get()); |
| 138 | + if (alertDialog != null) { |
| 139 | + alertDialog.cancel(); |
| 140 | + } |
| 141 | + }); |
| 142 | + radioGroup.addView(radioButton); |
| 143 | + } |
| 144 | + |
| 145 | + alertDialog.show(); |
| 146 | + } |
| 147 | + |
| 148 | + private void tryCrashPlayerWith( |
| 149 | + @NonNull final Player player, |
| 150 | + @NonNull final ExoPlaybackException exception |
| 151 | + ) { |
| 152 | + Log.d(TAG, "Crashing the player using player.onPlayerError(ex)"); |
| 153 | + try { |
| 154 | + player.onPlayerError(exception); |
| 155 | + } catch (final Exception exPlayer) { |
| 156 | + Log.e(TAG, |
| 157 | + "Run into an exception while crashing the player:", |
| 158 | + exPlayer); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private boolean isPlayerAvailable(final Player player) { |
| 163 | + return player != null; |
| 164 | + } |
| 165 | +} |
0 commit comments