1/* 2 * Copyright (C) 2017 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.support.text.emoji.widget; 18 19import android.content.Context; 20import android.support.annotation.IntRange; 21import android.support.annotation.Nullable; 22import android.support.text.emoji.EmojiCompat; 23import android.support.v7.widget.AppCompatEditText; 24import android.util.AttributeSet; 25import android.view.inputmethod.EditorInfo; 26import android.view.inputmethod.InputConnection; 27 28/** 29 * AppCompatEditText widget enhanced with emoji capability by using {@link EmojiEditTextHelper}. 30 * When used on devices running API 18 or below, this widget acts as a regular 31 * {@link AppCompatEditText}. 32 */ 33public class EmojiAppCompatEditText extends AppCompatEditText { 34 private EmojiEditTextHelper mEmojiEditTextHelper; 35 private boolean mInitialized; 36 37 public EmojiAppCompatEditText(Context context) { 38 super(context); 39 init(null /*attrs*/, 0 /*defStyleAttr*/); 40 } 41 42 public EmojiAppCompatEditText(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 init(attrs, android.support.v7.appcompat.R.attr.editTextStyle); 45 } 46 47 public EmojiAppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) { 48 super(context, attrs, defStyleAttr); 49 init(attrs, defStyleAttr); 50 } 51 52 private void init(@Nullable AttributeSet attrs, int defStyleAttr) { 53 if (!mInitialized) { 54 mInitialized = true; 55 final EditTextAttributeHelper attrHelper = new EditTextAttributeHelper(this, attrs, 56 defStyleAttr); 57 setMaxEmojiCount(attrHelper.getMaxEmojiCount()); 58 setKeyListener(super.getKeyListener()); 59 } 60 } 61 62 @Override 63 public void setKeyListener(android.text.method.KeyListener input) { 64 super.setKeyListener(getEmojiEditTextHelper().getKeyListener(input)); 65 } 66 67 @Override 68 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 69 InputConnection inputConnection = super.onCreateInputConnection(outAttrs); 70 return getEmojiEditTextHelper().onCreateInputConnection(inputConnection, outAttrs); 71 } 72 73 /** 74 * Set the maximum number of EmojiSpans to be added to a CharSequence. The number of spans in a 75 * CharSequence affects the performance of the EditText insert/delete operations. Insert/delete 76 * operations slow down as the number of spans increases. 77 * 78 * @param maxEmojiCount maximum number of EmojiSpans to be added to a single CharSequence, 79 * should be equal or greater than 0 80 * 81 * @see EmojiCompat#process(CharSequence, int, int, int) 82 */ 83 public void setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount) { 84 getEmojiEditTextHelper().setMaxEmojiCount(maxEmojiCount); 85 } 86 87 /** 88 * Returns the maximum number of EmojiSpans to be added to a CharSequence. 89 * 90 * @see #setMaxEmojiCount(int) 91 * @see EmojiCompat#process(CharSequence, int, int, int) 92 */ 93 public int getMaxEmojiCount() { 94 return getEmojiEditTextHelper().getMaxEmojiCount(); 95 } 96 97 private EmojiEditTextHelper getEmojiEditTextHelper() { 98 if (mEmojiEditTextHelper == null) { 99 mEmojiEditTextHelper = new EmojiEditTextHelper(this); 100 } 101 return mEmojiEditTextHelper; 102 } 103} 104