EmojiTransformationMethod.java revision f8ec169d022fbed42fd82091d24c45f3767cdfe7
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 android.support.text.emoji.widget;
17
18import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
19
20import android.graphics.Rect;
21import android.support.annotation.NonNull;
22import android.support.annotation.Nullable;
23import android.support.annotation.RestrictTo;
24import android.support.text.emoji.EmojiCompat;
25import android.text.method.TransformationMethod;
26import android.view.View;
27
28/**
29 * TransformationMethod wrapper in order to update transformed text with emojis.
30 *
31 * @hide
32 */
33@RestrictTo(LIBRARY_GROUP)
34class EmojiTransformationMethod implements TransformationMethod {
35    private final TransformationMethod mTransformationMethod;
36
37    EmojiTransformationMethod(TransformationMethod transformationMethod) {
38        mTransformationMethod = transformationMethod;
39    }
40
41    @Override
42    public CharSequence getTransformation(@Nullable CharSequence source, @NonNull final View view) {
43        if (view.isInEditMode()) {
44            return source;
45        }
46
47        if (mTransformationMethod != null) {
48            source = mTransformationMethod.getTransformation(source, view);
49        }
50
51        if (source != null) {
52            if (EmojiCompat.get().isInitialized()) {
53                return EmojiCompat.get().process(source);
54            }
55        }
56        return source;
57    }
58
59    @Override
60    public void onFocusChanged(final View view, final CharSequence sourceText,
61            final boolean focused, final int direction, final Rect previouslyFocusedRect) {
62        if (mTransformationMethod != null) {
63            mTransformationMethod.onFocusChanged(view, sourceText, focused, direction,
64                    previouslyFocusedRect);
65        }
66    }
67}
68