内蔵メディアビューアのキャッシュ動作を改善
This commit is contained in:
parent
d9c6793c15
commit
da319e66dc
|
@ -12,8 +12,8 @@ android {
|
|||
minSdkVersion 21
|
||||
targetSdkVersion 27
|
||||
|
||||
versionCode 227
|
||||
versionName "2.2.7"
|
||||
versionCode 228
|
||||
versionName "2.2.8"
|
||||
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
package jp.juggler.subwaytooter.api
|
||||
|
||||
import android.content.Context
|
||||
import android.support.test.InstrumentationRegistry
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
import jp.juggler.subwaytooter.table.SavedAccount
|
||||
|
@ -31,11 +30,14 @@ class TestTootApiClient {
|
|||
|
||||
override var currentCallCallback : CurrentCallCallback? = null
|
||||
|
||||
override fun getResponse(context: Context, request : Request) : Response {
|
||||
override fun getResponse(request : Request,cached:Boolean) : Response {
|
||||
return responseGenerator(request)
|
||||
}
|
||||
|
||||
override fun getWebSocket(context: Context,request : Request, webSocketListener : WebSocketListener) : WebSocket {
|
||||
override fun getWebSocket(
|
||||
request : Request,
|
||||
webSocketListener : WebSocketListener
|
||||
) : WebSocket {
|
||||
return webSocketGenerator(request, webSocketListener)
|
||||
}
|
||||
}
|
||||
|
@ -68,10 +70,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"id":999,"redirect_uri":"urn:ietf:wg:oauth:2.0:oob","client_id":"DUMMY_ID","client_secret":"DUMMY_SECRET"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"id":999,"redirect_uri":"urn:ietf:wg:oauth:2.0:oob","client_id":"DUMMY_ID","client_secret":"DUMMY_SECRET"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
// client credentialの検証
|
||||
|
@ -80,10 +84,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"id":999,"redirect_uri":"urn:ietf:wg:oauth:2.0:oob","client_id":"DUMMY_ID","client_secret":"DUMMY_SECRET"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"id":999,"redirect_uri":"urn:ietf:wg:oauth:2.0:oob","client_id":"DUMMY_ID","client_secret":"DUMMY_SECRET"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
"/oauth/token" -> when {
|
||||
|
@ -94,10 +100,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"access_token":"DUMMY_CLIENT_CREDENTIAL"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"access_token":"DUMMY_CLIENT_CREDENTIAL"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
}
|
||||
|
@ -108,10 +116,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"access_token":"DUMMY_ACCESS_TOKEN"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"access_token":"DUMMY_ACCESS_TOKEN"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
@ -135,10 +145,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
account1Json.toString()
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
account1Json.toString()
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
// インスタンス情報
|
||||
|
@ -157,10 +169,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
json.toString()
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
json.toString()
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
// 公開タイムライン
|
||||
|
@ -194,10 +208,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
array.toString()
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
array.toString()
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
@ -207,17 +223,19 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
mediaTypeTextPlain,
|
||||
request.url().toString()
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
mediaTypeTextPlain,
|
||||
request.url().toString()
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
webSocketGenerator = { request : Request, _ : WebSocketListener ->
|
||||
object:WebSocket{
|
||||
object : WebSocket {
|
||||
override fun queueSize() : Long {
|
||||
return 4096L
|
||||
}
|
||||
|
@ -393,10 +411,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("This is test")
|
||||
.body(ResponseBody.create(
|
||||
mediaTypeHtml,
|
||||
"""<html><body>Error!</body></html>"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
mediaTypeHtml,
|
||||
"""<html><body>Error!</body></html>"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
message = TootApiClient.simplifyErrorHtml(response, response.body()?.string() ?: "")
|
||||
|
@ -412,10 +432,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("This is test")
|
||||
.body(ResponseBody.create(
|
||||
MediaType.parse("text/plain"),
|
||||
"""Error!"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
MediaType.parse("text/plain"),
|
||||
"""Error!"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
message = TootApiClient.simplifyErrorHtml(response, response.body()?.string() ?: "")
|
||||
|
@ -431,10 +453,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("This is test")
|
||||
.body(ResponseBody.create(
|
||||
MediaType.parse("text/plain"),
|
||||
""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
MediaType.parse("text/plain"),
|
||||
""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
message = TootApiClient.simplifyErrorHtml(response, response.body()?.string() ?: "")
|
||||
|
@ -475,10 +499,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
message = TootApiClient.formatResponse(response, "caption", null)
|
||||
|
@ -495,10 +521,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("status-message")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
bodyString = response.body()?.string()
|
||||
|
@ -516,10 +544,12 @@ class TestTootApiClient {
|
|||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(500)
|
||||
.message("")
|
||||
.body(ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
))
|
||||
.body(
|
||||
ResponseBody.create(
|
||||
TootApiClient.MEDIA_TYPE_JSON,
|
||||
"""{"error":"Error!"}"""
|
||||
)
|
||||
)
|
||||
.build()
|
||||
|
||||
bodyString = response.body()?.string()
|
||||
|
@ -607,7 +637,10 @@ class TestTootApiClient {
|
|||
val bOk = client.sendRequest(result) { requestSimple }
|
||||
assertEquals(false, bOk)
|
||||
assertEquals("取得中: GET /", callback.progressString)
|
||||
assertEquals("instance: 通信エラー :NotImplementedError An operation is not implemented.", result.error)
|
||||
assertEquals(
|
||||
"instance: 通信エラー :NotImplementedError An operation is not implemented.",
|
||||
result.error
|
||||
)
|
||||
assertNull(result.response)
|
||||
|
||||
}
|
||||
|
@ -1161,8 +1194,8 @@ class TestTootApiClient {
|
|||
)
|
||||
client.account = accessInfo
|
||||
val result = client.request("/api/v1/timelines/public")
|
||||
println( result?.bodyString)
|
||||
|
||||
println(result?.bodyString)
|
||||
|
||||
val content = result?.jsonArray
|
||||
assertNotNull(content)
|
||||
println(content?.optJSONObject(0).toString())
|
||||
|
@ -1186,8 +1219,9 @@ class TestTootApiClient {
|
|||
callback = callback
|
||||
)
|
||||
client.account = accessInfo
|
||||
val result = client.webSocket("/api/v1/streaming/?stream=public:local",object:WebSocketListener(){
|
||||
})
|
||||
val result = client.webSocket("/api/v1/streaming/?stream=public:local",
|
||||
object : WebSocketListener() {
|
||||
})
|
||||
val ws = result?.data as? WebSocket
|
||||
assertNotNull(ws)
|
||||
ws?.cancel()
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.support.test.runner.AndroidJUnit4
|
|||
import android.test.mock.MockContext
|
||||
import jp.juggler.subwaytooter.api.TootParser
|
||||
import jp.juggler.subwaytooter.table.SavedAccount
|
||||
import jp.juggler.subwaytooter.util.notEmptyOrThrow
|
||||
import jp.juggler.subwaytooter.util.parseLong
|
||||
import jp.juggler.subwaytooter.util.toJsonObject
|
||||
import org.json.JSONArray
|
||||
|
|
|
@ -362,7 +362,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
|
|||
internal fun getHttpCached(client : TootApiClient, url : String) : TootApiResult? {
|
||||
val result = TootApiResult.makeWithCaption(url)
|
||||
|
||||
if(! client.sendRequest(result) {
|
||||
if(! client.sendRequest(result,cached = true) {
|
||||
okhttp3.Request.Builder()
|
||||
.url(url)
|
||||
.cacheControl(App1.CACHE_5MIN)
|
||||
|
@ -374,6 +374,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
|
|||
if(! response.isSuccessful) {
|
||||
return result.setError(TootApiClient.formatResponse(response, result.caption))
|
||||
}
|
||||
// log.d("cached=${ response.cacheResponse() != null }")
|
||||
|
||||
try {
|
||||
result.data = ProgressResponseBody.bytes(response) { bytesRead, bytesTotal ->
|
||||
|
|
|
@ -202,7 +202,7 @@ class App1 : Application() {
|
|||
|
||||
lateinit var ok_http_client : OkHttpClient
|
||||
|
||||
private lateinit var ok_http_client2 : OkHttpClient
|
||||
lateinit var ok_http_client2 : OkHttpClient
|
||||
|
||||
lateinit var pref : SharedPreferences
|
||||
|
||||
|
|
|
@ -19,7 +19,11 @@ import java.util.regex.Pattern
|
|||
|
||||
class TootApiClient(
|
||||
internal val context : Context,
|
||||
internal val httpClient : SimpleHttpClient = SimpleHttpClientImpl(context, App1.ok_http_client),
|
||||
internal val httpClient : SimpleHttpClient = SimpleHttpClientImpl(
|
||||
context,
|
||||
App1.ok_http_client,
|
||||
App1.ok_http_client2
|
||||
),
|
||||
internal val callback : TootApiCallback
|
||||
) {
|
||||
|
||||
|
@ -194,6 +198,7 @@ class TootApiClient(
|
|||
internal inline fun sendRequest(
|
||||
result : TootApiResult,
|
||||
progressPath : String? = null,
|
||||
cached : Boolean = false,
|
||||
block : () -> Request
|
||||
) : Boolean {
|
||||
return try {
|
||||
|
@ -211,7 +216,7 @@ class TootApiClient(
|
|||
)
|
||||
)
|
||||
|
||||
result.response = httpClient.getResponse(request)
|
||||
result.response = httpClient.getResponse(request,cached=cached)
|
||||
|
||||
null == result.error
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ object LogData :TableCompanion{
|
|||
)
|
||||
}
|
||||
|
||||
override fun onDBUpgrade(db : SQLiteDatabase, v_old : Int, v_new : Int) {
|
||||
override fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ interface CurrentCallCallback {
|
|||
|
||||
interface SimpleHttpClient {
|
||||
var currentCallCallback : CurrentCallCallback?
|
||||
fun getResponse(request : Request) : Response
|
||||
fun getResponse(request : Request,cached :Boolean = false) : Response
|
||||
fun getWebSocket(
|
||||
request : Request,
|
||||
webSocketListener : WebSocketListener
|
||||
|
@ -23,9 +23,11 @@ interface SimpleHttpClient {
|
|||
|
||||
class SimpleHttpClientImpl(
|
||||
context : Context,
|
||||
private val okHttpClient : OkHttpClient
|
||||
private val okHttpClient : OkHttpClient,
|
||||
private val okHttpClientCached : OkHttpClient
|
||||
) : SimpleHttpClient {
|
||||
|
||||
|
||||
companion object {
|
||||
val log = LogCategory("SimpleHttpClientImpl")
|
||||
var connectivityManager : ConnectivityManager? = null
|
||||
|
@ -40,9 +42,12 @@ class SimpleHttpClientImpl(
|
|||
|
||||
override var currentCallCallback : CurrentCallCallback? = null
|
||||
|
||||
override fun getResponse(request : Request) : Response {
|
||||
override fun getResponse(request : Request,cached :Boolean) : Response {
|
||||
checkNetworkState()
|
||||
val call = okHttpClient.newCall(request)
|
||||
val call = when(cached) {
|
||||
false -> okHttpClient
|
||||
true -> okHttpClientCached
|
||||
}.newCall(request)
|
||||
currentCallCallback?.onCallCreated(call)
|
||||
return call.execute()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue