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