/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v4.view; import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; import android.view.View; import android.view.accessibility.AccessibilityEvent; /** * Helper for accessing newer features in View. */ public class ViewCompat { /** * Always allow a user to over-scroll this view, provided it is a * view that can scroll. */ public static final int OVER_SCROLL_ALWAYS = 0; /** * Allow a user to over-scroll this view only if the content is large * enough to meaningfully scroll, provided it is a view that can scroll. */ public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1; /** * Never allow a user to over-scroll this view. */ public static final int OVER_SCROLL_NEVER = 2; interface ViewCompatImpl { public boolean canScrollHorizontally(View v, int direction); public boolean canScrollVertically(View v, int direction); public int getOverScrollMode(View v); public void setOverScrollMode(View v, int mode); public void onInitializeAccessibilityEvent(View v, AccessibilityEvent event); public void onPopulateAccessibilityEvent(View v, AccessibilityEvent event); public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfoCompat info); public void setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate); } static class BaseViewCompatImpl implements ViewCompatImpl { public boolean canScrollHorizontally(View v, int direction) { return false; } public boolean canScrollVertically(View v, int direction) { return false; } public int getOverScrollMode(View v) { return OVER_SCROLL_NEVER; } public void setOverScrollMode(View v, int mode) { // Do nothing; API doesn't exist } public void setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate) { // Do nothing; API doesn't exist } public void onPopulateAccessibilityEvent(View v, AccessibilityEvent event) { // Do nothing; API doesn't exist } public void onInitializeAccessibilityEvent(View v, AccessibilityEvent event) { // Do nothing; API doesn't exist } public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfoCompat info) { // Do nothing; API doesn't exist } } static class GBViewCompatImpl extends BaseViewCompatImpl { @Override public int getOverScrollMode(View v) { return ViewCompatGingerbread.getOverScrollMode(v); } @Override public void setOverScrollMode(View v, int mode) { ViewCompatGingerbread.setOverScrollMode(v, mode); } } static class ICSViewCompatImpl extends GBViewCompatImpl { @Override public boolean canScrollHorizontally(View v, int direction) { return ViewCompatICS.canScrollHorizontally(v, direction); } @Override public boolean canScrollVertically(View v, int direction) { return ViewCompatICS.canScrollVertically(v, direction); } @Override public void onPopulateAccessibilityEvent(View v, AccessibilityEvent event) { ViewCompatICS.onPopulateAccessibilityEvent(v, event); } @Override public void onInitializeAccessibilityEvent(View v, AccessibilityEvent event) { ViewCompatICS.onInitializeAccessibilityEvent(v, event); } @Override public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfoCompat info) { ViewCompatICS.onInitializeAccessibilityNodeInfo(v, info.getImpl()); } @Override public void setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate) { ViewCompatICS.setAccessibilityDelegate(v, delegate.getBridge()); } } static final ViewCompatImpl IMPL; static { final int version = android.os.Build.VERSION.SDK_INT; if (version >= 14) { IMPL = new ICSViewCompatImpl(); } else if (version >= 9) { IMPL = new GBViewCompatImpl(); } else { IMPL = new BaseViewCompatImpl(); } } public static boolean canScrollHorizontally(View v, int direction) { return IMPL.canScrollHorizontally(v, direction); } public static boolean canScrollVertically(View v, int direction) { return IMPL.canScrollVertically(v, direction); } public static int getOverScrollMode(View v) { return IMPL.getOverScrollMode(v); } public static void setOverScrollMode(View v, int mode) { IMPL.setOverScrollMode(v, mode); } public static void onPopulateAccessibilityEvent(View v, AccessibilityEvent event) { IMPL.onPopulateAccessibilityEvent(v, event); } public static void onInitializeAccessibilityEvent(View v, AccessibilityEvent event) { IMPL.onInitializeAccessibilityEvent(v, event); } public static void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfoCompat info) { IMPL.onInitializeAccessibilityNodeInfo(v, info); } public static void setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate) { IMPL.setAccessibilityDelegate(v, delegate); } }