Skip to content

Commit

Permalink
Merge branch 'master' into feat/capture-method
Browse files Browse the repository at this point in the history
  • Loading branch information
freeboub authored Mar 22, 2024
2 parents ef38b31 + bfb76e6 commit c76fbdf
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.exoplayer.source.ads.AdsMediaSource;

import com.facebook.react.uimanager.ThemedReactContext;

import java.io.IOException;

public class ImaAdsLoader implements AdsLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.common.ViewUtil;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.google.ads.interactivemedia.v3.api.AdError;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -449,14 +447,14 @@ public void receiveAdEvent(String event) {
receiveEvent(EVENT_ON_RECEIVE_AD_EVENT, map);
}

public void receiveAdErrorEvent(AdError error) {
public void receiveAdErrorEvent(String message, String code, String type) {
WritableMap map = Arguments.createMap();
map.putString("event", "ERROR");

WritableMap dataMap = Arguments.createMap();
dataMap.putString("message", error.getMessage());
dataMap.putString("code", String.valueOf(error.getErrorCode()));
dataMap.putString("type", String.valueOf(error.getErrorType()));
dataMap.putString("message", message);
dataMap.putString("code", code);
dataMap.putString("type", type);
map.putMap("data", dataMap);

receiveEvent(EVENT_ON_RECEIVE_AD_EVENT, map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,47 @@ import java.util.HashMap

object ReactBridgeUtils {
@JvmStatic
fun safeGetString(map: ReadableMap?, key: String?, fallback: String?): String? {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getString(key) else fallback
}
fun safeGetString(map: ReadableMap?, key: String?, fallback: String?): String? =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getString(key) else fallback

@JvmStatic
fun safeGetString(map: ReadableMap?, key: String?): String? {
return safeGetString(map, key, null)
}
fun safeGetString(map: ReadableMap?, key: String?): String? = safeGetString(map, key, null)

@JvmStatic
fun safeGetDynamic(map: ReadableMap?, key: String?, fallback: Dynamic?): Dynamic? {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDynamic(key) else fallback
}
fun safeGetDynamic(map: ReadableMap?, key: String?, fallback: Dynamic?): Dynamic? =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDynamic(key) else fallback

@JvmStatic
fun safeGetDynamic(map: ReadableMap?, key: String?): Dynamic? {
return safeGetDynamic(map, key, null)
}
fun safeGetDynamic(map: ReadableMap?, key: String?): Dynamic? = safeGetDynamic(map, key, null)

@JvmStatic
fun safeGetBool(map: ReadableMap?, key: String?, fallback: Boolean): Boolean {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getBoolean(key) else fallback
}
fun safeGetBool(map: ReadableMap?, key: String?, fallback: Boolean): Boolean =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getBoolean(key) else fallback

@JvmStatic
fun safeGetMap(map: ReadableMap?, key: String?): ReadableMap? {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getMap(key) else null
}
fun safeGetMap(map: ReadableMap?, key: String?): ReadableMap? = if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getMap(key) else null

@JvmStatic
fun safeGetArray(map: ReadableMap?, key: String?): ReadableArray? {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getArray(key) else null
}
fun safeGetArray(map: ReadableMap?, key: String?): ReadableArray? = if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getArray(key) else null

@JvmStatic
fun safeGetInt(map: ReadableMap?, key: String?, fallback: Int): Int {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getInt(key) else fallback
}
fun safeGetInt(map: ReadableMap?, key: String?, fallback: Int): Int =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getInt(key) else fallback

@JvmStatic
fun safeGetInt(map: ReadableMap?, key: String?): Int {
return safeGetInt(map, key, 0)
}
fun safeGetInt(map: ReadableMap?, key: String?): Int = safeGetInt(map, key, 0)

@JvmStatic
fun safeGetDouble(map: ReadableMap?, key: String?, fallback: Double): Double {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key) else fallback
}
fun safeGetDouble(map: ReadableMap?, key: String?, fallback: Double): Double =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key) else fallback

@JvmStatic
fun safeGetDouble(map: ReadableMap?, key: String?): Double {
return safeGetDouble(map, key, 0.0)
}
fun safeGetDouble(map: ReadableMap?, key: String?): Double = safeGetDouble(map, key, 0.0)

@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?, fallback: Float): Float {
return if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key).toFloat() else fallback
}
@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?, fallback: Float): Float =
if (map != null && map.hasKey(key!!) && !map.isNull(key)) map.getDouble(key).toFloat() else fallback

