|
18 | 18 |
|
19 | 19 | import io.material.catalog.R; |
20 | 20 |
|
| 21 | +import android.content.Context; |
21 | 22 | import android.os.Bundle; |
22 | 23 | import android.view.LayoutInflater; |
23 | 24 | import android.view.View; |
24 | 25 | import android.view.ViewGroup; |
25 | | -import android.widget.TextView; |
26 | | -import androidx.annotation.ColorInt; |
27 | | -import androidx.annotation.ColorRes; |
28 | | -import androidx.annotation.LayoutRes; |
| 26 | +import android.widget.LinearLayout; |
| 27 | +import androidx.annotation.IdRes; |
29 | 28 | import androidx.annotation.Nullable; |
30 | | -import androidx.annotation.StringRes; |
31 | | -import com.google.android.material.button.MaterialButton; |
32 | | -import com.google.android.material.color.MaterialColors; |
| 29 | +import com.google.android.material.color.DynamicColors; |
| 30 | +import com.google.android.material.color.HarmonizedColors; |
| 31 | +import com.google.android.material.color.HarmonizedColorsOptions; |
33 | 32 | import com.google.android.material.switchmaterial.SwitchMaterial; |
34 | | -import com.google.android.material.textfield.TextInputLayout; |
35 | 33 | import io.material.catalog.feature.DemoFragment; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
36 | 36 |
|
37 | 37 | /** A fragment that displays the Color Harmonization demo for the Catalog app. */ |
38 | 38 | public class ColorHarmonizationDemoFragment extends DemoFragment { |
39 | 39 |
|
40 | | - private static final int GREEN_RESOURCE_ID = R.color.green40; |
41 | | - private static final int RED_RESOURCE_ID = R.color.red40; |
42 | | - private static final int BLUE_RESOURCE_ID = R.color.blue90; |
| 40 | + private static final HarmonizableButtonData[] HARMONIZABLE_BUTTON_DATA_LIST = |
| 41 | + new HarmonizableButtonData[] { |
| 42 | + new HarmonizableButtonData( |
| 43 | + R.id.red_button_dark, R.color.error_reference, /* isLightButton= */ false), |
| 44 | + new HarmonizableButtonData( |
| 45 | + R.id.red_button_light, R.color.error_reference, /* isLightButton= */ true), |
| 46 | + new HarmonizableButtonData( |
| 47 | + R.id.yellow_button_dark, R.color.yellow_reference, /* isLightButton= */ false), |
| 48 | + new HarmonizableButtonData( |
| 49 | + R.id.yellow_button_light, R.color.yellow_reference, /* isLightButton= */ true), |
| 50 | + new HarmonizableButtonData( |
| 51 | + R.id.green_button_dark, R.color.green_reference, /* isLightButton= */ false), |
| 52 | + new HarmonizableButtonData( |
| 53 | + R.id.green_button_light, R.color.green_reference, /* isLightButton= */ true), |
| 54 | + new HarmonizableButtonData( |
| 55 | + R.id.blue_button_dark, R.color.blue_reference, /* isLightButton= */ false), |
| 56 | + new HarmonizableButtonData( |
| 57 | + R.id.blue_button_light, R.color.blue_reference, /* isLightButton= */ true), |
| 58 | + }; |
| 59 | + // TODO(b/231143697): Refactor this class to a DemoActivity and showcase harmonization using |
| 60 | + // error color attributes. |
| 61 | + private static final ColorHarmonizationGridRowData[] HARMONIZATION_GRID_ROW_DATA_LIST = |
| 62 | + new ColorHarmonizationGridRowData[] { |
| 63 | + new ColorHarmonizationGridRowData( |
| 64 | + R.id.cat_colors_error, |
| 65 | + R.id.cat_colors_harmonized_error, |
| 66 | + R.color.error_reference, |
| 67 | + R.array.cat_error_strings), |
| 68 | + new ColorHarmonizationGridRowData( |
| 69 | + R.id.cat_colors_yellow, |
| 70 | + R.id.cat_colors_harmonized_yellow, |
| 71 | + R.color.yellow_reference, |
| 72 | + R.array.cat_yellow_strings), |
| 73 | + new ColorHarmonizationGridRowData( |
| 74 | + R.id.cat_colors_green, |
| 75 | + R.id.cat_colors_harmonized_green, |
| 76 | + R.color.green_reference, |
| 77 | + R.array.cat_green_strings), |
| 78 | + new ColorHarmonizationGridRowData( |
| 79 | + R.id.cat_colors_blue, |
| 80 | + R.id.cat_colors_harmonized_blue, |
| 81 | + R.color.blue_reference, |
| 82 | + R.array.cat_blue_strings) |
| 83 | + }; |
| 84 | + |
| 85 | + private Context dynamicColorsContext; |
| 86 | + private Context harmonizedContext; |
| 87 | + private View demoView; |
| 88 | + |
| 89 | + private final List<HarmonizableButton> harmonizableButtonList = new ArrayList<>(); |
43 | 90 |
|
44 | 91 | @Nullable |
45 | 92 | @Override |
46 | 93 | public View onCreateDemoView( |
47 | 94 | @Nullable LayoutInflater layoutInflater, |
48 | 95 | @Nullable ViewGroup viewGroup, |
49 | 96 | @Nullable Bundle bundle) { |
50 | | - View view = layoutInflater.inflate(getColorsContent(), viewGroup, false /* attachToRoot */); |
51 | | - MaterialButton elevatedButton = view.findViewById(R.id.material_button); |
52 | | - MaterialButton unelevatedButton = view.findViewById(R.id.material_unelevated_button); |
53 | | - TextInputLayout textInputLayout = view.findViewById(R.id.material_text_input_layout); |
54 | | - TextView buttonColorHexValueText = view.findViewById(R.id.material_button_color_hex_value); |
55 | | - TextView unelevatedButtonColorHexValueText = |
56 | | - view.findViewById(R.id.material_unelevated_button_color_hex_value); |
57 | | - TextView textInputColorHexValueText = |
58 | | - view.findViewById(R.id.material_text_input_color_hex_value); |
59 | | - |
60 | | - elevatedButton.setBackgroundColor(getResources().getColor(GREEN_RESOURCE_ID)); |
61 | | - unelevatedButton.setBackgroundColor(getResources().getColor(RED_RESOURCE_ID)); |
62 | | - textInputLayout.setBoxBackgroundColor(getResources().getColor(BLUE_RESOURCE_ID)); |
63 | | - |
64 | | - SwitchMaterial enabledSwitch = view.findViewById(R.id.cat_color_enabled_switch); |
65 | | - enabledSwitch.setOnCheckedChangeListener( |
66 | | - (buttonView, isChecked) -> { |
67 | | - buttonColorHexValueText.setVisibility(View.VISIBLE); |
68 | | - unelevatedButtonColorHexValueText.setVisibility(View.VISIBLE); |
69 | | - textInputColorHexValueText.setVisibility(View.VISIBLE); |
| 97 | + demoView = |
| 98 | + layoutInflater.inflate( |
| 99 | + R.layout.cat_colors_harmonization_fragment, viewGroup, false /* attachToRoot */); |
70 | 100 |
|
71 | | - int maybeHarmonizedGreen = maybeHarmonizeWithPrimary(GREEN_RESOURCE_ID, isChecked); |
72 | | - int maybeHarmonizedRed = maybeHarmonizeWithPrimary(RED_RESOURCE_ID, isChecked); |
73 | | - int maybeHarmonizedBlue = maybeHarmonizeWithPrimary(BLUE_RESOURCE_ID, isChecked); |
74 | | - |
75 | | - elevatedButton.setBackgroundColor(maybeHarmonizedGreen); |
76 | | - unelevatedButton.setBackgroundColor(maybeHarmonizedRed); |
77 | | - textInputLayout.setBoxBackgroundColor(maybeHarmonizedBlue); |
78 | | - |
79 | | - // The %06X gives us zero-padded hex (always 6 chars long). |
80 | | - buttonColorHexValueText.setText( |
81 | | - getColorHexValueText(R.string.cat_color_hex_value_text, maybeHarmonizedGreen)); |
82 | | - unelevatedButtonColorHexValueText.setText( |
83 | | - getColorHexValueText(R.string.cat_color_hex_value_text, maybeHarmonizedRed)); |
84 | | - textInputColorHexValueText.setText( |
85 | | - getColorHexValueText(R.string.cat_color_hex_value_text, maybeHarmonizedBlue)); |
86 | | - }); |
87 | | - |
88 | | - return view; |
89 | | - } |
| 101 | + dynamicColorsContext = DynamicColors.wrapContextIfAvailable(requireContext()); |
| 102 | + HarmonizedColorsOptions options = |
| 103 | + new HarmonizedColorsOptions.Builder() |
| 104 | + .setColorResourceIds( |
| 105 | + new int[] { |
| 106 | + R.color.error_reference, |
| 107 | + R.color.yellow_reference, |
| 108 | + R.color.blue_reference, |
| 109 | + R.color.green_reference, |
| 110 | + }) |
| 111 | + .build(); |
| 112 | + harmonizedContext = HarmonizedColors.wrapContextIfAvailable(dynamicColorsContext, options); |
90 | 113 |
|
91 | | - private int maybeHarmonizeWithPrimary(@ColorRes int colorResId, boolean harmonize) { |
92 | | - return harmonize |
93 | | - ? MaterialColors.harmonizeWithPrimary(getContext(), getResources().getColor(colorResId)) |
94 | | - : getResources().getColor(colorResId); |
| 114 | + for (ColorHarmonizationGridRowData colorHarmonizationGridRowData : |
| 115 | + HARMONIZATION_GRID_ROW_DATA_LIST) { |
| 116 | + createColorGridAndPopulateLayout( |
| 117 | + dynamicColorsContext, |
| 118 | + colorHarmonizationGridRowData, |
| 119 | + colorHarmonizationGridRowData.getLeftLayoutId()); |
| 120 | + createColorGridAndPopulateLayout( |
| 121 | + harmonizedContext, |
| 122 | + colorHarmonizationGridRowData, |
| 123 | + colorHarmonizationGridRowData.getRightLayoutId()); |
| 124 | + } |
| 125 | + // Setup buttons text color based on current theme. |
| 126 | + for (HarmonizableButtonData harmonizableButtonData : HARMONIZABLE_BUTTON_DATA_LIST) { |
| 127 | + harmonizableButtonList.add(HarmonizableButton.create(demoView, harmonizableButtonData)); |
| 128 | + } |
| 129 | + updateButtons(/* harmonize= */ false); |
| 130 | + SwitchMaterial enabledSwitch = demoView.findViewById(R.id.cat_color_enabled_switch); |
| 131 | + enabledSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> updateButtons(isChecked)); |
| 132 | + return demoView; |
95 | 133 | } |
96 | 134 |
|
97 | | - private CharSequence getColorHexValueText(@StringRes int stringResId, @ColorInt int color) { |
98 | | - return getResources().getString(stringResId, String.format("#%06X", (0xFFFFFF & color))); |
| 135 | + private void createColorGridAndPopulateLayout( |
| 136 | + Context context, |
| 137 | + ColorHarmonizationGridRowData colorHarmonizationGridRowData, |
| 138 | + @IdRes int layoutId) { |
| 139 | + ColorGrid colorGrid = |
| 140 | + ColorGrid.createFromColorGridData( |
| 141 | + ColorGridData.createFromColorResId( |
| 142 | + context, |
| 143 | + colorHarmonizationGridRowData.getColorResId(), |
| 144 | + colorHarmonizationGridRowData.getColorNameIds())); |
| 145 | + LinearLayout layout = demoView.findViewById(layoutId); |
| 146 | + layout.addView(colorGrid.renderView(context, layout)); |
99 | 147 | } |
100 | 148 |
|
101 | | - @LayoutRes |
102 | | - protected int getColorsContent() { |
103 | | - return R.layout.cat_colors_harmonization_fragment; |
| 149 | + private void updateButtons(boolean harmonize) { |
| 150 | + for (HarmonizableButton button : harmonizableButtonList) { |
| 151 | + button.updateColors(harmonize); |
| 152 | + } |
104 | 153 | } |
105 | 154 | } |
0 commit comments