LibreTube/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt

96 lines
3.1 KiB
Kotlin
Raw Normal View History

2022-06-14 15:30:58 +05:30
package com.github.libretube.util
import android.app.PendingIntent
2022-06-19 01:14:22 +05:30
import android.content.Context
import android.content.Intent
2022-06-14 15:30:58 +05:30
import android.graphics.Bitmap
import android.graphics.BitmapFactory
2022-07-01 20:24:20 +05:30
import com.github.libretube.activities.MainActivity
2022-06-14 15:30:58 +05:30
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.PlayerNotificationManager
import java.net.URL
2022-06-14 22:38:02 +05:30
/**
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
*/
2022-06-14 15:30:58 +05:30
class DescriptionAdapter(
private val title: String,
private val channelName: String,
2022-06-19 01:14:22 +05:30
private val thumbnailUrl: String,
private val context: Context
) :
2022-06-14 15:30:58 +05:30
PlayerNotificationManager.MediaDescriptionAdapter {
2022-06-19 01:14:22 +05:30
/**
* sets the title of the notification
*/
2022-06-14 15:30:58 +05:30
override fun getCurrentContentTitle(player: Player): CharSequence {
// return controller.metadata.description.title.toString()
return title
}
2022-06-19 01:14:22 +05:30
/**
* overrides the action when clicking the notification
*/
2022-06-14 15:30:58 +05:30
override fun createCurrentContentIntent(player: Player): PendingIntent? {
// return controller.sessionActivity
2022-06-19 01:14:22 +05:30
/**
* 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)
2022-06-14 15:30:58 +05:30
}
2022-06-19 01:14:22 +05:30
/**
* the description of the notification (below the title)
*/
2022-06-14 15:30:58 +05:30
override fun getCurrentContentText(player: Player): CharSequence? {
// return controller.metadata.description.subtitle.toString()
return channelName
}
2022-06-19 01:14:22 +05:30
/**
* return the icon/thumbnail of the video
*/
2022-06-14 15:30:58 +05:30
override fun getCurrentLargeIcon(
player: Player,
callback: PlayerNotificationManager.BitmapCallback
): Bitmap? {
lateinit var bitmap: Bitmap
2022-06-19 01:14:22 +05:30
/**
* running on a new thread to prevent a NetworkMainThreadException
*/
2022-06-14 15:30:58 +05:30
val thread = Thread {
try {
2022-06-19 01:14:22 +05:30
/**
* try to GET the thumbnail from the URL
*/
2022-06-14 15:30:58 +05:30
val inputStream = URL(thumbnailUrl).openStream()
bitmap = BitmapFactory.decodeStream(inputStream)
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
}
thread.start()
thread.join()
2022-06-19 01:14:22 +05:30
/**
* returns the scaled bitmap if it got fetched successfully
*/
2022-06-14 15:30:58 +05:30
return try {
2022-06-19 01:14:22 +05:30
val resizedBitmap = Bitmap.createScaledBitmap(
2022-06-24 20:56:36 +05:30
bitmap,
2022-06-30 19:32:55 +05:30
bitmap.width,
bitmap.width,
2022-06-24 20:56:36 +05:30
false
2022-06-19 01:14:22 +05:30
)
resizedBitmap
2022-06-14 15:30:58 +05:30
} catch (e: Exception) {
null
}
}
}