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 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        public boolean onRequestSendAccessibilityEvent(ViewGroup group, View child,
48                AccessibilityEvent event);
49
50        public void setMotionEventSplittingEnabled(ViewGroup group, boolean split);
51        public int getLayoutMode(ViewGroup group);
52        public void setLayoutMode(ViewGroup group, int mode);
53        public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup);
54        public boolean isTransitionGroup(ViewGroup group);
55    }
56
57    static class ViewGroupCompatStubImpl implements ViewGroupCompatImpl {
58        public boolean onRequestSendAccessibilityEvent(
59                ViewGroup group, View child, AccessibilityEvent event) {
60            return true;
61        }
62
63        public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
64            // no-op, didn't exist.
65        }
66
67        @Override
68        public int getLayoutMode(ViewGroup group) {
69            return LAYOUT_MODE_CLIP_BOUNDS;
70        }
71
72        @Override
73        public void setLayoutMode(ViewGroup group, int mode) {
74            // no-op, didn't exist. Views only support clip bounds.
75        }
76
77        @Override
78        public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
79        }
80
81        @Override
82        public boolean isTransitionGroup(ViewGroup group) {
83            return false;
84        }
85    }
86
87    static class ViewGroupCompatHCImpl extends ViewGroupCompatStubImpl {
88        @Override
89        public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
90            ViewGroupCompatHC.setMotionEventSplittingEnabled(group, split);
91        }
92    }
93
94    static class ViewGroupCompatIcsImpl extends ViewGroupCompatHCImpl {
95        @Override
96        public boolean onRequestSendAccessibilityEvent(
97                ViewGroup group, View child, AccessibilityEvent event) {
98            return ViewGroupCompatIcs.onRequestSendAccessibilityEvent(group, child, event);
99        }
100    }
101
102    static class ViewGroupCompatJellybeanMR2Impl extends ViewGroupCompatIcsImpl {
103        @Override
104        public int getLayoutMode(ViewGroup group) {
105            return ViewGroupCompatJellybeanMR2.getLayoutMode(group);
106        }
107
108        @Override
109        public void setLayoutMode(ViewGroup group, int mode) {
110            ViewGroupCompatJellybeanMR2.setLayoutMode(group, mode);
111        }
112    }
113
114    static class ViewGroupCompatApi21Impl extends ViewGroupCompatJellybeanMR2Impl {
115        @Override
116        public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
117            ViewGroupCompatApi21.setTransitionGroup(group, isTransitionGroup);
118        }
119
120        @Override
121        public boolean isTransitionGroup(ViewGroup group) {
122            return ViewGroupCompatApi21.isTransitionGroup(group);
123        }
124    }
125
126    static final ViewGroupCompatImpl IMPL;
127    static {
128        final int version = Build.VERSION.SDK_INT;
129        if (version >= 21) {
130            IMPL = new ViewGroupCompatApi21Impl();
131        } else if (version >= 18) {
132            IMPL = new ViewGroupCompatJellybeanMR2Impl();
133        } else if (version >= 14) {
134            IMPL = new ViewGroupCompatIcsImpl();
135        } else if (version >= 11) {
136            IMPL = new ViewGroupCompatHCImpl();
137        } else {
138            IMPL = new ViewGroupCompatStubImpl();
139        }
140    }
141
142    /*
143     * Hide the constructor.
144     */
145    private ViewGroupCompat() {
146
147    }
148
149    /**
150     * Called when a child has requested sending an {@link AccessibilityEvent} and
151     * gives an opportunity to its parent to augment the event.
152     * <p>
153     * If an {@link AccessibilityDelegateCompat} has been specified via calling
154     * {@link ViewCompat#setAccessibilityDelegate(View, AccessibilityDelegateCompat)} its
155     * {@link AccessibilityDelegateCompat#onRequestSendAccessibilityEvent(ViewGroup, View,
156     * AccessibilityEvent)} is responsible for handling this call.
157     * </p>
158     *
159     * @param group The group whose method to invoke.
160     * @param child The child which requests sending the event.
161     * @param event The event to be sent.
162     * @return True if the event should be sent.
163     */
164    public static boolean onRequestSendAccessibilityEvent(ViewGroup group, View child,
165            AccessibilityEvent event) {
166        return IMPL.onRequestSendAccessibilityEvent(group, child, event);
167    }
168
169    /**
170     * Enable or disable the splitting of MotionEvents to multiple children during touch event
171     * dispatch. This behavior is enabled by default for applications that target an
172     * SDK version of 11 (Honeycomb) or newer. On earlier platform versions this feature
173     * was not supported and this method is a no-op.
174     *
175     * <p>When this option is enabled MotionEvents may be split and dispatched to different child
176     * views depending on where each pointer initially went down. This allows for user interactions
177     * such as scrolling two panes of content independently, chording of buttons, and performing
178     * independent gestures on different pieces of content.
179     *
180     * @param group ViewGroup to modify
181     * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
182     *              child views. <code>false</code> to only allow one child view to be the target of
183     *              any MotionEvent received by this ViewGroup.
184     */
185    public static void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
186        IMPL.setMotionEventSplittingEnabled(group, split);
187    }
188
189    /**
190     * Returns the basis of alignment during layout operations on this ViewGroup:
191     * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
192     * <p>
193     * If no layoutMode was explicitly set, either programmatically or in an XML resource,
194     * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
195     * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
196     *
197     * @return the layout mode to use during layout operations
198     *
199     * @see #setLayoutMode(ViewGroup, int)
200     */
201    public static int getLayoutMode(ViewGroup group) {
202        return IMPL.getLayoutMode(group);
203    }
204
205    /**
206     * Sets the basis of alignment during the layout of this ViewGroup.
207     * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
208     * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
209     *
210     * @param mode the layout mode to use during layout operations
211     *
212     * @see #getLayoutMode(ViewGroup)
213     */
214    public static void setLayoutMode(ViewGroup group, int mode) {
215        IMPL.setLayoutMode(group, mode);
216    }
217
218    /**
219     * Changes whether or not this ViewGroup should be treated as a single entity during
220     * Activity Transitions.
221     * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
222     *                          in Activity transitions. If false, the ViewGroup won't transition,
223     *                          only its children. If true, the entire ViewGroup will transition
224     *                          together.
225     */
226    public static void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) {
227        IMPL.setTransitionGroup(group, isTransitionGroup);
228    }
229
230    /**
231     * Returns true if this ViewGroup should be considered as a single entity for removal
232     * when executing an Activity transition. If this is false, child elements will move
233     * individually during the transition.
234     */
235    public static boolean isTransitionGroup(ViewGroup group) {
236        return IMPL.isTransitionGroup(group);
237    }
238}
239