Extract storage to its own class.

This commit is contained in:
Benoit Marty 2022-06-01 17:57:47 +02:00 committed by Benoit Marty
parent bdb2d29666
commit bbbeb4b283
6 changed files with 95 additions and 67 deletions

View File

@ -29,7 +29,7 @@ import kotlin.math.abs
private const val DEFAULT_PUSHER_FILE_TAG = "mobile"
class PushersManager @Inject constructor(
private val unifiedPushHelper: UnifiedPushHelper,
private val unifiedPushStore: UnifiedPushStore,
private val activeSessionHolder: ActiveSessionHolder,
private val localeProvider: LocaleProvider,
private val stringProvider: StringProvider,
@ -39,9 +39,9 @@ class PushersManager @Inject constructor(
val currentSession = activeSessionHolder.getActiveSession()
currentSession.pushersService().testPush(
unifiedPushHelper.getPushGateway()!!,
unifiedPushStore.getPushGateway()!!,
stringProvider.getString(R.string.pusher_app_id),
unifiedPushHelper.getEndpointOrToken().orEmpty(),
unifiedPushStore.getEndpointOrToken().orEmpty(),
TEST_EVENT_ID
)
}

View File

@ -18,13 +18,11 @@ package im.vector.app.core.pushers
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.content.edit
import androidx.fragment.app.FragmentActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.moshi.JsonClass
import im.vector.app.BuildConfig
import im.vector.app.R
import im.vector.app.core.di.DefaultSharedPreferences
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
@ -40,59 +38,13 @@ import javax.inject.Inject
class UnifiedPushHelper @Inject constructor(
private val context: Context,
private val unifiedPushStore: UnifiedPushStore,
private val stringProvider: StringProvider,
private val vectorPreferences: VectorPreferences,
private val matrix: Matrix,
) {
companion object {
private const val PREFS_ENDPOINT_OR_TOKEN = "UP_ENDPOINT_OR_TOKEN"
private const val PREFS_PUSH_GATEWAY = "PUSH_GATEWAY"
}
private val up = UnifiedPush
/**
* Retrieves the UnifiedPush Endpoint.
*
* @return the UnifiedPush Endpoint or null if not received
*/
fun getEndpointOrToken(): String? {
return DefaultSharedPreferences.getInstance(context).getString(PREFS_ENDPOINT_OR_TOKEN, null)
}
/**
* Store UnifiedPush Endpoint to the SharedPrefs.
* TODO Store in realm
*
* @param endpoint the endpoint to store
*/
fun storeUpEndpoint(endpoint: String?) {
DefaultSharedPreferences.getInstance(context).edit {
putString(PREFS_ENDPOINT_OR_TOKEN, endpoint)
}
}
/**
* Retrieves the Push Gateway.
*
* @return the Push Gateway or null if not defined
*/
fun getPushGateway(): String? {
return DefaultSharedPreferences.getInstance(context).getString(PREFS_PUSH_GATEWAY, null)
}
/**
* Store Push Gateway to the SharedPrefs.
* TODO Store in realm
*
* @param gateway the push gateway to store
*/
private fun storePushGateway(gateway: String?) {
DefaultSharedPreferences.getInstance(context).edit {
putString(PREFS_PUSH_GATEWAY, gateway)
}
}
fun register(
activity: FragmentActivity,
onDoneRunnable: Runnable? = null,
@ -189,13 +141,13 @@ class UnifiedPushHelper @Inject constructor(
vectorPreferences.setFdroidSyncBackgroundMode(mode)
runBlocking {
try {
pushersManager?.unregisterPusher(getEndpointOrToken().orEmpty())
pushersManager?.unregisterPusher(unifiedPushStore.getEndpointOrToken().orEmpty())
} catch (e: Exception) {
Timber.d(e, "Probably unregistering a non existing pusher")
}
}
storeUpEndpoint(null)
storePushGateway(null)
unifiedPushStore.storeUpEndpoint(null)
unifiedPushStore.storePushGateway(null)
up.unregisterApp(context)
}
@ -217,7 +169,7 @@ class UnifiedPushHelper @Inject constructor(
// register app_id type upfcm on sygnal
// the pushkey if FCM key
if (up.getDistributor(context) == context.packageName) {
storePushGateway(stringProvider.getString(R.string.pusher_http_url))
unifiedPushStore.storePushGateway(stringProvider.getString(R.string.pusher_http_url))
onDoneRunnable?.run()
return
}
@ -233,7 +185,7 @@ class UnifiedPushHelper @Inject constructor(
?.let { discoveryResponse ->
if (discoveryResponse.unifiedpush.gateway == "matrix") {
Timber.d("Using custom gateway")
storePushGateway(custom)
unifiedPushStore.storePushGateway(custom)
onDoneRunnable?.run()
return
}
@ -241,7 +193,7 @@ class UnifiedPushHelper @Inject constructor(
} catch (e: Throwable) {
Timber.d(e, "Cannot try custom gateway")
}
storePushGateway(gateway)
unifiedPushStore.storePushGateway(gateway)
onDoneRunnable?.run()
}
@ -275,7 +227,7 @@ class UnifiedPushHelper @Inject constructor(
}
fun getPrivacyFriendlyUpEndpoint(): String? {
val endpoint = getEndpointOrToken()
val endpoint = unifiedPushStore.getEndpointOrToken()
if (endpoint.isNullOrEmpty()) return endpoint
if (isEmbeddedDistributor()) {
return endpoint

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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
*
* 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.
*/
package im.vector.app.core.pushers
import android.content.Context
import androidx.core.content.edit
import im.vector.app.core.di.DefaultSharedPreferences
import javax.inject.Inject
class UnifiedPushStore @Inject constructor(
context: Context,
) {
companion object {
private const val PREFS_ENDPOINT_OR_TOKEN = "UP_ENDPOINT_OR_TOKEN"
private const val PREFS_PUSH_GATEWAY = "PUSH_GATEWAY"
}
private val defaultPrefs = DefaultSharedPreferences.getInstance(context)
/**
* Retrieves the UnifiedPush Endpoint.
*
* @return the UnifiedPush Endpoint or null if not received
*/
fun getEndpointOrToken(): String? {
return defaultPrefs.getString(PREFS_ENDPOINT_OR_TOKEN, null)
}
/**
* Store UnifiedPush Endpoint to the SharedPrefs.
*
* @param endpoint the endpoint to store
*/
fun storeUpEndpoint(endpoint: String?) {
defaultPrefs.edit {
putString(PREFS_ENDPOINT_OR_TOKEN, endpoint)
}
}
/**
* Retrieves the Push Gateway.
*
* @return the Push Gateway or null if not defined
*/
fun getPushGateway(): String? {
return defaultPrefs.getString(PREFS_PUSH_GATEWAY, null)
}
/**
* Store Push Gateway to the SharedPrefs.
*
* @param gateway the push gateway to store
*/
fun storePushGateway(gateway: String?) {
defaultPrefs.edit {
putString(PREFS_PUSH_GATEWAY, gateway)
}
}
}

View File

@ -86,6 +86,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
@Inject lateinit var wifiDetector: WifiDetector
@Inject lateinit var guardServiceStarter: GuardServiceStarter
@Inject lateinit var unifiedPushHelper: UnifiedPushHelper
@Inject lateinit var unifiedPushStore: UnifiedPushStore
private val coroutineScope = CoroutineScope(SupervisorJob())
@ -160,11 +161,11 @@ class VectorMessagingReceiver : MessagingReceiver() {
if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) {
// If the endpoint has changed
// or the gateway has changed
if (unifiedPushHelper.getEndpointOrToken() != endpoint) {
unifiedPushHelper.storeUpEndpoint(endpoint)
if (unifiedPushStore.getEndpointOrToken() != endpoint) {
unifiedPushStore.storeUpEndpoint(endpoint)
coroutineScope.launch {
unifiedPushHelper.storeCustomOrDefaultGateway(endpoint) {
unifiedPushHelper.getPushGateway()?.let {
unifiedPushStore.getPushGateway()?.let {
pushersManager.enqueueRegisterPusher(endpoint, it)
}
}
@ -192,7 +193,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
guardServiceStarter.start()
runBlocking {
try {
pushersManager.unregisterPusher(unifiedPushHelper.getEndpointOrToken().orEmpty())
pushersManager.unregisterPusher(unifiedPushStore.getEndpointOrToken().orEmpty())
} catch (e: Exception) {
Timber.tag(loggerTag.value).d("Probably unregistering a non existing pusher")
}

View File

@ -26,6 +26,7 @@ import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.pushers.UnifiedPushStore
import im.vector.app.core.resources.StringProvider
import org.matrix.android.sdk.api.session.pushers.PusherState
import javax.inject.Inject
@ -36,11 +37,12 @@ class TestEndpointAsTokenRegistration @Inject constructor(
private val pushersManager: PushersManager,
private val activeSessionHolder: ActiveSessionHolder,
private val unifiedPushHelper: UnifiedPushHelper,
private val unifiedPushStore: UnifiedPushStore,
) : TroubleshootTest(R.string.settings_troubleshoot_test_endpoint_registration_title) {
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
// Check if we have a registered pusher for this token
val endpoint = unifiedPushHelper.getEndpointOrToken() ?: run {
val endpoint = unifiedPushStore.getEndpointOrToken() ?: run {
status = TestStatus.FAILED
return
}

View File

@ -19,19 +19,19 @@ package im.vector.app.features.settings.troubleshoot
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import im.vector.app.R
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.pushers.UnifiedPushStore
import im.vector.app.core.resources.StringProvider
import javax.inject.Inject
class TestUnifiedPushGateway @Inject constructor(
private val unifiedPushHelper: UnifiedPushHelper,
private val unifiedPushStore: UnifiedPushStore,
private val stringProvider: StringProvider
) : TroubleshootTest(R.string.settings_troubleshoot_test_current_gateway_title) {
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
description = stringProvider.getString(
R.string.settings_troubleshoot_test_current_gateway,
unifiedPushHelper.getPushGateway()
unifiedPushStore.getPushGateway()
)
status = TestStatus.SUCCESS
}