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 com.example.android.support.text.emoji;
18 
19 import android.content.Context;
20 import android.support.annotation.Nullable;
21 import android.support.text.emoji.widget.EmojiTextViewHelper;
22 import android.support.v7.widget.AppCompatTextView;
23 import android.text.InputFilter;
24 import android.util.AttributeSet;
25 
26 
27 /**
28  * A sample implementation of custom TextView.
29  *
30  * <p>You can use {@link EmojiTextViewHelper} to make your custom TextView compatible with
31  * EmojiCompat.</p>
32  */
33 public class CustomTextView extends AppCompatTextView {
34 
35     private EmojiTextViewHelper mEmojiTextViewHelper;
36 
CustomTextView(Context context)37     public CustomTextView(Context context) {
38         this(context, null);
39     }
40 
CustomTextView(Context context, @Nullable AttributeSet attrs)41     public CustomTextView(Context context, @Nullable AttributeSet attrs) {
42         this(context, attrs, 0);
43     }
44 
CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)45     public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
46         super(context, attrs, defStyleAttr);
47         getEmojiTextViewHelper().updateTransformationMethod();
48     }
49 
50     @Override
setFilters(InputFilter[] filters)51     public void setFilters(InputFilter[] filters) {
52         super.setFilters(getEmojiTextViewHelper().getFilters(filters));
53     }
54 
55     @Override
setAllCaps(boolean allCaps)56     public void setAllCaps(boolean allCaps) {
57         super.setAllCaps(allCaps);
58         getEmojiTextViewHelper().setAllCaps(allCaps);
59     }
60 
61     /**
62      * Returns the {@link EmojiTextViewHelper} for this TextView.
63      *
64      * <p>This method can be called from super constructors through {@link
65      * #setFilters(InputFilter[])} or {@link #setAllCaps(boolean)}.</p>
66      */
getEmojiTextViewHelper()67     private EmojiTextViewHelper getEmojiTextViewHelper() {
68         if (mEmojiTextViewHelper == null) {
69             mEmojiTextViewHelper = new EmojiTextViewHelper(this);
70         }
71         return mEmojiTextViewHelper;
72     }
73 
74 }
75