FreeRDP
Loading...
Searching...
No Matches
ScrollView2D.java
1/*
2 * Copyright (C) 2006 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/*
17 * Revised 5/19/2010 by GORGES
18 * Now supports two-dimensional view scrolling
19 * http://GORGES.us
20 */
21
22package com.freerdp.freerdpcore.presentation;
23
24import android.content.Context;
25import android.graphics.Rect;
26import android.os.Build;
27import android.util.AttributeSet;
28import android.view.FocusFinder;
29import android.view.MotionEvent;
30import android.view.VelocityTracker;
31import android.view.View;
32import android.view.ViewConfiguration;
33import android.view.ViewGroup;
34import android.view.ViewParent;
35import android.view.animation.AnimationUtils;
36import android.widget.FrameLayout;
37import android.widget.LinearLayout;
38import android.widget.Scroller;
39
40import com.freerdp.freerdpcore.R;
41import android.widget.TextView;
42
43import java.util.List;
44
59public class ScrollView2D extends FrameLayout
60{
61
62 static final int ANIMATED_SCROLL_GAP = 250;
63 static final float MAX_SCROLL_FACTOR = 0.5f;
64 private final Rect mTempRect = new Rect();
65 private ScrollView2DListener scrollView2DListener = null;
66 private long mLastScroll;
67 private Scroller mScroller;
68 private boolean scrollEnabled = true;
74 private boolean mTwoDScrollViewMovedFocus;
78 private float mLastMotionY;
79 private float mLastMotionX;
84 private boolean mIsLayoutDirty = true;
90 private View mChildToScrollTo = null;
96 private boolean mIsBeingDragged = false;
100 private VelocityTracker mVelocityTracker;
104 private int mTouchSlop;
105 private int mMinimumVelocity;
106 private int mMaximumVelocity;
107 public ScrollView2D(Context context)
108 {
109 super(context);
110 initTwoDScrollView();
111 }
112
113 public ScrollView2D(Context context, AttributeSet attrs)
114 {
115 super(context, attrs);
116 initTwoDScrollView();
117 }
118
119 public ScrollView2D(Context context, AttributeSet attrs, int defStyle)
120 {
121 super(context, attrs, defStyle);
122 initTwoDScrollView();
123 }
124
125 @Override protected float getTopFadingEdgeStrength()
126 {
127 if (getChildCount() == 0)
128 {
129 return 0.0f;
130 }
131 final int length = getVerticalFadingEdgeLength();
132 if (getScrollY() < length)
133 {
134 return getScrollY() / (float)length;
135 }
136 return 1.0f;
137 }
138
139 @Override protected float getBottomFadingEdgeStrength()
140 {
141 if (getChildCount() == 0)
142 {
143 return 0.0f;
144 }
145 final int length = getVerticalFadingEdgeLength();
146 final int bottomEdge = getHeight() - getPaddingBottom();
147 final int span = getChildAt(0).getBottom() - getScrollY() - bottomEdge;
148 if (span < length)
149 {
150 return span / (float)length;
151 }
152 return 1.0f;
153 }
154
155 @Override protected float getLeftFadingEdgeStrength()
156 {
157 if (getChildCount() == 0)
158 {
159 return 0.0f;
160 }
161 final int length = getHorizontalFadingEdgeLength();
162 if (getScrollX() < length)
163 {
164 return getScrollX() / (float)length;
165 }
166 return 1.0f;
167 }
168
169 @Override protected float getRightFadingEdgeStrength()
170 {
171 if (getChildCount() == 0)
172 {
173 return 0.0f;
174 }
175 final int length = getHorizontalFadingEdgeLength();
176 final int rightEdge = getWidth() - getPaddingRight();
177 final int span = getChildAt(0).getRight() - getScrollX() - rightEdge;
178 if (span < length)
179 {
180 return span / (float)length;
181 }
182 return 1.0f;
183 }
184
188 public void setScrollEnabled(boolean enable)
189 {
190 scrollEnabled = enable;
191 }
192
198 {
199 return (int)(MAX_SCROLL_FACTOR * getHeight());
200 }
201
202 public int getMaxScrollAmountHorizontal()
203 {
204 return (int)(MAX_SCROLL_FACTOR * getWidth());
205 }
206
207 private void initTwoDScrollView()
208 {
209 mScroller = new Scroller(getContext());
210 setFocusable(true);
211 setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
212 setWillNotDraw(false);
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
217 }
218
219 @Override public void addView(View child)
220 {
221 if (getChildCount() > 0)
222 {
223 throw new IllegalStateException("TwoDScrollView can host only one direct child");
224 }
225 super.addView(child);
226 }
227
228 @Override public void addView(View child, int index)
229 {
230 if (getChildCount() > 0)
231 {
232 throw new IllegalStateException("TwoDScrollView can host only one direct child");
233 }
234 super.addView(child, index);
235 }
236
237 @Override public void addView(View child, ViewGroup.LayoutParams params)
238 {
239 if (getChildCount() > 0)
240 {
241 throw new IllegalStateException("TwoDScrollView can host only one direct child");
242 }
243 super.addView(child, params);
244 }
245
246 @Override public void addView(View child, int index, ViewGroup.LayoutParams params)
247 {
248 if (getChildCount() > 0)
249 {
250 throw new IllegalStateException("TwoDScrollView can host only one direct child");
251 }
252 super.addView(child, index, params);
253 }
254
258 private boolean canScroll()
259 {
260 if (!scrollEnabled)
261 return false;
262 View child = getChildAt(0);
263 if (child != null)
264 {
265 int childHeight = child.getHeight();
266 int childWidth = child.getWidth();
267 return (getHeight() < childHeight + getPaddingTop() + getPaddingBottom()) ||
268 (getWidth() < childWidth + getPaddingLeft() + getPaddingRight());
269 }
270 return false;
271 }
272
273 @Override public boolean onInterceptTouchEvent(MotionEvent ev)
274 {
275 /*
276 * This method JUST determines whether we want to intercept the motion.
277 * If we return true, onMotionEvent will be called and we do the actual
278 * scrolling there.
279 *
280 * Shortcut the most recurring case: the user is in the dragging
281 * state and he is moving his finger. We want to intercept this
282 * motion.
283 */
284 // Let child handle touchpad scroll gestures.
285 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
286 ev.getClassification() == MotionEvent.CLASSIFICATION_TWO_FINGER_SWIPE)
287 {
288 mIsBeingDragged = false;
289 return false;
290 }
291
292 final int action = ev.getAction();
293 if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged))
294 {
295 return true;
296 }
297 if (!canScroll())
298 {
299 mIsBeingDragged = false;
300 return false;
301 }
302 final float y = ev.getY();
303 final float x = ev.getX();
304 switch (action)
305 {
306 case MotionEvent.ACTION_MOVE:
307 /*
308 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
309 * whether the user has moved far enough from his original down touch.
310 */
311 /*
312 * Locally do absolute value. mLastMotionY is set to the y value
313 * of the down event.
314 */
315 final int yDiff = (int)Math.abs(y - mLastMotionY);
316 final int xDiff = (int)Math.abs(x - mLastMotionX);
317 if (yDiff > mTouchSlop || xDiff > mTouchSlop)
318 {
319 mIsBeingDragged = true;
320 }
321 break;
322
323 case MotionEvent.ACTION_DOWN:
324 /* Remember location of down touch */
325 mLastMotionY = y;
326 mLastMotionX = x;
327
328 /*
329 * If being flinged and user touches the screen, initiate drag;
330 * otherwise don't. mScroller.isFinished should be false when
331 * being flinged.
332 */
333 mIsBeingDragged = !mScroller.isFinished();
334 break;
335
336 case MotionEvent.ACTION_CANCEL:
337 case MotionEvent.ACTION_UP:
338 /* Release the drag */
339 mIsBeingDragged = false;
340 break;
341 }
342
343 /*
344 * The only time we want to intercept motion events is if we are in the
345 * drag mode.
346 */
347 return mIsBeingDragged;
348 }
349
350 @Override public boolean onTouchEvent(MotionEvent ev)
351 {
352
353 if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0)
354 {
355 // Don't handle edge touches immediately -- they may actually belong to one of our
356 // descendants.
357 return false;
358 }
359
360 if (!canScroll())
361 {
362 return false;
363 }
364
365 if (mVelocityTracker == null)
366 {
367 mVelocityTracker = VelocityTracker.obtain();
368 }
369 mVelocityTracker.addMovement(ev);
370
371 final int action = ev.getAction();
372 final float y = ev.getY();
373 final float x = ev.getX();
374
375 switch (action)
376 {
377 case MotionEvent.ACTION_DOWN:
378 /*
379 * If being flinged and user touches, stop the fling. isFinished
380 * will be false if being flinged.
381 */
382 if (!mScroller.isFinished())
383 {
384 mScroller.abortAnimation();
385 }
386
387 // Remember where the motion event started
388 mLastMotionY = y;
389 mLastMotionX = x;
390 break;
391 case MotionEvent.ACTION_MOVE:
392 // Scroll to follow the motion event
393 int deltaX = (int)(mLastMotionX - x);
394 int deltaY = (int)(mLastMotionY - y);
395 mLastMotionX = x;
396 mLastMotionY = y;
397
398 if (deltaX < 0)
399 {
400 if (getScrollX() < 0)
401 {
402 deltaX = 0;
403 }
404 }
405 else if (deltaX > 0)
406 {
407 final int rightEdge = getWidth() - getPaddingRight();
408 final int availableToScroll =
409 getChildAt(0).getRight() - getScrollX() - rightEdge;
410 if (availableToScroll > 0)
411 {
412 deltaX = Math.min(availableToScroll, deltaX);
413 }
414 else
415 {
416 deltaX = 0;
417 }
418 }
419 if (deltaY < 0)
420 {
421 if (getScrollY() < 0)
422 {
423 deltaY = 0;
424 }
425 }
426 else if (deltaY > 0)
427 {
428 final int bottomEdge = getHeight() - getPaddingBottom();
429 final int availableToScroll =
430 getChildAt(0).getBottom() - getScrollY() - bottomEdge;
431 if (availableToScroll > 0)
432 {
433 deltaY = Math.min(availableToScroll, deltaY);
434 }
435 else
436 {
437 deltaY = 0;
438 }
439 }
440 if (deltaY != 0 || deltaX != 0)
441 scrollBy(deltaX, deltaY);
442 break;
443 case MotionEvent.ACTION_UP:
444 final VelocityTracker velocityTracker = mVelocityTracker;
445 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
446 int initialXVelocity = (int)velocityTracker.getXVelocity();
447 int initialYVelocity = (int)velocityTracker.getYVelocity();
448 if ((Math.abs(initialXVelocity) + Math.abs(initialYVelocity) > mMinimumVelocity) &&
449 getChildCount() > 0)
450 {
451 fling(-initialXVelocity, -initialYVelocity);
452 }
453 if (mVelocityTracker != null)
454 {
455 mVelocityTracker.recycle();
456 mVelocityTracker = null;
457 }
458 }
459 return true;
460 }
461
477 private View findFocusableViewInMyBounds(final boolean topFocus, final int top,
478 final boolean leftFocus, final int left,
479 View preferredFocusable)
480 {
481 /*
482 * The fading edge's transparent side should be considered for focus
483 * since it's mostly visible, so we divide the actual fading edge length
484 * by 2.
485 */
486 final int verticalFadingEdgeLength = getVerticalFadingEdgeLength() / 2;
487 final int topWithoutFadingEdge = top + verticalFadingEdgeLength;
488 final int bottomWithoutFadingEdge = top + getHeight() - verticalFadingEdgeLength;
489 final int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength() / 2;
490 final int leftWithoutFadingEdge = left + horizontalFadingEdgeLength;
491 final int rightWithoutFadingEdge = left + getWidth() - horizontalFadingEdgeLength;
492
493 if ((preferredFocusable != null) &&
494 (preferredFocusable.getTop() < bottomWithoutFadingEdge) &&
495 (preferredFocusable.getBottom() > topWithoutFadingEdge) &&
496 (preferredFocusable.getLeft() < rightWithoutFadingEdge) &&
497 (preferredFocusable.getRight() > leftWithoutFadingEdge))
498 {
499 return preferredFocusable;
500 }
501 return findFocusableViewInBounds(topFocus, topWithoutFadingEdge, bottomWithoutFadingEdge,
502 leftFocus, leftWithoutFadingEdge, rightWithoutFadingEdge);
503 }
504
519 private View findFocusableViewInBounds(boolean topFocus, int top, int bottom, boolean leftFocus,
520 int left, int right)
521 {
522 List<View> focusables = getFocusables(View.FOCUS_FORWARD);
523 View focusCandidate = null;
524
525 /*
526 * A fully contained focusable is one where its top is below the bound's
527 * top, and its bottom is above the bound's bottom. A partially
528 * contained focusable is one where some part of it is within the
529 * bounds, but it also has some part that is not within bounds. A fully contained
530 * focusable is preferred to a partially contained focusable.
531 */
532 boolean foundFullyContainedFocusable = false;
533
534 int count = focusables.size();
535 for (int i = 0; i < count; i++)
536 {
537 View view = focusables.get(i);
538 int viewTop = view.getTop();
539 int viewBottom = view.getBottom();
540 int viewLeft = view.getLeft();
541 int viewRight = view.getRight();
542
543 if (top < viewBottom && viewTop < bottom && left < viewRight && viewLeft < right)
544 {
545 /*
546 * the focusable is in the target area, it is a candidate for
547 * focusing
548 */
549 final boolean viewIsFullyContained = (top < viewTop) && (viewBottom < bottom) &&
550 (left < viewLeft) && (viewRight < right);
551 if (focusCandidate == null)
552 {
553 /* No candidate, take this one */
554 focusCandidate = view;
555 foundFullyContainedFocusable = viewIsFullyContained;
556 }
557 else
558 {
559 final boolean viewIsCloserToVerticalBoundary =
560 (topFocus && viewTop < focusCandidate.getTop()) ||
561 (!topFocus && viewBottom > focusCandidate.getBottom());
562 final boolean viewIsCloserToHorizontalBoundary =
563 (leftFocus && viewLeft < focusCandidate.getLeft()) ||
564 (!leftFocus && viewRight > focusCandidate.getRight());
565 if (foundFullyContainedFocusable)
566 {
567 if (viewIsFullyContained && viewIsCloserToVerticalBoundary &&
568 viewIsCloserToHorizontalBoundary)
569 {
570 /*
571 * We're dealing with only fully contained views, so
572 * it has to be closer to the boundary to beat our
573 * candidate
574 */
575 focusCandidate = view;
576 }
577 }
578 else
579 {
580 if (viewIsFullyContained)
581 {
582 /* Any fully contained view beats a partially contained view */
583 focusCandidate = view;
584 foundFullyContainedFocusable = true;
585 }
586 else if (viewIsCloserToVerticalBoundary && viewIsCloserToHorizontalBoundary)
587 {
588 /*
589 * Partially contained view beats another partially
590 * contained view if it's closer
591 */
592 focusCandidate = view;
593 }
594 }
595 }
596 }
597 }
598 return focusCandidate;
599 }
600
613 public boolean fullScroll(int direction, boolean horizontal)
614 {
615 if (!horizontal)
616 {
617 boolean down = direction == View.FOCUS_DOWN;
618 int height = getHeight();
619 mTempRect.top = 0;
620 mTempRect.bottom = height;
621 if (down)
622 {
623 int count = getChildCount();
624 if (count > 0)
625 {
626 View view = getChildAt(count - 1);
627 mTempRect.bottom = view.getBottom();
628 mTempRect.top = mTempRect.bottom - height;
629 }
630 }
631 return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom, 0, 0, 0);
632 }
633 else
634 {
635 boolean right = direction == View.FOCUS_DOWN;
636 int width = getWidth();
637 mTempRect.left = 0;
638 mTempRect.right = width;
639 if (right)
640 {
641 int count = getChildCount();
642 if (count > 0)
643 {
644 View view = getChildAt(count - 1);
645 mTempRect.right = view.getBottom();
646 mTempRect.left = mTempRect.right - width;
647 }
648 }
649 return scrollAndFocus(0, 0, 0, direction, mTempRect.top, mTempRect.bottom);
650 }
651 }
652
666 private boolean scrollAndFocus(int directionY, int top, int bottom, int directionX, int left,
667 int right)
668 {
669 boolean handled = true;
670 int height = getHeight();
671 int containerTop = getScrollY();
672 int containerBottom = containerTop + height;
673 boolean up = directionY == View.FOCUS_UP;
674 int width = getWidth();
675 int containerLeft = getScrollX();
676 int containerRight = containerLeft + width;
677 boolean leftwards = directionX == View.FOCUS_UP;
678 View newFocused = findFocusableViewInBounds(up, top, bottom, leftwards, left, right);
679 if (newFocused == null)
680 {
681 newFocused = this;
682 }
683 if ((top >= containerTop && bottom <= containerBottom) ||
684 (left >= containerLeft && right <= containerRight))
685 {
686 handled = false;
687 }
688 else
689 {
690 int deltaY = up ? (top - containerTop) : (bottom - containerBottom);
691 int deltaX = leftwards ? (left - containerLeft) : (right - containerRight);
692 doScroll(deltaX, deltaY);
693 }
694 if (newFocused != findFocus() && newFocused.requestFocus(directionY))
695 {
696 mTwoDScrollViewMovedFocus = true;
697 mTwoDScrollViewMovedFocus = false;
698 }
699 return handled;
700 }
701
709 public boolean arrowScroll(int direction, boolean horizontal)
710 {
711 View currentFocused = findFocus();
712 if (currentFocused == this)
713 currentFocused = null;
714 View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
715 final int maxJump =
716 horizontal ? getMaxScrollAmountHorizontal() : getMaxScrollAmountVertical();
717
718 if (!horizontal)
719 {
720 if (nextFocused != null)
721 {
722 nextFocused.getDrawingRect(mTempRect);
723 offsetDescendantRectToMyCoords(nextFocused, mTempRect);
724 int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
725 doScroll(0, scrollDelta);
726 nextFocused.requestFocus(direction);
727 }
728 else
729 {
730 // no new focus
731 int scrollDelta = maxJump;
732 if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
733 {
734 scrollDelta = getScrollY();
735 }
736 else if (direction == View.FOCUS_DOWN)
737 {
738 if (getChildCount() > 0)
739 {
740 int daBottom = getChildAt(0).getBottom();
741 int screenBottom = getScrollY() + getHeight();
742 if (daBottom - screenBottom < maxJump)
743 {
744 scrollDelta = daBottom - screenBottom;
745 }
746 }
747 }
748 if (scrollDelta == 0)
749 {
750 return false;
751 }
752 doScroll(0, direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
753 }
754 }
755 else
756 {
757 if (nextFocused != null)
758 {
759 nextFocused.getDrawingRect(mTempRect);
760 offsetDescendantRectToMyCoords(nextFocused, mTempRect);
761 int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
762 doScroll(scrollDelta, 0);
763 nextFocused.requestFocus(direction);
764 }
765 else
766 {
767 // no new focus
768 int scrollDelta = maxJump;
769 if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
770 {
771 scrollDelta = getScrollY();
772 }
773 else if (direction == View.FOCUS_DOWN)
774 {
775 if (getChildCount() > 0)
776 {
777 int daBottom = getChildAt(0).getBottom();
778 int screenBottom = getScrollY() + getHeight();
779 if (daBottom - screenBottom < maxJump)
780 {
781 scrollDelta = daBottom - screenBottom;
782 }
783 }
784 }
785 if (scrollDelta == 0)
786 {
787 return false;
788 }
789 doScroll(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta, 0);
790 }
791 }
792 return true;
793 }
794
800 private void doScroll(int deltaX, int deltaY)
801 {
802 if (deltaX != 0 || deltaY != 0)
803 {
804 smoothScrollBy(deltaX, deltaY);
805 }
806 }
807
814 public final void smoothScrollBy(int dx, int dy)
815 {
816 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
817 if (duration > ANIMATED_SCROLL_GAP)
818 {
819 mScroller.startScroll(getScrollX(), getScrollY(), dx, dy);
820 awakenScrollBars(mScroller.getDuration());
821 invalidate();
822 }
823 else
824 {
825 if (!mScroller.isFinished())
826 {
827 mScroller.abortAnimation();
828 }
829 scrollBy(dx, dy);
830 }
831 mLastScroll = AnimationUtils.currentAnimationTimeMillis();
832 }
833
840 public final void smoothScrollTo(int x, int y)
841 {
842 smoothScrollBy(x - getScrollX(), y - getScrollY());
843 }
844
849 @Override protected int computeVerticalScrollRange()
850 {
851 int count = getChildCount();
852 return count == 0 ? getHeight() : (getChildAt(0)).getBottom();
853 }
854
855 @Override protected int computeHorizontalScrollRange()
856 {
857 int count = getChildCount();
858 return count == 0 ? getWidth() : (getChildAt(0)).getRight();
859 }
860
861 @Override
862 protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
863 {
864 ViewGroup.LayoutParams lp = child.getLayoutParams();
865 int childWidthMeasureSpec;
866 int childHeightMeasureSpec;
867
868 childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
869 getPaddingLeft() + getPaddingRight(), lp.width);
870 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
871
872 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
873 }
874
875 @Override
876 protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
877 int parentHeightMeasureSpec, int heightUsed)
878 {
879 final MarginLayoutParams lp = (MarginLayoutParams)child.getLayoutParams();
880 final int childWidthMeasureSpec =
881 MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
882 final int childHeightMeasureSpec =
883 MeasureSpec.makeMeasureSpec(lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
884
885 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
886 }
887
888 @Override public void computeScroll()
889 {
890 if (mScroller.computeScrollOffset())
891 {
892 // This is called at drawing time by ViewGroup. We don't want to
893 // re-show the scrollbars at this point, which scrollTo will do,
894 // so we replicate most of scrollTo here.
895 //
896 // It's a little odd to call onScrollChanged from inside the drawing.
897 //
898 // It is, except when you remember that computeScroll() is used to
899 // animate scrolling. So unless we want to defer the onScrollChanged()
900 // until the end of the animated scrolling, we don't really have a
901 // choice here.
902 //
903 // I agree. The alternative, which I think would be worse, is to post
904 // something and tell the subclasses later. This is bad because there
905 // will be a window where mScrollX/Y is different from what the app
906 // thinks it is.
907 //
908 int oldX = getScrollX();
909 int oldY = getScrollY();
910 int x = mScroller.getCurrX();
911 int y = mScroller.getCurrY();
912 if (getChildCount() > 0)
913 {
914 View child = getChildAt(0);
915 scrollTo(
916 clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth()),
917 clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(),
918 child.getHeight()));
919 }
920 else
921 {
922 scrollTo(x, y);
923 }
924 if (oldX != getScrollX() || oldY != getScrollY())
925 {
926 onScrollChanged(getScrollX(), getScrollY(), oldX, oldY);
927 }
928
929 // Keep on drawing until the animation has finished.
930 postInvalidate();
931 }
932 }
933
939 private void scrollToChild(View child)
940 {
941 child.getDrawingRect(mTempRect);
942 /* Offset from child's local coordinates to TwoDScrollView coordinates */
943 offsetDescendantRectToMyCoords(child, mTempRect);
944 int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
945 if (scrollDelta != 0)
946 {
947 scrollBy(0, scrollDelta);
948 }
949 }
950
959 private boolean scrollToChildRect(Rect rect, boolean immediate)
960 {
961 final int delta = computeScrollDeltaToGetChildRectOnScreen(rect);
962 final boolean scroll = delta != 0;
963 if (scroll)
964 {
965 if (immediate)
966 {
967 scrollBy(0, delta);
968 }
969 else
970 {
971 smoothScrollBy(0, delta);
972 }
973 }
974 return scroll;
975 }
976
986 {
987 if (getChildCount() == 0)
988 return 0;
989 int height = getHeight();
990 int screenTop = getScrollY();
991 int screenBottom = screenTop + height;
992 int fadingEdge = getVerticalFadingEdgeLength();
993 // leave room for top fading edge as long as rect isn't at very top
994 if (rect.top > 0)
995 {
996 screenTop += fadingEdge;
997 }
998
999 // leave room for bottom fading edge as long as rect isn't at very bottom
1000 if (rect.bottom < getChildAt(0).getHeight())
1001 {
1002 screenBottom -= fadingEdge;
1003 }
1004 int scrollYDelta = 0;
1005 if (rect.bottom > screenBottom && rect.top > screenTop)
1006 {
1007 // need to move down to get it in view: move down just enough so
1008 // that the entire rectangle is in view (or at least the first
1009 // screen size chunk).
1010 if (rect.height() > height)
1011 {
1012 // just enough to get screen size chunk on
1013 scrollYDelta += (rect.top - screenTop);
1014 }
1015 else
1016 {
1017 // get entire rect at bottom of screen
1018 scrollYDelta += (rect.bottom - screenBottom);
1019 }
1020
1021 // make sure we aren't scrolling beyond the end of our content
1022 int bottom = getChildAt(0).getBottom();
1023 int distanceToBottom = bottom - screenBottom;
1024 scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
1025 }
1026 else if (rect.top < screenTop && rect.bottom < screenBottom)
1027 {
1028 // need to move up to get it in view: move up just enough so that
1029 // entire rectangle is in view (or at least the first screen
1030 // size chunk of it).
1031
1032 if (rect.height() > height)
1033 {
1034 // screen size chunk
1035 scrollYDelta -= (screenBottom - rect.bottom);
1036 }
1037 else
1038 {
1039 // entire rect at top
1040 scrollYDelta -= (screenTop - rect.top);
1041 }
1042
1043 // make sure we aren't scrolling any further than the top our content
1044 scrollYDelta = Math.max(scrollYDelta, -getScrollY());
1045 }
1046 return scrollYDelta;
1047 }
1048
1049 @Override public void requestChildFocus(View child, View focused)
1050 {
1051 if (!mTwoDScrollViewMovedFocus)
1052 {
1053 if (!mIsLayoutDirty)
1054 {
1055 scrollToChild(focused);
1056 }
1057 else
1058 {
1059 // The child may not be laid out yet, we can't compute the scroll yet
1060 mChildToScrollTo = focused;
1061 }
1062 }
1063 super.requestChildFocus(child, focused);
1064 }
1065
1073 @Override
1074 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
1075 {
1076 // convert from forward / backward notation to up / down / left / right
1077 // (ugh).
1078 if (direction == View.FOCUS_FORWARD)
1079 {
1080 direction = View.FOCUS_DOWN;
1081 }
1082 else if (direction == View.FOCUS_BACKWARD)
1083 {
1084 direction = View.FOCUS_UP;
1085 }
1086
1087 final View nextFocus = previouslyFocusedRect == null
1088 ? FocusFinder.getInstance().findNextFocus(this, null, direction)
1089 : FocusFinder.getInstance().findNextFocusFromRect(
1090 this, previouslyFocusedRect, direction);
1091
1092 if (nextFocus == null)
1093 {
1094 return false;
1095 }
1096
1097 return nextFocus.requestFocus(direction, previouslyFocusedRect);
1098 }
1099
1100 @Override
1101 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)
1102 {
1103 // offset into coordinate space of this scroll view
1104 rectangle.offset(child.getLeft() - child.getScrollX(), child.getTop() - child.getScrollY());
1105 return scrollToChildRect(rectangle, immediate);
1106 }
1107
1108 @Override public void requestLayout()
1109 {
1110 mIsLayoutDirty = true;
1111 super.requestLayout();
1112 }
1113
1114 private int lastCenterContentH = -1;
1115 private int lastCenterTop = 0;
1116
1117 @Override protected void onLayout(boolean changed, int l, int t, int r, int b)
1118 {
1119 super.onLayout(changed, l, t, r, b);
1120 mIsLayoutDirty = false;
1121 // Give a child focus if it needs it
1122 if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this))
1123 {
1124 scrollToChild(mChildToScrollTo);
1125 }
1126 mChildToScrollTo = null;
1127
1128 // Center the content area (excluding touch-pointer padding) within the viewport.
1129 // This keeps the RDP surface visually centered regardless of pointer padding changes.
1130 if (getChildCount() > 0)
1131 {
1132 View child = getChildAt(0);
1133 SessionView sv = findViewById(R.id.sessionView);
1134 int ptw = sv != null ? sv.getTouchPointerPaddingWidth() : 0;
1135 int pth = sv != null ? sv.getTouchPointerPaddingHeight() : 0;
1136 int contentW = child.getMeasuredWidth() - ptw;
1137 int contentH = child.getMeasuredHeight() - pth;
1138 int usableW = getWidth() - getPaddingLeft() - getPaddingRight();
1139 int usableH = getHeight() - getPaddingTop() - getPaddingBottom();
1140 int left = getPaddingLeft() + Math.max(0, (usableW - contentW) / 2);
1141 int top;
1142 if (contentH == lastCenterContentH)
1143 {
1144 // viewport-only change (e.g. keyboard): keep position, don't re-center
1145 top = lastCenterTop;
1146 }
1147 else
1148 {
1149 top = getPaddingTop() + Math.max(0, (usableH - contentH) / 2);
1150 lastCenterContentH = contentH;
1151 lastCenterTop = top;
1152 }
1153 child.layout(left, top, left + child.getMeasuredWidth(),
1154 top + child.getMeasuredHeight());
1155 }
1156
1157 // Calling this with the present values causes it to re-clamp them
1158 scrollTo(getScrollX(), getScrollY());
1159 }
1160
1161 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
1162 {
1163 super.onSizeChanged(w, h, oldw, oldh);
1164
1165 View currentFocused = findFocus();
1166 if (null == currentFocused || this == currentFocused)
1167 return;
1168
1169 // If the currently-focused view was visible on the screen when the
1170 // screen was at the old height, then scroll the screen to make that
1171 // view visible with the new screen height.
1172 currentFocused.getDrawingRect(mTempRect);
1173 offsetDescendantRectToMyCoords(currentFocused, mTempRect);
1174 int scrollDeltaX = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
1175 int scrollDeltaY = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
1176 doScroll(scrollDeltaX, scrollDeltaY);
1177 }
1178
1182 private boolean isViewDescendantOf(View child, View parent)
1183 {
1184 if (child == parent)
1185 {
1186 return true;
1187 }
1188
1189 final ViewParent theParent = child.getParent();
1190 return (theParent instanceof ViewGroup) && isViewDescendantOf((View)theParent, parent);
1191 }
1192
1200 public void fling(int velocityX, int velocityY)
1201 {
1202 if (getChildCount() > 0)
1203 {
1204 int height = getHeight() - getPaddingBottom() - getPaddingTop();
1205 int bottom = getChildAt(0).getHeight();
1206 int width = getWidth() - getPaddingRight() - getPaddingLeft();
1207 int right = getChildAt(0).getWidth();
1208
1209 mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY, 0, right - width, 0,
1210 bottom - height);
1211
1212 final boolean movingDown = velocityY > 0;
1213 final boolean movingRight = velocityX > 0;
1214
1215 View newFocused = findFocusableViewInMyBounds(
1216 movingRight, mScroller.getFinalX(), movingDown, mScroller.getFinalY(), findFocus());
1217 if (newFocused == null)
1218 {
1219 newFocused = this;
1220 }
1221
1222 if (newFocused != findFocus() &&
1223 newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP))
1224 {
1225 mTwoDScrollViewMovedFocus = true;
1226 mTwoDScrollViewMovedFocus = false;
1227 }
1228
1229 awakenScrollBars(mScroller.getDuration());
1230 invalidate();
1231 }
1232 }
1233
1239 public void scrollTo(int x, int y)
1240 {
1241 // we rely on the fact the View.scrollBy calls scrollTo.
1242 if (getChildCount() > 0)
1243 {
1244 View child = getChildAt(0);
1245 x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
1246 y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
1247 if (x != getScrollX() || y != getScrollY())
1248 {
1249 super.scrollTo(x, y);
1250 }
1251 }
1252 }
1253
1254 private int clamp(int n, int my, int child)
1255 {
1256 if (my >= child || n < 0)
1257 {
1258 /* my >= child is this case:
1259 * |--------------- me ---------------|
1260 * |------ child ------|
1261 * or
1262 * |--------------- me ---------------|
1263 * |------ child ------|
1264 * or
1265 * |--------------- me ---------------|
1266 * |------ child ------|
1267 *
1268 * n < 0 is this case:
1269 * |------ me ------|
1270 * |-------- child --------|
1271 * |-- mScrollX --|
1272 */
1273 return 0;
1274 }
1275 if ((my + n) > child)
1276 {
1277 /* this case:
1278 * |------ me ------|
1279 * |------ child ------|
1280 * |-- mScrollX --|
1281 */
1282 return child - my;
1283 }
1284 return n;
1285 }
1286
1287 public void setScrollViewListener(ScrollView2DListener scrollViewListener)
1288 {
1289 this.scrollView2DListener = scrollViewListener;
1290 }
1291
1292 @Override protected void onScrollChanged(int x, int y, int oldx, int oldy)
1293 {
1294 super.onScrollChanged(x, y, oldx, oldy);
1295 if (scrollView2DListener != null)
1296 {
1297 scrollView2DListener.onScrollChanged(this, x, y, oldx, oldy);
1298 }
1299 }
1300
1301 // interface to receive notifications when the view is scrolled
1302 public interface ScrollView2DListener {
1303 void onScrollChanged(ScrollView2D scrollView, int x, int y, int oldx, int oldy);
1304 }
1305}
boolean arrowScroll(int direction, boolean horizontal)
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
boolean fullScroll(int direction, boolean horizontal)