1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 package com.android.launcher3.workprofile;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.MotionEvent;
21 
22 import com.android.launcher3.PagedView;
23 
24 /**
25  *  A {@link PagedView} for showing different views for the personal and work profile respectively.
26  */
27 public class PersonalWorkPagedView extends PagedView<PersonalWorkSlidingTabStrip> {
28 
29     static final float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
30     static final float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
31     static final float TOUCH_SLOP_DAMPING_FACTOR = 4;
32 
PersonalWorkPagedView(Context context)33     public PersonalWorkPagedView(Context context) {
34         this(context, null);
35     }
36 
PersonalWorkPagedView(Context context, AttributeSet attrs)37     public PersonalWorkPagedView(Context context, AttributeSet attrs) {
38         this(context, attrs, 0);
39     }
40 
PersonalWorkPagedView(Context context, AttributeSet attrs, int defStyle)41     public PersonalWorkPagedView(Context context, AttributeSet attrs, int defStyle) {
42         super(context, attrs, defStyle);
43     }
44 
45     @Override
getCurrentPageDescription()46     protected String getCurrentPageDescription() {
47         // Not necessary, tab-bar already has two tabs with their own descriptions.
48         return "";
49     }
50 
51     @Override
onScrollChanged(int l, int t, int oldl, int oldt)52     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
53         super.onScrollChanged(l, t, oldl, oldt);
54         mPageIndicator.setScroll(l, mMaxScroll);
55     }
56 
57     @Override
determineScrollingStart(MotionEvent ev)58     protected void determineScrollingStart(MotionEvent ev) {
59         float absDeltaX = Math.abs(ev.getX() - getDownMotionX());
60         float absDeltaY = Math.abs(ev.getY() - getDownMotionY());
61 
62         if (Float.compare(absDeltaX, 0f) == 0) return;
63 
64         float slope = absDeltaY / absDeltaX;
65         float theta = (float) Math.atan(slope);
66 
67         if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) {
68             cancelCurrentPageLongPress();
69         }
70 
71         if (theta > MAX_SWIPE_ANGLE) {
72             return;
73         } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
74             theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
75             float extraRatio = (float)
76                     Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
77             super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
78         } else {
79             super.determineScrollingStart(ev);
80         }
81     }
82 
83     @Override
hasOverlappingRendering()84     public boolean hasOverlappingRendering() {
85         return false;
86     }
87 
88     @Override
canScroll(float absVScroll, float absHScroll)89     protected boolean canScroll(float absVScroll, float absHScroll) {
90         return (absHScroll > absVScroll) && super.canScroll(absVScroll, absHScroll);
91     }
92 }
93