Change copying notification priority #281 and fix never timeout

This commit is contained in:
J-Jamet
2019-08-03 14:17:58 +02:00
parent 77a848cf0e
commit 949e6f247d

View File

@@ -32,6 +32,7 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.stylish.Stylish import com.kunzisoft.keepass.activities.stylish.Stylish
import com.kunzisoft.keepass.database.exception.SamsungClipboardException import com.kunzisoft.keepass.database.exception.SamsungClipboardException
import com.kunzisoft.keepass.timeout.ClipboardHelper import com.kunzisoft.keepass.timeout.ClipboardHelper
import com.kunzisoft.keepass.timeout.TimeoutHelper.NEVER
import java.util.* import java.util.*
class NotificationCopyingService : Service() { class NotificationCopyingService : Service() {
@@ -58,7 +59,7 @@ class NotificationCopyingService : Service() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID_COPYING, val channel = NotificationChannel(CHANNEL_ID_COPYING,
CHANNEL_NAME_COPYING, CHANNEL_NAME_COPYING,
NotificationManager.IMPORTANCE_LOW) NotificationManager.IMPORTANCE_HIGH)
notificationManager?.createNotificationChannel(channel) notificationManager?.createNotificationChannel(channel)
} }
@@ -73,25 +74,23 @@ class NotificationCopyingService : Service() {
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) val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val sClipClear = prefs.getString(getString(R.string.clipboard_timeout_key), val timeoutClipboardClear = prefs.getString(getString(R.string.clipboard_timeout_key),
getString(R.string.clipboard_timeout_default)) getString(R.string.clipboard_timeout_default))
notificationTimeoutMilliSecs = java.lang.Long.parseLong(sClipClear) notificationTimeoutMilliSecs = java.lang.Long.parseLong(timeoutClipboardClear)
when { when {
intent == null -> Log.w(TAG, "null intent") intent == null -> Log.w(TAG, "null intent")
ACTION_NEW_NOTIFICATION == intent.action -> { ACTION_NEW_NOTIFICATION == intent.action -> {
val title = intent.getStringExtra(EXTRA_ENTRY_TITLE) val title = intent.getStringExtra(EXTRA_ENTRY_TITLE)
newNotification(title, constructListOfField(intent)) newNotification(title, constructListOfField(intent))
} }
ACTION_CLEAN_CLIPBOARD == intent.action -> { ACTION_CLEAN_CLIPBOARD == intent.action -> {
stopTask(countingDownTask) stopTask(countingDownTask)
try { try {
clipboardHelper!!.cleanClipboard() clipboardHelper?.cleanClipboard()
} catch (e: SamsungClipboardException) { } catch (e: SamsungClipboardException) {
Log.e(TAG, "Clipboard can't be cleaned", e) Log.e(TAG, "Clipboard can't be cleaned", e)
} }
} }
else -> for (actionKey in NotificationField.allActionKeys) { else -> for (actionKey in NotificationField.allActionKeys) {
if (actionKey == intent.action) { if (actionKey == intent.action) {
@@ -157,15 +156,18 @@ class NotificationCopyingService : Service() {
val myNotificationId = notificationId val myNotificationId = notificationId
stopTask(cleanNotificationTimer) stopTask(cleanNotificationTimer)
cleanNotificationTimer = Thread { // If timer
try { if (notificationTimeoutMilliSecs != NEVER) {
Thread.sleep(notificationTimeoutMilliSecs) cleanNotificationTimer = Thread {
} catch (e: InterruptedException) { try {
cleanNotificationTimer = null Thread.sleep(notificationTimeoutMilliSecs)
} catch (e: InterruptedException) {
cleanNotificationTimer = null
}
notificationManager?.cancel(myNotificationId)
} }
notificationManager?.cancel(myNotificationId) cleanNotificationTimer?.start()
} }
cleanNotificationTimer?.start()
} }
private fun copyField(fieldToCopy: NotificationField, nextFields: ArrayList<NotificationField>) { private fun copyField(fieldToCopy: NotificationField, nextFields: ArrayList<NotificationField>) {
@@ -198,30 +200,35 @@ class NotificationCopyingService : Service() {
val myNotificationId = notificationId val myNotificationId = notificationId
countingDownTask = Thread { if (notificationTimeoutMilliSecs != NEVER) {
val maxPos = 100 countingDownTask = Thread {
val posDurationMills = notificationTimeoutMilliSecs / maxPos val maxPos = 100
for (pos in maxPos downTo 1) { val posDurationMills = notificationTimeoutMilliSecs / maxPos
builder.setProgress(maxPos, pos, false) for (pos in maxPos downTo 1) {
notificationManager?.notify(myNotificationId, builder.build()) builder.setProgress(maxPos, pos, false)
try { notificationManager?.notify(myNotificationId, builder.build())
Thread.sleep(posDurationMills) try {
} catch (e: InterruptedException) { Thread.sleep(posDurationMills)
break } catch (e: InterruptedException) {
} break
}
}
countingDownTask = null
notificationManager?.cancel(myNotificationId)
// Clean password only if no next field
if (nextFields.size <= 0)
try {
clipboardHelper?.cleanClipboard()
} catch (e: SamsungClipboardException) {
Log.e(TAG, "Clipboard can't be cleaned", e)
} }
countingDownTask = null
notificationManager?.cancel(myNotificationId)
// Clean password only if no next field
if (nextFields.size <= 0)
try {
clipboardHelper?.cleanClipboard()
} catch (e: SamsungClipboardException) {
Log.e(TAG, "Clipboard can't be cleaned", e)
}
}
countingDownTask?.start()
} else {
// No timer
notificationManager?.notify(myNotificationId, builder.build())
} }
countingDownTask?.start()
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Clipboard can't be populate", e) Log.e(TAG, "Clipboard can't be populate", e)