From d2c25d6caa93b35fb9d5c768186222ad766c531e Mon Sep 17 00:00:00 2001 From: Quickdesh <36792807+Quickdesh@users.noreply.github.com> Date: Wed, 27 Apr 2022 23:28:55 +0900 Subject: [PATCH] Few PiP fixes (#552) --- .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 2 + .../tachiyomi/ui/player/PlayerActivity.kt | 89 ++++++++++++------- .../tachiyomi/ui/player/PlayerControlsView.kt | 2 - .../ui/setting/SettingsPlayerController.kt | 9 ++ app/src/main/res/layout/player_controls.xml | 1 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 73 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 107437b01..3bcd6c916 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -11,6 +11,8 @@ object PreferenceKeys { const val defaultReadingMode = "pref_default_reading_mode_key" + const val pipEpisodeToasts = "pref_pip_episode_toasts" + const val defaultOrientationType = "pref_default_orientation_type_key" const val defaultPlayerOrientationType = "pref_default_player_orientation_type_key" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index db0abaa77..226d75cb3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -118,6 +118,8 @@ class PreferencesHelper(val context: Context) { fun defaultReadingMode() = prefs.getInt(Keys.defaultReadingMode, ReadingModeType.RIGHT_TO_LEFT.flagValue) + fun pipEpisodeToasts() = prefs.getBoolean(Keys.pipEpisodeToasts, true) + fun defaultOrientationType() = prefs.getInt(Keys.defaultOrientationType, OrientationType.FREE.flagValue) fun defaultPlayerOrientationType() = prefs.getInt(Keys.defaultPlayerOrientationType, OrientationType.FREE.flagValue) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt index 9732e55f4..9f139a4c4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt @@ -92,6 +92,7 @@ class PlayerActivity : } private var isInPipMode: Boolean = false + private var isPipStarted: Boolean = false private var mReceiver: BroadcastReceiver? = null @@ -311,10 +312,46 @@ class PlayerActivity : } presenter.init(anime, episode) } + val dm = DisplayMetrics() + windowManager.defaultDisplay.getRealMetrics(dm) + width = dm.widthPixels + height = dm.heightPixels + if (width <= height) { + switchOrientation(false) + } else { + switchOrientation(true) + } playerIsDestroyed = false } + /** + * Function to handle UI during orientation changes + */ + + private fun switchOrientation(isLandscape: Boolean) { + launchUI { + if (isLandscape) { + if (width <= height) { + width = height.also { height = width } + } + binding.playerControls.binding.controlsTopLandscape.visibility = View.VISIBLE + binding.playerControls.binding.controlsTopPortrait.visibility = View.GONE + setupGestures() + setViewMode() + } else { + if (width >= height) { + width = height.also { height = width } + } + binding.playerControls.binding.controlsTopPortrait.visibility = View.VISIBLE + binding.playerControls.binding.controlsTopLandscape.visibility = View.GONE + setupGestures() + setViewMode() + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) player.paused?.let { updatePictureInPictureActions(!it) } + } + } + /** * Sets up the gestures to be used */ @@ -322,11 +359,6 @@ class PlayerActivity : @Suppress("DEPRECATION") @SuppressLint("ClickableViewAccessibility") private fun setupGestures() { - val dm = DisplayMetrics() - windowManager.defaultDisplay.getRealMetrics(dm) - width = dm.widthPixels - height = dm.heightPixels - val gestures = Gestures(this, width.toFloat(), height.toFloat()) mDetector = GestureDetectorCompat(this, gestures) player.setOnTouchListener { v, event -> @@ -337,7 +369,13 @@ class PlayerActivity : override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - if (newConfig.orientation == ORIENTATION_PORTRAIT || newConfig.orientation == ORIENTATION_LANDSCAPE) launchUI { setupGestures(); setViewMode() } + if (!isPipStarted) { + if (newConfig.orientation == ORIENTATION_LANDSCAPE) { + switchOrientation(true) + } else if (newConfig.orientation == ORIENTATION_PORTRAIT) { + switchOrientation(false) + } + } } /** @@ -363,7 +401,7 @@ class PlayerActivity : when { epTxt == "Invalid" -> return epTxt == null -> { launchUI { toast(errorRes) }; showLoadingIndicator(false) } - isInPipMode -> launchUI { toast(epTxt) } + isInPipMode -> if (preferences.pipEpisodeToasts()) launchUI { toast(epTxt) } } } @@ -445,12 +483,8 @@ class PlayerActivity : fun rotatePlayer(view: View) { if (this.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) { this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE - binding.playerControls.binding.controlsTopLandscape.isVisible = true - binding.playerControls.binding.controlsTopPortrait.isVisible = false } else { this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT - binding.playerControls.binding.controlsTopLandscape.isVisible = false - binding.playerControls.binding.controlsTopPortrait.isVisible = true } } @@ -773,7 +807,7 @@ class PlayerActivity : @RequiresApi(Build.VERSION_CODES.O) override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration?) { isInPipMode = isInPictureInPictureMode - + isPipStarted = isInPipMode super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) if (isInPictureInPictureMode) { @@ -796,11 +830,9 @@ class PlayerActivity : } CONTROL_TYPE_PREVIOUS -> { switchEpisode(true) - player.paused?.let { updatePictureInPictureActions(!it) } } CONTROL_TYPE_NEXT -> { switchEpisode(false) - player.paused?.let { updatePictureInPictureActions(!it) } } } } @@ -816,9 +848,10 @@ class PlayerActivity : } } - @Suppress("DEPRECATION") - internal fun startPiP() { + @Suppress("DEPRECATION", "UNUSED_PARAMETER") + fun startPiP(view: View) { if (packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + isPipStarted = true player.paused?.let { updatePictureInPictureActions(!it) } ?.let { this.enterPictureInPictureMode(it) } } @@ -886,7 +919,7 @@ class PlayerActivity : ), ) - .setAspectRatio(player.videoAspect?.times(10000)?.let { Rational(it.toInt(), 10000) }) + .setAspectRatio(MPVLib.getPropertyInt("video-params/dw")?.let { width -> MPVLib.getPropertyInt("video-params/dh")?.let { height -> Rational(width, height) } }) .build() setPictureInPictureParams(mPictureInPictureParams) return mPictureInPictureParams @@ -1046,22 +1079,16 @@ class PlayerActivity : .coerceAtLeast(0) } } - launchUI { showLoadingIndicator(false) } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) player.paused?.let { updatePictureInPictureActions(!it) } - if (player.videoW!! / player.videoH!! >= 1) { - this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE - launchUI { - binding.playerControls.binding.controlsTopLandscape.visibility = View.VISIBLE - binding.playerControls.binding.controlsTopPortrait.visibility = View.GONE - } - } else { - this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT - launchUI { - binding.playerControls.binding.controlsTopPortrait.visibility = View.VISIBLE - binding.playerControls.binding.controlsTopLandscape.visibility = View.GONE + launchUI { + showLoadingIndicator(false) + if (player.videoW!! / player.videoH!! >= 1) { + this@PlayerActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + switchOrientation(true) + } else { + this@PlayerActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT + switchOrientation(false) } } - launchUI { setupGestures(); setViewMode() } } // mpv events diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerControlsView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerControlsView.kt index 7690c9a9b..ca80b9425 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerControlsView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerControlsView.kt @@ -71,8 +71,6 @@ class PlayerControlsView @JvmOverloads constructor(context: Context, attrs: Attr binding.backArrowBtnLandscape.setOnClickListener { activity.finishAndRemoveTask() } binding.backArrowBtnPortrait.setOnClickListener { activity.finishAndRemoveTask() } - binding.pipBtn.setOnClickListener { activity.startPiP() } - // Lock and Unlock controls binding.lockBtn.setOnClickListener { lockControls(true) } binding.unlockBtn.setOnClickListener { lockControls(false) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsPlayerController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsPlayerController.kt index 85886cbc8..a4250acd4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsPlayerController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsPlayerController.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.setting +import android.content.pm.PackageManager import android.os.Build import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R @@ -71,6 +72,14 @@ class SettingsPlayerController : SettingsController() { } } + if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + switchPreference { + key = Keys.pipEpisodeToasts + titleRes = R.string.pref_pip_episode_toasts + defaultValue = true + } + } + switchPreference { key = Keys.alwaysUseExternalPlayer titleRes = R.string.pref_always_use_external_player diff --git a/app/src/main/res/layout/player_controls.xml b/app/src/main/res/layout/player_controls.xml index 0daad0c0f..05b0b6458 100644 --- a/app/src/main/res/layout/player_controls.xml +++ b/app/src/main/res/layout/player_controls.xml @@ -392,6 +392,7 @@ android:layout_height="match_parent" android:background="?android:attr/selectableItemBackground" android:contentDescription="@string/action_player_pip" + android:onClick="startPiP" android:src="@drawable/ic_picture_in_picture_24dp" android:visibility="visible" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index af564adf2..74022bd40 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -444,6 +444,7 @@ 5s Always use external player Show content in display cutout + Show episode toasts when switching episodes in PiP mode External player preference 70% 75%