2019-01-24 22:07:52 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-01-23 23:48:15 +01:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2019-01-24 22:07:52 +01:00
|
|
|
|
2019-01-23 22:39:53 +01:00
|
|
|
package org.mozilla.fenix.components
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-07-02 21:58:28 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2019-01-23 22:39:53 +01:00
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import mozilla.components.browser.search.SearchEngineManager
|
2019-07-03 16:37:35 +02:00
|
|
|
import mozilla.components.browser.search.provider.AssetsSearchEngineProvider
|
|
|
|
import mozilla.components.browser.search.provider.localization.SearchLocalizationProvider
|
2019-05-07 23:36:37 +02:00
|
|
|
import org.mozilla.fenix.test.Mockable
|
2019-03-12 18:17:15 +01:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-07-03 16:37:35 +02:00
|
|
|
import java.util.Locale
|
|
|
|
|
|
|
|
// Create our own SearchLocalizationProivder that sets the region based on the country
|
|
|
|
// This is a temporary fix until we have a localization provider that can look up the users
|
|
|
|
// issue
|
|
|
|
// https://github.com/mozilla-mobile/fenix/issues/3869
|
|
|
|
// https://github.com/mozilla-mobile/android-components/issues/3637
|
|
|
|
class FenixLocaleSearchLocalizationProvider : SearchLocalizationProvider() {
|
|
|
|
override val language: String
|
|
|
|
get() = Locale.getDefault().language
|
|
|
|
|
|
|
|
override val country: String
|
|
|
|
get() = Locale.getDefault().country
|
|
|
|
|
|
|
|
override val region: String
|
|
|
|
get() = country
|
|
|
|
}
|
2019-01-23 22:39:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component group for all search engine integration related functionality.
|
|
|
|
*/
|
2019-05-07 23:36:37 +02:00
|
|
|
@Mockable
|
2019-01-23 22:39:53 +01:00
|
|
|
class Search(private val context: Context) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component provides access to a centralized registry of search engines.
|
|
|
|
*/
|
|
|
|
val searchEngineManager by lazy {
|
2019-07-03 16:37:35 +02:00
|
|
|
SearchEngineManager(coroutineContext = IO, providers = listOf(
|
|
|
|
AssetsSearchEngineProvider(FenixLocaleSearchLocalizationProvider())
|
|
|
|
)).apply {
|
2019-03-06 20:39:38 +01:00
|
|
|
registerForLocaleUpdates(context)
|
2019-01-23 22:39:53 +01:00
|
|
|
GlobalScope.launch {
|
2019-06-05 01:30:26 +02:00
|
|
|
loadAsync(context).await()
|
2019-01-23 22:39:53 +01:00
|
|
|
}
|
2019-03-12 18:17:15 +01:00
|
|
|
defaultSearchEngine = getDefaultSearchEngine(
|
|
|
|
context,
|
|
|
|
Settings.getInstance(context).defaultSearchEngineName
|
|
|
|
)
|
2019-01-23 22:39:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|