1 /* 2 * Copyright (C) 2011 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 17 package com.android.contacts.dialpad; 18 19 import android.content.Context; 20 import android.graphics.Rect; 21 import android.text.InputType; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 import android.view.accessibility.AccessibilityEvent; 25 import android.view.inputmethod.InputMethodManager; 26 import android.widget.EditText; 27 28 /** 29 * EditText which suppresses IME show up. 30 */ 31 public class DigitsEditText extends EditText { DigitsEditText(Context context, AttributeSet attrs)32 public DigitsEditText(Context context, AttributeSet attrs) { 33 super(context, attrs); 34 setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 35 setShowSoftInputOnFocus(false); 36 } 37 38 @Override onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)39 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 40 super.onFocusChanged(focused, direction, previouslyFocusedRect); 41 final InputMethodManager imm = ((InputMethodManager) getContext() 42 .getSystemService(Context.INPUT_METHOD_SERVICE)); 43 if (imm != null && imm.isActive(this)) { 44 imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0); 45 } 46 } 47 48 @Override onTouchEvent(MotionEvent event)49 public boolean onTouchEvent(MotionEvent event) { 50 final boolean ret = super.onTouchEvent(event); 51 // Must be done after super.onTouchEvent() 52 final InputMethodManager imm = ((InputMethodManager) getContext() 53 .getSystemService(Context.INPUT_METHOD_SERVICE)); 54 if (imm != null && imm.isActive(this)) { 55 imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0); 56 } 57 return ret; 58 } 59 60 @Override sendAccessibilityEventUnchecked(AccessibilityEvent event)61 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { 62 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) { 63 // Since we're replacing the text every time we add or remove a 64 // character, only read the difference. (issue 5337550) 65 final int added = event.getAddedCount(); 66 final int removed = event.getRemovedCount(); 67 final int length = event.getBeforeText().length(); 68 if (added > removed) { 69 event.setRemovedCount(0); 70 event.setAddedCount(1); 71 event.setFromIndex(length); 72 } else if (removed > added) { 73 event.setRemovedCount(1); 74 event.setAddedCount(0); 75 event.setFromIndex(length - 1); 76 } else { 77 return; 78 } 79 } else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) { 80 // The parent EditText class lets tts read "edit box" when this View has a focus, which 81 // confuses users on app launch (issue 5275935). 82 return; 83 } 84 super.sendAccessibilityEventUnchecked(event); 85 } 86 }