|
1 | 1 | package com.artemchep.pocketmode.services |
2 | 2 |
|
| 3 | +import android.app.Notification |
| 4 | +import android.app.NotificationChannel |
| 5 | +import android.app.NotificationManager |
| 6 | +import android.app.PendingIntent |
3 | 7 | import android.content.Context |
4 | 8 | import android.content.Intent |
5 | | -import androidx.work.Worker |
| 9 | +import androidx.core.app.NotificationCompat |
| 10 | +import androidx.core.content.getSystemService |
| 11 | +import androidx.lifecycle.asFlow |
| 12 | +import androidx.work.CoroutineWorker |
6 | 13 | import androidx.work.WorkerParameters |
7 | | -import com.artemchep.pocketmode.viewmodels.PocketViewModel |
| 14 | +import com.artemchep.pocketmode.Cfg |
| 15 | +import com.artemchep.pocketmode.R |
| 16 | +import com.artemchep.pocketmode.sensors.AccessAccessibilityLiveData |
| 17 | +import com.artemchep.pocketmode.ui.activities.MainActivity |
| 18 | +import kotlinx.coroutines.flow.first |
8 | 19 |
|
9 | 20 | /** |
10 | 21 | * @author Artem Chepurnoy |
11 | 22 | */ |
12 | 23 | class PocketServiceRestartWorker( |
13 | 24 | context: Context, |
14 | 25 | workerParams: WorkerParameters |
15 | | -) : Worker(context, workerParams) { |
16 | | - override fun doWork(): Result { |
17 | | - val intent = Intent(applicationContext, PocketViewModel::class.java) |
18 | | - applicationContext.startForegroundService(intent) |
| 26 | +) : CoroutineWorker(context, workerParams) { |
| 27 | + companion object { |
| 28 | + private const val NOTIFICATION_CHANNEL = "pocket_restart_channel" |
| 29 | + private const val NOTIFICATION_ID = 212 |
| 30 | + } |
| 31 | + |
| 32 | + override suspend fun doWork(): Result { |
| 33 | + val accessibilityGranted = AccessAccessibilityLiveData(applicationContext) |
| 34 | + .asFlow() |
| 35 | + .first() |
| 36 | + if (Cfg.isEnabled && (!PocketService.running || !accessibilityGranted)) { |
| 37 | + val nm = applicationContext.getSystemService<NotificationManager>()!! |
| 38 | + val n = createNotification( |
| 39 | + accessibilityGranted = accessibilityGranted, |
| 40 | + ) |
| 41 | + nm.notify(NOTIFICATION_ID, n) |
| 42 | + } |
19 | 43 |
|
20 | 44 | return Result.success() |
21 | 45 | } |
| 46 | + |
| 47 | + private fun createNotification( |
| 48 | + accessibilityGranted: Boolean, |
| 49 | + ): Notification { |
| 50 | + // Create notification channel |
| 51 | + val channelName = applicationContext.getString(R.string.notification_restart_channel) |
| 52 | + val channel = |
| 53 | + NotificationChannel( |
| 54 | + NOTIFICATION_CHANNEL, |
| 55 | + channelName, |
| 56 | + NotificationManager.IMPORTANCE_HIGH |
| 57 | + ) |
| 58 | + val nm = applicationContext.getSystemService<NotificationManager>()!! |
| 59 | + nm.createNotificationChannel(channel) |
| 60 | + |
| 61 | + val pi = if (accessibilityGranted) { |
| 62 | + val intent = Intent(applicationContext, PocketService::class.java) |
| 63 | + PendingIntent.getForegroundService( |
| 64 | + applicationContext, |
| 65 | + NOTIFICATION_ID, |
| 66 | + intent, |
| 67 | + PendingIntent.FLAG_IMMUTABLE |
| 68 | + ) |
| 69 | + } else { |
| 70 | + // We can not restart the service, because the system has taken the accessibility |
| 71 | + // permission of the app. |
| 72 | + val intent = Intent(applicationContext, MainActivity::class.java) |
| 73 | + PendingIntent.getActivity( |
| 74 | + applicationContext, |
| 75 | + NOTIFICATION_ID, |
| 76 | + intent, |
| 77 | + PendingIntent.FLAG_IMMUTABLE |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + val description = applicationContext.getString(R.string.notification_restart_description) |
| 82 | + return NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL) |
| 83 | + .setAutoCancel(true) |
| 84 | + .setSmallIcon(R.drawable.ic_outline_lock) |
| 85 | + .setContentIntent(pi) |
| 86 | + .setColor(0xFFf4ff81.toInt()) |
| 87 | + .setShowWhen(false) |
| 88 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 89 | + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
| 90 | + .setContentTitle(applicationContext.getString(R.string.notification_restart_title)) |
| 91 | + .setContentText(description) |
| 92 | + .setStyle(NotificationCompat.BigTextStyle().bigText(description)) |
| 93 | + .build() |
| 94 | + } |
22 | 95 | } |
0 commit comments