2022-09-04 22:52:40 -03:00
|
|
|
package com.geeksville.mesh.android
|
|
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import android.provider.Settings
|
2023-01-26 11:32:46 -03:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2022-09-04 22:52:40 -03:00
|
|
|
import androidx.core.content.edit
|
|
|
|
|
import com.geeksville.mesh.analytics.AnalyticsProvider
|
2023-01-26 11:32:46 -03:00
|
|
|
import com.geeksville.mesh.util.exceptionReporter
|
2022-09-04 22:52:40 -03:00
|
|
|
import com.google.android.gms.common.ConnectionResult
|
2022-09-05 00:14:08 -03:00
|
|
|
import com.google.android.gms.common.GoogleApiAvailabilityLight
|
2023-01-26 11:32:46 -03:00
|
|
|
import com.suddenh4x.ratingdialog.AppRating
|
2022-09-04 22:52:40 -03:00
|
|
|
|
|
|
|
|
fun isGooglePlayAvailable(context: Context): Boolean {
|
2022-09-05 00:14:08 -03:00
|
|
|
val a = GoogleApiAvailabilityLight.getInstance()
|
2022-09-04 22:52:40 -03:00
|
|
|
val r = a.isGooglePlayServicesAvailable(context)
|
|
|
|
|
return r != ConnectionResult.SERVICE_MISSING && r != ConnectionResult.SERVICE_INVALID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by kevinh on 1/4/15.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-04 00:22:41 -03:00
|
|
|
open class GeeksvilleApplication : Application(), Logging {
|
2022-09-04 22:52:40 -03:00
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
lateinit var analytics: AnalyticsProvider
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Are we running inside the testlab?
|
|
|
|
|
val isInTestLab: Boolean
|
|
|
|
|
get() {
|
|
|
|
|
val testLabSetting =
|
|
|
|
|
Settings.System.getString(contentResolver, "firebase.test.lab") ?: null
|
|
|
|
|
if(testLabSetting != null)
|
|
|
|
|
info("Testlab is $testLabSetting")
|
|
|
|
|
return "true" == testLabSetting
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val analyticsPrefs: SharedPreferences by lazy {
|
2022-09-13 01:25:36 -03:00
|
|
|
getSharedPreferences("analytics-prefs", Context.MODE_PRIVATE)
|
2022-09-04 22:52:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isAnalyticsAllowed: Boolean
|
|
|
|
|
get() = analyticsPrefs.getBoolean("allowed", true)
|
|
|
|
|
set(value) {
|
2022-09-13 01:25:36 -03:00
|
|
|
analyticsPrefs.edit {
|
2022-09-04 22:52:40 -03:00
|
|
|
putBoolean("allowed", value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change the flag with the providers
|
|
|
|
|
analytics.setEnabled(value && !isInTestLab) // Never do analytics in the test lab
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 11:32:46 -03:00
|
|
|
/** Ask user to rate in play store */
|
|
|
|
|
fun askToRate(activity: AppCompatActivity) {
|
|
|
|
|
if (!isGooglePlayAvailable(this)) return
|
|
|
|
|
exceptionReporter { // we don't want to crash our app because of bugs in this optional feature
|
|
|
|
|
AppRating.Builder(activity)
|
|
|
|
|
.setMinimumLaunchTimes(10) // default is 5, 3 means app is launched 3 or more times
|
|
|
|
|
.setMinimumDays(10) // default is 5, 0 means install day, 10 means app is launched 10 or more days later than installation
|
|
|
|
|
.setMinimumLaunchTimesToShowAgain(5) // default is 5, 1 means app is launched 1 or more times after neutral button clicked
|
|
|
|
|
.setMinimumDaysToShowAgain(14) // default is 14, 1 means app is launched 1 or more days after neutral button clicked
|
|
|
|
|
.showIfMeetsConditions()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 22:52:40 -03:00
|
|
|
override fun onCreate() {
|
2022-10-04 00:22:41 -03:00
|
|
|
super.onCreate()
|
2022-09-04 22:52:40 -03:00
|
|
|
|
2024-02-08 16:45:28 -03:00
|
|
|
val firebaseAnalytics = com.geeksville.mesh.analytics.FirebaseAnalytics(this)
|
|
|
|
|
analytics = firebaseAnalytics
|
2022-09-04 22:52:40 -03:00
|
|
|
|
|
|
|
|
// Set analytics per prefs
|
|
|
|
|
isAnalyticsAllowed = isAnalyticsAllowed
|
|
|
|
|
}
|
|
|
|
|
}
|