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) {
playerNotification = PlayerNotificationManager
.Builder(c, NOTIFICATION_ID, "background_mode").build()
.Builder(c, 1, "background_mode").build()
playerNotification.setPlayer(player)
playerNotification.setUsePreviousAction(false)
playerNotification.setUseNextAction(false)

View File

@ -1,8 +1,6 @@
package com.github.libretube
import android.app.DownloadManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
@ -152,33 +150,7 @@ class DownloadService : Service() {
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) {
// Creating the notification channel
val channelId = createNotificationChannel(
"service", "service", "DownloadService",
NotificationManager.IMPORTANCE_NONE
)
var pendingIntent: PendingIntent? = null
pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
@ -187,7 +159,7 @@ class DownloadService : Service() {
}
// Creating a notification and setting its various attributes
notification =
NotificationCompat.Builder(this@DownloadService, channelId)
NotificationCompat.Builder(this@DownloadService, "download_service")
.setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube")
.setContentText("Downloading")
@ -201,35 +173,27 @@ class DownloadService : Service() {
}
private fun downloadFailedNotification() {
val builder = NotificationCompat.Builder(this@DownloadService, "failed")
val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed))
.setContentText("failure")
.setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel(
"failed", "failed", "Download Failed",
NotificationManager.IMPORTANCE_DEFAULT
)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
notify(69, builder.build())
notify(3, builder.build())
}
}
private fun downloadSucceededNotification() {
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)
.setContentTitle(resources.getString(R.string.success))
.setContentText("success")
.setPriority(NotificationCompat.PRIORITY_HIGH)
createNotificationChannel(
"success", "success", "Download succeeded",
NotificationManager.IMPORTANCE_DEFAULT
)
with(NotificationManagerCompat.from(this@DownloadService)) {
// 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.os.Build
const val NOTIFICATION_ID = 1
class MyApp : Application() {
override fun 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() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = "Background Mode"
val descriptionText = "Shows a notification with buttons to control the audio player"
val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel("background_mode", name, importance)
mChannel.description = descriptionText
createNotificationChannel(
"download_service",
"Download Service",
"DownloadService",
NotificationManager.IMPORTANCE_NONE
)
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
notificationManager.createNotificationChannel(mChannel)
notificationManager.createNotificationChannel(channel)
}
}

View File

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