Blame client/Android/Studio/freeRDPCore/src/main/java/com/freerdp/freerdpcore/presentation/ScrollView2D.java

Packit Service fa4841
/*
Packit Service fa4841
 * Copyright (C) 2006 The Android Open Source Project
Packit Service fa4841
 *
Packit Service fa4841
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit Service fa4841
 * you may not use this file except in compliance with the License.
Packit Service fa4841
 * You may obtain a copy of the License at
Packit Service fa4841
 *
Packit Service fa4841
 *      http://www.apache.org/licenses/LICENSE-2.0
Packit Service fa4841
 *
Packit Service fa4841
 * Unless required by applicable law or agreed to in writing, software
Packit Service fa4841
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit Service fa4841
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit Service fa4841
 * See the License for the specific language governing permissions and
Packit Service fa4841
 * limitations under the License.
Packit Service fa4841
 */
Packit Service fa4841
/*
Packit Service fa4841
 * Revised 5/19/2010 by GORGES
Packit Service fa4841
 * Now supports two-dimensional view scrolling
Packit Service fa4841
 * http://GORGES.us
Packit Service fa4841
 */
Packit Service fa4841
Packit Service fa4841
package com.freerdp.freerdpcore.presentation;
Packit Service fa4841
Packit Service fa4841
import android.content.Context;
Packit Service fa4841
import android.graphics.Rect;
Packit Service fa4841
import android.util.AttributeSet;
Packit Service fa4841
import android.view.FocusFinder;
Packit Service fa4841
import android.view.KeyEvent;
Packit Service fa4841
import android.view.MotionEvent;
Packit Service fa4841
import android.view.VelocityTracker;
Packit Service fa4841
import android.view.View;
Packit Service fa4841
import android.view.ViewConfiguration;
Packit Service fa4841
import android.view.ViewGroup;
Packit Service fa4841
import android.view.ViewParent;
Packit Service fa4841
import android.view.animation.AnimationUtils;
Packit Service fa4841
import android.widget.FrameLayout;
Packit Service fa4841
import android.widget.LinearLayout;
Packit Service fa4841
import android.widget.Scroller;
Packit Service fa4841
import android.widget.TextView;
Packit Service fa4841
Packit Service fa4841
import java.util.List;
Packit Service fa4841
Packit Service fa4841
/**
Packit Service fa4841
 * Layout container for a view hierarchy that can be scrolled by the user,
Packit Service fa4841
 * allowing it to be larger than the physical display.  A TwoDScrollView
Packit Service fa4841
 * is a {@link FrameLayout}, meaning you should place one child in it
Packit Service fa4841
 * containing the entire contents to scroll; this child may itself be a layout
Packit Service fa4841
 * manager with a complex hierarchy of objects.  A child that is often used
Packit Service fa4841
 * is a {@link LinearLayout} in a vertical orientation, presenting a vertical
Packit Service fa4841
 * array of top-level items that the user can scroll through.
Packit Service fa4841
 * 

Packit Service fa4841
 * 

The {@link TextView} class also

Packit Service fa4841
 * takes care of its own scrolling, so does not require a TwoDScrollView, but
Packit Service fa4841
 * using the two together is possible to achieve the effect of a text view
Packit Service fa4841
 * within a larger container.
Packit Service fa4841
 */
Packit Service fa4841
public class ScrollView2D extends FrameLayout
Packit Service fa4841
{
Packit Service fa4841
Packit Service fa4841
	static final int ANIMATED_SCROLL_GAP = 250;
Packit Service fa4841
	static final float MAX_SCROLL_FACTOR = 0.5f;
Packit Service fa4841
	private final Rect mTempRect = new Rect();
Packit Service fa4841
	private ScrollView2DListener scrollView2DListener = null;
Packit Service fa4841
	private long mLastScroll;
Packit Service fa4841
	private Scroller mScroller;
Packit Service fa4841
	private boolean scrollEnabled = true;
Packit Service fa4841
	/**
Packit Service fa4841
	 * Flag to indicate that we are moving focus ourselves. This is so the
Packit Service fa4841
	 * code that watches for focus changes initiated outside this TwoDScrollView
Packit Service fa4841
	 * knows that it does not have to do anything.
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean mTwoDScrollViewMovedFocus;
Packit Service fa4841
	/**
Packit Service fa4841
	 * Position of the last motion event.
Packit Service fa4841
	 */
Packit Service fa4841
	private float mLastMotionY;
Packit Service fa4841
	private float mLastMotionX;
Packit Service fa4841
	/**
Packit Service fa4841
	 * True when the layout has changed but the traversal has not come through yet.
Packit Service fa4841
	 * Ideally the view hierarchy would keep track of this for us.
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean mIsLayoutDirty = true;
Packit Service fa4841
	/**
Packit Service fa4841
	 * The child to give focus to in the event that a child has requested focus while the
Packit Service fa4841
	 * layout is dirty. This prevents the scroll from being wrong if the child has not been
Packit Service fa4841
	 * laid out before requesting focus.
Packit Service fa4841
	 */
Packit Service fa4841
	private View mChildToScrollTo = null;
Packit Service fa4841
	/**
Packit Service fa4841
	 * True if the user is currently dragging this TwoDScrollView around. This is
Packit Service fa4841
	 * not the same as 'is being flinged', which can be checked by
Packit Service fa4841
	 * mScroller.isFinished() (flinging begins when the user lifts his finger).
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean mIsBeingDragged = false;
Packit Service fa4841
	/**
Packit Service fa4841
	 * Determines speed during touch scrolling
Packit Service fa4841
	 */
Packit Service fa4841
	private VelocityTracker mVelocityTracker;
Packit Service fa4841
	/**
Packit Service fa4841
	 * Whether arrow scrolling is animated.
Packit Service fa4841
	 */
Packit Service fa4841
	private int mTouchSlop;
Packit Service fa4841
	private int mMinimumVelocity;
Packit Service fa4841
	private int mMaximumVelocity;
Packit Service fa4841
	public ScrollView2D(Context context)
Packit Service fa4841
	{
Packit Service fa4841
		super(context);
Packit Service fa4841
		initTwoDScrollView();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	public ScrollView2D(Context context, AttributeSet attrs)
Packit Service fa4841
	{
Packit Service fa4841
		super(context, attrs);
Packit Service fa4841
		initTwoDScrollView();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	public ScrollView2D(Context context, AttributeSet attrs, int defStyle)
Packit Service fa4841
	{
Packit Service fa4841
		super(context, attrs, defStyle);
Packit Service fa4841
		initTwoDScrollView();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected float getTopFadingEdgeStrength()
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() == 0)
Packit Service fa4841
		{
Packit Service fa4841
			return 0.0f;
Packit Service fa4841
		}
Packit Service fa4841
		final int length = getVerticalFadingEdgeLength();
Packit Service fa4841
		if (getScrollY() < length)
Packit Service fa4841
		{
Packit Service fa4841
			return getScrollY() / (float)length;
Packit Service fa4841
		}
Packit Service fa4841
		return 1.0f;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected float getBottomFadingEdgeStrength()
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() == 0)
Packit Service fa4841
		{
Packit Service fa4841
			return 0.0f;
Packit Service fa4841
		}
Packit Service fa4841
		final int length = getVerticalFadingEdgeLength();
Packit Service fa4841
		final int bottomEdge = getHeight() - getPaddingBottom();
Packit Service fa4841
		final int span = getChildAt(0).getBottom() - getScrollY() - bottomEdge;
Packit Service fa4841
		if (span < length)
Packit Service fa4841
		{
Packit Service fa4841
			return span / (float)length;
Packit Service fa4841
		}
Packit Service fa4841
		return 1.0f;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected float getLeftFadingEdgeStrength()
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() == 0)
Packit Service fa4841
		{
Packit Service fa4841
			return 0.0f;
Packit Service fa4841
		}
Packit Service fa4841
		final int length = getHorizontalFadingEdgeLength();
Packit Service fa4841
		if (getScrollX() < length)
Packit Service fa4841
		{
Packit Service fa4841
			return getScrollX() / (float)length;
Packit Service fa4841
		}
Packit Service fa4841
		return 1.0f;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected float getRightFadingEdgeStrength()
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() == 0)
Packit Service fa4841
		{
Packit Service fa4841
			return 0.0f;
Packit Service fa4841
		}
