diff options
author | Sam Blitzstein <sblitz@google.com> | 2013-04-17 09:23:24 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2013-04-17 09:23:24 -0700 |
commit | 223a2d8a7de09feeea96f2a88da73d8e8b95a18b (patch) | |
tree | 44318a62e882622ff7dff50f87c720506d8cc8f3 | |
parent | 15ccbdd8ef27faf70e5ca0658dc025c62357a1bd (diff) | |
parent | 3f672cabe031e42b31eea2d5fd187bc4ad607f77 (diff) | |
download | android_frameworks_opt_colorpicker-223a2d8a7de09feeea96f2a88da73d8e8b95a18b.tar.gz android_frameworks_opt_colorpicker-223a2d8a7de09feeea96f2a88da73d8e8b95a18b.tar.bz2 android_frameworks_opt_colorpicker-223a2d8a7de09feeea96f2a88da73d8e8b95a18b.zip |
am 3f672cab: Making swatches accessible.
* commit '3f672cabe031e42b31eea2d5fd187bc4ad607f77':
Making swatches accessible.
-rw-r--r-- | res/values/strings.xml | 6 | ||||
-rw-r--r-- | src/com/android/colorpicker/ColorPickerPalette.java | 55 | ||||
-rw-r--r-- | src/com/android/colorpicker/ColorPickerSwatch.java | 1 |
3 files changed, 53 insertions, 9 deletions
diff --git a/res/values/strings.xml b/res/values/strings.xml index ed53ecb..a9bf37a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -13,7 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> -<resources> +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- Default title for color picker dialog [CHAR LIMIT=30] --> <string name="color_picker_default_title">Select a Color</string> + <!-- Content description for a color square. --> + <string name="color_swatch_description">Color <xliff:g id="color_index" example="14">%1$d</xliff:g></string> + <!-- Content description for a selected color square. --> + <string name="color_swatch_description_selected">Color <xliff:g id="color_index" example="14">%1$d</xliff:g> selected</string> </resources>
\ No newline at end of file diff --git a/src/com/android/colorpicker/ColorPickerPalette.java b/src/com/android/colorpicker/ColorPickerPalette.java index 06621b2..11b3dcd 100644 --- a/src/com/android/colorpicker/ColorPickerPalette.java +++ b/src/com/android/colorpicker/ColorPickerPalette.java @@ -17,6 +17,7 @@ package com.android.colorpicker; import android.content.Context; +import android.content.res.Resources; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; @@ -34,6 +35,9 @@ public class ColorPickerPalette extends TableLayout { public OnColorSelectedListener mOnColorSelectedListener; + private String mDescription; + private String mDescriptionSelected; + private int mSwatchLength; private int mMarginSize; private int mNumColumns; @@ -52,14 +56,18 @@ public class ColorPickerPalette extends TableLayout { */ public void init(int size, int columns, OnColorSelectedListener listener) { mNumColumns = columns; + Resources res = getResources(); if (size == ColorPickerDialog.SIZE_LARGE) { - mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_large); - mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_large); + mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_large); + mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_large); } else { - mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_small); - mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_small); + mSwatchLength = res.getDimensionPixelSize(R.dimen.color_swatch_small); + mMarginSize = res.getDimensionPixelSize(R.dimen.color_swatch_margins_small); } mOnColorSelectedListener = listener; + + mDescription = res.getString(R.string.color_swatch_description); + mDescriptionSelected = res.getString(R.string.color_swatch_description_selected); } private TableRow createTableRow() { @@ -74,19 +82,25 @@ public class ColorPickerPalette extends TableLayout { * Adds swatches to table in a serpentine format. */ public void drawPalette(int[] colors, int selectedColor) { - if (colors == null) { return; } this.removeAllViews(); + int tableElements = 0; int rowElements = 0; int rowNumber = 0; // Fills the table with swatches based on the array of colors. TableRow row = createTableRow(); - for (int color : colors) { - addSwatchToRow(row, createColorSwatch(color, selectedColor), rowNumber); + for (int color : colors) { + tableElements++; + + View colorSwatch = createColorSwatch(color, selectedColor); + setSwatchDescription(rowNumber, tableElements, rowElements, color == selectedColor, + colorSwatch); + addSwatchToRow(row, colorSwatch, rowNumber); + rowElements++; if (rowElements == mNumColumns) { addView(row); @@ -119,6 +133,33 @@ public class ColorPickerPalette extends TableLayout { } /** + * Add a content description to the specified swatch view. Because the colors get added in a + * snaking form, every other row will need to compensate for the fact that the colors are added + * in an opposite direction from their left->right/top->bottom order, which is how the system + * will arrange them for accessibility purposes. + */ + private void setSwatchDescription(int rowNumber, int index, int rowElements, boolean selected, + View swatch) { + int accessibilityIndex; + if (rowNumber % 2 == 0) { + // We're in a regular-ordered row + accessibilityIndex = index; + } else { + // We're in a backwards-ordered row. + int rowMax = ((rowNumber + 1) * mNumColumns); + accessibilityIndex = rowMax - rowElements; + } + + String description; + if (selected) { + description = String.format(mDescriptionSelected, accessibilityIndex); + } else { + description = String.format(mDescription, accessibilityIndex); + } + swatch.setContentDescription(description); + } + + /** * Creates a blank space to fill the row. */ private ImageView createBlankSpace() { diff --git a/src/com/android/colorpicker/ColorPickerSwatch.java b/src/com/android/colorpicker/ColorPickerSwatch.java index 250588c..890436b 100644 --- a/src/com/android/colorpicker/ColorPickerSwatch.java +++ b/src/com/android/colorpicker/ColorPickerSwatch.java @@ -30,7 +30,6 @@ import android.widget.ImageView; * Creates a circular swatch of a specified color. Adds a checkmark if marked as checked. */ public class ColorPickerSwatch extends FrameLayout implements View.OnClickListener { - private int mColor; private ImageView mSwatchImage; private ImageView mCheckmarkImage; |