mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
use coil for loading the notification image
This commit is contained in:
parent
32b5ae7094
commit
2f3075fab1
@ -5,18 +5,17 @@ import android.app.PendingIntent
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.drawable.BitmapDrawable
|
||||||
import android.support.v4.media.session.MediaSessionCompat
|
import android.support.v4.media.session.MediaSessionCompat
|
||||||
|
import coil.request.ImageRequest
|
||||||
import com.github.libretube.activities.MainActivity
|
import com.github.libretube.activities.MainActivity
|
||||||
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
||||||
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
||||||
import com.github.libretube.extensions.await
|
|
||||||
import com.github.libretube.obj.Streams
|
import com.github.libretube.obj.Streams
|
||||||
import com.google.android.exoplayer2.ExoPlayer
|
import com.google.android.exoplayer2.ExoPlayer
|
||||||
import com.google.android.exoplayer2.Player
|
import com.google.android.exoplayer2.Player
|
||||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
||||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class NowPlayingNotification(
|
class NowPlayingNotification(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@ -43,7 +42,7 @@ class NowPlayingNotification(
|
|||||||
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
||||||
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
||||||
*/
|
*/
|
||||||
inner class DescriptionAdapter() :
|
inner class DescriptionAdapter :
|
||||||
PlayerNotificationManager.MediaDescriptionAdapter {
|
PlayerNotificationManager.MediaDescriptionAdapter {
|
||||||
/**
|
/**
|
||||||
* sets the title of the notification
|
* sets the title of the notification
|
||||||
@ -83,36 +82,25 @@ class NowPlayingNotification(
|
|||||||
player: Player,
|
player: Player,
|
||||||
callback: PlayerNotificationManager.BitmapCallback
|
callback: PlayerNotificationManager.BitmapCallback
|
||||||
): Bitmap? {
|
): Bitmap? {
|
||||||
lateinit var bitmap: Bitmap
|
var resizedBitmap: Bitmap? = null
|
||||||
|
|
||||||
/**
|
val request = ImageRequest.Builder(context)
|
||||||
* running on a new thread to prevent a NetworkMainThreadException
|
.data(streams?.thumbnailUrl)
|
||||||
*/
|
.target { result ->
|
||||||
Thread {
|
val bitmap = (result as BitmapDrawable).bitmap
|
||||||
try {
|
resizedBitmap = Bitmap.createScaledBitmap(
|
||||||
/**
|
bitmap,
|
||||||
* try to GET the thumbnail from the URL
|
bitmap.width,
|
||||||
*/
|
bitmap.width,
|
||||||
val inputStream = URL(streams?.thumbnailUrl).openStream()
|
false
|
||||||
bitmap = BitmapFactory.decodeStream(inputStream)
|
)
|
||||||
} catch (ex: java.lang.Exception) {
|
|
||||||
ex.printStackTrace()
|
|
||||||
}
|
}
|
||||||
}.await()
|
.build()
|
||||||
/**
|
|
||||||
* returns the scaled bitmap if it got fetched successfully
|
ImageHelper.imageLoader.enqueue(request)
|
||||||
*/
|
|
||||||
return try {
|
// returns the scaled bitmap if it got fetched successfully
|
||||||
val resizedBitmap = Bitmap.createScaledBitmap(
|
return resizedBitmap
|
||||||
bitmap,
|
|
||||||
bitmap.width,
|
|
||||||
bitmap.width,
|
|
||||||
false
|
|
||||||
)
|
|
||||||
resizedBitmap
|
|
||||||
} catch (e: Exception) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user