-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from azrael8576/feat/video-pip
Implement Picture-in-Picture Mode in Video Module
- Loading branch information
Showing
10 changed files
with
340 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/common/src/main/java/com/wei/picquest/core/pip/PictureInPicture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.wei.picquest.core.pip | ||
|
||
import android.app.Activity | ||
import android.app.PictureInPictureParams | ||
import android.content.Context | ||
import android.content.ContextWrapper | ||
import android.content.pm.PackageManager | ||
import android.graphics.Rect | ||
import android.os.Build | ||
import android.util.Rational | ||
|
||
fun updatedPipParams( | ||
context: Context, | ||
rect: Rect, | ||
) { | ||
if (!context.isPictureInPictureSupported) return | ||
|
||
val aspect = Rational(rect.width(), rect.height()) | ||
val paramsBuilder = PictureInPictureParams.Builder() | ||
|
||
if (aspect.toFloat() in 0.418410..2.390000) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
paramsBuilder.setAspectRatio(aspect) | ||
paramsBuilder.setSourceRectHint(rect) | ||
} | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
paramsBuilder.setSeamlessResizeEnabled(true) | ||
} | ||
context.findActivity().setPictureInPictureParams(paramsBuilder.build()) | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
fun enterPictureInPicture( | ||
context: Context, | ||
) { | ||
context.findActivity().enterPictureInPictureMode() | ||
} | ||
|
||
val Context.isPictureInPictureSupported: Boolean | ||
get() { | ||
return packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O | ||
} | ||
|
||
val Context.isInPictureInPictureMode: Boolean | ||
get() { | ||
val currentActivity = findActivity() | ||
return currentActivity.isInPictureInPictureMode | ||
} | ||
|
||
internal fun Context.findActivity(): Activity { | ||
var context = this | ||
while (context is ContextWrapper) { | ||
if (context is Activity) return context | ||
context = context.baseContext | ||
} | ||
throw IllegalStateException("Activity not found. Unknown error.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.