Skip to content

Commit d4c05b9

Browse files
committed
NotificationRegistry: enforce creation of channels before they can be accessed
1 parent 768f462 commit d4c05b9

File tree

10 files changed

+47
-47
lines changed

10 files changed

+47
-47
lines changed

app/src/main/kotlin/at/bitfire/davdroid/db/AppDatabase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ abstract class AppDatabase: RoomDatabase() {
7171
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
7272
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_DATABASE_CORRUPTED) {
7373
val launcherIntent = Intent(context, AccountsActivity::class.java)
74-
NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_GENERAL)
74+
NotificationCompat.Builder(context, notificationRegistry.CHANNEL_GENERAL)
7575
.setSmallIcon(R.drawable.ic_warning_notify)
7676
.setContentTitle(context.getString(R.string.database_destructive_migration_title))
7777
.setContentText(context.getString(R.string.database_destructive_migration_text))

app/src/main/kotlin/at/bitfire/davdroid/log/LogFileHandler.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class LogFileHandler @Inject constructor(
120120

121121
private fun showNotification() {
122122
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_VERBOSE_LOGGING) {
123-
val builder = NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_DEBUG)
123+
val builder = NotificationCompat.Builder(context, notificationRegistry.CHANNEL_DEBUG)
124124
builder.setSmallIcon(R.drawable.ic_sd_card_notify)
125125
.setContentTitle(context.getString(R.string.app_settings_logging))
126126
.setCategory(NotificationCompat.CATEGORY_STATUS)

app/src/main/kotlin/at/bitfire/davdroid/servicedetection/RefreshCollectionsWorker.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class RefreshCollectionsWorker @AssistedInject constructor(
239239
* Used by WorkManager to show a foreground service notification for expedited jobs on Android <12.
240240
*/
241241
override suspend fun getForegroundInfo(): ForegroundInfo {
242-
val notification = NotificationCompat.Builder(applicationContext, NotificationRegistry.CHANNEL_STATUS)
242+
val notification = NotificationCompat.Builder(applicationContext, notificationRegistry.CHANNEL_STATUS)
243243
.setSmallIcon(R.drawable.ic_foreground_notify)
244244
.setContentTitle(applicationContext.getString(R.string.foreground_service_notify_title))
245245
.setContentText(applicationContext.getString(R.string.foreground_service_notify_text))
@@ -253,7 +253,7 @@ class RefreshCollectionsWorker @AssistedInject constructor(
253253

254254
private fun notifyRefreshError(contentText: String, contentIntent: Intent) {
255255
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_REFRESH_COLLECTIONS, tag = serviceId.toString()) {
256-
NotificationCompat.Builder(applicationContext, NotificationRegistry.CHANNEL_GENERAL)
256+
NotificationCompat.Builder(applicationContext, notificationRegistry.CHANNEL_GENERAL)
257257
.setSmallIcon(R.drawable.ic_sync_problem_notify)
258258
.setContentTitle(applicationContext.getString(R.string.refresh_collections_worker_refresh_failed))
259259
.setContentText(contentText)

app/src/main/kotlin/at/bitfire/davdroid/sync/SyncManager.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,10 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
853853
val channel: String
854854
val priority: Int
855855
if (e is IOException) {
856-
channel = NotificationRegistry.CHANNEL_SYNC_IO_ERRORS
856+
channel = notificationRegistry.CHANNEL_SYNC_IO_ERRORS
857857
priority = NotificationCompat.PRIORITY_MIN
858858
} else {
859-
channel = NotificationRegistry.CHANNEL_SYNC_ERRORS
859+
channel = notificationRegistry.CHANNEL_SYNC_ERRORS
860860
priority = NotificationCompat.PRIORITY_DEFAULT
861861
}
862862

@@ -924,7 +924,7 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
924924
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_INVALID_RESOURCE, tag = notificationTag) {
925925
val intent = buildDebugInfoIntent(e, null, collection.url.resolve(fileName))
926926

927-
val builder = NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_SYNC_WARNINGS)
927+
val builder = NotificationCompat.Builder(context, notificationRegistry.CHANNEL_SYNC_WARNINGS)
928928
builder.setSmallIcon(R.drawable.ic_warning_notify)
929929
.setContentTitle(notifyInvalidResourceTitle())
930930
.setContentText(context.getString(R.string.sync_invalid_resources_ignoring))

app/src/main/kotlin/at/bitfire/davdroid/sync/worker/BaseSyncWorker.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ abstract class BaseSyncWorker(
265265
logger.warning("Max retries on soft errors reached ($runAttemptCount of $MAX_RUN_ATTEMPTS). Treating as failed")
266266

267267
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_SYNC_ERROR, tag = softErrorNotificationTag) {
268-
NotificationCompat.Builder(applicationContext, NotificationRegistry.CHANNEL_SYNC_IO_ERRORS)
268+
NotificationCompat.Builder(applicationContext, notificationRegistry.CHANNEL_SYNC_IO_ERRORS)
269269
.setSmallIcon(R.drawable.ic_sync_problem_notify)
270270
.setContentTitle(account.name)
271271
.setContentText(applicationContext.getString(R.string.sync_error_retry_limit_reached))

app/src/main/kotlin/at/bitfire/davdroid/sync/worker/OneTimeSyncWorker.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class OneTimeSyncWorker @AssistedInject constructor(
157157
* Used by WorkManager to show a foreground service notification for expedited jobs on Android <12.
158158
*/
159159
override suspend fun getForegroundInfo(): ForegroundInfo {
160-
val notification = NotificationCompat.Builder(applicationContext, NotificationRegistry.CHANNEL_STATUS)
160+
val notification = NotificationCompat.Builder(applicationContext, notificationRegistry.CHANNEL_STATUS)
161161
.setSmallIcon(R.drawable.ic_foreground_notify)
162162
.setContentTitle(applicationContext.getString(R.string.foreground_service_notify_title))
163163
.setContentText(applicationContext.getString(R.string.foreground_service_notify_text))

app/src/main/kotlin/at/bitfire/davdroid/ui/NotificationRegistry.kt

+35-35
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class NotificationRegistry @Inject constructor(
4141
companion object {
4242

4343
// notification IDs
44-
4544
const val NOTIFY_VERBOSE_LOGGING = 1
4645
const val NOTIFY_REFRESH_COLLECTIONS = 2
4746
const val NOTIFY_DATABASE_CORRUPTED = 4
@@ -52,49 +51,50 @@ class NotificationRegistry @Inject constructor(
5251
const val NOTIFY_TASKS_PROVIDER_TOO_OLD = 20
5352
const val NOTIFY_PERMISSIONS = 21
5453

54+
}
5555

56-
// notification channels
5756

58-
/**
59-
* For notifications that don't fit into another channel.
60-
*/
61-
const val CHANNEL_GENERAL = "general"
57+
// notification channel names, accessible only when instance (and thus the channels) has been created
6258

63-
/**
64-
* For debugging notifications. High priority because a debugging session
65-
* has been activated by the user and they should know all the time.
66-
*
67-
* Currently only used for the "verbose logging active" notification.
68-
*/
69-
const val CHANNEL_DEBUG = "debug"
59+
/**
60+
* For notifications that don't fit into another channel.
61+
*/
62+
val CHANNEL_GENERAL = "general"
7063

71-
/**
72-
* Used to show progress, like that a service detection or WebDAV file access is running.
73-
*/
74-
const val CHANNEL_STATUS = "status"
64+
/**
65+
* For debugging notifications. High priority because a debugging session
66+
* has been activated by the user and they should know all the time.
67+
*
68+
* Currently only used for the "verbose logging active" notification.
69+
*/
70+
val CHANNEL_DEBUG = "debug"
7571

76-
/**
77-
* For sync-related notifications. Use the appropriate sub-channels for different types of sync problems.
78-
*/
79-
private const val CHANNEL_SYNC = "sync"
72+
/**
73+
* Used to show progress, like that a service detection or WebDAV file access is running.
74+
*/
75+
val CHANNEL_STATUS = "status"
8076

81-
/**
82-
* For sync errors that are not IO errors. Shown as normal priority.
83-
*/
84-
const val CHANNEL_SYNC_ERRORS = "syncProblems"
77+
/**
78+
* For sync-related notifications. Use the appropriate sub-channels for different types of sync problems.
79+
*/
80+
val CHANNEL_SYNC = "sync"
8581

86-
/**
87-
* For sync warnings. Shown as low priority.
88-
*/
89-
const val CHANNEL_SYNC_WARNINGS = "syncWarnings"
82+
/**
83+
* For sync errors that are not IO errors. Shown as normal priority.
84+
*/
85+
val CHANNEL_SYNC_ERRORS = "syncProblems"
9086

91-
/**
92-
* For sync IO errors. Shown as minimal priority because they might go away automatically, for instance
93-
* when the connection is working again.
94-
*/
95-
const val CHANNEL_SYNC_IO_ERRORS = "syncIoErrors"
87+
/**
88+
* For sync warnings. Shown as low priority.
89+
*/
90+
val CHANNEL_SYNC_WARNINGS = "syncWarnings"
91+
92+
/**
93+
* For sync IO errors. Shown as minimal priority because they might go away automatically, for instance
94+
* when the connection is working again.
95+
*/
96+
val CHANNEL_SYNC_IO_ERRORS = "syncIoErrors"
9697

97-
}
9898

9999
init {
100100
createChannels()

app/src/main/kotlin/at/bitfire/davdroid/util/TaskUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object TaskUtils {
105105
val tasksAppInfo = pm.getPackageInfo(e.provider.packageName, 0)
106106
val tasksAppLabel = tasksAppInfo.applicationInfo.loadLabel(pm)
107107

108-
val notify = NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_SYNC_ERRORS)
108+
val notify = NotificationCompat.Builder(context, registry.CHANNEL_SYNC_ERRORS)
109109
.setSmallIcon(R.drawable.ic_sync_problem_notify)
110110
.setContentTitle(context.getString(R.string.sync_error_tasks_too_old, tasksAppLabel))
111111
.setContentText(message)

app/src/main/kotlin/at/bitfire/davdroid/webdav/RandomAccessCallback.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class RandomAccessCallback @AssistedInject constructor(
7979
private val documentState = headResponse.toDocumentState() ?: throw IllegalArgumentException("Can only be used with ETag/Last-Modified")
8080

8181
private val notificationManager = NotificationManagerCompat.from(context)
82-
private val notification = NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_STATUS)
82+
private val notification = NotificationCompat.Builder(context, notificationRegistry.CHANNEL_STATUS)
8383
.setPriority(NotificationCompat.PRIORITY_LOW)
8484
.setCategory(NotificationCompat.CATEGORY_STATUS)
8585
.setContentTitle(context.getString(R.string.webdav_notification_access))

app/src/main/kotlin/at/bitfire/davdroid/webdav/StreamingFileDescriptor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StreamingFileDescriptor @AssistedInject constructor(
6262
var transferred: Long = 0
6363

6464
private val notificationManager = NotificationManagerCompat.from(context)
65-
private val notification = NotificationCompat.Builder(context, NotificationRegistry.CHANNEL_STATUS)
65+
private val notification = NotificationCompat.Builder(context, notificationRegistry.CHANNEL_STATUS)
6666
.setPriority(NotificationCompat.PRIORITY_LOW)
6767
.setCategory(NotificationCompat.CATEGORY_STATUS)
6868
.setContentText(dav.fileName())

0 commit comments

Comments
 (0)