Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Pip crash in player and subtitle styles not applied correctly #944

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.anilbeesetti.nextplayer.core.database.dao

import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import dev.anilbeesetti.nextplayer.core.database.entities.DirectoryEntity
import dev.anilbeesetti.nextplayer.core.database.relations.DirectoryWithMedia
Expand All @@ -19,6 +20,7 @@ interface DirectoryDao {
@Query("SELECT * FROM directories")
fun getAll(): Flow<List<DirectoryEntity>>

@Transaction
@Query("SELECT * FROM directories")
fun getAllWithMedia(): Flow<List<DirectoryWithMedia>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.PictureInPictureParams
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Typeface
Expand Down Expand Up @@ -184,6 +185,10 @@ class PlayerActivity : AppCompatActivity() {
private lateinit var videoTitleTextView: TextView
private lateinit var videoZoomButton: ImageButton

private val isPipSupported: Boolean by lazy {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
prettyPrintIntent()
Expand Down Expand Up @@ -229,10 +234,8 @@ class PlayerActivity : AppCompatActivity() {
videoTitleTextView = binding.playerView.findViewById(R.id.video_name)
videoZoomButton = binding.playerView.findViewById(R.id.btn_video_zoom)

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O) {
if (!isPipSupported) {
pipButton.visibility = View.GONE
} else {
updatePictureInPictureParams()
}

seekBar.addListener(object : TimeBar.OnScrubListener {
Expand Down Expand Up @@ -301,10 +304,15 @@ class PlayerActivity : AppCompatActivity() {
super.onStop()
}

@RequiresApi(Build.VERSION_CODES.O)
override fun onUserLeaveHint() {
super.onUserLeaveHint()
if (playerPreferences.autoPip) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
isPipSupported &&
playerPreferences.autoPip &&
player.isPlaying &&
!isControlsLocked
) {
this.enterPictureInPictureMode(updatePictureInPictureParams())
}
}
Expand All @@ -325,7 +333,7 @@ class PlayerActivity : AppCompatActivity() {

@RequiresApi(Build.VERSION_CODES.O)
private fun updatePictureInPictureParams(): PictureInPictureParams {
var params: PictureInPictureParams = PictureInPictureParams.Builder()
val params: PictureInPictureParams = PictureInPictureParams.Builder()
.setAspectRatio(Rational(16, 9))
.build()

Expand Down Expand Up @@ -390,21 +398,25 @@ class PlayerActivity : AppCompatActivity() {

subtitleView?.apply {
val captioningManager = getSystemService(Context.CAPTIONING_SERVICE) as CaptioningManager
val systemCaptionStyle = CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle)
val userStyle = CaptionStyleCompat(
Color.WHITE,
Color.BLACK.takeIf { playerPreferences.subtitleBackground } ?: Color.TRANSPARENT,
Color.TRANSPARENT,
CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW,
Color.BLACK,
Typeface.create(
playerPreferences.subtitleFont.toTypeface(),
Typeface.BOLD.takeIf { playerPreferences.subtitleTextBold } ?: Typeface.NORMAL
if (playerPreferences.useSystemCaptionStyle) {
val systemCaptionStyle = CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle)
setStyle(systemCaptionStyle)
} else {
val userStyle = CaptionStyleCompat(
Color.WHITE,
Color.BLACK.takeIf { playerPreferences.subtitleBackground } ?: Color.TRANSPARENT,
Color.TRANSPARENT,
CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW,
Color.BLACK,
Typeface.create(
playerPreferences.subtitleFont.toTypeface(),
Typeface.BOLD.takeIf { playerPreferences.subtitleTextBold } ?: Typeface.NORMAL
)
)
)
setStyle(systemCaptionStyle.takeIf { playerPreferences.useSystemCaptionStyle } ?: userStyle)
setStyle(userStyle)
setFixedTextSize(TypedValue.COMPLEX_UNIT_SP, playerPreferences.subtitleTextSize.toFloat())
}
setApplyEmbeddedStyles(playerPreferences.applyEmbeddedStyles)
setFixedTextSize(TypedValue.COMPLEX_UNIT_SP, playerPreferences.subtitleTextSize.toFloat())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import dev.anilbeesetti.nextplayer.core.data.models.VideoState
import dev.anilbeesetti.nextplayer.core.data.repository.MediaRepository
import dev.anilbeesetti.nextplayer.core.data.repository.PreferencesRepository
import dev.anilbeesetti.nextplayer.core.domain.GetSortedPlaylistUseCase
import dev.anilbeesetti.nextplayer.core.model.ApplicationPreferences
import dev.anilbeesetti.nextplayer.core.model.PlayerPreferences
import dev.anilbeesetti.nextplayer.core.model.Resume
import dev.anilbeesetti.nextplayer.core.model.VideoZoom
import dev.anilbeesetti.nextplayer.feature.player.extensions.isSchemaContent
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import timber.log.Timber

private const val END_POSITION_OFFSET = 5L
Expand All @@ -41,13 +41,13 @@ class PlayerViewModel @Inject constructor(
val playerPrefs = preferencesRepository.playerPreferences.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = PlayerPreferences()
initialValue = runBlocking { preferencesRepository.playerPreferences.first() }
)

val appPrefs = preferencesRepository.applicationPreferences.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = ApplicationPreferences()
initialValue = runBlocking { preferencesRepository.applicationPreferences.first() }
)

suspend fun updateState(uri: String?) {
Expand Down
Loading