Add notification for opened database and use compat notification manager

This commit is contained in:
J-Jamet
2019-10-28 11:50:06 +01:00
parent 3732ff1ebc
commit 5a6c466ebd
8 changed files with 98 additions and 16 deletions

View File

@@ -141,7 +141,10 @@
</intent-filter>
</activity>
<service
android:name="com.kunzisoft.keepass.notifications.DatabaseOpenNotificationService"
android:enabled="true"
android:exported="false" />
<service
android:name="com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService"
android:enabled="true"

View File

@@ -20,12 +20,14 @@
package com.kunzisoft.keepass.database.action
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.kunzisoft.keepass.app.database.CipherDatabaseAction
import com.kunzisoft.keepass.app.database.CipherDatabaseEntity
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.exception.LoadDatabaseException
import com.kunzisoft.keepass.notifications.DatabaseOpenNotificationService
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
@@ -82,6 +84,9 @@ class LoadDatabaseRunnable(private val context: Context,
catch (e: LoadDatabaseException) {
finishRun(false, e)
}
// Start the opening notification
context.startService(Intent(context, DatabaseOpenNotificationService::class.java))
}
override fun onFinishRun(result: Result) {

View File

@@ -33,7 +33,7 @@ import com.kunzisoft.keepass.timeout.TimeoutHelper.NEVER
import com.kunzisoft.keepass.utils.LOCK_ACTION
import java.util.*
class ClipboardEntryNotificationService : EntryNotificationService() {
class ClipboardEntryNotificationService : LockNotificationService() {
override var notificationId = 485
private var cleanNotificationTimerTask: Thread? = null

View File

@@ -0,0 +1,63 @@
package com.kunzisoft.keepass.notifications
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.GroupActivity
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.utils.LOCK_ACTION
class DatabaseOpenNotificationService: LockNotificationService() {
override var notificationId: Int = 340
private fun stopNotificationAndSendLock() {
// Send lock action
sendBroadcast(Intent(LOCK_ACTION))
// Stop the service
stopSelf()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when(intent?.action) {
ACTION_CLOSE_DATABASE -> {
stopNotificationAndSendLock()
}
else -> {
val databaseIntent = Intent(this, GroupActivity::class.java)
var pendingDatabaseFlag = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingDatabaseFlag = PendingIntent.FLAG_IMMUTABLE
}
val pendingDatabaseIntent = PendingIntent.getActivity(this, 0, databaseIntent, pendingDatabaseFlag)
val deleteIntent = Intent(this, DatabaseOpenNotificationService::class.java).apply {
action = ACTION_CLOSE_DATABASE
}
val pendingDeleteIntent = PendingIntent.getService(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val database = Database.getInstance()
if (database.loaded) {
notificationManager?.notify(notificationId, buildNewNotification().apply {
setSmallIcon(R.drawable.notification_ic_data_usage_24dp)
setContentTitle(getString(R.string.database_opened))
setContentText(database.name + " (" + database.version + ")")
setAutoCancel(false)
setContentIntent(pendingDatabaseIntent)
setDeleteIntent(pendingDeleteIntent)
}.build())
} else {
stopSelf()
}
}
}
return START_STICKY
}
companion object {
const val ACTION_CLOSE_DATABASE = "ACTION_CLOSE_DATABASE"
}
}

View File

@@ -12,7 +12,7 @@ import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.utils.LOCK_ACTION
class KeyboardEntryNotificationService : EntryNotificationService() {
class KeyboardEntryNotificationService : LockNotificationService() {
override var notificationId = 486
private var cleanNotificationTimerTask: Thread? = null
@@ -74,7 +74,6 @@ class KeyboardEntryNotificationService : EntryNotificationService() {
notificationManager?.cancel(notificationId)
notificationManager?.notify(notificationId, builder.build())
stopTask(cleanNotificationTimerTask)
// Timeout only if notification clear is available
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

View File

@@ -6,7 +6,7 @@ import android.content.Intent
import android.content.IntentFilter
import com.kunzisoft.keepass.utils.LOCK_ACTION
abstract class EntryNotificationService : NotificationService() {
abstract class LockNotificationService : NotificationService() {
private var lockBroadcastReceiver: BroadcastReceiver? = null

View File

@@ -3,19 +3,19 @@ package com.kunzisoft.keepass.notifications
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 androidx.core.app.NotificationCompat
import android.util.TypedValue
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.stylish.Stylish
abstract class NotificationService : Service() {
protected var notificationManager: NotificationManager? = null
protected var notificationManager: NotificationManagerCompat? = null
private var colorNotificationAccent: Int = 0
protected abstract var notificationId: Int
@@ -27,17 +27,19 @@ abstract class NotificationService : Service() {
override fun onCreate() {
super.onCreate()
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager = NotificationManagerCompat.from(this)
// Create notification channel for Oreo+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID_KEEPASS,
CHANNEL_NAME_KEEPASS,
NotificationManager.IMPORTANCE_DEFAULT).apply {
enableVibration(false)
setSound(null, null)
if (notificationManager?.getNotificationChannel(CHANNEL_ID_KEEPASS) == null) {
val channel = NotificationChannel(CHANNEL_ID_KEEPASS,
CHANNEL_NAME_KEEPASS,
NotificationManager.IMPORTANCE_DEFAULT).apply {
enableVibration(false)
setSound(null, null)
}
notificationManager?.createNotificationChannel(channel)
}
notificationManager?.createNotificationChannel(channel)
}
// Get the color
@@ -51,11 +53,19 @@ abstract class NotificationService : Service() {
protected fun buildNewNotification(): NotificationCompat.Builder {
return NotificationCompat.Builder(this, CHANNEL_ID_KEEPASS)
.setColor(colorNotificationAccent)
.setGroup(GROUP_KEEPASS)
//TODO .setGroup(GROUP_KEEPASS)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
}
// TODO only for > lollipop
protected fun buildSummaryNotification(): NotificationCompat.Builder {
return buildNewNotification().apply {
setSmallIcon(R.drawable.notification_ic_data_usage_24dp)
setGroupSummary(true)
}
}
override fun onDestroy() {
notificationManager?.cancel(notificationId)
@@ -66,5 +76,6 @@ abstract class NotificationService : Service() {
const val CHANNEL_ID_KEEPASS = "com.kunzisoft.keepass.notification.channel"
const val CHANNEL_NAME_KEEPASS = "KeePass DX notification"
const val GROUP_KEEPASS = "GROUP_KEEPASS"
const val SUMMARY_ID = 0
}
}

View File

@@ -257,6 +257,7 @@
<string name="password_size_summary">Sets default size of the generated passwords</string>
<string name="list_password_generator_options_title">Password characters</string>
<string name="list_password_generator_options_summary">Set allowed password generator characters</string>
<string name="database_opened">Database opened</string>
<string name="clipboard">Clipboard</string>
<string name="clipboard_notifications_title">Clipboard notifications</string>
<string name="clipboard_notifications_summary">Enable clipboard notifications to copy fields when viewing an entry</string>