PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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