mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
commit
24cac0a876
@ -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"
|
||||
|
@ -6,6 +6,7 @@ import android.app.NotificationManager
|
||||
import android.os.Build
|
||||
import android.os.StrictMode
|
||||
import android.os.StrictMode.VmPolicy
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import com.github.libretube.preferences.PreferenceHelper
|
||||
import com.github.libretube.preferences.PreferenceKeys
|
||||
import com.github.libretube.util.NotificationHelper
|
||||
@ -39,7 +40,7 @@ class MyApp : Application() {
|
||||
/**
|
||||
* initialize the notification listener in the background
|
||||
*/
|
||||
NotificationHelper.enqueueWork(this)
|
||||
NotificationHelper.enqueueWork(this, ExistingPeriodicWorkPolicy.KEEP)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,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
|
||||
|
@ -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())
|
||||
)
|
||||
|
@ -4,6 +4,7 @@ import android.os.Bundle
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.SwitchPreferenceCompat
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.activities.SettingsActivity
|
||||
import com.github.libretube.util.NotificationHelper
|
||||
@ -19,14 +20,22 @@ class NotificationSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val notificationsEnabled = findPreference<SwitchPreferenceCompat>(PreferenceKeys.NOTIFICATION_ENABLED)
|
||||
notificationsEnabled?.setOnPreferenceChangeListener { _, _ ->
|
||||
NotificationHelper.enqueueWork(requireContext())
|
||||
updateNotificationPrefs()
|
||||
true
|
||||
}
|
||||
|
||||
val checkingFrequency = findPreference<ListPreference>(PreferenceKeys.CHECKING_FREQUENCY)
|
||||
checkingFrequency?.setOnPreferenceChangeListener { _, _ ->
|
||||
NotificationHelper.enqueueWork(requireContext())
|
||||
updateNotificationPrefs()
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateNotificationPrefs() {
|
||||
// replace the previous queued work request
|
||||
NotificationHelper.enqueueWork(
|
||||
requireContext(),
|
||||
ExistingPeriodicWorkPolicy.REPLACE
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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())
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
@ -20,7 +22,8 @@ import java.util.concurrent.TimeUnit
|
||||
|
||||
object NotificationHelper {
|
||||
fun enqueueWork(
|
||||
context: Context
|
||||
context: Context,
|
||||
existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy
|
||||
) {
|
||||
// get the notification preferences
|
||||
PreferenceHelper.setContext(context)
|
||||
@ -57,7 +60,7 @@ object NotificationHelper {
|
||||
WorkManager.getInstance(context)
|
||||
.enqueueUniquePeriodicWork(
|
||||
uniqueWorkName,
|
||||
ExistingPeriodicWorkPolicy.REPLACE,
|
||||
existingPeriodicWorkPolicy,
|
||||
notificationWorker
|
||||
)
|
||||
} else {
|
||||
@ -136,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)
|
||||
@ -148,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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user