Packit Service fa4841
		final int length = getHorizontalFadingEdgeLength();
Packit Service fa4841
		final int rightEdge = getWidth() - getPaddingRight();
Packit Service fa4841
		final int span = getChildAt(0).getRight() - getScrollX() - rightEdge;
Packit Service fa4841
		if (span < length)
Packit Service fa4841
		{
Packit Service fa4841
			return span / (float)length;
Packit Service fa4841
		}
Packit Service fa4841
		return 1.0f;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Disable/Enable scrolling
Packit Service fa4841
	 */
Packit Service fa4841
	public void setScrollEnabled(boolean enable)
Packit Service fa4841
	{
Packit Service fa4841
		scrollEnabled = enable;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * @return The maximum amount this scroll view will scroll in response to
Packit Service fa4841
	 * an arrow event.
Packit Service fa4841
	 */
Packit Service fa4841
	public int getMaxScrollAmountVertical()
Packit Service fa4841
	{
Packit Service fa4841
		return (int)(MAX_SCROLL_FACTOR * getHeight());
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	public int getMaxScrollAmountHorizontal()
Packit Service fa4841
	{
Packit Service fa4841
		return (int)(MAX_SCROLL_FACTOR * getWidth());
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	private void initTwoDScrollView()
Packit Service fa4841
	{
Packit Service fa4841
		mScroller = new Scroller(getContext());
Packit Service fa4841
		setFocusable(true);
Packit Service fa4841
		setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
Packit Service fa4841
		setWillNotDraw(false);
Packit Service fa4841
		final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Packit Service fa4841
		mTouchSlop = configuration.getScaledTouchSlop();
Packit Service fa4841
		mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
Packit Service fa4841
		mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void addView(View child)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			throw new IllegalStateException("TwoDScrollView can host only one direct child");
Packit Service fa4841
		}
Packit Service fa4841
		super.addView(child);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void addView(View child, int index)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			throw new IllegalStateException("TwoDScrollView can host only one direct child");
Packit Service fa4841
		}
Packit Service fa4841
		super.addView(child, index);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void addView(View child, ViewGroup.LayoutParams params)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			throw new IllegalStateException("TwoDScrollView can host only one direct child");
Packit Service fa4841
		}
Packit Service fa4841
		super.addView(child, params);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void addView(View child, int index, ViewGroup.LayoutParams params)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			throw new IllegalStateException("TwoDScrollView can host only one direct child");
Packit Service fa4841
		}
