@@ -10,6 +10,7 @@ import android.os.AsyncTask
10
10
import android.os.Bundle
11
11
import android.os.Handler
12
12
import android.os.SystemClock
13
+ import android.os.ConditionVariable
13
14
import android.preference.PreferenceManager
14
15
import android.support.v7.app.AppCompatActivity
15
16
import android.text.TextUtils
@@ -429,7 +430,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
429
430
crypto_password_file_edit.setText(name)
430
431
crypto_password_file_edit.isEnabled = false
431
432
432
- delayTask?.skip = true
433
+ delayTask?.cancelAndSignal( true )
433
434
434
435
val data = Intent (this , PgpActivity ::class .java)
435
436
data.putExtra(" OPERATION" , " EDIT" )
@@ -615,10 +616,10 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
615
616
}
616
617
617
618
private fun setTimer () {
619
+
620
+ // make sure to cancel any running tasks as soon as possible
618
621
// 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 )
622
623
623
624
// launch a new one
624
625
delayTask = DelayShow (this )
@@ -640,9 +641,21 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
640
641
@SuppressLint(" StaticFieldLeak" )
641
642
inner class DelayShow (val activity : PgpActivity ) : AsyncTask<Void, Int, Boolean>() {
642
643
private val pb: ProgressBar by lazy { pbLoading }
643
- internal var skip = false
644
+ private var skip = false
645
+ private var cancelNotify = ConditionVariable ()
646
+
644
647
private var showTime: Int = 0
645
648
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
+
646
659
val settings: SharedPreferences by lazy {
647
660
PreferenceManager .getDefaultSharedPreferences(activity)
648
661
}
@@ -673,7 +686,13 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
673
686
override fun doInBackground (vararg params : Void ): Boolean? {
674
687
var current = 0
675
688
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
+
677
696
current++
678
697
publishProgress(current)
679
698
}
0 commit comments