Skip to content

Commit

Permalink
Refactor away usage of Compat notification classes
Browse files Browse the repository at this point in the history
Replace usage of the androidx backward compatability classes in
UpdateNotifier with the standard Android classes, using
Build.VERSION.SDK_INT to determine exactly which calls to use.

This will make it much clearer in future when minSdk is increased that
particular parts of the code are for specified API levels as opposed to
the backward compatability classes which wouldn't be immediately obvious
they weren't needed.
  • Loading branch information
ribbons committed Mar 3, 2024
1 parent 10505ac commit ec39b00
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions app/src/main/java/com/nerdoftheherd/tasker/rsync/UpdateNotifier.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright © 2022-2023 Matt Robinson
* Copyright © 2022-2024 Matt Robinson
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.nerdoftheherd.tasker.rsync

import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
Expand All @@ -16,9 +17,6 @@ import android.content.pm.PackageManager
import android.os.Build
import android.util.AtomicFile
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.nerdoftheherd.tasker.rsync.activities.UpdateActivity
import java.io.File
import java.net.HttpURLConnection
Expand Down Expand Up @@ -47,35 +45,30 @@ class UpdateNotifier {

if (info.version > current) {
Log.d(TAG, "Update found, about to show notification")
createNotificationChannel(context)
showNotification(context, info)
}
}

private fun createNotificationChannel(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}

val channel =
NotificationChannel(
NOTIF_CHANNEL,
context.getString(R.string.channel_updates),
NotificationManager.IMPORTANCE_LOW,
)

val notificationManager =
private fun showNotification(
context: Context,
info: VersionInfo,
) {
val manager =
context.getSystemService(
Context.NOTIFICATION_SERVICE,
) as NotificationManager

notificationManager.createNotificationChannel(channel)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(
NOTIF_CHANNEL,
context.getString(R.string.channel_updates),
NotificationManager.IMPORTANCE_LOW,
)

manager.createNotificationChannel(channel)
}

private fun showNotification(
context: Context,
info: VersionInfo,
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
context.checkSelfPermission(
Manifest.permission.POST_NOTIFICATIONS,
Expand All @@ -98,19 +91,25 @@ class UpdateNotifier {
val updatePI = PendingIntent.getActivity(context, 0, updateIntent, pendingFlags)

val notifBuilder =
NotificationCompat.Builder(context, NOTIF_CHANNEL)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, NOTIF_CHANNEL)
} else {
@Suppress("DEPRECATION")
Notification.Builder(context)
}
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.update_available))
.setContentText(context.getString(R.string.update_notif_text, info.version))
.setContentIntent(updatePI)
.setColor(ContextCompat.getColor(context, R.color.primary))

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notifBuilder.priority = NotificationCompat.PRIORITY_LOW
@Suppress("DEPRECATION")
notifBuilder.setPriority(Notification.PRIORITY_LOW)
} else {
notifBuilder.setColor(context.getColor(R.color.primary))
}

NotificationManagerCompat.from(context)
.notify(NOTIF_ID, notifBuilder.build())
manager.notify(NOTIF_ID, notifBuilder.build())
}
}
}

0 comments on commit ec39b00

Please sign in to comment.