Implement getForegroundInfo for expedited workers

This commit is contained in:
Jonas Lochmann 2021-09-06 02:00:00 +02:00
parent 34a852373d
commit 0baeafd1bf
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
7 changed files with 83 additions and 7 deletions

View file

@ -154,7 +154,7 @@ android {
dependencies {
def nav_version = "2.3.5"
def room_version = "2.3.0"
def work_version = '2.7.0-alpha05'
def work_version = '2.7.0-beta01'
def paging_version = "3.0.1"
implementation fileTree(dir: 'libs', include: ['*.jar'])

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,6 +31,8 @@ object NotificationIds {
const val USER_NOTIFICATION = 5
const val TIME_WARNING = 6
const val LOCAL_UPDATE_NOTIFICATION = 7
const val WORKER_REPORT_UNINSTALL = 8
const val WORKER_SYNC_BACKGROUND = 9
}
object NotificationChannels {
@ -40,6 +42,7 @@ object NotificationChannels {
const val UPDATE_NOTIFICATION = "update notification"
const val TIME_WARNING = "time warning"
const val PREMIUM_EXPIRES_NOTIFICATION = "premium expires"
const val BACKGROUND_SYNC_NOTIFICATION = "background sync"
private fun createAppStatusChannel(notificationManager: NotificationManager, context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -119,7 +122,7 @@ object NotificationChannels {
}
}
fun createPremiumExpiresChannel(notificationManager: NotificationManager, context: Context) {
private fun createPremiumExpiresChannel(notificationManager: NotificationManager, context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(
NotificationChannel(
@ -136,6 +139,25 @@ object NotificationChannels {
}
}
private fun createBackgroundSyncChannel(notificationManager: NotificationManager, context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(
NotificationChannel(
BACKGROUND_SYNC_NOTIFICATION,
context.getString(R.string.notification_channel_background_sync_title),
NotificationManager.IMPORTANCE_LOW
).apply {
description = context.getString(R.string.notification_channel_background_sync_text)
enableLights(false)
setSound(null, null)
enableVibration(false)
setShowBadge(false)
lockscreenVisibility = NotificationCompat.VISIBILITY_SECRET
}
)
}
}
fun createNotificationChannels(notificationManager: NotificationManager, context: Context) {
createAppStatusChannel(notificationManager, context)
createBlockedNotificationChannel(notificationManager, context)
@ -143,6 +165,7 @@ object NotificationChannels {
createUpdateNotificationChannel(notificationManager, context)
createTimeWarningsNotificationChannel(notificationManager, context)
createPremiumExpiresChannel(notificationManager, context)
createBackgroundSyncChannel(notificationManager, context)
}
}

View file

@ -15,6 +15,8 @@
*/
package io.timelimit.android.ui
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
@ -34,6 +36,7 @@ import io.timelimit.android.R
import io.timelimit.android.coroutines.runAsync
import io.timelimit.android.data.IdGenerator
import io.timelimit.android.extensions.showSafe
import io.timelimit.android.integration.platform.android.NotificationChannels
import io.timelimit.android.livedata.ignoreUnchanged
import io.timelimit.android.livedata.liveDataFromNullableValue
import io.timelimit.android.livedata.map
@ -73,6 +76,8 @@ class MainActivity : AppCompatActivity(), ActivityViewModelHolder {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
NotificationChannels.createNotificationChannels(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager, this)
if (savedInstanceState == null) {
NavHostFragment.create(R.navigation.nav_graph).let { navhost ->
supportFragmentManager.beginTransaction()

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,11 +17,14 @@ package io.timelimit.android.work
import android.content.Context
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.*
import io.timelimit.android.BuildConfig
import io.timelimit.android.R
import io.timelimit.android.integration.platform.android.NotificationChannels
import io.timelimit.android.integration.platform.android.NotificationIds
import io.timelimit.android.logic.DefaultAppLogic
@androidx.work.ExperimentalExpeditedWork
class ReportUninstallWorker(val context: Context, workerParameters: WorkerParameters): CoroutineWorker(context, workerParameters) {
companion object {
private const val DATA_AUTH_TOKEN = "deviceAuthToken"
@ -81,4 +84,19 @@ class ReportUninstallWorker(val context: Context, workerParameters: WorkerParame
Result.retry()
}
}
override suspend fun getForegroundInfo(): ForegroundInfo = ForegroundInfo(
NotificationIds.WORKER_REPORT_UNINSTALL,
NotificationCompat.Builder(context, NotificationChannels.BACKGROUND_SYNC_NOTIFICATION)
.setSmallIcon(R.drawable.ic_stat_timelapse)
.setContentTitle(context.getString(R.string.notification_background_sync_title))
.setContentText(context.getString(R.string.notification_background_sync_text))
.setWhen(0)
.setShowWhen(false)
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOnlyAlertOnce(true)
.build(),
0
)
}

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,15 +17,18 @@ package io.timelimit.android.work
import android.content.Context
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.*
import io.timelimit.android.BuildConfig
import io.timelimit.android.R
import io.timelimit.android.integration.platform.android.NotificationChannels
import io.timelimit.android.integration.platform.android.NotificationIds
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.sync.SyncingDisabledException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.concurrent.TimeUnit
@androidx.work.ExperimentalExpeditedWork
class SyncInBackgroundWorker(val context: Context, workerParameters: WorkerParameters): CoroutineWorker(context, workerParameters) {
companion object {
private const val LOG_TAG = "SyncInBackground"
@ -85,4 +88,19 @@ class SyncInBackgroundWorker(val context: Context, workerParameters: WorkerParam
}
}
}
override suspend fun getForegroundInfo(): ForegroundInfo = ForegroundInfo(
NotificationIds.WORKER_SYNC_BACKGROUND,
NotificationCompat.Builder(context, NotificationChannels.BACKGROUND_SYNC_NOTIFICATION)
.setSmallIcon(R.drawable.ic_stat_timelapse)
.setContentTitle(context.getString(R.string.notification_background_sync_title))
.setContentText(context.getString(R.string.notification_background_sync_text))
.setWhen(0)
.setShowWhen(false)
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOnlyAlertOnce(true)
.build(),
0
)
}

View file

@ -1087,6 +1087,9 @@
<string name="notification_channel_premium_expires_title">Vollversionablauf</string>
<string name="notification_channel_premium_expires_text">Benachrichtigungen, wenn die Vollversion bald abläuft</string>
<string name="notification_channel_background_sync_title">Hintergrundsychronisation</string>
<string name="notification_channel_background_sync_text">Zeigt manchmal technisch notwendige Benachrichtigungen an, wenn TimeLimit im Hintergrund synchronisiert</string>
<string name="notification_filter_not_blocked_title">TimeLimit hat eine Benachrichtigung blockiert</string>
<string name="notification_filter_blocking_failed_title">TimeLimit konnte eine Benachrichtigung nicht blockieren</string>
@ -1097,6 +1100,9 @@
<string name="notification_premium_expires_title">Die Vollversion wird bald ablaufen</string>
<string name="notification_premium_expires_text">Es erfolgt keine automatische Verlängerung</string>
<string name="notification_background_sync_title">TimeLimit ist aktiv</string>
<string name="notification_background_sync_text">eine Synchronisation läuft</string>
<string name="obsolete_message">Sie verwenden TimeLimit auf einer älteren Android-Version.
Das kann funktionieren, aber es wird nicht empfohlen.
</string>

View file

@ -1132,6 +1132,9 @@
<string name="notification_channel_premium_expires_title">Premium version expires</string>
<string name="notification_channel_premium_expires_text">Notification if the premium version expires shortly</string>
<string name="notification_channel_background_sync_title">Background sync</string>
<string name="notification_channel_background_sync_text">Sometimes shows technically required notifications when TimeLimit syncs in the background</string>
<string name="notification_filter_not_blocked_title">TimeLimit has blocked a notification</string>
<string name="notification_filter_blocking_failed_title">TimeLimit could not block a notification</string>
@ -1142,6 +1145,9 @@
<string name="notification_premium_expires_title">The premium version will expire soon</string>
<string name="notification_premium_expires_text">It will not renew automatically</string>
<string name="notification_background_sync_title">TimeLimit is running</string>
<string name="notification_background_sync_text">a background synchronisation is in progress</string>
<string name="obsolete_message">You are using TimeLimit at a obsolete Android version.
Although this can work, it is not recommend.
</string>