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