diff --git a/Meatspace/src/main/AndroidManifest.xml b/Meatspace/src/main/AndroidManifest.xml index 246a445..35851a2 100644 --- a/Meatspace/src/main/AndroidManifest.xml +++ b/Meatspace/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ + android:versionCode="10" + android:versionName="1.0.9"> generic parameter of the collection + * @return a string representation of the collection using the given separator + */ + public static String join(Collection collection, String separator) { + StringBuilder result = new StringBuilder(); + for(E object : collection) { + result.append(object.toString()); + result.append(separator); + } + return result.length() > 0 ? result.substring(0, result.length() - 1): ""; + } +} diff --git a/Meatspace/src/main/java/com/romainpiel/model/Chat.java b/Meatspace/src/main/java/com/romainpiel/model/Chat.java index 142598d..ffa670a 100644 --- a/Meatspace/src/main/java/com/romainpiel/model/Chat.java +++ b/Meatspace/src/main/java/com/romainpiel/model/Chat.java @@ -41,7 +41,7 @@ public static int compare(Chat lhs, Chat rhs) { public static class Value { private String fingerprint; private String message; - private String media; + private GifMedia media; private int ttl; private long created; private boolean isMuted; @@ -63,11 +63,11 @@ public void setMessage(String message) { this.message = message; } - public String getMedia() { + public GifMedia getMedia() { return media; } - public void setMedia(String media) { + public void setMedia(GifMedia media) { this.media = media; } diff --git a/Meatspace/src/main/java/com/romainpiel/model/GifMedia.java b/Meatspace/src/main/java/com/romainpiel/model/GifMedia.java new file mode 100644 index 0000000..cc2360e --- /dev/null +++ b/Meatspace/src/main/java/com/romainpiel/model/GifMedia.java @@ -0,0 +1,91 @@ +package com.romainpiel.model; + +import android.util.Base64; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.romainpiel.lib.utils.Debug; +import com.romainpiel.lib.utils.StringUtils; + +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * MeatspaceProject + * User: romainpiel + * Date: 08/12/2013 + * Time: 09:13 + */ +public class GifMedia { + + private static final String GIF_PREFIX = "data:image/gif;base64,"; + + private byte[] bytes; + + public byte[] getBytes() { + return bytes; + } + + public static JsonDeserializer getDeserializer() { + return new JsonDeserializer() { + @Override + public GifMedia deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { + String raw = jsonElement.getAsString(); + GifMedia result = new GifMedia(); + result.bytes = mediaToGIFbytes(raw); + return result; + } + }; + } + + public static String mediaFromGIFbytes(byte[] bytes) { + if (bytes == null) { + return null; + } + + String result = null; + try { + result = GIF_PREFIX + new String(Base64.encode(bytes, Base64.DEFAULT), "UTF-8"); + } catch (UnsupportedEncodingException e) { + Debug.out(e); + } + return result; + } + + public static byte[] mediaToGIFbytes(String media) { + if (media == null) { + return null; + } + + try { + return Base64.decode(unescape(media), Base64.DEFAULT); + } catch (IllegalArgumentException e) { + return null; + } + } + + public static String unescape(String template) { + Map tokens = new HashMap(); + tokens.put(GIF_PREFIX, ""); + tokens.put("%2b", "+"); + tokens.put("%2f", "/"); + + String patternString = "(" + StringUtils.join(tokens.keySet(), "|") + ")"; + Pattern pattern = Pattern.compile(patternString); + Matcher matcher = pattern.matcher(template); + + StringBuffer sb = new StringBuffer(); + while(matcher.find()) { + matcher.appendReplacement(sb, tokens.get(matcher.group(1))); + } + matcher.appendTail(sb); + + return sb.toString(); + } +}