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 package android.support.text.emoji.widget;
17 
18 import android.content.Context;
19 import android.os.Build;
20 import android.support.annotation.RequiresApi;
21 import android.text.InputFilter;
22 import android.util.AttributeSet;
23 import android.widget.TextView;
24 
25 /**
26  * TextView widget enhanced with emoji capability by using {@link EmojiTextViewHelper}. When used
27  * on devices running API 18 or below, this widget acts as a regular {@link TextView}.
28  */
29 public class EmojiTextView extends TextView {
30     private EmojiTextViewHelper mEmojiTextViewHelper;
31     private boolean mInitialized;
32 
EmojiTextView(Context context)33     public EmojiTextView(Context context) {
34         super(context);
35         init();
36     }
37 
EmojiTextView(Context context, AttributeSet attrs)38     public EmojiTextView(Context context, AttributeSet attrs) {
39         super(context, attrs);
40         init();
41     }
42 
EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr)43     public EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr) {
44         super(context, attrs, defStyleAttr);
45         init();
46     }
47 
48     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)49     public EmojiTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
50         super(context, attrs, defStyleAttr, defStyleRes);
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