|
| 1 | +package com.brentvatne.exoplayer |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.app.AppOpsManager |
| 5 | +import android.app.PictureInPictureParams |
| 6 | +import android.app.RemoteAction |
| 7 | +import android.content.Context |
| 8 | +import android.content.ContextWrapper |
| 9 | +import android.content.pm.PackageManager |
| 10 | +import android.graphics.Rect |
| 11 | +import android.graphics.drawable.Icon |
| 12 | +import android.os.Build |
| 13 | +import android.os.Process |
| 14 | +import android.util.Rational |
| 15 | +import androidx.activity.ComponentActivity |
| 16 | +import androidx.annotation.ChecksSdkIntAtLeast |
| 17 | +import androidx.annotation.RequiresApi |
| 18 | +import androidx.core.app.AppOpsManagerCompat |
| 19 | +import androidx.core.app.PictureInPictureModeChangedInfo |
| 20 | +import androidx.lifecycle.Lifecycle |
| 21 | +import androidx.media3.exoplayer.ExoPlayer |
| 22 | +import com.brentvatne.common.toolbox.DebugLog |
| 23 | +import com.brentvatne.receiver.PictureInPictureReceiver |
| 24 | +import com.facebook.react.uimanager.ThemedReactContext |
| 25 | + |
| 26 | +internal fun Context.findActivity(): ComponentActivity { |
| 27 | + var context = this |
| 28 | + while (context is ContextWrapper) { |
| 29 | + if (context is ComponentActivity) return context |
| 30 | + context = context.baseContext |
| 31 | + } |
| 32 | + throw IllegalStateException("Picture in picture should be called in the context of an Activity") |
| 33 | +} |
| 34 | + |
| 35 | +object PictureInPictureUtil { |
| 36 | + private const val FLAG_SUPPORTS_PICTURE_IN_PICTURE = 0x400000 |
| 37 | + private const val TAG = "PictureInPictureUtil" |
| 38 | + |
| 39 | + @JvmStatic |
| 40 | + fun addLifecycleEventListener(context: ThemedReactContext, view: ReactExoplayerView): Runnable { |
| 41 | + val activity = context.findActivity() |
| 42 | + |
| 43 | + val onPictureInPictureModeChanged: (info: PictureInPictureModeChangedInfo) -> Unit = { info: PictureInPictureModeChangedInfo -> |
| 44 | + view.setIsInPictureInPicture(info.isInPictureInPictureMode) |
| 45 | + if (!info.isInPictureInPictureMode && activity.lifecycle.currentState == Lifecycle.State.CREATED) { |
| 46 | + // when user click close button of PIP |
| 47 | + if (!view.playInBackground) view.setPausedModifier(true) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + val onUserLeaveHintCallback = { |
| 52 | + if (view.enterPictureInPictureOnLeave) { |
| 53 | + view.enterPictureInPictureMode() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + activity.addOnPictureInPictureModeChangedListener(onPictureInPictureModeChanged) |
| 58 | + |
| 59 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { |
| 60 | + activity.addOnUserLeaveHintListener(onUserLeaveHintCallback) |
| 61 | + } |
| 62 | + |
| 63 | + // @TODO convert to lambda when ReactExoplayerView migrated |
| 64 | + return object : Runnable { |
| 65 | + override fun run() { |
| 66 | + context.findActivity().removeOnPictureInPictureModeChangedListener(onPictureInPictureModeChanged) |
| 67 | + context.findActivity().removeOnUserLeaveHintListener(onUserLeaveHintCallback) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @JvmStatic |
| 73 | + fun enterPictureInPictureMode(context: ThemedReactContext, pictureInPictureParams: PictureInPictureParams?) { |
| 74 | + if (!isSupportPictureInPicture(context)) return |
| 75 | + if (isSupportPictureInPictureAction() && pictureInPictureParams != null) { |
| 76 | + try { |
| 77 | + context.findActivity().enterPictureInPictureMode(pictureInPictureParams) |
| 78 | + } catch (e: IllegalStateException) { |
| 79 | + DebugLog.e(TAG, e.toString()) |
| 80 | + } |
| 81 | + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 82 | + try { |
| 83 | + @Suppress("DEPRECATION") |
| 84 | + context.findActivity().enterPictureInPictureMode() |
| 85 | + } catch (e: IllegalStateException) { |
| 86 | + DebugLog.e(TAG, e.toString()) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @JvmStatic |
| 92 | + fun applyPlayingStatus( |
| 93 | + context: ThemedReactContext, |
| 94 | + pipParamsBuilder: PictureInPictureParams.Builder, |
| 95 | + receiver: PictureInPictureReceiver, |
| 96 | + isPaused: Boolean |
| 97 | + ) { |
| 98 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return |
| 99 | + val actions = getPictureInPictureActions(context, isPaused, receiver) |
| 100 | + pipParamsBuilder.setActions(actions) |
| 101 | + updatePictureInPictureActions(context, pipParamsBuilder.build()) |
| 102 | + } |
| 103 | + |
| 104 | + @JvmStatic |
| 105 | + fun applyAutoEnterEnabled(context: ThemedReactContext, pipParamsBuilder: PictureInPictureParams.Builder, autoEnterEnabled: Boolean) { |
| 106 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return |
| 107 | + pipParamsBuilder.setAutoEnterEnabled(autoEnterEnabled) |
| 108 | + updatePictureInPictureActions(context, pipParamsBuilder.build()) |
| 109 | + } |
| 110 | + |
| 111 | + @JvmStatic |
| 112 | + fun applySourceRectHint(context: ThemedReactContext, pipParamsBuilder: PictureInPictureParams.Builder, playerView: ExoPlayerView) { |
| 113 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return |
| 114 | + pipParamsBuilder.setSourceRectHint(calcRectHint(playerView)) |
| 115 | + updatePictureInPictureActions(context, pipParamsBuilder.build()) |
| 116 | + } |
| 117 | + |
| 118 | + private fun updatePictureInPictureActions(context: ThemedReactContext, pipParams: PictureInPictureParams) { |
| 119 | + if (!isSupportPictureInPictureAction()) return |
| 120 | + if (!isSupportPictureInPicture(context)) return |
| 121 | + try { |
| 122 | + context.findActivity().setPictureInPictureParams(pipParams) |
| 123 | + } catch (e: IllegalStateException) { |
| 124 | + DebugLog.e(TAG, e.toString()) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @JvmStatic |
| 129 | + @RequiresApi(Build.VERSION_CODES.O) |
| 130 | + fun getPictureInPictureActions(context: ThemedReactContext, isPaused: Boolean, receiver: PictureInPictureReceiver): ArrayList<RemoteAction> { |
| 131 | + val intent = receiver.getPipActionIntent(isPaused) |
| 132 | + val resource = |
| 133 | + if (isPaused) androidx.media3.ui.R.drawable.exo_icon_play else androidx.media3.ui.R.drawable.exo_icon_pause |
| 134 | + val icon = Icon.createWithResource(context, resource) |
| 135 | + val title = if (isPaused) "play" else "pause" |
| 136 | + return arrayListOf(RemoteAction(icon, title, title, intent)) |
| 137 | + } |
| 138 | + |
| 139 | + @JvmStatic |
| 140 | + @RequiresApi(Build.VERSION_CODES.O) |
| 141 | + private fun calcRectHint(playerView: ExoPlayerView): Rect { |
| 142 | + val hint = Rect() |
| 143 | + playerView.surfaceView?.getGlobalVisibleRect(hint) |
| 144 | + val location = IntArray(2) |
| 145 | + playerView.surfaceView?.getLocationOnScreen(location) |
| 146 | + |
| 147 | + val height = hint.bottom - hint.top |
| 148 | + hint.top = location[1] |
| 149 | + hint.bottom = hint.top + height |
| 150 | + return hint |
| 151 | + } |
| 152 | + |
| 153 | + @JvmStatic |
| 154 | + @RequiresApi(Build.VERSION_CODES.O) |
| 155 | + fun calcPictureInPictureAspectRatio(player: ExoPlayer): Rational { |
| 156 | + var aspectRatio = Rational(player.videoSize.width, player.videoSize.height) |
| 157 | + // AspectRatio for the activity in picture-in-picture, must be between 2.39:1 and 1:2.39 (inclusive). |
| 158 | + // https://developer.android.com/reference/android/app/PictureInPictureParams.Builder#setAspectRatio(android.util.Rational) |
| 159 | + val maximumRatio = Rational(239, 100) |
| 160 | + val minimumRatio = Rational(100, 239) |
| 161 | + if (aspectRatio.toFloat() > maximumRatio.toFloat()) { |
| 162 | + aspectRatio = maximumRatio |
| 163 | + } else if (aspectRatio.toFloat() < minimumRatio.toFloat()) { |
| 164 | + aspectRatio = minimumRatio |
| 165 | + } |
| 166 | + return aspectRatio |
| 167 | + } |
| 168 | + |
| 169 | + private fun isSupportPictureInPicture(context: ThemedReactContext): Boolean = |
| 170 | + checkIsApiSupport() && checkIsSystemSupportPIP(context) && checkIsUserAllowPIP(context) |
| 171 | + |
| 172 | + private fun isSupportPictureInPictureAction(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O |
| 173 | + |
| 174 | + @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N) |
| 175 | + private fun checkIsApiSupport(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N |
| 176 | + |
| 177 | + @RequiresApi(Build.VERSION_CODES.N) |
| 178 | + private fun checkIsSystemSupportPIP(context: ThemedReactContext): Boolean { |
| 179 | + val activity = context.findActivity() ?: return false |
| 180 | + |
| 181 | + val activityInfo = activity.packageManager.getActivityInfo(activity.componentName, PackageManager.GET_META_DATA) |
| 182 | + // detect current activity's android:supportsPictureInPicture value defined within AndroidManifest.xml |
| 183 | + // https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/content/pm/ActivityInfo.java;l=1090-1093;drc=7651f0a4c059a98f32b0ba30cd64500bf135385f |
| 184 | + val isActivitySupportPip = activityInfo.flags and FLAG_SUPPORTS_PICTURE_IN_PICTURE != 0 |
| 185 | + |
| 186 | + // PIP might be disabled on devices that have low RAM. |
| 187 | + val isPipAvailable = activity.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) |
| 188 | + |
| 189 | + return isActivitySupportPip && isPipAvailable |
| 190 | + } |
| 191 | + |
| 192 | + private fun checkIsUserAllowPIP(context: ThemedReactContext): Boolean { |
| 193 | + val activity = context.currentActivity ?: return false |
| 194 | + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 195 | + @SuppressLint("InlinedApi") |
| 196 | + val result = AppOpsManagerCompat.noteOpNoThrow( |
| 197 | + activity, |
| 198 | + AppOpsManager.OPSTR_PICTURE_IN_PICTURE, |
| 199 | + Process.myUid(), |
| 200 | + activity.packageName |
| 201 | + ) |
| 202 | + AppOpsManager.MODE_ALLOWED == result |
| 203 | + } else { |
| 204 | + Build.VERSION.SDK_INT < Build.VERSION_CODES.O && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments