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.intentresolver; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.MotionEvent; 22 import android.view.View; 23 24 import androidx.viewpager.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 44 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)45 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 46 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 47 if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.AT_MOST) { 48 return; 49 } 50 widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); 51 int height = getMeasuredHeight(); 52 int maxHeight = 0; 53 for (int i = 0; i < getChildCount(); i++) { 54 View child = getChildAt(i); 55 child.measure(widthMeasureSpec, 56 MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)); 57 if (maxHeight < child.getMeasuredHeight()) { 58 maxHeight = child.getMeasuredHeight(); 59 } 60 } 61 if (maxHeight > 0) { 62 height = maxHeight; 63 } 64 heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 65 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 66 } 67 68 /** 69 * Sets whether swiping sideways should happen. 70 * <p>Note that swiping is always disabled for RTL layouts (b/159110029 for context). 71 */ setSwipingEnabled(boolean swipingEnabled)72 void setSwipingEnabled(boolean swipingEnabled) { 73 mSwipingEnabled = swipingEnabled; 74 } 75 76 @Override onInterceptTouchEvent(MotionEvent ev)77 public boolean onInterceptTouchEvent(MotionEvent ev) { 78 return !isLayoutRtl() && mSwipingEnabled && super.onInterceptTouchEvent(ev); 79 } 80 } 81