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
17package com.example.android.support.text.emoji;
18
19import android.os.Bundle;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25import androidx.emoji.text.EmojiCompat;
26import androidx.fragment.app.Fragment;
27
28/**
29 * Main fragment.
30 */
31public class MainFragment extends Fragment {
32
33    // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F4BB] (PERSONAL COMPUTER)
34    private static final String WOMAN_TECHNOLOGIST = "\uD83D\uDC69\u200D\uD83D\uDCBB";
35
36    // [U+1F469] (WOMAN) + [U+200D] (ZERO WIDTH JOINER) + [U+1F3A4] (MICROPHONE)
37    private static final String WOMAN_SINGER = "\uD83D\uDC69\u200D\uD83C\uDFA4";
38
39    static final String EMOJI = WOMAN_TECHNOLOGIST + " " + WOMAN_SINGER;
40
41    private TextView mEmojiTextView;
42    private TextView mEmojiEditText;
43    private TextView mEmojiButton;
44    private TextView mRegularTextView;
45    private TextView mCustomTextView;
46
47    final Config.Listener mConfigListener = new Config.Listener() {
48        @Override
49        public void onEmojiCompatUpdated() {
50            init();
51        }
52    };
53
54    static MainFragment newInstance() {
55        MainFragment fragment = new MainFragment();
56        return fragment;
57    }
58
59    @Override
60    public View onCreateView(LayoutInflater inflater, ViewGroup container,
61            Bundle savedInstanceState) {
62        final View view = inflater.inflate(R.layout.fragment_main, container, false);
63
64        // TextView variant provided by EmojiCompat library
65        mEmojiTextView = view.findViewById(R.id.emoji_text_view);
66        // EditText variant provided by EmojiCompat library
67        mEmojiEditText = view.findViewById(R.id.emoji_edit_text);
68        // Button variant provided by EmojiCompat library
69        mEmojiButton = view.findViewById(R.id.emoji_button);
70        // Regular TextView without EmojiCompat support; you have to manually process the text
71        mRegularTextView = view.findViewById(R.id.regular_text_view);
72        // Custom TextView
73        mCustomTextView = view.findViewById(R.id.emoji_custom_text_view);
74
75        final TextView emojiListButton = view.findViewById(R.id.emoji_list_button);
76        emojiListButton.setOnClickListener(new View.OnClickListener() {
77            @Override
78            public void onClick(View v) {
79                ((MainActivity) getActivity()).showAllEmojis();
80            }
81        });
82
83        init();
84        return view;
85    }
86
87    @Override
88    public void onStart() {
89        super.onStart();
90        Config.get().registerListener(mConfigListener);
91    }
92
93    @Override
94    public void onStop() {
95        Config.get().unregisterListener(mConfigListener);
96        super.onStop();
97    }
98
99    private void init() {
100        mEmojiTextView.setText(getString(R.string.emoji_text_view, EMOJI));
101        mEmojiEditText.setText(getString(R.string.emoji_edit_text, EMOJI));
102        mEmojiButton.setText(getString(R.string.emoji_button, EMOJI));
103        mRegularTextView.setText(getString(R.string.regular_text_view, EMOJI));
104        EmojiCompat.get().registerInitCallback(new EmojiCompat.InitCallback() {
105            @Override
106            public void onInitialized() {
107                final EmojiCompat compat = EmojiCompat.get();
108                mRegularTextView.setText(
109                        compat.process(getString(R.string.regular_text_view, EMOJI)));
110            }
111        });
112        mCustomTextView.setText(getString(R.string.custom_text_view, EMOJI));
113    }
114}
115