diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt index e2ac4f8822..eaa3d57d42 100644 --- a/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt +++ b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt @@ -23,14 +23,14 @@ import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver import im.vector.app.features.settings.BackgroundSyncMode import im.vector.app.features.settings.VectorPreferences import timber.log.Timber +import javax.inject.Inject -object BackgroundSyncStarter { - fun start( - context: Context, - vectorPreferences: VectorPreferences, - activeSessionHolder: ActiveSessionHolder, - clock: Clock - ) { +class BackgroundSyncStarter @Inject constructor( + private val context: Context, + private val vectorPreferences: VectorPreferences, + private val clock: Clock +) { + fun start(activeSessionHolder: ActiveSessionHolder) { if (vectorPreferences.areNotificationEnabledForDevice()) { val activeSession = activeSessionHolder.getSafeActiveSession() ?: return when (vectorPreferences.getFdroidSyncBackgroundMode()) { diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt index aacd7723f5..f22aafbeb4 100644 --- a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt +++ b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt @@ -20,20 +20,20 @@ package im.vector.app.fdroid.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import im.vector.app.core.extensions.singletonEntryPoint +import dagger.hilt.android.AndroidEntryPoint +import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.fdroid.BackgroundSyncStarter import timber.log.Timber +import javax.inject.Inject +@AndroidEntryPoint class OnApplicationUpgradeOrRebootReceiver : BroadcastReceiver() { + @Inject lateinit var activeSessionHolder: ActiveSessionHolder + @Inject lateinit var backgroundSyncStarter: BackgroundSyncStarter + override fun onReceive(context: Context, intent: Intent) { Timber.v("## onReceive() ${intent.action}") - val singletonEntryPoint = context.singletonEntryPoint() - BackgroundSyncStarter.start( - context, - singletonEntryPoint.vectorPreferences(), - singletonEntryPoint.activeSessionHolder(), - singletonEntryPoint.clock() - ) + backgroundSyncStarter.start(activeSessionHolder) } } diff --git a/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt b/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt index 388521a96d..24ff00a353 100755 --- a/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt +++ b/vector/src/fdroid/java/im/vector/app/push/fcm/FcmHelper.kt @@ -21,15 +21,17 @@ import android.app.Activity import android.content.Context import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.PushersManager -import im.vector.app.core.time.Clock import im.vector.app.fdroid.BackgroundSyncStarter import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver -import im.vector.app.features.settings.VectorPreferences +import javax.inject.Inject /** * This class has an alter ego in the gplay variant. */ -object FcmHelper { +class FcmHelper @Inject constructor( + private val context: Context, + private val backgroundSyncStarter: BackgroundSyncStarter, +) { fun isFirebaseAvailable(): Boolean = false @@ -38,17 +40,16 @@ object FcmHelper { * * @return the FCM token or null if not received from FCM */ - fun getFcmToken(context: Context): String? { + fun getFcmToken(): String? { return null } /** * Store FCM token to the SharedPrefs * - * @param context android context * @param token the token to store */ - fun storeFcmToken(context: Context, token: String?) { + fun storeFcmToken(token: String?) { // No op } @@ -61,18 +62,13 @@ object FcmHelper { // No op } - fun onEnterForeground(context: Context, activeSessionHolder: ActiveSessionHolder) { + fun onEnterForeground(activeSessionHolder: ActiveSessionHolder) { // try to stop all regardless of background mode activeSessionHolder.getSafeActiveSession()?.syncService()?.stopAnyBackgroundSync() AlarmSyncBroadcastReceiver.cancelAlarm(context) } - fun onEnterBackground( - context: Context, - vectorPreferences: VectorPreferences, - activeSessionHolder: ActiveSessionHolder, - clock: Clock - ) { - BackgroundSyncStarter.start(context, vectorPreferences, activeSessionHolder, clock) + fun onEnterBackground(activeSessionHolder: ActiveSessionHolder) { + backgroundSyncStarter.start(activeSessionHolder) } } diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestFirebaseToken.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestFirebaseToken.kt index 5a9dc90ec4..e7e3157f6b 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestFirebaseToken.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestFirebaseToken.kt @@ -32,7 +32,8 @@ import javax.inject.Inject */ class TestFirebaseToken @Inject constructor( private val context: FragmentActivity, - private val stringProvider: StringProvider + private val stringProvider: StringProvider, + private val fcmHelper: FcmHelper, ) : TroubleshootTest(R.string.settings_troubleshoot_test_fcm_title) { override fun perform(activityResultLauncher: ActivityResultLauncher) { @@ -68,7 +69,7 @@ class TestFirebaseToken @Inject constructor( description = stringProvider.getString(R.string.settings_troubleshoot_test_fcm_success, tok) Timber.e("Retrieved FCM token success [$tok].") // Ensure it is well store in our local storage - FcmHelper.storeFcmToken(context, token) + fcmHelper.storeFcmToken(token) } status = TestStatus.SUCCESS } diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestTokenRegistration.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestTokenRegistration.kt index a6220c2018..8c21404d20 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestTokenRegistration.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestTokenRegistration.kt @@ -37,13 +37,14 @@ class TestTokenRegistration @Inject constructor( private val context: FragmentActivity, private val stringProvider: StringProvider, private val pushersManager: PushersManager, - private val activeSessionHolder: ActiveSessionHolder + private val activeSessionHolder: ActiveSessionHolder, + private val fcmHelper: FcmHelper, ) : TroubleshootTest(R.string.settings_troubleshoot_test_token_registration_title) { override fun perform(activityResultLauncher: ActivityResultLauncher) { // Check if we have a registered pusher for this token - val fcmToken = FcmHelper.getFcmToken(context) ?: run { + val fcmToken = fcmHelper.getFcmToken() ?: run { status = TestStatus.FAILED return } diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt b/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt index cb569d9c16..a4eb9efc73 100755 --- a/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt @@ -26,16 +26,21 @@ import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.DefaultSharedPreferences import im.vector.app.core.pushers.PushersManager -import im.vector.app.core.time.Clock -import im.vector.app.features.settings.VectorPreferences import timber.log.Timber +import javax.inject.Inject /** * This class store the FCM token in SharedPrefs and ensure this token is retrieved. * It has an alter ego in the fdroid variant. */ -object FcmHelper { - private val PREFS_KEY_FCM_TOKEN = "FCM_TOKEN" +class FcmHelper @Inject constructor( + context: Context, +) { + companion object { + private const val PREFS_KEY_FCM_TOKEN = "FCM_TOKEN" + } + + private val sharedPrefs = DefaultSharedPreferences.getInstance(context) fun isFirebaseAvailable(): Boolean = true @@ -44,22 +49,18 @@ object FcmHelper { * * @return the FCM token or null if not received from FCM */ - fun getFcmToken(context: Context): String? { - return DefaultSharedPreferences.getInstance(context).getString(PREFS_KEY_FCM_TOKEN, null) + fun getFcmToken(): String? { + return sharedPrefs.getString(PREFS_KEY_FCM_TOKEN, null) } /** * Store FCM token to the SharedPrefs * TODO Store in realm * - * @param context android context * @param token the token to store */ - fun storeFcmToken( - context: Context, - token: String? - ) { - DefaultSharedPreferences.getInstance(context).edit { + fun storeFcmToken(token: String?) { + sharedPrefs.edit { putString(PREFS_KEY_FCM_TOKEN, token) } } @@ -76,7 +77,7 @@ object FcmHelper { try { FirebaseMessaging.getInstance().token .addOnSuccessListener { token -> - storeFcmToken(activity, token) + storeFcmToken(token) if (registerPusher) { pushersManager.enqueueRegisterPusherWithFcmKey(token) } @@ -98,24 +99,19 @@ object FcmHelper { * it doesn't, display a dialog that allows users to download the APK from * the Google Play Store or enable it in the device's system settings. */ - fun checkPlayServices(context: Context): Boolean { + private fun checkPlayServices(context: Context): Boolean { val apiAvailability = GoogleApiAvailability.getInstance() val resultCode = apiAvailability.isGooglePlayServicesAvailable(context) return resultCode == ConnectionResult.SUCCESS } @Suppress("UNUSED_PARAMETER") - fun onEnterForeground(context: Context, activeSessionHolder: ActiveSessionHolder) { + fun onEnterForeground(activeSessionHolder: ActiveSessionHolder) { // No op } @Suppress("UNUSED_PARAMETER") - fun onEnterBackground( - context: Context, - vectorPreferences: VectorPreferences, - activeSessionHolder: ActiveSessionHolder, - clock: Clock - ) { + fun onEnterBackground(activeSessionHolder: ActiveSessionHolder) { // No op } } diff --git a/vector/src/main/java/im/vector/app/VectorApplication.kt b/vector/src/main/java/im/vector/app/VectorApplication.kt index 7db0f99f5f..e888a257ef 100644 --- a/vector/src/main/java/im/vector/app/VectorApplication.kt +++ b/vector/src/main/java/im/vector/app/VectorApplication.kt @@ -43,7 +43,6 @@ import dagger.hilt.android.HiltAndroidApp import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.extensions.configureAndStart import im.vector.app.core.extensions.startSyncing -import im.vector.app.core.time.Clock import im.vector.app.features.analytics.VectorAnalytics import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.configuration.VectorConfiguration @@ -86,7 +85,6 @@ class VectorApplication : @Inject lateinit var emojiCompatWrapper: EmojiCompatWrapper @Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler @Inject lateinit var activeSessionHolder: ActiveSessionHolder - @Inject lateinit var clock: Clock @Inject lateinit var notificationDrawerManager: NotificationDrawerManager @Inject lateinit var vectorPreferences: VectorPreferences @Inject lateinit var versionProvider: VersionProvider @@ -100,6 +98,7 @@ class VectorApplication : @Inject lateinit var vectorFileLogger: VectorFileLogger @Inject lateinit var vectorAnalytics: VectorAnalytics @Inject lateinit var matrix: Matrix + @Inject lateinit var fcmHelper: FcmHelper // font thread handler private var fontThreadHandler: Handler? = null @@ -174,7 +173,7 @@ class VectorApplication : ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver { override fun onResume(owner: LifecycleOwner) { Timber.i("App entered foreground") - FcmHelper.onEnterForeground(appContext, activeSessionHolder) + fcmHelper.onEnterForeground(activeSessionHolder) activeSessionHolder.getSafeActiveSession()?.also { it.syncService().stopAnyBackgroundSync() } @@ -182,7 +181,7 @@ class VectorApplication : override fun onPause(owner: LifecycleOwner) { Timber.i("App entered background") - FcmHelper.onEnterBackground(appContext, vectorPreferences, activeSessionHolder, clock) + fcmHelper.onEnterBackground(activeSessionHolder) } }) ProcessLifecycleOwner.get().lifecycle.addObserver(appStateHandler) diff --git a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt index 897797b0ab..4c56fd9ad3 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt @@ -45,6 +45,7 @@ class UnifiedPushHelper @Inject constructor( private val vectorPreferences: VectorPreferences, private val matrix: Matrix, private val vectorFeatures: VectorFeatures, + private val fcmHelper: FcmHelper, ) { private val up = UnifiedPush @@ -132,7 +133,7 @@ class UnifiedPushHelper @Inject constructor( cancellable: Boolean, ) { val internalDistributorName = stringProvider.getString( - if (FcmHelper.isFirebaseAvailable()) { + if (fcmHelper.isFirebaseAvailable()) { R.string.unifiedpush_distributor_fcm_fallback } else { R.string.unifiedpush_distributor_background_sync @@ -244,11 +245,11 @@ class UnifiedPushHelper @Inject constructor( } fun isEmbeddedDistributor(): Boolean { - return up.getDistributor(context) == context.packageName && FcmHelper.isFirebaseAvailable() + return up.getDistributor(context) == context.packageName && fcmHelper.isFirebaseAvailable() } fun isBackgroundSync(): Boolean { - return up.getDistributor(context) == context.packageName && !FcmHelper.isFirebaseAvailable() + return up.getDistributor(context) == context.packageName && !fcmHelper.isFirebaseAvailable() } fun getPrivacyFriendlyUpEndpoint(): String? { diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index 26bb72b26f..f2690fa18a 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -129,6 +129,7 @@ class HomeActivity : @Inject lateinit var initSyncStepFormatter: InitSyncStepFormatter @Inject lateinit var appStateHandler: AppStateHandler @Inject lateinit var unifiedPushHelper: UnifiedPushHelper + @Inject lateinit var fcmHelper: FcmHelper private val createSpaceResultLauncher = registerStartForActivityResult { activityResult -> if (activityResult.resultCode == Activity.RESULT_OK) { @@ -191,7 +192,7 @@ class HomeActivity : supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false) unifiedPushHelper.register(this) { if (unifiedPushHelper.isEmbeddedDistributor()) { - FcmHelper.ensureFcmTokenIsRetrieved( + fcmHelper.ensureFcmTokenIsRetrieved( this, pushManager, vectorPreferences.areNotificationEnabledForDevice() diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestAvailableUnifiedPushDistributors.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestAvailableUnifiedPushDistributors.kt index a66954b023..acc0142924 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestAvailableUnifiedPushDistributors.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestAvailableUnifiedPushDistributors.kt @@ -27,13 +27,14 @@ import javax.inject.Inject class TestAvailableUnifiedPushDistributors @Inject constructor( private val unifiedPushHelper: UnifiedPushHelper, private val stringProvider: StringProvider, + private val fcmHelper: FcmHelper, ) : TroubleshootTest(R.string.settings_troubleshoot_test_distributors_title) { override fun perform(activityResultLauncher: ActivityResultLauncher) { val distributors = unifiedPushHelper.getExternalDistributors() description = if (distributors.isEmpty()) { stringProvider.getString( - if (FcmHelper.isFirebaseAvailable()) { + if (fcmHelper.isFirebaseAvailable()) { R.string.settings_troubleshoot_test_distributors_gplay } else { R.string.settings_troubleshoot_test_distributors_fdroid