Strip down emoji manager; replace data source

This commit is contained in:
Philipp Heckel 2021-11-28 21:47:00 -05:00
parent 415046e098
commit 4444c9de9c
8 changed files with 22797 additions and 16937 deletions

View file

@ -38,3 +38,5 @@ Thank you to these fantastic resources:
* [Firebase Messaging Example](https://github.com/firebase/quickstart-android/blob/7147f60451b3eeaaa05fc31208ffb67e2df73c3c/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt) (Apache 2.0) * [Firebase Messaging Example](https://github.com/firebase/quickstart-android/blob/7147f60451b3eeaaa05fc31208ffb67e2df73c3c/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt) (Apache 2.0)
* [Designing a logo with Inkscape](https://www.youtube.com/watch?v=r2Kv61cd2P4) * [Designing a logo with Inkscape](https://www.youtube.com/watch?v=r2Kv61cd2P4)
* [Foreground service](https://robertohuertas.com/2019/06/29/android_foreground_services/) * [Foreground service](https://robertohuertas.com/2019/06/29/android_foreground_services/)
* [github/gemoji](https://github.com/github/gemoji) (MIT) for as data source for an up-to-date [emoji.json](https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json) file
* [emoji-java](https://github.com/vdurmont/emoji-java) (MIT) has been stripped and inlined to use the emoji.json file

View file

@ -1,87 +0,0 @@
package io.heckel.ntfy.emoji;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;
/**
* This class represents an emoji.<br>
* <br>
* This object is immutable so it can be used safely in a multithreaded context.
*
* @author Vincent DURMONT [vdurmont@gmail.com]
*/
public class Emoji {
private final List<String> aliases;
private final String unicode;
/**
* Constructor for the Emoji.
*
* @param aliases the aliases for this emoji
* @param bytes the bytes that represent the emoji
*/
protected Emoji(
List<String> aliases,
byte... bytes
) {
this.aliases = Collections.unmodifiableList(aliases);
int count = 0;
try {
this.unicode = new String(bytes, "UTF-8");
int stringLength = getUnicode().length();
String[] pointCodes = new String[stringLength];
String[] pointCodesHex = new String[stringLength];
for (int offset = 0; offset < stringLength; ) {
final int codePoint = getUnicode().codePointAt(offset);
pointCodes[count] = String.format("&#%d;", codePoint);
pointCodesHex[count++] = String.format("&#x%x;", codePoint);
offset += Character.charCount(codePoint);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the aliases of the emoji
*
* @return the aliases (unmodifiable)
*/
public List<String> getAliases() {
return this.aliases;
}
/**
* Returns the unicode representation of the emoji
*
* @return the unicode representation
*/
public String getUnicode() {
return this.unicode;
}
@Override
public boolean equals(Object other) {
return !(other == null || !(other instanceof Emoji)) &&
((Emoji) other).getUnicode().equals(getUnicode());
}
@Override
public int hashCode() {
return unicode.hashCode();
}
/**
* Returns the String representation of the Emoji object.
* @return the string representation
*/
@Override
public String toString() {
return "Emoji{aliases=" + aliases + ", unicode='" + unicode + "'}";
}
}

View file

@ -0,0 +1,33 @@
package io.heckel.ntfy.util;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;
/**
* This class represents an emoji.
*
* This class was originally written by Vincent DURMONT (vdurmont@gmail.com) as part of
* https://github.com/vdurmont/emoji-java, but has since been heavily stripped and modified.
*/
public class Emoji {
private final List<String> aliases;
private final String unicode;
protected Emoji(List<String> aliases, byte... bytes) {
this.aliases = Collections.unmodifiableList(aliases);
try {
this.unicode = new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public List<String> getAliases() {
return this.aliases;
}
public String getUnicode() {
return this.unicode;
}
}

View file

@ -1,4 +1,4 @@
package io.heckel.ntfy.emoji; package io.heckel.ntfy.util;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -11,24 +11,16 @@ import java.util.List;
/** /**
* Loads the emojis from a JSON database. * Loads the emojis from a JSON database.
* *
* @author Vincent DURMONT [vdurmont@gmail.com] * This was originally written to load
* https://github.com/vdurmont/emoji-java/blob/master/src/main/resources/emojis.json
*
* But now uses
* https://github.com/github/gemoji/blob/master/db/emoji.json
*
* This class was originally written by Vincent DURMONT (vdurmont@gmail.com) as part of
* https://github.com/vdurmont/emoji-java, but has since been heavily stripped and modified.
*/ */
public class EmojiLoader { public class EmojiLoader {
/**
* No need for a constructor, all the methods are static.
*/
private EmojiLoader() {}
/**
* Loads a JSONArray of emojis from an InputStream, parses it and returns the
* associated list of {@link Emoji}s
*
* @param stream the stream of the JSONArray
*
* @return the list of {@link Emoji}s
* @throws IOException if an error occurs while reading the stream or parsing
* the JSONArray
*/
public static List<Emoji> loadEmojis(InputStream stream) throws IOException, JSONException { public static List<Emoji> loadEmojis(InputStream stream) throws IOException, JSONException {
JSONArray emojisJSON = new JSONArray(inputStreamToString(stream)); JSONArray emojisJSON = new JSONArray(inputStreamToString(stream));
List<Emoji> emojis = new ArrayList<Emoji>(emojisJSON.length()); List<Emoji> emojis = new ArrayList<Emoji>(emojisJSON.length());

View file

@ -1,6 +1,5 @@
package io.heckel.ntfy.emoji; package io.heckel.ntfy.util;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -9,12 +8,12 @@ import java.util.Map;
/** /**
* Holds the loaded emojis and provides search functions. * Holds the loaded emojis and provides search functions.
* *
* @author Vincent DURMONT [vdurmont@gmail.com] * This class was originally written by Vincent DURMONT (vdurmont@gmail.com) as part of
* https://github.com/vdurmont/emoji-java, but has since been heavily stripped and modified.
*/ */
public class EmojiManager { public class EmojiManager {
private static final String PATH = "/emojis.json"; private static final String PATH = "/emoji.json"; // https://github.com/github/gemoji/blob/master/db/emoji.json
private static final Map<String, Emoji> EMOJIS_BY_ALIAS = private static final Map<String, Emoji> EMOJIS_BY_ALIAS = new HashMap<String, Emoji>();
new HashMap<String, Emoji>();
static { static {
try { try {
@ -31,19 +30,6 @@ public class EmojiManager {
} }
} }
/**
* No need for a constructor, all the methods are static.
*/
private EmojiManager() {}
/**
* Returns the {@link Emoji} for a given alias.
*
* @param alias the alias
*
* @return the associated {@link Emoji}, null if the alias
* is unknown
*/
public static Emoji getForAlias(String alias) { public static Emoji getForAlias(String alias) {
if (alias == null || alias.isEmpty()) { if (alias == null || alias.isEmpty()) {
return null; return null;
@ -57,5 +43,4 @@ public class EmojiManager {
alias.charAt(0) == ':' ? 1 : 0, alias.charAt(0) == ':' ? 1 : 0,
alias.charAt(len - 1) == ':' ? len - 1 : len); alias.charAt(len - 1) == ':' ? len - 1 : len);
} }
} }

View file

@ -5,7 +5,6 @@ import android.animation.ValueAnimator
import android.view.Window import android.view.Window
import io.heckel.ntfy.data.Notification import io.heckel.ntfy.data.Notification
import io.heckel.ntfy.data.Subscription import io.heckel.ntfy.data.Subscription
import io.heckel.ntfy.emoji.EmojiManager
import java.text.DateFormat import java.text.DateFormat
import java.util.* import java.util.*
@ -44,12 +43,7 @@ fun toEmojis(tags: List<String>): List<String> {
} }
fun toEmoji(tag: String): String? { fun toEmoji(tag: String): String? {
return when (tag.toLowerCase()) { return EmojiManager.getForAlias(tag)?.unicode
"warn", "warning" -> "\u26A0\uFE0F"
"success" -> "\u2714\uFE0F"
"failure" -> "\u274C"
else -> EmojiManager.getForAlias(tag)?.unicode
}
} }
fun unmatchedTags(tags: List<String>): List<String> { fun unmatchedTags(tags: List<String>): List<String> {

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff