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

http://eyes-free.googlecode.com/ · Java · 73 lines · 34 code · 11 blank · 28 comment · 4 complexity · 62b6d9d0cc2130fcd1722d8b5df4d197 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.view.accessibility.AccessibilityEvent;
  20. import android.view.accessibility.AccessibilityNodeInfo;
  21. import com.google.android.marvin.talkback.AccessibilityNodeInfoUtils;
  22. import com.google.android.marvin.talkback.R;
  23. /**
  24. * Processes editable text fields.
  25. *
  26. * @author alanv@google.com (Alan Viverette)
  27. */
  28. class RuleEditText implements NodeSpeechRule {
  29. @Override
  30. public boolean accept(AccessibilityNodeInfo node) {
  31. return AccessibilityNodeInfoUtils.nodeMatchesClassByType(node,
  32. android.widget.EditText.class);
  33. }
  34. @Override
  35. public CharSequence
  36. format(Context context, AccessibilityNodeInfo node, AccessibilityEvent event) {
  37. final CharSequence text = getText(context, node);
  38. return context.getString(R.string.template_edit_box, text);
  39. }
  40. /**
  41. * Inverts the default priorities of text and content description. If the
  42. * field is a password, only returns the content description or "password".
  43. *
  44. * @param context
  45. * @param node
  46. * @return A text description of the editable text area.
  47. */
  48. private CharSequence getText(Context context, AccessibilityNodeInfo node) {
  49. final CharSequence text = node.getText();
  50. if (!TextUtils.isEmpty(text) && !node.isPassword()) {
  51. return text;
  52. }
  53. final CharSequence contentDescription = node.getContentDescription();
  54. if (!TextUtils.isEmpty(contentDescription)) {
  55. return contentDescription;
  56. }
  57. if (node.isPassword()) {
  58. return context.getString(R.string.value_password);
  59. }
  60. return "";
  61. }
  62. }