blob: 53e0ccdbfa0e63e2de8d793e07cfc668a0fc1c6f [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 });
48
Justin Klaassen788431a2016-07-19 21:42:50 -070049 // Set a OnHoverListener to always return true for onHover events so that focus cannot
50 // pass through the item to the item below.
51 child.setOnHoverListener(new OnHoverListener() {
52 @Override
53 public boolean onHover(View v, MotionEvent event) {
54 v.onHoverEvent(event);
55 return true;
56 }
57 });
58 // Make the item focusable so it can be selected via a11y.
59 child.setFocusable(true);
60 // Set the content description of the item which will be used by a11y to identify it.
61 child.setContentDescription(getPageTitle(position));
62
Justin Klaassenda3b3692016-02-05 13:45:29 -080063 return child;
Justin Klaassen4b3af052014-05-27 17:53:10 -070064 }
65
66 @Override
67 public void destroyItem(ViewGroup container, int position, Object object) {
68 removeViewAt(position);
69 }
70
71 @Override
72 public boolean isViewFromObject(View view, Object object) {
73 return view == object;
74 }
75
76 @Override
77 public float getPageWidth(int position) {
Justin Klaassen0587d222014-06-19 10:44:21 -070078 return position == 1 ? 7.0f / 9.0f : 1.0f;
Justin Klaassen4b3af052014-05-27 17:53:10 -070079 }
Justin Klaassen788431a2016-07-19 21:42:50 -070080
81 @Override
82 public CharSequence getPageTitle(int position) {
83 final String[] pageDescriptions = getContext().getResources()
84 .getStringArray(R.array.desc_pad_pages);
85 return pageDescriptions[position];
86 }
Justin Klaassen4b3af052014-05-27 17:53:10 -070087 };
Justin Klaassen5c324202014-06-10 20:27:20 -070088
89 private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {
Justin Klaassen5c324202014-06-10 20:27:20 -070090 @Override
91 public void onPageSelected(int position) {
Justin Klaassen06c49442015-06-04 14:39:27 -070092 for (int i = getChildCount() - 1; i >= 0; --i) {
Justin Klaassen3d7653e2015-06-08 15:27:55 -070093 final View child = getChildAt(i);
Justin Klaassenda3b3692016-02-05 13:45:29 -080094 // Only the "peeking" or covered page should be clickable.
95 child.setClickable(i != position);
96
Justin Klaassen3d7653e2015-06-08 15:27:55 -070097 // Prevent clicks and accessibility focus from going through to descendants of
98 // other pages which are covered by the current page.
Justin Klaassen788431a2016-07-19 21:42:50 -070099 if (child instanceof ViewGroup) {
100 final ViewGroup childViewGroup = (ViewGroup) child;
101 for (int j = childViewGroup.getChildCount() - 1; j >= 0; --j) {
102 childViewGroup.getChildAt(j)
103 .setImportantForAccessibility(i == position
104 ? IMPORTANT_FOR_ACCESSIBILITY_AUTO
105 : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
106 }
107 }
Justin Klaassen5c324202014-06-10 20:27:20 -0700108 }
109 }
110 };
111
Justin Klaassen4b3af052014-05-27 17:53:10 -0700112 private final PageTransformer mPageTransformer = new PageTransformer() {
113 @Override
114 public void transformPage(View view, float position) {
Justin Klaassen5c324202014-06-10 20:27:20 -0700115 if (position < 0.0f) {
Justin Klaassen4b3af052014-05-27 17:53:10 -0700116 // Pin the left page to the left side.
117 view.setTranslationX(getWidth() * -position);
Justin Klaassen5c324202014-06-10 20:27:20 -0700118 view.setAlpha(Math.max(1.0f + position, 0.0f));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700119 } else {
Justin Klaassen5c324202014-06-10 20:27:20 -0700120 // Use the default slide transition when moving to the next page.
121 view.setTranslationX(0.0f);
122 view.setAlpha(1.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700123 }
124 }
125 };
126
Justin Klaassenda3b3692016-02-05 13:45:29 -0800127 private final GestureDetector.SimpleOnGestureListener mGestureWatcher =
128 new GestureDetector.SimpleOnGestureListener() {
Justin Klaassen788431a2016-07-19 21:42:50 -0700129 @Override
130 public boolean onDown(MotionEvent e) {
131 // Return true so calls to onSingleTapUp are not blocked.
132 return true;
133 }
Justin Klaassenda3b3692016-02-05 13:45:29 -0800134
Justin Klaassen788431a2016-07-19 21:42:50 -0700135 @Override
136 public boolean onSingleTapUp(MotionEvent ev) {
137 if (mClickedItemIndex != -1) {
138 getChildAt(mClickedItemIndex).performClick();
139 mClickedItemIndex = -1;
140 return true;
141 }
142 return super.onSingleTapUp(ev);
143 }
144 };
Justin Klaassenda3b3692016-02-05 13:45:29 -0800145
146 private final GestureDetector mGestureDetector;
147
148 private int mClickedItemIndex = -1;
149
Justin Klaassen4b3af052014-05-27 17:53:10 -0700150 public CalculatorPadViewPager(Context context) {
Justin Klaassen06c49442015-06-04 14:39:27 -0700151 this(context, null /* attrs */);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700152 }
153
154 public CalculatorPadViewPager(Context context, AttributeSet attrs) {
155 super(context, attrs);
156
Justin Klaassenda3b3692016-02-05 13:45:29 -0800157 mGestureDetector = new GestureDetector(context, mGestureWatcher);
158 mGestureDetector.setIsLongpressEnabled(false);
159
Justin Klaassen4b3af052014-05-27 17:53:10 -0700160 setAdapter(mStaticPagerAdapter);
Justin Klaassen06c49442015-06-04 14:39:27 -0700161 setBackgroundColor(Color.BLACK);
Annie Chin26e159e2016-05-18 15:17:14 -0700162 setPageMargin(-getResources().getDimensionPixelSize(R.dimen.pad_page_margin));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700163 setPageTransformer(false, mPageTransformer);
Justin Klaassen06c49442015-06-04 14:39:27 -0700164 addOnPageChangeListener(mOnPageChangeListener);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700165 }
166
167 @Override
168 protected void onFinishInflate() {
169 super.onFinishInflate();
170
171 // Invalidate the adapter's data set since children may have been added during inflation.
Justin Klaassen06c49442015-06-04 14:39:27 -0700172 getAdapter().notifyDataSetChanged();
173
174 // Let page change listener know about our initial position.
175 mOnPageChangeListener.onPageSelected(getCurrentItem());
176 }
177
178 @Override
179 public boolean onInterceptTouchEvent(MotionEvent ev) {
Justin Klaassen788431a2016-07-19 21:42:50 -0700180 // Always intercept touch events when a11y focused since otherwise they will be
181 // incorrectly offset by a11y before being dispatched to children.
182 boolean shouldIntercept = isAccessibilityFocused() || super.onInterceptTouchEvent(ev);
Justin Klaassen06c49442015-06-04 14:39:27 -0700183
184 // Only allow the current item to receive touch events.
185 if (!shouldIntercept && ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
186 final int x = (int) ev.getX() + getScrollX();
187 final int y = (int) ev.getY() + getScrollY();
188
189 final int childCount = getChildCount();
190 for (int i = childCount - 1; i >= 0; --i) {
191 final int childIndex = getChildDrawingOrder(childCount, i);
192 final View child = getChildAt(childIndex);
Justin Klaassen788431a2016-07-19 21:42:50 -0700193 if (child.isAccessibilityFocused()) {
194 // If a child is a11y focused then we must always intercept the touch event
195 // since it will be incorrectly offset by a11y.
196 shouldIntercept = true;
Justin Klaassenda3b3692016-02-05 13:45:29 -0800197 mClickedItemIndex = childIndex;
Justin Klaassen06c49442015-06-04 14:39:27 -0700198 break;
Justin Klaassen788431a2016-07-19 21:42:50 -0700199 } else if (child.getVisibility() == VISIBLE
200 && x >= child.getLeft() && x < child.getRight()
201 && y >= child.getTop() && y < child.getBottom()) {
202 shouldIntercept = childIndex != getCurrentItem();
203 mClickedItemIndex = childIndex;
204 // continue; since another child may be a11y focused.
Justin Klaassen06c49442015-06-04 14:39:27 -0700205 }
206 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700207 }
Justin Klaassen06c49442015-06-04 14:39:27 -0700208
209 return shouldIntercept;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700210 }
Justin Klaassenda3b3692016-02-05 13:45:29 -0800211
212 @Override
213 public boolean onTouchEvent(MotionEvent ev) {
214 // Allow both the gesture detector and super to handle the touch event so they both see
215 // the full sequence of events. This should be safe since the gesture detector only
216 // handle clicks and super only handles swipes.
217 mGestureDetector.onTouchEvent(ev);
218 return super.onTouchEvent(ev);
219 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700220}