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
17package android.text.method;
18
19import android.text.Layout;
20import android.text.NoCopySpan;
21import android.text.Selection;
22import android.text.Spannable;
23import android.text.style.ClickableSpan;
24import android.view.KeyEvent;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.TextView;
28
29/**
30 * A movement method that traverses links in the text buffer and scrolls if necessary.
31 * Supports clicking on links with DPad Center or Enter.
32 */
33public class LinkMovementMethod extends ScrollingMovementMethod {
34    private static final int CLICK = 1;
35    private static final int UP = 2;
36    private static final int DOWN = 3;
37
38    @Override
39    public boolean canSelectArbitrarily() {
40        return true;
41    }
42
43    @Override
44    protected boolean handleMovementKey(TextView widget, Spannable buffer, int keyCode,
45            int movementMetaState, KeyEvent event) {
46        switch (keyCode) {
47            case KeyEvent.KEYCODE_DPAD_CENTER:
48            case KeyEvent.KEYCODE_ENTER:
49                if (KeyEvent.metaStateHasNoModifiers(movementMetaState)) {
50                    if (event.getAction() == KeyEvent.ACTION_DOWN &&
51                            event.getRepeatCount() == 0 && action(CLICK, widget, buffer)) {
52                        return true;
53                    }
54                }
55                break;
56        }
57        return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
58    }
59
60    @Override
61    protected boolean up(TextView widget, Spannable buffer) {
62        if (action(UP, widget, buffer)) {
63            return true;
64        }
65
66        return super.up(widget, buffer);
67    }
68
69    @Override
70    protected boolean down(TextView widget, Spannable buffer) {
71        if (action(DOWN, widget, buffer)) {
72            return true;
73        }
74
75        return super.down(widget, buffer);
76    }
77
78    @Override
79    protected boolean left(TextView widget, Spannable buffer) {
80        if (action(UP, widget, buffer)) {
81            return true;
82        }
83
84        return super.left(widget, buffer);
85    }
86
87    @Override
88    protected boolean right(TextView widget, Spannable buffer) {
89        if (action(DOWN, widget, buffer)) {
90            return true;
91        }
92
93        return super.right(widget, buffer);
94    }
95
96    private boolean action(int what, TextView widget, Spannable buffer) {
97        Layout layout = widget.getLayout();
98
99        int padding = widget.getTotalPaddingTop() +
100                      widget.getTotalPaddingBottom();
101        int areaTop = widget.getScrollY();
102        int areaBot = areaTop + widget.getHeight() - padding;
103
104        int lineTop = layout.getLineForVertical(areaTop);
105        int lineBot = layout.getLineForVertical(areaBot);
106
107        int first = layout.getLineStart(lineTop);
108        int last = layout.getLineEnd(lineBot);
109
110        ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
111
112        int a = Selection.getSelectionStart(buffer);
113        int b = Selection.getSelectionEnd(buffer);
114
115        int selStart = Math.min(a, b);
116        int selEnd = Math.max(a, b);
117
118        if (selStart < 0) {
119            if (buffer.getSpanStart(FROM_BELOW) >= 0) {
120                selStart = selEnd = buffer.length();
121            }
122        }
123
124        if (selStart > last)
125            selStart = selEnd = Integer.MAX_VALUE;
126        if (selEnd < first)
127            selStart = selEnd = -1;
128
129        switch (what) {
130        case CLICK:
131            if (selStart == selEnd) {
132                return false;
133            }
134
135            ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
136
137            if (link.length != 1)
138                return false;
139
140            link[0].onClick(widget);
141            break;
142
143        case UP:
144            int bestStart, bestEnd;
145
146            bestStart = -1;
147            bestEnd = -1;
148
149            for (int i = 0; i < candidates.length; i++) {
150                int end = buffer.getSpanEnd(candidates[i]);
151
152                if (end < selEnd || selStart == selEnd) {
153                    if (end > bestEnd) {
154                        bestStart = buffer.getSpanStart(candidates[i]);
155                        bestEnd = end;
156                    }
157                }
158            }
159
160            if (bestStart >= 0) {
161                Selection.setSelection(buffer, bestEnd, bestStart);
162                return true;
163            }
164
165            break;
166
167        case DOWN:
168            bestStart = Integer.MAX_VALUE;
169            bestEnd = Integer.MAX_VALUE;
170
171            for (int i = 0; i < candidates.length; i++) {
172                int start = buffer.getSpanStart(candidates[i]);
173
174                if (start > selStart || selStart == selEnd) {
175                    if (start < bestStart) {
176                        bestStart = start;
177                        bestEnd = buffer.getSpanEnd(candidates[i]);
178                    }
179                }
180            }
181
182            if (bestEnd < Integer.MAX_VALUE) {
183                Selection.setSelection(buffer, bestStart, bestEnd);
184                return true;
185            }
186
187            break;
188        }
189
190        return false;
191    }
192
193    @Override
194    public boolean onTouchEvent(TextView widget, Spannable buffer,
195                                MotionEvent event) {
196        int action = event.getAction();
197
198        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
199            int x = (int) event.getX();
200            int y = (int) event.getY();
201
202            x -= widget.getTotalPaddingLeft();
203            y -= widget.getTotalPaddingTop();
204
205            x += widget.getScrollX();
206            y += widget.getScrollY();
207
208            Layout layout = widget.getLayout();
209            int line = layout.getLineForVertical(y);
210            int off = layout.getOffsetForHorizontal(line, x);
211
212            ClickableSpan[] links = buffer.getSpans(off, off, ClickableSpan.class);
213
214            if (links.length != 0) {
215                if (action == MotionEvent.ACTION_UP) {
216                    links[0].onClick(widget);
217                } else if (action == MotionEvent.ACTION_DOWN) {
218                    Selection.setSelection(buffer,
219                        buffer.getSpanStart(links[0]),
220                        buffer.getSpanEnd(links[0]));
221                }
222                return true;
223            } else {
224                Selection.removeSelection(buffer);
225            }
226        }
227
228        return super.onTouchEvent(widget, buffer, event);
229    }
230
231    @Override
232    public void initialize(TextView widget, Spannable text) {
233        Selection.removeSelection(text);
234        text.removeSpan(FROM_BELOW);
235    }
236
237    @Override
238    public void onTakeFocus(TextView view, Spannable text, int dir) {
239        Selection.removeSelection(text);
240
241        if ((dir & View.FOCUS_BACKWARD) != 0) {
242            text.setSpan(FROM_BELOW, 0, 0, Spannable.SPAN_POINT_POINT);
243        } else {
244            text.removeSpan(FROM_BELOW);
245        }
246    }
247
248    public static MovementMethod getInstance() {
249        if (sInstance == null)
250            sInstance = new LinkMovementMethod();
251
252        return sInstance;
253    }
254
255    private static LinkMovementMethod sInstance;
256    private static Object FROM_BELOW = new NoCopySpan.Concrete();
257}
258