LibreTube/app/src/main/java/com/github/libretube/util/NotificationHelper.kt

149 lines
5.6 KiB
Kotlin
Raw Normal View History

2022-07-28 16:09:56 +05:30
package com.github.libretube.util
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.preferences.PreferenceHelper
2022-07-28 18:01:35 +05:30
import com.github.libretube.preferences.PreferenceKeys
2022-07-28 16:09:56 +05:30
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import java.util.concurrent.TimeUnit
object NotificationHelper {
fun enqueueWork(
context: Context
) {
2022-07-28 19:28:22 +05:30
// get the notification preferences
2022-07-28 18:01:35 +05:30
PreferenceHelper.setContext(context)
val notificationsEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.NOTIFICATION_ENABLED,
true
2022-07-28 16:09:56 +05:30
)
2022-07-28 18:01:35 +05:30
val checkingFrequency = PreferenceHelper.getString(
PreferenceKeys.CHECKING_FREQUENCY,
"60"
).toLong()
val uniqueWorkName = "NotificationService"
if (notificationsEnabled) {
2022-07-28 19:28:22 +05:30
// requirements for the work
// here: network needed to run the task
2022-07-28 18:01:35 +05:30
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
2022-07-28 19:28:22 +05:30
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
2022-07-28 18:01:35 +05:30
NotificationWorker::class.java,
2022-07-28 18:19:02 +05:30
checkingFrequency,
TimeUnit.MINUTES
2022-07-28 16:09:56 +05:30
)
2022-07-28 18:01:35 +05:30
.setConstraints(constraints)
2022-07-28 19:28:22 +05:30
.build()
2022-07-28 18:01:35 +05:30
2022-07-28 19:28:22 +05:30
// enqueue the task
2022-07-28 18:01:35 +05:30
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
uniqueWorkName,
ExistingPeriodicWorkPolicy.REPLACE,
2022-07-28 19:28:22 +05:30
notificationWorker
2022-07-28 18:01:35 +05:30
)
} else {
WorkManager.getInstance(context)
.cancelUniqueWork(uniqueWorkName)
}
2022-07-28 16:09:56 +05:30
}
2022-07-28 18:01:35 +05:30
/**
* check whether new streams are available in subscriptions
*/
2022-07-28 16:09:56 +05:30
fun checkForNewStreams(context: Context) {
val token = PreferenceHelper.getToken()
2022-07-28 18:15:29 +05:30
if (token == "") return
2022-07-28 16:09:56 +05:30
runBlocking {
val task = async {
RetrofitInstance.authApi.getFeed(token)
}
2022-07-28 18:19:02 +05:30
// fetch the users feed
2022-07-28 18:15:29 +05:30
val videoFeed = try {
task.await()
} catch (e: Exception) {
return@runBlocking
}
2022-07-28 19:28:22 +05:30
2022-07-28 18:01:35 +05:30
val lastSeenStreamId = PreferenceHelper.getLatestVideoId()
2022-07-29 12:30:13 +05:30
val latestFeedStreamId = videoFeed[0].url.toID()
2022-07-28 19:28:22 +05:30
2022-07-28 18:01:35 +05:30
// first time notifications enabled
if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId)
else if (lastSeenStreamId != latestFeedStreamId) {
// get the index of the last user-seen video
var newStreamIndex = -1
videoFeed.forEachIndexed { index, stream ->
2022-07-29 12:30:13 +05:30
if (stream.url?.toID() == lastSeenStreamId) {
2022-07-28 18:01:35 +05:30
newStreamIndex = index
}
}
2022-07-28 18:19:02 +05:30
if (newStreamIndex == -1) return@runBlocking
2022-07-28 18:01:35 +05:30
val (title, description) = when (newStreamIndex) {
// only one new stream available
1 -> {
Pair(videoFeed[0].title, videoFeed[0].uploaderName)
}
else -> {
Pair(
// return the amount of new streams as title
context.getString(
R.string.new_streams_count,
newStreamIndex.toString()
),
// return the first few uploader as description
context.getString(
R.string.new_streams_by,
videoFeed[0].uploaderName + ", " + videoFeed[1].uploaderName + ", " + videoFeed[2].uploaderName
)
)
}
}
2022-07-28 19:28:22 +05:30
// save the id of the last recent video for the next time it's running
2022-07-29 12:30:13 +05:30
PreferenceHelper.setLatestVideoId(videoFeed[0].url.toID())
2022-07-28 18:01:35 +05:30
createNotification(context, title!!, description!!)
}
}
2022-07-28 16:09:56 +05:30
}
2022-07-28 18:15:29 +05:30
/**
* Notification that is created when new streams are found
*/
2022-07-28 16:09:56 +05:30
fun createNotification(context: Context, title: String, description: String) {
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 builder = NotificationCompat.Builder(context, "notification_worker")
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_bell)
.setContentText(description)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(context)) {
// notificationId is a unique int for each notification that you must define
notify(2, builder.build())
}
}
}