Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright 2018 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 
     17 package androidx.core.view;
     18 
     19 import android.os.Build;
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 import android.view.accessibility.AccessibilityEvent;
     23 
     24 import androidx.annotation.NonNull;
     25 import androidx.core.R;
     26 import androidx.core.view.ViewCompat.ScrollAxis;
     27 
     28 /**
     29  * Helper for accessing features in {@link ViewGroup}.
     30  */
     31 public final class ViewGroupCompat {
     32 
     33     /**
     34      * This constant is a {@link #setLayoutMode(ViewGroup, int) layoutMode}.
     35      * Clip bounds are the raw values of {@link android.view.View#getLeft() left},
     36      * {@link android.view.View#getTop() top},
     37      * {@link android.view.View#getRight() right} and {@link android.view.View#getBottom() bottom}.
     38      */
     39     public static final int LAYOUT_MODE_CLIP_BOUNDS = 0;
     40 
     41     /**
     42      * This constant is a {@link #setLayoutMode(ViewGroup, int) layoutMode}.
     43      * Optical bounds describe where a widget appears to be. They sit inside the clip
     44      * bounds which need to cover a larger area to allow other effects,
     45      * such as shadows and glows, to be drawn.
     46      */
     47     public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1;
     48 
     49     /*
     50      * Hide the constructor.
     51      */
     52     private ViewGroupCompat() {}
     53 
     54     /**
     55      * Called when a child has requested sending an {@link AccessibilityEvent} and
     56      * gives an opportunity to its parent to augment the event.
     57      * <p>
     58      * If an {@link AccessibilityDelegateCompat} has been specified via calling
     59      * {@link ViewCompat#setAccessibilityDelegate(View, AccessibilityDelegateCompat)} its
     60      * {@link AccessibilityDelegateCompat#onRequestSendAccessibilityEvent(ViewGroup, View,
     61      * AccessibilityEvent)} is responsible for handling this call.
     62      * </p>
     63      *
     64      * @param group The group whose method to invoke.
     65      * @param child The child which requests sending the event.
     66      * @param event The event to be sent.
     67      * @return True if the event should be sent.
     68      *
     69      * @deprecated Use {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)}
     70      * directly.
     71      */
     72     @Deprecated
     73     public static boolean onRequestSendAccessibilityEvent(ViewGroup group, View child,
     74             AccessibilityEvent event) {
     75         return group.onRequestSendAccessibilityEvent(child, event);
     76     }
     77 
     78     /**
     79      * Enable or disable the splitting of MotionEvents to multiple children during touch event
     80      * dispatch. This behavior is enabled by default for applications that target an
     81      * SDK version of 11 (Honeycomb) or newer. On earlier platform versions this feature
     82      * was not supported and this method is a no-op.
     83      *
     84      * <p>When this option is enabled MotionEvents may be split and dispatched to different child
     85      * views depending on where each pointer initially went down. This allows for user interactions
     86      * such as scrolling two panes of content independently, chording of buttons, and performing
     87      * independent gestures on different pieces of content.
     88      *
     89      * @param group ViewGroup to modify
     90      * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple
     91      *              child views. <code>false</code> to only allow one child view to be the target of
     92      *              any MotionEvent received by this ViewGroup.
     93      *
     94      * @deprecated Use {@link ViewGroup#setMotionEventSplittingEnabled(boolean)} directly.
     95      */
     96     @Deprecated
     97     public static void setMotionEventSplittingEnabled(ViewGroup group, boolean split) {
     98         group.setMotionEventSplittingEnabled(split);
     99     }
    100 
    101     /**
    102      * Returns the basis of alignment during layout operations on this ViewGroup:
    103      * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
    104      * <p>
    105      * If no layoutMode was explicitly set, either programmatically or in an XML resource,
    106      * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
    107      * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}.
    108      *
    109      * @return the layout mode to use during layout operations
    110      *
    111      * @see #setLayoutMode(ViewGroup, int)
    112      */
    113     public static int getLayoutMode(@NonNull ViewGroup group) {
    114         if (Build.VERSION.SDK_INT >= 18) {
    115             return group.getLayoutMode();
    116         }
    117         return LAYOUT_MODE_CLIP_BOUNDS;
    118     }
    119 
    120     /**
    121      * Sets the basis of alignment during the layout of this ViewGroup.
    122      * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or
    123      * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}.
    124      *
    125      * @param mode the layout mode to use during layout operations
    126      *
    127      * @see #getLayoutMode(ViewGroup)
    128      */
    129     public static void setLayoutMode(@NonNull ViewGroup group, int mode) {
    130         if (Build.VERSION.SDK_INT >= 18) {
    131             group.setLayoutMode(mode);
    132         }
    133     }
    134 
    135     /**
    136      * Changes whether or not this ViewGroup should be treated as a single entity during
    137      * Activity Transitions.
    138      * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit
    139      *                          in Activity transitions. If false, the ViewGroup won't transition,
    140      *                          only its children. If true, the entire ViewGroup will transition
    141      *                          together.
    142      */
    143     public static void setTransitionGroup(@NonNull ViewGroup group, boolean isTransitionGroup) {
    144         if (Build.VERSION.SDK_INT >= 21) {
    145             group.setTransitionGroup(isTransitionGroup);
    146         } else {
    147             group.setTag(R.id.tag_transition_group, isTransitionGroup);
    148         }
    149     }
    150 
    151     /**
    152      * Returns true if this ViewGroup should be considered as a single entity for removal
    153      * when executing an Activity transition. If this is false, child elements will move
    154      * individually during the transition.
    155      */
    156     public static boolean isTransitionGroup(@NonNull ViewGroup group) {
    157         if (Build.VERSION.SDK_INT >= 21) {
    158             return group.isTransitionGroup();
    159         }
    160         Boolean explicit = (Boolean) group.getTag(R.id.tag_transition_group);
    161         return (explicit != null && explicit)
    162                 || group.getBackground() != null
    163                 || ViewCompat.getTransitionName(group) != null;
    164     }
    165 
    166     /**
    167      * Return the current axes of nested scrolling for this ViewGroup.
    168      *
    169      * <p>A ViewGroup returning something other than {@link ViewCompat#SCROLL_AXIS_NONE} is
    170      * currently acting as a nested scrolling parent for one or more descendant views in
    171      * the hierarchy.</p>
    172      *
    173      * @return Flags indicating the current axes of nested scrolling
    174      * @see ViewCompat#SCROLL_AXIS_HORIZONTAL
    175      * @see ViewCompat#SCROLL_AXIS_VERTICAL
    176      * @see ViewCompat#SCROLL_AXIS_NONE
    177      */
    178     @ScrollAxis
    179     @SuppressWarnings("RedundantCast") // Intentionally invoking interface method.
    180     public static int getNestedScrollAxes(@NonNull ViewGroup group) {
    181         if (Build.VERSION.SDK_INT >= 21) {
    182             return group.getNestedScrollAxes();
    183         }
    184         if (group instanceof NestedScrollingParent) {
    185             return ((NestedScrollingParent) group).getNestedScrollAxes();
    186         }
    187         return ViewCompat.SCROLL_AXIS_NONE;
    188     }
    189 }
    190