Java Code Examples for androidx.core.view.ViewCompat#setImportantForAccessibility()

The following examples show how to use androidx.core.view.ViewCompat#setImportantForAccessibility() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnPopulateAccessibilityNode
static void onPopulateAccessibilityNode(
    View host,
    AccessibilityNodeInfoCompat node,
    @Prop(resType = ResType.STRING) CharSequence text,
    @Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine) {
  if (ViewCompat.getImportantForAccessibility(host)
      == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
    ViewCompat.setImportantForAccessibility(host, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
  }
  CharSequence contentDescription = node.getContentDescription();
  node.setText(contentDescription != null ? contentDescription : text);
  node.setContentDescription(contentDescription != null ? contentDescription : text);

  node.addAction(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
  node.addAction(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
  node.setMovementGranularities(
      AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER
          | AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD
          | AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_PARAGRAPH);

  if (!isSingleLine) {
    node.setMultiLine(true);
  }
}
 
Example 2
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
private void attrHandler(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    final Resources res = context.getResources();
    mDesiredMonthHeight = res.getDimensionPixelSize(R.dimen.date_picker_month_height);
    mDesiredDayOfWeekHeight = res.getDimensionPixelSize(R.dimen.date_picker_day_of_week_height);
    mDesiredDayHeight = res.getDimensionPixelSize(R.dimen.date_picker_day_height);
    mDesiredCellWidth = res.getDimensionPixelSize(R.dimen.date_picker_day_width);
    mDesiredDaySelectorRadius = res.getDimensionPixelSize(
            R.dimen.date_picker_day_selector_radius);

    // Set up accessibility components.
    mTouchHelper = new MonthViewTouchHelper(this);
    ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
    ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    mLocale = res.getConfiguration().locale;
    mCalendar = Calendar.getInstance(mLocale);

    mDayFormatter = NumberFormat.getIntegerInstance(mLocale);

    updateMonthYearLabel();
    updateDayOfWeekLabels();

    initPaints(res);
}
 
Example 3
Source File: ChipGroup.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public ChipGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  TypedArray a =
      ThemeEnforcement.obtainStyledAttributes(
          context,
          attrs,
          R.styleable.ChipGroup,
          defStyleAttr,
          DEF_STYLE_RES);

  int chipSpacing = a.getDimensionPixelOffset(R.styleable.ChipGroup_chipSpacing, 0);
  setChipSpacingHorizontal(
      a.getDimensionPixelOffset(R.styleable.ChipGroup_chipSpacingHorizontal, chipSpacing));
  setChipSpacingVertical(
      a.getDimensionPixelOffset(R.styleable.ChipGroup_chipSpacingVertical, chipSpacing));
  setSingleLine(a.getBoolean(R.styleable.ChipGroup_singleLine, false));
  setSingleSelection(a.getBoolean(R.styleable.ChipGroup_singleSelection, false));
  setSelectionRequired(a.getBoolean(R.styleable.ChipGroup_selectionRequired, false));
  int checkedChip = a.getResourceId(R.styleable.ChipGroup_checkedChip, View.NO_ID);
  if (checkedChip != View.NO_ID) {
    checkedId = checkedChip;
  }

  a.recycle();
  super.setOnHierarchyChangeListener(passThroughListener);

  ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
 
Example 4
Source File: TextInputLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private static void setIconClickable(
    @NonNull CheckableImageButton iconView, @Nullable OnLongClickListener onLongClickListener) {
  boolean iconClickable = ViewCompat.hasOnClickListeners(iconView);
  boolean iconLongClickable = onLongClickListener != null;
  boolean iconFocusable = iconClickable || iconLongClickable;
  iconView.setFocusable(iconFocusable);
  iconView.setClickable(iconClickable);
  iconView.setPressable(iconClickable);
  iconView.setLongClickable(iconLongClickable);
  ViewCompat.setImportantForAccessibility(
      iconView,
      iconFocusable
          ? ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES
          : ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
}
 
Example 5
Source File: ComponentAccessibilityDelegate.java    From litho with Apache License 2.0 5 votes vote down vote up
ComponentAccessibilityDelegate(
    View view, NodeInfo nodeInfo, boolean originalFocus, int originalImportantForAccessibility) {
  super(view);
  mView = view;
  mNodeInfo = nodeInfo;
  mSuperDelegate = new SuperDelegate();

  // We need to reset these two properties, as ExploreByTouchHelper sets focusable to "true" and
  // importantForAccessibility to "Yes" (if it is Auto). If we don't reset these it would force
  // every element that has this delegate attached to be focusable, and not allow for
  // announcement coalescing.
  mView.setFocusable(originalFocus);
  ViewCompat.setImportantForAccessibility(mView, originalImportantForAccessibility);
}
 
Example 6
Source File: MonthView.java    From cathode with Apache License 2.0 5 votes vote down vote up
public MonthView(Context context, AttributeSet attr) {
  super(context, attr);
  Resources res = context.getResources();

  mDayLabelCalendar = Calendar.getInstance();
  mCalendar = Calendar.getInstance();

  mDayOfWeekTypeface = res.getString(R.string.day_of_week_label_typeface);
  mMonthTitleTypeface = res.getString(R.string.sans_serif);

  mDayTextColor = res.getColor(R.color.date_picker_day);
  mTodayNumberColor = res.getColor(R.color.blue);
  mDisabledDayTextColor = res.getColor(R.color.date_picker_text_disabled);
  mMonthTitleColor = res.getColor(android.R.color.white);
  mMonthTitleBGColor = res.getColor(R.color.circle_background);
  mMonthDayLabelColor = res.getColor(R.color.date_picker_weekday);

  mStringBuilder = new StringBuilder(50);
  mFormatter = new Formatter(mStringBuilder, Locale.getDefault());

  MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.day_number_size);
  MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.month_label_size);
  MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.month_day_label_text_size);
  MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.month_list_item_header_height);
  DAY_SELECTED_CIRCLE_SIZE = res.getDimensionPixelSize(R.dimen.day_number_select_circle_radius);

  mRowHeight = (res.getDimensionPixelOffset(R.dimen.date_picker_view_animator_height)
      - getMonthHeaderSize()) / MAX_NUM_ROWS;

  // Set up accessibility components.
  mTouchHelper = getMonthViewTouchHelper();
  ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
  ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
  mLockAccessibilityDelegate = true;

  // Sets up any standard paints that will be used
  initView();
}
 
