abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/Emoji.java
blob: 98eb600930de1041ab846576d52e0f54f7206923 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package juloo.keyboard2;

import android.content.res.Resources;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Emoji
{
  private final KeyValue _kv;
  private List<Emoji> _skintones;

  protected Emoji(String bytecode)
  {
    this._kv = new KeyValue(bytecode, KeyValue.Kind.String, 0, 0);
    this._skintones = new ArrayList<>();
  }

  public KeyValue kv()
  {
    return _kv;
  }

  public List<Emoji> skintones()
  {
    return _skintones;
  }

  public void addSkintone(Emoji skintone)
  {
    _skintones.add(skintone);
  }


  private final static List<Emoji> _all = new ArrayList<>();
  private final static List<List<Emoji>> _groups = new ArrayList<>();
  private final static HashMap<String, Emoji> _stringMap = new HashMap<>();

  public static void init(Resources res)
  {
    if (!_all.isEmpty())
      return;

    try
    {
      InputStream inputStream = res.openRawResource(R.raw.emojis);
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
      String line;

      // Read emoji (until empty line)
      while (!(line = reader.readLine()).isEmpty())
      {
        Emoji e = new Emoji(line);

        _all.add(e);
        _stringMap.put(line, e);
      }

      // Read group indices
      if ((line = reader.readLine()) != null)
      {
        String[] tokens = line.split(" ");
        int last = 0;
        for (int i = 1; i < tokens.length; i++)
        {
          int next = Integer.parseInt(tokens[i]);
          _groups.add(_all.subList(last, next));
          last = next;
        }
        _groups.add(_all.subList(last, _all.size()));
      }

      inputStream = res.openRawResource(R.raw.emojis_skintone_modifiable);
      reader = new BufferedReader(new InputStreamReader(inputStream));

      // Read skintone modifiable emojis
      while ((line = reader.readLine()) != null)
      {
          int baseIndex = Integer.parseInt(line);
          Emoji baseEmoji = _all.get(baseIndex);
          
          while (!(line = reader.readLine()).isEmpty())
          {
            baseEmoji.addSkintone(new Emoji(line));
          }
      }
    }
    catch (IOException e) { Logs.exn("Emoji.init() failed", e); }
  }

  public static int getNumGroups()
  {
    return _groups.size();
  }

  public static List<Emoji> getEmojisByGroup(int groupIndex)
  {
    return _groups.get(groupIndex);
  }

  public static Emoji getEmojiByString(String value)
  {
    return _stringMap.get(value);
  }
}