1/* 2 * Copyright (C) 2013 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.content.Context; 20import android.os.Build; 21import android.view.View; 22import android.view.ViewParent; 23import android.view.accessibility.AccessibilityEvent; 24import android.view.accessibility.AccessibilityManager; 25 26/** 27 * Helper for accessing features in {@link ViewParent} 28 * introduced after API level 4 in a backwards compatible fashion. 29 */ 30public class ViewParentCompat { 31 32 interface ViewParentCompatImpl { 33 public boolean requestSendAccessibilityEvent( 34 ViewParent parent, View child, AccessibilityEvent event); 35 } 36 37 static class ViewParentCompatStubImpl implements ViewParentCompatImpl { 38 @Override 39 public boolean requestSendAccessibilityEvent( 40 ViewParent parent, View child, AccessibilityEvent event) { 41 // Emulate what ViewRootImpl does in ICS and above. 42 if (child == null) { 43 return false; 44 } 45 final AccessibilityManager manager = (AccessibilityManager) child.getContext() 46 .getSystemService(Context.ACCESSIBILITY_SERVICE); 47 manager.sendAccessibilityEvent(event); 48 return true; 49 } 50 } 51 52 static class ViewParentCompatICSImpl extends ViewParentCompatStubImpl { 53 @Override 54 public boolean requestSendAccessibilityEvent( 55 ViewParent parent, View child, AccessibilityEvent event) { 56 return ViewParentCompatICS.requestSendAccessibilityEvent(parent, child, event); 57 } 58 } 59 60 static final ViewParentCompatImpl IMPL; 61 static { 62 final int version = Build.VERSION.SDK_INT; 63 if (version >= 14) { 64 IMPL = new ViewParentCompatICSImpl(); 65 } else { 66 IMPL = new ViewParentCompatStubImpl(); 67 } 68 } 69 70 /* 71 * Hide the constructor. 72 */ 73 private ViewParentCompat() { 74 75 } 76 77 /** 78 * Called by a child to request from its parent to send an {@link AccessibilityEvent}. 79 * The child has already populated a record for itself in the event and is delegating 80 * to its parent to send the event. The parent can optionally add a record for itself. 81 * <p> 82 * Note: An accessibility event is fired by an individual view which populates the 83 * event with a record for its state and requests from its parent to perform 84 * the sending. The parent can optionally add a record for itself before 85 * dispatching the request to its parent. A parent can also choose not to 86 * respect the request for sending the event. The accessibility event is sent 87 * by the topmost view in the view tree.</p> 88 * 89 * @param parent The parent whose method to invoke. 90 * @param child The child which requests sending the event. 91 * @param event The event to be sent. 92 * @return True if the event was sent. 93 */ 94 public static boolean requestSendAccessibilityEvent( 95 ViewParent parent, View child, AccessibilityEvent event) { 96 return IMPL.requestSendAccessibilityEvent(parent, child, event); 97 } 98} 99