Example 7
Source File: SwipeDismissBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
  boolean handled = super.onLayoutChild(parent, child, layoutDirection);
  if (ViewCompat.getImportantForAccessibility(child)
      == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
    ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    updateAccessibilityActions(child);
  }
  return handled;
}
 
Example 8
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public BottomNavigationMenuView(Context context, AttributeSet attrs) {
  super(context, attrs);
  final Resources res = getResources();
  inactiveItemMaxWidth =
      res.getDimensionPixelSize(R.dimen.design_bottom_navigation_item_max_width);
  inactiveItemMinWidth =
      res.getDimensionPixelSize(R.dimen.design_bottom_navigation_item_min_width);
  activeItemMaxWidth =
      res.getDimensionPixelSize(R.dimen.design_bottom_navigation_active_item_max_width);
  activeItemMinWidth =
      res.getDimensionPixelSize(R.dimen.design_bottom_navigation_active_item_min_width);
  itemHeight = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_height);
  itemTextColorDefault = createDefaultColorStateList(android.R.attr.textColorSecondary);

  set = new AutoTransition();
  set.setOrdering(TransitionSet.ORDERING_TOGETHER);
  set.setDuration(ACTIVE_ANIMATION_DURATION_MS);
  set.setInterpolator(new FastOutSlowInInterpolator());
  set.addTransition(new TextScale());

  onClickListener =
      new OnClickListener() {
        @Override
        public void onClick(View v) {
          final BottomNavigationItemView itemView = (BottomNavigationItemView) v;
          MenuItem item = itemView.getItemData();
          if (!menu.performItemAction(item, presenter, 0)) {
            item.setChecked(true);
          }
        }
      };
  tempChildWidths = new int[BottomNavigationMenu.MAX_ITEM_COUNT];

  ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
 
Example 9
Source File: VerticalViewPager.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mTopEdge = new EdgeEffectCompat(context);
    mBottomEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }
}
 
