diff options
author | setine <uvm@talionis.net> | 2018-10-11 18:13:03 +0200 |
---|---|---|
committer | Mohamed Zenadi <zeapo@users.noreply.github.com> | 2018-10-11 18:13:03 +0200 |
commit | 2002e98c173860d77689badf26821af2fa859537 (patch) | |
tree | 2694095581a0599233b471a65a09879fbad8a8e4 | |
parent | 8ff0039be41e51e71e42553ff558640a7b74ec13 (diff) |
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.
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index b913fa91..4300aeba 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -10,6 +10,7 @@ import android.os.AsyncTask import android.os.Bundle import android.os.Handler import android.os.SystemClock +import android.os.ConditionVariable import android.preference.PreferenceManager import android.support.v7.app.AppCompatActivity import android.text.TextUtils @@ -429,7 +430,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { crypto_password_file_edit.setText(name) crypto_password_file_edit.isEnabled = false - delayTask?.skip = true + delayTask?.cancelAndSignal(true) val data = Intent(this, PgpActivity::class.java) data.putExtra("OPERATION", "EDIT") @@ -615,10 +616,10 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { } private fun setTimer() { + + // make sure to cancel any running tasks as soon as possible // if the previous task is still running, do not ask it to clear the password - if (delayTask?.status == AsyncTask.Status.RUNNING) { - delayTask?.skip = true - } + delayTask?.cancelAndSignal(true) // launch a new one delayTask = DelayShow(this) @@ -640,9 +641,21 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { @SuppressLint("StaticFieldLeak") inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() { private val pb: ProgressBar by lazy { pbLoading } - internal var skip = false + private var skip = false + private var cancelNotify = ConditionVariable() + private var showTime: Int = 0 + // Custom cancellation that can be triggered from another thread. + // + // This signals the DelayShow task to stop and avoids it having + // to poll the AsyncTask.isCancelled() excessively. If skipClearing + // is true, the cancelled task won't clear the clipboard. + fun cancelAndSignal(skipClearing : Boolean) { + skip = skipClearing + cancelNotify.open() + } + val settings: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(activity) } @@ -673,7 +686,13 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { override fun doInBackground(vararg params: Void): Boolean? { var current = 0 while (current < showTime) { - SystemClock.sleep(1000) + + // Block for 1s or until cancel is signalled + if(cancelNotify.block(1000)) { + Log.d("DELAY_SHOW", "Cancelled") + return true + } + current++ publishProgress(current) } |