Packit Service fa4841
		super.addView(child, index, params);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * @return Returns true this TwoDScrollView can be scrolled
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean canScroll()
Packit Service fa4841
	{
Packit Service fa4841
		if (!scrollEnabled)
Packit Service fa4841
			return false;
Packit Service fa4841
		View child = getChildAt(0);
Packit Service fa4841
		if (child != null)
Packit Service fa4841
		{
Packit Service fa4841
			int childHeight = child.getHeight();
Packit Service fa4841
			int childWidth = child.getWidth();
Packit Service fa4841
			return (getHeight() < childHeight + getPaddingTop() + getPaddingBottom()) ||
Packit Service fa4841
			    (getWidth() < childWidth + getPaddingLeft() + getPaddingRight());
Packit Service fa4841
		}
Packit Service fa4841
		return false;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public boolean dispatchKeyEvent(KeyEvent event)
Packit Service fa4841
	{
Packit Service fa4841
		// Let the focused view and/or our descendants get the key first
Packit Service fa4841
		boolean handled = super.dispatchKeyEvent(event);
Packit Service fa4841
		if (handled)
Packit Service fa4841
		{
Packit Service fa4841
			return true;
Packit Service fa4841
		}
Packit Service fa4841
		return executeKeyEvent(event);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * You can call this function yourself to have the scroll view perform
Packit Service fa4841
	 * scrolling from a key event, just as if the event had been dispatched to
Packit Service fa4841
	 * it by the view hierarchy.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param event The key event to execute.
Packit Service fa4841
	 * @return Return true if the event was handled, else false.
Packit Service fa4841
	 */
Packit Service fa4841
	public boolean executeKeyEvent(KeyEvent event)
Packit Service fa4841
	{
Packit Service fa4841
		mTempRect.setEmpty();
Packit Service fa4841
		if (!canScroll())
Packit Service fa4841
		{
Packit Service fa4841
			if (isFocused())
Packit Service fa4841
			{
Packit Service fa4841
				View currentFocused = findFocus();
Packit Service fa4841
				if (currentFocused == this)
Packit Service fa4841
					currentFocused = null;
Packit Service fa4841
				View nextFocused =
Packit Service fa4841
				    FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
Packit Service fa4841
				return nextFocused != null && nextFocused != this &&
Packit Service fa4841
				    nextFocused.requestFocus(View.FOCUS_DOWN);
Packit Service fa4841
			}
Packit Service fa4841
			return false;
Packit Service fa4841
		}
Packit Service fa4841
		boolean handled = false;
Packit Service fa4841
		if (event.getAction() == KeyEvent.ACTION_DOWN)
Packit Service fa4841
		{
Packit Service fa4841
			switch (event.getKeyCode())
Packit Service fa4841
			{
Packit Service fa4841
				case KeyEvent.KEYCODE_DPAD_UP:
Packit Service fa4841
					if (!event.isAltPressed())
Packit Service fa4841
					{
Packit Service fa4841
						handled = arrowScroll(View.FOCUS_UP, false);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						handled = fullScroll(View.FOCUS_UP, false);
Packit Service fa4841
					}
Packit Service fa4841
					break;
Packit Service fa4841
				case KeyEvent.KEYCODE_DPAD_DOWN:
Packit Service fa4841
					if (!event.isAltPressed())
Packit Service fa4841
					{
Packit Service fa4841
						handled = arrowScroll(View.FOCUS_DOWN, false);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						handled = fullScroll(View.FOCUS_DOWN, false);
Packit Service fa4841
					}
Packit Service fa4841
					break;
Packit Service fa4841
				case KeyEvent.KEYCODE_DPAD_LEFT:
Packit Service fa4841
					if (!event.isAltPressed())
Packit Service fa4841
					{
Packit Service fa4841
						handled = arrowScroll(View.FOCUS_LEFT, true);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						handled = fullScroll(View.FOCUS_LEFT, true);
Packit Service fa4841
					}
Packit Service fa4841
					break;
Packit Service fa4841
				case KeyEvent.KEYCODE_DPAD_RIGHT:
Packit Service fa4841
					if (!event.isAltPressed())
Packit Service fa4841
					{
Packit Service fa4841
						handled = arrowScroll(View.FOCUS_RIGHT, true);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						handled = fullScroll(View.FOCUS_RIGHT, true);
Packit Service fa4841
					}
Packit Service fa4841
					break;
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		return handled;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public boolean onInterceptTouchEvent(MotionEvent ev)
Packit Service fa4841
	{
Packit Service fa4841
		/*
Packit Service fa4841
		 * This method JUST determines whether we want to intercept the motion.
Packit Service fa4841
		 * If we return true, onMotionEvent will be called and we do the actual
Packit Service fa4841
		 * scrolling there.
Packit Service fa4841
		 *
Packit Service fa4841
		 * Shortcut the most recurring case: the user is in the dragging
Packit Service fa4841
		 * state and he is moving his finger.  We want to intercept this
Packit Service fa4841
		 * motion.
Packit Service fa4841
		 */
Packit Service fa4841
		final int action = ev.getAction();
Packit Service fa4841
		if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged))
Packit Service fa4841
		{
Packit Service fa4841
			return true;
Packit Service fa4841
		}
Packit Service fa4841
		if (!canScroll())
Packit Service fa4841
		{
Packit Service fa4841
			mIsBeingDragged = false;
Packit Service fa4841
			return false;
Packit Service fa4841
		}
Packit Service fa4841
		final float y = ev.getY();
Packit Service fa4841
		final float x = ev.getX();
Packit Service fa4841
		switch (action)
Packit Service fa4841
		{
Packit Service fa4841
			case MotionEvent.ACTION_MOVE:
Packit Service fa4841
				/*
Packit Service fa4841
				 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
Packit Service fa4841
				 * whether the user has moved far enough from his original down touch.
Packit Service fa4841
				 */
Packit Service fa4841
				/*
Packit Service fa4841
				 * Locally do absolute value. mLastMotionY is set to the y value
Packit Service fa4841
				 * of the down event.
Packit Service fa4841
				 */
Packit Service fa4841
				final int yDiff = (int)Math.abs(y - mLastMotionY);
Packit Service fa4841
				final int xDiff = (int)Math.abs(x - mLastMotionX);
Packit Service fa4841
				if (yDiff > mTouchSlop || xDiff > mTouchSlop)
Packit Service fa4841
				{
Packit Service fa4841
					mIsBeingDragged = true;
Packit Service fa4841
				}
Packit Service fa4841
				break;
Packit Service fa4841
Packit Service fa4841
			case MotionEvent.ACTION_DOWN:
Packit Service fa4841
				/* Remember location of down touch */
Packit Service fa4841
				mLastMotionY = y;
Packit Service fa4841
				mLastMotionX = x;
Packit Service fa4841
Packit Service fa4841
				/*
Packit Service fa4841
				 * If being flinged and user touches the screen, initiate drag;
Packit Service fa4841
				 * otherwise don't.  mScroller.isFinished should be false when
Packit Service fa4841
				 * being flinged.
Packit Service fa4841
				 */
Packit Service fa4841
				mIsBeingDragged = !mScroller.isFinished();
Packit Service fa4841
				break;
Packit Service fa4841
Packit Service fa4841
			case MotionEvent.ACTION_CANCEL:
Packit Service fa4841
			case MotionEvent.ACTION_UP:
Packit Service fa4841
				/* Release the drag */
Packit Service fa4841
				mIsBeingDragged = false;
Packit Service fa4841
				break;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		/*
Packit Service fa4841
		 * The only time we want to intercept motion events is if we are in the
Packit Service fa4841
		 * drag mode.
Packit Service fa4841
		 */
Packit Service fa4841
		return mIsBeingDragged;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public boolean onTouchEvent(MotionEvent ev)
Packit Service fa4841
	{
Packit Service fa4841
Packit Service fa4841
		if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0)
Packit Service fa4841
		{
Packit Service fa4841
			// Don't handle edge touches immediately -- they may actually belong to one of our
Packit Service fa4841
			// descendants.
Packit Service fa4841
			return false;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		if (!canScroll())
Packit Service fa4841
		{
Packit Service fa4841
			return false;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		if (mVelocityTracker == null)
Packit Service fa4841
		{
Packit Service fa4841
			mVelocityTracker = VelocityTracker.obtain();
Packit Service fa4841
		}
Packit Service fa4841
		mVelocityTracker.addMovement(ev);
Packit Service fa4841
Packit Service fa4841
		final int action = ev.getAction();
Packit Service fa4841
		final float y = ev.getY();
Packit Service fa4841
		final float x = ev.getX();
Packit Service fa4841
Packit Service fa4841
		switch (action)
Packit Service fa4841
		{
Packit Service fa4841
			case MotionEvent.ACTION_DOWN:
Packit Service fa4841
				/*
Packit Service fa4841
				 * If being flinged and user touches, stop the fling. isFinished
Packit Service fa4841
				 * will be false if being flinged.
Packit Service fa4841
				 */
Packit Service fa4841
				if (!mScroller.isFinished())
Packit Service fa4841
				{
Packit Service fa4841
					mScroller.abortAnimation();
Packit Service fa4841
				}
Packit Service fa4841
Packit Service fa4841
				// Remember where the motion event started
Packit Service fa4841
				mLastMotionY = y;
Packit Service fa4841
				mLastMotionX = x;
Packit Service fa4841
				break;
Packit Service fa4841
			case MotionEvent.ACTION_MOVE:
Packit Service fa4841
				// Scroll to follow the motion event
Packit Service fa4841
				int deltaX = (int)(mLastMotionX - x);
Packit Service fa4841
				int deltaY = (int)(mLastMotionY - y);
Packit Service fa4841
				mLastMotionX = x;
Packit Service fa4841
				mLastMotionY = y;
Packit Service fa4841
Packit Service fa4841
				if (deltaX < 0)
Packit Service fa4841
				{
Packit Service fa4841
					if (getScrollX() < 0)
Packit Service fa4841
					{
Packit Service fa4841
						deltaX = 0;
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				else if (deltaX > 0)
Packit Service fa4841
				{
Packit Service fa4841
					final int rightEdge = getWidth() - getPaddingRight();
Packit Service fa4841
					final int availableToScroll =
Packit Service fa4841
					    getChildAt(0).getRight() - getScrollX() - rightEdge;
Packit Service fa4841
					if (availableToScroll > 0)
Packit Service fa4841
					{
Packit Service fa4841
						deltaX = Math.min(availableToScroll, deltaX);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						deltaX = 0;
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				if (deltaY < 0)
Packit Service fa4841
				{
Packit Service fa4841
					if (getScrollY() < 0)
Packit Service fa4841
					{
Packit Service fa4841
						deltaY = 0;
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				else if (deltaY > 0)
Packit Service fa4841
				{
Packit Service fa4841
					final int bottomEdge = getHeight() - getPaddingBottom();
Packit Service fa4841
					final int availableToScroll =
Packit Service fa4841
					    getChildAt(0).getBottom() - getScrollY() - bottomEdge;
Packit Service fa4841
					if (availableToScroll > 0)
Packit Service fa4841
					{
Packit Service fa4841
						deltaY = Math.min(availableToScroll, deltaY);
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						deltaY = 0;
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				if (deltaY != 0 || deltaX != 0)
Packit Service fa4841
					scrollBy(deltaX, deltaY);
Packit Service fa4841
				break;
Packit Service fa4841
			case MotionEvent.ACTION_UP:
Packit Service fa4841
				final VelocityTracker velocityTracker = mVelocityTracker;
Packit Service fa4841
				velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
Packit Service fa4841
				int initialXVelocity = (int)velocityTracker.getXVelocity();
Packit Service fa4841
				int initialYVelocity = (int)velocityTracker.getYVelocity();
Packit Service fa4841
				if ((Math.abs(initialXVelocity) + Math.abs(initialYVelocity) > mMinimumVelocity) &&
Packit Service fa4841
				    getChildCount() > 0)
Packit Service fa4841
				{
Packit Service fa4841
					fling(-initialXVelocity, -initialYVelocity);
Packit Service fa4841
				}
Packit Service fa4841
				if (mVelocityTracker != null)
Packit Service fa4841
				{
Packit Service fa4841
					mVelocityTracker.recycle();
Packit Service fa4841
					mVelocityTracker = null;
Packit Service fa4841
				}
Packit Service fa4841
		}
Packit Service fa4841
		return true;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Finds the next focusable component that fits in this View's bounds
Packit Service fa4841
	 * (excluding fading edges) pretending that this View's top is located at
Packit Service fa4841
	 * the parameter top.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param topFocus           look for a candidate is the one at the top of the bounds
Packit Service fa4841
	 *                           if topFocus is true, or at the bottom of the bounds if topFocus is
Packit Service fa4841
	 *                           false
Packit Service fa4841
	 * @param top                the top offset of the bounds in which a focusable must be
Packit Service fa4841
	 *                           found (the fading edge is assumed to start at this position)
Packit Service fa4841
	 * @param preferredFocusable the View that has highest priority and will be
Packit Service fa4841
	 *                           returned if it is within my bounds (null is valid)
Packit Service fa4841
	 * @return the next focusable component in the bounds or null if none can be
Packit Service fa4841
	 * found
Packit Service fa4841
	 */
Packit Service fa4841
	private View findFocusableViewInMyBounds(final boolean topFocus, final int top,
Packit Service fa4841
	                                         final boolean leftFocus, final int left,
Packit Service fa4841
	                                         View preferredFocusable)
Packit Service fa4841
	{
Packit Service fa4841
		/*
Packit Service fa4841
		 * The fading edge's transparent side should be considered for focus
Packit Service fa4841
		 * since it's mostly visible, so we divide the actual fading edge length
Packit Service fa4841
		 * by 2.
Packit Service fa4841
		 */
Packit Service fa4841
		final int verticalFadingEdgeLength = getVerticalFadingEdgeLength() / 2;
Packit Service fa4841
		final int topWithoutFadingEdge = top + verticalFadingEdgeLength;
Packit Service fa4841
		final int bottomWithoutFadingEdge = top + getHeight() - verticalFadingEdgeLength;
Packit Service fa4841
		final int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength() / 2;
Packit Service fa4841
		final int leftWithoutFadingEdge = left + horizontalFadingEdgeLength;
Packit Service fa4841
		final int rightWithoutFadingEdge = left + getWidth() - horizontalFadingEdgeLength;
Packit Service fa4841
Packit Service fa4841
		if ((preferredFocusable != null) &&
Packit Service fa4841
		    (preferredFocusable.getTop() < bottomWithoutFadingEdge) &&
Packit Service fa4841
		    (preferredFocusable.getBottom() > topWithoutFadingEdge) &&
Packit Service fa4841
		    (preferredFocusable.getLeft() < rightWithoutFadingEdge) &&
Packit Service fa4841
		    (preferredFocusable.getRight() > leftWithoutFadingEdge))
Packit Service fa4841
		{
Packit Service fa4841
			return preferredFocusable;
Packit Service fa4841
		}
Packit Service fa4841
		return findFocusableViewInBounds(topFocus, topWithoutFadingEdge, bottomWithoutFadingEdge,
Packit Service fa4841
		                                 leftFocus, leftWithoutFadingEdge, rightWithoutFadingEdge);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Finds the next focusable component that fits in the specified bounds.
Packit Service fa4841
	 * 

Packit Service fa4841
	 *
Packit Service fa4841
	 * @param topFocus look for a candidate is the one at the top of the bounds
Packit Service fa4841
	 *                 if topFocus is true, or at the bottom of the bounds if topFocus is
Packit Service fa4841
	 *                 false
Packit Service fa4841
	 * @param top      the top offset of the bounds in which a focusable must be
Packit Service fa4841
	 *                 found
Packit Service fa4841
	 * @param bottom   the bottom offset of the bounds in which a focusable must
Packit Service fa4841
	 *                 be found
Packit Service fa4841
	 * @return the next focusable component in the bounds or null if none can
Packit Service fa4841
	 * be found
Packit Service fa4841
	 */
Packit Service fa4841
	private View findFocusableViewInBounds(boolean topFocus, int top, int bottom, boolean leftFocus,
Packit Service fa4841
	                                       int left, int right)
Packit Service fa4841
	{
Packit Service fa4841
		List<View> focusables = getFocusables(View.FOCUS_FORWARD);
Packit Service fa4841
		View focusCandidate = null;
Packit Service fa4841
Packit Service fa4841
		/*
Packit Service fa4841
		 * A fully contained focusable is one where its top is below the bound's
Packit Service fa4841
		 * top, and its bottom is above the bound's bottom. A partially
Packit Service fa4841
		 * contained focusable is one where some part of it is within the
Packit Service fa4841
		 * bounds, but it also has some part that is not within bounds.  A fully contained
Packit Service fa4841
		 * focusable is preferred to a partially contained focusable.
Packit Service fa4841
		 */
Packit Service fa4841
		boolean foundFullyContainedFocusable = false;
Packit Service fa4841
Packit Service fa4841
		int count = focusables.size();
Packit Service fa4841
		for (int i = 0; i < count; i++)
Packit Service fa4841
		{
Packit Service fa4841
			View view = focusables.get(i);
Packit Service fa4841
			int viewTop = view.getTop();
Packit Service fa4841
			int viewBottom = view.getBottom();
Packit Service fa4841
			int viewLeft = view.getLeft();
Packit Service fa4841
			int viewRight = view.getRight();
Packit Service fa4841
Packit Service fa4841
			if (top < viewBottom && viewTop < bottom && left < viewRight && viewLeft < right)
Packit Service fa4841
			{
Packit Service fa4841
				/*
Packit Service fa4841
				 * the focusable is in the target area, it is a candidate for
Packit Service fa4841
				 * focusing
Packit Service fa4841
				 */
Packit Service fa4841
				final boolean viewIsFullyContained = (top < viewTop) && (viewBottom < bottom) &&
Packit Service fa4841
				                                     (left < viewLeft) && (viewRight < right);
Packit Service fa4841
				if (focusCandidate == null)
Packit Service fa4841
				{
Packit Service fa4841
					/* No candidate, take this one */
Packit Service fa4841
					focusCandidate = view;
Packit Service fa4841
					foundFullyContainedFocusable = viewIsFullyContained;
Packit Service fa4841
				}
Packit Service fa4841
				else
Packit Service fa4841
				{
Packit Service fa4841
					final boolean viewIsCloserToVerticalBoundary =
Packit Service fa4841
					    (topFocus && viewTop < focusCandidate.getTop()) ||
Packit Service fa4841
					    (!topFocus && viewBottom > focusCandidate.getBottom());
Packit Service fa4841
					final boolean viewIsCloserToHorizontalBoundary =
Packit Service fa4841
					    (leftFocus && viewLeft < focusCandidate.getLeft()) ||
Packit Service fa4841
					    (!leftFocus && viewRight > focusCandidate.getRight());
Packit Service fa4841
					if (foundFullyContainedFocusable)
Packit Service fa4841
					{
Packit Service fa4841
						if (viewIsFullyContained && viewIsCloserToVerticalBoundary &&
Packit Service fa4841
						    viewIsCloserToHorizontalBoundary)
Packit Service fa4841
						{
Packit Service fa4841
							/*
Packit Service fa4841
							 * We're dealing with only fully contained views, so
Packit Service fa4841
							 * it has to be closer to the boundary to beat our
Packit Service fa4841
							 * candidate
Packit Service fa4841
							 */
Packit Service fa4841
							focusCandidate = view;
Packit Service fa4841
						}
Packit Service fa4841
					}
Packit Service fa4841
					else
Packit Service fa4841
					{
Packit Service fa4841
						if (viewIsFullyContained)
Packit Service fa4841
						{
Packit Service fa4841
							/* Any fully contained view beats a partially contained view */
Packit Service fa4841
							focusCandidate = view;
Packit Service fa4841
							foundFullyContainedFocusable = true;
Packit Service fa4841
						}
Packit Service fa4841
						else if (viewIsCloserToVerticalBoundary && viewIsCloserToHorizontalBoundary)
Packit Service fa4841
						{
Packit Service fa4841
							/*
Packit Service fa4841
							 * Partially contained view beats another partially
Packit Service fa4841
							 * contained view if it's closer
Packit Service fa4841
							 */
Packit Service fa4841
							focusCandidate = view;
Packit Service fa4841
						}
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		return focusCandidate;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * 

Handles scrolling in response to a "home/end" shortcut press. This

Packit Service fa4841
	 * method will scroll the view to the top or bottom and give the focus
Packit Service fa4841
	 * to the topmost/bottommost component in the new visible area. If no
Packit Service fa4841
	 * component is a good candidate for focus, this scrollview reclaims the
Packit Service fa4841
	 * focus.

Packit Service fa4841
	 *
Packit Service fa4841
	 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
Packit Service fa4841
	 *                  to go the top of the view or
Packit Service fa4841
	 *                  {@link android.view.View#FOCUS_DOWN} to go the bottom
Packit Service fa4841
	 * @return true if the key event is consumed by this method, false otherwise
Packit Service fa4841
	 */
Packit Service fa4841
	public boolean fullScroll(int direction, boolean horizontal)
Packit Service fa4841
	{
Packit Service fa4841
		if (!horizontal)
Packit Service fa4841
		{
Packit Service fa4841
			boolean down = direction == View.FOCUS_DOWN;
Packit Service fa4841
			int height = getHeight();
Packit Service fa4841
			mTempRect.top = 0;
Packit Service fa4841
			mTempRect.bottom = height;
Packit Service fa4841
			if (down)
Packit Service fa4841
			{
Packit Service fa4841
				int count = getChildCount();
Packit Service fa4841
				if (count > 0)
Packit Service fa4841
				{
Packit Service fa4841
					View view = getChildAt(count - 1);
Packit Service fa4841
					mTempRect.bottom = view.getBottom();
Packit Service fa4841
					mTempRect.top = mTempRect.bottom - height;
Packit Service fa4841
				}
Packit Service fa4841
			}
Packit Service fa4841
			return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom, 0, 0, 0);
Packit Service fa4841
		}
Packit Service fa4841
		else
Packit Service fa4841
		{
Packit Service fa4841
			boolean right = direction == View.FOCUS_DOWN;
Packit Service fa4841
			int width = getWidth();
Packit Service fa4841
			mTempRect.left = 0;
Packit Service fa4841
			mTempRect.right = width;
Packit Service fa4841
			if (right)
Packit Service fa4841
			{
Packit Service fa4841
				int count = getChildCount();
Packit Service fa4841
				if (count > 0)
Packit Service fa4841
				{
Packit Service fa4841
					View view = getChildAt(count - 1);
Packit Service fa4841
					mTempRect.right = view.getBottom();
Packit Service fa4841
					mTempRect.left = mTempRect.right - width;
Packit Service fa4841
				}
Packit Service fa4841
			}
Packit Service fa4841
			return scrollAndFocus(0, 0, 0, direction, mTempRect.top, mTempRect.bottom);
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * 

Scrolls the view to make the area defined by top and

Packit Service fa4841
	 * bottom visible. This method attempts to give the focus
Packit Service fa4841
	 * to a component visible in this area. If no component can be focused in
Packit Service fa4841
	 * the new visible area, the focus is reclaimed by this scrollview.

Packit Service fa4841
	 *
Packit Service fa4841
	 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
Packit Service fa4841
	 *                  to go upward
Packit Service fa4841
	 *                  {@link android.view.View#FOCUS_DOWN} to downward
Packit Service fa4841
	 * @param top       the top offset of the new area to be made visible
Packit Service fa4841
	 * @param bottom    the bottom offset of the new area to be made visible
Packit Service fa4841
	 * @return true if the key event is consumed by this method, false otherwise
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean scrollAndFocus(int directionY, int top, int bottom, int directionX, int left,
Packit Service fa4841
	                               int right)
Packit Service fa4841
	{
Packit Service fa4841
		boolean handled = true;
Packit Service fa4841
		int height = getHeight();
Packit Service fa4841
		int containerTop = getScrollY();
Packit Service fa4841
		int containerBottom = containerTop + height;
Packit Service fa4841
		boolean up = directionY == View.FOCUS_UP;
Packit Service fa4841
		int width = getWidth();
Packit Service fa4841
		int containerLeft = getScrollX();
Packit Service fa4841
		int containerRight = containerLeft + width;
Packit Service fa4841
		boolean leftwards = directionX == View.FOCUS_UP;
Packit Service fa4841
		View newFocused = findFocusableViewInBounds(up, top, bottom, leftwards, left, right);
Packit Service fa4841
		if (newFocused == null)
Packit Service fa4841
		{
Packit Service fa4841
			newFocused = this;
Packit Service fa4841
		}
Packit Service fa4841
		if ((top >= containerTop && bottom <= containerBottom) ||
Packit Service fa4841
		    (left >= containerLeft && right <= containerRight))
Packit Service fa4841
		{
Packit Service fa4841
			handled = false;
Packit Service fa4841
		}
Packit Service fa4841
		else
Packit Service fa4841
		{
Packit Service fa4841
			int deltaY = up ? (top - containerTop) : (bottom - containerBottom);
Packit Service fa4841
			int deltaX = leftwards ? (left - containerLeft) : (right - containerRight);
Packit Service fa4841
			doScroll(deltaX, deltaY);
Packit Service fa4841
		}
Packit Service fa4841
		if (newFocused != findFocus() && newFocused.requestFocus(directionY))
Packit Service fa4841
		{
Packit Service fa4841
			mTwoDScrollViewMovedFocus = true;
Packit Service fa4841
			mTwoDScrollViewMovedFocus = false;
Packit Service fa4841
		}
Packit Service fa4841
		return handled;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Handle scrolling in response to an up or down arrow click.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param direction The direction corresponding to the arrow key that was
Packit Service fa4841
	 *                  pressed
Packit Service fa4841
	 * @return True if we consumed the event, false otherwise
Packit Service fa4841
	 */
Packit Service fa4841
	public boolean arrowScroll(int direction, boolean horizontal)
Packit Service fa4841
	{
Packit Service fa4841
		View currentFocused = findFocus();
Packit Service fa4841
		if (currentFocused == this)
Packit Service fa4841
			currentFocused = null;
Packit Service fa4841
		View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
Packit Service fa4841
		final int maxJump =
Packit Service fa4841
		    horizontal ? getMaxScrollAmountHorizontal() : getMaxScrollAmountVertical();
Packit Service fa4841
Packit Service fa4841
		if (!horizontal)
Packit Service fa4841
		{
Packit Service fa4841
			if (nextFocused != null)
Packit Service fa4841
			{
Packit Service fa4841
				nextFocused.getDrawingRect(mTempRect);
Packit Service fa4841
				offsetDescendantRectToMyCoords(nextFocused, mTempRect);
Packit Service fa4841
				int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
Packit Service fa4841
				doScroll(0, scrollDelta);
Packit Service fa4841
				nextFocused.requestFocus(direction);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				// no new focus
Packit Service fa4841
				int scrollDelta = maxJump;
Packit Service fa4841
				if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
Packit Service fa4841
				{
Packit Service fa4841
					scrollDelta = getScrollY();
Packit Service fa4841
				}
Packit Service fa4841
				else if (direction == View.FOCUS_DOWN)
Packit Service fa4841
				{
Packit Service fa4841
					if (getChildCount() > 0)
Packit Service fa4841
					{
Packit Service fa4841
						int daBottom = getChildAt(0).getBottom();
Packit Service fa4841
						int screenBottom = getScrollY() + getHeight();
Packit Service fa4841
						if (daBottom - screenBottom < maxJump)
Packit Service fa4841
						{
Packit Service fa4841
							scrollDelta = daBottom - screenBottom;
Packit Service fa4841
						}
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				if (scrollDelta == 0)
Packit Service fa4841
				{
Packit Service fa4841
					return false;
Packit Service fa4841
				}
Packit Service fa4841
				doScroll(0, direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		else
Packit Service fa4841
		{
Packit Service fa4841
			if (nextFocused != null)
Packit Service fa4841
			{
Packit Service fa4841
				nextFocused.getDrawingRect(mTempRect);
Packit Service fa4841
				offsetDescendantRectToMyCoords(nextFocused, mTempRect);
Packit Service fa4841
				int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
Packit Service fa4841
				doScroll(scrollDelta, 0);
Packit Service fa4841
				nextFocused.requestFocus(direction);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				// no new focus
Packit Service fa4841
				int scrollDelta = maxJump;
Packit Service fa4841
				if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
Packit Service fa4841
				{
Packit Service fa4841
					scrollDelta = getScrollY();
Packit Service fa4841
				}
Packit Service fa4841
				else if (direction == View.FOCUS_DOWN)
Packit Service fa4841
				{
Packit Service fa4841
					if (getChildCount() > 0)
Packit Service fa4841
					{
Packit Service fa4841
						int daBottom = getChildAt(0).getBottom();
Packit Service fa4841
						int screenBottom = getScrollY() + getHeight();
Packit Service fa4841
						if (daBottom - screenBottom < maxJump)
Packit Service fa4841
						{
Packit Service fa4841
							scrollDelta = daBottom - screenBottom;
Packit Service fa4841
						}
Packit Service fa4841
					}
Packit Service fa4841
				}
Packit Service fa4841
				if (scrollDelta == 0)
Packit Service fa4841
				{
Packit Service fa4841
					return false;
Packit Service fa4841
				}
Packit Service fa4841
				doScroll(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta, 0);
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		return true;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Smooth scroll by a Y delta
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param delta the number of pixels to scroll by on the Y axis
Packit Service fa4841
	 */
Packit Service fa4841
	private void doScroll(int deltaX, int deltaY)
Packit Service fa4841
	{
Packit Service fa4841
		if (deltaX != 0 || deltaY != 0)
Packit Service fa4841
		{
Packit Service fa4841
			smoothScrollBy(deltaX, deltaY);
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param dx the number of pixels to scroll by on the X axis
Packit Service fa4841
	 * @param dy the number of pixels to scroll by on the Y axis
Packit Service fa4841
	 */
Packit Service fa4841
	public final void smoothScrollBy(int dx, int dy)
Packit Service fa4841
	{
Packit Service fa4841
		long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
Packit Service fa4841
		if (duration > ANIMATED_SCROLL_GAP)
Packit Service fa4841
		{
Packit Service fa4841
			mScroller.startScroll(getScrollX(), getScrollY(), dx, dy);
Packit Service fa4841
			awakenScrollBars(mScroller.getDuration());
Packit Service fa4841
			invalidate();
Packit Service fa4841
		}
Packit Service fa4841
		else
Packit Service fa4841
		{
Packit Service fa4841
			if (!mScroller.isFinished())
Packit Service fa4841
			{
Packit Service fa4841
				mScroller.abortAnimation();
Packit Service fa4841
			}
Packit Service fa4841
			scrollBy(dx, dy);
Packit Service fa4841
		}
Packit Service fa4841
		mLastScroll = AnimationUtils.currentAnimationTimeMillis();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Like {@link #scrollTo}, but scroll smoothly instead of immediately.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param x the position where to scroll on the X axis
Packit Service fa4841
	 * @param y the position where to scroll on the Y axis
Packit Service fa4841
	 */
Packit Service fa4841
	public final void smoothScrollTo(int x, int y)
Packit Service fa4841
	{
Packit Service fa4841
		smoothScrollBy(x - getScrollX(), y - getScrollY());
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * 

The scroll range of a scroll view is the overall height of all of its

Packit Service fa4841
	 * children.

Packit Service fa4841
	 */
Packit Service fa4841
	@Override protected int computeVerticalScrollRange()
Packit Service fa4841
	{
Packit Service fa4841
		int count = getChildCount();
Packit Service fa4841
		return count == 0 ? getHeight() : (getChildAt(0)).getBottom();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected int computeHorizontalScrollRange()
Packit Service fa4841
	{
Packit Service fa4841
		int count = getChildCount();
Packit Service fa4841
		return count == 0 ? getWidth() : (getChildAt(0)).getRight();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override
Packit Service fa4841
	protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
Packit Service fa4841
	{
Packit Service fa4841
		ViewGroup.LayoutParams lp = child.getLayoutParams();
Packit Service fa4841
		int childWidthMeasureSpec;
Packit Service fa4841
		int childHeightMeasureSpec;
Packit Service fa4841
Packit Service fa4841
		childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Packit Service fa4841
		                                            getPaddingLeft() + getPaddingRight(), lp.width);
Packit Service fa4841
		childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Packit Service fa4841
Packit Service fa4841
		child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override
Packit Service fa4841
	protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
Packit Service fa4841
	                                       int parentHeightMeasureSpec, int heightUsed)
Packit Service fa4841
	{
Packit Service fa4841
		final MarginLayoutParams lp = (MarginLayoutParams)child.getLayoutParams();
Packit Service fa4841
		final int childWidthMeasureSpec =
Packit Service fa4841
		    MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
Packit Service fa4841
		final int childHeightMeasureSpec =
Packit Service fa4841
		    MeasureSpec.makeMeasureSpec(lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
Packit Service fa4841
Packit Service fa4841
		child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void computeScroll()
Packit Service fa4841
	{
Packit Service fa4841
		if (mScroller.computeScrollOffset())
Packit Service fa4841
		{
Packit Service fa4841
			// This is called at drawing time by ViewGroup.  We don't want to
Packit Service fa4841
			// re-show the scrollbars at this point, which scrollTo will do,
Packit Service fa4841
			// so we replicate most of scrollTo here.
Packit Service fa4841
			//
Packit Service fa4841
			//         It's a little odd to call onScrollChanged from inside the drawing.
Packit Service fa4841
			//
Packit Service fa4841
			//         It is, except when you remember that computeScroll() is used to
Packit Service fa4841
			//         animate scrolling. So unless we want to defer the onScrollChanged()
Packit Service fa4841
			//         until the end of the animated scrolling, we don't really have a
Packit Service fa4841
			//         choice here.
Packit Service fa4841
			//
Packit Service fa4841
			//         I agree.  The alternative, which I think would be worse, is to post
Packit Service fa4841
			//         something and tell the subclasses later.  This is bad because there
Packit Service fa4841
			//         will be a window where mScrollX/Y is different from what the app
Packit Service fa4841
			//         thinks it is.
Packit Service fa4841
			//
Packit Service fa4841
			int oldX = getScrollX();
Packit Service fa4841
			int oldY = getScrollY();
Packit Service fa4841
			int x = mScroller.getCurrX();
Packit Service fa4841
			int y = mScroller.getCurrY();
Packit Service fa4841
			if (getChildCount() > 0)
Packit Service fa4841
			{
Packit Service fa4841
				View child = getChildAt(0);
Packit Service fa4841
				scrollTo(
Packit Service fa4841
				    clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth()),
Packit Service fa4841
				    clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(),
Packit Service fa4841
				          child.getHeight()));
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				scrollTo(x, y);
Packit Service fa4841
			}
Packit Service fa4841
			if (oldX != getScrollX() || oldY != getScrollY())
Packit Service fa4841
			{
Packit Service fa4841
				onScrollChanged(getScrollX(), getScrollY(), oldX, oldY);
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			// Keep on drawing until the animation has finished.
Packit Service fa4841
			postInvalidate();
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Scrolls the view to the given child.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param child the View to scroll to
Packit Service fa4841
	 */
Packit Service fa4841
	private void scrollToChild(View child)
Packit Service fa4841
	{
Packit Service fa4841
		child.getDrawingRect(mTempRect);
Packit Service fa4841
		/* Offset from child's local coordinates to TwoDScrollView coordinates */
Packit Service fa4841
		offsetDescendantRectToMyCoords(child, mTempRect);
Packit Service fa4841
		int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
Packit Service fa4841
		if (scrollDelta != 0)
Packit Service fa4841
		{
Packit Service fa4841
			scrollBy(0, scrollDelta);
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * If rect is off screen, scroll just enough to get it (or at least the
Packit Service fa4841
	 * first screen size chunk of it) on screen.
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param rect      The rectangle.
Packit Service fa4841
	 * @param immediate True to scroll immediately without animation
Packit Service fa4841
	 * @return true if scrolling was performed
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean scrollToChildRect(Rect rect, boolean immediate)
Packit Service fa4841
	{
Packit Service fa4841
		final int delta = computeScrollDeltaToGetChildRectOnScreen(rect);
Packit Service fa4841
		final boolean scroll = delta != 0;
Packit Service fa4841
		if (scroll)
Packit Service fa4841
		{
Packit Service fa4841
			if (immediate)
Packit Service fa4841
			{
Packit Service fa4841
				scrollBy(0, delta);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				smoothScrollBy(0, delta);
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		return scroll;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Compute the amount to scroll in the Y direction in order to get
Packit Service fa4841
	 * a rectangle completely on the screen (or, if taller than the screen,
Packit Service fa4841
	 * at least the first screen size chunk of it).
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param rect The rect.
Packit Service fa4841
	 * @return The scroll delta.
Packit Service fa4841
	 */
Packit Service fa4841
	protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() == 0)
Packit Service fa4841
			return 0;
Packit Service fa4841
		int height = getHeight();
Packit Service fa4841
		int screenTop = getScrollY();
Packit Service fa4841
		int screenBottom = screenTop + height;
Packit Service fa4841
		int fadingEdge = getVerticalFadingEdgeLength();
Packit Service fa4841
		// leave room for top fading edge as long as rect isn't at very top
Packit Service fa4841
		if (rect.top > 0)
Packit Service fa4841
		{
Packit Service fa4841
			screenTop += fadingEdge;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		// leave room for bottom fading edge as long as rect isn't at very bottom
Packit Service fa4841
		if (rect.bottom < getChildAt(0).getHeight())
Packit Service fa4841
		{
Packit Service fa4841
			screenBottom -= fadingEdge;
Packit Service fa4841
		}
Packit Service fa4841
		int scrollYDelta = 0;
Packit Service fa4841
		if (rect.bottom > screenBottom && rect.top > screenTop)
Packit Service fa4841
		{
Packit Service fa4841
			// need to move down to get it in view: move down just enough so
Packit Service fa4841
			// that the entire rectangle is in view (or at least the first
Packit Service fa4841
			// screen size chunk).
Packit Service fa4841
			if (rect.height() > height)
Packit Service fa4841
			{
Packit Service fa4841
				// just enough to get screen size chunk on
Packit Service fa4841
				scrollYDelta += (rect.top - screenTop);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				// get entire rect at bottom of screen
Packit Service fa4841
				scrollYDelta += (rect.bottom - screenBottom);
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			// make sure we aren't scrolling beyond the end of our content
Packit Service fa4841
			int bottom = getChildAt(0).getBottom();
Packit Service fa4841
			int distanceToBottom = bottom - screenBottom;
Packit Service fa4841
			scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
Packit Service fa4841
		}
Packit Service fa4841
		else if (rect.top < screenTop && rect.bottom < screenBottom)
Packit Service fa4841
		{
Packit Service fa4841
			// need to move up to get it in view: move up just enough so that
Packit Service fa4841
			// entire rectangle is in view (or at least the first screen
Packit Service fa4841
			// size chunk of it).
Packit Service fa4841
Packit Service fa4841
			if (rect.height() > height)
Packit Service fa4841
			{
Packit Service fa4841
				// screen size chunk
Packit Service fa4841
				scrollYDelta -= (screenBottom - rect.bottom);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				// entire rect at top
Packit Service fa4841
				scrollYDelta -= (screenTop - rect.top);
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			// make sure we aren't scrolling any further than the top our content
Packit Service fa4841
			scrollYDelta = Math.max(scrollYDelta, -getScrollY());
Packit Service fa4841
		}
Packit Service fa4841
		return scrollYDelta;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void requestChildFocus(View child, View focused)
Packit Service fa4841
	{
Packit Service fa4841
		if (!mTwoDScrollViewMovedFocus)
Packit Service fa4841
		{
Packit Service fa4841
			if (!mIsLayoutDirty)
Packit Service fa4841
			{
Packit Service fa4841
				scrollToChild(focused);
Packit Service fa4841
			}
Packit Service fa4841
			else
Packit Service fa4841
			{
Packit Service fa4841
				// The child may not be laid out yet, we can't compute the scroll yet
Packit Service fa4841
				mChildToScrollTo = focused;
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
		super.requestChildFocus(child, focused);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * When looking for focus in children of a scroll view, need to be a little
Packit Service fa4841
	 * more careful not to give focus to something that is scrolled off screen.
Packit Service fa4841
	 * 

Packit Service fa4841
	 * This is more expensive than the default {@link android.view.ViewGroup}
Packit Service fa4841
	 * implementation, otherwise this behavior might have been made the default.
Packit Service fa4841
	 */
Packit Service fa4841
	@Override
Packit Service fa4841
	protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
Packit Service fa4841
	{
Packit Service fa4841
		// convert from forward / backward notation to up / down / left / right
Packit Service fa4841
		// (ugh).
Packit Service fa4841
		if (direction == View.FOCUS_FORWARD)
Packit Service fa4841
		{
Packit Service fa4841
			direction = View.FOCUS_DOWN;
Packit Service fa4841
		}
Packit Service fa4841
		else if (direction == View.FOCUS_BACKWARD)
Packit Service fa4841
		{
Packit Service fa4841
			direction = View.FOCUS_UP;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		final View nextFocus = previouslyFocusedRect == null
Packit Service fa4841
		                           ? FocusFinder.getInstance().findNextFocus(this, null, direction)
Packit Service fa4841
		                           : FocusFinder.getInstance().findNextFocusFromRect(
Packit Service fa4841
		                                 this, previouslyFocusedRect, direction);
Packit Service fa4841
Packit Service fa4841
		if (nextFocus == null)
Packit Service fa4841
		{
Packit Service fa4841
			return false;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		return nextFocus.requestFocus(direction, previouslyFocusedRect);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override
Packit Service fa4841
	public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)
Packit Service fa4841
	{
Packit Service fa4841
		// offset into coordinate space of this scroll view
Packit Service fa4841
		rectangle.offset(child.getLeft() - child.getScrollX(), child.getTop() - child.getScrollY());
Packit Service fa4841
		return scrollToChildRect(rectangle, immediate);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override public void requestLayout()
Packit Service fa4841
	{
Packit Service fa4841
		mIsLayoutDirty = true;
Packit Service fa4841
		super.requestLayout();
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected void onLayout(boolean changed, int l, int t, int r, int b)
Packit Service fa4841
	{
Packit Service fa4841
		super.onLayout(changed, l, t, r, b);
Packit Service fa4841
		mIsLayoutDirty = false;
Packit Service fa4841
		// Give a child focus if it needs it
Packit Service fa4841
		if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this))
Packit Service fa4841
		{
Packit Service fa4841
			scrollToChild(mChildToScrollTo);
Packit Service fa4841
		}
Packit Service fa4841
		mChildToScrollTo = null;
Packit Service fa4841
Packit Service fa4841
		// Calling this with the present values causes it to re-clam them
Packit Service fa4841
		scrollTo(getScrollX(), getScrollY());
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
Packit Service fa4841
	{
Packit Service fa4841
		super.onSizeChanged(w, h, oldw, oldh);
Packit Service fa4841
Packit Service fa4841
		View currentFocused = findFocus();
Packit Service fa4841
		if (null == currentFocused || this == currentFocused)
Packit Service fa4841
			return;
Packit Service fa4841
Packit Service fa4841
		// If the currently-focused view was visible on the screen when the
Packit Service fa4841
		// screen was at the old height, then scroll the screen to make that
Packit Service fa4841
		// view visible with the new screen height.
Packit Service fa4841
		currentFocused.getDrawingRect(mTempRect);
Packit Service fa4841
		offsetDescendantRectToMyCoords(currentFocused, mTempRect);
Packit Service fa4841
		int scrollDeltaX = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
Packit Service fa4841
		int scrollDeltaY = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
Packit Service fa4841
		doScroll(scrollDeltaX, scrollDeltaY);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Return true if child is an descendant of parent, (or equal to the parent).
Packit Service fa4841
	 */
Packit Service fa4841
	private boolean isViewDescendantOf(View child, View parent)
Packit Service fa4841
	{
Packit Service fa4841
		if (child == parent)
Packit Service fa4841
		{
Packit Service fa4841
			return true;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		final ViewParent theParent = child.getParent();
Packit Service fa4841
		return (theParent instanceof ViewGroup) && isViewDescendantOf((View)theParent, parent);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * Fling the scroll view
Packit Service fa4841
	 *
Packit Service fa4841
	 * @param velocityY The initial velocity in the Y direction. Positive
Packit Service fa4841
	 *                  numbers mean that the finger/curor is moving down the screen,
Packit Service fa4841
	 *                  which means we want to scroll towards the top.
Packit Service fa4841
	 */
Packit Service fa4841
	public void fling(int velocityX, int velocityY)
Packit Service fa4841
	{
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			int height = getHeight() - getPaddingBottom() - getPaddingTop();
Packit Service fa4841
			int bottom = getChildAt(0).getHeight();
Packit Service fa4841
			int width = getWidth() - getPaddingRight() - getPaddingLeft();
Packit Service fa4841
			int right = getChildAt(0).getWidth();
Packit Service fa4841
Packit Service fa4841
			mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY, 0, right - width, 0,
Packit Service fa4841
			                bottom - height);
Packit Service fa4841
Packit Service fa4841
			final boolean movingDown = velocityY > 0;
Packit Service fa4841
			final boolean movingRight = velocityX > 0;
Packit Service fa4841
Packit Service fa4841
			View newFocused = findFocusableViewInMyBounds(
Packit Service fa4841
			    movingRight, mScroller.getFinalX(), movingDown, mScroller.getFinalY(), findFocus());
Packit Service fa4841
			if (newFocused == null)
Packit Service fa4841
			{
Packit Service fa4841
				newFocused = this;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			if (newFocused != findFocus() &&
Packit Service fa4841
			    newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP))
Packit Service fa4841
			{
Packit Service fa4841
				mTwoDScrollViewMovedFocus = true;
Packit Service fa4841
				mTwoDScrollViewMovedFocus = false;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			awakenScrollBars(mScroller.getDuration());
Packit Service fa4841
			invalidate();
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	/**
Packit Service fa4841
	 * {@inheritDoc}
Packit Service fa4841
	 * 

Packit Service fa4841
	 * 

This version also clamps the scrolling to the bounds of our child.

Packit Service fa4841
	 */
Packit Service fa4841
	public void scrollTo(int x, int y)
Packit Service fa4841
	{
Packit Service fa4841
		// we rely on the fact the View.scrollBy calls scrollTo.
Packit Service fa4841
		if (getChildCount() > 0)
Packit Service fa4841
		{
Packit Service fa4841
			View child = getChildAt(0);
Packit Service fa4841
			x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
Packit Service fa4841
			y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
Packit Service fa4841
			if (x != getScrollX() || y != getScrollY())
Packit Service fa4841
			{
Packit Service fa4841
				super.scrollTo(x, y);
Packit Service fa4841
			}
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	private int clamp(int n, int my, int child)
Packit Service fa4841
	{
Packit Service fa4841
		if (my >= child || n < 0)
Packit Service fa4841
		{
Packit Service fa4841
			/* my >= child is this case:
Packit Service fa4841
			 *                    |--------------- me ---------------|
Packit Service fa4841
			 *     |------ child ------|
Packit Service fa4841
			 * or
Packit Service fa4841
			 *     |--------------- me ---------------|
Packit Service fa4841
			 *            |------ child ------|
Packit Service fa4841
			 * or
Packit Service fa4841
			 *     |--------------- me ---------------|
Packit Service fa4841
			 *                                  |------ child ------|
Packit Service fa4841
			 *
Packit Service fa4841
			 * n < 0 is this case:
Packit Service fa4841
			 *     |------ me ------|
Packit Service fa4841
			 *                    |-------- child --------|
Packit Service fa4841
			 *     |-- mScrollX --|
Packit Service fa4841
			 */
Packit Service fa4841
			return 0;
Packit Service fa4841
		}
Packit Service fa4841
		if ((my + n) > child)
Packit Service fa4841
		{
Packit Service fa4841
			/* this case:
Packit Service fa4841
			 *                    |------ me ------|
Packit Service fa4841
			 *     |------ child ------|
Packit Service fa4841
			 *     |-- mScrollX --|
Packit Service fa4841
			 */
Packit Service fa4841
			return child - my;
Packit Service fa4841
		}
Packit Service fa4841
		return n;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	public void setScrollViewListener(ScrollView2DListener scrollViewListener)
Packit Service fa4841
	{
Packit Service fa4841
		this.scrollView2DListener = scrollViewListener;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	@Override protected void onScrollChanged(int x, int y, int oldx, int oldy)
Packit Service fa4841
	{
Packit Service fa4841
		super.onScrollChanged(x, y, oldx, oldy);
Packit Service fa4841
		if (scrollView2DListener != null)
Packit Service fa4841
		{
Packit Service fa4841
			scrollView2DListener.onScrollChanged(this, x, y, oldx, oldy);
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	// interface to receive notifications when the view is scrolled
Packit Service fa4841
	public interface ScrollView2DListener {
Packit Service fa4841
		abstract void onScrollChanged(ScrollView2D scrollView, int x, int y, int oldx, int oldy);
Packit Service fa4841
	}
Packit Service fa4841
}