1/* 2 * Copyright (C) 2013 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 19/** 20 * Helper for accessing features in <code>ScaleGestureDetector</code> introduced 21 * after API level 19 (KitKat) in a backwards compatible fashion. 22 */ 23public final class ScaleGestureDetectorCompat { 24 static final ScaleGestureDetectorImpl IMPL; 25 26 interface ScaleGestureDetectorImpl { 27 28 public void setQuickScaleEnabled(Object o, boolean enabled); 29 30 public boolean isQuickScaleEnabled(Object o); 31 } 32 33 private static class BaseScaleGestureDetectorImpl implements ScaleGestureDetectorImpl { 34 BaseScaleGestureDetectorImpl() { 35 } 36 37 @Override 38 public void setQuickScaleEnabled(Object o, boolean enabled) { 39 // Intentionally blank 40 } 41 42 @Override 43 public boolean isQuickScaleEnabled(Object o) { 44 return false; 45 } 46 } 47 48 private static class ScaleGestureDetectorCompatKitKatImpl implements ScaleGestureDetectorImpl { 49 ScaleGestureDetectorCompatKitKatImpl() { 50 } 51 52 @Override 53 public void setQuickScaleEnabled(Object o, boolean enabled) { 54 ScaleGestureDetectorCompatKitKat.setQuickScaleEnabled(o, enabled); 55 } 56 57 @Override 58 public boolean isQuickScaleEnabled(Object o) { 59 return ScaleGestureDetectorCompatKitKat.isQuickScaleEnabled(o); 60 } 61 } 62 63 static { 64 final int version = android.os.Build.VERSION.SDK_INT; 65 if (version >= 19) { // KitKat 66 IMPL = new ScaleGestureDetectorCompatKitKatImpl(); 67 } else { 68 IMPL = new BaseScaleGestureDetectorImpl(); 69 } 70 } 71 72 private ScaleGestureDetectorCompat() {} 73 74 /** 75 * Set whether the associated <code>OnScaleGestureListener</code> should receive onScale 76 * callbacks when the user performs a doubleTap followed by a swipe. Note that this is enabled 77 * by default if the app targets API 19 and newer. 78 * @param enabled true to enable quick scaling, false to disable 79 */ 80 public static void setQuickScaleEnabled(Object scaleGestureDetector, boolean enabled) { 81 IMPL.setQuickScaleEnabled(scaleGestureDetector, enabled); 82 } 83 84 /** 85 * Return whether the quick scale gesture, in which the user performs a double tap followed by a 86 * swipe, should perform scaling. See <code>setQuickScaleEnabled(Object, boolean)<code>. 87 */ 88 public static boolean isQuickScaleEnabled(Object scaleGestureDetector) { 89 return IMPL.isQuickScaleEnabled(scaleGestureDetector); // KitKat 90 } 91} 92