Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 2002e98

Browse files
setinezeapo
authored andcommitted
Cancel running DelayShow async tasks (#416) (#431)
These tasks were filling up the threadpool slots and leading to delays in executing further AsyncTasks after a while.
1 parent 8ff0039 commit 2002e98

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.os.AsyncTask
1010
import android.os.Bundle
1111
import android.os.Handler
1212
import android.os.SystemClock
13+
import android.os.ConditionVariable
1314
import android.preference.PreferenceManager
1415
import android.support.v7.app.AppCompatActivity
1516
import android.text.TextUtils
@@ -429,7 +430,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
429430
crypto_password_file_edit.setText(name)
430431
crypto_password_file_edit.isEnabled = false
431432

432-
delayTask?.skip = true
433+
delayTask?.cancelAndSignal(true)
433434

434435
val data = Intent(this, PgpActivity::class.java)
435436
data.putExtra("OPERATION", "EDIT")
@@ -615,10 +616,10 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
615616
}
616617

617618
private fun setTimer() {
619+
620+
// make sure to cancel any running tasks as soon as possible
618621
// if the previous task is still running, do not ask it to clear the password
619-
if (delayTask?.status == AsyncTask.Status.RUNNING) {
620-
delayTask?.skip = true
621-
}
622+
delayTask?.cancelAndSignal(true)
622623

623624
// launch a new one
624625
delayTask = DelayShow(this)
@@ -640,9 +641,21 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
640641
@SuppressLint("StaticFieldLeak")
641642
inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() {
642643
private val pb: ProgressBar by lazy { pbLoading }
643-
internal var skip = false
644+
private var skip = false
645+
private var cancelNotify = ConditionVariable()
646+
644647
private var showTime: Int = 0
645648

649+
// Custom cancellation that can be triggered from another thread.
650+
//
651+
// This signals the DelayShow task to stop and avoids it having
652+
// to poll the AsyncTask.isCancelled() excessively. If skipClearing
653+
// is true, the cancelled task won't clear the clipboard.
654+
fun cancelAndSignal(skipClearing : Boolean) {
655+
skip = skipClearing
656+
cancelNotify.open()
657+
}
658+
646659
val settings: SharedPreferences by lazy {
647660
PreferenceManager.getDefaultSharedPreferences(activity)
648661
}
@@ -673,7 +686,13 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
673686
override fun doInBackground(vararg params: Void): Boolean? {
674687
var current = 0
675688
while (current < showTime) {
676-
SystemClock.sleep(1000)
689+
690+
// Block for 1s or until cancel is signalled
691+
if(cancelNotify.block(1000)) {
692+
Log.d("DELAY_SHOW", "Cancelled")
693+
return true
694+
}
695+
677696
current++
678697
publishProgress(current)
679698
}

0 commit comments

Comments
 (0)