ViewGroupCompat.java revision 15375aa6fd54b036f97f99229aefab2822c8a1c9
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 android.support.v4.view;
18
19import android.os.Build;
20import android.view.View;
21import android.view.ViewGroup;
22import android.view.accessibility.AccessibilityEvent;
23
24/**
25 * Helper for accessing features in {@link ViewGroup}
26 * introduced after API level 4 in a backwards compatible fashion.
27 */
28public final class ViewGroupCompat {
29
30    /**
31     * This constant is a {@link #setLayoutMode(ViewGroup, int) layoutMode}.
32     * Clip bounds are the raw values of {@link android.view.View#getLeft() left},
33     * {@link android.view.View#getTop() top},
34     * {@link android.view.View#getRight() right} and {@link android.view.View#getBottom() bottom}.
35     */
36    public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
37
38    /**
39     * This constant is a {@link #setLayoutMode(ViewGroup, int) layoutMode}.
40     * Optical bounds describe where a widget appears to be. They sit inside the clip
41     * bounds which need to cover a larger area to allow other effects,
42     * such as shadows and glows, to be drawn.
43     */
44    public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
45
46    interface ViewGroupCompatImpl {
47        boolean onRequestSendAccessibilityEvent(ViewGroup group, View child,
48                AccessibilityEvent event);
49        void setMotionEventSplittingEnabled(ViewGroup group, boolean split);
50        int getLayoutMode(ViewGroup group);
51        void setLayoutMode(ViewGroup group, int mode);
52        void setTransitionGroup(ViewGroup group, boolean isTransitionGroup);
53        boolean isTransitionGroup(ViewGroup group);
54        int getNestedScrollAxes(ViewGroup group);
55    }
56
57    static class ViewGroupCompatStubImpl implements ViewGroupCompatImpl {
58        @Override
59        public boolean onRequestSendAccessibilityEvent(
60                ViewGroup group, View child, AccessibilityEvent event) {
61            return true;
62        }
63
64        @Override
65        public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
66            // no-op, didn't exist.
67        }
68
69        @Override
70        public int getLayoutMode(ViewGroup group) {
71            return LAYOUT_MODE_CLIP_BOUNDS;
72        }
73
74        @Override
75        public void setLayoutMode(ViewGroup group, int mode) {
76            // no-op, didn't exist. Views only support clip bounds.
77        }
78
79        @Override
80        public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
81        }
82
83        @Override
84        public boolean isTransitionGroup(ViewGroup group) {
85            return false;
86        }
87
88        @Override
89        public int getNestedScrollAxes(ViewGroup group) {
90            if (group instanceof NestedScrollingParent) {
91                return ((NestedScrollingParent) group).getNestedScrollAxes();
92            }
93            return ViewCompat.SCROLL_AXIS_NONE;
94        }
95    }
96
97    static class ViewGroupCompatHCImpl extends ViewGroupCompatStubImpl {
98        @Override
99        public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
100            ViewGroupCompatHC.setMotionEventSplittingEnabled(group, split);
101        }
102    }
103
104    static class ViewGroupCompatIcsImpl extends ViewGroupCompatHCImpl {
105        @Override
106        public boolean onRequestSendAccessibilityEvent(
107                ViewGroup group, View child, AccessibilityEvent event) {
108            return ViewGroupCompatIcs.onRequestSendAccessibilityEvent(group, child, event);
109        }
110    }
111
112    static class ViewGroupCompatJellybeanMR2Impl extends ViewGroupCompatIcsImpl {
113        @Override
114        public int getLayoutMode(ViewGroup group) {
115            return ViewGroupCompatJellybeanMR2.getLayoutMode(group);
116        }
117
118        @Override
119        public void setLayoutMode(ViewGroup group, int mode) {
120            ViewGroupCompatJellybeanMR2.setLayoutMode(group, mode);
121        }
122    }
123
124    static class ViewGroupCompatLollipopImpl extends ViewGroupCompatJellybeanMR2Impl {
125        @Override
126        public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
127            ViewGroupCompatLollipop.setTransitionGroup(group, isTransitionGroup);
128        }
129
130        @Override
131        public boolean isTransitionGroup(ViewGroup group) {
132            return ViewGroupCompatLollipop.isTransitionGroup(group);
133        }
134
135        @Override
136        public int getNestedScrollAxes(ViewGroup group) {
137            return ViewGroupCompatLollipop.getNestedScrollAxes(group);
138        }
139    }
140
141    static final ViewGroupCompatImpl IMPL;
142    static {
143        final int version = Build.VERSION.SDK_INT;
144        if (version >= 21) {
145            IMPL = new ViewGroupCompatLollipopImpl();
146        } else if (version >= 18) {
147            IMPL = new ViewGroupCompatJellybeanMR2Impl();
148        } else if (version >= 14) {
149            IMPL = new ViewGroupCompatIcsImpl();
150        } else if (version >= 11) {
151            IMPL = new ViewGroupCompatHCImpl();
152        } else {
153            IMPL = new ViewGroupCompatStubImpl();
154        }
155    }
156
157    /*
158     * Hide the constructor.
159     */
160    private ViewGroupCompat() {}
161
162    /**
163     * Called when a child has requested sending an {@link AccessibilityEvent} and
164     * gives an opportunity to its parent to augment the event.
165     * <p>
166     * If an {@link AccessibilityDelegateCompat} has been specified via calling
167     * {@link ViewCompat#setAccessibilityDelegate(View, AccessibilityDelegateCompat)} its
168     * {@link AccessibilityDelegateCompat#onRequestSendAccessibilityEvent(ViewGroup, View,
169     * AccessibilityEvent)} is responsible for handling this call.
170     * </p>
171     *
172     * @param group The group whose method to invoke.
173     * @param child The child which requests sending the event.
174     * @param event The event to be sent.
175     * @return True if the event should be sent.
176     */
177    public static boolean onRequestSendAccessibilityEvent(ViewGroup group, View child,
178            AccessibilityEvent event) {
179        return IMPL.onRequestSendAccessibilityEvent(group, child, event);
180    }
181
182    /**
183     * Enable or disable the splitting of MotionEvents to multiple children during touch event
184     * dispatch. This behavior is enabled by default for applications that target an
185     * SDK version of 11 (Honeycomb) or newer. On earlier platform versions this feature
186     * was not supported and this method is a no-op.
187     *
188     * <p>When this option is enabled MotionEvents may be split and dispatched to different child
189     * views depending on where each pointer initially went down. This allows for user interactions
190     * such as scrolling two panes of content independently, chording of buttons, and performing
191     * independent gestures on different pieces of content.
192     *
193     * @param group ViewGroup to modify
194     * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
195     *              child views. <code>false</code> to only allow one child view to be the target of
196     *              any MotionEvent received by this ViewGroup.
197     */
198    public static void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
199        IMPL.setMotionEventSplittingEnabled(group, split);
200    }
201
202    /**
203     * Returns the basis of alignment during layout operations on this ViewGroup:
204     * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
205     * <p>
206     * If no layoutMode was explicitly set, either programmatically or in an XML resource,
207     * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
208     * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
209     *
210     * @return the layout mode to use during layout operations
211     *
212     * @see #setLayoutMode(ViewGroup, int)
213     */
214    public static int getLayoutMode(ViewGroup group) {
215        return IMPL.getLayoutMode(group);
216    }
217
218    /**
219     * Sets the basis of alignment during the layout of this ViewGroup.
220     * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
221     * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
222     *
223     * @param mode the layout mode to use during layout operations
224     *
225     * @see #getLayoutMode(ViewGroup)
226     */
227    public static void setLayoutMode(ViewGroup group, int mode) {
228        IMPL.setLayoutMode(group, mode);
229    }
230
231    /**
232     * Changes whether or not this ViewGroup should be treated as a single entity during
233     * Activity Transitions.
234     * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
235     *                          in Activity transitions. If false, the ViewGroup won't transition,
236     *                          only its children. If true, the entire ViewGroup will transition
237     *                          together.
238     */
239    public static void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
240        IMPL.setTransitionGroup(group, isTransitionGroup);
241    }
242
243    /**
244     * Returns true if this ViewGroup should be considered as a single entity for removal
245     * when executing an Activity transition. If this is false, child elements will move
246     * individually during the transition.
247     */
248    public static boolean isTransitionGroup(ViewGroup group) {
249        return IMPL.isTransitionGroup(group);
250    }
251
252    /**
253     * Return the current axes of nested scrolling for this ViewGroup.
254     *
255     * <p>A ViewGroup returning something other than {@link ViewCompat#SCROLL_AXIS_NONE} is
256     * currently acting as a nested scrolling parent for one or more descendant views in
257     * the hierarchy.</p>
258     *
259     * @return Flags indicating the current axes of nested scrolling
260     * @see ViewCompat#SCROLL_AXIS_HORIZONTAL
261     * @see ViewCompat#SCROLL_AXIS_VERTICAL
262     * @see ViewCompat#SCROLL_AXIS_NONE
263     */
264    public static int getNestedScrollAxes(ViewGroup group) {
265        return IMPL.getNestedScrollAxes(group);
266    }
267}
268