This commit is contained in:
Bnyro 2022-06-06 16:42:46 +02:00
parent aa139247cb
commit 0c1a21f9f5
4 changed files with 35 additions and 56 deletions

View File

@ -68,7 +68,7 @@ class BackgroundMode {
*/ */
private fun initializePlayerNotification(c: Context) { private fun initializePlayerNotification(c: Context) {
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(c, NOTIFICATION_ID, "background_mode").build() .Builder(c, 1, "background_mode").build()
playerNotification.setPlayer(player) playerNotification.setPlayer(player)
playerNotification.setUsePreviousAction(false) playerNotification.setUsePreviousAction(false)
playerNotification.setUseNextAction(false) playerNotification.setUseNextAction(false)

View File

@ -1,8 +1,6 @@
package com.github.libretube package com.github.libretube
import android.app.DownloadManager import android.app.DownloadManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
@ -152,33 +150,7 @@ class DownloadService : Service() {
return downloadManager.enqueue(request) return downloadManager.enqueue(request)
} }
private fun createNotificationChannel(
id: String,
name: String,
descriptionText: String,
importance: Int
): String {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
id
} else ""
}
private fun downloadNotification(intent: Intent) { private fun downloadNotification(intent: Intent) {
// Creating the notification channel
val channelId = createNotificationChannel(
"service", "service", "DownloadService",
NotificationManager.IMPORTANCE_NONE
)
var pendingIntent: PendingIntent? = null var pendingIntent: PendingIntent? = null
pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE) PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
@ -187,7 +159,7 @@ class DownloadService : Service() {
} }
// Creating a notification and setting its various attributes // Creating a notification and setting its various attributes
notification = notification =
NotificationCompat.Builder(this@DownloadService, channelId) NotificationCompat.Builder(this@DownloadService, "download_service")
.setSmallIcon(R.drawable.ic_download) .setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube") .setContentTitle("LibreTube")
.setContentText("Downloading") .setContentText("Downloading")
@ -201,35 +173,27 @@ class DownloadService : Service() {
} }
private fun downloadFailedNotification() { private fun downloadFailedNotification() {
val builder = NotificationCompat.Builder(this@DownloadService, "failed") val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
.setSmallIcon(R.drawable.ic_download) .setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed)) .setContentTitle(resources.getString(R.string.downloadfailed))
.setContentText("failure") .setContentText("failure")
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel(
"failed", "failed", "Download Failed",
NotificationManager.IMPORTANCE_DEFAULT
)
with(NotificationManagerCompat.from(this@DownloadService)) { with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define // notificationId is a unique int for each notification that you must define
notify(69, builder.build()) notify(3, builder.build())
} }
} }
private fun downloadSucceededNotification() { private fun downloadSucceededNotification() {
Log.i(TAG, "Download succeeded") Log.i(TAG, "Download succeeded")
val builder = NotificationCompat.Builder(this@DownloadService, "failed") val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
.setSmallIcon(R.drawable.ic_download) .setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.success)) .setContentTitle(resources.getString(R.string.success))
.setContentText("success") .setContentText("success")
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel(
"success", "success", "Download succeeded",
NotificationManager.IMPORTANCE_DEFAULT
)
with(NotificationManagerCompat.from(this@DownloadService)) { with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define // notificationId is a unique int for each notification that you must define
notify(70, builder.build()) notify(4, builder.build())
} }
} }

View File

@ -5,8 +5,6 @@ import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.os.Build import android.os.Build
const val NOTIFICATION_ID = 1
class MyApp : Application() { class MyApp : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -15,19 +13,37 @@ class MyApp : Application() {
} }
/** /**
* Initializes the required [NotificationChannel] for the app. * Initializes the required [NotificationChannel]s for the app.
*/ */
private fun initializeNotificationChannels() { private fun initializeNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(
// Create the NotificationChannel "download_service",
val name = "Background Mode" "Download Service",
val descriptionText = "Shows a notification with buttons to control the audio player" "DownloadService",
val importance = NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_NONE
val mChannel = NotificationChannel("background_mode", name, importance) )
mChannel.description = descriptionText createNotificationChannel(
"background_mode",
"Background Mode",
"Shows a notification with buttons to control the audio player",
NotificationManager.IMPORTANCE_LOW
)
}
private fun createNotificationChannel(
id: String,
name: String,
descriptionText: String,
importance: Int
) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, name, importance)
channel.description = descriptionText
// Register the channel in the system
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel) notificationManager.createNotificationChannel(channel)
} }
} }

View File

@ -42,7 +42,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.IS_DOWNLOAD_RUNNING import com.github.libretube.IS_DOWNLOAD_RUNNING
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.NOTIFICATION_ID
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.SponsorBlockSettings import com.github.libretube.SponsorBlockSettings
import com.github.libretube.adapters.CommentsAdapter import com.github.libretube.adapters.CommentsAdapter
@ -339,7 +338,7 @@ class PlayerFragment : Fragment() {
val notificationManager = context?.getSystemService( val notificationManager = context?.getSystemService(
Context.NOTIFICATION_SERVICE Context.NOTIFICATION_SERVICE
) as NotificationManager ) as NotificationManager
notificationManager.cancel(NOTIFICATION_ID) notificationManager.cancel(1)
exoPlayer.release() exoPlayer.release()
} catch (e: Exception) { } catch (e: Exception) {
} }
@ -812,7 +811,7 @@ class PlayerFragment : Fragment() {
mediaSessionConnector.setPlayer(exoPlayer) mediaSessionConnector.setPlayer(exoPlayer)
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(c, NOTIFICATION_ID, "background_mode") .Builder(c, 1, "background_mode")
.build() .build()
playerNotification.apply { playerNotification.apply {