Change notification icons

This commit is contained in:
J-Jamet
2019-10-29 10:09:46 +01:00
parent 2737755b85
commit c563787f73
3 changed files with 50 additions and 26 deletions

View File

@@ -29,6 +29,7 @@ import com.kunzisoft.keepass.database.exception.SamsungClipboardException
import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.model.EntryInfo
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.ClipboardHelper import com.kunzisoft.keepass.timeout.ClipboardHelper
import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.timeout.TimeoutHelper.NEVER import com.kunzisoft.keepass.timeout.TimeoutHelper.NEVER
import com.kunzisoft.keepass.utils.LOCK_ACTION import com.kunzisoft.keepass.utils.LOCK_ACTION
import java.util.* import java.util.*
@@ -57,10 +58,9 @@ class ClipboardEntryNotificationService : LockNotificationService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//Get settings //Get settings
val prefs = PreferenceManager.getDefaultSharedPreferences(this) notificationTimeoutMilliSecs = PreferenceManager.getDefaultSharedPreferences(this)
val timeoutClipboardClear = prefs.getString(getString(R.string.clipboard_timeout_key), .getString(getString(R.string.clipboard_timeout_key),
getString(R.string.clipboard_timeout_default)) ?: "6000" getString(R.string.clipboard_timeout_default))?.toLong() ?: TimeoutHelper.DEFAULT_TIMEOUT
notificationTimeoutMilliSecs = java.lang.Long.parseLong(timeoutClipboardClear)
when { when {
intent == null -> Log.w(TAG, "null intent") intent == null -> Log.w(TAG, "null intent")
@@ -209,6 +209,7 @@ class ClipboardEntryNotificationService : LockNotificationService() {
} }
override fun onDestroy() { override fun onDestroy() {
cleanClipboard()
stopTask(cleanCopyNotificationTimerTask) stopTask(cleanCopyNotificationTimerTask)
cleanCopyNotificationTimerTask = null cleanCopyNotificationTimerTask = null
@@ -236,13 +237,15 @@ class ClipboardEntryNotificationService : LockNotificationService() {
(entry.containsCustomFieldsProtected() && PreferencesUtil.allowCopyPasswordAndProtectedFields(context)) (entry.containsCustomFieldsProtected() && PreferencesUtil.allowCopyPasswordAndProtectedFields(context))
) )
var startService = false
val intent = Intent(context, ClipboardEntryNotificationService::class.java)
// If notifications enabled in settings // If notifications enabled in settings
// Don't if application timeout // Don't if application timeout
if (PreferencesUtil.isClipboardNotificationsEnable(context)) { if (PreferencesUtil.isClipboardNotificationsEnable(context)) {
if (containsUsernameToCopy || containsPasswordToCopy || containsExtraFieldToCopy) { if (containsUsernameToCopy || containsPasswordToCopy || containsExtraFieldToCopy) {
// username already copied, waiting for user's action before copy password. // username already copied, waiting for user's action before copy password.
val intent = Intent(context, ClipboardEntryNotificationService::class.java)
intent.action = ACTION_NEW_NOTIFICATION intent.action = ACTION_NEW_NOTIFICATION
intent.putExtra(EXTRA_ENTRY_TITLE, entry.title) intent.putExtra(EXTRA_ENTRY_TITLE, entry.title)
// Construct notification fields // Construct notification fields
@@ -286,10 +289,14 @@ class ClipboardEntryNotificationService : LockNotificationService() {
} }
// Add notifications // Add notifications
startService = true
intent.putParcelableArrayListExtra(EXTRA_FIELDS, notificationFields) intent.putParcelableArrayListExtra(EXTRA_FIELDS, notificationFields)
context.startService(intent) context.startService(intent)
} }
} }
if (!startService)
context.stopService(intent)
} }
} }
} }

View File

