1 /*
2  * Copyright (C) 2018 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.systemui.qs.touch;
17 
18 /**
19  * Utility methods for overscroll damping and related effect.
20  *
21  * Copied from packages/apps/Launcher3/src/com/android/launcher3/touch/OverScroll.java
22  */
23 public class OverScroll {
24 
25     private static final float OVERSCROLL_DAMP_FACTOR = 0.07f;
26 
27     /**
28      * This curve determines how the effect of scrolling over the limits of the page diminishes
29      * as the user pulls further and further from the bounds
30      *
31      * @param f The percentage of how much the user has overscrolled.
32      * @return A transformed percentage based on the influence curve.
33      */
overScrollInfluenceCurve(float f)34     private static float overScrollInfluenceCurve(float f) {
35         f -= 1.0f;
36         return f * f * f + 1.0f;
37     }
38 
39     /**
40      * @param amount The original amount overscrolled.
41      * @param max The maximum amount that the View can overscroll.
42      * @return The dampened overscroll amount.
43      */
dampedScroll(float amount, int max)44     public static int dampedScroll(float amount, int max) {
45         if (Float.compare(amount, 0) == 0) return 0;
46 
47         float f = amount / max;
48         f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f)));
49 
50         // Clamp this factor, f, to -1 < f < 1
51         if (Math.abs(f) >= 1) {
52             f /= Math.abs(f);
53         }
54 
55         return Math.round(OVERSCROLL_DAMP_FACTOR * f * max);
56     }
57 }
58