| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | package com.brentvatne.exoplayer;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 13:13:36 +01:00
										 |  |  | import android.annotation.SuppressLint;
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | import android.app.Dialog;
 | 
					
						
							|  |  |  | import android.content.Context;
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  | import android.os.Handler;
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | import android.view.ViewGroup;
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  | import android.view.Window;
 | 
					
						
							|  |  |  | import android.view.WindowManager;
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | import android.widget.FrameLayout;
 | 
					
						
							|  |  |  | import android.widget.ImageButton;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 01:12:08 -07:00
										 |  |  | import androidx.activity.OnBackPressedCallback;
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  | import androidx.media3.ui.LegacyPlayerControlView;
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  | import com.brentvatne.common.toolbox.DebugLog;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.lang.ref.WeakReference;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 13:13:36 +01:00
										 |  |  | @SuppressLint("PrivateResource")
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | public class FullScreenPlayerView extends Dialog {
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     private final LegacyPlayerControlView playerControlView;
 | 
					
						
							|  |  |  |     private final ExoPlayerView exoPlayerView;
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |     private final ReactExoplayerView reactExoplayerView;
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     private ViewGroup parent;
 | 
					
						
							|  |  |  |     private final FrameLayout containerView;
 | 
					
						
							|  |  |  |     private final OnBackPressedCallback onBackPressedCallback;
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |     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());
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |     public FullScreenPlayerView(Context context, ExoPlayerView exoPlayerView, ReactExoplayerView reactExoplayerView, LegacyPlayerControlView playerControlView, OnBackPressedCallback onBackPressedCallback) {
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         super(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
 | 
					
						
							|  |  |  |         this.playerControlView = playerControlView;
 | 
					
						
							|  |  |  |         this.exoPlayerView = exoPlayerView;
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |         this.reactExoplayerView = reactExoplayerView;
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         this.onBackPressedCallback = onBackPressedCallback;
 | 
					
						
							|  |  |  |         containerView = new FrameLayout(context);
 | 
					
						
							|  |  |  |         setContentView(containerView, generateDefaultLayoutParams());
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         mKeepScreenOnUpdater = new KeepScreenOnUpdater(this);
 | 
					
						
							|  |  |  |         mKeepScreenOnHandler = new Handler();
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     @Override
 | 
					
						
							|  |  |  |     public void onBackPressed() {
 | 
					
						
							|  |  |  |         super.onBackPressed();
 | 
					
						
							|  |  |  |         onBackPressedCallback.handleOnBackPressed();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-08-18 01:12:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     @Override
 | 
					
						
							|  |  |  |     protected void onStart() {
 | 
					
						
							|  |  |  |         parent = (FrameLayout)(exoPlayerView.getParent());
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         parent.removeView(exoPlayerView);
 | 
					
						
							|  |  |  |         containerView.addView(exoPlayerView, generateDefaultLayoutParams());
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         if (playerControlView != null) {
 | 
					
						
							|  |  |  |             ImageButton imageButton = playerControlView.findViewById(com.brentvatne.react.R.id.exo_fullscreen);
 | 
					
						
							|  |  |  |             imageButton.setImageResource(androidx.media3.ui.R.drawable.exo_icon_fullscreen_exit);
 | 
					
						
							|  |  |  |             imageButton.setContentDescription(getContext().getString(androidx.media3.ui.R.string.exo_controls_fullscreen_exit_description));
 | 
					
						
							|  |  |  |             parent.removeView(playerControlView);
 | 
					
						
							|  |  |  |             containerView.addView(playerControlView, generateDefaultLayoutParams());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         super.onStart();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     @Override
 | 
					
						
							|  |  |  |     protected void onStop() {
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |         mKeepScreenOnHandler.removeCallbacks(mKeepScreenOnUpdater);
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         containerView.removeView(exoPlayerView);
 | 
					
						
							|  |  |  |         parent.addView(exoPlayerView, generateDefaultLayoutParams());
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         if (playerControlView != null) {
 | 
					
						
							|  |  |  |             ImageButton imageButton = playerControlView.findViewById(com.brentvatne.react.R.id.exo_fullscreen);
 | 
					
						
							|  |  |  |             imageButton.setImageResource(androidx.media3.ui.R.drawable.exo_icon_fullscreen_enter);
 | 
					
						
							|  |  |  |             imageButton.setContentDescription(getContext().getString(androidx.media3.ui.R.string.exo_controls_fullscreen_enter_description));
 | 
					
						
							|  |  |  |             containerView.removeView(playerControlView);
 | 
					
						
							|  |  |  |             parent.addView(playerControlView, generateDefaultLayoutParams());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         parent.requestLayout();
 | 
					
						
							|  |  |  |         parent = null;
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |         super.onStop();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 09:17:00 +01:00
										 |  |  |     @Override
 | 
					
						
							|  |  |  |     public void onAttachedToWindow() {
 | 
					
						
							|  |  |  |         super.onAttachedToWindow();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (reactExoplayerView.getPreventsDisplaySleepDuringVideoPlayback()) {
 | 
					
						
							|  |  |  |             mKeepScreenOnHandler.post(mKeepScreenOnUpdater);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-18 22:13:54 +09:00
										 |  |  |     private FrameLayout.LayoutParams generateDefaultLayoutParams() {
 | 
					
						
							|  |  |  |         FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
 | 
					
						
							|  |  |  |                 FrameLayout.LayoutParams.MATCH_PARENT,
 | 
					
						
							|  |  |  |                 FrameLayout.LayoutParams.MATCH_PARENT
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  |         layoutParams.setMargins(0, 0, 0, 0);
 | 
					
						
							|  |  |  |         return layoutParams;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-07-14 11:10:18 -07:00
										 |  |  | }
 |