/TalkBack/src/com/google/android/marvin/talkback/PlayableListPreference.java

http://eyes-free.googlecode.com/ · Java · 113 lines · 71 code · 20 blank · 22 comment · 12 complexity · 22441bec3fd20a79290f649720cdb050 MD5 · raw file

  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.android.marvin.talkback;
  17. import android.content.Context;
  18. import android.content.res.Resources;
  19. import android.preference.ListPreference;
  20. import android.util.AttributeSet;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.view.View.AccessibilityDelegate;
  24. import android.view.View.OnClickListener;
  25. import android.view.accessibility.AccessibilityEvent;
  26. import android.widget.ImageView;
  27. /**
  28. * Extends {@link ListPreference} by providing a preview button for sounds and
  29. * vibration patterns.
  30. *
  31. * @author alanv@google.com (Alan Viverette)
  32. */
  33. public class PlayableListPreference extends ListPreference {
  34. private static final String TAG = PlayableListPreference.class.getSimpleName();
  35. private static final String TYPE_SOUND = "raw/";
  36. private static final String TYPE_VIBRATION = "array/";
  37. private final FeedbackController mFeedbackController;
  38. public PlayableListPreference(Context context) {
  39. this(context, null);
  40. }
  41. public PlayableListPreference(Context context, AttributeSet attrs) {
  42. super(context, attrs);
  43. setWidgetLayoutResource(R.layout.play_button);
  44. mFeedbackController = FeedbackController.getInstance(context);
  45. }
  46. @Override
  47. protected void onBindView(View view) {
  48. final ImageView playButton = (ImageView) view.findViewById(R.id.play_button);
  49. if (playButton != null) {
  50. playButton.setOnClickListener(mClickListener);
  51. playButton.setAccessibilityDelegate(mDelegate);
  52. }
  53. super.onBindView(view);
  54. }
  55. private void playCurrentValue() {
  56. final String value = getValue();
  57. if (value == null || !value.matches("\\w+/\\w+")) {
  58. Log.e(TAG, "Invalid resource format: " + value);
  59. return;
  60. }
  61. final Context context = getContext();
  62. final Resources res = context.getResources();
  63. final int resId = res.getIdentifier(value, null, context.getPackageName());
  64. if (resId <= 0) {
  65. Log.e(TAG, "Unable to load resource identifier: " + value);
  66. return;
  67. }
  68. if (value.startsWith(TYPE_SOUND)) {
  69. mFeedbackController.playSound(resId, 1.0f);
  70. } else if (value.startsWith(TYPE_VIBRATION)) {
  71. mFeedbackController.playVibration(resId);
  72. } else {
  73. Log.e(TAG, "Unknown resource type: " + value);
  74. return;
  75. }
  76. }
  77. private final AccessibilityDelegate mDelegate = new AccessibilityDelegate() {
  78. @Override
  79. public void sendAccessibilityEvent(View host, int eventType) {
  80. // Don't send VIEW_CLICKED events.
  81. if (eventType == AccessibilityEvent.TYPE_VIEW_CLICKED) {
  82. return;
  83. }
  84. super.sendAccessibilityEvent(host, eventType);
  85. }
  86. };
  87. private final OnClickListener mClickListener = new OnClickListener() {
  88. @Override
  89. public void onClick(View v) {
  90. playCurrentValue();
  91. }
  92. };
  93. }