1 /* 2 * Copyright (C) 2011 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 android.support.v4.view; 18 19 import android.view.VelocityTracker; 20 21 /** 22 * Helper for accessing features in {@link VelocityTracker} 23 * introduced after API level 4 in a backwards compatible fashion. 24 */ 25 public class VelocityTrackerCompat { 26 /** 27 * Interface for the full API. 28 */ 29 interface VelocityTrackerVersionImpl { getXVelocity(VelocityTracker tracker, int pointerId)30 public float getXVelocity(VelocityTracker tracker, int pointerId); getYVelocity(VelocityTracker tracker, int pointerId)31 public float getYVelocity(VelocityTracker tracker, int pointerId); 32 } 33 34 /** 35 * Interface implementation that doesn't use anything about v4 APIs. 36 */ 37 static class BaseVelocityTrackerVersionImpl implements VelocityTrackerVersionImpl { 38 @Override getXVelocity(VelocityTracker tracker, int pointerId)39 public float getXVelocity(VelocityTracker tracker, int pointerId) { 40 return tracker.getXVelocity(); 41 } 42 @Override getYVelocity(VelocityTracker tracker, int pointerId)43 public float getYVelocity(VelocityTracker tracker, int pointerId) { 44 return tracker.getYVelocity(); 45 } 46 } 47 48 /** 49 * Interface implementation for devices with at least v11 APIs. 50 */ 51 static class HoneycombVelocityTrackerVersionImpl implements VelocityTrackerVersionImpl { 52 @Override getXVelocity(VelocityTracker tracker, int pointerId)53 public float getXVelocity(VelocityTracker tracker, int pointerId) { 54 return VelocityTrackerCompatHoneycomb.getXVelocity(tracker, pointerId); 55 } 56 @Override getYVelocity(VelocityTracker tracker, int pointerId)57 public float getYVelocity(VelocityTracker tracker, int pointerId) { 58 return VelocityTrackerCompatHoneycomb.getYVelocity(tracker, pointerId); 59 } 60 } 61 62 /** 63 * Select the correct implementation to use for the current platform. 64 */ 65 static final VelocityTrackerVersionImpl IMPL; 66 static { 67 if (android.os.Build.VERSION.SDK_INT >= 11) { 68 IMPL = new HoneycombVelocityTrackerVersionImpl(); 69 } else { 70 IMPL = new BaseVelocityTrackerVersionImpl(); 71 } 72 } 73 74 // ------------------------------------------------------------------- 75 76 /** 77 * Call {@link VelocityTracker#getXVelocity(int)}. 78 * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} device, 79 * returns {@link VelocityTracker#getXVelocity()}. 80 */ getXVelocity(VelocityTracker tracker, int pointerId)81 public static float getXVelocity(VelocityTracker tracker, int pointerId) { 82 return IMPL.getXVelocity(tracker, pointerId); 83 } 84 85 /** 86 * Call {@link VelocityTracker#getYVelocity(int)}. 87 * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} device, 88 * returns {@link VelocityTracker#getYVelocity()}. 89 */ getYVelocity(VelocityTracker tracker, int pointerId)90 public static float getYVelocity(VelocityTracker tracker, int pointerId) { 91 return IMPL.getYVelocity(tracker, pointerId); 92 } 93 } 94