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