[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/877 - Add Search Engine Settings
parent
81afb77e04
commit
43e2c306fa
@ -0,0 +1,50 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
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/. */
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.RadioGroup
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
class RadioSearchEngineListPreference : SearchEngineListPreference,
|
||||
RadioGroup.OnCheckedChangeListener {
|
||||
|
||||
override val itemResId: Int
|
||||
get() = R.layout.search_engine_radio_button
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
|
||||
context,
|
||||
attrs,
|
||||
defStyleAttr
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
|
||||
super.onBindViewHolder(holder)
|
||||
searchEngineGroup!!.setOnCheckedChangeListener(this)
|
||||
}
|
||||
|
||||
override fun updateDefaultItem(defaultButton: CompoundButton) {
|
||||
defaultButton.isChecked = true
|
||||
}
|
||||
|
||||
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
|
||||
/* onCheckedChanged is called intermittently before the search engine table is full, so we
|
||||
must check these conditions to prevent crashes and inconsistent states. */
|
||||
if (group.childCount != searchEngines.count() || group.getChildAt(checkedId) == null ||
|
||||
!group.getChildAt(checkedId).isPressed
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
val newDefaultEngine = searchEngines[checkedId]
|
||||
Settings.getInstance(group.context).setDefaultSearchEngineByName(newDefaultEngine.name)
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* 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/. */
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
class SearchEngineFragment : PreferenceFragmentCompat() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
(activity as AppCompatActivity).title = getString(R.string.preferences_search_engine)
|
||||
(activity as AppCompatActivity).supportActionBar?.show()
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.search_engine_preferences, rootKey)
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
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/. */
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.widget.CompoundButton
|
||||
import android.widget.RadioGroup
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceViewHolder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import mozilla.components.browser.search.SearchEngine
|
||||
import org.mozilla.fenix.DefaultThemeManager
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
abstract class SearchEngineListPreference : Preference, CoroutineScope {
|
||||
private val job = Job()
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = job + Dispatchers.Main
|
||||
protected var searchEngines: List<SearchEngine> = emptyList()
|
||||
protected var searchEngineGroup: RadioGroup? = null
|
||||
|
||||
protected abstract val itemResId: Int
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
|
||||
layoutResource = R.layout.preference_search_engine_chooser
|
||||
}
|
||||
|
||||
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
|
||||
context,
|
||||
attrs,
|
||||
defStyleAttr
|
||||
) {
|
||||
layoutResource = R.layout.preference_search_engine_chooser
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
|
||||
super.onBindViewHolder(holder)
|
||||
searchEngineGroup = holder!!.itemView.findViewById(R.id.search_engine_group)
|
||||
val context = searchEngineGroup!!.context
|
||||
|
||||
searchEngines = context.components.search.searchEngineManager.getSearchEngines(context)
|
||||
.sortedBy { it.name }
|
||||
|
||||
refreshSearchEngineViews(context)
|
||||
}
|
||||
|
||||
override fun onDetached() {
|
||||
job.cancel()
|
||||
super.onDetached()
|
||||
}
|
||||
|
||||
protected abstract fun updateDefaultItem(defaultButton: CompoundButton)
|
||||
|
||||
private fun refreshSearchEngineViews(context: Context) {
|
||||
if (searchEngineGroup == null) {
|
||||
// We want to refresh the search engine list of this preference in onResume,
|
||||
// but the first time this preference is created onResume is called before onCreateView
|
||||
// so searchEngineGroup is not set yet.
|
||||
return
|
||||
}
|
||||
|
||||
val defaultSearchEngine =
|
||||
context.components.search.searchEngineManager.getDefaultSearchEngine(
|
||||
context,
|
||||
Settings.getInstance(context).defaultSearchEngineName
|
||||
).identifier
|
||||
|
||||
searchEngineGroup!!.removeAllViews()
|
||||
|
||||
val layoutInflater = LayoutInflater.from(context)
|
||||
val layoutParams = ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
|
||||
for (i in searchEngines.indices) {
|
||||
val engine = searchEngines[i]
|
||||
val engineId = engine.identifier
|
||||
val engineItem = makeButtonFromSearchEngine(engine, layoutInflater, context.resources)
|
||||
engineItem.id = i
|
||||
engineItem.tag = engineId
|
||||
if (engineId == defaultSearchEngine) {
|
||||
updateDefaultItem(engineItem)
|
||||
}
|
||||
searchEngineGroup!!.addView(engineItem, layoutParams)
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeButtonFromSearchEngine(
|
||||
engine: SearchEngine,
|
||||
layoutInflater: LayoutInflater,
|
||||
res: Resources
|
||||
): CompoundButton {
|
||||
val buttonItem = layoutInflater.inflate(itemResId, null) as CompoundButton
|
||||
buttonItem.text = engine.name
|
||||
val iconSize = res.getDimension(R.dimen.preference_icon_drawable_size).toInt()
|
||||
val engineIcon = BitmapDrawable(res, engine.icon)
|
||||
engineIcon.setBounds(0, 0, iconSize, iconSize)
|
||||
val attr =
|
||||
DefaultThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
|
||||
val buttonDrawable = ContextCompat.getDrawable(context, attr)
|
||||
buttonDrawable.apply {
|
||||
this?.setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
|
||||
}
|
||||
buttonItem.setCompoundDrawables(engineIcon, null, buttonDrawable, null)
|
||||
return buttonItem
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.mozilla.fenix.utils
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
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/. */
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.preference.PreferenceManager
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.getPreferenceKey
|
||||
|
||||
/**
|
||||
* A simple wrapper for SharedPreferences that makes reading preference a little bit easier.
|
||||
*/
|
||||
class Settings private constructor(context: Context) {
|
||||
|
||||
companion object {
|
||||
var instance: Settings? = null
|
||||
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun getInstance(context: Context): Settings {
|
||||
if (instance == null) {
|
||||
instance = Settings(context.applicationContext)
|
||||
}
|
||||
return instance ?: throw AssertionError("Instance cleared")
|
||||
}
|
||||
}
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
|
||||
private val preferences: SharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
|
||||
val defaultSearchEngineName: String
|
||||
get() = preferences.getString(
|
||||
appContext.getPreferenceKey(R.string.pref_key_search_engine),
|
||||
""
|
||||
) ?: ""
|
||||
|
||||
fun setDefaultSearchEngineByName(name: String) {
|
||||
preferences.edit()
|
||||
.putString(appContext.getPreferenceKey(R.string.pref_key_search_engine), name)
|
||||
.apply()
|
||||
}
|
||||
|
||||
fun showSearchSuggestions(): Boolean = preferences.getBoolean(
|
||||
appContext.getPreferenceKey(R.string.pref_key_show_search_suggestions),
|
||||
true
|
||||
)
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- 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/. -->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioGroup android:id="@+id/search_engine_group"
|
||||
android:paddingTop="8dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- 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/. -->
|
||||
|
||||
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/radio_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/search_engine_radio_button_height"
|
||||
android:layout_gravity="start"
|
||||
android:background="?android:selectableItemBackground"
|
||||
android:button="@null"
|
||||
android:drawableStart="@drawable/ic_close"
|
||||
android:drawableEnd="?android:attr/listChoiceIndicatorSingle"
|
||||
android:drawablePadding="16dp"
|
||||
android:paddingStart="@dimen/radio_button_padding_horizontal"
|
||||
android:paddingTop="@dimen/radio_button_padding_vertical"
|
||||
android:paddingEnd="@dimen/radio_button_padding_horizontal"
|
||||
android:paddingBottom="@dimen/radio_button_padding_vertical"
|
||||
android:textAlignment="textStart"
|
||||
android:textAppearance="?android:attr/textAppearanceListItem" />
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- 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/. -->
|
||||
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<org.mozilla.fenix.settings.RadioSearchEngineListPreference />
|
||||
<SwitchPreference
|
||||
android:key="@string/pref_key_show_search_suggestions"
|
||||
android:title="@string/preferences_show_search_suggestions"
|
||||
app:iconSpaceReserved="false" />
|
||||
</PreferenceScreen>
|
Loading…
Reference in New Issue