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