[fenix] Switch RadioButtonPreference to ConstraintLayout

Also ensure that RTL works properly.
pull/600/head
Tiger Oakes 5 years ago committed by Emily Kager
parent ba3ef0deda
commit 95a7afe4e2

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class AccountAuthErrorPreference @JvmOverloads constructor( class AccountAuthErrorPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
attributeSetId: Int = 0 attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) { ) : Preference(context, attrs, attributeSetId) {
var email: String? = null var email: String? = null

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class AccountPreference @JvmOverloads constructor( class AccountPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
attributeSetId: Int = 0 attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) { ) : Preference(context, attrs, attributeSetId) {
var displayName: String? = null var displayName: String? = null
var email: String? = null var email: String? = null

@ -15,7 +15,7 @@ import org.mozilla.fenix.R
class DefaultBrowserPreference @JvmOverloads constructor( class DefaultBrowserPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
attributeSetId: Int = 0 attributeSetId: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, attributeSetId) { ) : Preference(context, attrs, attributeSetId) {
private var switchView: Switch? = null private var switchView: Switch? = null

@ -98,17 +98,17 @@ fun PhoneFeature.getPreferenceKey(context: Context): String {
} }
} }
/* 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 * 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() { fun RadioButton.setStartCheckedIndicator() {
val attr = val attr = ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
ThemeManager.resolveAttribute(android.R.attr.listChoiceIndicatorSingle, context)
val buttonDrawable = ContextCompat.getDrawable(context, attr) val buttonDrawable = ContextCompat.getDrawable(context, attr)
buttonDrawable.apply { buttonDrawable?.apply {
this?.setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight) setBounds(0, 0, this.intrinsicWidth, this.intrinsicHeight)
} }
this.setCompoundDrawables(buttonDrawable, null, null, null) setCompoundDrawablesRelative(buttonDrawable, null, null, null)
} }
fun initBlockedByAndroidView(phoneFeature: PhoneFeature, blockedByAndroidView: View) { fun initBlockedByAndroidView(phoneFeature: PhoneFeature, blockedByAndroidView: View) {

@ -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

@ -15,7 +15,7 @@ import org.mozilla.fenix.utils.Settings
class RadioSearchEngineListPreference @JvmOverloads constructor( class RadioSearchEngineListPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
defStyleAttr: Int = 0 defStyleAttr: Int = android.R.attr.preferenceStyle
) : SearchEngineListPreference(context, attrs, defStyleAttr) { ) : SearchEngineListPreference(context, attrs, defStyleAttr) {
override val itemResId: Int override val itemResId: Int
get() = R.layout.search_engine_radio_button get() = R.layout.search_engine_radio_button

@ -31,7 +31,7 @@ import kotlin.coroutines.CoroutineContext
abstract class SearchEngineListPreference @JvmOverloads constructor( abstract class SearchEngineListPreference @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
defStyleAttr: Int = 0 defStyleAttr: Int = android.R.attr.preferenceStyle
) : Preference(context, attrs, defStyleAttr), CompoundButton.OnCheckedChangeListener, CoroutineScope { ) : Preference(context, attrs, defStyleAttr), CompoundButton.OnCheckedChangeListener, CoroutineScope {
private val job = Job() private val job = Job()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext

@ -3,9 +3,9 @@
- License, v. 2.0. If a copy of the MPL was not distributed with this - 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/. --> - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout" android:id="@+id/relativeLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -16,45 +16,41 @@
android:paddingBottom="@dimen/radio_button_preference_vertical" android:paddingBottom="@dimen/radio_button_preference_vertical"
android:baselineAligned="false"> android:baselineAligned="false">
<LinearLayout android:layout_width="wrap_content" <RadioButton
android:layout_height="wrap_content" android:id="@+id/radio_button"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_button"
android:layout_width="match_parent"
android:layout_height="@dimen/radio_button_preference_height"
android:background="@android:color/transparent"
android:layout_gravity="start"
android:button="@null"
android:clickable="false"
android:focusable="false"
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="@dimen/radio_button_preference_height"
android:background="@android:color/transparent"
android:layout_gravity="start"
android:button="@null"
android:clickable="false"
android:focusable="false"
tools:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="@dimen/radio_button_preference_drawable_padding"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:gravity="center|start"
android:layout_height="@dimen/radio_button_preference_height"
tools:text="Use recommended settings"
android:textAppearance="?android:attr/textAppearanceListItem"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/radio_button"/>
<TextView
android:id="@+id/widget_summary"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
<TextView tools:text="@string/preference_recommended_settings_summary"
android:id="@+id/title" app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent" app:layout_constraintEnd_toEndOf="parent"
android:gravity="center|start" app:layout_constraintTop_toBottomOf="@+id/radio_button"
android:layout_height="@dimen/radio_button_preference_height" app:layout_constraintStart_toStartOf="@+id/title" />
tools:text="Use recommended settings"
android:textAppearance="?android:attr/textAppearanceListItem"/> </androidx.constraintlayout.widget.ConstraintLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
tools:text="@string/preference_recommended_settings_summary"/>
</LinearLayout>
</LinearLayout>

Loading…
Cancel
Save