/TalkBack/src/com/google/android/marvin/talkback/speechrules/RuleImageView.java

http://eyes-free.googlecode.com/ · Java · 64 lines · 33 code · 7 blank · 24 comment · 7 complexity · d0fa9d9576da6c7e3000e0847e1e300c MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  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.speechrules;
  17. import android.content.Context;
  18. import android.text.TextUtils;
  19. import android.util.Log;
  20. import android.view.accessibility.AccessibilityEvent;
  21. import android.view.accessibility.AccessibilityNodeInfo;
  22. import com.google.android.marvin.talkback.AccessibilityNodeInfoUtils;
  23. import com.google.android.marvin.talkback.R;
  24. /**
  25. * Processes images that are not image buttons.
  26. *
  27. * @author alanv@google.com (Alan Viverette)
  28. */
  29. public class RuleImageView extends RuleDefault {
  30. @Override
  31. public boolean accept(AccessibilityNodeInfo node) {
  32. return AccessibilityNodeInfoUtils.nodeMatchesClassByType(node,
  33. android.widget.ImageView.class)
  34. && !AccessibilityNodeInfoUtils.nodeMatchesClassByType(node,
  35. android.widget.ImageButton.class);
  36. }
  37. @Override
  38. public CharSequence
  39. format(Context context, AccessibilityNodeInfo node, AccessibilityEvent event) {
  40. final CharSequence text = super.format(context, node, event);
  41. final boolean isActionable = AccessibilityNodeInfoUtils.isActionable(node);
  42. final boolean hasLabel = !TextUtils.isEmpty(text);
  43. if (isActionable && !hasLabel) {
  44. // Log an error and announce a number for actionable unlabeled
  45. // images.
  46. Log.e("TalkBack", "Unlabeled image in " + node.getPackageName());
  47. final int nodeInt = (node.hashCode() % 100);
  48. return context.getString(R.string.template_unlabeled_image_view, nodeInt);
  49. } else if (!isActionable && hasLabel) {
  50. // Append "image" to non-actionable labeled images.
  51. return context.getString(R.string.template_image_view, text);
  52. } else {
  53. // Otherwise, just return the label.
  54. return text;
  55. }
  56. }
  57. }