/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. package com.google.android.marvin.talkback;
  17. import android.view.accessibility.AccessibilityEvent;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. /**
  22. * This class is an event queue which keeps track of relevant events. Such
  23. * events do not have {@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED}
  24. * . We treat such events in a special manner.
  25. */
  26. class EventQueue extends ArrayList<AccessibilityEvent> {
  27. /** The maximal size to the queue of cached events. */
  28. private static final int EVENT_QUEUE_MAX_SIZE = 10;
  29. private int mNonNotificationEventCount;
  30. @Override
  31. public boolean add(AccessibilityEvent event) {
  32. final AccessibilityEvent clone = AccessibilityEvent.obtain(event);
  33. if (!isNotificationEvent(clone)) {
  34. mNonNotificationEventCount++;
  35. }
  36. final boolean result = super.add(clone);
  37. enforceRelevantEventSize();
  38. return result;
  39. }
  40. @Override
  41. public void add(int location, AccessibilityEvent object) {
  42. throw new UnsupportedOperationException();
  43. }
  44. @Override
  45. public boolean addAll(Collection<? extends AccessibilityEvent> collection) {
  46. throw new UnsupportedOperationException();
  47. }
  48. @Override
  49. public boolean addAll(int location, Collection<? extends AccessibilityEvent> collection) {
  50. throw new UnsupportedOperationException();
  51. }
  52. @Override
  53. public AccessibilityEvent remove(int location) {
  54. final AccessibilityEvent event = get(location);
  55. if (event != null && !isNotificationEvent(event)) {
  56. mNonNotificationEventCount--;
  57. }
  58. return super.remove(location);
  59. }
  60. @Override
  61. public boolean remove(Object object) {
  62. throw new UnsupportedOperationException();
  63. }
  64. @Override
  65. public void clear() {
  66. final Iterator<AccessibilityEvent> iterator = iterator();
  67. while (iterator.hasNext()) {
  68. final AccessibilityEvent next = iterator.next();
  69. // Never remove notification events.
  70. if (!isNotificationEvent(next)) {
  71. iterator.remove();
  72. }
  73. }
  74. mNonNotificationEventCount = 0;
  75. }
  76. /**
  77. * Enforces that the event queue is not more than
  78. * {@link #EVENT_QUEUE_MAX_SIZE}. The excessive events are
  79. * pruned through a FIFO strategy i.e. removing the oldest event first.
  80. */
  81. public void enforceRelevantEventSize() {
  82. final Iterator<AccessibilityEvent> iterator = iterator();
  83. while (iterator().hasNext()
  84. && (mNonNotificationEventCount > EVENT_QUEUE_MAX_SIZE)) {
  85. final AccessibilityEvent next = iterator.next();
  86. // Never remove notification events.
  87. if (!isNotificationEvent(next)) {
  88. mNonNotificationEventCount--;
  89. iterator.remove();
  90. }
  91. }
  92. }
  93. /**
  94. * Returns if an event type is
  95. * AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED.
  96. */
  97. private static boolean isNotificationEvent(AccessibilityEvent event) {
  98. return (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
  99. }
  100. }