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 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 @Override 87 public int getNestedScrollAxes(ViewGroup group) { 88 if (group instanceof NestedScrollingParent) { 89 return ((NestedScrollingParent) group).getNestedScrollAxes(); 90 } 91 return ViewCompat.SCROLL_AXIS_NONE; 92 } 93 } 94 95 static class ViewGroupCompatHCImpl extends ViewGroupCompatStubImpl { 96 @Override 97 public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) { 98 ViewGroupCompatHC.setMotionEventSplittingEnabled(group, split); 99 } 100 } 101 102 static class ViewGroupCompatIcsImpl extends ViewGroupCompatHCImpl { 103 @Override 104 public boolean onRequestSendAccessibilityEvent( 105 ViewGroup group, View child, AccessibilityEvent event) { 106 return ViewGroupCompatIcs.onRequestSendAccessibilityEvent(group, child, event); 107 } 108 } 109 110 static class ViewGroupCompatJellybeanMR2Impl extends ViewGroupCompatIcsImpl { 111 @Override 112 public int getLayoutMode(ViewGroup group) { 113 return ViewGroupCompatJellybeanMR2.getLayoutMode(group); 114 } 115 116 @Override 117 public void setLayoutMode(ViewGroup group, int mode) { 118 ViewGroupCompatJellybeanMR2.setLayoutMode(group, mode); 119 } 120 } 121 122 static class ViewGroupCompatLollipopImpl extends ViewGroupCompatJellybeanMR2Impl { 123 @Override 124 public void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) { 125 ViewGroupCompatLollipop.setTransitionGroup(group, isTransitionGroup); 126 } 127 128 @Override 129 public boolean isTransitionGroup(ViewGroup group) { 130 return ViewGroupCompatLollipop.isTransitionGroup(group); 131 } 132 133 @Override 134 public int getNestedScrollAxes(ViewGroup group) { 135 return ViewGroupCompatLollipop.getNestedScrollAxes(group); 136 } 137 } 138 139 static final ViewGroupCompatImpl IMPL; 140 static { 141 final int version = Build.VERSION.SDK_INT; 142 if (version >= 21) { 143 IMPL = new ViewGroupCompatLollipopImpl(); 144 } else if (version >= 18) { 145 IMPL = new ViewGroupCompatJellybeanMR2Impl(); 146 } else if (version >= 14) { 147 IMPL = new ViewGroupCompatIcsImpl(); 148 } else if (version >= 11) { 149 IMPL = new ViewGroupCompatHCImpl(); 150 } else { 151 IMPL = new ViewGroupCompatStubImpl(); 152 } 153 } 154 155 /* 156 * Hide the constructor. 157 */ 158 private ViewGroupCompat() {} 159 160 /** 161 * Called when a child has requested sending an {@link AccessibilityEvent} and 162 * gives an opportunity to its parent to augment the event. 163 * <p> 164 * If an {@link AccessibilityDelegateCompat} has been specified via calling 165 * {@link ViewCompat#setAccessibilityDelegate(View, AccessibilityDelegateCompat)} its 166 * {@link AccessibilityDelegateCompat#onRequestSendAccessibilityEvent(ViewGroup, View, 167 * AccessibilityEvent)} is responsible for handling this call. 168 * </p> 169 * 170 * @param group The group whose method to invoke. 171 * @param child The child which requests sending the event. 172 * @param event The event to be sent. 173 * @return True if the event should be sent. 174 */ 175 public static boolean onRequestSendAccessibilityEvent(ViewGroup group, View child, 176 AccessibilityEvent event) { 177 return IMPL.onRequestSendAccessibilityEvent(group, child, event); 178 } 179 180 /** 181 * Enable or disable the splitting of MotionEvents to multiple children during touch event 182 * dispatch. This behavior is enabled by default for applications that target an 183 * SDK version of 11 (Honeycomb) or newer. On earlier platform versions this feature 184 * was not supported and this method is a no-op. 185 * 186 * <p>When this option is enabled MotionEvents may be split and dispatched to different child 187 * views depending on where each pointer initially went down. This allows for user interactions 188 * such as scrolling two panes of content independently, chording of buttons, and performing 189 * independent gestures on different pieces of content. 190 * 191 * @param group ViewGroup to modify 192 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple 193 * child views. <code>false</code> to only allow one child view to be the target of 194 * any MotionEvent received by this ViewGroup. 195 */ 196 public static void setMotionEventSplittingEnabled(ViewGroup group, boolean split) { 197 IMPL.setMotionEventSplittingEnabled(group, split); 198 } 199 200 /** 201 * Returns the basis of alignment during layout operations on this ViewGroup: 202 * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. 203 * <p> 204 * If no layoutMode was explicitly set, either programmatically or in an XML resource, 205 * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, 206 * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}. 207 * 208 * @return the layout mode to use during layout operations 209 * 210 * @see #setLayoutMode(ViewGroup, int) 211 */ 212 public static int getLayoutMode(ViewGroup group) { 213 return IMPL.getLayoutMode(group); 214 } 215 216 /** 217 * Sets the basis of alignment during the layout of this ViewGroup. 218 * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or 219 * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. 220 * 221 * @param mode the layout mode to use during layout operations 222 * 223 * @see #getLayoutMode(ViewGroup) 224 */ 225 public static void setLayoutMode(ViewGroup group, int mode) { 226 IMPL.setLayoutMode(group, mode); 227 } 228 229 /** 230 * Changes whether or not this ViewGroup should be treated as a single entity during 231 * Activity Transitions. 232 * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit 233 * in Activity transitions. If false, the ViewGroup won't transition, 234 * only its children. If true, the entire ViewGroup will transition 235 * together. 236 */ 237 public static void setTransitionGroup(ViewGroup group, boolean isTransitionGroup) { 238 IMPL.setTransitionGroup(group, isTransitionGroup); 239 } 240 241 /** 242 * Returns true if this ViewGroup should be considered as a single entity for removal 243 * when executing an Activity transition. If this is false, child elements will move 244 * individually during the transition. 245 */ 246 public static boolean isTransitionGroup(ViewGroup group) { 247 return IMPL.isTransitionGroup(group); 248 } 249 250 /** 251 * Return the current axes of nested scrolling for this ViewGroup. 252 * 253 * <p>A ViewGroup returning something other than {@link ViewCompat#SCROLL_AXIS_NONE} is 254 * currently acting as a nested scrolling parent for one or more descendant views in 255 * the hierarchy.</p> 256 * 257 * @return Flags indicating the current axes of nested scrolling 258 * @see ViewCompat#SCROLL_AXIS_HORIZONTAL 259 * @see ViewCompat#SCROLL_AXIS_VERTICAL 260 * @see ViewCompat#SCROLL_AXIS_NONE 261 */ 262 public static int getNestedScrollAxes(ViewGroup group) { 263 return IMPL.getNestedScrollAxes(group); 264 } 265} 266