• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.wm.shell.back
18 
19 import android.view.MotionEvent
20 import android.view.VelocityTracker
21 
22 internal class ProgressVelocityTracker {
23     private val velocityTracker: VelocityTracker = VelocityTracker.obtain()
24     private var downTime = -1L
25 
addPositionnull26     fun addPosition(timeMillis: Long, position: Float) {
27         if (downTime == -1L) downTime = timeMillis
28         velocityTracker.addMovement(
29             MotionEvent.obtain(
30                 /* downTime */ downTime,
31                 /* eventTime */ timeMillis,
32                 /* action */ MotionEvent.ACTION_MOVE,
33                 /* x */ position,
34                 /* y */ 0f,
35                 /* metaState */0
36             )
37         )
38     }
39 
40     /** calculates current velocity (unit: progress per second) */
calculateVelocitynull41     fun calculateVelocity(): Float {
42         velocityTracker.computeCurrentVelocity(1000)
43         return velocityTracker.xVelocity
44     }
45 
resetTrackingnull46     fun resetTracking() {
47         velocityTracker.clear()
48         downTime = -1L
49     }
50 }