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.RequiresApi;
24import android.support.annotation.RestrictTo;
25import android.support.text.emoji.EmojiCompat;
26import android.text.method.TransformationMethod;
27import android.view.View;
28
29/**
30 * TransformationMethod wrapper in order to update transformed text with emojis.
31 *
32 * @hide
33 */
34@RestrictTo(LIBRARY_GROUP)
35@RequiresApi(19)
36class EmojiTransformationMethod implements TransformationMethod {
37    private final TransformationMethod mTransformationMethod;
38
39    EmojiTransformationMethod(TransformationMethod transformationMethod) {
40        mTransformationMethod = transformationMethod;
41    }
42
43    @Override
44    public CharSequence getTransformation(@Nullable CharSequence source, @NonNull final View view) {
45        if (view.isInEditMode()) {
46            return source;
47        }
48
49        if (mTransformationMethod != null) {
50            source = mTransformationMethod.getTransformation(source, view);
51        }
52
53        if (source != null) {
54            switch (EmojiCompat.get().getLoadState()){
55                case EmojiCompat.LOAD_STATE_SUCCEEDED:
56                    return EmojiCompat.get().process(source);
57                case EmojiCompat.LOAD_STATE_LOADING:
58                case EmojiCompat.LOAD_STATE_FAILED:
59                default:
60                    break;
61            }
62        }
63        return source;
64    }
65
66    @Override
67    public void onFocusChanged(final View view, final CharSequence sourceText,
68            final boolean focused, final int direction, final Rect previouslyFocusedRect) {
69        if (mTransformationMethod != null) {
70            mTransformationMethod.onFocusChanged(view, sourceText, focused, direction,
71                    previouslyFocusedRect);
72        }
73    }
74}
75