diff --git a/app/src/main/java/com/kunzisoft/keepass/notifications/ClipboardEntryNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/notifications/ClipboardEntryNotificationService.kt index cfbca19a8..730a1fce4 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/ClipboardEntryNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/ClipboardEntryNotificationService.kt @@ -29,6 +29,7 @@ import com.kunzisoft.keepass.database.exception.SamsungClipboardException import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.timeout.ClipboardHelper +import com.kunzisoft.keepass.timeout.TimeoutHelper import com.kunzisoft.keepass.timeout.TimeoutHelper.NEVER import com.kunzisoft.keepass.utils.LOCK_ACTION import java.util.* @@ -57,10 +58,9 @@ class ClipboardEntryNotificationService : LockNotificationService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { //Get settings - val prefs = PreferenceManager.getDefaultSharedPreferences(this) - val timeoutClipboardClear = prefs.getString(getString(R.string.clipboard_timeout_key), - getString(R.string.clipboard_timeout_default)) ?: "6000" - notificationTimeoutMilliSecs = java.lang.Long.parseLong(timeoutClipboardClear) + notificationTimeoutMilliSecs = PreferenceManager.getDefaultSharedPreferences(this) + .getString(getString(R.string.clipboard_timeout_key), + getString(R.string.clipboard_timeout_default))?.toLong() ?: TimeoutHelper.DEFAULT_TIMEOUT when { intent == null -> Log.w(TAG, "null intent") @@ -209,6 +209,7 @@ class ClipboardEntryNotificationService : LockNotificationService() { } override fun onDestroy() { + cleanClipboard() stopTask(cleanCopyNotificationTimerTask) cleanCopyNotificationTimerTask = null @@ -236,13 +237,15 @@ class ClipboardEntryNotificationService : LockNotificationService() { (entry.containsCustomFieldsProtected() && PreferencesUtil.allowCopyPasswordAndProtectedFields(context)) ) + var startService = false + val intent = Intent(context, ClipboardEntryNotificationService::class.java) + // If notifications enabled in settings // Don't if application timeout if (PreferencesUtil.isClipboardNotificationsEnable(context)) { if (containsUsernameToCopy || containsPasswordToCopy || containsExtraFieldToCopy) { // username already copied, waiting for user's action before copy password. - val intent = Intent(context, ClipboardEntryNotificationService::class.java) intent.action = ACTION_NEW_NOTIFICATION intent.putExtra(EXTRA_ENTRY_TITLE, entry.title) // Construct notification fields @@ -286,10 +289,14 @@ class ClipboardEntryNotificationService : LockNotificationService() { } // Add notifications + startService = true intent.putParcelableArrayListExtra(EXTRA_FIELDS, notificationFields) context.startService(intent) } } + + if (!startService) + context.stopService(intent) } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/notifications/KeyboardEntryNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/notifications/KeyboardEntryNotificationService.kt index 35d66c395..ec4d87b03 100644 --- a/app/src/main/java/com/kunzisoft/keepass/notifications/KeyboardEntryNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/notifications/KeyboardEntryNotificationService.kt @@ -21,12 +21,8 @@ class KeyboardEntryNotificationService : LockNotificationService() { private var pendingDeleteIntent: PendingIntent? = null private fun stopNotificationAndSendLockIfNeeded() { - // Remove the entry from the keyboard - MagikIME.removeEntry(this) // Clear the entry if define in preferences - val sharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(this) - if (sharedPreferences.getBoolean(getString(R.string.keyboard_notification_entry_clear_close_key), - resources.getBoolean(R.bool.keyboard_notification_entry_clear_close_default))) { + if (PreferencesUtil.isClearKeyboardNotificationEnable(this)) { sendBroadcast(Intent(LOCK_ACTION)) } // Stop the service @@ -34,6 +30,11 @@ class KeyboardEntryNotificationService : LockNotificationService() { } 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 { intent == null -> Log.w(TAG, "null intent") ACTION_CLEAN_KEYBOARD_ENTRY == intent.action -> { @@ -76,19 +77,7 @@ class KeyboardEntryNotificationService : LockNotificationService() { stopTask(cleanNotificationTimerTask) // Timeout only if notification clear is available - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(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 (PreferencesUtil.isClearKeyboardNotificationEnable(this)) { if (notificationTimeoutMilliSecs != TimeoutHelper.NEVER) { cleanNotificationTimerTask = Thread { val maxPos = 100 @@ -111,7 +100,15 @@ class KeyboardEntryNotificationService : LockNotificationService() { } } + override fun onTaskRemoved(rootIntent: Intent?) { + MagikIME.removeEntry(this) + + super.onTaskRemoved(rootIntent) + } + override fun onDestroy() { + // Remove the entry from the keyboard + MagikIME.removeEntry(this) stopTask(cleanNotificationTimerTask) cleanNotificationTimerTask = null @@ -129,12 +126,26 @@ class KeyboardEntryNotificationService : LockNotificationService() { const val ACTION_CLEAN_KEYBOARD_ENTRY = "ACTION_CLEAN_KEYBOARD_ENTRY" 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 if (PreferencesUtil.isKeyboardNotificationEntryEnable(context)) { - context.startService(Intent(context, KeyboardEntryNotificationService::class.java).apply { - putExtra(ENTRY_INFO_KEY, entry) - }) + if (containsUsernameToCopy || containsPasswordToCopy || containsExtraFieldToCopy) { + startService = true + context.startService(intent.apply { + putExtra(ENTRY_INFO_KEY, entry) + }) + } } + + if (!startService) + context.stopService(intent) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt index 46222da3b..296da1489 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt @@ -209,6 +209,12 @@ object PreferencesUtil { 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) { val prefs = PreferenceManager.getDefaultSharedPreferences(context) prefs.edit()