Skip to content

Added version check through Remote Config #3150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ dependencies {
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-messaging-directboot'
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-config'

// Hilt
implementation "com.google.dagger:hilt-android:$project.hiltVersion"
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/org/groundplatform/android/GroundApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.core.os.LocaleListCompat
import androidx.hilt.work.HiltWorkerFactory
import androidx.multidex.MultiDexApplication
import androidx.work.Configuration
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject
import org.groundplatform.android.Config.isReleaseBuild
Expand All @@ -35,6 +36,7 @@ class GroundApplication : MultiDexApplication(), Configuration.Provider {
@Inject lateinit var crashReportingTree: CrashReportingTree
@Inject lateinit var workerFactory: HiltWorkerFactory
@Inject lateinit var localValueStore: LocalValueStore
@Inject lateinit var remoteConfig: FirebaseRemoteConfig

override val workManagerConfiguration: Configuration
get() = Configuration.Builder().setWorkerFactory(workerFactory).build()
Expand All @@ -51,6 +53,7 @@ class GroundApplication : MultiDexApplication(), Configuration.Provider {
val selectedLanguage = localValueStore.selectedLanguage
val appLocale = LocaleListCompat.forLanguageTags(selectedLanguage)
AppCompatDelegate.setApplicationLocales(appLocale)
initiateRemoteConfig()
}

private fun setStrictMode() {
Expand All @@ -61,6 +64,22 @@ class GroundApplication : MultiDexApplication(), Configuration.Provider {
StrictMode.setVmPolicy(VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().build())
}

/**
* Initializes Firebase Remote Config by setting default values from the provided XML file and
* fetching remote values to activate them for use in the app.
*
* This method:
* - Sets default values using `firebase_remote_config_defaults.xml`
* - Asynchronously fetches the latest Remote Config values from Firebase
* - Immediately activates the fetched values
*
* Call this during app startup to ensure Remote Config values are available.
*/
private fun initiateRemoteConfig() {
remoteConfig.setDefaultsAsync(R.xml.firebase_remote_config_defaults)
remoteConfig.fetchAndActivate()
}

/** Reports any error with priority more than "info" to Crashlytics. */
class CrashReportingTree
@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package org.groundplatform.android
import android.content.Context
import android.content.res.Resources
import com.google.android.gms.common.GoogleApiAvailability
import com.google.firebase.Firebase
import com.google.firebase.FirebaseApp
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.remoteConfig
import com.google.firebase.remoteconfig.remoteConfigSettings
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -43,4 +48,16 @@ object GroundApplicationModule {
}

@Provides fun provideLocale() = Locale.getDefault()

@Provides
@Singleton
fun provideFirebaseRemoteConfig(@ApplicationContext context: Context): FirebaseRemoteConfig {
if (FirebaseApp.getApps(context).isEmpty()) {
FirebaseApp.initializeApp(context)
}

return Firebase.remoteConfig.apply {
setConfigSettingsAsync(remoteConfigSettings { minimumFetchIntervalInSeconds = 3600 })
}
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/org/groundplatform/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.groundplatform.android

import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
Expand Down Expand Up @@ -197,6 +198,9 @@ class MainActivity : AbstractActivity() {
override fun onResume() {
super.onResume()
viewModel.checkAuthStatus()
if (viewModel.isAppUpdateAvailable()) {
showForceUpdateDialog()
}
}

/** Override up button behavior to use Navigation Components back stack. */
Expand Down Expand Up @@ -261,4 +265,28 @@ class MainActivity : AbstractActivity() {
navigate(action)
}
}

private fun showForceUpdateDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.dialog_title_update_required)
.setMessage(R.string.dialog_message_update_required)
.setCancelable(false)
.setPositiveButton(R.string.dialog_button_update) { dialog, _ ->
val appPackageName = packageName
try {
startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName"))
)
} catch (e: ActivityNotFoundException) {
Timber.e("Not able to open play store: $e")
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName"),
)
)
}
}
.show()
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/org/groundplatform/android/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.android.gms.auth.api.signin.GoogleSignInStatusCodes.SIGN_IN_CANCELLED
import com.google.android.gms.common.api.ApiException
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -55,6 +56,7 @@ constructor(
private val reactivateLastSurvey: ReactivateLastSurveyUseCase,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
private val authenticationManager: AuthenticationManager,
private val remoteConfig: FirebaseRemoteConfig,
) : AbstractViewModel() {

private val _navigationRequests: MutableSharedFlow<MainUiState?> = MutableSharedFlow()
Expand Down Expand Up @@ -138,4 +140,10 @@ constructor(

/** Returns true if the user has already accepted the Terms of Service. */
private fun isTosAccepted(): Boolean = termsOfServiceRepository.isTermsOfServiceAccepted

fun isAppUpdateAvailable(currentVersion: Int = BuildConfig.VERSION_CODE): Boolean {
val forceUpdate = remoteConfig.getBoolean("force_update")
val latestVersion = remoteConfig.getLong("latest_version_code")
return forceUpdate && currentVersion.toLong() < latestVersion
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,8 @@
<string name="lang_french">Francés</string>
<string name="lang_spanish">Español</string>
<string name="lang_portuguese">Portugués</string>

<string name="dialog_title_update_required">Actualización requerida</string>
<string name="dialog_message_update_required">Hay una nueva versión de la aplicación disponible. Por favor, actualiza para seguir usando la aplicación.</string>
<string name="dialog_button_update">Actualizar</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,8 @@
<string name="lang_french">Français</string>
<string name="lang_spanish">Espagnol</string>
<string name="lang_portuguese">Portugais</string>

<string name="dialog_title_update_required">Mise à jour requise</string>
<string name="dialog_message_update_required">Une nouvelle version de l&#8217;application est disponible. Veuillez mettre à jour pour continuer à utiliser l&#8217;application.</string>
<string name="dialog_button_update">Mettre à jour</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,8 @@
<string name="lang_french">Francês</string>
<string name="lang_spanish">Espanhol</string>
<string name="lang_portuguese">Português</string>

<string name="dialog_title_update_required">Atualização necessária</string>
<string name="dialog_message_update_required">Uma nova versão do aplicativo está disponível. Atualize para continuar usando o aplicativo.</string>
<string name="dialog_button_update">Atualizar</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,8 @@
<string name="lang_french">French</string>
<string name="lang_spanish">Spanish</string>
<string name="lang_portuguese">Portuguese</string>

<string name="dialog_title_update_required">Update Required</string>
<string name="dialog_message_update_required">A new version of the app is available. Please update to continue using the app.</string>
<string name="dialog_button_update">Update</string>
</resources>
28 changes: 28 additions & 0 deletions app/src/main/res/xml/firebase_remote_config_defaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2025 Google LLC
~
~ 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
~
~ https://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.
-->

<defaultsMap>
<entry>
<key>force_update</key>
<value>false</value>
</entry>
<entry>
<key>latest_version_code</key>
<value>0</value>
</entry>
</defaultsMap>
Loading
Loading