BayernMessenger/vector/src/main/java/im/vector/riotredesign/VectorApplication.kt

189 lines
7.2 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2019-01-18 10:12:08 +00:00
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2019-01-18 10:12:08 +00:00
*/
2018-10-03 15:56:33 +00:00
package im.vector.riotredesign
2019-06-19 08:46:59 +00:00
import android.app.AlarmManager
2018-10-03 15:56:33 +00:00
import android.app.Application
2019-06-19 08:46:59 +00:00
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
2019-06-19 08:46:59 +00:00
import android.content.Intent
import android.content.ServiceConnection
import android.content.res.Configuration
import android.os.Handler
import android.os.HandlerThread
2019-06-19 08:46:59 +00:00
import android.os.IBinder
import androidx.core.provider.FontRequest
import androidx.core.provider.FontsContractCompat
2019-06-19 08:46:59 +00:00
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDex
import com.airbnb.epoxy.EpoxyAsyncUtil
import com.airbnb.epoxy.EpoxyController
import com.facebook.stetho.Stetho
import com.github.piasy.biv.BigImageViewer
import com.github.piasy.biv.loader.glide.GlideImageLoader
import com.jakewharton.threetenabp.AndroidThreeTen
2019-03-19 11:29:45 +00:00
import im.vector.matrix.android.api.Matrix
2019-06-19 08:46:59 +00:00
import im.vector.matrix.android.internal.session.sync.job.SyncService
import im.vector.riotredesign.core.di.AppModule
2019-06-20 13:22:40 +00:00
import im.vector.riotredesign.core.services.AlarmSyncBroadcastReceiver
2019-06-19 08:46:59 +00:00
import im.vector.riotredesign.core.services.VectorSyncService
2019-06-06 16:34:14 +00:00
import im.vector.riotredesign.features.configuration.VectorConfiguration
import im.vector.riotredesign.features.crypto.keysbackup.KeysBackupModule
import im.vector.riotredesign.features.home.HomeModule
2019-05-16 08:23:57 +00:00
import im.vector.riotredesign.features.lifecycle.VectorActivityLifecycleCallbacks
2019-06-19 08:46:59 +00:00
import im.vector.riotredesign.features.notifications.NotificationUtils
import im.vector.riotredesign.features.notifications.PushRuleTriggerListener
import im.vector.riotredesign.features.rageshake.VectorFileLogger
import im.vector.riotredesign.features.rageshake.VectorUncaughtExceptionHandler
2019-05-24 20:27:26 +00:00
import im.vector.riotredesign.features.roomdirectory.RoomDirectoryModule
import im.vector.riotredesign.features.version.getVersion
2019-06-20 13:22:40 +00:00
import im.vector.riotredesign.push.fcm.FcmHelper
2019-06-19 08:46:59 +00:00
import org.koin.android.ext.android.get
2019-06-06 16:34:14 +00:00
import org.koin.android.ext.android.inject
import org.koin.log.EmptyLogger
2018-10-03 15:56:33 +00:00
import org.koin.standalone.StandAloneContext.startKoin
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*
2019-06-19 08:46:59 +00:00
import java.util.concurrent.TimeUnit
2018-10-03 15:56:33 +00:00
2019-06-20 13:22:40 +00:00
class VectorApplication : Application() {
2018-10-03 15:56:33 +00:00
2019-05-16 08:23:57 +00:00
lateinit var appContext: Context
//font thread handler
private var mFontThreadHandler: Handler? = null
2019-06-06 16:34:14 +00:00
val vectorConfiguration: VectorConfiguration by inject()
2019-05-16 08:23:57 +00:00
2019-06-19 08:46:59 +00:00
// var slowMode = false
2018-10-03 15:56:33 +00:00
override fun onCreate() {
super.onCreate()
2019-05-16 08:23:57 +00:00
appContext = this
logInfo()
VectorUncaughtExceptionHandler.activate(this)
// Log
VectorFileLogger.init(this)
Timber.plant(Timber.DebugTree(), VectorFileLogger)
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
}
AndroidThreeTen.init(this)
BigImageViewer.initialize(GlideImageLoader.with(applicationContext))
EpoxyController.defaultDiffingHandler = EpoxyAsyncUtil.getAsyncBackgroundHandler()
EpoxyController.defaultModelBuildingHandler = EpoxyAsyncUtil.getAsyncBackgroundHandler()
val appModule = AppModule(applicationContext).definition
val homeModule = HomeModule().definition
2019-05-24 20:27:26 +00:00
val roomDirectoryModule = RoomDirectoryModule().definition
val keysBackupModule = KeysBackupModule().definition
val koin = startKoin(listOf(appModule, homeModule, roomDirectoryModule, keysBackupModule), logger = EmptyLogger())
2019-03-19 11:29:45 +00:00
Matrix.getInstance().setApplicationFlavor(BuildConfig.FLAVOR_DESCRIPTION)
2019-05-16 08:23:57 +00:00
registerActivityLifecycleCallbacks(VectorActivityLifecycleCallbacks())
2019-06-06 16:34:14 +00:00
val fontRequest = FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
"Noto Color Emoji Compat",
R.array.com_google_android_gms_fonts_certs
)
FontsContractCompat.requestFont(this, fontRequest, koin.koinContext.get<EmojiCompatFontProvider>(), getFontThreadHandler())
2019-06-06 16:34:14 +00:00
vectorConfiguration.initConfiguration()
2019-06-19 08:46:59 +00:00
NotificationUtils.createNotificationChannels(applicationContext)
ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun entersForeground() {
2019-06-20 13:22:40 +00:00
AlarmSyncBroadcastReceiver.cancelAlarm(appContext)
Matrix.getInstance().currentSession?.also {
it.stopAnyBackgroundSync()
2019-06-19 08:46:59 +00:00
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun entersBackground() {
Timber.i("App entered background")
2019-06-20 13:22:40 +00:00
if (FcmHelper.isPushSupported()) {
//TODO FCM fallback
} else {
//TODO check if notifications are enabled for this device
//We need to use alarm in this mode
AlarmSyncBroadcastReceiver.scheduleAlarm(applicationContext,4_000L)
2019-06-19 08:46:59 +00:00
Timber.i("Alarm scheduled to restart service")
2019-06-20 13:22:40 +00:00
2019-06-19 08:46:59 +00:00
}
}
})
Matrix.getInstance().currentSession?.let {
it.refreshPushers()
//bind to the sync service
get<PushRuleTriggerListener>().startWithSession(it)
}
2018-10-03 15:56:33 +00:00
}
private fun logInfo() {
val appVersion = getVersion(longFormat = true, useBuildNumber = true)
val sdkVersion = Matrix.getSdkVersion()
val date = SimpleDateFormat("MM-dd HH:mm:ss.SSSZ", Locale.US).format(Date())
Timber.v("----------------------------------------------------------------")
Timber.v("----------------------------------------------------------------")
Timber.v(" Application version: $appVersion")
Timber.v(" SDK version: $sdkVersion")
Timber.v(" Local time: $date")
Timber.v("----------------------------------------------------------------")
Timber.v("----------------------------------------------------------------\n\n\n\n")
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
2019-06-06 16:34:14 +00:00
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
vectorConfiguration.onConfigurationChanged(newConfig)
}
private fun getFontThreadHandler(): Handler {
if (mFontThreadHandler == null) {
val handlerThread = HandlerThread("fonts")
handlerThread.start()
mFontThreadHandler = Handler(handlerThread.looper)
}
return mFontThreadHandler!!
}
2018-10-03 15:56:33 +00:00
}