ViewCompat.java revision 560114f591be31d0fb73c26a1ee1cc0a15184aba
1bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell/*
2bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Copyright (C) 2011 The Android Open Source Project
3bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
4bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * you may not use this file except in compliance with the License.
6bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * You may obtain a copy of the License at
7bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
8bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
10bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Unless required by applicable law or agreed to in writing, software
11bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * See the License for the specific language governing permissions and
14bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * limitations under the License.
15bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell */
16bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
17bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powellpackage android.support.v4.view;
18bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
19bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powellimport android.view.View;
20bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
21bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell/**
22bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Helper for accessing newer features in View.
23bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell */
24bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powellpublic class ViewCompat {
25560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    /**
26560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     * Always allow a user to over-scroll this view, provided it is a
27560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     * view that can scroll.
28560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     */
29560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    public static final int OVER_SCROLL_ALWAYS = 0;
30560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
31560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    /**
32560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     * Allow a user to over-scroll this view only if the content is large
33560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     * enough to meaningfully scroll, provided it is a view that can scroll.
34560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     */
35560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
36560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
37560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    /**
38560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     * Never allow a user to over-scroll this view.
39560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell     */
40560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    public static final int OVER_SCROLL_NEVER = 2;
41560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
42bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    interface ViewCompatImpl {
43bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollHorizontally(View v, int direction);
44bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollVertically(View v, int direction);
45560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public int getOverScrollMode(View v);
46560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public void setOverScrollMode(View v, int mode);
47bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
48bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
49bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    static class BaseViewCompatImpl implements ViewCompatImpl {
50bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollHorizontally(View v, int direction) {
51bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            return false;
52bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        }
53bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollVertically(View v, int direction) {
54bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            return false;
55bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        }
56560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public int getOverScrollMode(View v) {
57560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell            return OVER_SCROLL_NEVER;
58560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        }
59560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public void setOverScrollMode(View v, int mode) {
60560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell            // Do nothing; API doesn't exist
61560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        }
62bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
63bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
64560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    static class GBViewCompatImpl extends BaseViewCompatImpl {
65560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public int getOverScrollMode(View v) {
66560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell            return ViewCompatGingerbread.getOverScrollMode(v);
67560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        }
68560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        public void setOverScrollMode(View v, int mode) {
69560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell            ViewCompatGingerbread.setOverScrollMode(v, mode);
70560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        }
71560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    }
72560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
73560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    static class ICSViewCompatImpl extends GBViewCompatImpl {
74bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollHorizontally(View v, int direction) {
75bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            return ViewCompatICS.canScrollHorizontally(v, direction);
76bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        }
77bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        public boolean canScrollVertically(View v, int direction) {
78bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            return ViewCompatICS.canScrollVertically(v, direction);
79bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        }
80bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
81bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
82bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    static final ViewCompatImpl IMPL;
83bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    static {
84560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        final int version = android.os.Build.VERSION.SDK_INT;
85560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        if (version >= 14) {
86bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            IMPL = new ICSViewCompatImpl();
87560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        } else if (version >= 9) {
88560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell            IMPL = new GBViewCompatImpl();
89bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        } else {
90bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell            IMPL = new BaseViewCompatImpl();
91bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        }
92bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
93bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
94bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    public static boolean canScrollHorizontally(View v, int direction) {
95bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        return IMPL.canScrollHorizontally(v, direction);
96bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
97bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
98bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    public static boolean canScrollVertically(View v, int direction) {
99bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell        return IMPL.canScrollVertically(v, direction);
100bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    }
101560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
102560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    public static int getOverScrollMode(View v) {
103560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        return IMPL.getOverScrollMode(v);
104560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    }
105560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
106560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    public static void setOverScrollMode(View v, int mode) {
107560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell        IMPL.setOverScrollMode(v, mode);
108560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    }
109bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell}
110