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