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 
17 package androidx.emoji.widget;
18 
19 import android.content.Context;
20 import android.text.InputFilter;
21 import android.util.AttributeSet;
22 
23 import androidx.appcompat.widget.AppCompatTextView;
24 
25 /**
26  * AppCompatTextView widget enhanced with emoji capability by using {@link EmojiTextViewHelper}.
27  * When used on devices running API 18 or below, this widget acts as a regular
28  * {@link AppCompatTextView}.
29  */
30 public class EmojiAppCompatTextView extends AppCompatTextView {
31     private EmojiTextViewHelper mEmojiTextViewHelper;
32 
33     /**
34      * Prevent calling {@link #init()} multiple times in case super() constructors
35      * call other constructors.
36      */
37     private boolean mInitialized;
38 
EmojiAppCompatTextView(Context context)39     public EmojiAppCompatTextView(Context context) {
40         super(context);
41         init();
42     }
43 
EmojiAppCompatTextView(Context context, AttributeSet attrs)44     public EmojiAppCompatTextView(Context context, AttributeSet attrs) {
45         super(context, attrs);
46         init();
47     }
48 
EmojiAppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr)49     public EmojiAppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) {
50         super(context, attrs, defStyleAttr);
51         init();
52     }
53 
init()54     private void init() {
55         if (!mInitialized) {
56             mInitialized = true;
57             getEmojiTextViewHelper().updateTransformationMethod();
58         }
59     }
60 
61     @Override
setFilters(InputFilter[] filters)62     public void setFilters(InputFilter[] filters) {
63         super.setFilters(getEmojiTextViewHelper().getFilters(filters));
64     }
65 
66     @Override
setAllCaps(boolean allCaps)67     public void setAllCaps(boolean allCaps) {
68         super.setAllCaps(allCaps);
69         getEmojiTextViewHelper().setAllCaps(allCaps);
70     }
71 
getEmojiTextViewHelper()72     private EmojiTextViewHelper getEmojiTextViewHelper() {
73         if (mEmojiTextViewHelper == null) {
74             mEmojiTextViewHelper = new EmojiTextViewHelper(this);
75         }
76         return mEmojiTextViewHelper;
77     }
78 }
79