EmojiEditText.java revision 7aee4cbe81e43d7cfcb271809caefccfe6b86a89
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 */
33public class EmojiEditText extends EditText {
34    private EmojiEditTextHelper mEmojiEditTextHelper;
35
36    /**
37     * Prevent calling {@link #init(AttributeSet, int)} multiple times in case super() constructors
38     * call other constructors.
39     */
40    private boolean mInitialized;
41
42    public EmojiEditText(Context context) {
43        super(context);
44        init(null /*attrs*/, 0 /*defStyleAttr*/);
45    }
46
47    public EmojiEditText(Context context, AttributeSet attrs) {
48        super(context, attrs);
49        init(attrs, android.R.attr.editTextStyle);
50    }
51
52    public EmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
53        super(context, attrs, defStyleAttr);
54        init(attrs, defStyleAttr);
55    }
56
57    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
58    public EmojiEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
59        super(context, attrs, defStyleAttr, defStyleRes);
60        init(attrs, defStyleAttr);
61    }
62
63    private void init(@Nullable AttributeSet attrs, int defStyleAttr) {
64        if (!mInitialized) {
65            mInitialized = true;
66            final EditTextAttributeHelper attrHelper = new EditTextAttributeHelper(this, attrs,
67                    defStyleAttr);
68            setMaxEmojiCount(attrHelper.getMaxEmojiCount());
69            setKeyListener(super.getKeyListener());
70        }
71    }
72
73    @Override
74    public void setKeyListener(android.text.method.KeyListener keyListener) {
75        super.setKeyListener(getEmojiEditTextHelper().getKeyListener(keyListener));
76    }
77
78    @Override
79    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
80        final InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
81        return getEmojiEditTextHelper().onCreateInputConnection(inputConnection, outAttrs);
82    }
83
84    /**
85     * Set the maximum number of EmojiSpans to be added to a CharSequence. The number of spans in a
86     * CharSequence affects the performance of the EditText insert/delete operations. Insert/delete
87     * operations slow down as the number of spans increases.
88     *
89     * @param maxEmojiCount maximum number of EmojiSpans to be added to a single CharSequence,
90     *                      should be equal or greater than 0
91     *
92     * @see EmojiCompat#process(CharSequence, int, int, int)
93     */
94    public void setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount) {
95        getEmojiEditTextHelper().setMaxEmojiCount(maxEmojiCount);
96    }
97
98    /**
99     * Returns the maximum number of EmojiSpans to be added to a CharSequence.
100     *
101     * @see #setMaxEmojiCount(int)
102     * @see EmojiCompat#process(CharSequence, int, int, int)
103     */
104    public int getMaxEmojiCount() {
105        return getEmojiEditTextHelper().getMaxEmojiCount();
106    }
107
108    private EmojiEditTextHelper getEmojiEditTextHelper() {
109        if (mEmojiEditTextHelper == null) {
110            mEmojiEditTextHelper = new EmojiEditTextHelper(this);
111        }
112        return mEmojiEditTextHelper;
113    }
114}
115