From fa59ff799a4b7f1092ed6bc9514dff526eaf1af4 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 5 Jun 2022 11:42:33 +0200 Subject: [PATCH] cleanup --- app/src/main/AndroidManifest.xml | 2 +- .../com/github/libretube/BackgroundMode.kt | 2 +- .../github/libretube/{myApp.kt => MyApp.kt} | 4 +- .../libretube/fragments/PlayerFragment.kt | 61 ++++++++++--------- 4 files changed, 36 insertions(+), 33 deletions(-) rename app/src/main/java/com/github/libretube/{myApp.kt => MyApp.kt} (94%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7cc95d66a..cf45f4b9c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,7 +15,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MY" - android:name=".myApp" + android:name=".MyApp" android:networkSecurityConfig="@xml/network_security_config" android:largeHeap="true" android:requestLegacyExternalStorage="true" diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index c85e4fd06..12f80aff7 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -68,7 +68,7 @@ class BackgroundMode { */ private fun initializePlayerNotification(c: Context) { playerNotification = PlayerNotificationManager - .Builder(c, 1, "background_mode").build() + .Builder(c, NOTIFICATION_ID, "background_mode").build() playerNotification.setPlayer(player) playerNotification.setUsePreviousAction(false) playerNotification.setUseNextAction(false) diff --git a/app/src/main/java/com/github/libretube/myApp.kt b/app/src/main/java/com/github/libretube/MyApp.kt similarity index 94% rename from app/src/main/java/com/github/libretube/myApp.kt rename to app/src/main/java/com/github/libretube/MyApp.kt index d5eb50444..7c37bbee6 100644 --- a/app/src/main/java/com/github/libretube/myApp.kt +++ b/app/src/main/java/com/github/libretube/MyApp.kt @@ -5,7 +5,9 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.os.Build -class myApp : Application() { +const val NOTIFICATION_ID = 1 + +class MyApp : Application() { override fun onCreate() { super.onCreate() 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 f8a86cad6..8a75223aa 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -42,6 +42,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.IS_DOWNLOAD_RUNNING import com.github.libretube.MainActivity +import com.github.libretube.NOTIFICATION_ID import com.github.libretube.R import com.github.libretube.SponsorBlockSettings import com.github.libretube.adapters.CommentsAdapter @@ -118,7 +119,6 @@ class PlayerFragment : Fragment() { private lateinit var mediaSession: MediaSessionCompat private lateinit var mediaSessionConnector: MediaSessionConnector private lateinit var playerNotification: PlayerNotificationManager - private val notificationId = 1 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -332,12 +332,15 @@ class PlayerFragment : Fragment() { override fun onDestroy() { super.onDestroy() try { - exoPlayer.release() - // kill notification - val nManager = context?.getSystemService( + mediaSession.isActive = false + mediaSession.release() + mediaSessionConnector.setPlayer(null) + playerNotification.setPlayer(null) + val notificationManager = context?.getSystemService( Context.NOTIFICATION_SERVICE ) as NotificationManager - nManager.cancel(notificationId) + notificationManager.cancel(NOTIFICATION_ID) + exoPlayer.release() } catch (e: Exception) { } } @@ -430,13 +433,15 @@ class PlayerFragment : Fragment() { } private fun prepareExoPlayerView() { - exoPlayerView.setShowSubtitleButton(true) - exoPlayerView.setShowNextButton(false) - exoPlayerView.setShowPreviousButton(false) - exoPlayerView.setRepeatToggleModes(RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) - // exoPlayerView.controllerShowTimeoutMs = 1500 - exoPlayerView.controllerHideOnTouch = true - exoPlayerView.player = exoPlayer + exoPlayerView.apply { + setShowSubtitleButton(true) + setShowNextButton(false) + setShowPreviousButton(false) + setRepeatToggleModes(RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) + // controllerShowTimeoutMs = 1500 + controllerHideOnTouch = true + player = exoPlayer + } } private fun initializePlayerView(view: View, response: Streams) { @@ -793,33 +798,29 @@ class PlayerFragment : Fragment() { .build() exoPlayer.setAudioAttributes(audioAttributes, true) - - setMediaItem(requireContext()) initializePlayerNotification(requireContext()) } - private fun setMediaItem(c: Context) { + private fun initializePlayerNotification(c: Context) { + mediaSession = MediaSessionCompat(c, this.javaClass.name) - mediaSession.isActive = true - /* might be useful for setting the notification title - mediaSession.setMetadata(MediaMetadataCompat.Builder() - .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "") - .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, "") - .build() - ) - */ + mediaSession.apply { + isActive = true + } + mediaSessionConnector = MediaSessionConnector(mediaSession) mediaSessionConnector.setPlayer(exoPlayer) - } - private fun initializePlayerNotification(c: Context) { playerNotification = PlayerNotificationManager - .Builder(c, notificationId, "background_mode") + .Builder(c, NOTIFICATION_ID, "background_mode") .build() - playerNotification.setPlayer(exoPlayer) - playerNotification.setUseNextAction(false) - playerNotification.setUsePreviousAction(false) - playerNotification.setMediaSessionToken(mediaSession.sessionToken) + + playerNotification.apply { + setPlayer(exoPlayer) + setUseNextAction(false) + setUsePreviousAction(false) + setMediaSessionToken(mediaSession.sessionToken) + } } private fun isSubscribed(button: MaterialButton, channel_id: String) {