From 3e056df520676cfa7b16f1a22d0a1aae6e741e89 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 21 Mar 2021 15:12:12 -0400 Subject: [PATCH 1/3] bypass the error so it doesn't crash --- .../java/awais/instagrabber/customviews/emoji/Emoji.java | 3 ++- .../instagrabber/customviews/emoji/EmojiCategory.java | 9 +++++++++ .../java/awais/instagrabber/utils/emoji/EmojiParser.java | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java b/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java index d90c4d8e..2ce07a6e 100644 --- a/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java +++ b/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java @@ -35,7 +35,7 @@ public class Emoji { } public GoogleCompatEmojiDrawable getDrawable() { - if (drawable == null) { + if (drawable == null && unicode != null) { drawable = new GoogleCompatEmojiDrawable(unicode); } return drawable; @@ -60,6 +60,7 @@ public class Emoji { return "Emoji{" + "unicode='" + unicode + '\'' + ", name='" + name + '\'' + + ", variants=" + variants + '}'; } } diff --git a/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java b/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java index 4a621db0..8147a3aa 100644 --- a/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java +++ b/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java @@ -18,6 +18,10 @@ public class EmojiCategory { this.type = type; } + public EmojiCategory() { + this.type = null; + } + public EmojiCategoryType getType() { return type; } @@ -73,4 +77,9 @@ public class EmojiCategory { public int hashCode() { return Objects.hash(type); } + + @Override + public String toString() { + return "EmojiCategory {TYPE=" + type + ", EMOJIS=" + emojis + "}"; + } } 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 09a67225..dc29f12b 100644 --- a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java +++ b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java @@ -68,7 +68,7 @@ public final class EmojiParser { .build() .stream()) .collect(Collectors.toMap(Emoji::getUnicode, Function.identity())); - } catch (IOException e) { + } catch (Exception e) { Log.e(TAG, "EmojiParser: ", e); } } From 358beffa9dbcf5d8eec669d6a45e746aaa18a4c4 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 21 Mar 2021 15:56:09 -0400 Subject: [PATCH 2/3] small fix --- .../main/java/awais/instagrabber/fragments/HashTagFragment.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index d822587d..75e72949 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -378,6 +378,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe if (getArguments() == null) return; final HashTagFragmentArgs fragmentArgs = HashTagFragmentArgs.fromBundle(getArguments()); hashtag = fragmentArgs.getHashtag(); + if (hashtag.charAt(0) == '#') hashtag = hashtag.substring(1); fetchHashtagModel(); } From 6b047c4ebb8cb2a86c1a9ab1383c982e3fdb6383 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 22 Mar 2021 07:37:41 +0900 Subject: [PATCH 3/3] Add custom deserializer for EmojiCategory and Emoji class, to fix parsing error --- .../instagrabber/customviews/emoji/Emoji.java | 7 +-- .../customviews/emoji/EmojiCategory.java | 17 +++--- .../emoji/EmojiCategoryDeserializer.java | 52 +++++++++++++++++++ .../utils/emoji/EmojiDeserializer.java | 44 ++++++++++++++++ .../instagrabber/utils/emoji/EmojiParser.java | 10 +++- 5 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java create mode 100644 app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java diff --git a/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java b/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java index 2ce07a6e..2be01c47 100644 --- a/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java +++ b/app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java @@ -2,7 +2,6 @@ package awais.instagrabber.customviews.emoji; import androidx.annotation.NonNull; -import java.util.LinkedList; import java.util.List; import java.util.Objects; @@ -12,10 +11,12 @@ public class Emoji { private final List variants; private GoogleCompatEmojiDrawable drawable; - public Emoji(final String unicode, final String name) { + public Emoji(final String unicode, + final String name, + final List variants) { this.unicode = unicode; this.name = name; - this.variants = new LinkedList<>(); + this.variants = variants; } public String getUnicode() { diff --git a/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java b/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java index 8147a3aa..89a2c866 100644 --- a/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java +++ b/app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java @@ -1,8 +1,8 @@ package awais.instagrabber.customviews.emoji; import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -10,16 +10,13 @@ import awais.instagrabber.R; public class EmojiCategory { private final EmojiCategoryType type; - private final Map emojis = new LinkedHashMap<>(); + private final Map emojis; @DrawableRes private int drawableRes; - public EmojiCategory(final EmojiCategoryType type) { + public EmojiCategory(final EmojiCategoryType type, final Map emojis) { this.type = type; - } - - public EmojiCategory() { - this.type = null; + this.emojis = emojis; } public EmojiCategoryType getType() { @@ -78,8 +75,12 @@ public class EmojiCategory { return Objects.hash(type); } + @NonNull @Override public String toString() { - return "EmojiCategory {TYPE=" + type + ", EMOJIS=" + emojis + "}"; + return "EmojiCategory{" + + "type=" + type + + ", emojis=" + emojis + + '}'; } } diff --git a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java new file mode 100644 index 00000000..1fc510e2 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java @@ -0,0 +1,52 @@ +package awais.instagrabber.utils.emoji; + +import android.util.Log; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; +import java.util.LinkedHashMap; +import java.util.Map; + +import awais.instagrabber.customviews.emoji.Emoji; +import awais.instagrabber.customviews.emoji.EmojiCategory; +import awais.instagrabber.customviews.emoji.EmojiCategoryType; + +public class EmojiCategoryDeserializer implements JsonDeserializer { + private static final String TAG = EmojiCategoryDeserializer.class.getSimpleName(); + + @Override + public EmojiCategory deserialize(final JsonElement json, + final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException { + final JsonObject jsonObject = json.getAsJsonObject(); + final JsonElement typeElement = jsonObject.get("type"); + final JsonObject emojisObject = jsonObject.getAsJsonObject("emojis"); + if (typeElement == null || emojisObject == null) { + throw new JsonParseException("Invalid json for EmojiCategory"); + } + final String typeString = typeElement.getAsString(); + EmojiCategoryType type; + try { + type = EmojiCategoryType.valueOf(typeString); + } catch (IllegalArgumentException e) { + Log.e(TAG, "deserialize: ", e); + type = EmojiCategoryType.OTHERS; + } + final Map emojis = new LinkedHashMap<>(); + for (final Map.Entry emojiObjectEntry : emojisObject.entrySet()) { + final String unicode = emojiObjectEntry.getKey(); + final JsonElement value = emojiObjectEntry.getValue(); + if (unicode == null || value == null) { + throw new JsonParseException("Invalid json for EmojiCategory"); + } + final Emoji emoji = context.deserialize(value, Emoji.class); + emojis.put(unicode, emoji); + } + return new EmojiCategory(type, emojis); + } +} diff --git a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java new file mode 100644 index 00000000..cc774d60 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java @@ -0,0 +1,44 @@ +package awais.instagrabber.utils.emoji; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; +import java.util.LinkedList; +import java.util.List; + +import awais.instagrabber.customviews.emoji.Emoji; + +public class EmojiDeserializer implements JsonDeserializer { + @Override + public Emoji deserialize(final JsonElement json, + final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException { + final JsonObject jsonObject = json.getAsJsonObject(); + final JsonElement unicodeElement = jsonObject.get("unicode"); + final JsonElement nameElement = jsonObject.get("name"); + if (unicodeElement == null || nameElement == null) { + throw new JsonParseException("Invalid json for Emoji class"); + } + final JsonElement variantsElement = jsonObject.get("variants"); + final List variants = new LinkedList<>(); + if (variantsElement != null) { + final JsonArray variantsArray = variantsElement.getAsJsonArray(); + for (final JsonElement variantElement : variantsArray) { + final Emoji variant = context.deserialize(variantElement, Emoji.class); + if (variant != null) { + variants.add(variant); + } + } + } + return new Emoji( + unicodeElement.getAsString(), + nameElement.getAsString(), + variants + ); + } +} 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 dc29f12b..17c8327c 100644 --- a/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java +++ b/app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java @@ -3,10 +3,11 @@ package awais.instagrabber.utils.emoji; import android.util.Log; import com.google.common.collect.ImmutableList; +import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; -import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; import java.util.Collection; @@ -52,7 +53,12 @@ public final class EmojiParser { } try (final InputStream in = classLoader.getResourceAsStream(file)) { final String json = NetworkUtils.readFromInputStream(in); - final Gson gson = new Gson(); + final Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .registerTypeAdapter(EmojiCategory.class, new EmojiCategoryDeserializer()) + .registerTypeAdapter(Emoji.class, new EmojiDeserializer()) + .setLenient() + .create(); final Type type = new TypeToken>() {}.getType(); categoryMap = gson.fromJson(json, type); // Log.d(TAG, "EmojiParser: " + categoryMap);