1
0
Fork 0

Feature #2088: Async load the default search engine icon at startup (#2113)

master
Will Hawkins 2019-04-30 20:45:56 -04:00 committed by Colin Lee
parent dc3abe2698
commit 0bcff089d6
2 changed files with 55 additions and 31 deletions

View File

@ -17,6 +17,11 @@ import org.mozilla.fenix.GleanMetrics.Metrics
import org.mozilla.fenix.GleanMetrics.QuickActionSheet
import org.mozilla.fenix.GleanMetrics.SearchDefaultEngine
import org.mozilla.fenix.ext.components
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
private class EventWrapper<T : Enum<T>>(
private val recorder: ((Map<T, String>?) -> Unit),
@ -160,35 +165,47 @@ private val Event.wrapper
class GleanMetricsService(private val context: Context) : MetricsService {
private var initialized = false
/*
* We need to keep an eye on when we are done starting so that we don't
* accidentally stop ourselves before we've ever started.
*/
private lateinit var starter: Job
override fun start() {
Glean.setUploadEnabled(true)
if (initialized) return
Glean.initialize(context)
Metrics.apply {
defaultBrowser.set(Browsers.all(context).isDefaultBrowser)
defaultMozBrowser.set(MozillaProductDetector.getMozillaBrowserDefault(context) ?: "")
mozillaProducts.set(MozillaProductDetector.getInstalledMozillaProducts(context))
}
SearchDefaultEngine.apply {
val defaultEngine = context
.components
.search
.searchEngineManager
.defaultSearchEngine ?: return@apply
code.set(defaultEngine.identifier)
name.set(defaultEngine.name)
submissionUrl.set(defaultEngine.buildSearchUrl(""))
}
initialized = true
starter = CoroutineScope(Dispatchers.Default).launch {
Glean.initialize(context)
Metrics.apply {
defaultBrowser.set(Browsers.all(context).isDefaultBrowser)
defaultMozBrowser.set(MozillaProductDetector.getMozillaBrowserDefault(context) ?: "")
mozillaProducts.set(MozillaProductDetector.getInstalledMozillaProducts(context))
}
SearchDefaultEngine.apply {
val defaultEngine = context
.components
.search
.searchEngineManager
.defaultSearchEngine ?: return@apply
code.set(defaultEngine.identifier)
name.set(defaultEngine.name)
submissionUrl.set(defaultEngine.buildSearchUrl(""))
Glean.setUploadEnabled(true)
}
}
}
override fun stop() {
/*
* We cannot stop until we're done starting.
*/
runBlocking { starter.join(); }
Glean.setUploadEnabled(false)
}

View File

@ -22,8 +22,10 @@ import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
@ -103,10 +105,19 @@ class HomeFragment : Fragment(), CoroutineScope {
setupHomeMenu()
val searchIcon = requireComponents.search.searchEngineManager.getDefaultSearchEngine(
requireContext()
).let {
BitmapDrawable(resources, it.icon)
launch(Dispatchers.Default) {
val iconSize = resources.getDimension(R.dimen.preference_icon_drawable_size).toInt()
val searchIcon = requireComponents.search.searchEngineManager.getDefaultSearchEngine(
requireContext()
).let {
BitmapDrawable(resources, it.icon)
}
searchIcon.setBounds(0, 0, iconSize, iconSize)
runBlocking(Dispatchers.Main) {
view.toolbar.setCompoundDrawables(searchIcon, null, null, null)
}
}
view.menuButton.setOnClickListener {
@ -115,10 +126,6 @@ class HomeFragment : Fragment(), CoroutineScope {
orientation = BrowserMenu.Orientation.DOWN
)
}
val iconSize = resources.getDimension(R.dimen.preference_icon_drawable_size).toInt()
searchIcon.setBounds(0, 0, iconSize, iconSize)
view.toolbar.setCompoundDrawables(searchIcon, null, null, null)
val roundToInt = (toolbarPaddingDp * Resources.getSystem().displayMetrics.density).roundToInt()
view.toolbar.compoundDrawablePadding = roundToInt
view.toolbar.setOnClickListener {