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