blob: ee311f4bba49474337b965fb8104f7defe2ca5e5 [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);
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 Klaassen4b3af052014-05-27 17:53:10 -070048 }
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 Klaassen0587d222014-06-19 10:44:21 -070062 return position == 1 ? 7.0f / 9.0f : 1.0f;
Justin Klaassen4b3af052014-05-27 17:53:10 -070063 }
64 };
Justin Klaassen5c324202014-06-10 20:27:20 -070065
66 private final OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {
Justin Klaassen5c324202014-06-10 20:27:20 -070067 @Override
68 public void onPageSelected(int position) {
Justin Klaassen06c49442015-06-04 14:39:27 -070069 for (int i = getChildCount() - 1; i >= 0; --i) {
Justin Klaassen3d7653e2015-06-08 15:27:55 -070070 final View child = getChildAt(i);
Justin Klaassenda3b3692016-02-05 13:45:29 -080071 // Only the "peeking" or covered page should be clickable.
72 child.setClickable(i != position);
73
Justin Klaassen3d7653e2015-06-08 15:27:55 -070074 // Prevent clicks and accessibility focus from going through to descendants of
75 // other pages which are covered by the current page.
Justin Klaassen3d7653e2015-06-08 15:27:55 -070076 child.setImportantForAccessibility(i == position
Justin Klaassen06c49442015-06-04 14:39:27 -070077 ? IMPORTANT_FOR_ACCESSIBILITY_AUTO
Justin Klaassen3d7653e2015-06-08 15:27:55 -070078 : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
Justin Klaassen5c324202014-06-10 20:27:20 -070079 }
80 }
81 };
82
Justin Klaassen4b3af052014-05-27 17:53:10 -070083 private final PageTransformer mPageTransformer = new PageTransformer() {
84 @Override
85 public void transformPage(View view, float position) {
Justin Klaassen5c324202014-06-10 20:27:20 -070086 if (position < 0.0f) {
Justin Klaassen4b3af052014-05-27 17:53:10 -070087 // Pin the left page to the left side.
88 view.setTranslationX(getWidth() * -position);
Justin Klaassen5c324202014-06-10 20:27:20 -070089 view.setAlpha(Math.max(1.0f + position, 0.0f));
Justin Klaassen4b3af052014-05-27 17:53:10 -070090 } else {
Justin Klaassen5c324202014-06-10 20:27:20 -070091 // Use the default slide transition when moving to the next page.
92 view.setTranslationX(0.0f);
93 view.setAlpha(1.0f);
Justin Klaassen4b3af052014-05-27 17:53:10 -070094 }
95 }
96 };
97
Justin Klaassenda3b3692016-02-05 13:45:29 -080098 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 Klaassen4b3af052014-05-27 17:53:10 -0700121 public CalculatorPadViewPager(Context context) {
Justin Klaassen06c49442015-06-04 14:39:27 -0700122 this(context, null /* attrs */);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700123 }
124
125 public CalculatorPadViewPager(Context context, AttributeSet attrs) {
126 super(context, attrs);
127
Justin Klaassenda3b3692016-02-05 13:45:29 -0800128 mGestureDetector = new GestureDetector(context, mGestureWatcher);
129 mGestureDetector.setIsLongpressEnabled(false);
130
Justin Klaassen4b3af052014-05-27 17:53:10 -0700131 setAdapter(mStaticPagerAdapter);
Justin Klaassen06c49442015-06-04 14:39:27 -0700132 setBackgroundColor(Color.BLACK);
Annie Chin26e159e2016-05-18 15:17:14 -0700133 setPageMargin(-getResources().getDimensionPixelSize(R.dimen.pad_page_margin));
Justin Klaassen4b3af052014-05-27 17:53:10 -0700134 setPageTransformer(false, mPageTransformer);
Justin Klaassen06c49442015-06-04 14:39:27 -0700135 addOnPageChangeListener(mOnPageChangeListener);
Justin Klaassen4b3af052014-05-27 17:53:10 -0700136 }
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 Klaassen06c49442015-06-04 14:39:27 -0700143 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 Klaassenda3b3692016-02-05 13:45:29 -0800166 mClickedItemIndex = childIndex;
Justin Klaassen06c49442015-06-04 14:39:27 -0700167 break;
168 }
169 }
Justin Klaassen4b3af052014-05-27 17:53:10 -0700170 }
Justin Klaassen06c49442015-06-04 14:39:27 -0700171
172 return shouldIntercept;
Justin Klaassen4b3af052014-05-27 17:53:10 -0700173 }
Justin Klaassenda3b3692016-02-05 13:45:29 -0800174
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 Klaassen4b3af052014-05-27 17:53:10 -0700183}