Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 1 | /* |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 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.calculator2; |
| 18 | |
| 19 | import android.content.Context; |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 20 | import android.graphics.Color; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 21 | import android.support.v4.view.PagerAdapter; |
| 22 | import android.support.v4.view.ViewPager; |
| 23 | import android.util.AttributeSet; |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 24 | import android.view.GestureDetector; |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 25 | import android.view.MotionEvent; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | |
| 29 | public class CalculatorPadViewPager extends ViewPager { |
| 30 | |
| 31 | private final PagerAdapter mStaticPagerAdapter = new PagerAdapter() { |
| 32 | @Override |
| 33 | public int getCount() { |
| 34 | return getChildCount(); |
| 35 | } |
| 36 | |
| 37 | @Override |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 38 | public View instantiateItem(ViewGroup container, final int position) { |
| 39 | final View child = getChildAt(position); |
| 40 | child.setOnClickListener(new View.OnClickListener() { |
| 41 | @Override |
| 42 | public void onClick(View v) { |
| 43 | setCurrentItem(position, true /* smoothScroll */); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | return child; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void destroyItem(ViewGroup container, int position, Object object) { |
| 52 | removeViewAt(position); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean isViewFromObject(View view, Object object) { |
| 57 | return view == object; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public float getPageWidth(int position) { |
Justin Klaassen | 0587d22 | 2014-06-19 10:44:21 -0700 | [diff] [blame] | 62 | return position == 1 ? 7.0f / 9.0f : 1.0f; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 63 | } |
| 64 | }; |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 65 | |
| 66 | private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 67 | @Override |
| 68 | public void onPageSelected(int position) { |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 69 | for (int i = getChildCount() - 1; i >= 0; --i) { |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 70 | final View child = getChildAt(i); |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 71 | // Only the "peeking" or covered page should be clickable. |
| 72 | child.setClickable(i != position); |
| 73 | |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 74 | // Prevent clicks and accessibility focus from going through to descendants of |
| 75 | // other pages which are covered by the current page. |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 76 | child.setImportantForAccessibility(i == position |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 77 | ? IMPORTANT_FOR_ACCESSIBILITY_AUTO |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 78 | : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | }; |
| 82 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 83 | private final PageTransformer mPageTransformer = new PageTransformer() { |
| 84 | @Override |
| 85 | public void transformPage(View view, float position) { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 86 | if (position < 0.0f) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 87 | // Pin the left page to the left side. |
| 88 | view.setTranslationX(getWidth() * -position); |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 89 | view.setAlpha(Math.max(1.0f + position, 0.0f)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 90 | } else { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 91 | // Use the default slide transition when moving to the next page. |
| 92 | view.setTranslationX(0.0f); |
| 93 | view.setAlpha(1.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 98 | private final GestureDetector.SimpleOnGestureListener mGestureWatcher = |
| 99 | new GestureDetector.SimpleOnGestureListener() { |
| 100 | @Override |
| 101 | public boolean onSingleTapUp(MotionEvent ev) { |
| 102 | if (mClickedItemIndex != -1) { |
| 103 | getChildAt(mClickedItemIndex).performClick(); |
| 104 | mClickedItemIndex = -1; |
| 105 | return true; |
| 106 | } |
| 107 | return super.onSingleTapUp(ev); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean onDown(MotionEvent e) { |
| 112 | // Return true so calls to onSingleTapUp are not blocked |
| 113 | return true; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | private final GestureDetector mGestureDetector; |
| 118 | |
| 119 | private int mClickedItemIndex = -1; |
| 120 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 121 | public CalculatorPadViewPager(Context context) { |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 122 | this(context, null /* attrs */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public CalculatorPadViewPager(Context context, AttributeSet attrs) { |
| 126 | super(context, attrs); |
| 127 | |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 128 | mGestureDetector = new GestureDetector(context, mGestureWatcher); |
| 129 | mGestureDetector.setIsLongpressEnabled(false); |
| 130 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 131 | setAdapter(mStaticPagerAdapter); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 132 | setBackgroundColor(Color.BLACK); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame^] | 133 | setPageMargin(-getResources().getDimensionPixelSize(R.dimen.pad_page_margin)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 134 | setPageTransformer(false, mPageTransformer); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 135 | addOnPageChangeListener(mOnPageChangeListener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | protected void onFinishInflate() { |
| 140 | super.onFinishInflate(); |
| 141 | |
| 142 | // Invalidate the adapter's data set since children may have been added during inflation. |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 143 | getAdapter().notifyDataSetChanged(); |
| 144 | |
| 145 | // Let page change listener know about our initial position. |
| 146 | mOnPageChangeListener.onPageSelected(getCurrentItem()); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 151 | boolean shouldIntercept = super.onInterceptTouchEvent(ev); |
| 152 | |
| 153 | // Only allow the current item to receive touch events. |
| 154 | if (!shouldIntercept && ev.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 155 | final int x = (int) ev.getX() + getScrollX(); |
| 156 | final int y = (int) ev.getY() + getScrollY(); |
| 157 | |
| 158 | final int childCount = getChildCount(); |
| 159 | for (int i = childCount - 1; i >= 0; --i) { |
| 160 | final int childIndex = getChildDrawingOrder(childCount, i); |
| 161 | final View child = getChildAt(childIndex); |
| 162 | if (child.getVisibility() == View.VISIBLE |
| 163 | && x >= child.getLeft() && x < child.getRight() |
| 164 | && y >= child.getTop() && y < child.getBottom()) { |
| 165 | shouldIntercept = (childIndex != getCurrentItem()); |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 166 | mClickedItemIndex = childIndex; |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 170 | } |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 171 | |
| 172 | return shouldIntercept; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 173 | } |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public boolean onTouchEvent(MotionEvent ev) { |
| 177 | // Allow both the gesture detector and super to handle the touch event so they both see |
| 178 | // the full sequence of events. This should be safe since the gesture detector only |
| 179 | // handle clicks and super only handles swipes. |
| 180 | mGestureDetector.onTouchEvent(ev); |
| 181 | return super.onTouchEvent(ev); |
| 182 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 183 | } |