From c398b92eb150174e305688f115788d7328707131 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 13 Aug 2019 17:12:35 +0200 Subject: [PATCH] Encapsulate DatabaseTaskNotification --- app/src/main/AndroidManifest.xml | 2 +- .../action/DatabaseTaskNotificationService.kt | 87 ------------------- .../database/action/ProgressDialogThread.kt | 3 +- .../DatabaseTaskNotificationService.kt | 44 ++++++++++ 4 files changed, 47 insertions(+), 89 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/action/DatabaseTaskNotificationService.kt create mode 100644 app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9906df0b5..0f48dc49b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -142,7 +142,7 @@ = Build.VERSION_CODES.O) { - val channel = NotificationChannel(CHANNEL_ID_DATABASE_TASK, - CHANNEL_NAME_DATABASE_TASK, - NotificationManager.IMPORTANCE_LOW) - notificationManager?.createNotificationChannel(channel) - } - - // Get the color - setTheme(Stylish.getThemeId(this)) - val typedValue = TypedValue() - val theme = theme - theme.resolveAttribute(R.attr.colorPrimary, typedValue, true) - colorNotificationAccent = typedValue.data - } - - override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - if (intent == null) { - Log.w(TAG, "null intent") - } else { - newNotification(intent.getIntExtra(DATABASE_TASK_TITLE_KEY, R.string.saving_database)) - } - return START_NOT_STICKY - } - - private fun newNotification(title: Int) { - - val builder = NotificationCompat.Builder(this, CHANNEL_ID_DATABASE_TASK) - .setSmallIcon(R.drawable.ic_data_usage_white_24dp) - .setColor(colorNotificationAccent) - .setContentTitle(getString(title)) - .setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setVisibility(NotificationCompat.VISIBILITY_SECRET) - //.setContentText(getString(R.string.saving_database)) - .setAutoCancel(false) - .setContentIntent(null) - startForeground(notificationId, builder.build()) - } - - override fun onDestroy() { - - notificationManager?.cancel(notificationId) - - super.onDestroy() - } - - companion object { - - private val TAG = DatabaseTaskNotificationService::class.java.name - - const val DATABASE_TASK_TITLE_KEY = "DatabaseTaskTitle" - - private const val CHANNEL_ID_DATABASE_TASK = "com.kunzisoft.database.notification.task.channel" - private const val CHANNEL_NAME_DATABASE_TASK = "Database task notification" - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt index bc3099a74..f086f46bc 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/ProgressDialogThread.kt @@ -5,7 +5,8 @@ import android.os.AsyncTask import android.os.Build import android.support.annotation.StringRes import android.support.v4.app.FragmentActivity -import com.kunzisoft.keepass.database.action.DatabaseTaskNotificationService.Companion.DATABASE_TASK_TITLE_KEY +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.DATABASE_TASK_TITLE_KEY import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment import com.kunzisoft.keepass.tasks.ProgressTaskUpdater diff --git a/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt new file mode 100644 index 000000000..bf2e37132 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/DatabaseTaskNotificationService.kt @@ -0,0 +1,44 @@ +package com.kunzisoft.keepass.notifications + +import android.content.Intent +import android.util.Log +import com.kunzisoft.keepass.R + +class DatabaseTaskNotificationService : NotificationService() { + + private val notificationId = 532 + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent == null) { + Log.w(TAG, "null intent") + } else { + newNotification(intent.getIntExtra(DATABASE_TASK_TITLE_KEY, R.string.saving_database)) + } + return START_NOT_STICKY + } + + private fun newNotification(title: Int) { + + val builder = buildNewNotification() + .setSmallIcon(R.drawable.ic_data_usage_white_24dp) + .setContentTitle(getString(title)) + .setAutoCancel(false) + .setContentIntent(null) + startForeground(notificationId, builder.build()) + } + + override fun onDestroy() { + + notificationManager?.cancel(notificationId) + + super.onDestroy() + } + + companion object { + + private val TAG = DatabaseTaskNotificationService::class.java.name + + const val DATABASE_TASK_TITLE_KEY = "DatabaseTaskTitle" + } + +} \ No newline at end of file