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