notification thumbnail and description

This commit is contained in:
Bnyro 2022-06-14 12:00:58 +02:00
parent dfde1354d6
commit 2a8ae96feb
2 changed files with 102 additions and 24 deletions

View File

@ -58,6 +58,7 @@ import com.github.libretube.obj.Streams
import com.github.libretube.obj.Subscribe import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.SponsorBlockSettings import com.github.libretube.preferences.SponsorBlockSettings
import com.github.libretube.util.CronetHelper import com.github.libretube.util.CronetHelper
import com.github.libretube.util.DescriptionAdapter
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.google.android.exoplayer2.C import com.google.android.exoplayer2.C
@ -85,11 +86,11 @@ import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView import com.google.android.material.card.MaterialCardView
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import org.chromium.net.CronetEngine
import retrofit2.HttpException
import java.io.IOException import java.io.IOException
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.abs import kotlin.math.abs
import org.chromium.net.CronetEngine
import retrofit2.HttpException
var isFullScreen = false var isFullScreen = false
@ -123,6 +124,10 @@ class PlayerFragment : Fragment() {
private lateinit var mediaSessionConnector: MediaSessionConnector private lateinit var mediaSessionConnector: MediaSessionConnector
private lateinit var playerNotification: PlayerNotificationManager private lateinit var playerNotification: PlayerNotificationManager
private lateinit var title: String
private lateinit var uploader: String
private lateinit var thumbnailUrl: String
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
@ -209,7 +214,7 @@ class PlayerFragment : Fragment() {
playerMotionLayout.progress = 1.toFloat() playerMotionLayout.progress = 1.toFloat()
playerMotionLayout.transitionToStart() playerMotionLayout.transitionToStart()
fetchJson(view) fetchJsonAndInitPlayer(view)
view.findViewById<ImageView>(R.id.close_imageView).setOnClickListener { view.findViewById<ImageView>(R.id.close_imageView).setOnClickListener {
motionLayout.transitionToEnd() motionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
@ -373,7 +378,7 @@ class PlayerFragment : Fragment() {
} }
} }
private fun fetchJson(view: View) { private fun fetchJsonAndInitPlayer(view: View) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
@ -388,6 +393,40 @@ class PlayerFragment : Fragment() {
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} }
// for the notification description adapter
title = response.title!!
uploader = response.uploader!!
thumbnailUrl = response.thumbnailUrl!!
// check whether related streams are enabled
val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext())
relatedStreamsEnabled = sharedPreferences.getBoolean("related_streams_toggle", true)
runOnUiThread {
createExoPlayer(view)
prepareExoPlayerView()
if (response.chapters != null) initializeChapters(response.chapters)
setResolutionAndSubtitles(view, response)
// support for time stamped links
if (arguments?.getLong("timeStamp") != null) {
val position = arguments?.getLong("timeStamp")!! * 1000
exoPlayer.seekTo(position)
}
exoPlayer.prepare()
exoPlayer.play()
initializePlayerView(view, response)
initializePlayerNotification(requireContext())
fetchSponsorBlockSegments()
if (!relatedStreamsEnabled) toggleComments()
}
}
}
run()
}
private fun fetchSponsorBlockSegments() {
fun run() {
lifecycleScope.launchWhenCreated {
if (SponsorBlockSettings.sponsorBlockEnabled) { if (SponsorBlockSettings.sponsorBlockEnabled) {
val categories: ArrayList<String> = arrayListOf() val categories: ArrayList<String> = arrayListOf()
if (SponsorBlockSettings.introEnabled) { if (SponsorBlockSettings.introEnabled) {
@ -426,26 +465,6 @@ class PlayerFragment : Fragment() {
} }
} }
} }
// check whether related streams are enabled
val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext())
relatedStreamsEnabled = sharedPreferences.getBoolean("related_streams_toggle", true)
runOnUiThread {
createExoPlayer(view)
prepareExoPlayerView()
if (response.chapters != null) initializeChapters(response.chapters)
setResolutionAndSubtitles(view, response)
// support for time stamped links
if (arguments?.getLong("timeStamp") != null) {
val position = arguments?.getLong("timeStamp")!! * 1000
exoPlayer.seekTo(position)
}
exoPlayer.prepare()
exoPlayer.play()
initializePlayerView(view, response)
initializePlayerNotification(requireContext())
if (!relatedStreamsEnabled) toggleComments()
}
} }
} }
run() run()
@ -863,6 +882,9 @@ class PlayerFragment : Fragment() {
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(c, 1, "background_mode") .Builder(c, 1, "background_mode")
.setMediaDescriptionAdapter(
DescriptionAdapter(title, uploader, thumbnailUrl)
)
.build() .build()
playerNotification.apply { playerNotification.apply {

View File

@ -0,0 +1,56 @@
package com.github.libretube.util
import android.app.PendingIntent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.PlayerNotificationManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.URL
// used to show title and thumbnail of the video in the notification
class DescriptionAdapter(
private val title: String,
private val channelName: String,
private val thumbnailUrl: String) :
PlayerNotificationManager.MediaDescriptionAdapter {
override fun getCurrentContentTitle(player: Player): CharSequence {
// return controller.metadata.description.title.toString()
return title
}
override fun createCurrentContentIntent(player: Player): PendingIntent? {
// return controller.sessionActivity
return null
}
override fun getCurrentContentText(player: Player): CharSequence? {
// return controller.metadata.description.subtitle.toString()
return channelName
}
override fun getCurrentLargeIcon(
player: Player,
callback: PlayerNotificationManager.BitmapCallback
): Bitmap? {
lateinit var bitmap: Bitmap
val thread = Thread {
try {
val inputStream = URL(thumbnailUrl).openStream()
bitmap = BitmapFactory.decodeStream(inputStream)
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
}
thread.start()
thread.join()
return try {
bitmap
} catch (e: Exception) {
null
}
}
}