From 58a69f2da6c4c009ec9fc24369b4c4783ae229f5 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 23 Mar 2021 00:00:29 +0900 Subject: [PATCH] Read emojis json with context --- .../instagrabber/activities/MainActivity.java | 2 +- .../instagrabber/utils/emoji/EmojiParser.java | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index df2369cc..8328d3c3 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -171,7 +171,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage getSupportFragmentManager().addOnBackStackChangedListener(this); // Initialise the internal map AppExecutors.getInstance().tasksThread().execute(() -> { - EmojiParser.getInstance(); + EmojiParser.setup(this); EmojiVariantManager.getInstance(); }); initEmojiCompat(); diff --git a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java index 17c8327c..ac584c34 100644 --- a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java +++ b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java @@ -1,7 +1,10 @@ package awais.instagrabber.utils.emoji; +import android.content.Context; import android.util.Log; +import androidx.annotation.NonNull; + import com.google.common.collect.ImmutableList; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; @@ -18,6 +21,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; +import awais.instagrabber.R; import awais.instagrabber.customviews.emoji.Emoji; import awais.instagrabber.customviews.emoji.EmojiCategory; import awais.instagrabber.customviews.emoji.EmojiCategoryType; @@ -33,25 +37,25 @@ public final class EmojiParser { private Map categoryMap = Collections.emptyMap(); private ImmutableList categories; - public static EmojiParser getInstance() { + public static void setup(@NonNull final Context context) { if (instance == null) { synchronized (LOCK) { if (instance == null) { - instance = new EmojiParser(); + instance = new EmojiParser(context); } } } - return instance; } - private EmojiParser() { - final String file = "res/raw/emojis.json"; - final ClassLoader classLoader = getClass().getClassLoader(); - if (classLoader == null) { - Log.e(TAG, "Emoji: classLoader is null"); - return; + public static EmojiParser getInstance() { + if (instance == null) { + throw new RuntimeException("Setup not done!"); } - try (final InputStream in = classLoader.getResourceAsStream(file)) { + return instance; + } + + private EmojiParser(final Context context) { + try (final InputStream in = context.getResources().openRawResource(R.raw.emojis)) { final String json = NetworkUtils.readFromInputStream(in); final Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)