ViewCompat.java revision 560114f591be31d0fb73c26a1ee1cc0a15184aba
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.View;
20
21/**
22 * Helper for accessing newer features in View.
23 */
24public class ViewCompat {
25    /**
26     * Always allow a user to over-scroll this view, provided it is a
27     * view that can scroll.
28     */
29    public static final int OVER_SCROLL_ALWAYS = 0;
30
31    /**
32     * Allow a user to over-scroll this view only if the content is large
33     * enough to meaningfully scroll, provided it is a view that can scroll.
34     */
35    public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
36
37    /**
38     * Never allow a user to over-scroll this view.
39     */
40    public static final int OVER_SCROLL_NEVER = 2;
41
42    interface ViewCompatImpl {
43        public boolean canScrollHorizontally(View v, int direction);
44        public boolean canScrollVertically(View v, int direction);
45        public int getOverScrollMode(View v);
46        public void setOverScrollMode(View v, int mode);
47    }
48
49    static class BaseViewCompatImpl implements ViewCompatImpl {
50        public boolean canScrollHorizontally(View v, int direction) {
51            return false;
52        }
53        public boolean canScrollVertically(View v, int direction) {
54            return false;
55        }
56        public int getOverScrollMode(View v) {
57            return OVER_SCROLL_NEVER;
58        }
59        public void setOverScrollMode(View v, int mode) {
60            // Do nothing; API doesn't exist
61        }
62    }
63
64    static class GBViewCompatImpl extends BaseViewCompatImpl {
65        public int getOverScrollMode(View v) {
66            return ViewCompatGingerbread.getOverScrollMode(v);
67        }
68        public void setOverScrollMode(View v, int mode) {
69            ViewCompatGingerbread.setOverScrollMode(v, mode);
70        }
71    }
72
73    static class ICSViewCompatImpl extends GBViewCompatImpl {
74        public boolean canScrollHorizontally(View v, int direction) {
75            return ViewCompatICS.canScrollHorizontally(v, direction);
76        }
77        public boolean canScrollVertically(View v, int direction) {
78            return ViewCompatICS.canScrollVertically(v, direction);
79        }
80    }
81
82    static final ViewCompatImpl IMPL;
83    static {
84        final int version = android.os.Build.VERSION.SDK_INT;
85        if (version >= 14) {
86            IMPL = new ICSViewCompatImpl();
87        } else if (version >= 9) {
88            IMPL = new GBViewCompatImpl();
89        } else {
90            IMPL = new BaseViewCompatImpl();
91        }
92    }
93
94    public static boolean canScrollHorizontally(View v, int direction) {
95        return IMPL.canScrollHorizontally(v, direction);
96    }
97
98    public static boolean canScrollVertically(View v, int direction) {
99        return IMPL.canScrollVertically(v, direction);
100    }
101
102    public static int getOverScrollMode(View v) {
103        return IMPL.getOverScrollMode(v);
104    }
105
106    public static void setOverScrollMode(View v, int mode) {
107        IMPL.setOverScrollMode(v, mode);
108    }
109}
110