From 0c5b4c51dedacdaff6a52e0f4d3d635ca207010d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 26 Jul 2022 07:55:07 +0200 Subject: [PATCH] cleanup --- .../libretube/fragments/PlayerFragment.kt | 25 ++++++++++++++++--- .../{KeyboardHelper.kt => HideKeyboard.kt} | 0 2 files changed, 21 insertions(+), 4 deletions(-) rename app/src/main/java/com/github/libretube/util/{KeyboardHelper.kt => HideKeyboard.kt} (100%) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 84ba5fb56..7947ba2ed 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -1,5 +1,6 @@ package com.github.libretube.fragments +import android.app.ActivityManager import android.app.NotificationManager import android.app.PictureInPictureParams import android.content.Context @@ -57,6 +58,7 @@ import com.github.libretube.obj.Streams import com.github.libretube.obj.Subscribe import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys +import com.github.libretube.services.BackgroundMode import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.CronetHelper @@ -1708,14 +1710,29 @@ class PlayerFragment : Fragment() { } fun onUserLeaveHint() { + if (SDK_INT >= Build.VERSION_CODES.O && shouldStartPiP()) { + activity?.enterPictureInPictureMode(updatePipParams()) + } + } + + private fun shouldStartPiP(): Boolean { val bounds = Rect() binding.playerScrollView.getHitRect(bounds) - if (SDK_INT >= Build.VERSION_CODES.O && - (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN) - ) { - activity?.enterPictureInPictureMode(updatePipParams()) + val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java) + + return (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN) && + (exoPlayer.isPlaying || !backgroundModeRunning) + } + + private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean { + val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + for (service in manager.getRunningServices(Int.MAX_VALUE)) { + if (serviceClass.name == service.service.className) { + return true + } } + return false } private fun updatePipParams() = PictureInPictureParams.Builder() diff --git a/app/src/main/java/com/github/libretube/util/KeyboardHelper.kt b/app/src/main/java/com/github/libretube/util/HideKeyboard.kt similarity index 100% rename from app/src/main/java/com/github/libretube/util/KeyboardHelper.kt rename to app/src/main/java/com/github/libretube/util/HideKeyboard.kt