BaseKeyListener.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.view.KeyEvent; 20import android.view.View; 21import android.os.Message; 22import android.util.Log; 23import android.text.*; 24import android.widget.TextView; 25 26public abstract class BaseKeyListener 27extends MetaKeyKeyListener 28implements KeyListener { 29 /* package */ static final Object OLD_SEL_START = new Object(); 30 31 /** 32 * Performs the action that happens when you press the DEL key in 33 * a TextView. If there is a selection, deletes the selection; 34 * otherwise, DEL alone deletes the character before the cursor, 35 * if any; 36 * ALT+DEL deletes everything on the line the cursor is on. 37 * 38 * @return true if anything was deleted; false otherwise. 39 */ 40 public boolean backspace(View view, Editable content, int keyCode, 41 KeyEvent event) { 42 int selStart, selEnd; 43 boolean result = true; 44 45 { 46 int a = Selection.getSelectionStart(content); 47 int b = Selection.getSelectionEnd(content); 48 49 selStart = Math.min(a, b); 50 selEnd = Math.max(a, b); 51 } 52 53 if (selStart != selEnd) { 54 content.delete(selStart, selEnd); 55 } else if (altBackspace(view, content, keyCode, event)) { 56 result = true; 57 } else { 58 int to = TextUtils.getOffsetBefore(content, selEnd); 59 60 if (to != selEnd) { 61 content.delete(Math.min(to, selEnd), Math.max(to, selEnd)); 62 } 63 else { 64 result = false; 65 } 66 } 67 68 if (result) 69 adjustMetaAfterKeypress(content); 70 71 return result; 72 } 73 74 private boolean altBackspace(View view, Editable content, int keyCode, 75 KeyEvent event) { 76 if (getMetaState(content, META_ALT_ON) != 1) { 77 return false; 78 } 79 80 if (!(view instanceof TextView)) { 81 return false; 82 } 83 84 Layout layout = ((TextView) view).getLayout(); 85 86 if (layout == null) { 87 return false; 88 } 89 90 int l = layout.getLineForOffset(Selection.getSelectionStart(content)); 91 int start = layout.getLineStart(l); 92 int end = layout.getLineEnd(l); 93 94 if (end == start) { 95 return false; 96 } 97 98 content.delete(start, end); 99 return true; 100 } 101 102 public boolean onKeyDown(View view, Editable content, 103 int keyCode, KeyEvent event) { 104 if (keyCode == KeyEvent.KEYCODE_DEL) { 105 backspace(view, content, keyCode, event); 106 return true; 107 } 108 109 return super.onKeyDown(view, content, keyCode, event); 110 } 111} 112 113