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); |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 40 | |
| 41 | // Set a OnClickListener to scroll to item's position when it isn't the current item. |
| 42 | child.setOnClickListener(new OnClickListener() { |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 43 | @Override |
| 44 | public void onClick(View v) { |
| 45 | setCurrentItem(position, true /* smoothScroll */); |
| 46 | } |
| 47 | }); |
Justin Klaassen | 2cf1289 | 2016-08-08 11:58:26 -0700 | [diff] [blame^] | 48 | // Set an OnTouchListener to always return true for onTouch events so that a touch |
| 49 | // sequence cannot pass through the item to the item below. |
| 50 | child.setOnTouchListener(new OnTouchListener() { |
| 51 | @Override |
| 52 | public boolean onTouch(View v, MotionEvent event) { |
| 53 | v.onTouchEvent(event); |
| 54 | return true; |
| 55 | } |
| 56 | }); |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 57 | |
Justin Klaassen | 2cf1289 | 2016-08-08 11:58:26 -0700 | [diff] [blame^] | 58 | // Set an OnHoverListener to always return true for onHover events so that focus cannot |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 59 | // pass through the item to the item below. |
| 60 | child.setOnHoverListener(new OnHoverListener() { |
| 61 | @Override |
| 62 | public boolean onHover(View v, MotionEvent event) { |
| 63 | v.onHoverEvent(event); |
| 64 | return true; |
| 65 | } |
| 66 | }); |
| 67 | // Make the item focusable so it can be selected via a11y. |
| 68 | child.setFocusable(true); |
| 69 | // Set the content description of the item which will be used by a11y to identify it. |
| 70 | child.setContentDescription(getPageTitle(position)); |
| 71 | |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 72 | return child; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void destroyItem(ViewGroup container, int position, Object object) { |
| 77 | removeViewAt(position); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean isViewFromObject(View view, Object object) { |
| 82 | return view == object; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public float getPageWidth(int position) { |
Justin Klaassen | 0587d22 | 2014-06-19 10:44:21 -0700 | [diff] [blame] | 87 | return position == 1 ? 7.0f / 9.0f : 1.0f; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 88 | } |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | public CharSequence getPageTitle(int position) { |
| 92 | final String[] pageDescriptions = getContext().getResources() |
| 93 | .getStringArray(R.array.desc_pad_pages); |
| 94 | return pageDescriptions[position]; |
| 95 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 96 | }; |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 97 | |
| 98 | private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void onPageSelected(int position) { |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 101 | for (int i = getChildCount() - 1; i >= 0; --i) { |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 102 | final View child = getChildAt(i); |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 103 | // Only the "peeking" or covered page should be clickable. |
| 104 | child.setClickable(i != position); |
| 105 | |
Justin Klaassen | 3d7653e | 2015-06-08 15:27:55 -0700 | [diff] [blame] | 106 | // Prevent clicks and accessibility focus from going through to descendants of |
| 107 | // other pages which are covered by the current page. |
Justin Klaassen | 2cf1289 | 2016-08-08 11:58:26 -0700 | [diff] [blame^] | 108 | if (child instanceof ViewGroup) { |
| 109 | final ViewGroup childViewGroup = (ViewGroup) child; |
| 110 | for (int j = childViewGroup.getChildCount() - 1; j >= 0; --j) { |
| 111 | childViewGroup.getChildAt(j) |
| 112 | .setImportantForAccessibility(i == position |
| 113 | ? IMPORTANT_FOR_ACCESSIBILITY_AUTO |
| 114 | : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
| 115 | } |
| 116 | } |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 121 | private final PageTransformer mPageTransformer = new PageTransformer() { |
| 122 | @Override |
| 123 | public void transformPage(View view, float position) { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 124 | if (position < 0.0f) { |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 125 | // Pin the left page to the left side. |
| 126 | view.setTranslationX(getWidth() * -position); |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 127 | view.setAlpha(Math.max(1.0f + position, 0.0f)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 128 | } else { |
Justin Klaassen | 5c32420 | 2014-06-10 20:27:20 -0700 | [diff] [blame] | 129 | // Use the default slide transition when moving to the next page. |
| 130 | view.setTranslationX(0.0f); |
| 131 | view.setAlpha(1.0f); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | }; |
| 135 | |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 136 | private final GestureDetector.SimpleOnGestureListener mGestureWatcher = |
| 137 | new GestureDetector.SimpleOnGestureListener() { |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public boolean onDown(MotionEvent e) { |
| 140 | // Return true so calls to onSingleTapUp are not blocked. |
| 141 | return true; |
| 142 | } |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 143 | |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public boolean onSingleTapUp(MotionEvent ev) { |
| 146 | if (mClickedItemIndex != -1) { |
| 147 | getChildAt(mClickedItemIndex).performClick(); |
| 148 | mClickedItemIndex = -1; |
| 149 | return true; |
| 150 | } |
| 151 | return super.onSingleTapUp(ev); |
| 152 | } |
| 153 | }; |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 154 | |
| 155 | private final GestureDetector mGestureDetector; |
| 156 | |
| 157 | private int mClickedItemIndex = -1; |
| 158 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 159 | public CalculatorPadViewPager(Context context) { |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 160 | this(context, null /* attrs */); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | public CalculatorPadViewPager(Context context, AttributeSet attrs) { |
| 164 | super(context, attrs); |
| 165 | |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 166 | mGestureDetector = new GestureDetector(context, mGestureWatcher); |
| 167 | mGestureDetector.setIsLongpressEnabled(false); |
| 168 | |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 169 | setAdapter(mStaticPagerAdapter); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 170 | setBackgroundColor(Color.BLACK); |
Annie Chin | 26e159e | 2016-05-18 15:17:14 -0700 | [diff] [blame] | 171 | setPageMargin(-getResources().getDimensionPixelSize(R.dimen.pad_page_margin)); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 172 | setPageTransformer(false, mPageTransformer); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 173 | addOnPageChangeListener(mOnPageChangeListener); |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void onFinishInflate() { |
| 178 | super.onFinishInflate(); |
| 179 | |
| 180 | // 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] | 181 | getAdapter().notifyDataSetChanged(); |
| 182 | |
| 183 | // Let page change listener know about our initial position. |
| 184 | mOnPageChangeListener.onPageSelected(getCurrentItem()); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 189 | // Always intercept touch events when a11y focused since otherwise they will be |
| 190 | // incorrectly offset by a11y before being dispatched to children. |
| 191 | boolean shouldIntercept = isAccessibilityFocused() || super.onInterceptTouchEvent(ev); |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 192 | |
| 193 | // Only allow the current item to receive touch events. |
| 194 | if (!shouldIntercept && ev.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 195 | final int x = (int) ev.getX() + getScrollX(); |
| 196 | final int y = (int) ev.getY() + getScrollY(); |
| 197 | |
Justin Klaassen | 2cf1289 | 2016-08-08 11:58:26 -0700 | [diff] [blame^] | 198 | // Reset the previously clicked item index. |
| 199 | mClickedItemIndex = -1; |
| 200 | |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 201 | final int childCount = getChildCount(); |
| 202 | for (int i = childCount - 1; i >= 0; --i) { |
| 203 | final int childIndex = getChildDrawingOrder(childCount, i); |
| 204 | final View child = getChildAt(childIndex); |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 205 | if (child.isAccessibilityFocused()) { |
| 206 | // If a child is a11y focused then we must always intercept the touch event |
| 207 | // since it will be incorrectly offset by a11y. |
| 208 | shouldIntercept = true; |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 209 | mClickedItemIndex = childIndex; |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 210 | break; |
Justin Klaassen | 2cf1289 | 2016-08-08 11:58:26 -0700 | [diff] [blame^] | 211 | } else if (mClickedItemIndex == -1 |
| 212 | && child.getVisibility() == VISIBLE |
Justin Klaassen | 788431a | 2016-07-19 21:42:50 -0700 | [diff] [blame] | 213 | && x >= child.getLeft() && x < child.getRight() |
| 214 | && y >= child.getTop() && y < child.getBottom()) { |
| 215 | shouldIntercept = childIndex != getCurrentItem(); |
| 216 | mClickedItemIndex = childIndex; |
| 217 | // continue; since another child may be a11y focused. |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 220 | } |
Justin Klaassen | 06c4944 | 2015-06-04 14:39:27 -0700 | [diff] [blame] | 221 | |
| 222 | return shouldIntercept; |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 223 | } |
Justin Klaassen | da3b369 | 2016-02-05 13:45:29 -0800 | [diff] [blame] | 224 | |
| 225 | @Override |
| 226 | public boolean onTouchEvent(MotionEvent ev) { |
| 227 | // Allow both the gesture detector and super to handle the touch event so they both see |
| 228 | // the full sequence of events. This should be safe since the gesture detector only |
| 229 | // handle clicks and super only handles swipes. |
| 230 | mGestureDetector.onTouchEvent(ev); |
| 231 | return super.onTouchEvent(ev); |
| 232 | } |
Justin Klaassen | 4b3af05 | 2014-05-27 17:53:10 -0700 | [diff] [blame] | 233 | } |