PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/TalkBack/src/com/google/android/marvin/talkback/formatter/TouchExplorationSystemUiFormatter.java

http://eyes-free.googlecode.com/
Java | 87 lines | 50 code | 17 blank | 20 comment | 8 complexity | 4d8fed93a62967e01095bd76ba96f92c 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");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.android.marvin.talkback.formatter;
18
19import android.content.Context;
20import android.os.Bundle;
21import android.text.TextUtils;
22import android.view.accessibility.AccessibilityEvent;
23
24import com.google.android.marvin.talkback.Filter;
25import com.google.android.marvin.talkback.Formatter;
26import com.google.android.marvin.talkback.R;
27import com.google.android.marvin.talkback.Utils;
28import com.google.android.marvin.talkback.Utterance;
29
30import java.util.Collections;
31import java.util.List;
32
33/**
34 * Formatter for touch exploration events from the System UI.
35 *
36 * @author svetoslavganov@google.com (Svetoslav Ganov)
37 */
38public class TouchExplorationSystemUiFormatter implements Filter, Formatter {
39    private static final String SYSTEM_UI_PACKAGE_NAME = "com.android.systemui";
40    private static final String RECORD_SEPARATOR = " ";
41
42    private final StringBuilder mLastUtteranceText = new StringBuilder();
43
44    @Override
45    public boolean accept(AccessibilityEvent event, Context context, Bundle args) {
46        return (event.getEventType() == AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
47                && SYSTEM_UI_PACKAGE_NAME.equals(event.getPackageName())
48                && (event.getRecordCount() > 0);
49    }
50
51    @Override
52    public boolean format(AccessibilityEvent event, Context context, Utterance utterance, Bundle args) {
53        final StringBuilder utteranceText = utterance.getText();
54
55        if (event.getRecordCount() > 0) {
56            final List<CharSequence> entries = event.getRecord(0).getText();
57
58            Collections.reverse(entries);
59
60            for (final CharSequence entry : entries) {
61                utteranceText.append(entry);
62                utteranceText.append(RECORD_SEPARATOR);
63            }
64        } else {
65            final CharSequence eventText = Utils.getEventText(context, event);
66
67            utteranceText.append(eventText);
68        }
69
70        if (TextUtils.isEmpty(utteranceText)) {
71            return false;
72        }
73
74        if (TextUtils.equals(mLastUtteranceText, utteranceText)) {
75            utteranceText.setLength(0);
76            return false;
77        }
78
79        utterance.getVibrationPatterns().add(R.array.view_hovered_pattern);
80        utterance.getEarcons().add(R.raw.hover);
81
82        mLastUtteranceText.setLength(0);
83        mLastUtteranceText.append(utteranceText);
84        
85        return true;
86    }
87}