Example 10
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void updateImportantForAccessibility(boolean expanded) {
    if (viewRef == null) {
        return;
    }

    ViewParent viewParent = viewRef.get().getParent();
    if (!(viewParent instanceof CoordinatorLayout)) {
        return;
    }

    CoordinatorLayout parent = (CoordinatorLayout) viewParent;
    final int childCount = parent.getChildCount();
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && expanded) {
        if (importantForAccessibilityMap == null) {
            importantForAccessibilityMap = new HashMap<>(childCount);
        } else {
            // The important for accessibility values of the child views have been saved already.
            return;
        }
    }

    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if (child == viewRef.get()) {
            continue;
        }

        if (expanded) {
            // Saves the important for accessibility value of the child view.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                importantForAccessibilityMap.put(child, child.getImportantForAccessibility());
            }
            if (updateImportantForAccessibilityOnSiblings) {
                ViewCompat.setImportantForAccessibility(
                        child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
            }
        } else {
            if (updateImportantForAccessibilityOnSiblings
                    && importantForAccessibilityMap != null
                    && importantForAccessibilityMap.containsKey(child)) {
                // Restores the original important for accessibility value of the child view.
                ViewCompat.setImportantForAccessibility(child, importantForAccessibilityMap.get(child));
            }
        }
    }

    if (!expanded) {
        importantForAccessibilityMap = null;
    }
}
 
Example 11
Source File: MonthView.java    From MaterialDateTimePicker with Apache License 2.0 4 votes vote down vote up
public MonthView(Context context, AttributeSet attr, DatePickerController controller) {
    super(context, attr);
    mController = controller;
    Resources res = context.getResources();

    mDayLabelCalendar = Calendar.getInstance(mController.getTimeZone(), mController.getLocale());
    mCalendar = Calendar.getInstance(mController.getTimeZone(), mController.getLocale());

    mDayOfWeekTypeface = res.getString(R.string.mdtp_day_of_week_label_typeface);
    mMonthTitleTypeface = res.getString(R.string.mdtp_sans_serif);

    boolean darkTheme = mController != null && mController.isThemeDark();
    if (darkTheme) {
        mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal_dark_theme);
        mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day_dark_theme);
        mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled_dark_theme);
        mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted_dark_theme);
    } else {
        mDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_normal);
        mMonthDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_month_day);
        mDisabledDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_disabled);
        mHighlightedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_date_picker_text_highlighted);
    }
    mSelectedDayTextColor = ContextCompat.getColor(context, R.color.mdtp_white);
    mTodayNumberColor = mController.getAccentColor();
    mMonthTitleColor = ContextCompat.getColor(context, R.color.mdtp_white);

    mStringBuilder = new StringBuilder(50);

    MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size);
    MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size);
    MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size);
    MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height);
    MONTH_HEADER_SIZE_V2 = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height_v2);
    DAY_SELECTED_CIRCLE_SIZE = mController.getVersion() == DatePickerDialog.Version.VERSION_1
            ? res.getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius)
            : res.getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius_v2);
    DAY_HIGHLIGHT_CIRCLE_SIZE = res
            .getDimensionPixelSize(R.dimen.mdtp_day_highlight_circle_radius);
    DAY_HIGHLIGHT_CIRCLE_MARGIN = res
            .getDimensionPixelSize(R.dimen.mdtp_day_highlight_circle_margin);

    if (mController.getVersion() == DatePickerDialog.Version.VERSION_1) {
        mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height)
                - getMonthHeaderSize()) / MAX_NUM_ROWS;
    } else {
        mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height_v2)
                - getMonthHeaderSize() - MONTH_DAY_LABEL_TEXT_SIZE * 2) / MAX_NUM_ROWS;
    }

    mEdgePadding = mController.getVersion() == DatePickerDialog.Version.VERSION_1
            ? 0
            : context.getResources().getDimensionPixelSize(R.dimen.mdtp_date_picker_view_animator_padding_v2);

    // Set up accessibility components.
    mTouchHelper = getMonthViewTouchHelper();
    ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
    ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    mLockAccessibilityDelegate = true;

    // Sets up any standard paints that will be used
    initView();
}
 
