Add database task notification

This commit is contained in:
J-Jamet
2019-07-26 17:08:05 +02:00
parent 63b80634c2
commit 2202f718b9
4 changed files with 108 additions and 0 deletions

View File

@@ -142,6 +142,10 @@
</activity>
<service
android:name="com.kunzisoft.keepass.database.action.DatabaseTaskNotificationService"
android:enabled="true"
android:exported="false" />
<!-- Receiver for Keyboard -->
<receiver
android:name="com.kunzisoft.keepass.magikeyboard.receiver.NotificationDeleteBroadcastReceiver"

View File

@@ -0,0 +1,87 @@
package com.kunzisoft.keepass.database.action
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.util.Log
import android.util.TypedValue
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.stylish.Stylish
class DatabaseTaskNotificationService : Service() {
private var notificationManager: NotificationManager? = null
private val notificationId = 532
private var colorNotificationAccent: Int = 0
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Create notification channel for Oreo+
if (Build.VERSION.SDK_INT >= 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"
}
}

View File

@@ -1,8 +1,11 @@
package com.kunzisoft.keepass.database.action
import android.content.Intent
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.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
@@ -21,10 +24,18 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
private var actionRunnableAsyncTask: ActionRunnableAsyncTask? = null
var actionFinishInUIThread: ActionRunnable? = null
private var intentDatabaseTask:Intent = Intent(activity, DatabaseTaskNotificationService::class.java)
init {
actionRunnableAsyncTask = ActionRunnableAsyncTask(progressTaskDialogFragment,
{
activity.runOnUiThread {
intentDatabaseTask.putExtra(DATABASE_TASK_TITLE_KEY, titleId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intentDatabaseTask)
} else {
activity.startService(intentDatabaseTask)
}
TimeoutHelper.temporarilyDisableTimeout()
// Show the dialog
ProgressTaskDialogFragment.start(activity, progressTaskDialogFragment)
@@ -35,6 +46,7 @@ open class ProgressDialogThread(private val activity: FragmentActivity,
// Remove the progress task
ProgressTaskDialogFragment.stop(activity)
TimeoutHelper.releaseTemporarilyDisableTimeoutAndLockIfTimeout(activity)
activity.stopService(intentDatabaseTask)
}
})
}

View File

@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M13,2.05v3.03c3.39,0.49 6,3.39 6,6.92 0,0.9 -0.18,1.75 -0.48,2.54l2.6,1.53c0.56,-1.24 0.88,-2.62 0.88,-4.07 0,-5.18 -3.95,-9.45 -9,-9.95zM12,19c-3.87,0 -7,-3.13 -7,-7 0,-3.53 2.61,-6.43 6,-6.92V2.05c-5.06,0.5 -9,4.76 -9,9.95 0,5.52 4.47,10 9.99,10 3.31,0 6.24,-1.61 8.06,-4.09l-2.6,-1.53C16.17,17.98 14.21,19 12,19z"/>
</vector>