1/*
2 * Copyright (C) 2014 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
17package com.android.systemui.statusbar.phone;
18
19import android.util.Pools;
20import android.view.MotionEvent;
21import android.view.VelocityTracker;
22
23/**
24 * An implementation of {@link VelocityTrackerInterface} using the platform-standard
25 * {@link VelocityTracker}.
26 */
27public class PlatformVelocityTracker implements VelocityTrackerInterface {
28
29    private static final Pools.SynchronizedPool<PlatformVelocityTracker> sPool =
30            new Pools.SynchronizedPool<>(2);
31
32    private VelocityTracker mTracker;
33
34    public static PlatformVelocityTracker obtain() {
35        PlatformVelocityTracker tracker = sPool.acquire();
36        if (tracker == null) {
37            tracker = new PlatformVelocityTracker();
38        }
39        tracker.setTracker(VelocityTracker.obtain());
40        return tracker;
41    }
42
43    public void setTracker(VelocityTracker tracker) {
44        mTracker = tracker;
45    }
46
47    @Override
48    public void addMovement(MotionEvent event) {
49        mTracker.addMovement(event);
50    }
51
52    @Override
53    public void computeCurrentVelocity(int units) {
54        mTracker.computeCurrentVelocity(units);
55    }
56
57    @Override
58    public float getXVelocity() {
59        return mTracker.getXVelocity();
60    }
61
62    @Override
63    public float getYVelocity() {
64        return mTracker.getYVelocity();
65    }
66
67    @Override
68    public void recycle() {
69        mTracker.recycle();
70        sPool.release(this);
71    }
72}
73