1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text.method;
import android.graphics.Rect;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
/**
* A movement method that provides cursor movement and selection.
* Supports displaying the context menu on DPad Center.
*/
public class ArrowKeyMovementMethod extends BaseMovementMethod implements MovementMethod {
private static boolean isSelecting(Spannable buffer) {
return ((MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SHIFT_ON) == 1) ||
(MetaKeyKeyListener.getMetaState(buffer, MetaKeyKeyListener.META_SELECTING) != 0));
}
private static int getCurrentLineTop(Spannable buffer, Layout layout) {
return layout.getLineTop(layout.getLineForOffset(Selection.getSelectionEnd(buffer)));
}
private static int getPageHeight(TextView widget) {
// This calculation does not take into account the view transformations that
// may have been applied to the child or its containers. In case of scaling or
// rotation, the calculated page height may be incorrect.
final Rect rect = new Rect();
return widget.getGlobalVisibleRect(rect) ? rect.height() : 0;
}
@Override
protected boolean handleMovementKey(TextView widget, Spannable buffer, int keyCode,
int movementMetaState, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
if (KeyEvent.metaStateHasNoModifiers(movementMetaState)) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0
&& MetaKeyKeyListener.getMetaState(buffer,
MetaKeyKeyListener.META_SELECTING, event) != 0) {
return widget.showContextMenu();
}
}
break;
}
return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
}
@Override
protected boolean left(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendLeft(buffer, layout);
} else {
return Selection.moveLeft(buffer, layout);
}
}
@Override
protected boolean right(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendRight(buffer, layout);
} else {
return Selection.moveRight(buffer, layout);
}
}
@Override
protected boolean up(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendUp(buffer, layout);
} else {
return Selection.moveUp(buffer, layout);
}
}
@Override
protected boolean down(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendDown(buffer, layout);
} else {
return Selection.moveDown(buffer, layout);
}
}
@Override
protected boolean pageUp(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
final boolean selecting = isSelecting(buffer);
final int targetY = getCurrentLineTop(buffer, layout) - getPageHeight(widget);
boolean handled = false;
for (;;) {
final int previousSelectionEnd = Selection.getSelectionEnd(buffer);
if (selecting) {
Selection.extendUp(buffer, layout);
} else {
Selection.moveUp(buffer, layout);
}
if (Selection.getSelectionEnd(buffer) == previousSelectionEnd) {
break;
}
handled = true;
if (getCurrentLineTop(buffer, layout) <= targetY) {
break;
}
}
return handled;
}
@Override
protected boolean pageDown(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
final boolean selecting = isSelecting(buffer);
final int targetY = getCurrentLineTop(buffer, layout) + getPageHeight(widget);
boolean handled = false;
for (;;) {
final int previousSelectionEnd = Selection.getSelectionEnd(buffer);
if (selecting) {
Selection.extendDown(buffer, layout);
} else {
Selection.moveDown(buffer, layout);
}
if (Selection.getSelectionEnd(buffer) == previousSelectionEnd) {
break;
}
handled = true;
if (getCurrentLineTop(buffer, layout) >= targetY) {
break;
}
}
return handled;
}
@Override
protected boolean top(TextView widget, Spannable buffer) {
if (isSelecting(buffer)) {
Selection.extendSelection(buffer, 0);
} else {
Selection.setSelection(buffer, 0);
}
return true;
}
@Override
protected boolean bottom(TextView widget, Spannable buffer) {
if (isSelecting(buffer)) {
Selection.extendSelection(buffer, buffer.length());
} else {
Selection.setSelection(buffer, buffer.length());
}
return true;
}
@Override
protected boolean lineStart(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendToLeftEdge(buffer, layout);
} else {
return Selection.moveToLeftEdge(buffer, layout);
}
}
@Override
protected boolean lineEnd(TextView widget, Spannable buffer) {
final Layout layout = widget.getLayout();
if (isSelecting(buffer)) {
return Selection.extendToRightEdge(buffer, layout);
} else {
return Selection.moveToRightEdge(buffer, layout);
}
}
/** {@hide} */
@Override
protected boolean leftWord(TextView widget, Spannable buffer) {
final int selectionEnd = widget.getSelectionEnd();
final WordIterator wordIterator = widget.getWordIterator();
wordIterator.setCharSequence(buffer, selectionEnd, selectionEnd);
return Selection.moveToPreceding(buffer, wordIterator, isSelecting(buffer));
}
/** {@hide} */
@Override
protected boolean rightWord(TextView widget, Spannable buffer) {
final int selectionEnd = widget.getSelectionEnd();
final WordIterator wordIterator = widget.getWordIterator();
wordIterator.setCharSequence(buffer, selectionEnd, selectionEnd);
return Selection.moveToFollowing(buffer, wordIterator, isSelecting(buffer));
}
@Override
protected boolean home(TextView widget, Spannable buffer) {
return lineStart(widget, buffer);
}
@Override
protected boolean end(TextView widget, Spannable buffer) {
return lineEnd(widget, buffer);
}
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int initialScrollX = -1;
int initialScrollY = -1;
final int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
initialScrollX = Touch.getInitialScrollX(widget, buffer);
initialScrollY = Touch.getInitialScrollY(widget, buffer);
}
boolean wasTouchSelecting = isSelecting(buffer);
boolean handled = Touch.onTouchEvent(widget, buffer, event);
if (widget.didTouchFocusSelect()) {
return handled;
}
if (action == MotionEvent.ACTION_DOWN) {
// For touch events, the code should run only when selection is active.
if (isSelecting(buffer)) {
if (!widget.isFocused()) {
if (!widget.requestFocus()) {
return handled;
}
}
int offset = widget.getOffsetForPosition(event.getX(), event.getY());
buffer.setSpan(LAST_TAP_DOWN, offset, offset, Spannable.SPAN_POINT_POINT);
// Disallow intercepting of the touch events, so that
// users can scroll and select at the same time.
// without this, users would get booted out of select
// mode once the view detected it needed to scroll.
widget.getParent().requestDisallowInterceptTouchEvent(true);
}
} else if (widget.isFocused()) {
if (action == MotionEvent.ACTION_MOVE) {
if (isSelecting(buffer) && handled) {
final int startOffset = buffer.getSpanStart(LAST_TAP_DOWN);
// Before selecting, make sure we've moved out of the "slop".
// handled will be true, if we're in select mode AND we're
// OUT of the slop
// Turn long press off while we're selecting. User needs to
// re-tap on the selection to enable long press
widget.cancelLongPress();
// Update selection as we're moving the selection area.
// Get the current touch position
final int offset = widget.getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection(buffer, Math.min(startOffset, offset),
Math.max(startOffset, offset));
return true;
}
} else if (action == MotionEvent.ACTION_UP) {
// If we have scrolled, then the up shouldn't move the cursor,
// but we do need to make sure the cursor is still visible at
// the current scroll offset to avoid the scroll jumping later
// to show it.
if ((initialScrollY >= 0 && initialScrollY != widget.getScrollY()) ||
(initialScrollX >= 0 && initialScrollX != widget.getScrollX())) {
widget.moveCursorToVisibleOffset();
return true;
}
if (wasTouchSelecting) {
final int startOffset = buffer.getSpanStart(LAST_TAP_DOWN);
final int endOffset = widget.getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection(buffer, Math.min(startOffset, endOffset),
Math.max(startOffset, endOffset));
buffer.removeSpan(LAST_TAP_DOWN);
}
MetaKeyKeyListener.adjustMetaAfterKeypress(buffer);
MetaKeyKeyListener.resetLockedMeta(buffer);
return true;
}
}
return handled;
}
@Override
public boolean canSelectArbitrarily() {
return true;
}
@Override
public void initialize(TextView widget, Spannable text) {
Selection.setSelection(text, 0);
}
@Override
public void onTakeFocus(TextView view, Spannable text, int dir) {
if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
if (view.getLayout() == null) {
// This shouldn't be null, but do something sensible if it is.
Selection.setSelection(text, text.length());
}
} else {
Selection.setSelection(text, text.length());
}
}
public static MovementMethod getInstance() {
if (sInstance == null) {
sInstance = new ArrowKeyMovementMethod();
}
return sInstance;
}
private static final Object LAST_TAP_DOWN = new Object();
private static ArrowKeyMovementMethod sInstance;
}
|