2018-01-04 19:52:25 +01:00
|
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
|
import android.app.Activity
|
|
|
|
|
import android.app.Application
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper
|
2018-05-18 21:39:30 +02:00
|
|
|
|
import android.os.Build
|
2020-09-09 20:13:11 +02:00
|
|
|
|
import android.os.Handler
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import android.util.Log
|
2020-01-31 08:46:30 +01:00
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
|
import com.bumptech.glide.GlideBuilder
|
|
|
|
|
import com.bumptech.glide.Registry
|
|
|
|
|
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
|
|
|
|
|
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
|
|
|
|
|
import com.bumptech.glide.load.engine.executor.GlideExecutor
|
2018-12-01 00:02:18 +01:00
|
|
|
|
import com.bumptech.glide.load.model.GlideUrl
|
2021-02-20 12:22:22 +01:00
|
|
|
|
import jp.juggler.emoji.EmojiMap
|
2018-01-12 10:01:25 +01:00
|
|
|
|
import jp.juggler.subwaytooter.api.TootApiClient
|
2018-12-01 00:02:18 +01:00
|
|
|
|
import jp.juggler.subwaytooter.table.*
|
|
|
|
|
import jp.juggler.subwaytooter.util.CustomEmojiCache
|
|
|
|
|
import jp.juggler.subwaytooter.util.CustomEmojiLister
|
|
|
|
|
import jp.juggler.subwaytooter.util.ProgressResponseBody
|
|
|
|
|
import jp.juggler.util.*
|
|
|
|
|
import okhttp3.*
|
2018-12-06 22:38:49 +01:00
|
|
|
|
import org.conscrypt.Conscrypt
|
2020-12-08 21:24:42 +01:00
|
|
|
|
import ru.gildor.coroutines.okhttp.await
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import java.io.File
|
|
|
|
|
import java.io.InputStream
|
2019-08-13 16:01:48 +02:00
|
|
|
|
import java.net.CookieHandler
|
|
|
|
|
import java.net.CookieManager
|
|
|
|
|
import java.net.CookiePolicy
|
2018-12-06 22:38:49 +01:00
|
|
|
|
import java.security.Security
|
2018-12-01 00:02:18 +01:00
|
|
|
|
import java.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
|
import java.util.concurrent.TimeUnit
|
2019-01-15 15:47:08 +01:00
|
|
|
|
import kotlin.math.max
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
class App1 : Application() {
|
|
|
|
|
|
|
|
|
|
override fun onCreate() {
|
|
|
|
|
log.d("onCreate")
|
|
|
|
|
super.onCreate()
|
2020-02-14 15:16:07 +01:00
|
|
|
|
prepare(applicationContext, "App1.onCreate")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onTerminate() {
|
|
|
|
|
log.d("onTerminate")
|
|
|
|
|
super.onTerminate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DBOpenHelper(context : Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
|
2018-05-18 22:48:29 +02:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
override fun onCreate(db : SQLiteDatabase) {
|
2018-03-16 15:13:34 +01:00
|
|
|
|
for(ti in tableList) {
|
|
|
|
|
ti.onDBCreate(db)
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) {
|
2018-03-16 15:13:34 +01:00
|
|
|
|
for(ti in tableList) {
|
|
|
|
|
ti.onDBUpgrade(db, oldVersion, newVersion)
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
internal val log = LogCategory("App1")
|
|
|
|
|
|
|
|
|
|
const val FILE_PROVIDER_AUTHORITY = "jp.juggler.subwaytooter.FileProvider"
|
|
|
|
|
|
|
|
|
|
internal const val DB_NAME = "app_db"
|
|
|
|
|
|
|
|
|
|
// 2017/4/25 v10 1=>2 SavedAccount に通知設定を追加
|
|
|
|
|
// 2017/4/25 v10 1=>2 NotificationTracking テーブルを追加
|
|
|
|
|
// 2017/4/29 v20 2=>5 MediaShown,ContentWarningのインデクスが間違っていたので貼り直す
|
|
|
|
|
// 2017/4/29 v23 5=>6 MutedAppテーブルの追加、UserRelationテーブルの追加
|
|
|
|
|
// 2017/5/01 v26 6=>7 AcctSetテーブルの追加
|
|
|
|
|
// 2017/5/02 v32 7=>8 (この変更は取り消された)
|
|
|
|
|
// 2017/5/02 v32 8=>9 AcctColor テーブルの追加
|
|
|
|
|
// 2017/5/04 v33 9=>10 SavedAccountに項目追加
|
|
|
|
|
// 2017/5/08 v41 10=>11 MutedWord テーブルの追加
|
|
|
|
|
// 2017/5/17 v59 11=>12 PostDraft テーブルの追加
|
|
|
|
|
// 2017/5/23 v68 12=>13 SavedAccountに項目追加
|
|
|
|
|
// 2017/5/25 v69 13=>14 SavedAccountに項目追加
|
|
|
|
|
// 2017/5/27 v73 14=>15 TagSetテーブルの追加
|
|
|
|
|
// 2017/7/22 v99 15=>16 SavedAccountに項目追加
|
|
|
|
|
// 2017/7/22 v106 16=>17 AcctColor に項目追加
|
|
|
|
|
// 2017/9/23 v161 17=>18 SavedAccountに項目追加
|
|
|
|
|
// 2017/9/23 v161 18=>19 ClientInfoテーブルを置き換える
|
|
|
|
|
// 2017/12/01 v175 19=>20 UserRelation に項目追加
|
|
|
|
|
// 2018/1/03 v197 20=>21 HighlightWord テーブルを追加
|
2018-03-15 17:23:43 +01:00
|
|
|
|
// 2018/3/16 v226 21=>22 FavMuteテーブルを追加
|
2018-04-17 14:41:22 +02:00
|
|
|
|
// 2018/4/17 v236 22=>23 SavedAccountテーブルに項目追加
|
2018-04-20 12:43:09 +02:00
|
|
|
|
// 2018/4/20 v240 23=>24 SavedAccountテーブルに項目追加
|
2018-05-16 21:09:07 +02:00
|
|
|
|
// 2018/5/16 v252 24=>25 SubscriptionServerKey テーブルを追加
|
|
|
|
|
// 2018/5/16 v252 25=>26 SubscriptionServerKey テーブルを丸ごと変更
|
2018-08-04 20:56:51 +02:00
|
|
|
|
// 2018/8/5 v264 26 => 27 SavedAccountテーブルに項目追加
|
2018-08-16 21:58:30 +02:00
|
|
|
|
// 2018/8/17 v267 27 => 28 SavedAccountテーブルに項目追加
|
2018-08-20 02:07:55 +02:00
|
|
|
|
// 2018/8/19 v267 28 => 29 (失敗)ContentWarningMisskey, MediaShownMisskey テーブルを追加
|
2018-08-20 19:37:42 +02:00
|
|
|
|
// 2018/8/19 v268 29 => 30 ContentWarningMisskey, MediaShownMisskey, UserRelationMisskeyテーブルを追加
|
|
|
|
|
// 2018/8/19 v268 30 => 31 (29)で失敗しておかしくなったContentWarningとMediaShownを作り直す
|
2018-08-28 14:25:45 +02:00
|
|
|
|
// 2018/8/28 v279 31 => 32 UserRelation,UserRelationMisskey にendorsedを追加
|
2018-08-30 08:51:32 +02:00
|
|
|
|
// 2018/8/28 v280 32 => 33 NotificationTracking テーブルの作り直し。SavedAccountに通知二種類を追加
|
2018-10-30 20:29:00 +01:00
|
|
|
|
// 2018/10/31 v296 33 => 34 UserRelationMisskey に blocked_by を追加
|
|
|
|
|
// 2018/10/31 v296 34 => 35 UserRelationMisskey に requested_by を追加
|
2018-12-06 09:31:50 +01:00
|
|
|
|
// 2018/12/6 v317 35 => 36 ContentWarningテーブルの作り直し。
|
2019-06-04 01:24:54 +02:00
|
|
|
|
// 2019/6/4 v351 36 => 37 SavedAccount テーブルに項目追加。
|
|
|
|
|
// 2019/6/4 v351 37 => 38 SavedAccount テーブルに項目追加。
|
2019-08-12 05:32:39 +02:00
|
|
|
|
// 2019/8/12 v362 38 => 39 SavedAccount テーブルに項目追加。
|
2019-10-22 20:21:03 +02:00
|
|
|
|
// 2019/10/22 39 => 40 NotificationTracking テーブルに項目追加。
|
|
|
|
|
// 2019/10/22 40 => 41 NotificationCache テーブルに項目追加。
|
|
|
|
|
// 2019/10/23 41=> 42 SavedAccount テーブルに項目追加。
|
2019-11-15 07:13:59 +01:00
|
|
|
|
// 2019/11/15 42=> 43 HighlightWord テーブルに項目追加。
|
2019-12-16 16:56:27 +01:00
|
|
|
|
// 2019/12/17 43=> 44 SavedAccount テーブルに項目追加。
|
2019-12-17 18:58:49 +01:00
|
|
|
|
// 2019/12/18 44=> 45 SavedAccount テーブルに項目追加。
|
2019-12-18 08:52:55 +01:00
|
|
|
|
// 2019/12/18 44=> 46 SavedAccount テーブルに項目追加。
|
2020-06-08 01:14:53 +02:00
|
|
|
|
// 2020/6/8 46 => 54 別ブランチで色々してた。このブランチには影響ないが onDowngrade()を実装してないので上げてしまう
|
2020-07-19 14:28:12 +02:00
|
|
|
|
// 2020/7/19 54=>55 UserRelation テーブルに項目追加。
|
2020-09-07 14:20:11 +02:00
|
|
|
|
// 2020/9/7 55=>56 SavedAccountテーブルにCOL_DOMAINを追加。
|
2020-09-20 04:51:15 +02:00
|
|
|
|
// 2020/9/20 56=>57 SavedAccountテーブルに項目追加
|
|
|
|
|
// 2020/9/20 57=>58 UserRelationテーブルに項目追加
|
2021-02-09 23:25:06 +01:00
|
|
|
|
// 2021/2/10 58=>59 SavedAccountテーブルに項目追加
|
2021-05-11 08:12:43 +02:00
|
|
|
|
// 2021/5/11 59=>60 SavedAccountテーブルに項目追加
|
2019-11-15 07:13:59 +01:00
|
|
|
|
|
2021-05-11 08:12:43 +02:00
|
|
|
|
internal const val DB_VERSION = 60
|
2018-03-16 15:13:34 +01:00
|
|
|
|
|
|
|
|
|
private val tableList = arrayOf(
|
|
|
|
|
LogData,
|
|
|
|
|
SavedAccount,
|
|
|
|
|
ClientInfo,
|
|
|
|
|
MediaShown,
|
|
|
|
|
ContentWarning,
|
|
|
|
|
NotificationTracking,
|
2019-10-22 20:21:03 +02:00
|
|
|
|
NotificationCache,
|
2018-03-16 15:13:34 +01:00
|
|
|
|
MutedApp,
|
|
|
|
|
UserRelation,
|
|
|
|
|
AcctSet,
|
|
|
|
|
AcctColor,
|
|
|
|
|
MutedWord,
|
|
|
|
|
PostDraft,
|
|
|
|
|
TagSet,
|
|
|
|
|
HighlightWord,
|
2018-05-16 21:09:07 +02:00
|
|
|
|
FavMute,
|
|
|
|
|
SubscriptionServerKey
|
2018-03-16 15:13:34 +01:00
|
|
|
|
)
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
private lateinit var db_open_helper : DBOpenHelper
|
|
|
|
|
|
|
|
|
|
val database : SQLiteDatabase get() = db_open_helper.writableDatabase
|
|
|
|
|
|
2019-09-29 22:38:09 +02:00
|
|
|
|
// private val APPROVED_CIPHER_SUITES = arrayOf(
|
|
|
|
|
//
|
|
|
|
|
// // 以下は okhttp 3 のデフォルト
|
|
|
|
|
// // This is nearly equal to the cipher suites supported in Chrome 51, current as of 2016-05-25.
|
|
|
|
|
// // All of these suites are available on Android 7.0; earlier releases support a subset of these
|
|
|
|
|
// // suites. https://github.com/square/okhttp/issues/1972
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
|
//
|
|
|
|
|
// // Note that the following cipher suites are all on HTTP/2's bad cipher suites list. We'll
|
|
|
|
|
// // continue to include them until better suites are commonly available. For example, none
|
|
|
|
|
// // of the better cipher suites listed above shipped with Android 4.4 or Java 7.
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
|
// CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
|
// CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
|
// CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
|
|
|
//
|
|
|
|
|
// //https://www.ssllabs.com/ssltest/analyze.html?d=mastodon.cloud&latest
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, // mastodon.cloud用 デフォルトにはない
|
|
|
|
|
// CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, //mastodon.cloud用 デフォルトにはない
|
|
|
|
|
//
|
|
|
|
|
// // https://www.ssllabs.com/ssltest/analyze.html?d=m.sighash.info
|
|
|
|
|
// CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, // m.sighash.info 用 デフォルトにはない
|
|
|
|
|
// CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, // m.sighash.info 用 デフォルトにはない
|
|
|
|
|
// CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, // m.sighash.info 用 デフォルトにはない
|
|
|
|
|
// CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA
|
|
|
|
|
// ) // m.sighash.info 用 デフォルトにはない
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// private int getBitmapPoolSize( Context context ){
|
|
|
|
|
// ActivityManager am = ((ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE));
|
|
|
|
|
// int memory = am.getMemoryClass();
|
|
|
|
|
// int largeMemory = am.getLargeMemoryClass();
|
|
|
|
|
// // どちらも単位はMB
|
2018-01-28 20:03:04 +01:00
|
|
|
|
// warning.d("MemoryClass=%d, LargeMemoryClass = %d",memory,largeMemory);
|
2018-01-04 19:52:25 +01:00
|
|
|
|
//
|
|
|
|
|
// int maxSize;
|
|
|
|
|
// if( am.isLowRamDevice() ){
|
|
|
|
|
// maxSize = 5 * 1024; // 単位はKiB
|
|
|
|
|
// }else if( largeMemory >= 512 ){
|
|
|
|
|
// maxSize = 128 * 1024; // 単位はKiB
|
|
|
|
|
// }else if( largeMemory >= 256 ){
|
|
|
|
|
// maxSize = 64 * 1024; // 単位はKiB
|
|
|
|
|
// }else{
|
|
|
|
|
// maxSize = 10 * 1024; // 単位はKiB
|
|
|
|
|
// }
|
|
|
|
|
// return maxSize * 1024;
|
|
|
|
|
// }
|
|
|
|
|
|
2020-02-14 15:16:07 +01:00
|
|
|
|
val reNotAllowedInUserAgent = "[^\\x21-\\x7e]+".asciiPattern()
|
2018-05-18 22:48:29 +02:00
|
|
|
|
|
|
|
|
|
val userAgentDefault =
|
|
|
|
|
"SubwayTooter/${BuildConfig.VERSION_NAME} Android/${Build.VERSION.RELEASE}"
|
|
|
|
|
|
2018-05-19 10:58:58 +02:00
|
|
|
|
private fun getUserAgent() : String {
|
2018-05-18 22:48:29 +02:00
|
|
|
|
val userAgentCustom = Pref.spUserAgent(pref)
|
2018-05-19 10:58:58 +02:00
|
|
|
|
return when {
|
2020-04-07 08:28:33 +02:00
|
|
|
|
userAgentCustom.isNotEmpty() && ! reNotAllowedInUserAgent.matcher(userAgentCustom)
|
|
|
|
|
.find() -> userAgentCustom
|
2018-05-19 10:58:58 +02:00
|
|
|
|
else -> userAgentDefault
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:38:09 +02:00
|
|
|
|
private val user_agent_interceptor = object : Interceptor {
|
2019-08-24 05:35:22 +02:00
|
|
|
|
override fun intercept(chain : Interceptor.Chain) : Response {
|
|
|
|
|
val request_with_user_agent = chain.request().newBuilder()
|
|
|
|
|
.header("User-Agent", getUserAgent())
|
|
|
|
|
.build()
|
|
|
|
|
return chain.proceed(request_with_user_agent)
|
|
|
|
|
}
|
2018-05-18 22:48:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:38:09 +02:00
|
|
|
|
private var cookieManager : CookieManager? = null
|
|
|
|
|
private var cookieJar : CookieJar? = null
|
2019-08-13 16:01:48 +02:00
|
|
|
|
|
2018-08-31 14:47:40 +02:00
|
|
|
|
private fun prepareOkHttp(
|
2018-09-22 09:01:20 +02:00
|
|
|
|
timeoutSecondsConnect : Int,
|
|
|
|
|
timeoutSecondsRead : Int
|
2018-08-31 14:47:40 +02:00
|
|
|
|
) : OkHttpClient.Builder {
|
2019-09-29 22:38:09 +02:00
|
|
|
|
|
2019-08-13 16:01:48 +02:00
|
|
|
|
var cookieJar = this.cookieJar
|
2019-09-29 22:38:09 +02:00
|
|
|
|
if(cookieJar == null) {
|
|
|
|
|
val cookieManager = CookieManager().apply {
|
2019-08-13 16:01:48 +02:00
|
|
|
|
setCookiePolicy(CookiePolicy.ACCEPT_ALL)
|
|
|
|
|
}
|
|
|
|
|
CookieHandler.setDefault(cookieManager)
|
|
|
|
|
cookieJar = JavaNetCookieJar(cookieManager)
|
2019-09-29 22:38:09 +02:00
|
|
|
|
|
2019-08-13 16:01:48 +02:00
|
|
|
|
this.cookieManager = cookieManager
|
|
|
|
|
this.cookieJar = cookieJar
|
|
|
|
|
}
|
2019-01-15 15:47:08 +01:00
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val spec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
|
2018-12-06 22:38:49 +01:00
|
|
|
|
.allEnabledCipherSuites()
|
|
|
|
|
.allEnabledTlsVersions()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
return OkHttpClient.Builder()
|
2018-08-31 14:47:40 +02:00
|
|
|
|
.connectTimeout(timeoutSecondsConnect.toLong(), TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(timeoutSecondsRead.toLong(), TimeUnit.SECONDS)
|
|
|
|
|
.writeTimeout(timeoutSecondsRead.toLong(), TimeUnit.SECONDS)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
.pingInterval(10, TimeUnit.SECONDS)
|
2018-12-06 22:38:49 +01:00
|
|
|
|
.connectionSpecs(Collections.singletonList(spec))
|
2019-01-15 15:47:08 +01:00
|
|
|
|
.sslSocketFactory(MySslSocketFactory, MySslSocketFactory.trustManager)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
.addInterceptor(ProgressResponseBody.makeInterceptor())
|
2018-05-18 21:39:30 +02:00
|
|
|
|
.addInterceptor(user_agent_interceptor)
|
2019-09-29 22:38:09 +02:00
|
|
|
|
|
2019-08-13 16:01:48 +02:00
|
|
|
|
// クッキーの導入は検討中。とりあえずmstdn.jpではクッキー有効でも改善しなかったので現時点では追加しない
|
|
|
|
|
// .cookieJar(cookieJar)
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lateinit var ok_http_client : OkHttpClient
|
|
|
|
|
|
2018-12-06 09:31:50 +01:00
|
|
|
|
private lateinit var ok_http_client2 : OkHttpClient
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2018-09-01 02:49:57 +02:00
|
|
|
|
lateinit var ok_http_client_media_viewer : OkHttpClient
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
lateinit var pref : SharedPreferences
|
|
|
|
|
|
2020-04-07 07:12:02 +02:00
|
|
|
|
// lateinit var task_executor : ThreadPoolExecutor
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
|
lateinit var custom_emoji_cache : CustomEmojiCache
|
|
|
|
|
|
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
|
lateinit var custom_emoji_lister : CustomEmojiLister
|
|
|
|
|
|
2020-02-14 15:16:07 +01:00
|
|
|
|
fun prepare(app_context : Context, caller : String) : AppState {
|
2018-01-13 16:24:51 +01:00
|
|
|
|
var state = appStateX
|
2018-01-17 02:16:26 +01:00
|
|
|
|
if(state != null) return state
|
2021-02-20 12:22:22 +01:00
|
|
|
|
|
2020-02-12 16:41:25 +01:00
|
|
|
|
log.d("initialize AppState. caller=$caller")
|
2021-02-20 12:22:22 +01:00
|
|
|
|
|
|
|
|
|
// initialize EmojiMap
|
|
|
|
|
EmojiMap.load(app_context)
|
2020-02-12 16:41:25 +01:00
|
|
|
|
|
2018-12-06 22:38:49 +01:00
|
|
|
|
// initialize Conscrypt
|
|
|
|
|
Security.insertProviderAt(
|
|
|
|
|
Conscrypt.newProvider(),
|
|
|
|
|
1 /* 1 means first position */
|
|
|
|
|
)
|
|
|
|
|
|
2018-12-06 09:31:50 +01:00
|
|
|
|
initializeFont()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
|
pref = app_context.pref()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
run {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// We want at least 2 threads and at most 4 threads in the core pool,
|
|
|
|
|
// preferring to have 1 less than the CPU count to avoid saturating
|
|
|
|
|
// the CPU with background work
|
|
|
|
|
|
2020-05-16 16:59:18 +02:00
|
|
|
|
// val CPU_COUNT = Runtime.getRuntime().availableProcessors()
|
|
|
|
|
// val CORE_POOL_SIZE = max(2, min(CPU_COUNT - 1, 4))
|
|
|
|
|
// val MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1
|
|
|
|
|
// val KEEP_ALIVE_SECONDS = 30
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-05-16 16:59:18 +02:00
|
|
|
|
// // デフォルトだとキューはmax128で、溢れることがある
|
|
|
|
|
// val sPoolWorkQueue = LinkedBlockingQueue<Runnable>(999)
|
|
|
|
|
//
|
|
|
|
|
// val sThreadFactory = object : ThreadFactory {
|
|
|
|
|
// private val mCount = AtomicInteger(1)
|
|
|
|
|
//
|
|
|
|
|
// override fun newThread(r : Runnable) : Thread {
|
|
|
|
|
// return Thread(r, "SubwayTooterTask #" + mCount.getAndIncrement())
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2020-04-07 08:28:33 +02:00
|
|
|
|
|
|
|
|
|
// task_executor = ThreadPoolExecutor(
|
|
|
|
|
// CORE_POOL_SIZE // pool size
|
|
|
|
|
// , MAXIMUM_POOL_SIZE // max pool size
|
|
|
|
|
// , KEEP_ALIVE_SECONDS.toLong() // keep-alive-seconds
|
|
|
|
|
// , TimeUnit.SECONDS // unit of keep-alive-seconds
|
|
|
|
|
// , sPoolWorkQueue, sThreadFactory
|
|
|
|
|
// )
|
|
|
|
|
//
|
|
|
|
|
// task_executor.allowCoreThreadTimeOut(true)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
|
2020-02-12 16:41:25 +01:00
|
|
|
|
log.d("prepareDB 1")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
db_open_helper = DBOpenHelper(app_context)
|
|
|
|
|
|
|
|
|
|
// if( BuildConfig.DEBUG){
|
|
|
|
|
// SQLiteDatabase db = db_open_helper.getWritableDatabase();
|
|
|
|
|
// db_open_helper.onCreate( db );
|
|
|
|
|
// }
|
|
|
|
|
|
2020-02-12 16:41:25 +01:00
|
|
|
|
log.d("prepareDB 2")
|
2018-08-20 02:07:55 +02:00
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
|
AcctSet.deleteOld(now)
|
|
|
|
|
UserRelation.deleteOld(now)
|
|
|
|
|
ContentWarning.deleteOld(now)
|
|
|
|
|
MediaShown.deleteOld(now)
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
|
// if( USE_OLD_EMOJIONE ){
|
|
|
|
|
// if( typeface_emoji == null ){
|
|
|
|
|
// typeface_emoji = TypefaceUtils.load( app_context.getAssets(), "emojione_android.ttf" );
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if( image_loader == null ){
|
|
|
|
|
// image_loader = new MyImageLoader(
|
|
|
|
|
// Volley.newRequestQueue( getApplicationContext() )
|
|
|
|
|
// , new BitmapCache( getApplicationContext() )
|
|
|
|
|
// );
|
|
|
|
|
// }
|
2018-01-13 16:24:51 +01:00
|
|
|
|
|
2020-02-12 16:41:25 +01:00
|
|
|
|
log.d("create okhttp client")
|
2018-01-13 16:24:51 +01:00
|
|
|
|
run {
|
2018-09-01 02:49:57 +02:00
|
|
|
|
// API用のHTTP設定はキャッシュを使わない
|
2020-09-08 02:17:22 +02:00
|
|
|
|
ok_http_client = prepareOkHttp(60, 60)
|
2019-01-15 15:47:08 +01:00
|
|
|
|
.build()
|
2018-09-22 09:01:20 +02:00
|
|
|
|
|
2018-09-01 02:49:57 +02:00
|
|
|
|
// ディスクキャッシュ
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val cacheDir = File(app_context.cacheDir, "http2")
|
|
|
|
|
val cache = Cache(cacheDir, 30000000L)
|
2018-09-01 02:49:57 +02:00
|
|
|
|
|
|
|
|
|
// カスタム絵文字用のHTTP設定はキャッシュを使う
|
2020-09-08 02:17:22 +02:00
|
|
|
|
ok_http_client2 = prepareOkHttp(60, 60)
|
2019-01-15 15:47:08 +01:00
|
|
|
|
.cache(cache)
|
|
|
|
|
.build()
|
2018-09-22 09:01:20 +02:00
|
|
|
|
|
2019-01-15 15:47:08 +01:00
|
|
|
|
// 内蔵メディアビューア用のHTTP設定はタイムアウトを調整可能
|
|
|
|
|
val mediaReadTimeout = max(3, Pref.spMediaReadTimeout.toInt(pref))
|
|
|
|
|
ok_http_client_media_viewer = prepareOkHttp(mediaReadTimeout, mediaReadTimeout)
|
|
|
|
|
.cache(cache)
|
|
|
|
|
.build()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 20:13:11 +02:00
|
|
|
|
val handler = Handler(app_context.mainLooper)
|
2018-01-13 16:24:51 +01:00
|
|
|
|
|
2020-09-09 20:13:11 +02:00
|
|
|
|
log.d("create custom emoji cache.")
|
2020-09-27 12:59:54 +02:00
|
|
|
|
custom_emoji_cache = CustomEmojiCache(app_context, handler)
|
|
|
|
|
custom_emoji_lister = CustomEmojiLister(app_context, handler)
|
2018-01-10 16:47:35 +01:00
|
|
|
|
|
2019-08-23 03:52:31 +02:00
|
|
|
|
ColumnType.dump()
|
|
|
|
|
|
2020-02-12 16:41:25 +01:00
|
|
|
|
log.d("create AppState.")
|
2019-08-23 03:52:31 +02:00
|
|
|
|
|
2020-09-27 12:59:54 +02:00
|
|
|
|
state = AppState(app_context, handler, pref)
|
2018-01-13 16:24:51 +01:00
|
|
|
|
appStateX = state
|
2020-02-14 15:16:07 +01:00
|
|
|
|
|
2020-02-12 17:20:15 +01:00
|
|
|
|
// getAppState()を使える状態にしてからカラム一覧をロードする
|
|
|
|
|
log.d("load column list...")
|
|
|
|
|
state.loadColumnList()
|
2020-02-12 16:41:25 +01:00
|
|
|
|
|
|
|
|
|
log.d("prepare() complete! caller=$caller")
|
2020-02-14 15:16:07 +01:00
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
|
private var appStateX : AppState? = null
|
|
|
|
|
|
2020-02-14 15:16:07 +01:00
|
|
|
|
fun getAppState(context : Context, caller : String = "getAppState") : AppState {
|
|
|
|
|
return prepare(context.applicationContext, caller)
|
2018-01-13 16:24:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun sound(item : HighlightWord) {
|
|
|
|
|
try {
|
|
|
|
|
appStateX?.sound(item)
|
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.trace(ex)
|
|
|
|
|
// java.lang.NoSuchFieldError:
|
|
|
|
|
// at jp.juggler.subwaytooter.App1$Companion.sound (App1.kt:544)
|
|
|
|
|
// at jp.juggler.subwaytooter.Column$startRefresh$task$1.onPostExecute (Column.kt:2432)
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Suppress("UNUSED_PARAMETER")
|
2018-01-13 16:24:51 +01:00
|
|
|
|
fun registerGlideComponents(context : Context, glide : Glide, registry : Registry) {
|
2018-05-19 10:58:58 +02:00
|
|
|
|
// カスタムされたokhttpを優先的に使うためにprependを指定する
|
|
|
|
|
registry.prepend(
|
|
|
|
|
GlideUrl::class.java,
|
|
|
|
|
InputStream::class.java,
|
|
|
|
|
OkHttpUrlLoader.Factory(ok_http_client)
|
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
fun applyGlideOptions(context : Context, builder : GlideBuilder) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// ログレベル
|
2018-01-10 16:47:35 +01:00
|
|
|
|
builder.setLogLevel(Log.ERROR)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// エラー処理
|
2018-01-13 16:24:51 +01:00
|
|
|
|
val catcher = GlideExecutor.UncaughtThrowableStrategy { ex ->
|
2018-01-11 10:31:25 +01:00
|
|
|
|
log.trace(ex)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
2020-09-09 20:13:11 +02:00
|
|
|
|
builder.setDiskCacheExecutor(
|
|
|
|
|
GlideExecutor.newDiskCacheBuilder()
|
|
|
|
|
.setUncaughtThrowableStrategy(catcher).build()
|
|
|
|
|
)
|
|
|
|
|
builder.setSourceExecutor(
|
|
|
|
|
GlideExecutor.newSourceBuilder()
|
|
|
|
|
.setUncaughtThrowableStrategy(catcher).build()
|
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
builder.setDiskCache(InternalCacheDiskCacheFactory(context, 10 * 1024 * 1024))
|
|
|
|
|
|
|
|
|
|
// DEBUG 画像のディスクキャッシュの消去
|
|
|
|
|
// new Thread(new Runnable() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public void run() {
|
|
|
|
|
// Glide.get(context).clearDiskCache();
|
|
|
|
|
// }
|
|
|
|
|
// }).start();
|
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
//
|
|
|
|
|
// ////////////
|
|
|
|
|
// // サンプル1:キャッシュサイズを自動で計算する
|
|
|
|
|
// val calculator = MemorySizeCalculator.Builder(context)
|
|
|
|
|
// .setMemoryCacheScreens(2f)
|
|
|
|
|
// .setBitmapPoolScreens(3f)
|
|
|
|
|
// .build()
|
|
|
|
|
//
|
|
|
|
|
// builder.setMemoryCache(LruResourceCache(calculator.memoryCacheSize.toLong()))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
// サンプル2:キャッシュサイズをアプリが決める
|
|
|
|
|
// int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb
|
|
|
|
|
// builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
|
|
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
// サンプル3 : 自前のメモリキャッシュ
|
|
|
|
|
// builder.setMemoryCache(new YourAppMemoryCacheImpl());
|
|
|
|
|
|
2018-01-13 16:24:51 +01:00
|
|
|
|
// builder.setBitmapPool(LruBitmapPool(calculator.bitmapPoolSize.toLong()))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// ディスクキャッシュを保存する場所を変えたい場合
|
|
|
|
|
// builder.setDiskCache(new ExternalDiskCacheFactory(context));
|
|
|
|
|
|
|
|
|
|
// ディスクキャッシュのサイズを変えたい場合
|
2018-01-13 16:24:51 +01:00
|
|
|
|
// val diskCacheSizeBytes = 1024 * 1024 * 100 // 100 MB
|
|
|
|
|
// builder.setDiskCache(InternalDiskCacheFactory(context, diskCacheSizeBytes))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
// Although RequestOptions are typically specified per request,
|
|
|
|
|
// you can also apply a default set of RequestOptions that will be applied to every load
|
|
|
|
|
// you start in your application by using an AppGlideModule:
|
2018-01-13 16:24:51 +01:00
|
|
|
|
// val ro = RequestOptions()
|
|
|
|
|
// .format(DecodeFormat.PREFER_ARGB_8888)
|
|
|
|
|
// .disallowHardwareConfig()
|
|
|
|
|
// builder.setDefaultRequestOptions(ro)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 21:30:22 +01:00
|
|
|
|
fun setActivityTheme(
|
2020-09-29 19:44:56 +02:00
|
|
|
|
activity : AppCompatActivity,
|
2019-10-07 21:39:56 +02:00
|
|
|
|
noActionBar : Boolean = false,
|
2020-02-14 15:16:07 +01:00
|
|
|
|
forceDark : Boolean = false
|
2018-01-18 21:30:22 +01:00
|
|
|
|
) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-02-14 15:16:07 +01:00
|
|
|
|
prepare(activity.applicationContext, "setActivityTheme")
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2018-12-06 09:31:50 +01:00
|
|
|
|
val theme_idx = Pref.ipUiTheme(pref)
|
|
|
|
|
activity.setTheme(
|
2020-02-14 15:16:07 +01:00
|
|
|
|
if(forceDark || theme_idx == 1) {
|
2019-10-07 21:39:56 +02:00
|
|
|
|
if(noActionBar) R.style.AppTheme_Dark_NoActionBar else R.style.AppTheme_Dark
|
2020-02-14 15:16:07 +01:00
|
|
|
|
} else {
|
2019-10-07 21:39:56 +02:00
|
|
|
|
if(noActionBar) R.style.AppTheme_Light_NoActionBar else R.style.AppTheme_Light
|
2018-12-06 09:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
|
activity.setStatusBarColor(forceDark = forceDark)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 15:47:08 +01:00
|
|
|
|
internal val CACHE_CONTROL = CacheControl.Builder()
|
2019-09-25 10:42:25 +02:00
|
|
|
|
.maxAge(1, TimeUnit.DAYS) // キャッシュが新鮮であると考えられる時間
|
2018-01-04 19:52:25 +01:00
|
|
|
|
.build()
|
|
|
|
|
|
2020-12-08 21:24:42 +01:00
|
|
|
|
suspend fun getHttpCached(url : String) : ByteArray? {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val response : Response
|
|
|
|
|
|
|
|
|
|
try {
|
2018-12-08 16:29:07 +01:00
|
|
|
|
val request_builder = Request.Builder()
|
2019-01-15 15:47:08 +01:00
|
|
|
|
.cacheControl(CACHE_CONTROL)
|
|
|
|
|
.url(url)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2019-08-24 05:35:22 +02:00
|
|
|
|
val call = ok_http_client2.newCall(request_builder.build())
|
2020-12-08 21:24:42 +01:00
|
|
|
|
response = call.await()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "getHttp network error.")
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(! response.isSuccessful) {
|
2018-01-12 10:01:25 +01:00
|
|
|
|
log.e(TootApiClient.formatResponse(response, "getHttp response error."))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return try {
|
2019-08-24 05:35:22 +02:00
|
|
|
|
response.body?.bytes()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "getHttp content error.")
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 21:24:42 +01:00
|
|
|
|
suspend fun getHttpCachedString(
|
2018-12-06 09:31:50 +01:00
|
|
|
|
url : String,
|
2020-05-16 16:59:18 +02:00
|
|
|
|
accessInfo : SavedAccount? = null,
|
2018-12-06 09:31:50 +01:00
|
|
|
|
builderBlock : (Request.Builder) -> Unit = {}
|
|
|
|
|
) : String? {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
val response : Response
|
|
|
|
|
|
|
|
|
|
try {
|
2019-08-24 05:35:22 +02:00
|
|
|
|
val request_builder = Request.Builder()
|
2018-11-04 01:34:18 +01:00
|
|
|
|
.url(url)
|
2019-01-15 15:47:08 +01:00
|
|
|
|
.cacheControl(CACHE_CONTROL)
|
2018-12-06 09:31:50 +01:00
|
|
|
|
|
2020-05-16 16:59:18 +02:00
|
|
|
|
val access_token = accessInfo?.getAccessToken()
|
|
|
|
|
if(access_token?.isNotEmpty() == true) {
|
|
|
|
|
request_builder.header("Authorization", "Bearer $access_token")
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 01:34:18 +01:00
|
|
|
|
builderBlock(request_builder)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
2019-08-24 05:35:22 +02:00
|
|
|
|
val call = ok_http_client2.newCall(request_builder.build())
|
2020-12-08 21:24:42 +01:00
|
|
|
|
response = call.await()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "getHttp network error.")
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(! response.isSuccessful) {
|
2018-01-12 10:01:25 +01:00
|
|
|
|
log.e(TootApiClient.formatResponse(response, "getHttp response error."))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return try {
|
2019-08-24 05:35:22 +02:00
|
|
|
|
response.body?.string()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
|
log.e(ex, "getHttp content error.")
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 16:33:13 +02:00
|
|
|
|
// https://developer.android.com/preview/features/gesturalnav?hl=ja
|
2019-09-29 22:38:09 +02:00
|
|
|
|
fun initEdgeToEdge(@Suppress("UNUSED_PARAMETER") activity : Activity) {
|
|
|
|
|
// if(Build.VERSION.SDK_INT >= 29){
|
|
|
|
|
// val viewRoot = activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0)
|
|
|
|
|
// viewRoot.systemUiVisibility = (viewRoot.systemUiVisibility
|
|
|
|
|
// or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
|
|
// or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
|
|
|
|
|
// viewRoot.setOnApplyWindowInsetsListener { v, insets ->
|
|
|
|
|
// insets.consumeSystemWindowInsets()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|