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