Skip to content

Instantly share code, notes, and snippets.

@mvasilova
Created December 9, 2019 07:37
Show Gist options
  • Save mvasilova/02518552c3a568a4bf52c0afdccfb238 to your computer and use it in GitHub Desktop.
Save mvasilova/02518552c3a568a4bf52c0afdccfb238 to your computer and use it in GitHub Desktop.
Force update App Android (Kotlin, FirebaseRemoteConfig)
//usages
private lateinit var updateApp: UpdateApp
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
updateApp = UpdateApp(this)
}
override fun onResume() {
super.onResume()
updateApp.check()
}
//extensions
fun getAppVersion(context: Context): String {
var result = ""
try {
result = context.packageManager.getPackageInfo(context.packageName, 0).versionName
result = result.replace("[a-zA-Z]|-".toRegex(), "")
} catch (e: PackageManager.NameNotFoundException) { }
return result
}
fun openStore(context: Context, updateUrl: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(updateUrl))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
//DON'T FORGET TO ADD CONFIG PARAMS TO FIREBASE ACCOUNT
//the left menu choose remote config and add these parameters.
//see params name in companion object in UpdateApp.kt
//Thanks
//This code is pulled and adapted from UpdateMe library (https://github.com/salyangoz/updateme-android)
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
import kotlinx.android.synthetic.main.dialog_common_errors.view.*
import ru.profsoft.farmakopeika.R
import ru.profsoft.farmakopeika.ui.utils.getAppVersion
import ru.profsoft.farmakopeika.ui.utils.gone
import ru.profsoft.farmakopeika.ui.utils.openStore
class UpdateApp(private val activity: AppCompatActivity) {
private val remoteConfig = FirebaseRemoteConfig.getInstance()
init {
val configSettings = FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build()
remoteConfig.setConfigSettingsAsync(configSettings)
}
fun check() {
remoteConfig.fetchAndActivate()
.addOnCompleteListener(activity) { task ->
if (task.isSuccessful) {
val dialogMessage = remoteConfig.getString(KEY_DIALOG_DESCRIPTION)
val actualVersion = remoteConfig.getString(KEY_CURRENT_VERSION)
val updateUrl = remoteConfig.getString(KEY_UPDATE_URL)
val appVersion = getAppVersion(activity)
if (appVersion < actualVersion) {
val dialogView = LayoutInflater.from(activity)
.inflate(R.layout.dialog_common_errors, null)
val builder = AlertDialog.Builder(activity).setView(dialogView).setCancelable(false).show()
dialogView.tvMessage.text = dialogMessage
dialogView.btnNegative.gone()
dialogView.view.gone()
dialogView.btnPositive.text = activity.getString(R.string.dialog_button_update)
dialogView.btnPositive.setOnClickListener {
openStore(activity, updateUrl)
builder.cancel()
}
}
}
}
}
companion object {
const val KEY_CURRENT_VERSION = "android_current_version_app"
const val KEY_UPDATE_URL = "android_store_url"
const val KEY_DIALOG_DESCRIPTION = "android_dialog_description"
}
}
@nicemak
Copy link

nicemak commented Dec 20, 2019

thanks for the gist :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment