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 */
16package androidx.emoji.widget;
17
18import android.content.Context;
19import android.os.Build;
20import android.text.InputFilter;
21import android.util.AttributeSet;
22import android.widget.Button;
23
24import androidx.annotation.RequiresApi;
25
26/**
27 * Button widget enhanced with emoji capability by using {@link EmojiTextViewHelper}. When used
28 * on devices running API 18 or below, this widget acts as a regular {@link Button}.
29 */
30public class EmojiButton extends Button {
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
39    public EmojiButton(Context context) {
40        super(context);
41        init();
42    }
43
44    public EmojiButton(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        init();
47    }
48
49    public EmojiButton(Context context, AttributeSet attrs, int defStyleAttr) {
50        super(context, attrs, defStyleAttr);
51        init();
52    }
53
54    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
55    public EmojiButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
56        super(context, attrs, defStyleAttr, defStyleRes);
57        init();
58    }
59
60    private void init() {
61        if (!mInitialized) {
62            mInitialized = true;
63            getEmojiTextViewHelper().updateTransformationMethod();
64        }
65    }
66
67    @Override
68    public void setFilters(InputFilter[] filters) {
69        super.setFilters(getEmojiTextViewHelper().getFilters(filters));
70    }
71
72    @Override
73    public void setAllCaps(boolean allCaps) {
74        super.setAllCaps(allCaps);
75        getEmojiTextViewHelper().setAllCaps(allCaps);
76    }
77
78    private EmojiTextViewHelper getEmojiTextViewHelper() {
79        if (mEmojiTextViewHelper == null) {
80            mEmojiTextViewHelper = new EmojiTextViewHelper(this);
81        }
82        return mEmojiTextViewHelper;
83    }
84}
85