2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.graphics.Rect;
20 import android.graphics.Region;
22 import java.util.ArrayList;
23 import java.util.concurrent.CopyOnWriteArrayList;
26 * A view tree observer is used to register listeners that can be notified of global
27 * changes in the view tree. Such global events include, but are not limited to,
28 * layout of the whole tree, beginning of the drawing pass, touch mode change....
30 * A ViewTreeObserver should never be instantiated by applications as it is provided
31 * by the views hierarchy. Refer to {@link android.view.View#getViewTreeObserver()}
32 * for more information.
34 public final class ViewTreeObserver {
35 // Recursive listeners use CopyOnWriteArrayList
36 private CopyOnWriteArrayList<OnWindowFocusChangeListener> mOnWindowFocusListeners;
37 private CopyOnWriteArrayList<OnWindowAttachListener> mOnWindowAttachListeners;
38 private CopyOnWriteArrayList<OnGlobalFocusChangeListener> mOnGlobalFocusListeners;
39 private CopyOnWriteArrayList<OnTouchModeChangeListener> mOnTouchModeChangeListeners;
41 // Non-recursive listeners use CopyOnWriteArray
42 // Any listener invoked from ViewRootImpl.performTraversals() should not be recursive
43 private CopyOnWriteArray<OnGlobalLayoutListener> mOnGlobalLayoutListeners;
44 private CopyOnWriteArray<OnComputeInternalInsetsListener> mOnComputeInternalInsetsListeners;
45 private CopyOnWriteArray<OnScrollChangedListener> mOnScrollChangedListeners;
46 private CopyOnWriteArray<OnPreDrawListener> mOnPreDrawListeners;
48 // These listeners cannot be mutated during dispatch
49 private ArrayList<OnDrawListener> mOnDrawListeners;
51 private boolean mAlive = true;
54 * Interface definition for a callback to be invoked when the view hierarchy is
55 * attached to and detached from its window.
57 public interface OnWindowAttachListener {
59 * Callback method to be invoked when the view hierarchy is attached to a window
61 public void onWindowAttached();
64 * Callback method to be invoked when the view hierarchy is detached from a window
66 public void onWindowDetached();
70 * Interface definition for a callback to be invoked when the view hierarchy's window
71 * focus state changes.
73 public interface OnWindowFocusChangeListener {
75 * Callback method to be invoked when the window focus changes in the view tree.
77 * @param hasFocus Set to true if the window is gaining focus, false if it is
80 public void onWindowFocusChanged(boolean hasFocus);
84 * Interface definition for a callback to be invoked when the focus state within
85 * the view tree changes.
87 public interface OnGlobalFocusChangeListener {
89 * Callback method to be invoked when the focus changes in the view tree. When
90 * the view tree transitions from touch mode to non-touch mode, oldFocus is null.
91 * When the view tree transitions from non-touch mode to touch mode, newFocus is
92 * null. When focus changes in non-touch mode (without transition from or to
93 * touch mode) either oldFocus or newFocus can be null.
95 * @param oldFocus The previously focused view, if any.
96 * @param newFocus The newly focused View, if any.
98 public void onGlobalFocusChanged(View oldFocus, View newFocus);
102 * Interface definition for a callback to be invoked when the global layout state
103 * or the visibility of views within the view tree changes.
105 public interface OnGlobalLayoutListener {
107 * Callback method to be invoked when the global layout state or the visibility of views
108 * within the view tree changes
110 public void onGlobalLayout();
114 * Interface definition for a callback to be invoked when the view tree is about to be drawn.
116 public interface OnPreDrawListener {
118 * Callback method to be invoked when the view tree is about to be drawn. At this point, all
119 * views in the tree have been measured and given a frame. Clients can use this to adjust
120 * their scroll bounds or even to request a new layout before drawing occurs.
122 * @return Return true to proceed with the current drawing pass, or false to cancel.
124 * @see android.view.View#onMeasure
125 * @see android.view.View#onLayout
126 * @see android.view.View#onDraw
128 public boolean onPreDraw();
132 * Interface definition for a callback to be invoked when the view tree is about to be drawn.
134 public interface OnDrawListener {
136 * <p>Callback method to be invoked when the view tree is about to be drawn. At this point,
137 * views cannot be modified in any way.</p>
139 * <p>Unlike with {@link OnPreDrawListener}, this method cannot be used to cancel the
140 * current drawing pass.</p>
142 * <p>An {@link OnDrawListener} listener <strong>cannot be added or removed</strong>
143 * from this method.</p>
145 * @see android.view.View#onMeasure
146 * @see android.view.View#onLayout
147 * @see android.view.View#onDraw
149 public void onDraw();
153 * Interface definition for a callback to be invoked when the touch mode changes.
155 public interface OnTouchModeChangeListener {
157 * Callback method to be invoked when the touch mode changes.
159 * @param isInTouchMode True if the view hierarchy is now in touch mode, false otherwise.
161 public void onTouchModeChanged(boolean isInTouchMode);
165 * Interface definition for a callback to be invoked when
166 * something in the view tree has been scrolled.
168 public interface OnScrollChangedListener {
170 * Callback method to be invoked when something in the view tree
173 public void onScrollChanged();
177 * Parameters used with OnComputeInternalInsetsListener.
179 * We are not yet ready to commit to this API and support it, so
182 public final static class InternalInsetsInfo {
184 * Offsets from the frame of the window at which the content of
185 * windows behind it should be placed.
187 public final Rect contentInsets = new Rect();
190 * Offsets from the frame of the window at which windows behind it
193 public final Rect visibleInsets = new Rect();
196 * Touchable region defined relative to the origin of the frame of the window.
197 * Only used when {@link #setTouchableInsets(int)} is called with
198 * the option {@link #TOUCHABLE_INSETS_REGION}.
200 public final Region touchableRegion = new Region();
203 * Option for {@link #setTouchableInsets(int)}: the entire window frame
206 public static final int TOUCHABLE_INSETS_FRAME = 0;
209 * Option for {@link #setTouchableInsets(int)}: the area inside of
210 * the content insets can be touched.
212 public static final int TOUCHABLE_INSETS_CONTENT = 1;
215 * Option for {@link #setTouchableInsets(int)}: the area inside of
216 * the visible insets can be touched.
218 public static final int TOUCHABLE_INSETS_VISIBLE = 2;
221 * Option for {@link #setTouchableInsets(int)}: the area inside of
222 * the provided touchable region in {@link #touchableRegion} can be touched.
224 public static final int TOUCHABLE_INSETS_REGION = 3;
227 * Set which parts of the window can be touched: either
228 * {@link #TOUCHABLE_INSETS_FRAME}, {@link #TOUCHABLE_INSETS_CONTENT},
229 * {@link #TOUCHABLE_INSETS_VISIBLE}, or {@link #TOUCHABLE_INSETS_REGION}.
231 public void setTouchableInsets(int val) {
232 mTouchableInsets = val;
235 int mTouchableInsets;
238 contentInsets.setEmpty();
239 visibleInsets.setEmpty();
240 touchableRegion.setEmpty();
241 mTouchableInsets = TOUCHABLE_INSETS_FRAME;
245 return contentInsets.isEmpty()
246 && visibleInsets.isEmpty()
247 && touchableRegion.isEmpty()
248 && mTouchableInsets == TOUCHABLE_INSETS_FRAME;
252 public int hashCode() {
253 int result = contentInsets.hashCode();
254 result = 31 * result + visibleInsets.hashCode();
255 result = 31 * result + touchableRegion.hashCode();
256 result = 31 * result + mTouchableInsets;
261 public boolean equals(Object o) {
262 if (this == o) return true;
263 if (o == null || getClass() != o.getClass()) return false;
265 InternalInsetsInfo other = (InternalInsetsInfo)o;
266 return mTouchableInsets == other.mTouchableInsets &&
267 contentInsets.equals(other.contentInsets) &&
268 visibleInsets.equals(other.visibleInsets) &&
269 touchableRegion.equals(other.touchableRegion);
272 void set(InternalInsetsInfo other) {
273 contentInsets.set(other.contentInsets);
274 visibleInsets.set(other.visibleInsets);
275 touchableRegion.set(other.touchableRegion);
276 mTouchableInsets = other.mTouchableInsets;
281 * Interface definition for a callback to be invoked when layout has
282 * completed and the client can compute its interior insets.
284 * We are not yet ready to commit to this API and support it, so
287 public interface OnComputeInternalInsetsListener {
289 * Callback method to be invoked when layout has completed and the
290 * client can compute its interior insets.
292 * @param inoutInfo Should be filled in by the implementation with
293 * the information about the insets of the window. This is called
294 * with whatever values the previous OnComputeInternalInsetsListener
295 * returned, if there are multiple such listeners in the window.
297 public void onComputeInternalInsets(InternalInsetsInfo inoutInfo);
301 * Creates a new ViewTreeObserver. This constructor should not be called
307 * Merges all the listeners registered on the specified observer with the listeners
308 * registered on this object. After this method is invoked, the specified observer
309 * will return false in {@link #isAlive()} and should not be used anymore.
311 * @param observer The ViewTreeObserver whose listeners must be added to this observer
313 void merge(ViewTreeObserver observer) {
314 if (observer.mOnWindowAttachListeners != null) {
315 if (mOnWindowAttachListeners != null) {
316 mOnWindowAttachListeners.addAll(observer.mOnWindowAttachListeners);
318 mOnWindowAttachListeners = observer.mOnWindowAttachListeners;
322 if (observer.mOnWindowFocusListeners != null) {
323 if (mOnWindowFocusListeners != null) {
324 mOnWindowFocusListeners.addAll(observer.mOnWindowFocusListeners);
326 mOnWindowFocusListeners = observer.mOnWindowFocusListeners;
330 if (observer.mOnGlobalFocusListeners != null) {
331 if (mOnGlobalFocusListeners != null) {
332 mOnGlobalFocusListeners.addAll(observer.mOnGlobalFocusListeners);
334 mOnGlobalFocusListeners = observer.mOnGlobalFocusListeners;
338 if (observer.mOnGlobalLayoutListeners != null) {
339 if (mOnGlobalLayoutListeners != null) {
340 mOnGlobalLayoutListeners.addAll(observer.mOnGlobalLayoutListeners);
342 mOnGlobalLayoutListeners = observer.mOnGlobalLayoutListeners;
346 if (observer.mOnPreDrawListeners != null) {
347 if (mOnPreDrawListeners != null) {
348 mOnPreDrawListeners.addAll(observer.mOnPreDrawListeners);
350 mOnPreDrawListeners = observer.mOnPreDrawListeners;
354 if (observer.mOnTouchModeChangeListeners != null) {
355 if (mOnTouchModeChangeListeners != null) {
356 mOnTouchModeChangeListeners.addAll(observer.mOnTouchModeChangeListeners);
358 mOnTouchModeChangeListeners = observer.mOnTouchModeChangeListeners;
362 if (observer.mOnComputeInternalInsetsListeners != null) {
363 if (mOnComputeInternalInsetsListeners != null) {
364 mOnComputeInternalInsetsListeners.addAll(observer.mOnComputeInternalInsetsListeners);
366 mOnComputeInternalInsetsListeners = observer.mOnComputeInternalInsetsListeners;
370 if (observer.mOnScrollChangedListeners != null) {
371 if (mOnScrollChangedListeners != null) {
372 mOnScrollChangedListeners.addAll(observer.mOnScrollChangedListeners);
374 mOnScrollChangedListeners = observer.mOnScrollChangedListeners;
382 * Register a callback to be invoked when the view hierarchy is attached to a window.
384 * @param listener The callback to add
386 * @throws IllegalStateException If {@link #isAlive()} returns false
388 public void addOnWindowAttachListener(OnWindowAttachListener listener) {
391 if (mOnWindowAttachListeners == null) {
392 mOnWindowAttachListeners
393 = new CopyOnWriteArrayList<OnWindowAttachListener>();
396 mOnWindowAttachListeners.add(listener);
400 * Remove a previously installed window attach callback.
402 * @param victim The callback to remove
404 * @throws IllegalStateException If {@link #isAlive()} returns false
406 * @see #addOnWindowAttachListener(android.view.ViewTreeObserver.OnWindowAttachListener)
408 public void removeOnWindowAttachListener(OnWindowAttachListener victim) {
410 if (mOnWindowAttachListeners == null) {
413 mOnWindowAttachListeners.remove(victim);
417 * Register a callback to be invoked when the window focus state within the view tree changes.
419 * @param listener The callback to add
421 * @throws IllegalStateException If {@link #isAlive()} returns false
423 public void addOnWindowFocusChangeListener(OnWindowFocusChangeListener listener) {
426 if (mOnWindowFocusListeners == null) {
427 mOnWindowFocusListeners
428 = new CopyOnWriteArrayList<OnWindowFocusChangeListener>();
431 mOnWindowFocusListeners.add(listener);
435 * Remove a previously installed window focus change callback.
437 * @param victim The callback to remove
439 * @throws IllegalStateException If {@link #isAlive()} returns false
441 * @see #addOnWindowFocusChangeListener(android.view.ViewTreeObserver.OnWindowFocusChangeListener)
443 public void removeOnWindowFocusChangeListener(OnWindowFocusChangeListener victim) {
445 if (mOnWindowFocusListeners == null) {
448 mOnWindowFocusListeners.remove(victim);
452 * Register a callback to be invoked when the focus state within the view tree changes.
454 * @param listener The callback to add
456 * @throws IllegalStateException If {@link #isAlive()} returns false
458 public void addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener listener) {
461 if (mOnGlobalFocusListeners == null) {
462 mOnGlobalFocusListeners = new CopyOnWriteArrayList<OnGlobalFocusChangeListener>();
465 mOnGlobalFocusListeners.add(listener);
469 * Remove a previously installed focus change callback.
471 * @param victim The callback to remove
473 * @throws IllegalStateException If {@link #isAlive()} returns false
475 * @see #addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener)
477 public void removeOnGlobalFocusChangeListener(OnGlobalFocusChangeListener victim) {
479 if (mOnGlobalFocusListeners == null) {
482 mOnGlobalFocusListeners.remove(victim);
486 * Register a callback to be invoked when the global layout state or the visibility of views
487 * within the view tree changes
489 * @param listener The callback to add
491 * @throws IllegalStateException If {@link #isAlive()} returns false
493 public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) {
496 if (mOnGlobalLayoutListeners == null) {
497 mOnGlobalLayoutListeners = new CopyOnWriteArray<OnGlobalLayoutListener>();
500 mOnGlobalLayoutListeners.add(listener);
504 * Remove a previously installed global layout callback
506 * @param victim The callback to remove
508 * @throws IllegalStateException If {@link #isAlive()} returns false
510 * @deprecated Use #removeOnGlobalLayoutListener instead
512 * @see #addOnGlobalLayoutListener(OnGlobalLayoutListener)
515 public void removeGlobalOnLayoutListener(OnGlobalLayoutListener victim) {
516 removeOnGlobalLayoutListener(victim);
520 * Remove a previously installed global layout callback
522 * @param victim The callback to remove
524 * @throws IllegalStateException If {@link #isAlive()} returns false
526 * @see #addOnGlobalLayoutListener(OnGlobalLayoutListener)
528 public void removeOnGlobalLayoutListener(OnGlobalLayoutListener victim) {
530 if (mOnGlobalLayoutListeners == null) {
533 mOnGlobalLayoutListeners.remove(victim);
537 * Register a callback to be invoked when the view tree is about to be drawn
539 * @param listener The callback to add
541 * @throws IllegalStateException If {@link #isAlive()} returns false
543 public void addOnPreDrawListener(OnPreDrawListener listener) {
546 if (mOnPreDrawListeners == null) {
547 mOnPreDrawListeners = new CopyOnWriteArray<OnPreDrawListener>();
550 mOnPreDrawListeners.add(listener);
554 * Remove a previously installed pre-draw callback
556 * @param victim The callback to remove
558 * @throws IllegalStateException If {@link #isAlive()} returns false
560 * @see #addOnPreDrawListener(OnPreDrawListener)
562 public void removeOnPreDrawListener(OnPreDrawListener victim) {
564 if (mOnPreDrawListeners == null) {
567 mOnPreDrawListeners.remove(victim);
571 * <p>Register a callback to be invoked when the view tree is about to be drawn.</p>
572 * <p><strong>Note:</strong> this method <strong>cannot</strong> be invoked from
573 * {@link android.view.ViewTreeObserver.OnDrawListener#onDraw()}.</p>
575 * @param listener The callback to add
577 * @throws IllegalStateException If {@link #isAlive()} returns false
579 public void addOnDrawListener(OnDrawListener listener) {
582 if (mOnDrawListeners == null) {
583 mOnDrawListeners = new ArrayList<OnDrawListener>();
586 mOnDrawListeners.add(listener);
590 * <p>Remove a previously installed pre-draw callback.</p>
591 * <p><strong>Note:</strong> this method <strong>cannot</strong> be invoked from
592 * {@link android.view.ViewTreeObserver.OnDrawListener#onDraw()}.</p>
594 * @param victim The callback to remove
596 * @throws IllegalStateException If {@link #isAlive()} returns false
598 * @see #addOnDrawListener(OnDrawListener)
600 public void removeOnDrawListener(OnDrawListener victim) {
602 if (mOnDrawListeners == null) {
605 mOnDrawListeners.remove(victim);
609 * Register a callback to be invoked when a view has been scrolled.
611 * @param listener The callback to add
613 * @throws IllegalStateException If {@link #isAlive()} returns false
615 public void addOnScrollChangedListener(OnScrollChangedListener listener) {
618 if (mOnScrollChangedListeners == null) {
619 mOnScrollChangedListeners = new CopyOnWriteArray<OnScrollChangedListener>();
622 mOnScrollChangedListeners.add(listener);
626 * Remove a previously installed scroll-changed callback
628 * @param victim The callback to remove
630 * @throws IllegalStateException If {@link #isAlive()} returns false
632 * @see #addOnScrollChangedListener(OnScrollChangedListener)
634 public void removeOnScrollChangedListener(OnScrollChangedListener victim) {
636 if (mOnScrollChangedListeners == null) {
639 mOnScrollChangedListeners.remove(victim);
643 * Register a callback to be invoked when the invoked when the touch mode changes.
645 * @param listener The callback to add
647 * @throws IllegalStateException If {@link #isAlive()} returns false
649 public void addOnTouchModeChangeListener(OnTouchModeChangeListener listener) {
652 if (mOnTouchModeChangeListeners == null) {
653 mOnTouchModeChangeListeners = new CopyOnWriteArrayList<OnTouchModeChangeListener>();
656 mOnTouchModeChangeListeners.add(listener);
660 * Remove a previously installed touch mode change callback
662 * @param victim The callback to remove
664 * @throws IllegalStateException If {@link #isAlive()} returns false
666 * @see #addOnTouchModeChangeListener(OnTouchModeChangeListener)
668 public void removeOnTouchModeChangeListener(OnTouchModeChangeListener victim) {
670 if (mOnTouchModeChangeListeners == null) {
673 mOnTouchModeChangeListeners.remove(victim);
677 * Register a callback to be invoked when the invoked when it is time to
678 * compute the window's internal insets.
680 * @param listener The callback to add
682 * @throws IllegalStateException If {@link #isAlive()} returns false
684 * We are not yet ready to commit to this API and support it, so
687 public void addOnComputeInternalInsetsListener(OnComputeInternalInsetsListener listener) {
690 if (mOnComputeInternalInsetsListeners == null) {
691 mOnComputeInternalInsetsListeners =
692 new CopyOnWriteArray<OnComputeInternalInsetsListener>();
695 mOnComputeInternalInsetsListeners.add(listener);
699 * Remove a previously installed internal insets computation callback
701 * @param victim The callback to remove
703 * @throws IllegalStateException If {@link #isAlive()} returns false
705 * @see #addOnComputeInternalInsetsListener(OnComputeInternalInsetsListener)
707 * We are not yet ready to commit to this API and support it, so
710 public void removeOnComputeInternalInsetsListener(OnComputeInternalInsetsListener victim) {
712 if (mOnComputeInternalInsetsListeners == null) {
715 mOnComputeInternalInsetsListeners.remove(victim);
718 private void checkIsAlive() {
720 throw new IllegalStateException("This ViewTreeObserver is not alive, call "
721 + "getViewTreeObserver() again");
726 * Indicates whether this ViewTreeObserver is alive. When an observer is not alive,
727 * any call to a method (except this one) will throw an exception.
729 * If an application keeps a long-lived reference to this ViewTreeObserver, it should
730 * always check for the result of this method before calling any other method.
732 * @return True if this object is alive and be used, false otherwise.
734 public boolean isAlive() {
739 * Marks this ViewTreeObserver as not alive. After invoking this method, invoking
740 * any other method but {@link #isAlive()} and {@link #kill()} will throw an Exception.
744 private void kill() {
749 * Notifies registered listeners that window has been attached/detached.
751 final void dispatchOnWindowAttachedChange(boolean attached) {
752 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
753 // perform the dispatching. The iterator is a safe guard against listeners that
754 // could mutate the list by calling the various add/remove methods. This prevents
755 // the array from being modified while we iterate it.
756 final CopyOnWriteArrayList<OnWindowAttachListener> listeners
757 = mOnWindowAttachListeners;
758 if (listeners != null && listeners.size() > 0) {
759 for (OnWindowAttachListener listener : listeners) {
760 if (attached) listener.onWindowAttached();
761 else listener.onWindowDetached();
767 * Notifies registered listeners that window focus has changed.
769 final void dispatchOnWindowFocusChange(boolean hasFocus) {
770 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
771 // perform the dispatching. The iterator is a safe guard against listeners that
772 // could mutate the list by calling the various add/remove methods. This prevents
773 // the array from being modified while we iterate it.
774 final CopyOnWriteArrayList<OnWindowFocusChangeListener> listeners
775 = mOnWindowFocusListeners;
776 if (listeners != null && listeners.size() > 0) {
777 for (OnWindowFocusChangeListener listener : listeners) {
778 listener.onWindowFocusChanged(hasFocus);
784 * Notifies registered listeners that focus has changed.
786 final void dispatchOnGlobalFocusChange(View oldFocus, View newFocus) {
787 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
788 // perform the dispatching. The iterator is a safe guard against listeners that
789 // could mutate the list by calling the various add/remove methods. This prevents
790 // the array from being modified while we iterate it.
791 final CopyOnWriteArrayList<OnGlobalFocusChangeListener> listeners = mOnGlobalFocusListeners;
792 if (listeners != null && listeners.size() > 0) {
793 for (OnGlobalFocusChangeListener listener : listeners) {
794 listener.onGlobalFocusChanged(oldFocus, newFocus);
800 * Notifies registered listeners that a global layout happened. This can be called
801 * manually if you are forcing a layout on a View or a hierarchy of Views that are
802 * not attached to a Window or in the GONE state.
804 public final void dispatchOnGlobalLayout() {
805 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
806 // perform the dispatching. The iterator is a safe guard against listeners that
807 // could mutate the list by calling the various add/remove methods. This prevents
808 // the array from being modified while we iterate it.
809 final CopyOnWriteArray<OnGlobalLayoutListener> listeners = mOnGlobalLayoutListeners;
810 if (listeners != null && listeners.size() > 0) {
811 CopyOnWriteArray.Access<OnGlobalLayoutListener> access = listeners.start();
813 int count = access.size();
814 for (int i = 0; i < count; i++) {
815 access.get(i).onGlobalLayout();
824 * Returns whether there are listeners for on pre-draw events.
826 final boolean hasOnPreDrawListeners() {
827 return mOnPreDrawListeners != null && mOnPreDrawListeners.size() > 0;
831 * Notifies registered listeners that the drawing pass is about to start. If a
832 * listener returns true, then the drawing pass is canceled and rescheduled. This can
833 * be called manually if you are forcing the drawing on a View or a hierarchy of Views
834 * that are not attached to a Window or in the GONE state.
836 * @return True if the current draw should be canceled and resceduled, false otherwise.
838 @SuppressWarnings("unchecked")
839 public final boolean dispatchOnPreDraw() {
840 boolean cancelDraw = false;
841 final CopyOnWriteArray<OnPreDrawListener> listeners = mOnPreDrawListeners;
842 if (listeners != null && listeners.size() > 0) {
843 CopyOnWriteArray.Access<OnPreDrawListener> access = listeners.start();
845 int count = access.size();
846 for (int i = 0; i < count; i++) {
847 cancelDraw |= !(access.get(i).onPreDraw());
857 * Notifies registered listeners that the drawing pass is about to start.
859 public final void dispatchOnDraw() {
860 if (mOnDrawListeners != null) {
861 final ArrayList<OnDrawListener> listeners = mOnDrawListeners;
862 int numListeners = listeners.size();
863 for (int i = 0; i < numListeners; ++i) {
864 listeners.get(i).onDraw();
870 * Notifies registered listeners that the touch mode has changed.
872 * @param inTouchMode True if the touch mode is now enabled, false otherwise.
874 final void dispatchOnTouchModeChanged(boolean inTouchMode) {
875 final CopyOnWriteArrayList<OnTouchModeChangeListener> listeners =
876 mOnTouchModeChangeListeners;
877 if (listeners != null && listeners.size() > 0) {
878 for (OnTouchModeChangeListener listener : listeners) {
879 listener.onTouchModeChanged(inTouchMode);
885 * Notifies registered listeners that something has scrolled.
887 final void dispatchOnScrollChanged() {
888 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
889 // perform the dispatching. The iterator is a safe guard against listeners that
890 // could mutate the list by calling the various add/remove methods. This prevents
891 // the array from being modified while we iterate it.
892 final CopyOnWriteArray<OnScrollChangedListener> listeners = mOnScrollChangedListeners;
893 if (listeners != null && listeners.size() > 0) {
894 CopyOnWriteArray.Access<OnScrollChangedListener> access = listeners.start();
896 int count = access.size();
897 for (int i = 0; i < count; i++) {
898 access.get(i).onScrollChanged();
907 * Returns whether there are listeners for computing internal insets.
909 final boolean hasComputeInternalInsetsListeners() {
910 final CopyOnWriteArray<OnComputeInternalInsetsListener> listeners =
911 mOnComputeInternalInsetsListeners;
912 return (listeners != null && listeners.size() > 0);
916 * Calls all listeners to compute the current insets.
918 final void dispatchOnComputeInternalInsets(InternalInsetsInfo inoutInfo) {
919 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
920 // perform the dispatching. The iterator is a safe guard against listeners that
921 // could mutate the list by calling the various add/remove methods. This prevents
922 // the array from being modified while we iterate it.
923 final CopyOnWriteArray<OnComputeInternalInsetsListener> listeners =
924 mOnComputeInternalInsetsListeners;
925 if (listeners != null && listeners.size() > 0) {
926 CopyOnWriteArray.Access<OnComputeInternalInsetsListener> access = listeners.start();
928 int count = access.size();
929 for (int i = 0; i < count; i++) {
930 access.get(i).onComputeInternalInsets(inoutInfo);
939 * Copy on write array. This array is not thread safe, and only one loop can
940 * iterate over this array at any given time. This class avoids allocations
941 * until a concurrent modification happens.
945 * CopyOnWriteArray.Access<MyData> access = array.start();
947 * for (int i = 0; i < access.size(); i++) {
948 * MyData d = access.get(i);
954 static class CopyOnWriteArray<T> {
955 private ArrayList<T> mData = new ArrayList<T>();
956 private ArrayList<T> mDataCopy;
958 private final Access<T> mAccess = new Access<T>();
960 private boolean mStart;
962 static class Access<T> {
963 private ArrayList<T> mData;
967 return mData.get(index);
978 private ArrayList<T> getArray() {
980 if (mDataCopy == null) mDataCopy = new ArrayList<T>(mData);
987 if (mStart) throw new IllegalStateException("Iteration already started");
990 mAccess.mData = mData;
991 mAccess.mSize = mData.size();
996 if (!mStart) throw new IllegalStateException("Iteration not started");
998 if (mDataCopy != null) {
1000 mAccess.mData.clear();
1007 return getArray().size();
1011 getArray().add(item);
1014 void addAll(CopyOnWriteArray<T> array) {
1015 getArray().addAll(array.mData);
1018 void remove(T item) {
1019 getArray().remove(item);