PageRenderTime 80ms CodeModel.GetById 73ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://eyes-free.googlecode.com/
Java | 125 lines | 68 code | 25 blank | 32 comment | 10 complexity | d14f3ff91658e762d11c6387c13154ed MD5 | raw file
  1/*
  2 * Copyright (C) 2009 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.view.accessibility.AccessibilityEvent;
 20
 21import java.util.ArrayList;
 22import java.util.Collection;
 23import java.util.Iterator;
 24
 25/**
 26 * This class is an event queue which keeps track of relevant events. Such
 27 * events do not have {@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED}
 28 * . We treat such events in a special manner.
 29 */
 30class EventQueue extends ArrayList<AccessibilityEvent> {
 31    /** The maximal size to the queue of cached events. */
 32    private static final int EVENT_QUEUE_MAX_SIZE = 10;
 33    
 34    private int mNonNotificationEventCount;
 35
 36    @Override
 37    public boolean add(AccessibilityEvent event) {
 38        final AccessibilityEvent clone = AccessibilityEvent.obtain(event);
 39        
 40        if (!isNotificationEvent(clone)) {
 41            mNonNotificationEventCount++;
 42        }
 43
 44        final boolean result = super.add(clone);
 45
 46        enforceRelevantEventSize();
 47
 48        return result;
 49    }
 50
 51    @Override
 52    public void add(int location, AccessibilityEvent object) {
 53        throw new UnsupportedOperationException();
 54    }
 55
 56    @Override
 57    public boolean addAll(Collection<? extends AccessibilityEvent> collection) {
 58        throw new UnsupportedOperationException();
 59    }
 60
 61    @Override
 62    public boolean addAll(int location, Collection<? extends AccessibilityEvent> collection) {
 63        throw new UnsupportedOperationException();
 64    }
 65
 66    @Override
 67    public AccessibilityEvent remove(int location) {
 68        final AccessibilityEvent event = get(location);
 69
 70        if (event != null && !isNotificationEvent(event)) {
 71            mNonNotificationEventCount--;
 72        }
 73
 74        return super.remove(location);
 75    }
 76
 77    @Override
 78    public boolean remove(Object object) {
 79        throw new UnsupportedOperationException();
 80    }
 81
 82    @Override
 83    public void clear() {
 84        final Iterator<AccessibilityEvent> iterator = iterator();
 85
 86        while (iterator.hasNext()) {
 87            final AccessibilityEvent next = iterator.next();
 88
 89            // Never remove notification events.
 90            if (!isNotificationEvent(next)) {
 91                iterator.remove();
 92            }
 93        }
 94
 95        mNonNotificationEventCount = 0;
 96    }
 97
 98    /**
 99     * Enforces that the event queue is not more than
100     * {@link #EVENT_QUEUE_MAX_SIZE}. The excessive events are
101     * pruned through a FIFO strategy i.e. removing the oldest event first.
102     */
103    public void enforceRelevantEventSize() {
104        final Iterator<AccessibilityEvent> iterator = iterator();
105
106        while (iterator().hasNext()
107                && (mNonNotificationEventCount > EVENT_QUEUE_MAX_SIZE)) {
108            final AccessibilityEvent next = iterator.next();
109
110            // Never remove notification events.
111            if (!isNotificationEvent(next)) {
112                mNonNotificationEventCount--;
113                iterator.remove();
114            }
115        }
116    }
117
118    /**
119     * Returns if an event type is
120     * AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED.
121     */
122    private static boolean isNotificationEvent(AccessibilityEvent event) {
123        return (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
124    }
125}