diff options
author | George Lin <giolin@google.com> | 2023-10-27 18:33:44 +0000 |
---|---|---|
committer | George Lin <giolin@google.com> | 2023-10-27 19:52:05 +0000 |
commit | 4c99e2164ea970069793ce99abc9679d484bf2b4 (patch) | |
tree | 61fede30b40d91b469889b4ebec9d7209767dc74 /src/com/android | |
parent | 0768fb09137a8068b3326d24521563e93cd9230a (diff) | |
download | ThemePicker-4c99e2164ea970069793ce99abc9679d484bf2b4.tar.gz |
Use the Android RadioButton for clock size options
Major reason of the change is that we need to properly support a11y and
the best way is to use the original Android UI components.
To implement title and description without creating 2 TextViews, we use
SpannableString to give style to title and description.
Test: Manually tested that the view looks the same and the a11y works as
Test: expected.
Bug: 280937148
Flag: NONE
Change-Id: Id81c3adcc07892f0688d9169b64861b72a4a957d
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt | 80 | ||||
-rw-r--r-- | src/com/android/customization/picker/clock/ui/view/ClockSizeRadioButtonGroup.kt | 50 |
2 files changed, 66 insertions, 64 deletions
diff --git a/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt b/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt index 6e745d54..0f39c547 100644 --- a/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt +++ b/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt @@ -15,11 +15,18 @@ */ package com.android.customization.picker.clock.ui.binder +import android.content.Context import android.content.res.Configuration +import android.text.Spannable +import android.text.SpannableString +import android.text.style.TextAppearanceSpan import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout +import android.widget.RadioButton +import android.widget.RadioGroup +import android.widget.RadioGroup.OnCheckedChangeListener import android.widget.SeekBar import androidx.core.view.doOnPreDraw import androidx.core.view.isInvisible @@ -35,7 +42,6 @@ import com.android.customization.picker.clock.shared.ClockSize import com.android.customization.picker.clock.ui.adapter.ClockSettingsTabAdapter import com.android.customization.picker.clock.ui.view.ClockCarouselView import com.android.customization.picker.clock.ui.view.ClockHostView -import com.android.customization.picker.clock.ui.view.ClockSizeRadioButtonGroup import com.android.customization.picker.clock.ui.view.ClockViewFactory import com.android.customization.picker.clock.ui.viewmodel.ClockSettingsViewModel import com.android.customization.picker.color.ui.binder.ColorOptionIconBinder @@ -83,14 +89,27 @@ object ClockSettingsBinder { } ) - val sizeOptions = - view.requireViewById<ClockSizeRadioButtonGroup>(R.id.clock_size_radio_button_group) - sizeOptions.onRadioButtonClickListener = - object : ClockSizeRadioButtonGroup.OnRadioButtonClickListener { - override fun onClick(size: ClockSize) { - viewModel.setClockSize(size) - } + val onCheckedChangeListener = OnCheckedChangeListener { _, id -> + when (id) { + R.id.radio_dynamic -> viewModel.setClockSize(ClockSize.DYNAMIC) + R.id.radio_small -> viewModel.setClockSize(ClockSize.SMALL) } + } + val clockSizeRadioGroup = + view.requireViewById<RadioGroup>(R.id.clock_size_radio_button_group) + clockSizeRadioGroup.setOnCheckedChangeListener(onCheckedChangeListener) + view.requireViewById<RadioButton>(R.id.radio_dynamic).text = + getRadioText( + view.context.applicationContext, + view.resources.getString(R.string.clock_size_dynamic), + view.resources.getString(R.string.clock_size_dynamic_description) + ) + view.requireViewById<RadioButton>(R.id.radio_small).text = + getRadioText( + view.context.applicationContext, + view.resources.getString(R.string.clock_size_small), + view.resources.getString(R.string.clock_size_small_description) + ) val colorOptionContainer = view.requireViewById<View>(R.id.color_picker_container) lifecycleOwner.lifecycleScope.launch { @@ -110,11 +129,11 @@ object ClockSettingsBinder { when (tab) { ClockSettingsViewModel.Tab.COLOR -> { colorOptionContainer.isVisible = true - sizeOptions.isInvisible = true + clockSizeRadioGroup.isInvisible = true } ClockSettingsViewModel.Tab.SIZE -> { colorOptionContainer.isInvisible = true - sizeOptions.isVisible = true + clockSizeRadioGroup.isVisible = true } } } @@ -189,16 +208,28 @@ object ClockSettingsBinder { clockHostView.addView(clockView) when (size) { ClockSize.DYNAMIC -> { - sizeOptions.radioButtonDynamic.isChecked = true - sizeOptions.radioButtonSmall.isChecked = false + // When clock size data flow emits clock size signal, we want + // to update the view without triggering on checked change, + // which is supposed to be triggered by user interaction only. + clockSizeRadioGroup.setOnCheckedChangeListener(null) + clockSizeRadioGroup.check(R.id.radio_dynamic) + clockSizeRadioGroup.setOnCheckedChangeListener( + onCheckedChangeListener + ) clockHostView.doOnPreDraw { it.pivotX = it.width / 2F it.pivotY = it.height / 2F } } ClockSize.SMALL -> { - sizeOptions.radioButtonDynamic.isChecked = false - sizeOptions.radioButtonSmall.isChecked = true + // When clock size data flow emits clock size signal, we want + // to update the view without triggering on checked change, + // which is supposed to be triggered by user interaction only. + clockSizeRadioGroup.setOnCheckedChangeListener(null) + clockSizeRadioGroup.check(R.id.radio_small) + clockSizeRadioGroup.setOnCheckedChangeListener( + onCheckedChangeListener + ) clockHostView.doOnPreDraw { it.pivotX = ClockCarouselView.getCenteredHostViewPivotX(it) it.pivotY = 0F @@ -238,4 +269,25 @@ object ClockSettingsBinder { } ) } + + private fun getRadioText( + context: Context, + title: String, + description: String + ): SpannableString { + val text = SpannableString(title + "\n" + description) + text.setSpan( + TextAppearanceSpan(context, R.style.SectionTitleTextStyle), + 0, + title.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + text.setSpan( + TextAppearanceSpan(context, R.style.SectionSubtitleTextStyle), + title.length + 1, + title.length + 1 + description.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + return text + } } diff --git a/src/com/android/customization/picker/clock/ui/view/ClockSizeRadioButtonGroup.kt b/src/com/android/customization/picker/clock/ui/view/ClockSizeRadioButtonGroup.kt deleted file mode 100644 index 909491a3..00000000 --- a/src/com/android/customization/picker/clock/ui/view/ClockSizeRadioButtonGroup.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.customization.picker.clock.ui.view - -import android.content.Context -import android.util.AttributeSet -import android.view.LayoutInflater -import android.view.View -import android.widget.FrameLayout -import android.widget.RadioButton -import com.android.customization.picker.clock.shared.ClockSize -import com.android.wallpaper.R - -/** The radio button group to pick the clock size. */ -class ClockSizeRadioButtonGroup( - context: Context, - attrs: AttributeSet?, -) : FrameLayout(context, attrs) { - - interface OnRadioButtonClickListener { - fun onClick(size: ClockSize) - } - - val radioButtonDynamic: RadioButton - val radioButtonSmall: RadioButton - var onRadioButtonClickListener: OnRadioButtonClickListener? = null - - init { - LayoutInflater.from(context).inflate(R.layout.clock_size_radio_button_group, this, true) - radioButtonDynamic = requireViewById(R.id.radio_button_dynamic) - val buttonDynamic = requireViewById<View>(R.id.button_container_dynamic) - buttonDynamic.setOnClickListener { onRadioButtonClickListener?.onClick(ClockSize.DYNAMIC) } - radioButtonSmall = requireViewById(R.id.radio_button_large) - val buttonLarge = requireViewById<View>(R.id.button_container_small) - buttonLarge.setOnClickListener { onRadioButtonClickListener?.onClick(ClockSize.SMALL) } - } -} |