2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils
|
|
|
|
import org.json.JSONArray
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
|
|
import java.util.LinkedList
|
|
|
|
|
|
|
|
class TaskList {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private val log = LogCategory("TaskList")
|
|
|
|
private const val FILE_TASK_LIST = "JOB_TASK_LIST"
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var _list : LinkedList<JSONObject>
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
@Synchronized
|
|
|
|
private fun prepareList(context : Context) : LinkedList<JSONObject> {
|
2018-01-04 19:52:25 +01:00
|
|
|
if(! ::_list.isInitialized) {
|
|
|
|
_list = LinkedList()
|
|
|
|
|
|
|
|
try {
|
|
|
|
context.openFileInput(FILE_TASK_LIST).use { inputStream ->
|
|
|
|
val bao = ByteArrayOutputStream()
|
|
|
|
IOUtils.copy(inputStream, bao)
|
2018-01-21 13:46:36 +01:00
|
|
|
val array = bao.toByteArray().decodeUTF8().toJsonArray()
|
2018-01-04 19:52:25 +01:00
|
|
|
var i = 0
|
|
|
|
val ie = array.length()
|
|
|
|
while(i < ie) {
|
|
|
|
val item = array.optJSONObject(i)
|
|
|
|
if(item != null) _list.add(item)
|
|
|
|
++ i
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "TaskList: prepareArray failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _list
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
@Synchronized
|
|
|
|
private fun saveArray(context : Context) {
|
2018-01-04 19:52:25 +01:00
|
|
|
val list = prepareList(context)
|
|
|
|
try {
|
|
|
|
log.d("saveArray size=%s", list.size)
|
|
|
|
val array = JSONArray()
|
|
|
|
for(item in list) {
|
|
|
|
array.put(item)
|
|
|
|
}
|
2018-01-21 13:46:36 +01:00
|
|
|
val data = array.toString().encodeUTF8()
|
|
|
|
context.openFileOutput(FILE_TASK_LIST, Context.MODE_PRIVATE)
|
|
|
|
.use { IOUtils.write(data, it) }
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "TaskList: saveArray failed.size=%s", list.size)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
fun addLast(context : Context, removeOld : Boolean, taskData : JSONObject) {
|
|
|
|
val list = prepareList(context)
|
|
|
|
if(removeOld) {
|
|
|
|
val it = list.iterator()
|
|
|
|
while(it.hasNext()) {
|
|
|
|
val item = it.next()
|
|
|
|
if(taskData == item) it.remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list.addLast(taskData)
|
|
|
|
saveArray(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("unused")
|
|
|
|
@Synchronized
|
|
|
|
fun hasNext(context : Context) : Boolean {
|
|
|
|
return prepareList(context).isNotEmpty()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
fun next(context : Context) : JSONObject? {
|
|
|
|
val list = prepareList(context)
|
|
|
|
val item = if(list.isEmpty()) null else list.removeFirst()
|
|
|
|
saveArray(context)
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|