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

http://eyes-free.googlecode.com/ · Java · 142 lines · 81 code · 26 blank · 35 comment · 10 complexity · b59a08a7286355c29710af3efbc4c0e2 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;
  17. import android.content.Context;
  18. import android.os.Handler;
  19. import android.os.Message;
  20. import android.speech.tts.TextToSpeech;
  21. import android.text.TextUtils;
  22. import android.view.accessibility.AccessibilityEvent;
  23. import android.view.accessibility.AccessibilityNodeInfo;
  24. import com.google.android.marvin.talkback.TalkBackService.EventProcessor;
  25. import com.google.android.marvin.talkback.speechrules.NodeProcessor;
  26. class ProcessorLongHover implements EventProcessor {
  27. private final Context mContext;
  28. private final SpeechController mSpeechController;
  29. private final LongHoverHandler mHandler;
  30. public ProcessorLongHover(Context context, SpeechController speechController) {
  31. mContext = context;
  32. mSpeechController = speechController;
  33. mHandler = new LongHoverHandler();
  34. }
  35. @Override
  36. public void process(AccessibilityEvent event) {
  37. final int eventType = event.getEventType();
  38. mHandler.cancelLongHoverTimeout();
  39. if (eventType != AccessibilityEvent.TYPE_VIEW_HOVER_ENTER) {
  40. return;
  41. }
  42. mHandler.startLongHoverTimeout(event);
  43. }
  44. private class LongHoverHandler extends Handler {
  45. /** Message identifier for a verbose (long-hover) notification. */
  46. private static final int LONG_HOVER_TIMEOUT = 1;
  47. /** Timeout before reading a verbose (long-hover) notification. */
  48. private static final long DELAY_LONG_HOVER_TIMEOUT = 1000;
  49. /** The event that will be read by the utterance complete action. */
  50. private AccessibilityEvent mPendingLongHoverEvent;
  51. @Override
  52. public void handleMessage(Message msg) {
  53. switch (msg.what) {
  54. case LONG_HOVER_TIMEOUT: {
  55. final AccessibilityEvent event = (AccessibilityEvent) msg.obj;
  56. handleLongHoverTimeout(event);
  57. event.recycle();
  58. break;
  59. }
  60. }
  61. }
  62. /**
  63. * Given an {@link AccessibilityEvent}, obtains and speaks a long
  64. * hover utterance.
  65. *
  66. * @param event The source event.
  67. */
  68. private void handleLongHoverTimeout(AccessibilityEvent event) {
  69. final AccessibilityNodeInfo node = event.getSource();
  70. if (node == null) {
  71. return;
  72. }
  73. final CharSequence text = NodeProcessor.processVerbose(mContext, node);
  74. node.recycle();
  75. if (TextUtils.isEmpty(text)) {
  76. return;
  77. }
  78. mSpeechController.cleanUpAndSpeak(text, TextToSpeech.QUEUE_FLUSH);
  79. }
  80. /**
  81. * Starts the long hover timeout. Call this for every VIEW_HOVER
  82. * event.
  83. */
  84. private void startLongHoverTimeout(AccessibilityEvent event) {
  85. mPendingLongHoverEvent = AccessibilityEvent.obtain(event);
  86. mSpeechController.addUtteranceCompleteAction(-1, mLongHoverRunnable);
  87. }
  88. /**
  89. * Removes the long hover timeout and completion action. Call this
  90. * for every event.
  91. */
  92. private void cancelLongHoverTimeout() {
  93. removeMessages(LONG_HOVER_TIMEOUT);
  94. if (mPendingLongHoverEvent != null) {
  95. mSpeechController.removeUtteranceCompleteAction(mLongHoverRunnable);
  96. mPendingLongHoverEvent.recycle();
  97. mPendingLongHoverEvent = null;
  98. }
  99. }
  100. /**
  101. * Posts a delayed long hover action.
  102. */
  103. private final Runnable mLongHoverRunnable = new Runnable() {
  104. @Override
  105. public void run() {
  106. final AccessibilityEvent event = mPendingLongHoverEvent;
  107. if (event == null) {
  108. return;
  109. }
  110. final AccessibilityEvent eventClone = AccessibilityEvent.obtain(event);
  111. final Message msg = obtainMessage(LONG_HOVER_TIMEOUT, eventClone);
  112. sendMessageDelayed(msg, DELAY_LONG_HOVER_TIMEOUT);
  113. }
  114. };
  115. }
  116. }