Merge pull request #3542 from Isira-Seneviratne/Stream_thumbnails

Display stream notification thumbnails.
This commit is contained in:
Isira Seneviratne 2023-04-13 18:06:24 +05:30 committed by GitHub
commit 6e0a016cc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 4 deletions

View File

@ -107,6 +107,7 @@ object PreferenceKeys {
* Notifications
*/
const val NOTIFICATION_ENABLED = "notification_toggle"
const val SHOW_STREAM_THUMBNAILS = "show_stream_thumbnails"
const val CHECKING_FREQUENCY = "checking_frequency"
const val REQUIRED_NETWORK = "required_network"
const val IGNORED_NOTIFICATION_CHANNELS = "ignored_notification_channels"

View File

@ -11,6 +11,7 @@ import coil.disk.DiskCache
import coil.load
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.request.ImageResult
import com.github.libretube.api.CronetHelper
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.toAndroidUri
@ -73,6 +74,14 @@ object ImageHelper {
imageLoader.enqueue(request)
}
suspend fun getImage(context: Context, url: String?): ImageResult {
val request = ImageRequest.Builder(context)
.data(url)
.build()
return imageLoader.execute(request)
}
fun getDownloadedImage(context: Context, path: Path): Bitmap? {
return path.toAndroidUriOrNull()?.let { getImage(context, it) }
}

View File

@ -4,11 +4,13 @@ import android.app.NotificationManager
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.github.libretube.R
@ -20,6 +22,7 @@ import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference
@ -133,7 +136,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
*
* For more information, see https://developer.android.com/develop/ui/views/notifications/group
*/
private fun createNotificationsForChannel(group: String, streams: List<StreamItem>) {
private suspend fun createNotificationsForChannel(group: String, streams: List<StreamItem>) {
val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
@ -146,14 +149,28 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
val notification = createNotificationBuilder(group)
val notificationBuilder = createNotificationBuilder(group)
.setContentTitle(it.title)
.setContentText(it.uploaderName)
// The intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.build()
notificationManager.notify(code, notification)
// Load stream thumbnails if the relevant toggle is enabled.
if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) {
val thumbnail = withContext(Dispatchers.IO) {
ImageHelper.getImage(applicationContext, it.thumbnail).drawable?.toBitmap()
}
notificationBuilder
.setLargeIcon(thumbnail)
.setStyle(
NotificationCompat.BigPictureStyle()
.bigPicture(thumbnail)
.bigLargeIcon(null as Bitmap?) // Hides the icon when expanding
)
}
notificationManager.notify(code, notificationBuilder.build())
}
val summaryId = ++notificationId

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z" />
</vector>

View File

@ -268,6 +268,8 @@
<string name="notifications">Notifications</string>
<string name="notify_new_streams">Notifications for new streams</string>
<string name="notify_new_streams_summary">Notifications about fresh content from creators you follow.</string>
<string name="show_stream_thumbnails">Show stream thumbnails</string>
<string name="show_stream_thumbnails_summary">Show the thumbnails of new streams. Enabling this will consume additional data.</string>
<string name="checking_frequency">Checking every …</string>
<string name="new_streams_count">%1$s new streams available</string>
<string name="new_streams_by">New streams by %1$s…</string>

View File

@ -11,6 +11,13 @@
app:key="notification_toggle"
app:title="@string/notify_new_streams" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_image"
app:defaultValue="false"
app:key="show_stream_thumbnails"
app:title="@string/show_stream_thumbnails"
app:summary="@string/show_stream_thumbnails_summary" />
<ListPreference
android:icon="@drawable/ic_time"
app:defaultValue="60"