Home | History | Annotate | Download | only in emoji
      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 
     17 package com.example.android.support.text.emoji;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.util.Log;
     22 
     23 import androidx.annotation.NonNull;
     24 import androidx.annotation.Nullable;
     25 import androidx.core.provider.FontRequest;
     26 import androidx.emoji.bundled.BundledEmojiCompatConfig;
     27 import androidx.emoji.text.EmojiCompat;
     28 import androidx.emoji.text.FontRequestEmojiCompatConfig;
     29 
     30 import java.util.HashSet;
     31 import java.util.Set;
     32 
     33 class Config {
     34     private static final String TAG = "EmojiDemo";
     35 
     36     public static final String PREF_NAME = "emojicompat";
     37     public static final String KEY_ENABLED = "enabled";
     38     public static final String KEY_REPLACE_ALL = "replaceAll";
     39     public static final String KEY_DOWNLOADABLE = "downloadable";
     40     public static final String KEY_INDICATOR = "indicator";
     41     private static Config sInstance;
     42 
     43     private SharedPreferences mSharedPref;
     44     private Context mContext;
     45     private boolean mCompatEnabled;
     46     private boolean mReplaceAll;
     47     private boolean mDownloadable;
     48     private boolean mIndicator;
     49 
     50     private Set<Listener> mListeners = new HashSet<>();
     51 
     52     private Config() {
     53     }
     54 
     55     void init(Context context) {
     56         this.mContext = context;
     57         mSharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
     58         mCompatEnabled = mSharedPref.getBoolean(KEY_ENABLED, false);
     59         mReplaceAll = mSharedPref.getBoolean(KEY_REPLACE_ALL, false);
     60         mDownloadable = mSharedPref.getBoolean(KEY_DOWNLOADABLE, false);
     61         mIndicator = mSharedPref.getBoolean(KEY_INDICATOR, false);
     62         resetEmojiCompat();
     63     }
     64 
     65     static synchronized Config get() {
     66         if (sInstance == null) {
     67             sInstance = new Config();
     68         }
     69         return sInstance;
     70     }
     71 
     72     void registerListener(Listener listener) {
     73         mListeners.add(listener);
     74     }
     75 
     76     void unregisterListener(Listener listener) {
     77         mListeners.remove(listener);
     78     }
     79 
     80     void update(boolean compatEnabled, boolean replaceAll, boolean downloadable,
     81             boolean indicator) {
     82         mCompatEnabled = compatEnabled;
     83         mReplaceAll = replaceAll;
     84         mDownloadable = downloadable;
     85         mIndicator = indicator;
     86         mSharedPref.edit().putBoolean(KEY_ENABLED, mCompatEnabled).apply();
     87         mSharedPref.edit().putBoolean(KEY_REPLACE_ALL, mReplaceAll).apply();
     88         mSharedPref.edit().putBoolean(KEY_DOWNLOADABLE, mDownloadable).apply();
     89         mSharedPref.edit().putBoolean(KEY_INDICATOR, mIndicator).apply();
     90         resetEmojiCompat();
     91         for (Listener listener : mListeners) {
     92             listener.onEmojiCompatUpdated();
     93         }
     94     }
     95 
     96     private void resetEmojiCompat() {
     97         final EmojiCompat.Config config;
     98         if (mCompatEnabled) {
     99             if (mDownloadable) {
    100                 final FontRequest fontRequest = new FontRequest(
    101                         mContext.getString(R.string.provider_authority),
    102                         mContext.getString(R.string.provider_package),
    103                         mContext.getString(R.string.font_query),
    104                         R.array.com_google_android_gms_fonts_certs);
    105 
    106                 config = new FontRequestEmojiCompatConfig(mContext, fontRequest);
    107             } else {
    108                 config = new BundledEmojiCompatConfig(mContext);
    109             }
    110         } else {
    111             config = new EmojiCompat.Config(new EmojiCompat.MetadataRepoLoader() {
    112                 @Override
    113                 public void load(@NonNull EmojiCompat.MetadataRepoLoaderCallback loaderCallback) {
    114                     loaderCallback.onFailed(new RuntimeException("Disable"));
    115                 }
    116             }) {
    117             };
    118         }
    119 
    120         config.setReplaceAll(mReplaceAll)
    121                 .setEmojiSpanIndicatorEnabled(mIndicator)
    122                 .registerInitCallback(new EmojiCompat.InitCallback() {
    123                     @Override
    124                     public void onInitialized() {
    125                         Log.i(TAG, "EmojiCompat initialized");
    126                     }
    127 
    128                     @Override
    129                     public void onFailed(@Nullable Throwable throwable) {
    130                         Log.e(TAG, "EmojiCompat initialization failed", throwable);
    131                     }
    132                 });
    133 
    134         EmojiCompat.reset(config);
    135     }
    136 
    137     boolean isCompatEnabled() {
    138         return mCompatEnabled;
    139     }
    140 
    141     boolean isReplaceAll() {
    142         return mCompatEnabled && mReplaceAll;
    143     }
    144 
    145     boolean isDownloadable() {
    146         return mCompatEnabled && mDownloadable;
    147     }
    148 
    149     boolean isIndicator() {
    150         return mCompatEnabled && mIndicator;
    151     }
    152 
    153     interface Listener {
    154         void onEmojiCompatUpdated();
    155     }
    156 }
    157