Skip to content

Commit

Permalink
use notification groups
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Jan 19, 2024
1 parent 84da4d5 commit dc1e122
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.pkay.rcloneexplorer.notifications

import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
Expand Down Expand Up @@ -58,14 +59,16 @@ class GenericSyncNotification(var mContext: Context) {

}

fun setNotificationChannel(channelID: String, channelName: String, description: String) {
fun setNotificationChannel(channelID: String, channelName: String, description: String, groupID: String, groupDescription: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_LOW)
channel.description = description
// Register the channel with the system
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupID, groupDescription))
channel.group = groupID
notificationManager.createNotificationChannel(channel)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SyncServiceNotifications(var mContext: Context) {
const val CHANNEL_SUCCESS_ID = "ca.pkay.rcexplorer.sync_service_success"
const val CHANNEL_FAIL_ID = "ca.pkay.rcexplorer.sync_service_fail"

const val GROUP_ID = "ca.pkay.rcexplorer.sync_service.group"

const val PERSISTENT_NOTIFICATION_ID_FOR_SYNC = 162
const val CANCEL_ID_NOTSET = "CANCEL_ID_NOTSET"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.preference.PreferenceManager
import androidx.work.WorkManager
import ca.pkay.rcloneexplorer.BroadcastReceivers.SyncRestartAction
import ca.pkay.rcloneexplorer.R
Expand All @@ -18,13 +15,36 @@ import ca.pkay.rcloneexplorer.util.NotificationUtils
import ca.pkay.rcloneexplorer.workmanager.SyncWorker
import ca.pkay.rcloneexplorer.workmanager.SyncWorker.Companion.EXTRA_TASK_ID
import de.felixnuesse.extract.extensions.tag
import de.felixnuesse.extract.notifications.DiscardChannels
import java.util.UUID

abstract class WorkerNotification(var mContext: Context) {

abstract val CHANNEL_ID: String
abstract val CHANNEL_SUCCESS_ID: String
abstract val CHANNEL_FAIL_ID: String


val CHANNEL_SUCCESS_ID = this.CHANNEL_ID +"_success"
open val CHANNEL_FAIL_ID = this.CHANNEL_ID+"_fail"

val GROUP_ID = "de.felixnuesse.extract.taskworker.group"
val GROUP_DESCRIPTION = mContext.getString(R.string.workernotification_group_description)



// Todo: Either make all resources, or all strings. Not both.
abstract val titleStartingSync: Int
abstract val serviceOngoingTitle: Int
abstract val serviceFailed: Int
abstract val serviceCancelled: Int
abstract val serviceSuccess: Int


abstract val channel_ongoing_title: String
abstract val channel_ongoing_description: String
abstract val channel_success_title: String
abstract val channel_success_description: String
abstract val channel_failed_title: String
abstract val channel_failed_description: String


abstract val PERSISTENT_NOTIFICATION_ID: Int
Expand All @@ -33,14 +53,40 @@ abstract class WorkerNotification(var mContext: Context) {
const val CANCEL_ID_NOTSET = "CANCEL_ID_NOTSET"
}

private val OPERATION_FAILED_GROUP = "ca.pkay.rcexplorer.OPERATION_FAILED_GROUP" // Check if those can stay the same when channels are muted
// Check if those can stay the same when channels are muted
private val OPERATION_FAILED_GROUP = "ca.pkay.rcexplorer.OPERATION_FAILED_GROUP"
private val OPERATION_SUCCESS_GROUP = "ca.pkay.rcexplorer.OPERATION_SUCCESS_GROUP"


private var mCancelUnsetId: UUID = UUID.randomUUID()
private var mCancelId: UUID = mCancelUnsetId


fun generateChannels() {
DiscardChannels.discard(mContext)
GenericSyncNotification(mContext).setNotificationChannel(
CHANNEL_ID,
channel_ongoing_title,
channel_ongoing_description,
GROUP_ID,
GROUP_DESCRIPTION
)
GenericSyncNotification(mContext).setNotificationChannel(
CHANNEL_SUCCESS_ID,
channel_success_title,
channel_success_description,
GROUP_ID,
GROUP_DESCRIPTION
)
GenericSyncNotification(mContext).setNotificationChannel(
CHANNEL_FAIL_ID,
channel_failed_title,
channel_failed_description,
GROUP_ID,
GROUP_DESCRIPTION
)
}


fun setCancelId(id: UUID) {
mCancelId = id
Expand All @@ -60,7 +106,7 @@ abstract class WorkerNotification(var mContext: Context) {
)
val builder = NotificationCompat.Builder(mContext, CHANNEL_FAIL_ID)
.setSmallIcon(R.drawable.ic_twotone_cloud_error_24)
.setContentTitle(mContext.getString(R.string.operation_failed))
.setContentTitle(mContext.getString(serviceFailed))
.setContentText(content)
.setStyle(
NotificationCompat.BigTextStyle().bigText(content)
Expand Down Expand Up @@ -89,7 +135,7 @@ abstract class WorkerNotification(var mContext: Context) {
)
val builder = NotificationCompat.Builder(mContext, CHANNEL_FAIL_ID)
.setSmallIcon(R.drawable.ic_twotone_cloud_error_24)
.setContentTitle(mContext.getString(R.string.operation_failed_cancelled))
.setContentTitle(mContext.getString(serviceCancelled))
.setContentText(content)
.setStyle(
NotificationCompat.BigTextStyle().bigText(content)
Expand All @@ -107,7 +153,7 @@ abstract class WorkerNotification(var mContext: Context) {
fun showSuccessNotification(title: String, content: String, notificationId: Int) {
val builder = NotificationCompat.Builder(mContext, CHANNEL_SUCCESS_ID)
.setSmallIcon(R.drawable.ic_twotone_cloud_done_24)
.setContentTitle(mContext.getString(R.string.operation_success, title))
.setContentTitle(mContext.getString(serviceSuccess, title))
.setContentText(content)
.setStyle(
NotificationCompat.BigTextStyle().bigText(
Expand All @@ -132,7 +178,7 @@ abstract class WorkerNotification(var mContext: Context) {
}

val builder = GenericSyncNotification(mContext).updateGenericNotification(
mContext.getString(R.string.syncing_service, title),
mContext.getString(serviceOngoingTitle, title),
content,
R.drawable.ic_twotone_rounded_cloud_sync_24,
bigTextArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import androidx.work.WorkManager
import androidx.work.WorkRequest
import ca.pkay.rcloneexplorer.Items.FileItem
import ca.pkay.rcloneexplorer.Items.RemoteItem
import de.felixnuesse.extract.notifications.implementations.DeleteWorkerNotification
import de.felixnuesse.extract.notifications.implementations.DownloadWorkerNotification
import de.felixnuesse.extract.notifications.implementations.MoveWorkerNotification
import de.felixnuesse.extract.notifications.implementations.UploadWorkerNotification
import java.util.Random


Expand All @@ -22,6 +26,8 @@ class EphemeralTaskManager(private var mContext: Context) {
downloadItem: FileItem,
selectedPath: String) {

DownloadWorkerNotification(context).generateChannels()

val data = Data.Builder()
data.putString(EphemeralWorker.EPHEMERAL_TYPE, Type.DOWNLOAD.name)
addRemoteItemToData(EphemeralWorker.REMOTE, remote, data)
Expand All @@ -36,8 +42,10 @@ class EphemeralTaskManager(private var mContext: Context) {
context: Context,
remote: RemoteItem,
file: String,
targetpath: String
) {
targetpath: String) {

UploadWorkerNotification(context).generateChannels()

val data = Data.Builder()
data.putString(EphemeralWorker.EPHEMERAL_TYPE, Type.UPLOAD.name)
addRemoteItemToData(EphemeralWorker.REMOTE, remote, data)
Expand All @@ -55,6 +63,9 @@ class EphemeralTaskManager(private var mContext: Context) {
file: FileItem,
readablePath: String
) {

MoveWorkerNotification(context).generateChannels()

val data = Data.Builder()
data.putString(EphemeralWorker.EPHEMERAL_TYPE, Type.MOVE.name)
addRemoteItemToData(EphemeralWorker.REMOTE, remote, data)
Expand All @@ -70,6 +81,9 @@ class EphemeralTaskManager(private var mContext: Context) {
file: FileItem,
currentPath: String
) {

DeleteWorkerNotification(context).generateChannels()

val data = Data.Builder()
data.putString(EphemeralWorker.EPHEMERAL_TYPE, Type.DELETE.name)
addRemoteItemToData(EphemeralWorker.REMOTE, remote, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@ import androidx.preference.PreferenceManager
import androidx.work.ForegroundInfo
import androidx.work.Worker
import androidx.work.WorkerParameters
import ca.pkay.rcloneexplorer.Database.DatabaseHandler
import ca.pkay.rcloneexplorer.Items.FileItem
import ca.pkay.rcloneexplorer.Items.RemoteItem
import ca.pkay.rcloneexplorer.Items.Task
import ca.pkay.rcloneexplorer.Log2File
import ca.pkay.rcloneexplorer.R
import ca.pkay.rcloneexplorer.Rclone
import ca.pkay.rcloneexplorer.notifications.GenericSyncNotification
import ca.pkay.rcloneexplorer.notifications.prototypes.WorkerNotification
import ca.pkay.rcloneexplorer.notifications.support.StatusObject
import ca.pkay.rcloneexplorer.util.FLog
import ca.pkay.rcloneexplorer.util.SyncLog
import ca.pkay.rcloneexplorer.util.WifiConnectivitiyUtil
import de.felixnuesse.extract.extensions.tag
import de.felixnuesse.extract.notifications.implementations.DownloadWorkerNotification
import kotlinx.serialization.json.Json
import org.json.JSONException
import org.json.JSONObject
import java.io.BufferedReader
Expand All @@ -39,10 +35,12 @@ import java.io.InputStreamReader
import java.io.InterruptedIOException
import kotlin.random.Random
import android.util.Log
import de.felixnuesse.extract.notifications.implementations.UploadWorkerNotification


class EphemeralWorker (private var mContext: Context, workerParams: WorkerParameters): Worker(mContext, workerParams) {


companion object {
const val EPHEMERAL_TYPE = "TASK_EPHEMERAL_TYPE"
const val REMOTE = "REMOTE"
Expand Down Expand Up @@ -84,12 +82,10 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
private val ongoingNotificationID = Random.nextInt()


private var mTitle: String = mContext.getString(R.string.sync_service_notification_startingsync)

private var mTitle: String = getString(mNotificationManager?.titleStartingSync)

override fun doWork(): Result {

prepareNotifications()
registerBroadcastReceivers()

updateForegroundNotification(mNotificationManager?.updateNotification(
Expand All @@ -104,10 +100,6 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
val type = Type.valueOf(inputData.getString(EPHEMERAL_TYPE) ?: "")
mNotificationManager = prepareNotificationManager(type)

if (mNotificationManager == null){
log("Warning: No valid Notifications are available")
}

val remoteItem = getRemoteitemFromParcel(REMOTE)
if(remoteItem == null){
log("$REMOTE: No valid remote was passed!")
Expand Down Expand Up @@ -201,9 +193,10 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
postSync()
}

fun prepareNotificationManager(type: Type): WorkerNotification? {
fun prepareNotificationManager(type: Type): WorkerNotification {
return when(type){
Type.DOWNLOAD -> DownloadWorkerNotification(mContext)
Type.UPLOAD -> UploadWorkerNotification(mContext)
else -> DownloadWorkerNotification(mContext)
}
}
Expand Down Expand Up @@ -386,26 +379,6 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
return true
}

private fun prepareNotifications() {

GenericSyncNotification(mContext).setNotificationChannel(
mNotificationManager?.CHANNEL_ID?:"",
getString(R.string.sync_service_notification_channel_title),
getString(R.string.sync_service_notification_channel_description)
)
GenericSyncNotification(mContext).setNotificationChannel(
mNotificationManager?.CHANNEL_SUCCESS_ID?:"",
getString(R.string.sync_service_notification_channel_success_title),
getString(R.string.sync_service_notification_channel_success_description)
)
GenericSyncNotification(mContext).setNotificationChannel(
mNotificationManager?.CHANNEL_FAIL_ID?:"",
getString(R.string.sync_service_notification_channel_fail_title),
getString(R.string.sync_service_notification_channel_fail_description)
)

}

private fun sendUploadFinishedBroadcast(remote: String, path: String?) {
val intent = Intent()
intent.action = getString(R.string.background_service_broadcast)
Expand All @@ -417,8 +390,11 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
// Creates an instance of ForegroundInfo which can be used to update the
// ongoing notification.
private fun updateForegroundNotification(notification: Notification?) {
notification?.let {
setForegroundAsync(ForegroundInfo(ongoingNotificationID, it, FOREGROUND_SERVICE_TYPE_DATA_SYNC))

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
notification?.let {
setForegroundAsync(ForegroundInfo(ongoingNotificationID, it, FOREGROUND_SERVICE_TYPE_DATA_SYNC))
}
}
}

Expand All @@ -427,8 +403,12 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
FLog.e(tag(), "EphemeralWorker: $message")
}

private fun getString(@StringRes resId: Int): String {
return mContext.getString(resId)
private fun getString(@StringRes resId: Int?): String {
return if (resId == null) {
"Error"
} else {
mContext.getString(resId)
}
}

private fun registerBroadcastReceivers() {
Expand Down Expand Up @@ -459,6 +439,7 @@ class EphemeralWorker (private var mContext: Context, workerParams: WorkerParame
parcel.setDataPosition(0)
return FileItem.CREATOR.createFromParcel(parcel)
}

private fun getRemoteitemFromParcel(key: String): RemoteItem? {

val sourceParcelByteArray = inputData.getByteArray(key)
Expand Down
21 changes: 15 additions & 6 deletions app/src/main/java/ca/pkay/rcloneexplorer/workmanager/SyncWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ca.pkay.rcloneexplorer.Rclone
import ca.pkay.rcloneexplorer.notifications.GenericSyncNotification
import ca.pkay.rcloneexplorer.notifications.ReportNotifications
import ca.pkay.rcloneexplorer.notifications.SyncServiceNotifications
import ca.pkay.rcloneexplorer.notifications.SyncServiceNotifications.Companion.GROUP_ID
import ca.pkay.rcloneexplorer.notifications.support.StatusObject
import ca.pkay.rcloneexplorer.util.FLog
import ca.pkay.rcloneexplorer.util.SyncLog
Expand Down Expand Up @@ -345,24 +346,32 @@ class SyncWorker (private var mContext: Context, workerParams: WorkerParameters)
private fun prepareNotifications() {

GenericSyncNotification(mContext).setNotificationChannel(
SyncServiceNotifications.CHANNEL_ID,
getString(R.string.sync_service_notification_channel_title),
getString(R.string.sync_service_notification_channel_description)
SyncServiceNotifications.CHANNEL_ID,
getString(R.string.sync_service_notification_channel_title),
getString(R.string.sync_service_notification_channel_description),
GROUP_ID,
getString(R.string.sync_service_notification_group)
)
GenericSyncNotification(mContext).setNotificationChannel(
SyncServiceNotifications.CHANNEL_SUCCESS_ID,
getString(R.string.sync_service_notification_channel_success_title),
getString(R.string.sync_service_notification_channel_success_description)
getString(R.string.sync_service_notification_channel_success_description),
GROUP_ID,
getString(R.string.sync_service_notification_group)
)
GenericSyncNotification(mContext).setNotificationChannel(
SyncServiceNotifications.CHANNEL_FAIL_ID,
getString(R.string.sync_service_notification_channel_fail_title),
getString(R.string.sync_service_notification_channel_fail_description)
getString(R.string.sync_service_notification_channel_fail_description),
GROUP_ID,
getString(R.string.sync_service_notification_group)
)
GenericSyncNotification(mContext).setNotificationChannel(
ReportNotifications.CHANNEL_REPORT_ID,
getString(R.string.sync_service_notification_channel_report_title),
getString(R.string.sync_service_notification_channel_report_description)
getString(R.string.sync_service_notification_channel_report_description),
GROUP_ID,
getString(R.string.sync_service_notification_group)
)

}
Expand Down
Loading

0 comments on commit dc1e122

Please sign in to comment.