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