Skip to content

Instantly share code, notes, and snippets.

@odedhb
Created September 14, 2015 11:36
Show Gist options
  • Save odedhb/1cd45653ba32598f7ad2 to your computer and use it in GitHub Desktop.
Save odedhb/1cd45653ba32598f7ad2 to your computer and use it in GitHub Desktop.
Emoji source - a helper JAVA class that converts emojis from a json file to a string you can use in android. This was created to be implemented in http://wheredatapp.com, android's greatest search engine.
package com.nextstagesearch.sources;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by oded on 9/14/15.
* Emoji source - a helper JAVA class that converts emojis from a json file to a string you can use in android.
* This was created to be implemented in http://wheredatapp.com, android's greatest search engine.
*/
public class EmojiSource {
public final static void iterateEmojis(Context context, Emoji emoji) {
try {
JSONObject jsonEmojis = new JSONObject(jsonEmojis(context));
Iterator<?> keys = jsonEmojis.keys();
while (keys.hasNext()) {
JSONObject jsonEmoji = jsonEmojis.getJSONObject((String) keys.next());
List<String> keywords = new ArrayList<>();
JSONArray jsonArray = jsonEmoji.getJSONArray("keywords");
for (int i = 0; i < jsonArray.length(); i++) {
keywords.add(jsonArray.get(i).toString());
}
String emojiString = getEmoji(jsonEmoji.getString("unicode"));
if (emojiString == null) continue;
emoji.doSomethingWithEmoji(emojiString, jsonEmoji.getString("name"), keywords);
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
private final static String jsonEmojis(Context context) {
String json = null;
try {
//emoji from: https://github.com/Ranks/emojione/blob/master/emoji.json
InputStream is = context.getAssets().open("csv/emoji.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private interface Emoji {
void doSomethingWithEmoji(String emoji, String description, List<String> keywords);
}
private static String getEmoji(String unicode) {
if (unicode.contains("-")) return null;
return new String(Character.toChars(Integer.decode("0x" + unicode)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment