2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*
|
2019-01-25 13:04:59 +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-04 13:19:03 +00:00
|
|
|
package im.vector.matrix.android.api
|
2018-10-03 15:56:33 +00:00
|
|
|
|
2018-10-08 15:52:45 +00:00
|
|
|
import android.content.Context
|
2019-01-18 15:26:17 +00:00
|
|
|
import androidx.lifecycle.ProcessLifecycleOwner
|
2019-06-20 17:26:59 +00:00
|
|
|
import androidx.work.Configuration
|
|
|
|
import androidx.work.WorkManager
|
2018-10-18 09:16:02 +00:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2019-06-21 09:13:16 +00:00
|
|
|
import im.vector.matrix.android.BuildConfig
|
2018-10-08 15:52:45 +00:00
|
|
|
import im.vector.matrix.android.api.auth.Authenticator
|
2019-06-20 17:26:59 +00:00
|
|
|
import im.vector.matrix.android.internal.SessionManager
|
2019-06-17 16:17:37 +00:00
|
|
|
import im.vector.matrix.android.internal.di.DaggerMatrixComponent
|
2019-03-19 11:29:45 +00:00
|
|
|
import im.vector.matrix.android.internal.network.UserAgentHolder
|
2018-10-17 13:20:26 +00:00
|
|
|
import im.vector.matrix.android.internal.util.BackgroundDetectionObserver
|
2019-06-17 16:17:37 +00:00
|
|
|
import org.matrix.olm.OlmManager
|
2019-01-03 20:25:51 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
2019-06-14 14:32:23 +00:00
|
|
|
import javax.inject.Inject
|
2018-10-03 15:56:33 +00:00
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
data class MatrixConfiguration(
|
|
|
|
val applicationFlavor: String = "Default-application-flavor"
|
|
|
|
) {
|
|
|
|
|
|
|
|
interface Provider {
|
|
|
|
fun providesMatrixConfiguration(): MatrixConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* This is the main entry point to the matrix sdk.
|
|
|
|
* To get the singleton instance, use getInstance static method.
|
|
|
|
*/
|
2019-06-20 17:26:59 +00:00
|
|
|
class Matrix private constructor(context: Context, matrixConfiguration: MatrixConfiguration) {
|
2018-10-08 15:52:45 +00:00
|
|
|
|
2019-06-14 14:32:23 +00:00
|
|
|
@Inject internal lateinit var authenticator: Authenticator
|
|
|
|
@Inject internal lateinit var userAgentHolder: UserAgentHolder
|
|
|
|
@Inject internal lateinit var backgroundDetectionObserver: BackgroundDetectionObserver
|
2019-06-20 17:26:59 +00:00
|
|
|
@Inject internal lateinit var olmManager: OlmManager
|
|
|
|
@Inject internal lateinit var sessionManager: SessionManager
|
2018-10-03 15:56:33 +00:00
|
|
|
|
|
|
|
init {
|
2018-12-20 14:44:01 +00:00
|
|
|
Monarchy.init(context)
|
2019-06-17 16:17:37 +00:00
|
|
|
DaggerMatrixComponent.factory().create(context).inject(this)
|
2019-06-20 17:26:59 +00:00
|
|
|
if (context.applicationContext !is Configuration.Provider) {
|
|
|
|
WorkManager.initialize(context, Configuration.Builder().build())
|
|
|
|
}
|
2018-10-17 13:20:26 +00:00
|
|
|
ProcessLifecycleOwner.get().lifecycle.addObserver(backgroundDetectionObserver)
|
2019-06-20 17:26:59 +00:00
|
|
|
userAgentHolder.setApplicationFlavor(matrixConfiguration.applicationFlavor)
|
2018-10-03 15:56:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
fun getUserAgent() = userAgentHolder.userAgent
|
|
|
|
|
2018-10-08 15:52:45 +00:00
|
|
|
fun authenticator(): Authenticator {
|
|
|
|
return authenticator
|
2018-10-03 15:56:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 14:44:01 +00:00
|
|
|
companion object {
|
2019-06-20 17:26:59 +00:00
|
|
|
|
2018-12-20 14:44:01 +00:00
|
|
|
private lateinit var instance: Matrix
|
2019-01-03 20:25:51 +00:00
|
|
|
private val isInit = AtomicBoolean(false)
|
2018-12-20 14:44:01 +00:00
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
fun initialize(context: Context, matrixConfiguration: MatrixConfiguration) {
|
2019-01-03 20:25:51 +00:00
|
|
|
if (isInit.compareAndSet(false, true)) {
|
2019-06-20 17:26:59 +00:00
|
|
|
instance = Matrix(context.applicationContext, matrixConfiguration)
|
2019-01-03 20:25:51 +00:00
|
|
|
}
|
2018-12-20 14:44:01 +00:00
|
|
|
}
|
2018-10-08 15:52:45 +00:00
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
fun getInstance(context: Context): Matrix {
|
|
|
|
if (isInit.compareAndSet(false, true)) {
|
|
|
|
val appContext = context.applicationContext
|
|
|
|
if (appContext is MatrixConfiguration.Provider) {
|
|
|
|
val matrixConfiguration = (appContext as MatrixConfiguration.Provider).providesMatrixConfiguration()
|
|
|
|
instance = Matrix(appContext, matrixConfiguration)
|
|
|
|
} else {
|
2019-07-09 13:26:32 +00:00
|
|
|
throw IllegalStateException("Matrix is not initialized properly." +
|
|
|
|
" You should call Matrix.initialize or let your application implements MatrixConfiguration.Provider.")
|
2019-06-20 17:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 14:44:01 +00:00
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2019-06-21 09:13:16 +00:00
|
|
|
fun getSdkVersion(): String {
|
|
|
|
return BuildConfig.VERSION_NAME + " (" + BuildConfig.GIT_SDK_REVISION + ")"
|
|
|
|
}
|
2018-12-20 14:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|