@@ -21,12 +21,8 @@ class KeyboardEntryNotificationService : LockNotificationService() {
private var pendingDeleteIntent: PendingIntent? = null private var pendingDeleteIntent: PendingIntent? = null
private fun stopNotificationAndSendLockIfNeeded() { private fun stopNotificationAndSendLockIfNeeded() {
// Remove the entry from the keyboard
MagikIME.removeEntry(this)
// Clear the entry if define in preferences // Clear the entry if define in preferences
val sharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(this) if (PreferencesUtil.isClearKeyboardNotificationEnable(this)) {
if (sharedPreferences.getBoolean(getString(R.string.keyboard_notification_entry_clear_close_key),
resources.getBoolean(R.bool.keyboard_notification_entry_clear_close_default))) {
sendBroadcast(Intent(LOCK_ACTION)) sendBroadcast(Intent(LOCK_ACTION))
} }
// Stop the service // Stop the service
@@ -34,6 +30,11 @@ class KeyboardEntryNotificationService : LockNotificationService() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//Get settings
notificationTimeoutMilliSecs = PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.keyboard_entry_timeout_key),
getString(R.string.timeout_default))?.toLong() ?: TimeoutHelper.DEFAULT_TIMEOUT
when { when {
intent == null -> Log.w(TAG, "null intent") intent == null -> Log.w(TAG, "null intent")
ACTION_CLEAN_KEYBOARD_ENTRY == intent.action -> { ACTION_CLEAN_KEYBOARD_ENTRY == intent.action -> {
@@ -76,19 +77,7 @@ class KeyboardEntryNotificationService : LockNotificationService() {
stopTask(cleanNotificationTimerTask) stopTask(cleanNotificationTimerTask)
// Timeout only if notification clear is available // Timeout only if notification clear is available
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) if (PreferencesUtil.isClearKeyboardNotificationEnable(this)) {
if (sharedPreferences.getBoolean(getString(R.string.keyboard_notification_entry_clear_close_key),
resources.getBoolean(R.bool.keyboard_notification_entry_clear_close_default))) {
val keyboardTimeout = sharedPreferences.getString(getString(R.string.keyboard_entry_timeout_key),
getString(R.string.timeout_default))
notificationTimeoutMilliSecs = try {
keyboardTimeout?.let {
java.lang.Long.parseLong(keyboardTimeout)
} ?: 0
} catch (e: NumberFormatException) {
TimeoutHelper.DEFAULT_TIMEOUT
}
if (notificationTimeoutMilliSecs != TimeoutHelper.NEVER) { if (notificationTimeoutMilliSecs != TimeoutHelper.NEVER) {
cleanNotificationTimerTask = Thread { cleanNotificationTimerTask = Thread {
val maxPos = 100 val maxPos = 100
@@ -111,7 +100,15 @@ class KeyboardEntryNotificationService : LockNotificationService() {
} }
} }
override fun onTaskRemoved(rootIntent: Intent?) {
MagikIME.removeEntry(this)
super.onTaskRemoved(rootIntent)
}
override fun onDestroy() { override fun onDestroy() {
// Remove the entry from the keyboard
MagikIME.removeEntry(this)
stopTask(cleanNotificationTimerTask) stopTask(cleanNotificationTimerTask)
cleanNotificationTimerTask = null cleanNotificationTimerTask = null
@@ -129,13 +126,27 @@ class KeyboardEntryNotificationService : LockNotificationService() {
const val ACTION_CLEAN_KEYBOARD_ENTRY = "ACTION_CLEAN_KEYBOARD_ENTRY" const val ACTION_CLEAN_KEYBOARD_ENTRY = "ACTION_CLEAN_KEYBOARD_ENTRY"
fun launchNotificationIfAllowed(context: Context, entry: EntryInfo) { fun launchNotificationIfAllowed(context: Context, entry: EntryInfo) {
val containsUsernameToCopy = entry.username.isNotEmpty()
val containsPasswordToCopy = entry.password.isNotEmpty()
val containsExtraFieldToCopy = entry.customFields.isNotEmpty()
var startService = false
val intent = Intent(context, KeyboardEntryNotificationService::class.java)
// Show the notification if allowed in Preferences // Show the notification if allowed in Preferences
if (PreferencesUtil.isKeyboardNotificationEntryEnable(context)) { if (PreferencesUtil.isKeyboardNotificationEntryEnable(context)) {
context.startService(Intent(context, KeyboardEntryNotificationService::class.java).apply { if (containsUsernameToCopy || containsPasswordToCopy || containsExtraFieldToCopy) {
startService = true
context.startService(intent.apply {
putExtra(ENTRY_INFO_KEY, entry) putExtra(ENTRY_INFO_KEY, entry)
}) })
} }
} }
if (!startService)
context.stopService(intent)
}
} }
} }

View File

@@ -209,6 +209,12 @@ object PreferencesUtil {
context.resources.getBoolean(R.bool.clear_clipboard_notification_default)) context.resources.getBoolean(R.bool.clear_clipboard_notification_default))
} }
fun isClearKeyboardNotificationEnable(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.keyboard_notification_entry_clear_close_key),
context.resources.getBoolean(R.bool.keyboard_notification_entry_clear_close_default))
}
fun setAllowCopyPasswordAndProtectedFields(context: Context, allowCopy: Boolean) { fun setAllowCopyPasswordAndProtectedFields(context: Context, allowCopy: Boolean) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context) val prefs = PreferenceManager.getDefaultSharedPreferences(context)
prefs.edit() prefs.edit()