blob: 560260b4f8c0df93300703fb07be1fa5e5a9c37e [file] [log] [blame]
Justin Klaassen4b3af052014-05-27 17:53:10 -07001/*
Justin Klaassen06c49442015-06-04 14:39:27 -07002 * Copyright (C) 2015 The Android Open Source Project
Justin Klaassen4b3af052014-05-27 17:53:10 -07003 *
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
17package com.android.calculator2;
18
19import android.content.Context;
Justin Klaassen06c49442015-06-04 14:39:27 -070020import android.graphics.Color;
Justin Klaassen4b3af052014-05-27 17:53:10 -070021import android.support.v4.view.PagerAdapter;
22import android.support.v4.view.ViewPager;
23import android.util.AttributeSet;
Justin Klaassenda3b3692016-02-05 13:45:29 -080024import android.view.GestureDetector;
Justin Klaassen06c49442015-06-04 14:39:27 -070025import android.view.MotionEvent;
Justin Klaassen4b3af052014-05-27 17:53:10 -070026import android.view.View;
27import android.view.ViewGroup;
28
29public 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 Klaassenda3b3692016-02-05 13:45:29 -080038 public View instantiateItem(ViewGroup container, final int position) {
39 final View child = getChildAt(position);
Justin Klaassen788431a2016-07-19 21:42:50 -070040
41 // Set a OnClickListener to scroll to item's position when it isn't the current item.
42 child.setOnClickListener(new OnClickListener() {
Justin Klaassenda3b3692016-02-05 13:45:29 -080043 @Override
44 public void onClick(View v) {
45 setCurrentItem(position, true /* smoothScroll */);
46 }
47 });
Justin Klaassen2cf12892016-08-08 11:58:26 -070048 // 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 Klaassenda3b3692016-02-05 13:45:29 -080057
Justin Klaassen2cf12892016-08-08 11:58:26 -070058 // Set an OnHoverListener to always return true for onHover events so that focus cannot
Justin Klaassen788431a2016-07-19 21:42:50 -070059 // 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 Klaassenda3b3692016-02-05 13:45:29 -080072 return child;
Justin Klaassen4b3af052014-05-27 17:53:10 -070073 }
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 Klaassen0587d222014-06-19 10:44:21 -070087 return position == 1 ? 7.0f / 9.0f : 1.0f;
Justin Klaassen4b3af052014-05-27 17:53:10 -070088 }
Justin Klaassen788431a2016-07-19 21:42:50 -070089
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 Klaassen4b3af052014-05-27 17:53:10 -070096 };
Justin Klaassen5c324202014-06-10 20:27:20 -070097
98 private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {
Justin Klaassen5c324202014-06-10 20:27:20 -070099 @Override
100 public void onPageSelected(int position) {
Justin Klaassen06c49442015-06-04 14:39:27 -0700101 for (int i = getChildCount() - 1; i >= 0; --i) {
Justin Klaassen3d7653e2015-06-08 15:27:55 -0700102 final View child = getChildAt(i);
Justin Klaassenda3b3692016-02-05 13:45:29 -0800103 // Only the "peeking" or covered page should be clickable.
104 child.setClickable(i != position);
105
Justin Klaassen3d7653e2015-06-08 15:27:55 -0700106 // Prevent clicks and accessibility focus from going through to descendants of
107 // other pages which are covered by the current page.
Justin Klaassen2cf12892016-08-08 11:58:26 -0700108 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 Klaassen5c324202014-06-10 20:27:20 -0700117 }
118 }
119 };
120
Justin Klaassen4b3af052014-05-27 17:53:10 -0700121 private final PageTransformer mPageTransformer = new PageTransformer() {
122 @Override
123 public void transformPage(View view, float position) {
Justin Klaassen5c324202014-06-10 20:27:20 -0700124 if (position < 0.0f) {
Justin Klaassen4b3af052014-05-27 17:53:10 -0700125 // Pin the left page to the left side.
126 view.setTranslationX(getWidth() * -position);
Justin Klaassen5c324202014-06-10 20:27:20 -0700127 view.setAlpha(Math.max(1.0f + position, 0.0f));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700128 } else {
Justin Klaassen5c324202014-06-10 20:27:20 -0700129 // Use the default slide transition when moving to the next page.
130 view.setTranslationX(0.0f);
131 view.setAlpha(1.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700132 }
133 }
134 };
135
Justin Klaassenda3b3692016-02-05 13:45:29 -0800136 private final GestureDetector.SimpleOnGestureListener mGestureWatcher =
137 new GestureDetector.SimpleOnGestureListener() {
Justin Klaassen788431a2016-07-19 21:42:50 -0700138 @Override
139 public boolean onDown(MotionEvent e) {
140 // Return true so calls to onSingleTapUp are not blocked.
141 return true;
142 }
Justin Klaassenda3b3692016-02-05 13:45:29 -0800143
Justin Klaassen788431a2016-07-19 21:42:50 -0700144 @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 Klaassenda3b3692016-02-05 13:45:29 -0800154
155 private final GestureDetector mGestureDetector;
156
157 private int mClickedItemIndex = -1;
158
Justin Klaassen4b3af052014-05-27 17:53:10 -0700159 public CalculatorPadViewPager(Context context) {
Justin Klaassen06c49442015-06-04 14:39:27 -0700160 this(context, null /* attrs */);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700161 }
162
163 public CalculatorPadViewPager(Context context, AttributeSet attrs) {
164 super(context, attrs);
165
Justin Klaassenda3b3692016-02-05 13:45:29 -0800166 mGestureDetector = new GestureDetector(context, mGestureWatcher);
167 mGestureDetector.setIsLongpressEnabled(false);
168
Justin Klaassen4b3af052014-05-27 17:53:10 -0700169 setAdapter(mStaticPagerAdapter);
Justin Klaassen06c49442015-06-04 14:39:27 -0700170 setBackgroundColor(Color.BLACK);
Annie Chin26e159e2016-05-18 15:17:14 -0700171 setPageMargin(-getResources().getDimensionPixelSize(R.dimen.pad_page_margin));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700172 setPageTransformer(false, mPageTransformer);
Justin Klaassen06c49442015-06-04 14:39:27 -0700173 addOnPageChangeListener(mOnPageChangeListener);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700174 }
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 Klaassen06c49442015-06-04 14:39:27 -0700181 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 Klaassen788431a2016-07-19 21:42:50 -0700189 // 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 Klaassen06c49442015-06-04 14:39:27 -0700192
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 Klaassen2cf12892016-08-08 11:58:26 -0700198 // Reset the previously clicked item index.
199 mClickedItemIndex = -1;
200
Justin Klaassen06c49442015-06-04 14:39:27 -0700201 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 Klaassen788431a2016-07-19 21:42:50 -0700205 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 Klaassenda3b3692016-02-05 13:45:29 -0800209 mClickedItemIndex = childIndex;
Justin Klaassen06c49442015-06-04 14:39:27 -0700210 break;
Justin Klaassen2cf12892016-08-08 11:58:26 -0700211 } else if (mClickedItemIndex == -1
212 && child.getVisibility() == VISIBLE
Justin Klaassen788431a2016-07-19 21:42:50 -0700213 && 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 Klaassen06c49442015-06-04 14:39:27 -0700218 }
219 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700220 }
Justin Klaassen06c49442015-06-04 14:39:27 -0700221
222 return shouldIntercept;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700223 }
Justin Klaassenda3b3692016-02-05 13:45:29 -0800224
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 Klaassen4b3af052014-05-27 17:53:10 -0700233}