diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f24097dde..839c395a0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,7 +34,8 @@ android:supportsPictureInPicture="true" android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" android:hardwareAccelerated="true" - android:screenOrientation="userPortrait"> + android:screenOrientation="userPortrait" + android:launchMode="singleTop"> diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index ebd851e01..79ff8a319 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -77,7 +77,8 @@ class BackgroundMode { DescriptionAdapter( response?.title!!, response?.uploader!!, - response?.thumbnailUrl!! + response?.thumbnailUrl!!, + c ) ) .build() 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 55b74e3b3..ec7728127 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -721,11 +721,11 @@ class PlayerFragment : Fragment() { } // create a list of subtitles val subtitle = mutableListOf() - if (response.subtitles!!.isNotEmpty()) { + response.subtitles!!.forEach { subtitle.add( - SubtitleConfiguration.Builder(response.subtitles[0].url!!.toUri()) - .setMimeType(response.subtitles[0].mimeType!!) // The correct MIME type (required). - .setLanguage(response.subtitles[0].code) // The subtitle language (optional). + SubtitleConfiguration.Builder(it.url!!.toUri()) + .setMimeType(it.mimeType!!) // The correct MIME type (required). + .setLanguage(it.code) // The subtitle language (optional). .build() ) } @@ -867,7 +867,7 @@ class PlayerFragment : Fragment() { playerNotification = PlayerNotificationManager .Builder(c, 1, "background_mode") .setMediaDescriptionAdapter( - DescriptionAdapter(title, uploader, thumbnailUrl) + DescriptionAdapter(title, uploader, thumbnailUrl, requireContext()) ) .build() diff --git a/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt b/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt index 25ed99fbb..ad8feb7ae 100644 --- a/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt @@ -1,8 +1,11 @@ package com.github.libretube.util import android.app.PendingIntent +import android.content.Context +import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory +import com.github.libretube.MainActivity import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.ui.PlayerNotificationManager import java.net.URL @@ -14,32 +17,58 @@ import java.net.URL class DescriptionAdapter( private val title: String, private val channelName: String, - private val thumbnailUrl: String + private val thumbnailUrl: String, + private val context: Context ) : PlayerNotificationManager.MediaDescriptionAdapter { + /** + * sets the title of the notification + */ override fun getCurrentContentTitle(player: Player): CharSequence { // return controller.metadata.description.title.toString() return title } + /** + * overrides the action when clicking the notification + */ override fun createCurrentContentIntent(player: Player): PendingIntent? { // return controller.sessionActivity - return null + /** + * starts a new MainActivity Intent when the player notification is clicked + * it doesn't start a completely new MainActivity because the MainActivity's launchMode + * is set to "singleTop" in the AndroidManifest (important!!!) + * that's the only way to launch back into the previous activity (e.g. the player view + */ + val intent = Intent(context, MainActivity::class.java) + return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) } + /** + * the description of the notification (below the title) + */ override fun getCurrentContentText(player: Player): CharSequence? { // return controller.metadata.description.subtitle.toString() return channelName } + /** + * return the icon/thumbnail of the video + */ override fun getCurrentLargeIcon( player: Player, callback: PlayerNotificationManager.BitmapCallback ): Bitmap? { lateinit var bitmap: Bitmap + + /** + * running on a new thread to prevent a NetworkMainThreadException + */ val thread = Thread { try { - // try to parse the thumbnailUrl to a Bitmap + /** + * try to GET the thumbnail from the URL + */ val inputStream = URL(thumbnailUrl).openStream() bitmap = BitmapFactory.decodeStream(inputStream) } catch (ex: java.lang.Exception) { @@ -48,9 +77,14 @@ class DescriptionAdapter( } thread.start() thread.join() - // return bitmap if initialized + /** + * returns the scaled bitmap if it got fetched successfully + */ return try { - bitmap + val resizedBitmap = Bitmap.createScaledBitmap( + bitmap, 1080, 1080, false + ) + resizedBitmap } catch (e: Exception) { null }