1/* 2 * Copyright (C) 2008 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.Layout.Alignment; 21import android.text.NoCopySpan; 22import android.text.Spannable; 23import android.view.KeyEvent; 24import android.view.MotionEvent; 25import android.view.ViewConfiguration; 26import android.widget.TextView; 27 28public class Touch { 29 private Touch() { } 30 31 /** 32 * Scrolls the specified widget to the specified coordinates, except 33 * constrains the X scrolling position to the horizontal regions of 34 * the text that will be visible after scrolling to the specified 35 * Y position. 36 */ 37 public static void scrollTo(TextView widget, Layout layout, int x, int y) { 38 final int horizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight(); 39 final int availableWidth = widget.getWidth() - horizontalPadding; 40 41 final int top = layout.getLineForVertical(y); 42 Alignment a = layout.getParagraphAlignment(top); 43 boolean ltr = layout.getParagraphDirection(top) > 0; 44 45 int left, right; 46 if (widget.getHorizontallyScrolling()) { 47 final int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom(); 48 final int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding); 49 50 left = Integer.MAX_VALUE; 51 right = 0; 52 53 for (int i = top; i <= bottom; i++) { 54 left = (int) Math.min(left, layout.getLineLeft(i)); 55 right = (int) Math.max(right, layout.getLineRight(i)); 56 } 57 } else { 58 left = 0; 59 right = availableWidth; 60 } 61 62 final int actualWidth = right - left; 63 64 if (actualWidth < availableWidth) { 65 if (a == Alignment.ALIGN_CENTER) { 66 x = left - ((availableWidth - actualWidth) / 2); 67 } else if ((ltr && (a == Alignment.ALIGN_OPPOSITE)) || 68 (!ltr && (a == Alignment.ALIGN_NORMAL)) || 69 (a == Alignment.ALIGN_RIGHT)) { 70 // align_opposite does NOT mean align_right, we need the paragraph 71 // direction to resolve it to left or right 72 x = left - (availableWidth - actualWidth); 73 } else { 74 x = left; 75 } 76 } else { 77 x = Math.min(x, right - availableWidth); 78 x = Math.max(x, left); 79 } 80 81 widget.scrollTo(x, y); 82 } 83 84 /** 85 * Handles touch events for dragging. You may want to do other actions 86 * like moving the cursor on touch as well. 87 */ 88 public static boolean onTouchEvent(TextView widget, Spannable buffer, 89 MotionEvent event) { 90 DragState[] ds; 91 92 switch (event.getActionMasked()) { 93 case MotionEvent.ACTION_DOWN: 94 ds = buffer.getSpans(0, buffer.length(), DragState.class); 95 96 for (int i = 0; i < ds.length; i++) { 97 buffer.removeSpan(ds[i]); 98 } 99 100 buffer.setSpan(new DragState(event.getX(), event.getY(), 101 widget.getScrollX(), widget.getScrollY()), 102 0, 0, Spannable.SPAN_MARK_MARK); 103 return true; 104 105 case MotionEvent.ACTION_UP: 106 ds = buffer.getSpans(0, buffer.length(), DragState.class); 107 108 for (int i = 0; i < ds.length; i++) { 109 buffer.removeSpan(ds[i]); 110 } 111 112 if (ds.length > 0 && ds[0].mUsed) { 113 return true; 114 } else { 115 return false; 116 } 117 118 case MotionEvent.ACTION_MOVE: 119 ds = buffer.getSpans(0, buffer.length(), DragState.class); 120 121 if (ds.length > 0) { 122 if (ds[0].mFarEnough == false) { 123 int slop = ViewConfiguration.get(widget.getContext()).getScaledTouchSlop(); 124 125 if (Math.abs(event.getX() - ds[0].mX) >= slop || 126 Math.abs(event.getY() - ds[0].mY) >= slop) { 127 ds[0].mFarEnough = true; 128 } 129 } 130 131 if (ds[0].mFarEnough) { 132 ds[0].mUsed = true; 133 boolean cap = (event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0 134 || MetaKeyKeyListener.getMetaState(buffer, 135 MetaKeyKeyListener.META_SHIFT_ON) == 1 136 || MetaKeyKeyListener.getMetaState(buffer, 137 MetaKeyKeyListener.META_SELECTING) != 0; 138 float dx; 139 float dy; 140 if (cap) { 141 // if we're selecting, we want the scroll to go in 142 // the direction of the drag 143 dx = event.getX() - ds[0].mX; 144 dy = event.getY() - ds[0].mY; 145 } else { 146 dx = ds[0].mX - event.getX(); 147 dy = ds[0].mY - event.getY(); 148 } 149 ds[0].mX = event.getX(); 150 ds[0].mY = event.getY(); 151 152 int nx = widget.getScrollX() + (int) dx; 153 int ny = widget.getScrollY() + (int) dy; 154 155 int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom(); 156 Layout layout = widget.getLayout(); 157 158 ny = Math.min(ny, layout.getHeight() - (widget.getHeight() - padding)); 159 ny = Math.max(ny, 0); 160 161 int oldX = widget.getScrollX(); 162 int oldY = widget.getScrollY(); 163 164 scrollTo(widget, layout, nx, ny); 165 166 // If we actually scrolled, then cancel the up action. 167 if (oldX != widget.getScrollX() || oldY != widget.getScrollY()) { 168 widget.cancelLongPress(); 169 } 170 171 return true; 172 } 173 } 174 } 175 176 return false; 177 } 178 179 /** 180 * @param widget The text view. 181 * @param buffer The text buffer. 182 */ 183 public static int getInitialScrollX(TextView widget, Spannable buffer) { 184 DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class); 185 return ds.length > 0 ? ds[0].mScrollX : -1; 186 } 187 188 /** 189 * @param widget The text view. 190 * @param buffer The text buffer. 191 */ 192 public static int getInitialScrollY(TextView widget, Spannable buffer) { 193 DragState[] ds = buffer.getSpans(0, buffer.length(), DragState.class); 194 return ds.length > 0 ? ds[0].mScrollY : -1; 195 } 196 197 private static class DragState implements NoCopySpan { 198 public float mX; 199 public float mY; 200 public int mScrollX; 201 public int mScrollY; 202 public boolean mFarEnough; 203 public boolean mUsed; 204 205 public DragState(float x, float y, int scrollX, int scrollY) { 206 mX = x; 207 mY = y; 208 mScrollX = scrollX; 209 mScrollY = scrollY; 210 } 211 } 212} 213