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 interface ViewGroupCompatImpl { 31 public boolean onRequestSendAccessibilityEvent(ViewGroup group, View child, 32 AccessibilityEvent event); 33 34 public void setMotionEventSplittingEnabled(ViewGroup group, boolean split); 35 } 36 37 static class ViewGroupCompatStubImpl implements ViewGroupCompatImpl { 38 public boolean onRequestSendAccessibilityEvent( 39 ViewGroup group, View child, AccessibilityEvent event) { 40 return true; 41 } 42 43 public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) { 44 // no-op, didn't exist. 45 } 46 } 47 48 static class ViewGroupCompatHCImpl extends ViewGroupCompatStubImpl { 49 @Override 50 public void setMotionEventSplittingEnabled(ViewGroup group, boolean split) { 51 ViewGroupCompatHC.setMotionEventSplittingEnabled(group, split); 52 } 53 } 54 55 static class ViewGroupCompatIcsImpl extends ViewGroupCompatHCImpl { 56 @Override 57 public boolean onRequestSendAccessibilityEvent( 58 ViewGroup group, View child, AccessibilityEvent event) { 59 return ViewGroupCompatIcs.onRequestSendAccessibilityEvent(group, child, event); 60 } 61 } 62 63 static final ViewGroupCompatImpl IMPL; 64 static { 65 final int version = Build.VERSION.SDK_INT; 66 if (version >= 14) { 67 IMPL = new ViewGroupCompatIcsImpl(); 68 } else if (version >= 11) { 69 IMPL = new ViewGroupCompatHCImpl(); 70 } else { 71 IMPL = new ViewGroupCompatStubImpl(); 72 } 73 } 74 75 /* 76 * Hide the constructor. 77 */ 78 private ViewGroupCompat() { 79 80 } 81 82 /** 83 * Called when a child has requested sending an {@link AccessibilityEvent} and 84 * gives an opportunity to its parent to augment the event. 85 * <p> 86 * If an {@link AccessibilityDelegateCompat} has been specified via calling 87 * {@link ViewCompat#setAccessibilityDelegate(View, AccessibilityDelegateCompat)} its 88 * {@link AccessibilityDelegateCompat#onRequestSendAccessibilityEvent(ViewGroup, View, 89 * AccessibilityEvent)} is responsible for handling this call. 90 * </p> 91 * 92 * @param group The group whose method to invoke. 93 * @param child The child which requests sending the event. 94 * @param event The event to be sent. 95 * @return True if the event should be sent. 96 */ 97 public static boolean onRequestSendAccessibilityEvent(ViewGroup group, View child, 98 AccessibilityEvent event) { 99 return IMPL.onRequestSendAccessibilityEvent(group, child, event); 100 } 101 102 /** 103 * Enable or disable the splitting of MotionEvents to multiple children during touch event 104 * dispatch. This behavior is enabled by default for applications that target an 105 * SDK version of 11 (Honeycomb) or newer. On earlier platform versions this feature 106 * was not supported and this method is a no-op. 107 * 108 * <p>When this option is enabled MotionEvents may be split and dispatched to different child 109 * views depending on where each pointer initially went down. This allows for user interactions 110 * such as scrolling two panes of content independently, chording of buttons, and performing 111 * independent gestures on different pieces of content. 112 * 113 * @param group ViewGroup to modify 114 * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple 115 * child views. <code>false</code> to only allow one child view to be the target of 116 * any MotionEvent received by this ViewGroup. 117 */ 118 public static void setMotionEventSplittingEnabled(ViewGroup group, boolean split) { 119 IMPL.setMotionEventSplittingEnabled(group, split); 120 } 121} 122