Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
romainpiel committed Dec 8, 2013
2 parents 0a0b66a + 29ce706 commit eaddf8a
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Meatspace/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.romainpiel.meatspace"
android:versionCode="9"
android:versionName="1.0.8">
android:versionCode="10"
android:versionName="1.0.9">

<uses-sdk
android:minSdkVersion="14"
Expand Down
23 changes: 19 additions & 4 deletions Meatspace/src/main/java/com/romainpiel/lib/api/ApiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import android.content.Context;
import android.os.Handler;

import com.bugsense.trace.BugSenseHandler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.koushikdutta.async.http.socketio.ConnectCallback;
import com.koushikdutta.async.http.socketio.SocketIOClient;
import com.romainpiel.lib.bus.BusManager;
import com.romainpiel.lib.gif.GIFUtils;
import com.romainpiel.meatspace.BuildConfig;
import com.romainpiel.model.ChatRequest;
import com.romainpiel.model.GifMedia;

/**
* Meatspace
Expand All @@ -25,7 +27,8 @@ public class ApiManager {

private Gson jsonParser;

private ApiManager() {}
private ApiManager() {
}

public static ApiManager get() {
if (instance == null) {
Expand All @@ -36,7 +39,9 @@ public static ApiManager get() {

public Gson getJsonParser() {
if (jsonParser == null) {
jsonParser = new Gson();
jsonParser = new GsonBuilder()
.registerTypeAdapter(GifMedia.class, GifMedia.getDeserializer())
.create();
}
return jsonParser;
}
Expand All @@ -50,10 +55,20 @@ public void disconnect(SocketIOClient client) {
}

public void emit(Context context, String text, byte[] picture, String fingerprint) {
String media = GifMedia.mediaFromGIFbytes(picture);

if (media == null) {
// malformed GIF
if (!BuildConfig.DEBUG) {
BugSenseHandler.sendEvent("null gif, bytes==null "+ (picture == null));
}
return;
}

ChatRequest chatRequest = new ChatRequest(
context.getString(BuildConfig.MEATSPACE_KEY),
text,
GIFUtils.mediaFromGIFbytes(picture),
media,
fingerprint
);
BusManager.get().getChatBus().post(chatRequest);
Expand Down
37 changes: 0 additions & 37 deletions Meatspace/src/main/java/com/romainpiel/lib/gif/GIFUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import android.widget.PopupMenu;
import android.widget.TextView;

import com.bugsense.trace.BugSenseHandler;
import com.romainpiel.lib.gif.GIFUtils;
import com.romainpiel.lib.ui.helper.InflateHelper;
import com.romainpiel.lib.ui.listener.OnMenuClickListener;
import com.romainpiel.lib.ui.listener.OnViewChangedListener;
import com.romainpiel.lib.utils.Debug;
import com.romainpiel.meatspace.BuildConfig;
import com.romainpiel.meatspace.R;
import com.romainpiel.model.Chat;

Expand Down Expand Up @@ -79,15 +76,12 @@ public void bind(final Chat chat) {
Chat.Value value = chat.getValue();

try {
ByteArrayInputStream in = new ByteArrayInputStream(GIFUtils.mediaToGIFbytes(value.getMedia()));
ByteArrayInputStream in = new ByteArrayInputStream(value.getMedia().getBytes());
GifDrawable gifFromStream = new GifDrawable(in);
gif.setImageDrawable(gifFromStream);
gif.setVisibility(VISIBLE);
} catch (Exception e) {
Debug.out(e);
if (!BuildConfig.DEBUG) {
BugSenseHandler.sendExceptionMessage("gif", value.getMedia(), e);
}
gif.setVisibility(INVISIBLE);
}

Expand Down
28 changes: 28 additions & 0 deletions Meatspace/src/main/java/com/romainpiel/lib/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.romainpiel.lib.utils;

import java.util.Collection;

/**
* Meatspace
* User: romainpiel
* Date: 28/08/2013
* Time: 11:50
*/
public class StringUtils {

/**
* Utility method to build a [spearator] separated string of a collection
* @param collection collection to read
* @param separator separator
* @param <E> generic parameter of the collection
* @return a string representation of the collection using the given separator
*/
public static <E> String join(Collection<E> 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): "";
}
}
6 changes: 3 additions & 3 deletions Meatspace/src/main/java/com/romainpiel/model/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
91 changes: 91 additions & 0 deletions Meatspace/src/main/java/com/romainpiel/model/GifMedia.java
Original file line number Diff line number Diff line change
@@ -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<GifMedia> getDeserializer() {
return new JsonDeserializer<GifMedia>() {
@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<String,String> tokens = new HashMap<String,String>();
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();
}
}

0 comments on commit eaddf8a

Please sign in to comment.