For #19804: Changed `var isDefaultBrowser` to a function

The change to the function makes it so when the Settings.kt class is initialized, the isDefaultBrowser, which calls the
BrowserCache, won't get called right away. `isDefaultBrowser()` is known to take quite a while on start up on the G5+ (approx
30-40ms).
upstream-sync
Marc Leclair 3 years ago committed by mergify[bot]
parent 79fcbcba83
commit b8bbdb45bc

@ -314,14 +314,11 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
// Launch this on a background thread so as not to affect startup performance
lifecycleScope.launch(IO) {
if (
settings().isDefaultBrowser() &&
settings().wasDefaultBrowserOnLastResume != settings().isDefaultBrowser()
settings().checkDefaultBrowserAndSet()
) {
metrics.track(Event.ChangedToDefaultBrowser)
}
settings().wasDefaultBrowserOnLastResume = settings().isDefaultBrowser()
DefaultBrowserNotificationWorker.setDefaultBrowserNotificationIfNeeded(applicationContext)
}
}

@ -471,15 +471,21 @@ class Settings(private val appContext: Context) : PreferencesHolder {
)
/**
* Caches the last known "is default browser" state when the app was paused.
* For an up to do date state use `isDefaultBrowser` instead.
* Declared as a function for performance purposes. When this class is first initialized, the
* isDefaultBrowser function takes approx. 30-40ms.
*/
var wasDefaultBrowserOnLastResume by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_default_browser),
default = isDefaultBrowser()
)
fun checkDefaultBrowserAndSet(): Boolean {
val prefKey = appContext.getPreferenceKey(R.string.pref_key_default_browser)
val isDefaultBrowser = isDefaultBrowser()
this.preferences.edit().putBoolean(prefKey, isDefaultBrowser).apply()
return this.preferences.getBoolean(prefKey, isDefaultBrowser) != isDefaultBrowser
}
fun isDefaultBrowser(): Boolean {
/**
* This function is "blocking" since calling this can take approx. 30-40ms (timing taken on a
* G5+).
*/
private fun isDefaultBrowserBlocking(): Boolean {
val browsers = BrowsersCache.all(appContext)
return browsers.isDefaultBrowser
}

Loading…
Cancel
Save