ExtractEditText.java revision 15a4d2ffd04dc6c70f2cd17dae12ac6bc14c69ab
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.inputmethodservice; 18 19import android.content.Context; 20import android.util.AttributeSet; 21import android.view.inputmethod.ExtractedText; 22import android.widget.EditText; 23 24/*** 25 * Specialization of {@link EditText} for showing and interacting with the 26 * extracted text in a full-screen input method. 27 */ 28public class ExtractEditText extends EditText { 29 private InputMethodService mIME; 30 private int mSettingExtractedText; 31 32 public ExtractEditText(Context context) { 33 super(context, null); 34 } 35 36 public ExtractEditText(Context context, AttributeSet attrs) { 37 super(context, attrs, com.android.internal.R.attr.editTextStyle); 38 } 39 40 public ExtractEditText(Context context, AttributeSet attrs, int defStyle) { 41 super(context, attrs, defStyle); 42 } 43 44 void setIME(InputMethodService ime) { 45 mIME = ime; 46 } 47 48 /** 49 * Start making changes that will not be reported to the client. That 50 * is, {@link #onSelectionChanged(int, int)} will not result in sending 51 * the new selection to the client 52 */ 53 public void startInternalChanges() { 54 mSettingExtractedText += 1; 55 } 56 57 /** 58 * Finish making changes that will not be reported to the client. That 59 * is, {@link #onSelectionChanged(int, int)} will not result in sending 60 * the new selection to the client 61 */ 62 public void finishInternalChanges() { 63 mSettingExtractedText -= 1; 64 } 65 66 /** 67 * Implement just to keep track of when we are setting text from the 68 * client (vs. seeing changes in ourself from the user). 69 */ 70 @Override public void setExtractedText(ExtractedText text) { 71 try { 72 mSettingExtractedText++; 73 super.setExtractedText(text); 74 } finally { 75 mSettingExtractedText--; 76 } 77 } 78 79 /** 80 * Report to the underlying text editor about selection changes. 81 */ 82 @Override protected void onSelectionChanged(int selStart, int selEnd) { 83 if (mSettingExtractedText == 0 && mIME != null && selStart >= 0 && selEnd >= 0) { 84 mIME.onExtractedSelectionChanged(selStart, selEnd); 85 } 86 } 87 88 /** 89 * Redirect clicks to the IME for handling there. First allows any 90 * on click handler to run, though. 91 */ 92 @Override public boolean performClick() { 93 if (!super.performClick() && mIME != null) { 94 mIME.onExtractedTextClicked(); 95 return true; 96 } 97 return false; 98 } 99 100 @Override public boolean onTextContextMenuItem(int id) { 101 if (mIME != null) { 102 if (mIME.onExtractTextContextMenuItem(id)) { 103 return true; 104 } 105 } 106 return super.onTextContextMenuItem(id); 107 } 108 109 /** 110 * We are always considered to be an input method target. 111 */ 112 public boolean isInputMethodTarget() { 113 return true; 114 } 115 116 /** 117 * Return true if the edit text is currently showing a scroll bar. 118 */ 119 public boolean hasVerticalScrollBar() { 120 return computeVerticalScrollRange() > computeVerticalScrollExtent(); 121 } 122 123 /** 124 * Pretend like the window this view is in always has focus, so its 125 * highlight and cursor will be displayed. 126 */ 127 @Override public boolean hasWindowFocus() { 128 return this.isEnabled() ? true : false; 129 } 130 131 /** 132 * Pretend like this view always has focus, so its 133 * highlight and cursor will be displayed. 134 */ 135 @Override public boolean isFocused() { 136 return this.isEnabled() ? true : false; 137 } 138 139 /** 140 * Pretend like this view always has focus, so its 141 * highlight and cursor will be displayed. 142 */ 143 @Override public boolean hasFocus() { 144 return this.isEnabled() ? true : false; 145 } 146} 147