-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
data/src/main/java/com/m3u/data/worker/ProgrammeReminder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.m3u.data.worker | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.media.AudioAttributes | ||
import android.media.RingtoneManager | ||
import androidx.core.graphics.drawable.toBitmapOrNull | ||
import androidx.hilt.work.HiltWorker | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
import com.m3u.data.R | ||
import com.m3u.data.repository.media.MediaRepository | ||
import com.m3u.data.repository.programme.ProgrammeRepository | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedInject | ||
import kotlinx.datetime.Clock | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
@HiltWorker | ||
class ProgrammeReminder @AssistedInject constructor( | ||
@Assisted private val context: Context, | ||
@Assisted params: WorkerParameters, | ||
private val programmeRepository: ProgrammeRepository, | ||
private val notificationManager: NotificationManager, | ||
private val mediaRepository: MediaRepository | ||
) : CoroutineWorker(context, params) { | ||
private val programmeId = inputData.getInt(PROGRAMME_ID, -1) | ||
private val notificationId: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { | ||
ATOMIC_NOTIFICATION_ID.incrementAndGet() | ||
} | ||
|
||
override suspend fun doWork(): Result { | ||
createChannel() | ||
if (programmeId == -1) { | ||
return Result.failure() | ||
} | ||
val programme = programmeRepository.getById(programmeId) ?: return Result.failure() | ||
val drawable = mediaRepository.loadDrawable(programme.icon.orEmpty()) | ||
val builder = Notification.Builder(context, CHANNEL_ID) | ||
.setContentTitle(programme.title) | ||
.setContentText(programme.description) | ||
.setSmallIcon(R.drawable.baseline_notifications_none_24) | ||
if (drawable != null) { | ||
builder.setLargeIcon(drawable.toBitmapOrNull()) | ||
} | ||
notificationManager.notify( | ||
notificationId, | ||
builder.build() | ||
) | ||
return Result.success() | ||
} | ||
|
||
private fun createChannel() { | ||
val channel = NotificationChannel( | ||
CHANNEL_ID, | ||
NOTIFICATION_NAME, | ||
NotificationManager.IMPORTANCE_HIGH | ||
) | ||
channel.description = "Receive programme notifications" | ||
channel.setSound( | ||
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), | ||
AudioAttributes.Builder() | ||
.setUsage(AudioAttributes.USAGE_MEDIA) | ||
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE) | ||
.build() | ||
) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
|
||
companion object { | ||
private const val CHANNEL_ID = "reminder_channel" | ||
private const val NOTIFICATION_NAME = "programme_reminder" | ||
private const val PROGRAMME_ID = "programme_id" | ||
fun readProgrammeId(tags: Collection<String>): Int? = tags.find { | ||
it.startsWith("id=") | ||
} | ||
?.drop(3) | ||
?.toIntOrNull() | ||
|
||
const val TAG = CHANNEL_ID | ||
private val ATOMIC_NOTIFICATION_ID = AtomicInteger() | ||
|
||
operator fun invoke( | ||
workManager: WorkManager, | ||
programmeId: Int, | ||
programmeStart: Long | ||
) { | ||
val now = Clock.System.now().toEpochMilliseconds() | ||
if (now > programmeStart) return | ||
val data = workDataOf( | ||
PROGRAMME_ID to programmeId | ||
) | ||
val request = OneTimeWorkRequestBuilder<ProgrammeReminder>() | ||
.addTag(TAG) | ||
.addTag("id=$programmeId") | ||
.setInputData(data) | ||
.setInitialDelay(programmeStart - now, TimeUnit.MILLISECONDS) | ||
.build() | ||
workManager.enqueue(request) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
data/src/main/res/drawable/baseline_notifications_none_24.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp"> | ||
|
||
<path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6z"/> | ||
|
||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters