Listener for task dialog

This commit is contained in:
J-Jamet
2020-04-26 09:10:18 +02:00
parent 8201b42135
commit 229db1242a
4 changed files with 25 additions and 6 deletions

View File

@@ -120,6 +120,12 @@ class ProgressDialogThread(private val activity: FragmentActivity) {
} }
} }
private var progressDisplayListener = object: ProgressTaskDialogFragment.DisplayListener {
override fun onDisplayDialog() {
mBinder?.allowFinishTask()
}
}
private fun startOrUpdateDialog(titleId: Int?, messageId: Int?, warningId: Int?) { private fun startOrUpdateDialog(titleId: Int?, messageId: Int?, warningId: Int?) {
if (progressTaskDialogFragment == null) { if (progressTaskDialogFragment == null) {
progressTaskDialogFragment = activity.supportFragmentManager progressTaskDialogFragment = activity.supportFragmentManager
@@ -127,6 +133,7 @@ class ProgressDialogThread(private val activity: FragmentActivity) {
} }
if (progressTaskDialogFragment == null) { if (progressTaskDialogFragment == null) {
progressTaskDialogFragment = ProgressTaskDialogFragment() progressTaskDialogFragment = ProgressTaskDialogFragment()
progressTaskDialogFragment?.displayListener = progressDisplayListener
progressTaskDialogFragment?.show(activity.supportFragmentManager, PROGRESS_TASK_DIALOG_TAG) progressTaskDialogFragment?.show(activity.supportFragmentManager, PROGRESS_TASK_DIALOG_TAG)
} }
progressTaskDialogFragment?.apply { progressTaskDialogFragment?.apply {
@@ -143,6 +150,7 @@ class ProgressDialogThread(private val activity: FragmentActivity) {
} }
private fun stopDialog() { private fun stopDialog() {
progressTaskDialogFragment?.displayListener = null
progressTaskDialogFragment?.dismissAllowingStateLoss() progressTaskDialogFragment?.dismissAllowingStateLoss()
progressTaskDialogFragment = null progressTaskDialogFragment = null
} }

View File

@@ -59,8 +59,12 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat
fun getService(): DatabaseTaskNotificationService = this@DatabaseTaskNotificationService fun getService(): DatabaseTaskNotificationService = this@DatabaseTaskNotificationService
fun addActionTaskListener(actionTaskListener: ActionTaskListener) { fun allowFinishTask() {
// To prevent task dialog to be unbound before the display
actionRunnableAsyncTask?.allowFinishTask?.set(true) actionRunnableAsyncTask?.allowFinishTask?.set(true)
}
fun addActionTaskListener(actionTaskListener: ActionTaskListener) {
mActionTaskListeners.add(actionTaskListener) mActionTaskListeners.add(actionTaskListener)
} }
@@ -178,8 +182,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat
stopSelf() stopSelf()
} }
) )
// To keep the task active until a binder is connected
actionRunnableAsyncTask?.allowFinishTask?.set(mActionTaskListeners.size >= 1)
actionRunnableAsyncTask?.execute({ actionRunnableNotNull }) actionRunnableAsyncTask?.execute({ actionRunnableNotNull })
} }
@@ -587,8 +589,6 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat
resultTask = result resultTask = result
} }
} }
// Min wait to show the dialog
Thread.sleep(200)
// Additional wait if the dialog take time to show (device with low memory) // Additional wait if the dialog take time to show (device with low memory)
while(!allowFinishTask.get()) { while(!allowFinishTask.get()) {
Thread.sleep(100) Thread.sleep(100)

View File

@@ -44,6 +44,8 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
private var warningView: TextView? = null private var warningView: TextView? = null
private var progressView: ProgressBar? = null private var progressView: ProgressBar? = null
var displayListener: DisplayListener? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
activity?.let { activity?.let {
@@ -73,6 +75,11 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
return super.onCreateDialog(savedInstanceState) return super.onCreateDialog(savedInstanceState)
} }
override fun onResume() {
super.onResume()
displayListener?.onDisplayDialog()
}
fun setTitle(@StringRes titleId: Int) { fun setTitle(@StringRes titleId: Int) {
this.title = titleId this.title = titleId
} }
@@ -103,6 +110,10 @@ open class ProgressTaskDialogFragment : DialogFragment(), ProgressTaskUpdater {
updateView(warningView, warning) updateView(warningView, warning)
} }
interface DisplayListener {
fun onDisplayDialog()
}
companion object { companion object {
const val PROGRESS_TASK_DIALOG_TAG = "progressDialogFragment" const val PROGRESS_TASK_DIALOG_TAG = "progressDialogFragment"

View File

@@ -119,7 +119,7 @@ fun Context.closeDatabase() {
// Stop the notification service // Stop the notification service
stopService(Intent(this, ClipboardEntryNotificationService::class.java)) stopService(Intent(this, ClipboardEntryNotificationService::class.java))
Log.i(Context::class.java.name, "Shutdown after inactivity or manual lock") Log.i(Context::class.java.name, "Close database after inactivity or manual lock")
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?)?.apply { (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?)?.apply {
cancelAll() cancelAll()
} }