Home | History | Annotate | Download | only in emoji
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.support.text.emoji;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 
     22 import java.io.BufferedReader;
     23 import java.io.IOException;
     24 import java.io.InputStream;
     25 import java.io.InputStreamReader;
     26 import java.util.ArrayList;
     27 import java.util.List;
     28 
     29 class EmojiRepo {
     30     private static final String TAG = "EmojiData";
     31     private static List<EmojiData> sEmojis = new ArrayList<>();
     32 
     33     private EmojiRepo() {
     34     }
     35 
     36     static List<EmojiData> getEmojis() {
     37         return sEmojis;
     38     }
     39 
     40     static synchronized void load(final Context context) {
     41         new Thread(new Runnable() {
     42             @Override
     43             public void run() {
     44                 try {
     45                     read(context);
     46                 } catch (Throwable t) {
     47                     Log.e(TAG, "Cannot load emojis", t);
     48                 }
     49             }
     50         }).run();
     51     }
     52 
     53     private static void read(Context context) throws IOException {
     54         final InputStream inputStream = context.getAssets().open("emojis.txt");
     55         try {
     56             final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
     57             final StringBuilder stringBuilder = new StringBuilder();
     58             final StringBuilder codepointBuilder = new StringBuilder();
     59             List<Integer> codepointsList;
     60 
     61             String s;
     62             while ((s = reader.readLine()) != null) {
     63                 s = s.trim();
     64                 // skip comments
     65                 if (s.isEmpty() || s.startsWith("#")) continue;
     66 
     67                 stringBuilder.setLength(0);
     68                 codepointBuilder.setLength(0);
     69                 codepointsList = new ArrayList<>();
     70 
     71                 // emoji codepoints are space separated: i.e. 0x1f1e6 0x1f1e8
     72                 final String[] split = s.split(" ");
     73 
     74                 for (int index = 0; index < split.length; index++) {
     75                     final String part = split[index].trim();
     76                     int codepoint = Integer.parseInt(part, 16);
     77                     codepointsList.add(codepoint);
     78                     codepointBuilder.append(String.format("u+%04x", codepoint));
     79                     codepointBuilder.append(" ");
     80                     stringBuilder.append(Character.toChars(codepoint));
     81                 }
     82                 final EmojiData emojiData = new EmojiData(stringBuilder.toString(), codepointsList,
     83                         codepointBuilder.toString());
     84                 sEmojis.add(emojiData);
     85             }
     86         } finally {
     87             inputStream.close();
     88         }
     89     }
     90 
     91     static class EmojiData {
     92         private String mEmoji;
     93         private List<Integer> mCodepoints;
     94         private String mCodepointString;
     95 
     96         EmojiData(String emoji, List<Integer> codepoints, String codepointString) {
     97             mEmoji = emoji;
     98             mCodepoints = codepoints;
     99             mCodepointString = codepointString;
    100         }
    101 
    102         public String getEmoji() {
    103             return mEmoji;
    104         }
    105 
    106         public List<Integer> getCodepoints() {
    107             return mCodepoints;
    108         }
    109 
    110         public String getCodepointString() {
    111             return mCodepointString;
    112         }
    113     }
    114 }
    115