From bf0d6bde2cad67aa7b5bcfaebf2cddd50a5ae9fd Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 26 Feb 2024 12:33:30 +0100 Subject: [PATCH] chore: bump target sdk to 34 and fix pip issues --- app/build.gradle.kts | 2 +- .../github/libretube/helpers/PlayerHelper.kt | 13 ++++++----- .../ui/fragments/DownloadsFragment.kt | 11 ++++++---- .../libretube/ui/fragments/PlayerFragment.kt | 22 ++++++++++--------- .../libretube/util/NowPlayingNotification.kt | 11 +++++++--- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 312928e1b..1fdde0184 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -14,7 +14,7 @@ android { defaultConfig { applicationId = "com.github.libretube" minSdk = 21 - targetSdk = 33 + targetSdk = 34 versionCode = 47 versionName = "0.21.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index a6e6cd64a..b36fe9a55 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -221,10 +221,11 @@ object PlayerHelper { false ) - private val behaviorWhenMinimized = PreferenceHelper.getString( - PreferenceKeys.BEHAVIOR_WHEN_MINIMIZED, - "pip" - ) + private val behaviorWhenMinimized + get() = PreferenceHelper.getString( + PreferenceKeys.BEHAVIOR_WHEN_MINIMIZED, + "pip" + ) val pipEnabled: Boolean get() = behaviorWhenMinimized == "pip" @@ -386,7 +387,9 @@ object PlayerHelper { } private fun getPendingIntent(activity: Activity, event: PlayerEvent): PendingIntent { - val intent = Intent(getIntentAction(activity)).putExtra(CONTROL_TYPE, event) + val intent = Intent(getIntentAction(activity)) + .setPackage(activity.packageName) + .putExtra(CONTROL_TYPE, event) return PendingIntentCompat.getBroadcast(activity, event.ordinal, intent, 0, false)!! } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index 3aa6fcd39..4de9a64ea 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -10,6 +10,7 @@ import android.os.IBinder import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.content.ContextCompat import androidx.core.view.isGone import androidx.core.view.isInvisible import androidx.core.view.isVisible @@ -193,10 +194,12 @@ class DownloadsFragment : DynamicLayoutManagerFragment() { override fun onResume() { super.onResume() - val filter = IntentFilter() - filter.addAction(DownloadService.ACTION_SERVICE_STARTED) - filter.addAction(DownloadService.ACTION_SERVICE_STOPPED) - context?.registerReceiver(downloadReceiver, filter) + + val filter = IntentFilter().apply { + addAction(DownloadService.ACTION_SERVICE_STARTED) + addAction(DownloadService.ACTION_SERVICE_STOPPED) + } + ContextCompat.registerReceiver(requireContext(), downloadReceiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED) } fun bindDownloadService(ids: IntArray? = null) { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index a008563bf..53e73a5d5 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -23,6 +23,7 @@ import android.view.ViewGroup.LayoutParams import android.widget.Toast import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.TransitionAdapter +import androidx.core.content.ContextCompat import androidx.core.content.getSystemService import androidx.core.graphics.drawable.toDrawable import androidx.core.net.toUri @@ -371,9 +372,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playerLayoutOrientation = resources.configuration.orientation // broadcast receiver for PiP actions - context?.registerReceiver( + ContextCompat.registerReceiver( + requireContext(), broadcastReceiver, - IntentFilter(PlayerHelper.getIntentAction(requireContext())) + IntentFilter(PlayerHelper.getIntentAction(requireContext())), + ContextCompat.RECEIVER_NOT_EXPORTED ) fullscreenResolution = PlayerHelper.getDefaultResolution(requireContext(), true) @@ -595,6 +598,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playOnBackground() } + binding.relPlayerPip.isVisible = PictureInPictureCompat.isPictureInPictureAvailable(requireContext()) + binding.relPlayerPip.setOnClickListener { PictureInPictureCompat.enterPictureInPictureMode(requireActivity(), pipParams) } @@ -1586,11 +1591,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { } fun onUserLeaveHint() { - if (PlayerHelper.pipEnabled && shouldStartPiP()) { + if (shouldStartPiP()) { PictureInPictureCompat.enterPictureInPictureMode(requireActivity(), pipParams) - return - } - if (PlayerHelper.pauseOnQuit) { + } else if (PlayerHelper.pauseOnQuit) { exoPlayer.pause() } } @@ -1625,14 +1628,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { /** * Detect whether PiP is supported and enabled */ - private fun usePiP(): Boolean { + private fun shouldUsePip(): Boolean { return PictureInPictureCompat.isPictureInPictureAvailable(requireContext()) && PlayerHelper.pipEnabled } private fun shouldStartPiP(): Boolean { - return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning( - requireContext() - ) + return shouldUsePip() && exoPlayer.isPlaying && + !BackgroundHelper.isBackgroundServiceRunning(requireContext()) } private fun killPlayerFragment() { diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index 8669c8bb5..d30197f29 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -14,6 +14,7 @@ import android.support.v4.media.session.PlaybackStateCompat import androidx.annotation.DrawableRes import androidx.core.app.NotificationCompat import androidx.core.app.PendingIntentCompat +import androidx.core.content.ContextCompat import androidx.core.content.getSystemService import androidx.core.graphics.drawable.toBitmap import androidx.media.app.NotificationCompat.MediaStyle @@ -92,7 +93,8 @@ class NowPlayingNotification( } private fun createIntent(action: String): PendingIntent? { - val intent = Intent(action).setPackage(context.packageName) + val intent = Intent(action) + .setPackage(context.packageName) return PendingIntentCompat .getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false) @@ -374,9 +376,12 @@ class NowPlayingNotification( } private fun createActionReceiver() { - listOf(PREV, NEXT, REWIND, FORWARD, PLAY_PAUSE, STOP).forEach { - context.registerReceiver(notificationActionReceiver, IntentFilter(it)) + val filter = IntentFilter().apply { + listOf(PREV, NEXT, REWIND, FORWARD, PLAY_PAUSE, STOP).forEach { + addAction(it) + } } + ContextCompat.registerReceiver(context, notificationActionReceiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED) } /**