Home | History | Annotate | Download | only in widget
      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 package android.support.text.emoji.widget;
     17 
     18 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
     19 
     20 import android.graphics.Rect;
     21 import android.support.annotation.NonNull;
     22 import android.support.annotation.Nullable;
     23 import android.support.annotation.RequiresApi;
     24 import android.support.annotation.RestrictTo;
     25 import android.support.text.emoji.EmojiCompat;
     26 import android.text.method.TransformationMethod;
     27 import 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)
     36 class 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             }
     58         }
     59         return source;
     60     }
     61 
     62     @Override
     63     public void onFocusChanged(final View view, final CharSequence sourceText,
     64             final boolean focused, final int direction, final Rect previouslyFocusedRect) {
     65         if (mTransformationMethod != null) {
     66             mTransformationMethod.onFocusChanged(view, sourceText, focused, direction,
     67                     previouslyFocusedRect);
     68         }
     69     }
     70 }
     71