2018-01-04 19:52:25 +01:00
|
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
import android.content.ContentValues
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import android.content.Context
|
2020-02-05 09:18:28 +01:00
|
|
|
|
import android.database.sqlite.SQLiteDatabase
|
|
|
|
|
import android.database.sqlite.SQLiteDatabaseCorruptException
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper
|
2019-08-12 03:28:13 +02:00
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
|
import android.graphics.Canvas
|
|
|
|
|
import android.graphics.RectF
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import android.os.Handler
|
|
|
|
|
import android.os.SystemClock
|
2020-02-05 09:18:28 +01:00
|
|
|
|
import android.provider.BaseColumns
|
2019-03-20 05:35:07 +01:00
|
|
|
|
import com.caverock.androidsvg.SVG
|
2019-08-12 03:28:13 +02:00
|
|
|
|
import jp.juggler.apng.ApngFrames
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import jp.juggler.subwaytooter.App1
|
2020-02-05 09:18:28 +01:00
|
|
|
|
import jp.juggler.subwaytooter.table.TableCompanion
|
2018-12-01 00:02:18 +01:00
|
|
|
|
import jp.juggler.util.LogCategory
|
2019-08-12 03:28:13 +02:00
|
|
|
|
import java.io.ByteArrayInputStream
|
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
|
import java.util.*
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
2020-02-05 09:18:28 +01:00
|
|
|
|
import java.util.concurrent.TimeUnit
|
2019-03-20 05:35:07 +01:00
|
|
|
|
import kotlin.math.ceil
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-02-12 16:04:36 +01:00
|
|
|
|
class CustomEmojiCache(val context : Context) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
private val log = LogCategory("CustomEmojiCache")
|
|
|
|
|
|
2018-01-20 07:51:14 +01:00
|
|
|
|
internal const val DEBUG = false
|
2018-01-04 19:52:25 +01:00
|
|
|
|
internal const val CACHE_MAX = 512 // 使用中のビットマップは掃除しないので、頻度によってはこれより多くなることもある
|
|
|
|
|
internal const val ERROR_EXPIRE = 60000L * 10
|
|
|
|
|
|
|
|
|
|
private val elapsedTime : Long
|
|
|
|
|
get() = SystemClock.elapsedRealtime()
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
|
|
|
|
// カスタム絵文字のキャッシュ専用のデータベースファイルを作る
|
|
|
|
|
// (DB破損などの際に削除してしまえるようにする)
|
|
|
|
|
private const val CACHE_DB_NAME = "emoji_cache_db"
|
|
|
|
|
private const val CACHE_DB_VERSION = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DbCache(
|
|
|
|
|
val id : Long,
|
|
|
|
|
val timeUsed : Long,
|
|
|
|
|
val data : ByteArray
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
companion object : TableCompanion {
|
|
|
|
|
|
|
|
|
|
const val table = "custom_emoji_cache"
|
|
|
|
|
|
|
|
|
|
const val COL_ID = BaseColumns._ID
|
|
|
|
|
const val COL_TIME_SAVE = "time_save"
|
|
|
|
|
const val COL_TIME_USED = "time_used"
|
|
|
|
|
const val COL_URL = "url"
|
|
|
|
|
const val COL_DATA = "data"
|
|
|
|
|
|
|
|
|
|
override fun onDBCreate(db : SQLiteDatabase) {
|
|
|
|
|
db.execSQL(
|
|
|
|
|
"""create table if not exists $table
|
|
|
|
|
($COL_ID INTEGER PRIMARY KEY
|
|
|
|
|
,$COL_TIME_SAVE integer not null
|
|
|
|
|
,$COL_TIME_USED integer not null
|
|
|
|
|
,$COL_URL text not null
|
|
|
|
|
,$COL_DATA blob not null
|
|
|
|
|
)""".trimIndent()
|
|
|
|
|
)
|
|
|
|
|
db.execSQL("create unique index if not exists ${table}_url on ${table}($COL_URL)")
|
|
|
|
|
db.execSQL("create index if not exists ${table}_old on ${table}($COL_TIME_USED)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDBUpgrade(
|
|
|
|
|
db : SQLiteDatabase,
|
|
|
|
|
oldVersion : Int,
|
|
|
|
|
newVersion : Int
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun load(db : SQLiteDatabase, url : String, now : Long) =
|
|
|
|
|
db.rawQuery(
|
|
|
|
|
"select $COL_ID,$COL_TIME_USED,$COL_DATA from $table where $COL_URL=?",
|
|
|
|
|
arrayOf(url)
|
|
|
|
|
)?.use { cursor ->
|
|
|
|
|
if(cursor.count == 0)
|
|
|
|
|
null
|
|
|
|
|
else {
|
|
|
|
|
cursor.moveToNext()
|
|
|
|
|
DbCache(
|
|
|
|
|
id = cursor.getLong(cursor.getColumnIndex(COL_ID)),
|
|
|
|
|
timeUsed = cursor.getLong(cursor.getColumnIndex(COL_TIME_USED)),
|
|
|
|
|
data = cursor.getBlob(cursor.getColumnIndex(COL_DATA))
|
|
|
|
|
).apply {
|
|
|
|
|
if(now - timeUsed >= 5 * 3600000L) {
|
|
|
|
|
db.update(
|
|
|
|
|
table,
|
|
|
|
|
ContentValues().apply {
|
|
|
|
|
put(COL_TIME_USED, now)
|
|
|
|
|
},
|
|
|
|
|
"id=?",
|
|
|
|
|
arrayOf(id.toString())
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun sweep(db : SQLiteDatabase, now : Long) {
|
|
|
|
|
val expire = now - TimeUnit.DAYS.toMillis(30)
|
|
|
|
|
db.delete(
|
|
|
|
|
table,
|
|
|
|
|
"$COL_TIME_USED < ?",
|
|
|
|
|
arrayOf(expire.toString())
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun update(db : SQLiteDatabase, url : String, data : ByteArray) {
|
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
|
db.replace(table,
|
|
|
|
|
null,
|
|
|
|
|
ContentValues().apply {
|
|
|
|
|
put(COL_URL, url)
|
|
|
|
|
put(COL_DATA, data)
|
|
|
|
|
put(COL_TIME_USED, now)
|
|
|
|
|
put(COL_TIME_SAVE, now)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class DbOpenHelper(val context : Context) :
|
|
|
|
|
SQLiteOpenHelper(context, CACHE_DB_NAME, null, CACHE_DB_VERSION) {
|
|
|
|
|
|
|
|
|
|
private val tables = arrayOf(DbCache)
|
|
|
|
|
override fun onCreate(db : SQLiteDatabase) =
|
|
|
|
|
tables.forEach { it.onDBCreate(db) }
|
|
|
|
|
|
|
|
|
|
override fun onUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) =
|
|
|
|
|
tables.forEach { it.onDBUpgrade(db, oldVersion, newVersion) }
|
|
|
|
|
|
|
|
|
|
fun deleteDatabase() {
|
|
|
|
|
try {
|
|
|
|
|
close()
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
SQLiteDatabase.deleteDatabase(context.getDatabasePath(databaseName))
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 12:00:44 +01:00
|
|
|
|
private class CacheItem(val url : String, var frames : ApngFrames?) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
var time_used : Long = elapsedTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Request(
|
|
|
|
|
val refTarget : WeakReference<Any>,
|
|
|
|
|
val url : String,
|
2019-03-20 05:35:07 +01:00
|
|
|
|
val onLoadComplete : () -> Unit
|
2018-01-04 19:52:25 +01:00
|
|
|
|
)
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
// APNGデコード済のキャッシュデータ
|
|
|
|
|
private val cache = ConcurrentHashMap<String, CacheItem>()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// エラーキャッシュ
|
|
|
|
|
private val cache_error = ConcurrentHashMap<String, Long>()
|
|
|
|
|
private val cache_error_item = CacheItem("error", null)
|
|
|
|
|
|
|
|
|
|
// リクエストキュー
|
|
|
|
|
// キャンセル操作の都合上、アクセス時に排他が必要
|
|
|
|
|
private val queue = LinkedList<Request>()
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
private val handler = Handler(context.mainLooper)
|
|
|
|
|
|
|
|
|
|
private val dbOpenHelper = DbOpenHelper(context)
|
2020-02-12 16:04:36 +01:00
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
private var lastSweepDbCache = 0L
|
2020-02-12 16:04:36 +01:00
|
|
|
|
|
|
|
|
|
private val workerLock = Any()
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
2020-02-12 16:04:36 +01:00
|
|
|
|
// 他の変数より後に初期化すること
|
|
|
|
|
private val workers =
|
|
|
|
|
(1 .. 4).map { Worker(workerLock).apply { start() } }.toList()
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
// DB処理を行い、SQLiteDatabaseCorruptExceptionを検出したらDBを削除してリトライする
|
|
|
|
|
private fun <T : Any> useDbCache(block : (SQLiteDatabase) -> T?) : T? {
|
|
|
|
|
for(nTry in (1 .. 3)) {
|
|
|
|
|
try {
|
|
|
|
|
val db = dbOpenHelper.writableDatabase ?: break
|
|
|
|
|
return block(db)
|
|
|
|
|
} catch(ex : SQLiteDatabaseCorruptException) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
dbOpenHelper.deleteDatabase()
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
break
|
|
|
|
|
}
|
2019-09-25 10:42:25 +02:00
|
|
|
|
}
|
2020-02-05 09:18:28 +01:00
|
|
|
|
return null
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
2019-10-22 23:39:20 +02:00
|
|
|
|
// ネットワーク接続が切り替わったタイミングでエラーキャッシュをクリアする
|
|
|
|
|
fun onNetworkChanged() {
|
|
|
|
|
cache_error.clear()
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// カラムのリロードボタンを押したタイミングでエラーキャッシュをクリアする
|
|
|
|
|
fun clearErrorCache() {
|
|
|
|
|
cache_error.clear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// tag_target を持つリクエストまたはtagがGCされたリクエストをキューから除去する
|
|
|
|
|
fun cancelRequest(refTarget : WeakReference<Any>) {
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val targetTag = refTarget.get() ?: return
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
synchronized(queue) {
|
|
|
|
|
val it = queue.iterator()
|
|
|
|
|
while(it.hasNext()) {
|
|
|
|
|
val request = it.next()
|
|
|
|
|
val tag = request.refTarget.get()
|
|
|
|
|
if(tag === null || tag === targetTag) it.remove()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getCached(now : Long, url : String) : CacheItem? {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
// 成功キャッシュ
|
|
|
|
|
val item = cache[url]
|
|
|
|
|
if(item != null) {
|
|
|
|
|
item.time_used = now
|
|
|
|
|
return item
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// エラーキャッシュ
|
|
|
|
|
val time_error = cache_error[url]
|
|
|
|
|
if(time_error != null && now < time_error + ERROR_EXPIRE) {
|
|
|
|
|
return cache_error_item
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 05:35:07 +01:00
|
|
|
|
fun getFrames(
|
|
|
|
|
refDrawTarget : WeakReference<Any>?,
|
|
|
|
|
url : String,
|
|
|
|
|
onLoadComplete : () -> Unit
|
|
|
|
|
) : ApngFrames? {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
try {
|
2019-03-20 05:35:07 +01:00
|
|
|
|
if(refDrawTarget?.get() == null) {
|
2019-09-14 22:09:52 +02:00
|
|
|
|
log.e("draw: DrawTarget is null ")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancelRequest(refDrawTarget)
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
// APNG frame cache
|
2018-01-04 19:52:25 +01:00
|
|
|
|
synchronized(cache) {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
getCached(elapsedTime, url)?.let { return it.frames }
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
// キャンセル操作の都合上、排他が必要
|
|
|
|
|
synchronized(queue) {
|
2018-01-10 16:47:35 +01:00
|
|
|
|
queue.addLast(Request(refDrawTarget, url, onLoadComplete))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
workers.first().notifyEx()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
// たまにcache変数がなぜかnullになる端末があるらしい
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
fun delete() {
|
|
|
|
|
synchronized(cache) {
|
|
|
|
|
for(entry in cache.entries) {
|
|
|
|
|
entry.value.frames?.dispose()
|
|
|
|
|
}
|
|
|
|
|
cache.clear()
|
|
|
|
|
cache_error.clear()
|
|
|
|
|
}
|
|
|
|
|
dbOpenHelper.deleteDatabase()
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
private inner class Worker(waiter : Any?) : WorkerBase(waiter) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
override fun cancel() {
|
|
|
|
|
// このスレッドはプロセスが生きてる限りキャンセルされない
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun run() {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
var ts : Long
|
|
|
|
|
var te : Long
|
2018-01-04 19:52:25 +01:00
|
|
|
|
while(true) {
|
|
|
|
|
try {
|
2018-12-03 09:07:07 +01:00
|
|
|
|
var queue_size : Int
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val request = synchronized(queue) {
|
|
|
|
|
val x = if(queue.isNotEmpty()) queue.removeFirst() else null
|
|
|
|
|
queue_size = queue.size
|
2019-09-25 10:42:25 +02:00
|
|
|
|
x
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(request == null) {
|
|
|
|
|
if(DEBUG) log.d("wait")
|
2020-02-05 09:18:28 +01:00
|
|
|
|
|
|
|
|
|
synchronized(cache) {
|
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
|
if(now - lastSweepDbCache >= TimeUnit.DAYS.toMillis(1)) {
|
|
|
|
|
lastSweepDbCache = now
|
|
|
|
|
useDbCache { DbCache.sweep(it, now) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
ts = elapsedTime
|
2018-01-04 19:52:25 +01:00
|
|
|
|
waitEx(86400000L)
|
2019-09-25 10:42:25 +02:00
|
|
|
|
te = elapsedTime
|
|
|
|
|
if(te - ts >= 200L) log.d("sleep ${te - ts}ms")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 描画先がGCされたなら何もしない
|
|
|
|
|
request.refTarget.get() ?: continue
|
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
ts = elapsedTime
|
2018-01-04 19:52:25 +01:00
|
|
|
|
var cache_size : Int = - 1
|
2020-02-05 09:18:28 +01:00
|
|
|
|
val cache_used = synchronized(cache) {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
val now = elapsedTime
|
|
|
|
|
val item = getCached(now, request.url)
|
|
|
|
|
if(item != null) {
|
|
|
|
|
if(item.frames != null) {
|
|
|
|
|
fireCallback(request)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2019-09-25 10:42:25 +02:00
|
|
|
|
return@synchronized true
|
|
|
|
|
}
|
|
|
|
|
sweep_cache(now)
|
|
|
|
|
cache_size = cache.size
|
|
|
|
|
return@synchronized false
|
|
|
|
|
}
|
|
|
|
|
te = elapsedTime
|
|
|
|
|
if(te - ts >= 200L) log.d("cache_used? ${te - ts}ms")
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
if(cache_used) continue
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
if(DEBUG)
|
2019-03-20 05:35:07 +01:00
|
|
|
|
log.d(
|
|
|
|
|
"start get image. queue_size=%d, cache_size=%d url=%s",
|
|
|
|
|
queue_size,
|
|
|
|
|
cache_size,
|
|
|
|
|
request.url
|
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
|
|
|
|
|
|
// データベースからロードしてみる
|
|
|
|
|
ts = elapsedTime
|
|
|
|
|
val dbCache = useDbCache { DbCache.load(it, request.url, now) }
|
|
|
|
|
te = elapsedTime
|
|
|
|
|
if(te - ts >= 200L) log.d("DbCache.load ${te - ts}ms")
|
|
|
|
|
|
|
|
|
|
var data = dbCache?.data
|
|
|
|
|
|
|
|
|
|
// データベースにblobがなければHTTPリクエスト
|
|
|
|
|
if(data == null) {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
ts = elapsedTime
|
2020-02-05 09:18:28 +01:00
|
|
|
|
data = try {
|
|
|
|
|
App1.getHttpCached(request.url)
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e("get failed. url=%s", request.url)
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
null
|
|
|
|
|
}
|
2019-09-25 10:42:25 +02:00
|
|
|
|
te = elapsedTime
|
|
|
|
|
if(te - ts >= 200L) log.d("image get? ${te - ts}ms")
|
|
|
|
|
|
2020-02-05 09:18:28 +01:00
|
|
|
|
if(data != null) {
|
|
|
|
|
useDbCache { db ->
|
|
|
|
|
DbCache.update(db, request.url, data)
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2020-02-05 09:18:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts = elapsedTime
|
|
|
|
|
val frames = try {
|
|
|
|
|
data?.let { decodeAPNG(it, request.url) }
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
log.e(ex, "decode failed.")
|
|
|
|
|
null
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2020-02-05 09:18:28 +01:00
|
|
|
|
te = elapsedTime
|
|
|
|
|
if(te - ts >= 200L) log.d("image decode? ${te - ts}ms")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
ts = elapsedTime
|
2018-01-04 19:52:25 +01:00
|
|
|
|
synchronized(cache) {
|
|
|
|
|
if(frames == null) {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
cache_error[request.url] = elapsedTime
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} else {
|
|
|
|
|
var item : CacheItem? = cache[request.url]
|
|
|
|
|
if(item == null) {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
// 新しいキャッシュ項目
|
2018-01-04 19:52:25 +01:00
|
|
|
|
item = CacheItem(request.url, frames)
|
2018-01-21 13:46:36 +01:00
|
|
|
|
cache[request.url] = item
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} else {
|
2020-02-05 09:18:28 +01:00
|
|
|
|
// 古いキャッシュを更新する
|
2018-01-04 19:52:25 +01:00
|
|
|
|
item.frames?.dispose()
|
|
|
|
|
item.frames = frames
|
|
|
|
|
}
|
|
|
|
|
fireCallback(request)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-25 10:42:25 +02:00
|
|
|
|
te = elapsedTime
|
2020-02-05 09:18:28 +01:00
|
|
|
|
if(te - ts >= 200L) log.d("update_cache ${te - ts}ms")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
|
|
|
|
|
// Fujitsu F-01H(F01H), 2048MB RAM, Android 6.0
|
|
|
|
|
// java.lang.NullPointerException:
|
|
|
|
|
// at java.util.concurrent.ConcurrentHashMap.get (ConcurrentHashMap.java:772)
|
|
|
|
|
// at jp.juggler.subwaytooter.util.CustomEmojiCache$Worker.run (CustomEmojiCache.java:183)
|
|
|
|
|
|
|
|
|
|
waitEx(3000L)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun fireCallback(request : Request) {
|
2018-01-10 16:47:35 +01:00
|
|
|
|
handler.post { request.onLoadComplete() }
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun sweep_cache(now : Long) {
|
|
|
|
|
|
|
|
|
|
// キャッシュ限界を超過した数
|
|
|
|
|
val over = cache.size - CACHE_MAX
|
|
|
|
|
|
|
|
|
|
// 超過した数がある程度大きくなるまで掃除しない
|
|
|
|
|
if(over <= 64) return
|
|
|
|
|
|
|
|
|
|
// 掃除する候補
|
|
|
|
|
val list = ArrayList<CacheItem>()
|
|
|
|
|
for(item in cache.values) {
|
|
|
|
|
// 最近使われていないものが掃除対象
|
|
|
|
|
if(now - item.time_used > 1000L) list.add(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 昇順ソート
|
|
|
|
|
list.sortBy { it.time_used }
|
|
|
|
|
|
|
|
|
|
// 古い物から順に捨てる
|
|
|
|
|
var removed = 0
|
|
|
|
|
for(item in list) {
|
|
|
|
|
cache.remove(item.url)
|
|
|
|
|
item.frames?.dispose()
|
2019-03-20 05:35:07 +01:00
|
|
|
|
if(++ removed >= over) break
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 12:00:44 +01:00
|
|
|
|
private fun decodeAPNG(data : ByteArray, url : String) : ApngFrames? {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
try {
|
2019-08-11 18:52:08 +02:00
|
|
|
|
// APNGをデコード
|
2019-09-25 10:42:25 +02:00
|
|
|
|
val x = ApngFrames.parse(64) { ByteArrayInputStream(data) }
|
2019-08-11 18:52:08 +02:00
|
|
|
|
if(x != null) return x
|
2018-01-11 10:31:25 +01:00
|
|
|
|
// fall thru
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
2018-01-11 10:31:25 +01:00
|
|
|
|
if(DEBUG) log.trace(ex)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
log.e(ex, "PNG decode failed. %s ", url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通常のビットマップでのロードを試みる
|
|
|
|
|
try {
|
|
|
|
|
val b = decodeBitmap(data, 128)
|
|
|
|
|
if(b != null) {
|
|
|
|
|
if(DEBUG) log.d("bitmap decoded.")
|
2018-01-27 12:00:44 +01:00
|
|
|
|
return ApngFrames(b)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} else {
|
|
|
|
|
log.e("Bitmap decode returns null. %s", url)
|
|
|
|
|
}
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
|
|
|
|
// fall thru
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "Bitmap decode failed. %s", url)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 05:35:07 +01:00
|
|
|
|
// SVGのロードを試みる
|
|
|
|
|
try {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
val b = decodeSVG(url, data, 128.toFloat())
|
2019-03-20 05:35:07 +01:00
|
|
|
|
if(b != null) {
|
|
|
|
|
if(DEBUG) log.d("SVG decoded.")
|
|
|
|
|
return ApngFrames(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fall thru
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "SVG decode failed. %s", url)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val options = BitmapFactory.Options()
|
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
private fun decodeBitmap(
|
|
|
|
|
data : ByteArray,
|
|
|
|
|
@Suppress("SameParameterValue") pixel_max : Int
|
|
|
|
|
) : Bitmap? {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
options.inJustDecodeBounds = true
|
|
|
|
|
options.inScaled = false
|
|
|
|
|
options.outWidth = 0
|
|
|
|
|
options.outHeight = 0
|
|
|
|
|
BitmapFactory.decodeByteArray(data, 0, data.size, options)
|
|
|
|
|
var w = options.outWidth
|
|
|
|
|
var h = options.outHeight
|
|
|
|
|
if(w <= 0 || h <= 0) {
|
|
|
|
|
log.e("can't decode bounds.")
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
var bits = 0
|
|
|
|
|
while(w > pixel_max || h > pixel_max) {
|
|
|
|
|
++ bits
|
|
|
|
|
w = w shr 1
|
|
|
|
|
h = h shr 1
|
|
|
|
|
}
|
|
|
|
|
options.inJustDecodeBounds = false
|
|
|
|
|
options.inSampleSize = 1 shl bits
|
|
|
|
|
return BitmapFactory.decodeByteArray(data, 0, data.size, options)
|
|
|
|
|
}
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
private fun decodeSVG(
|
|
|
|
|
url : String,
|
|
|
|
|
data : ByteArray,
|
|
|
|
|
@Suppress("SameParameterValue") pixelMax : Float
|
|
|
|
|
) : Bitmap? {
|
2019-03-20 05:35:07 +01:00
|
|
|
|
try {
|
|
|
|
|
val svg = SVG.getFromInputStream(ByteArrayInputStream(data))
|
2019-03-26 02:50:37 +01:00
|
|
|
|
|
2019-09-25 10:42:25 +02:00
|
|
|
|
val src_w =
|
|
|
|
|
svg.documentWidth // the width in pixels, or -1 if there is no width available.
|
|
|
|
|
val src_h =
|
|
|
|
|
svg.documentHeight // the height in pixels, or -1 if there is no height available.
|
|
|
|
|
val aspect = if(src_w <= 0f || src_h <= 0f) {
|
2019-04-07 15:47:05 +02:00
|
|
|
|
// widthやheightの情報がない
|
|
|
|
|
1f
|
2019-09-25 10:42:25 +02:00
|
|
|
|
} else {
|
2019-04-07 15:47:05 +02:00
|
|
|
|
src_w / src_h
|
|
|
|
|
}
|
2019-03-20 05:35:07 +01:00
|
|
|
|
|
|
|
|
|
val dst_w : Float
|
|
|
|
|
val dst_h : Float
|
|
|
|
|
if(aspect >= 1f) {
|
|
|
|
|
dst_w = pixelMax
|
|
|
|
|
dst_h = pixelMax / aspect
|
2019-09-25 10:42:25 +02:00
|
|
|
|
} else {
|
2019-03-20 05:35:07 +01:00
|
|
|
|
dst_h = pixelMax
|
|
|
|
|
dst_w = pixelMax * aspect
|
|
|
|
|
}
|
|
|
|
|
val w_ceil = ceil(dst_w)
|
|
|
|
|
val h_ceil = ceil(dst_h)
|
|
|
|
|
|
|
|
|
|
// Create a Bitmap to render our SVG to
|
|
|
|
|
val b = Bitmap.createBitmap(w_ceil.toInt(), h_ceil.toInt(), Bitmap.Config.ARGB_8888)
|
|
|
|
|
// Create a Canvas to use for rendering
|
|
|
|
|
val canvas = Canvas(b)
|
|
|
|
|
|
|
|
|
|
svg.renderToCanvas(
|
|
|
|
|
canvas,
|
|
|
|
|
if(aspect >= 1f) {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
RectF(0f, h_ceil - dst_h, dst_w, dst_h) // 後半はw,hを指定する
|
2019-03-20 05:35:07 +01:00
|
|
|
|
} else {
|
2019-09-25 10:42:25 +02:00
|
|
|
|
RectF(w_ceil - dst_w, 0f, dst_w, dst_h) // 後半はw,hを指定する
|
2019-03-20 05:35:07 +01:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return b
|
|
|
|
|
} catch(ex : Throwable) {
|
2019-04-12 22:19:15 +02:00
|
|
|
|
log.e(ex, "decodeSVG failed. $url")
|
2019-03-20 05:35:07 +01:00
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|