@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?): Float {
return safeGetFloat(map, key, 0.0f)
}
@JvmStatic fun safeGetFloat(map: ReadableMap?, key: String?): Float = safeGetFloat(map, key, 0.0f)

/**
* toStringMap converts a [ReadableMap] into a HashMap.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ private void setVideoView() {
}
}

public boolean isPlaying() {
return player != null && player.isPlaying();
}

public void setSubtitleStyle(SubtitleStyle style) {
// ensure we reset subtile style before reapplying it
subtitleLayout.setUserDefaultStyle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,73 @@
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import androidx.activity.OnBackPressedCallback;
import androidx.media3.ui.LegacyPlayerControlView;

import com.brentvatne.common.toolbox.DebugLog;

import java.lang.ref.WeakReference;

@SuppressLint("PrivateResource")
public class FullScreenPlayerView extends Dialog {
private final LegacyPlayerControlView playerControlView;
private final ExoPlayerView exoPlayerView;
private final ReactExoplayerView reactExoplayerView;
private ViewGroup parent;
private final FrameLayout containerView;
private final OnBackPressedCallback onBackPressedCallback;
private final Handler mKeepScreenOnHandler;
private final Runnable mKeepScreenOnUpdater;

private static class KeepScreenOnUpdater implements Runnable {
private final static long UPDATE_KEEP_SCREEN_ON_FLAG_MS = 200;
private final WeakReference<FullScreenPlayerView> mFullscreenPlayer;

KeepScreenOnUpdater(FullScreenPlayerView player) {
mFullscreenPlayer = new WeakReference<>(player);
}

@Override
public void run() {
try {
FullScreenPlayerView fullscreenVideoPlayer = mFullscreenPlayer.get();
if (fullscreenVideoPlayer != null) {
final Window window = fullscreenVideoPlayer.getWindow();
if (window != null) {
boolean isPlaying = fullscreenVideoPlayer.exoPlayerView.isPlaying();
if (isPlaying) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
fullscreenVideoPlayer.mKeepScreenOnHandler.postDelayed(this, UPDATE_KEEP_SCREEN_ON_FLAG_MS);
}
} catch (Exception ex) {
DebugLog.e("ExoPlayer Exception", "Failed to flag FLAG_KEEP_SCREEN_ON on fullscreeen.");
DebugLog.e("ExoPlayer Exception", ex.toString());
}
}
}

public FullScreenPlayerView(Context context, ExoPlayerView exoPlayerView, LegacyPlayerControlView playerControlView, OnBackPressedCallback onBackPressedCallback) {
public FullScreenPlayerView(Context context, ExoPlayerView exoPlayerView, ReactExoplayerView reactExoplayerView, LegacyPlayerControlView playerControlView, OnBackPressedCallback onBackPressedCallback) {
super(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
this.playerControlView = playerControlView;
this.exoPlayerView = exoPlayerView;
this.reactExoplayerView = reactExoplayerView;
this.onBackPressedCallback = onBackPressedCallback;
containerView = new FrameLayout(context);
setContentView(containerView, generateDefaultLayoutParams());

mKeepScreenOnUpdater = new KeepScreenOnUpdater(this);
mKeepScreenOnHandler = new Handler();
}

@Override
Expand Down Expand Up @@ -53,6 +98,7 @@ protected void onStart() {

@Override
protected void onStop() {
mKeepScreenOnHandler.removeCallbacks(mKeepScreenOnUpdater);
containerView.removeView(exoPlayerView);
parent.addView(exoPlayerView, generateDefaultLayoutParams());

Expand All @@ -70,6 +116,15 @@ protected void onStop() {
super.onStop();
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();

if (reactExoplayerView.getPreventsDisplaySleepDuringVideoPlayback()) {
mKeepScreenOnHandler.post(mKeepScreenOnUpdater);
}
}

private FrameLayout.LayoutParams generateDefaultLayoutParams() {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.uimanager.ThemedReactContext;
import com.google.ads.interactivemedia.v3.api.AdError;
import com.google.ads.interactivemedia.v3.api.AdEvent;
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -205,11 +206,11 @@ public class ReactExoplayerView extends FrameLayout implements
private String extension;
private boolean repeat;
private String audioTrackType;
private Dynamic audioTrackValue;
private String audioTrackValue;
private String videoTrackType;
private Dynamic videoTrackValue;
private String videoTrackValue;
private String textTrackType;
private Dynamic textTrackValue;
private String textTrackValue;
private ReadableArray textTracks;
private boolean disableFocus;
private boolean focusable = true;
Expand Down Expand Up @@ -405,7 +406,7 @@ private void initializePlayerControl() {
}

if (fullScreenPlayerView == null) {
fullScreenPlayerView = new FullScreenPlayerView(getContext(), exoPlayerView, playerControlView, new OnBackPressedCallback(true) {
fullScreenPlayerView = new FullScreenPlayerView(getContext(), exoPlayerView, this, playerControlView, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
setFullscreen(false);
Expand Down Expand Up @@ -1634,7 +1635,7 @@ public void disableTrack(int rendererIndex) {
trackSelector.setParameters(disableParameters);
}

public void setSelectedTrack(int trackType, String type, Dynamic value) {
public void setSelectedTrack(int trackType, String type, String value) {
if (player == null) return;
int rendererIndex = getTrackRendererIndex(trackType);
if (rendererIndex == C.INDEX_UNSET) {
Expand All @@ -1660,25 +1661,26 @@ public void setSelectedTrack(int trackType, String type, Dynamic value) {
} else if (type.equals("language")) {
for (int i = 0; i < groups.length; ++i) {
Format format = groups.get(i).getFormat(0);
if (format.language != null && format.language.equals(value.asString())) {
if (format.language != null && format.language.equals(value)) {
groupIndex = i;
break;
}
}
} else if (type.equals("title")) {
for (int i = 0; i < groups.length; ++i) {
Format format = groups.get(i).getFormat(0);
if (format.id != null && format.id.equals(value.asString())) {
if (format.id != null && format.id.equals(value)) {
groupIndex = i;
break;
}
}
} else if (type.equals("index")) {
if (value.asInt() < groups.length) {
groupIndex = value.asInt();
int iValue = Integer.parseInt(value);
if (iValue < groups.length) {
groupIndex = iValue;
}
} else if (type.equals("resolution")) {
int height = value.asInt();
int height = Integer.parseInt(value);
for (int i = 0; i < groups.length; ++i) { // Search for the exact height
TrackGroup group = groups.get(i);
Format closestFormat = null;
Expand Down Expand Up @@ -1830,19 +1832,19 @@ public ExoPlayerView getExoPlayerView() {
return this.exoPlayerView;
}

public void setSelectedVideoTrack(String type, Dynamic value) {
public void setSelectedVideoTrack(String type, String value) {
videoTrackType = type;
videoTrackValue = value;
setSelectedTrack(C.TRACK_TYPE_VIDEO, videoTrackType, videoTrackValue);
}

public void setSelectedAudioTrack(String type, Dynamic value) {
public void setSelectedAudioTrack(String type, String value) {
audioTrackType = type;
audioTrackValue = value;
setSelectedTrack(C.TRACK_TYPE_AUDIO, audioTrackType, audioTrackValue);
}

public void setSelectedTextTrack(String type, Dynamic value) {
public void setSelectedTextTrack(String type, String value) {
textTrackType = type;
textTrackValue = value;
setSelectedTrack(C.TRACK_TYPE_TEXT, textTrackType, textTrackValue);
Expand Down Expand Up @@ -1906,6 +1908,11 @@ public void seekTo(long positionMs) {
}

public void setRateModifier(float newRate) {
if (newRate <= 0) {
DebugLog.w(TAG, "cannot set rate <= 0");
return;
}

rate = newRate;

if (player != null) {
Expand Down Expand Up @@ -1963,6 +1970,10 @@ public void setDisableBuffering(boolean disableBuffering) {
this.disableBuffering = disableBuffering;
}

public boolean getPreventsDisplaySleepDuringVideoPlayback() {
return preventsDisplaySleepDuringVideoPlayback;
}

private void updateFullScreenButtonVisbility() {
if (playerControlView != null) {
final ImageButton fullScreenButton = playerControlView.findViewById(R.id.exo_fullscreen);
Expand Down Expand Up @@ -2128,6 +2139,7 @@ public void onAdEvent(AdEvent adEvent) {

@Override
public void onAdError(AdErrorEvent adErrorEvent) {
eventEmitter.receiveAdErrorEvent(adErrorEvent.getError());
AdError error = adErrorEvent.getError();
eventEmitter.receiveAdErrorEvent(error.getMessage(), String.valueOf(error.getErrorCode()), String.valueOf(error.getErrorType()));
}
}
Loading

0 comments on commit c76fbdf

Please sign in to comment.