cleanup notifcation channels

This commit is contained in:
Bnyro 2022-07-30 11:21:18 +02:00
parent 41cf1c782d
commit 357978f09e
6 changed files with 45 additions and 20 deletions

View File

@ -32,3 +32,19 @@ const val YOUTUBE_FRONTEND_URL = "https://www.youtube.com"
* Retrofit Instance
*/
const val PIPED_API_URL = "https://pipedapi.kavin.rocks/"
/**
* Notification IDs
*/
const val PLAYER_NOTIFICATION_ID = 0
const val PUSH_NOTIFICATION_ID = 1
const val DOWNLOAD_PENDING_NOTIFICATION_ID = 2
const val DOWNLOAD_FAILURE_NOTIFICATION_ID = 3
const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 4
/**
* Notification Channel IDs
*/
const val DOWNLOAD_CHANNEL_ID = "download_service"
const val BACKGROUND_CHANNEL_ID = "background_mode"
const val PUSH_CHANNEL_ID = "notification_worker"

View File

@ -66,19 +66,19 @@ class MyApp : Application() {
*/
private fun initializeNotificationChannels() {
createNotificationChannel(
"download_service",
DOWNLOAD_CHANNEL_ID,
"Download Service",
"Shows a notification when downloading media.",
NotificationManager.IMPORTANCE_NONE
)
createNotificationChannel(
"background_mode",
BACKGROUND_CHANNEL_ID,
"Background Mode",
"Shows a notification with buttons to control the audio player",
NotificationManager.IMPORTANCE_LOW
)
createNotificationChannel(
"notification_worker",
PUSH_CHANNEL_ID,
"Notification Worker",
"Shows a notification when new streams are available.",
NotificationManager.IMPORTANCE_DEFAULT

View File

@ -34,7 +34,9 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.BACKGROUND_CHANNEL_ID
import com.github.libretube.Globals
import com.github.libretube.PLAYER_NOTIFICATION_ID
import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.ChaptersAdapter
@ -1518,7 +1520,7 @@ class PlayerFragment : Fragment() {
mediaSessionConnector.setPlayer(exoPlayer)
playerNotification = PlayerNotificationManager
.Builder(c, 1, "background_mode")
.Builder(c, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
.setMediaDescriptionAdapter(
DescriptionAdapter(title, uploader, thumbnailUrl, requireContext())
)

View File

@ -9,6 +9,8 @@ import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.support.v4.media.session.MediaSessionCompat
import com.github.libretube.BACKGROUND_CHANNEL_ID
import com.github.libretube.PLAYER_NOTIFICATION_ID
import com.github.libretube.R
import com.github.libretube.obj.Streams
import com.github.libretube.preferences.PreferenceHelper
@ -182,7 +184,7 @@ class BackgroundMode : Service() {
*/
private fun initializePlayerNotification() {
playerNotification = PlayerNotificationManager
.Builder(this, 1, "background_mode")
.Builder(this, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
// set the description of the notification
.setMediaDescriptionAdapter(
DescriptionAdapter(

View File

@ -18,6 +18,10 @@ import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.arthenica.ffmpegkit.FFmpegKit
import com.github.libretube.DOWNLOAD_CHANNEL_ID
import com.github.libretube.DOWNLOAD_FAILURE_NOTIFICATION_ID
import com.github.libretube.DOWNLOAD_PENDING_NOTIFICATION_ID
import com.github.libretube.DOWNLOAD_SUCCESS_NOTIFICATION_ID
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.obj.DownloadType
@ -199,7 +203,7 @@ class DownloadService : Service() {
}
// Creating a notification and setting its various attributes
notification =
NotificationCompat.Builder(this@DownloadService, "download_service")
NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_download)
.setContentTitle("LibreTube")
.setContentText(getString(R.string.downloading))
@ -209,31 +213,31 @@ class DownloadService : Service() {
.setProgress(100, 0, true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
startForeground(2, notification.build())
startForeground(DOWNLOAD_PENDING_NOTIFICATION_ID, notification.build())
}
private fun downloadFailedNotification() {
val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.downloadfailed))
.setContentText(getString(R.string.fail))
.setPriority(NotificationCompat.PRIORITY_HIGH)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
notify(3, builder.build())
notify(DOWNLOAD_FAILURE_NOTIFICATION_ID, builder.build())
}
}
private fun downloadSucceededNotification() {
Log.i(TAG, "Download succeeded")
val builder = NotificationCompat.Builder(this@DownloadService, "download_service")
val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(resources.getString(R.string.success))
.setContentText(getString(R.string.fail))
.setPriority(NotificationCompat.PRIORITY_HIGH)
with(NotificationManagerCompat.from(this@DownloadService)) {
// notificationId is a unique int for each notification that you must define
notify(4, builder.build())
notify(DOWNLOAD_SUCCESS_NOTIFICATION_ID, builder.build())
}
}
@ -267,12 +271,6 @@ class DownloadService : Service() {
) {
// CALLED WHEN SESSION GENERATES STATISTICS
Log.e(TAG + "stat", it.time.toString())
/*val progress = it.time/(10*duration!!)
if (progress<1){
notification
.setProgress(progressMax, progress.toInt(), false)
service.notify(1,notification.build())
}*/
}
}

View File

@ -10,6 +10,8 @@ import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.github.libretube.PUSH_CHANNEL_ID
import com.github.libretube.PUSH_NOTIFICATION_ID
import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.preferences.PreferenceHelper
@ -137,9 +139,14 @@ object NotificationHelper {
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val pendingIntent: PendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationCompat.Builder(context, "notification_worker")
val builder = NotificationCompat.Builder(context, PUSH_CHANNEL_ID)
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_bell)
.setContentText(description)
@ -149,7 +156,7 @@ object NotificationHelper {
.setAutoCancel(true)
with(NotificationManagerCompat.from(context)) {
// notificationId is a unique int for each notification that you must define
notify(2, builder.build())
notify(PUSH_NOTIFICATION_ID, builder.build())
}
}
}