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.ViewConfiguration;
20
21/**
22 * Helper for accessing features in {@link ViewConfiguration}
23 * introduced after API level 4 in a backwards compatible fashion.
24 */
25public final class ViewConfigurationCompat {
26    /**
27     * Interface for the full API.
28     */
29    interface ViewConfigurationVersionImpl {
30        public int getScaledPagingTouchSlop(ViewConfiguration config);
31        public boolean hasPermanentMenuKey(ViewConfiguration config);
32    }
33
34    /**
35     * Interface implementation that doesn't use anything about v4 APIs.
36     */
37    static class BaseViewConfigurationVersionImpl implements ViewConfigurationVersionImpl {
38        @Override
39        public int getScaledPagingTouchSlop(ViewConfiguration config) {
40            return config.getScaledTouchSlop();
41        }
42
43        @Override
44        public boolean hasPermanentMenuKey(ViewConfiguration config) {
45            // Pre-HC devices will always have a menu button
46            return true;
47        }
48    }
49
50    /**
51     * Interface implementation for devices with at least v8 APIs.
52     */
53    static class FroyoViewConfigurationVersionImpl extends BaseViewConfigurationVersionImpl {
54        @Override
55        public int getScaledPagingTouchSlop(ViewConfiguration config) {
56            return ViewConfigurationCompatFroyo.getScaledPagingTouchSlop(config);
57        }
58    }
59
60    /**
61     * Interface implementation for devices with at least v11 APIs.
62     */
63    static class HoneycombViewConfigurationVersionImpl extends FroyoViewConfigurationVersionImpl {
64        @Override
65        public boolean hasPermanentMenuKey(ViewConfiguration config) {
66            // There is no way to check on Honeycomb so we assume false
67            return false;
68        }
69    }
70
71    /**
72     * Interface implementation for devices with at least v14 APIs.
73     */
74    static class IcsViewConfigurationVersionImpl extends HoneycombViewConfigurationVersionImpl {
75        @Override
76        public boolean hasPermanentMenuKey(ViewConfiguration config) {
77            return ViewConfigurationCompatICS.hasPermanentMenuKey(config);
78        }
79    }
80
81    /**
82     * Select the correct implementation to use for the current platform.
83     */
84    static final ViewConfigurationVersionImpl IMPL;
85    static {
86        if (android.os.Build.VERSION.SDK_INT >= 14) {
87            IMPL = new IcsViewConfigurationVersionImpl();
88        } else if (android.os.Build.VERSION.SDK_INT >= 11) {
89            IMPL = new HoneycombViewConfigurationVersionImpl();
90        } else if (android.os.Build.VERSION.SDK_INT >= 8) {
91            IMPL = new FroyoViewConfigurationVersionImpl();
92        } else {
93            IMPL = new BaseViewConfigurationVersionImpl();
94        }
95    }
96
97    // -------------------------------------------------------------------
98
99    /**
100     * Call {@link ViewConfiguration#getScaledPagingTouchSlop()}.
101     * If running on a pre-{@link android.os.Build.VERSION_CODES#FROYO} device,
102     * returns {@link ViewConfiguration#getScaledTouchSlop()}.
103     */
104    public static int getScaledPagingTouchSlop(ViewConfiguration config) {
105        return IMPL.getScaledPagingTouchSlop(config);
106    }
107
108    /**
109     * Report if the device has a permanent menu key available to the user, in a backwards
110     * compatible way.
111     */
112    public static boolean hasPermanentMenuKey(ViewConfiguration config) {
113        return IMPL.hasPermanentMenuKey(config);
114    }
115
116    private ViewConfigurationCompat() {}
117}
118