|
|
@ -5,30 +5,26 @@
|
|
|
|
package org.mozilla.fenix.settings
|
|
|
|
package org.mozilla.fenix.settings
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.text.TextUtils
|
|
|
|
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.view.View
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.RadioButton
|
|
|
|
import android.widget.RadioButton
|
|
|
|
import android.widget.TextView
|
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.core.content.res.TypedArrayUtils.getAttr
|
|
|
|
import androidx.core.content.res.TypedArrayUtils
|
|
|
|
|
|
|
|
import androidx.core.content.withStyledAttributes
|
|
|
|
import androidx.core.content.withStyledAttributes
|
|
|
|
import androidx.core.text.HtmlCompat
|
|
|
|
import androidx.core.text.HtmlCompat
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceViewHolder
|
|
|
|
import androidx.preference.PreferenceViewHolder
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ThemeManager
|
|
|
|
|
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
|
|
|
|
|
|
|
|
class RadioButtonPreference @JvmOverloads constructor(
|
|
|
|
class RadioButtonPreference @JvmOverloads constructor(
|
|
|
|
context: Context,
|
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null,
|
|
|
|
attrs: AttributeSet? = null
|
|
|
|
attributeSetId: Int = 0
|
|
|
|
) : Preference(context, attrs) {
|
|
|
|
) : Preference(context, attrs, attributeSetId) {
|
|
|
|
|
|
|
|
private val radioGroups = mutableListOf<RadioButtonPreference>()
|
|
|
|
private val radioGroups = mutableListOf<RadioButtonPreference>()
|
|
|
|
private lateinit var summaryView: TextView
|
|
|
|
private lateinit var summaryView: TextView
|
|
|
|
private lateinit var radioButton: RadioButton
|
|
|
|
private lateinit var radioButton: RadioButton
|
|
|
|
var shouldSummaryBeParsedAsHtmlContent: Boolean = true
|
|
|
|
private var shouldSummaryBeParsedAsHtmlContent: Boolean = true
|
|
|
|
private var defaultValue: Boolean = false
|
|
|
|
private var defaultValue: Boolean = false
|
|
|
|
private var clickListener: (() -> Unit)? = null
|
|
|
|
private var clickListener: (() -> Unit)? = null
|
|
|
|
|
|
|
|
|
|
|
@ -38,38 +34,19 @@ class RadioButtonPreference @JvmOverloads constructor(
|
|
|
|
context.withStyledAttributes(
|
|
|
|
context.withStyledAttributes(
|
|
|
|
attrs,
|
|
|
|
attrs,
|
|
|
|
androidx.preference.R.styleable.Preference,
|
|
|
|
androidx.preference.R.styleable.Preference,
|
|
|
|
TypedArrayUtils.getAttr(
|
|
|
|
getAttr(context, androidx.preference.R.attr.preferenceStyle, android.R.attr.preferenceStyle),
|
|
|
|
context, androidx.preference.R.attr.preferenceStyle, android.R.attr.preferenceStyle
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
if (hasValue(androidx.preference.R.styleable.Preference_defaultValue)) {
|
|
|
|
defaultValue = when {
|
|
|
|
defaultValue = getBoolean(
|
|
|
|
hasValue(androidx.preference.R.styleable.Preference_defaultValue) ->
|
|
|
|
androidx.preference.R.styleable.Preference_defaultValue,
|
|
|
|
getBoolean(androidx.preference.R.styleable.Preference_defaultValue, false)
|
|
|
|
false
|
|
|
|
hasValue(androidx.preference.R.styleable.Preference_android_defaultValue) ->
|
|
|
|
)
|
|
|
|
getBoolean(androidx.preference.R.styleable.Preference_android_defaultValue, false)
|
|
|
|
} else if (hasValue(androidx.preference.R.styleable.Preference_android_defaultValue)) {
|
|
|
|
else -> false
|
|
|
|
defaultValue = getBoolean(
|
|
|
|
|
|
|
|
androidx.preference.R.styleable.Preference_android_defaultValue,
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml
|
|
|
|
|
|
|
|
* as a result we have to apply it programmatically. More info about this issue https://github.com/mozilla-mobile/fenix/issues/1414
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
fun RadioButton.setStartCheckedIndicator() {
|
|
|
|
|
|
|
|
val attr =
|
|
|
|
|
|
|
|
ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
|
|
|
|
|
|
|
|
val buttonDrawable = ContextCompat.getDrawable(context, attr)
|
|
|
|
|
|
|
|
buttonDrawable.apply {
|
|
|
|
|
|
|
|
this?.setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setCompoundDrawables(buttonDrawable, null, null, null)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun addToRadioGroup(radioPreference: RadioButtonPreference) {
|
|
|
|
fun addToRadioGroup(radioPreference: RadioButtonPreference) {
|
|
|
|
radioGroups.add(radioPreference)
|
|
|
|
radioGroups.add(radioPreference)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -118,20 +95,21 @@ class RadioButtonPreference @JvmOverloads constructor(
|
|
|
|
private fun bindTitle(holder: PreferenceViewHolder) {
|
|
|
|
private fun bindTitle(holder: PreferenceViewHolder) {
|
|
|
|
val titleView = holder.findViewById(R.id.title) as TextView
|
|
|
|
val titleView = holder.findViewById(R.id.title) as TextView
|
|
|
|
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(title)) {
|
|
|
|
if (!title.isNullOrEmpty()) {
|
|
|
|
titleView.text = title
|
|
|
|
titleView.text = title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun bindSummaryView(holder: PreferenceViewHolder) {
|
|
|
|
private fun bindSummaryView(holder: PreferenceViewHolder) {
|
|
|
|
summaryView = holder.findViewById(R.id.widget_summary) as TextView
|
|
|
|
summaryView = holder.findViewById(R.id.widget_summary) as TextView
|
|
|
|
if (!TextUtils.isEmpty(summary)) {
|
|
|
|
|
|
|
|
if (shouldSummaryBeParsedAsHtmlContent) {
|
|
|
|
if (!summary.isNullOrEmpty()) {
|
|
|
|
summaryView.text =
|
|
|
|
summaryView.text = if (shouldSummaryBeParsedAsHtmlContent) {
|
|
|
|
HtmlCompat.fromHtml(summary.toString(), HtmlCompat.FROM_HTML_MODE_COMPACT)
|
|
|
|
HtmlCompat.fromHtml(summary.toString(), HtmlCompat.FROM_HTML_MODE_COMPACT)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
summaryView.text = summary
|
|
|
|
summary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
summaryView.visibility = View.VISIBLE
|
|
|
|
summaryView.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
summaryView.visibility = View.GONE
|
|
|
|
summaryView.visibility = View.GONE
|
|
|
|