aboutsummaryrefslogtreecommitdiffstats
path: root/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconGroupsLoader.java
blob: 350e710d2a72c8bd3ab27b760d076893b7de7515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package github.ankushsachdeva.emojicon;

import android.content.Context;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by aleksandr.naumov on 14.05.2015.
 */
class EmojiconGroupsLoader {
    private static EmojiconGroupsLoader sInstance;
    private final List<EmojiconGroup> mGroups;

    public static synchronized EmojiconGroupsLoader getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new EmojiconGroupsLoader(context);
        }
        return sInstance;
    }

    private EmojiconGroupsLoader(Context context) {
        try {
            mGroups = parseEmojiXml(context);
        } catch (IOException | XmlPullParserException ex) {
            throw new RuntimeException(ex);
        }
    }

    private List<EmojiconGroup> parseEmojiXml(Context context) throws IOException, XmlPullParserException {
        ArrayList<EmojiconGroup> groups = new ArrayList<>();
        XmlPullParser parser = context.getResources().getXml(R.xml.emoji);
        parser.next();
        String groupName = null;
        for (int eventType = parser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = parser.next()) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if ("group".equals(parser.getName())) {
                        groupName = parser.getAttributeValue(0);
                    }
                    break;
                case XmlPullParser.TEXT:
                    if (groupName != null) {
                        groups.add(EmojiconGroup.fromString(
                                parser.getText(),
                                getGroupNameIcon(groupName)
                        ));
                        groupName = null;
                    }
                    break;
            }
        }
        return groups;
    }

    private int getGroupNameIcon(String groupName) {
        for (KnownGroupNames knownGroupName : KnownGroupNames.values()) {
            if (groupName.toLowerCase().equals(knownGroupName.name().toLowerCase())) {
                return knownGroupName.getIconResId();
            }
        }
        throw new IllegalArgumentException(String.format("Unknown group name '%s'", groupName));
    }

    public List<EmojiconGroup> getGroups() {
        return mGroups;
    }

    public enum KnownGroupNames {
        RECENT(R.drawable.ic_emoji_recent_light),
        SMILES(R.drawable.ic_emoji_people_light),
        NATURE(R.drawable.ic_emoji_nature_light),
        OBJECTS(R.drawable.ic_emoji_objects_light),
        TECH(R.drawable.ic_emoji_places_light),
        SYMBOLS(R.drawable.ic_emoji_symbols_light);

        private final int iconResId;

        KnownGroupNames(int iconResId) {
            this.iconResId = iconResId;
        }

        public int getIconResId() {
            return iconResId;
        }
    }
}