Example 12
Source File: BottomNavigationItemView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public BottomNavigationItemView(
    @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  final Resources res = getResources();

  LayoutInflater.from(context).inflate(R.layout.design_bottom_navigation_item, this, true);
  setBackgroundResource(R.drawable.design_bottom_navigation_item_background);
  defaultMargin = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_margin);

  icon = findViewById(R.id.icon);
  smallLabel = findViewById(R.id.smallLabel);
  largeLabel = findViewById(R.id.largeLabel);
  // The labels used aren't always visible, so they are unreliable for accessibility. Instead,
  // the content description of the BottomNavigationItemView should be used for accessibility.
  ViewCompat.setImportantForAccessibility(smallLabel, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
  ViewCompat.setImportantForAccessibility(largeLabel, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
  setFocusable(true);
  calculateTextScaleFactors(smallLabel.getTextSize(), largeLabel.getTextSize());

  // TODO(b/138148581): Support displaying a badge on label-only bottom navigation views.
  if (icon != null) {
    icon.addOnLayoutChangeListener(
        new OnLayoutChangeListener() {
          @Override
          public void onLayoutChange(
              View v,
              int left,
              int top,
              int right,
              int bottom,
              int oldLeft,
              int oldTop,
              int oldRight,
              int oldBottom) {
            if (icon.getVisibility() == VISIBLE) {
              tryUpdateBadgeBounds(icon);
            }
          }
        });
  }
}
 
Example 13
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void updateImportantForAccessibility(boolean expanded) {
  if (viewRef == null) {
    return;
  }

  ViewParent viewParent = viewRef.get().getParent();
  if (!(viewParent instanceof CoordinatorLayout)) {
    return;
  }

  CoordinatorLayout parent = (CoordinatorLayout) viewParent;
  final int childCount = parent.getChildCount();
  if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && expanded) {
    if (importantForAccessibilityMap == null) {
      importantForAccessibilityMap = new HashMap<>(childCount);
    } else {
      // The important for accessibility values of the child views have been saved already.
      return;
    }
  }

  for (int i = 0; i < childCount; i++) {
    final View child = parent.getChildAt(i);
    if (child == viewRef.get()) {
      continue;
    }

    if (expanded) {
      // Saves the important for accessibility value of the child view.
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        importantForAccessibilityMap.put(child, child.getImportantForAccessibility());
      }
      if (updateImportantForAccessibilityOnSiblings) {
        ViewCompat.setImportantForAccessibility(
            child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
      }
    } else {
      if (updateImportantForAccessibilityOnSiblings
          && importantForAccessibilityMap != null
          && importantForAccessibilityMap.containsKey(child)) {
        // Restores the original important for accessibility value of the child view.
        ViewCompat.setImportantForAccessibility(child, importantForAccessibilityMap.get(child));
      }
    }
  }

  if (!expanded) {
    importantForAccessibilityMap = null;
  }
}
 
Example 14
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
  if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
    child.setFitsSystemWindows(true);
  }

  if (viewRef == null) {
    // First layout with this behavior.
    peekHeightMin =
        parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
    setSystemGestureInsets(parent);
    viewRef = new WeakReference<>(child);
    // Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
    // default to android:background declared in styles or layout.
    if (shapeThemingEnabled && materialShapeDrawable != null) {
      ViewCompat.setBackground(child, materialShapeDrawable);
    }
    // Set elevation on MaterialShapeDrawable
    if (materialShapeDrawable != null) {
      // Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
      materialShapeDrawable.setElevation(
          elevation == -1 ? ViewCompat.getElevation(child) : elevation);
      // Update the material shape based on initial state.
      isShapeExpanded = state == STATE_EXPANDED;
      materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
    }
    updateAccessibilityActions();
    if (ViewCompat.getImportantForAccessibility(child)
        == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
      ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }
  }
  if (viewDragHelper == null) {
    viewDragHelper = ViewDragHelper.create(parent, dragCallback);
  }

  int savedTop = child.getTop();
  // First let the parent lay it out
  parent.onLayoutChild(child, layoutDirection);
  // Offset the bottom sheet
  parentWidth = parent.getWidth();
  parentHeight = parent.getHeight();
  fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
  calculateHalfExpandedOffset();
  calculateCollapsedOffset();

  if (state == STATE_EXPANDED) {
    ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
  } else if (state == STATE_HALF_EXPANDED) {
    ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
  } else if (hideable && state == STATE_HIDDEN) {
    ViewCompat.offsetTopAndBottom(child, parentHeight);
  } else if (state == STATE_COLLAPSED) {
    ViewCompat.offsetTopAndBottom(child, collapsedOffset);
  } else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
    ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
  }

  nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
  return true;
}
 
