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 package android.text.method;
18 
19 import android.text.Layout;
20 import android.text.Spannable;
21 import android.view.MotionEvent;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 /**
26  * A movement method that interprets movement keys by scrolling the text buffer.
27  */
28 public class ScrollingMovementMethod extends BaseMovementMethod implements MovementMethod {
29     @Override
left(TextView widget, Spannable buffer)30     protected boolean left(TextView widget, Spannable buffer) {
31         return scrollLeft(widget, buffer, 1);
32     }
33 
34     @Override
right(TextView widget, Spannable buffer)35     protected boolean right(TextView widget, Spannable buffer) {
36         return scrollRight(widget, buffer, 1);
37     }
38 
39     @Override
up(TextView widget, Spannable buffer)40     protected boolean up(TextView widget, Spannable buffer) {
41         return scrollUp(widget, buffer, 1);
42     }
43 
44     @Override
down(TextView widget, Spannable buffer)45     protected boolean down(TextView widget, Spannable buffer) {
46         return scrollDown(widget, buffer, 1);
47     }
48 
49     @Override
pageUp(TextView widget, Spannable buffer)50     protected boolean pageUp(TextView widget, Spannable buffer) {
51         return scrollPageUp(widget, buffer);
52     }
53 
54     @Override
pageDown(TextView widget, Spannable buffer)55     protected boolean pageDown(TextView widget, Spannable buffer) {
56         return scrollPageDown(widget, buffer);
57     }
58 
59     @Override
top(TextView widget, Spannable buffer)60     protected boolean top(TextView widget, Spannable buffer) {
61         return scrollTop(widget, buffer);
62     }
63 
64     @Override
bottom(TextView widget, Spannable buffer)65     protected boolean bottom(TextView widget, Spannable buffer) {
66         return scrollBottom(widget, buffer);
67     }
68 
69     @Override
lineStart(TextView widget, Spannable buffer)70     protected boolean lineStart(TextView widget, Spannable buffer) {
71         return scrollLineStart(widget, buffer);
72     }
73 
74     @Override
lineEnd(TextView widget, Spannable buffer)75     protected boolean lineEnd(TextView widget, Spannable buffer) {
76         return scrollLineEnd(widget, buffer);
77     }
78 
79     @Override
home(TextView widget, Spannable buffer)80     protected boolean home(TextView widget, Spannable buffer) {
81         return top(widget, buffer);
82     }
83 
84     @Override
end(TextView widget, Spannable buffer)85     protected boolean end(TextView widget, Spannable buffer) {
86         return bottom(widget, buffer);
87     }
88 
89     @Override
onTouchEvent(TextView widget, Spannable buffer, MotionEvent event)90     public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
91         return Touch.onTouchEvent(widget, buffer, event);
92     }
93 
94     @Override
onTakeFocus(TextView widget, Spannable text, int dir)95     public void onTakeFocus(TextView widget, Spannable text, int dir) {
96         Layout layout = widget.getLayout();
97 
98         if (layout != null && (dir & View.FOCUS_FORWARD) != 0) {
99             widget.scrollTo(widget.getScrollX(),
100                             layout.getLineTop(0));
101         }
102         if (layout != null && (dir & View.FOCUS_BACKWARD) != 0) {
103             int padding = widget.getTotalPaddingTop() +
104                           widget.getTotalPaddingBottom();
105             int line = layout.getLineCount() - 1;
106 
107             widget.scrollTo(widget.getScrollX(),
108                             layout.getLineTop(line+1) -
109                             (widget.getHeight() - padding));
110         }
111     }
112 
getInstance()113     public static MovementMethod getInstance() {
114         if (sInstance == null)
115             sInstance = new ScrollingMovementMethod();
116 
117         return sInstance;
118     }
119 
120     private static ScrollingMovementMethod sInstance;
121 }
122