2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.api
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.AsyncTask
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.SystemClock
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
import java.text.NumberFormat
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.App1
|
2018-01-13 07:38:32 +01:00
|
|
|
import jp.juggler.subwaytooter.dialog.ProgressDialogEx
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
|
|
|
|
|
|
|
/*
|
|
|
|
非同期タスク(TootTask)を実行します。
|
|
|
|
- 内部でAsyncTaskを使います。Android Lintの警告を抑制します
|
|
|
|
- ProgressDialogを表示します。抑制することも可能です。
|
|
|
|
- TootApiClientの初期化を行います
|
|
|
|
- TootApiClientからの進捗イベントをProgressDialogに伝達します。
|
|
|
|
*/
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
@Suppress("DEPRECATION","MemberVisibilityCanPrivate")
|
2018-01-18 21:30:22 +01:00
|
|
|
class TootTaskRunner(
|
2018-01-04 19:52:25 +01:00
|
|
|
context : Context,
|
2018-01-10 16:47:35 +01:00
|
|
|
private val progress_style : Int = PROGRESS_SPINNER
|
2018-01-04 19:52:25 +01:00
|
|
|
) : TootApiCallback {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
const val PROGRESS_NONE = - 1
|
2018-01-13 07:38:32 +01:00
|
|
|
const val PROGRESS_SPINNER = ProgressDialogEx.STYLE_SPINNER
|
|
|
|
const val PROGRESS_HORIZONTAL = ProgressDialogEx.STYLE_HORIZONTAL
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
private val percent_format : NumberFormat by lazy {
|
|
|
|
val v = NumberFormat.getPercentInstance()
|
|
|
|
v.maximumFractionDigits = 0
|
|
|
|
v
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ProgressInfo {
|
2018-01-10 16:47:35 +01:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
// HORIZONTALスタイルの場合、初期メッセージがないと後からメッセージを指定しても表示されない
|
|
|
|
internal var message = " "
|
|
|
|
internal var isIndeterminate = true
|
|
|
|
internal var value = 0
|
|
|
|
internal var max = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// has AsyncTask
|
2018-01-10 16:47:35 +01:00
|
|
|
private class MyTask(
|
|
|
|
private val runner : TootTaskRunner
|
|
|
|
) : AsyncTask<Void, Void, TootApiResult?>() {
|
2018-03-25 15:27:58 +02:00
|
|
|
var isActive :Boolean = true
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun doInBackground(vararg voids : Void) : TootApiResult? {
|
|
|
|
val callback = runner.callback
|
|
|
|
return if( callback == null) TootApiResult("callback is null") else callback.background(runner.client)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun onCancelled(result : TootApiResult?) {
|
2018-01-04 19:52:25 +01:00
|
|
|
onPostExecute(result)
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun onPostExecute(result : TootApiResult?) {
|
2018-03-25 15:27:58 +02:00
|
|
|
isActive = false
|
2018-01-04 19:52:25 +01:00
|
|
|
runner.dismissProgress()
|
|
|
|
runner.callback?.handleResult(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private val handler : Handler
|
|
|
|
private val client : TootApiClient
|
|
|
|
private val task : MyTask
|
|
|
|
private val info = ProgressInfo()
|
2018-01-13 07:38:32 +01:00
|
|
|
private var progress : ProgressDialogEx? = null
|
2018-01-04 19:52:25 +01:00
|
|
|
private var progress_prefix : String? = null
|
|
|
|
|
|
|
|
private val refContext : WeakReference<Context>
|
|
|
|
|
|
|
|
private var callback : TootTask? = null
|
|
|
|
private var last_message_shown : Long = 0
|
|
|
|
|
|
|
|
private val proc_progress_message = object : Runnable {
|
|
|
|
override fun run() {
|
|
|
|
synchronized(this) {
|
2018-01-10 16:47:35 +01:00
|
|
|
if( progress?.isShowing == true ){
|
2018-01-04 19:52:25 +01:00
|
|
|
showProgressMessage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
init {
|
|
|
|
this.refContext = WeakReference(context)
|
|
|
|
this.handler = Handler()
|
2018-01-12 10:01:25 +01:00
|
|
|
this.client = TootApiClient(context, callback=this)
|
2018-01-04 19:52:25 +01:00
|
|
|
this.task = MyTask(this)
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:27:58 +02:00
|
|
|
val isActive : Boolean
|
|
|
|
get()= task.isActive
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
fun run(callback : TootTask) {
|
|
|
|
openProgress()
|
|
|
|
|
|
|
|
this.callback = callback
|
|
|
|
|
|
|
|
task.executeOnExecutor(App1.task_executor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun run(access_info : SavedAccount, callback : TootTask) {
|
2018-01-12 10:01:25 +01:00
|
|
|
client.account =access_info
|
2018-01-04 19:52:25 +01:00
|
|
|
run(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun run(instance : String, callback : TootTask) {
|
2018-01-12 10:01:25 +01:00
|
|
|
client.instance = instance
|
2018-01-04 19:52:25 +01:00
|
|
|
run(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun progressPrefix(s : String) : TootTaskRunner {
|
|
|
|
this.progress_prefix = s
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// implements TootApiClient.Callback
|
|
|
|
|
|
|
|
override val isApiCancelled : Boolean
|
|
|
|
get()= task.isCancelled
|
|
|
|
|
|
|
|
override fun publishApiProgress(s : String) {
|
|
|
|
synchronized(this) {
|
|
|
|
info.message = s
|
|
|
|
info.isIndeterminate = true
|
|
|
|
}
|
|
|
|
delayProgressMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun publishApiProgressRatio(value : Int, max : Int) {
|
|
|
|
synchronized(this) {
|
|
|
|
info.isIndeterminate = false
|
|
|
|
info.value = value
|
|
|
|
info.max = max
|
|
|
|
}
|
|
|
|
delayProgressMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// ProgressDialog
|
|
|
|
|
|
|
|
private fun openProgress() {
|
|
|
|
// open progress
|
|
|
|
if(progress_style != PROGRESS_NONE) {
|
|
|
|
val context = refContext.get()
|
|
|
|
if(context != null && context is Activity) {
|
2018-01-13 07:38:32 +01:00
|
|
|
val progress = ProgressDialogEx(context)
|
2018-01-10 16:47:35 +01:00
|
|
|
this.progress = progress
|
|
|
|
progress.setCancelable(true)
|
|
|
|
progress.setOnCancelListener { task.cancel(true) }
|
|
|
|
progress.setProgressStyle(progress_style)
|
2018-01-04 19:52:25 +01:00
|
|
|
showProgressMessage()
|
2018-01-10 16:47:35 +01:00
|
|
|
progress.show()
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ダイアログを閉じる
|
|
|
|
private fun dismissProgress() {
|
2018-01-21 17:47:13 +01:00
|
|
|
progress?.dismiss()
|
|
|
|
progress = null
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ダイアログのメッセージを更新する
|
|
|
|
// 初期化時とメッセージ更新時に呼ばれる
|
|
|
|
private fun showProgressMessage() {
|
|
|
|
val progress = this.progress ?: return
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
val message = info.message.trim { it <= ' ' }
|
2018-01-04 19:52:25 +01:00
|
|
|
val progress_prefix = this.progress_prefix
|
2018-01-10 16:47:35 +01:00
|
|
|
progress .setMessage(
|
|
|
|
if( progress_prefix == null || progress_prefix.isEmpty() ) {
|
|
|
|
message
|
|
|
|
}else if( message.isEmpty()) {
|
|
|
|
progress_prefix
|
|
|
|
}else {
|
|
|
|
"$progress_prefix\n$message"
|
|
|
|
}
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
progress.isIndeterminate = info.isIndeterminate
|
2018-01-04 19:52:25 +01:00
|
|
|
if(info.isIndeterminate) {
|
2018-01-10 16:47:35 +01:00
|
|
|
progress.setProgressNumberFormat(null)
|
|
|
|
progress.setProgressPercentFormat(null)
|
2018-01-04 19:52:25 +01:00
|
|
|
} else {
|
2018-01-10 16:47:35 +01:00
|
|
|
progress.progress = info.value
|
|
|
|
progress.max = info.max
|
|
|
|
progress.setProgressNumberFormat("%1$,d / %2$,d")
|
|
|
|
progress.setProgressPercentFormat(percent_format)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
last_message_shown = SystemClock.elapsedRealtime()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 少し後にダイアログのメッセージを更新する
|
|
|
|
// あまり頻繁に更新せず、しかし繰り返し呼ばれ続けても時々は更新したい
|
|
|
|
// どのスレッドから呼ばれるか分からない
|
|
|
|
private fun delayProgressMessage() {
|
|
|
|
var wait = 100L + last_message_shown - SystemClock.elapsedRealtime()
|
|
|
|
wait = if(wait < 0L) 0L else if(wait > 100L) 100L else wait
|
|
|
|
|
|
|
|
synchronized(this) {
|
|
|
|
handler.removeCallbacks(proc_progress_message)
|
|
|
|
handler.postDelayed(proc_progress_message, wait)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|