These tasks were filling up the threadpool slots and leading to delays in executing further AsyncTasks after a while.
This commit is contained in:
parent
8ff0039be4
commit
2002e98c17
1 changed files with 25 additions and 6 deletions
|
@ -10,6 +10,7 @@ import android.os.AsyncTask
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
|
import android.os.ConditionVariable
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
import android.support.v7.app.AppCompatActivity
|
import android.support.v7.app.AppCompatActivity
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
|
@ -429,7 +430,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
|
||||||
crypto_password_file_edit.setText(name)
|
crypto_password_file_edit.setText(name)
|
||||||
crypto_password_file_edit.isEnabled = false
|
crypto_password_file_edit.isEnabled = false
|
||||||
|
|
||||||
delayTask?.skip = true
|
delayTask?.cancelAndSignal(true)
|
||||||
|
|
||||||
val data = Intent(this, PgpActivity::class.java)
|
val data = Intent(this, PgpActivity::class.java)
|
||||||
data.putExtra("OPERATION", "EDIT")
|
data.putExtra("OPERATION", "EDIT")
|
||||||
|
@ -615,10 +616,10 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setTimer() {
|
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 the previous task is still running, do not ask it to clear the password
|
||||||
if (delayTask?.status == AsyncTask.Status.RUNNING) {
|
delayTask?.cancelAndSignal(true)
|
||||||
delayTask?.skip = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// launch a new one
|
// launch a new one
|
||||||
delayTask = DelayShow(this)
|
delayTask = DelayShow(this)
|
||||||
|
@ -640,9 +641,21 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
|
||||||
@SuppressLint("StaticFieldLeak")
|
@SuppressLint("StaticFieldLeak")
|
||||||
inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() {
|
inner class DelayShow(val activity: PgpActivity) : AsyncTask<Void, Int, Boolean>() {
|
||||||
private val pb: ProgressBar by lazy { pbLoading }
|
private val pb: ProgressBar by lazy { pbLoading }
|
||||||
internal var skip = false
|
private var skip = false
|
||||||
|
private var cancelNotify = ConditionVariable()
|
||||||
|
|
||||||
private var showTime: Int = 0
|
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 {
|
val settings: SharedPreferences by lazy {
|
||||||
PreferenceManager.getDefaultSharedPreferences(activity)
|
PreferenceManager.getDefaultSharedPreferences(activity)
|
||||||
}
|
}
|
||||||
|
@ -673,7 +686,13 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
|
||||||
override fun doInBackground(vararg params: Void): Boolean? {
|
override fun doInBackground(vararg params: Void): Boolean? {
|
||||||
var current = 0
|
var current = 0
|
||||||
while (current < showTime) {
|
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++
|
current++
|
||||||
publishProgress(current)
|
publishProgress(current)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue