diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt index 901ba75d16..a121b8a85d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt @@ -99,12 +99,28 @@ class Matrix private constructor(context: Context, matrixConfiguration: MatrixCo private lateinit var instance: Matrix private val isInit = AtomicBoolean(false) + /** + * Creates a new instance of Matrix, it's recommend to manage this instance as a singleton. + * To make use of the built in singleton use Matrix.initialise() and/or Matrix.getInstance(context) instead + **/ + fun createInstance(context: Context, matrixConfiguration: MatrixConfiguration): Matrix { + return Matrix(context.applicationContext, matrixConfiguration) + } + + /** + * Initializes a singleton instance of Matrix for the given MatrixConfiguration + * This instance will be returned by Matrix.getInstance(context) + */ fun initialize(context: Context, matrixConfiguration: MatrixConfiguration) { if (isInit.compareAndSet(false, true)) { instance = Matrix(context.applicationContext, matrixConfiguration) } } + /** + * Either provides an already initialized singleton Matrix instance or queries the application context for a MatrixConfiguration.Provider + * to lazily create and store the instance. + */ fun getInstance(context: Context): Matrix { if (isInit.compareAndSet(false, true)) { val appContext = context.applicationContext diff --git a/vector/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt b/vector/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt index fab41a68bb..322f5fa23d 100644 --- a/vector/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt +++ b/vector/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt @@ -26,6 +26,5 @@ fun getMatrixInstance(): Matrix { val configuration = MatrixConfiguration( roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context) ) - Matrix.initialize(context, configuration) - return Matrix.getInstance(context) + return Matrix.createInstance(context, configuration) } diff --git a/vector/src/main/java/im/vector/app/core/di/SingletonModule.kt b/vector/src/main/java/im/vector/app/core/di/SingletonModule.kt index 7db88b801b..b4bee417b4 100644 --- a/vector/src/main/java/im/vector/app/core/di/SingletonModule.kt +++ b/vector/src/main/java/im/vector/app/core/di/SingletonModule.kt @@ -120,8 +120,7 @@ object VectorStaticModule { @Provides @Singleton fun providesMatrix(context: Context, configuration: MatrixConfiguration): Matrix { - Matrix.initialize(context, configuration) - return Matrix.getInstance(context) + return Matrix.createInstance(context, configuration) } @Provides