EmojiAppCompatEditText.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 */
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
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 EmojiAppCompatEditText(Context context) {
43        super(context);
44        init(null /*attrs*/, 0 /*defStyleAttr*/);
45    }
46
47    public EmojiAppCompatEditText(Context context, AttributeSet attrs) {
48        super(context, attrs);
49        init(attrs, android.support.v7.appcompat.R.attr.editTextStyle);
50    }
51
52    public EmojiAppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) {
53        super(context, attrs, defStyleAttr);
54        init(attrs, defStyleAttr);
55    }
56
57    private void init(@Nullable AttributeSet attrs, int defStyleAttr) {
58        if (!mInitialized) {
59            mInitialized = true;
60            final EditTextAttributeHelper attrHelper = new EditTextAttributeHelper(this, attrs,
61                    defStyleAttr);
62            setMaxEmojiCount(attrHelper.getMaxEmojiCount());
63            setKeyListener(super.getKeyListener());
64        }
65    }
66
67    @Override
68    public void setKeyListener(android.text.method.KeyListener input) {
69        super.setKeyListener(getEmojiEditTextHelper().getKeyListener(input));
70    }
71
72    @Override
73    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
74        InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
75        return getEmojiEditTextHelper().onCreateInputConnection(inputConnection, outAttrs);
76    }
77
78    /**
79     * Set the maximum number of EmojiSpans to be added to a CharSequence. The number of spans in a
80     * CharSequence affects the performance of the EditText insert/delete operations. Insert/delete
81     * operations slow down as the number of spans increases.
82     *
83     * @param maxEmojiCount maximum number of EmojiSpans to be added to a single CharSequence,
84     *                      should be equal or greater than 0
85     *
86     * @see EmojiCompat#process(CharSequence, int, int, int)
87     */
88    public void setMaxEmojiCount(@IntRange(from = 0) int maxEmojiCount) {
89        getEmojiEditTextHelper().setMaxEmojiCount(maxEmojiCount);
90    }
91
92    /**
93     * Returns the maximum number of EmojiSpans to be added to a CharSequence.
94     *
95     * @see #setMaxEmojiCount(int)
96     * @see EmojiCompat#process(CharSequence, int, int, int)
97     */
98    public int getMaxEmojiCount() {
99        return getEmojiEditTextHelper().getMaxEmojiCount();
100    }
101
102    private EmojiEditTextHelper getEmojiEditTextHelper() {
103        if (mEmojiEditTextHelper == null) {
104            mEmojiEditTextHelper = new EmojiEditTextHelper(this);
105        }
106        return mEmojiEditTextHelper;
107    }
108}
109