Example 15
Source File: CustomViewPager.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
void initViewPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new Scroller(context, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = configuration.getScaledPagingTouchSlop();
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffectCompat(context);
    mRightEdge = new EdgeEffectCompat(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new androidx.core.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                              final WindowInsetsCompat originalInsets) {
                    // First let the ViewPager itself try and consume them...
                    final WindowInsetsCompat applied =
                            ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the ViewPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since ViewPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 16
Source File: SliderPager.java    From Android-Image-Slider with Apache License 2.0 4 votes vote down vote up
void initSliderPager() {
    setWillNotDraw(false);
    setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
    setFocusable(true);
    final Context context = getContext();
    mScroller = new OwnScroller(context, DEFAULT_SCROLL_DURATION, sInterpolator);
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    final float density = context.getResources().getDisplayMetrics().density;

    mTouchSlop = configuration.getScaledPagingTouchSlop();
    mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    mLeftEdge = new EdgeEffect(context);
    mRightEdge = new EdgeEffect(context);

    mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
    mCloseEnough = (int) (CLOSE_ENOUGH * density);
    mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);

    ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());

    if (ViewCompat.getImportantForAccessibility(this)
            == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        ViewCompat.setImportantForAccessibility(this,
                ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new androidx.core.view.OnApplyWindowInsetsListener() {
                private final Rect mTempRect = new Rect();

                @Override
                public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                              final WindowInsetsCompat originalInsets) {
                    // First let the SliderPager itself try and consume them...
                    final WindowInsetsCompat applied =
                            ViewCompat.onApplyWindowInsets(v, originalInsets);
                    if (applied.isConsumed()) {
                        // If the SliderPager consumed all insets, return now
                        return applied;
                    }

                    // Now we'll manually dispatch the insets to our children. Since SliderPager
                    // children are always full-height, we do not want to use the standard
                    // ViewGroup dispatchApplyWindowInsets since if child 0 consumes them,
                    // the rest of the children will not receive any insets. To workaround this
                    // we manually dispatch the applied insets, not allowing children to
                    // consume them from each other. We do however keep track of any insets
                    // which are consumed, returning the union of our children's consumption
                    final Rect res = mTempRect;
                    res.left = applied.getSystemWindowInsetLeft();
                    res.top = applied.getSystemWindowInsetTop();
                    res.right = applied.getSystemWindowInsetRight();
                    res.bottom = applied.getSystemWindowInsetBottom();

                    for (int i = 0, count = getChildCount(); i < count; i++) {
                        final WindowInsetsCompat childInsets = ViewCompat
                                .dispatchApplyWindowInsets(getChildAt(i), applied);
                        // Now keep track of any consumed by tracking each dimension's min
                        // value
                        res.left = Math.min(childInsets.getSystemWindowInsetLeft(),
                                res.left);
                        res.top = Math.min(childInsets.getSystemWindowInsetTop(),
                                res.top);
                        res.right = Math.min(childInsets.getSystemWindowInsetRight(),
                                res.right);
                        res.bottom = Math.min(childInsets.getSystemWindowInsetBottom(),
                                res.bottom);
                    }

                    // Now return a new WindowInsets, using the consumed window insets
                    return applied.replaceSystemWindowInsets(
                            res.left, res.top, res.right, res.bottom);
                }
            });
}
 
Example 17
Source File: FabTransformationSheetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void updateImportantForAccessibility(@NonNull View sheet, boolean expanded) {
  ViewParent viewParent = sheet.getParent();
  if (!(viewParent instanceof CoordinatorLayout)) {
    return;
  }

  CoordinatorLayout parent = (CoordinatorLayout) viewParent;
  final int childCount = parent.getChildCount();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && expanded) {
    importantForAccessibilityMap = new HashMap<>(childCount);
  }

  for (int i = 0; i < childCount; i++) {
    final View child = parent.getChildAt(i);

    // Don't change the accessibility importance of the sheet or the scrim.
    boolean hasScrimBehavior =
        (child.getLayoutParams() instanceof CoordinatorLayout.LayoutParams)
            && (((CoordinatorLayout.LayoutParams) child.getLayoutParams()).getBehavior()
                instanceof FabTransformationScrimBehavior);
    if (child == sheet || hasScrimBehavior) {
      continue;
    }

    if (!expanded) {
      if (importantForAccessibilityMap != null
          && importantForAccessibilityMap.containsKey(child)) {
        // Restores the original important for accessibility value of the child view.
        ViewCompat.setImportantForAccessibility(child, importantForAccessibilityMap.get(child));
      }
    } else {
      // Saves the important for accessibility value of the child view.
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        importantForAccessibilityMap.put(child, child.getImportantForAccessibility());
      }

      ViewCompat.setImportantForAccessibility(
          child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
    }
  }

  if (!expanded) {
    importantForAccessibilityMap = null;
  }
}
 
