1 /* 2 * Copyright (C) 2019 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 com.android.internal.app; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.MotionEvent; 22 import android.view.View; 23 24 import com.android.internal.widget.ViewPager; 25 26 /** 27 * A {@link ViewPager} which wraps around its tallest child's height. 28 * <p>Normally {@link ViewPager} instances expand their height to cover all remaining space in 29 * the layout. 30 * <p>This class is used for the intent resolver and share sheet's tabbed view. 31 */ 32 public class ResolverViewPager extends ViewPager { 33 34 private boolean mSwipingEnabled = true; 35 ResolverViewPager(Context context)36 public ResolverViewPager(Context context) { 37 super(context); 38 } 39 ResolverViewPager(Context context, AttributeSet attrs)40 public ResolverViewPager(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 } 43 ResolverViewPager(Context context, AttributeSet attrs, int defStyleAttr)44 public ResolverViewPager(Context context, AttributeSet attrs, int defStyleAttr) { 45 super(context, attrs, defStyleAttr); 46 } 47 ResolverViewPager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48 public ResolverViewPager(Context context, AttributeSet attrs, 49 int defStyleAttr, int defStyleRes) { 50 super(context, attrs, defStyleAttr, defStyleRes); 51 } 52 53 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)54 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 55 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 56 if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.AT_MOST) { 57 return; 58 } 59 widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); 60 int height = getMeasuredHeight(); 61 int maxHeight = 0; 62 for (int i = 0; i < getChildCount(); i++) { 63 View child = getChildAt(i); 64 child.measure(widthMeasureSpec, 65 MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)); 66 if (maxHeight < child.getMeasuredHeight()) { 67 maxHeight = child.getMeasuredHeight(); 68 } 69 } 70 if (maxHeight > 0) { 71 height = maxHeight; 72 } 73 heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 74 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 75 } 76 77 /** 78 * Sets whether swiping sideways should happen. 79 * <p>Note that swiping is always disabled for RTL layouts (b/159110029 for context). 80 */ setSwipingEnabled(boolean swipingEnabled)81 void setSwipingEnabled(boolean swipingEnabled) { 82 mSwipingEnabled = swipingEnabled; 83 } 84 85 @Override onInterceptTouchEvent(MotionEvent ev)86 public boolean onInterceptTouchEvent(MotionEvent ev) { 87 return !isLayoutRtl() && mSwipingEnabled && super.onInterceptTouchEvent(ev); 88 } 89 } 90