1 /*
2  * Copyright (C) 2019 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 
17 package com.android.launcher3.touch;
18 
19 import android.content.res.Resources;
20 import android.graphics.Rect;
21 import android.view.MotionEvent;
22 import android.view.VelocityTracker;
23 import android.view.View;
24 import android.view.accessibility.AccessibilityEvent;
25 
26 import com.android.launcher3.Utilities;
27 
28 public class DefaultPagedViewHandler implements PagedOrientationHandler {
29     @Override
getPrimaryValue(int x, int y)30     public int getPrimaryValue(int x, int y) {
31         return x;
32     }
33 
34     @Override
getSecondaryValue(int x, int y)35     public int getSecondaryValue(int x, int y) {
36         return y;
37     }
38 
39     @Override
getPrimaryValue(float x, float y)40     public float getPrimaryValue(float x, float y) {
41         return x;
42     }
43 
44     @Override
getSecondaryValue(float x, float y)45     public float getSecondaryValue(float x, float y) {
46         return y;
47     }
48 
49     @Override
setPrimary(T target, Int2DAction<T> action, int param)50     public <T> void setPrimary(T target, Int2DAction<T> action, int param) {
51         action.call(target, param, 0);
52     }
53 
54     @Override
setPrimary(T target, Float2DAction<T> action, float param)55     public <T> void setPrimary(T target, Float2DAction<T> action, float param) {
56         action.call(target, param, 0);
57     }
58 
59     @Override
getPrimaryDirection(MotionEvent event, int pointerIndex)60     public float getPrimaryDirection(MotionEvent event, int pointerIndex) {
61         return event.getX(pointerIndex);
62     }
63 
64     @Override
getPrimaryVelocity(VelocityTracker velocityTracker, int pointerId)65     public float getPrimaryVelocity(VelocityTracker velocityTracker, int pointerId) {
66         return velocityTracker.getXVelocity(pointerId);
67     }
68 
69     @Override
getMeasuredSize(View view)70     public int getMeasuredSize(View view) {
71         return view.getMeasuredWidth();
72     }
73 
74     @Override
getPrimaryScroll(View view)75     public int getPrimaryScroll(View view) {
76         return view.getScrollX();
77     }
78 
79     @Override
getPrimaryScale(View view)80     public float getPrimaryScale(View view) {
81         return view.getScaleX();
82     }
83 
84     @Override
setMaxScroll(AccessibilityEvent event, int maxScroll)85     public void setMaxScroll(AccessibilityEvent event, int maxScroll) {
86         event.setMaxScrollX(maxScroll);
87     }
88 
89     @Override
getRecentsRtlSetting(Resources resources)90     public boolean getRecentsRtlSetting(Resources resources) {
91         return !Utilities.isRtl(resources);
92     }
93 
94     @Override
getChildStart(View view)95     public int getChildStart(View view) {
96         return view.getLeft();
97     }
98 
99     @Override
getCenterForPage(View view, Rect insets)100     public int getCenterForPage(View view, Rect insets) {
101         return (view.getPaddingTop() + view.getMeasuredHeight() + insets.top
102             - insets.bottom - view.getPaddingBottom()) / 2;
103     }
104 
105     @Override
getScrollOffsetStart(View view, Rect insets)106     public int getScrollOffsetStart(View view, Rect insets) {
107         return insets.left + view.getPaddingLeft();
108     }
109 
110     @Override
getScrollOffsetEnd(View view, Rect insets)111     public int getScrollOffsetEnd(View view, Rect insets) {
112         return view.getWidth() - view.getPaddingRight() - insets.right;
113     }
114 
115     @Override
getChildBounds(View child, int childStart, int pageCenter, boolean layoutChild)116     public ChildBounds getChildBounds(View child, int childStart, int pageCenter,
117             boolean layoutChild) {
118         final int childWidth = child.getMeasuredWidth();
119         final int childRight = childStart + childWidth;
120         final int childHeight = child.getMeasuredHeight();
121         final int childTop = pageCenter - childHeight / 2;
122         if (layoutChild) {
123             child.layout(childStart, childTop, childRight, childTop + childHeight);
124         }
125         return new ChildBounds(childWidth, childHeight, childRight, childTop);
126     }
127 
128 }
129