Example 18
Source File: MountState.java    From litho with Apache License 2.0 4 votes vote down vote up
private static void unsetImportantForAccessibility(View view) {
  ViewCompat.setImportantForAccessibility(view, IMPORTANT_FOR_ACCESSIBILITY_AUTO);
}
 
Example 19
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean onLayoutChild(
        @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
    if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
        child.setFitsSystemWindows(true);
    }

    if (viewRef == null) {
        // First layout with this behavior.
        peekHeightMin =
                parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
        viewRef = new WeakReference<>(child);
        // Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
        // default to android:background declared in styles or layout.
        if (shapeThemingEnabled && materialShapeDrawable != null) {
            ViewCompat.setBackground(child, materialShapeDrawable);
        }
        // Set elevation on MaterialShapeDrawable
        if (materialShapeDrawable != null) {
            // Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
            materialShapeDrawable.setElevation(
                    elevation == -1 ? ViewCompat.getElevation(child) : elevation);
            // Update the material shape based on initial state.
            isShapeExpanded = state == STATE_EXPANDED;
            materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
        }
        updateAccessibilityActions();
        if (ViewCompat.getImportantForAccessibility(child)
                == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
        }
    }
    if (viewDragHelper == null) {
        viewDragHelper = ViewDragHelper.create(parent, dragCallback);
    }

    int savedTop = child.getTop();
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);
    // Offset the bottom sheet
    parentWidth = parent.getWidth();
    parentHeight = parent.getHeight();
    fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
    calculateHalfExpandedOffset();
    calculateCollapsedOffset();

    if (state == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
    } else if (state == STATE_HALF_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
    } else if (hideable && state == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, parentHeight);
    } else if (state == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, collapsedOffset);
    } else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
        ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
    }

    nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
    return true;
}
 
Example 20
Source File: MonthView.java    From PersianDateRangePicker with Apache License 2.0 4 votes vote down vote up
public MonthView(Context context, AttributeSet attr, DatePickerController controller) {
  super(context, attr);
  mController = controller;
  Resources res = context.getResources();

  mDayLabelCalendar = new PersianDate();
  mPersianDate = new PersianDate();

  mDayOfWeekTypeface = res.getString(R.string.mdtp_day_of_week_label_typeface);
  mMonthTitleTypeface = res.getString(R.string.mdtp_sans_serif);

  boolean darkTheme = mController != null && mController.isThemeDark();
  if (darkTheme) {
    mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal_dark_theme);
    mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day_dark_theme);
    mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled_dark_theme);
    mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted_dark_theme);
  } else {
    mDayTextColor = res.getColor(R.color.mdtp_date_picker_text_normal);
    mMonthDayTextColor = res.getColor(R.color.mdtp_date_picker_month_day);
    mDisabledDayTextColor = res.getColor(R.color.mdtp_date_picker_text_disabled);
    mHighlightedDayTextColor = res.getColor(R.color.mdtp_date_picker_text_highlighted);
  }
  mSelectedDayTextColor = res.getColor(R.color.mdtp_white);
  mTodayNumberColor = res.getColor(R.color.mdtp_accent_color);
  mMonthTitleColor = res.getColor(R.color.mdtp_white);

  mStringBuilder = new StringBuilder(50);

  MINI_DAY_NUMBER_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_day_number_size);
  MONTH_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_label_size);
  MONTH_DAY_LABEL_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.mdtp_month_day_label_text_size);
  MONTH_HEADER_SIZE = res.getDimensionPixelOffset(R.dimen.mdtp_month_list_item_header_height);
  DAY_SELECTED_CIRCLE_SIZE = res
    .getDimensionPixelSize(R.dimen.mdtp_day_number_select_circle_radius);

  mRowHeight = (res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height)
    - getMonthHeaderSize()) / MAX_NUM_ROWS;

  // Set up accessibility components.
  mTouchHelper = getMonthViewTouchHelper();
  ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
  ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
  mLockAccessibilityDelegate = true;

  // Sets up any standard paints that will be used
  initView();
}