2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2018-2024 Carsten Pfeiffer, Matthieu Baerts, Taavi Eomรคe
|
2018-11-25 23:56:52 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2018-11-25 23:56:52 +01:00
|
|
|
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
2018-12-23 22:58:43 +01:00
|
|
|
import android.content.Context;
|
|
|
|
|
2019-02-13 03:26:04 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2018-12-23 22:58:43 +01:00
|
|
|
import io.wax911.emojify.EmojiManager;
|
2018-11-25 23:56:52 +01:00
|
|
|
import io.wax911.emojify.EmojiUtils;
|
|
|
|
|
|
|
|
public class EmojiConverter {
|
2019-02-13 03:26:04 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(EmojiConverter.class);
|
2018-12-23 23:00:48 +01:00
|
|
|
|
2018-11-27 00:19:05 +01:00
|
|
|
private static final String[][] simpleEmojiMapping = {
|
|
|
|
{"\uD83D\uDE00", ":-D"}, // grinning
|
|
|
|
{"\uD83D\uDE01", ":-D"}, // grinning_face_with_smiling_eyes
|
|
|
|
{"\uD83D\uDE02", ":'D"}, // face_with_tears_of_joy
|
|
|
|
{"\uD83D\uDE03", ":-D"}, // smiling_face_with_open_mouth
|
|
|
|
{"\uD83D\uDE04", ":-D"}, // smiling_face_with_open_mouth_and_smiling_eyes
|
|
|
|
{"\uD83D\uDE05", ":'D"}, // smiling_face_with_open_mouth_and_cold_sweat
|
|
|
|
{"\uD83D\uDE06", "X-D"}, // smiling_face_with_open_mouth_and_tightly-closed_eyes
|
|
|
|
{"\uD83D\uDE07", "O:-)"}, // innocent
|
|
|
|
{"\uD83D\uDE09", ";-)"}, // wink
|
|
|
|
{"\uD83D\uDE0A", ":-)"}, // blush
|
|
|
|
{"\uD83D\uDE0B", ":-p"}, // yum
|
|
|
|
{"\uD83D\uDE0E", "B-)"}, // sunglasses
|
|
|
|
{"\uD83D\uDE15", ":-/"}, // confused
|
|
|
|
{"\uD83D\uDE16", ":-S"}, // confounded_face
|
|
|
|
{"\uD83D\uDE17", ":*"}, // kissing_face
|
2018-12-23 23:00:48 +01:00
|
|
|
{"\uD83D\uDE18", ";-*"}, // face_throwing_a_kiss
|
|
|
|
{"\uD83D\uDE19", ":-*"}, // kissing_face_with_smiling_eyes
|
2018-11-27 00:19:05 +01:00
|
|
|
{"\uD83D\uDE1A", ":-*"}, // kissing_closed_eyes
|
|
|
|
{"\uD83D\uDE1B", ":-P"}, // stuck_out_tongue
|
|
|
|
{"\uD83D\uDE1C", ";-P"}, // stuck_out_tongue_winking_eye
|
|
|
|
{"\uD83D\uDE1D", "X-P"}, // stuck_out_tongue_and_tightly-closed_eyes
|
|
|
|
{"\uD83D\uDE1E", ":-S"}, // disappointed
|
|
|
|
{"\uD83D\uDE20", ":-@"}, // angry_face
|
|
|
|
{"\uD83D\uDE21", ":-@"}, // pouting_face
|
|
|
|
{"\uD83D\uDE22", ":'("}, // cry
|
|
|
|
{"\uD83D\uDE23", ":-("}, // preserving_face
|
|
|
|
{"\uD83D\uDE25", ":'("}, // disappointed_but_relieved_face
|
|
|
|
{"\uD83D\uDE2D", ":'("}, // loudly_crying_face
|
|
|
|
{"\uD83D\uDE2E", ":-O"}, // open_mouth
|
|
|
|
{"\uD83D\uDE32", "X-o"}, // astonished_face
|
|
|
|
{"\uD83D\uDE42", ":)"}, // slightly_smiling_face
|
|
|
|
{"\uD83D\uDE43", "(-:"}, // upside_down_face
|
|
|
|
{"\u2639", ":-("}, // frowning_face
|
|
|
|
{"\u2764", "<3"}, // heart
|
|
|
|
};
|
|
|
|
|
2018-12-24 09:21:50 +01:00
|
|
|
private static boolean isEmojiDataInitialised = false;
|
2018-12-23 22:58:43 +01:00
|
|
|
|
2018-11-27 00:19:05 +01:00
|
|
|
private static String convertSimpleEmojiToAscii(String text) {
|
|
|
|
for (String[] emojiMap : simpleEmojiMapping) {
|
|
|
|
text = text.replace(emojiMap[0], emojiMap[1]);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2018-12-24 09:21:50 +01:00
|
|
|
private static synchronized void initEmojiData(Context context) {
|
2018-12-23 22:58:43 +01:00
|
|
|
// Do a lazy initialisation not to slowdown the startup and when it is needed
|
2018-12-24 09:21:50 +01:00
|
|
|
if (!isEmojiDataInitialised) {
|
2018-12-23 22:58:43 +01:00
|
|
|
EmojiManager.initEmojiData(context);
|
2018-12-24 09:21:50 +01:00
|
|
|
isEmojiDataInitialised = true;
|
2018-12-23 22:58:43 +01:00
|
|
|
}
|
2018-12-24 09:21:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String convertAdvancedEmojiToAscii(String text, Context context) {
|
|
|
|
initEmojiData(context);
|
2019-02-13 03:26:04 +01:00
|
|
|
try {
|
|
|
|
return EmojiUtils.shortCodify(text);
|
|
|
|
} catch (Exception e){
|
2019-02-28 21:57:45 +01:00
|
|
|
LOG.warn("An exception occured when converting advanced emoji to ASCII: " + text);
|
2019-02-13 03:26:04 +01:00
|
|
|
return text;
|
|
|
|
}
|
2018-12-23 22:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String convertUnicodeEmojiToAscii(String text, Context context) {
|
2018-11-27 00:19:05 +01:00
|
|
|
text = convertSimpleEmojiToAscii(text);
|
|
|
|
|
2018-12-23 22:58:43 +01:00
|
|
|
text = convertAdvancedEmojiToAscii(text, context);
|
|
|
|
|
|
|
|
return text;
|
2018-11-25 23:56:52 +01:00
|
|
|
}
|
|
|
|
}
|