notification click && subtitles fix

This commit is contained in:
Bnyro 2022-06-18 21:44:22 +02:00
parent a56ff97665
commit fc2a001b45
4 changed files with 48 additions and 12 deletions

View File

@ -34,7 +34,8 @@
android:supportsPictureInPicture="true" android:supportsPictureInPicture="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:hardwareAccelerated="true" android:hardwareAccelerated="true"
android:screenOrientation="userPortrait"> android:screenOrientation="userPortrait"
android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />

View File

@ -77,7 +77,8 @@ class BackgroundMode {
DescriptionAdapter( DescriptionAdapter(
response?.title!!, response?.title!!,
response?.uploader!!, response?.uploader!!,
response?.thumbnailUrl!! response?.thumbnailUrl!!,
c
) )
) )
.build() .build()

View File

@ -721,11 +721,11 @@ class PlayerFragment : Fragment() {
} }
// create a list of subtitles // create a list of subtitles
val subtitle = mutableListOf<SubtitleConfiguration>() val subtitle = mutableListOf<SubtitleConfiguration>()
if (response.subtitles!!.isNotEmpty()) { response.subtitles!!.forEach {
subtitle.add( subtitle.add(
SubtitleConfiguration.Builder(response.subtitles[0].url!!.toUri()) SubtitleConfiguration.Builder(it.url!!.toUri())
.setMimeType(response.subtitles[0].mimeType!!) // The correct MIME type (required). .setMimeType(it.mimeType!!) // The correct MIME type (required).
.setLanguage(response.subtitles[0].code) // The subtitle language (optional). .setLanguage(it.code) // The subtitle language (optional).
.build() .build()
) )
} }
@ -867,7 +867,7 @@ class PlayerFragment : Fragment() {
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(c, 1, "background_mode") .Builder(c, 1, "background_mode")
.setMediaDescriptionAdapter( .setMediaDescriptionAdapter(
DescriptionAdapter(title, uploader, thumbnailUrl) DescriptionAdapter(title, uploader, thumbnailUrl, requireContext())
) )
.build() .build()

View File

@ -1,8 +1,11 @@
package com.github.libretube.util package com.github.libretube.util
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import com.github.libretube.MainActivity
import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.ui.PlayerNotificationManager
import java.net.URL import java.net.URL
@ -14,32 +17,58 @@ import java.net.URL
class DescriptionAdapter( class DescriptionAdapter(
private val title: String, private val title: String,
private val channelName: String, private val channelName: String,
private val thumbnailUrl: String private val thumbnailUrl: String,
private val context: Context
) : ) :
PlayerNotificationManager.MediaDescriptionAdapter { PlayerNotificationManager.MediaDescriptionAdapter {
/**
* sets the title of the notification
*/
override fun getCurrentContentTitle(player: Player): CharSequence { override fun getCurrentContentTitle(player: Player): CharSequence {
// return controller.metadata.description.title.toString() // return controller.metadata.description.title.toString()
return title return title
} }
/**
* overrides the action when clicking the notification
*/
override fun createCurrentContentIntent(player: Player): PendingIntent? { override fun createCurrentContentIntent(player: Player): PendingIntent? {
// return controller.sessionActivity // 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? { override fun getCurrentContentText(player: Player): CharSequence? {
// return controller.metadata.description.subtitle.toString() // return controller.metadata.description.subtitle.toString()
return channelName return channelName
} }
/**
* return the icon/thumbnail of the video
*/
override fun getCurrentLargeIcon( override fun getCurrentLargeIcon(
player: Player, player: Player,
callback: PlayerNotificationManager.BitmapCallback callback: PlayerNotificationManager.BitmapCallback
): Bitmap? { ): Bitmap? {
lateinit var bitmap: Bitmap lateinit var bitmap: Bitmap
/**
* running on a new thread to prevent a NetworkMainThreadException
*/
val thread = Thread { val thread = Thread {
try { try {
// try to parse the thumbnailUrl to a Bitmap /**
* try to GET the thumbnail from the URL
*/
val inputStream = URL(thumbnailUrl).openStream() val inputStream = URL(thumbnailUrl).openStream()
bitmap = BitmapFactory.decodeStream(inputStream) bitmap = BitmapFactory.decodeStream(inputStream)
} catch (ex: java.lang.Exception) { } catch (ex: java.lang.Exception) {
@ -48,9 +77,14 @@ class DescriptionAdapter(
} }
thread.start() thread.start()
thread.join() thread.join()
// return bitmap if initialized /**
* returns the scaled bitmap if it got fetched successfully
*/
return try { return try {
bitmap val resizedBitmap = Bitmap.createScaledBitmap(
bitmap, 1080, 1080, false
)
resizedBitmap
} catch (e: Exception) { } catch (e: Exception) {
null null
} }