Skip to content

Commit

Permalink
Improved Download Google Static Map (#51)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Cappelli <[email protected]>
  • Loading branch information
sw1ftc0d3r committed Mar 23, 2014
1 parent 8ed56aa commit 0a8037a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 64 deletions.
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
android:theme="@style/Theme.Light"
android:allowBackup="true">

<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="TE_LO_STO_PE_DI" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCXBmXiUCI4Xykhe-Un-CKlpDYEQUTgisg" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

<!-- Services -->
Expand Down
78 changes: 22 additions & 56 deletions src/org/kontalk/service/MessageCenterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -66,7 +65,6 @@
import org.kontalk.client.BlockingCommand;
import org.kontalk.client.E2EEncryption;
import org.kontalk.client.EndpointServer;
import org.kontalk.client.HttpDownload;
import org.kontalk.client.KontalkConnection;
import org.kontalk.client.OutOfBandData;
import org.kontalk.client.Ping;
Expand Down Expand Up @@ -106,7 +104,8 @@
import org.kontalk.service.XMPPConnectionHelper.ConnectionHelperListener;
import org.kontalk.ui.MessagingNotification;
import org.kontalk.ui.MessagingPreferences;
import org.kontalk.util.GMStaticUrlBuilder;
import org.kontalk.util.GMStaticMap;
import org.kontalk.util.GMStaticMap.GMStaticMapListener;
import org.kontalk.util.MediaStorage;
import org.kontalk.util.MessageUtils;
import org.kontalk.util.RandomString;
Expand Down Expand Up @@ -1310,35 +1309,25 @@ private Uri incoming(CompositeMessage msg) {
LocationComponent _location = (LocationComponent) msg.getComponent(LocationComponent.class);
if (_location instanceof LocationComponent) {
LocationComponent location = _location;
GMStaticUrlBuilder url = new GMStaticUrlBuilder()
.setCenter(location.getLatitude(), location.getLongitude())
.setMapType("roadmap")
.setMarker("red", '\0', location.getLatitude(), location.getLongitude())
.setSensor(false)
.setSize(600, 300)
.setZoom(13);

final File dest = new File(getCacheDir(), String.format(Locale.US, "%f", location.getLatitude())
+ "_"
+ String.format(Locale.US, "%f", location.getLongitude())+ ".png");
final Uri _uri = msgUri;

new HttpDownload(url.toString(), dest, new Runnable() {
public void run() {
ContentValues v = new ContentValues(1);
v.put(Messages.ATTACHMENT_PREVIEW_PATH, dest.toString());
getContentResolver().update(_uri, v, null, null);

MessagingNotification.delayedUpdateMessagesNotification(getApplicationContext(), false);
}
},

new Runnable() {
public void run() {
// TODO
}
})
.start();

final Uri _uri = msgUri;

GMStaticMap.getInstance(this).requestMap(
location.getLatitude(),
location.getLongitude(),
new GMStaticMapListener() {
public void completed(File destination) {
ContentValues v = new ContentValues(1);
v.put(Messages.ATTACHMENT_PREVIEW_PATH, destination.toString());
getContentResolver().update(_uri, v, null, null);

MessagingNotification.delayedUpdateMessagesNotification
(getApplicationContext(), false);
}

public void error(File destination) {
}
});
}

// mark sender as registered in the users database
Expand Down Expand Up @@ -2445,33 +2434,10 @@ else if (VCardComponent.supportsMimeType(mime)) {
PacketExtension _location = m.getExtension(UserLocation.ELEMENT_NAME, UserLocation.NAMESPACE);
if (_location != null && _location instanceof UserLocation) {
UserLocation location = (UserLocation) _location;
GMStaticUrlBuilder url = new GMStaticUrlBuilder()
.setCenter(location.getLatitude(), location.getLongitude())
.setMapType("roadmap")
.setMarker("red", '\0', location.getLatitude(), location.getLongitude())
.setSensor(false)
.setSize(600, 300)
.setZoom(13);

final File dest = new File(getCacheDir(), String.format(Locale.US, "%f", location.getLatitude())
+ "_"
+ String.format(Locale.US, "%f", location.getLongitude())+ ".png");
new HttpDownload(url.toString(), dest, new Runnable() {
public void run() {
ContentValues v = new ContentValues(1);
v.put(Messages.ATTACHMENT_PREVIEW_PATH, dest.getAbsolutePath());
}
},
new Runnable() {
public void run() {
// TODO
}
})
.start();

msg.addComponent(new LocationComponent
(location.getLatitude(),
location.getLongitude(), dest));
location.getLongitude(), null));
}

if (msg != null) {
Expand Down
43 changes: 36 additions & 7 deletions src/org/kontalk/ui/MessageListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import org.kontalk.message.LocationComponent;
import org.kontalk.message.TextComponent;
import org.kontalk.provider.MyMessages.Messages;
import org.kontalk.util.GMStaticMap;
import org.kontalk.util.GMStaticMap.GMStaticMapListener;
import org.kontalk.util.MessageUtils;
import org.kontalk.util.MessageUtils.SmileyImageSpan;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand Down Expand Up @@ -371,16 +375,41 @@ private SpannableStringBuilder formatMessage(final Contact contact, final Patter
if (loc != null) {
String placeholder = "Map";
buf.insert(0, placeholder);
try {
File preview = loc.getCachedMap();
Bitmap bitmap = BitmapFactory.decodeFile(preview.getAbsolutePath());
if (bitmap != null) {
ImageSpan imgSpan = new MaxSizeImageSpan(getContext(), bitmap);
buf.setSpan(imgSpan, 0, placeholder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// add newline if there is some text after
if (!thumbnailOnly)
buf.insert(placeholder.length(), "\n");

final long msgId = mMessage.getDatabaseId();
File map = GMStaticMap.getInstance(getContext())
.requestMap(loc.getLatitude(), loc.getLongitude(), new GMStaticMapListener() {
public void completed(File destination) {
ContentValues v = new ContentValues(1);
v.put(Messages.ATTACHMENT_PREVIEW_PATH, destination.toString());
getContext().getContentResolver().update(ContentUris
.withAppendedId(Messages.CONTENT_URI,
msgId), v, null, null);
}

public void error(File destination) {
}
});

if (map != null) {

try {
Bitmap bitmap = BitmapFactory.decodeFile(map.getAbsolutePath());
if (bitmap != null) {
ImageSpan imgSpan = new MaxSizeImageSpan(getContext(), bitmap);
buf.setSpan(imgSpan, 0, placeholder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
catch (Exception e) {

}
} catch (Exception e) {

}

}
}

Expand Down
131 changes: 131 additions & 0 deletions src/org/kontalk/util/GMStaticMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.kontalk.util;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.kontalk.client.HttpDownload;

import android.content.Context;


public class GMStaticMap {

private static final String GM_MAP_TYPE = "roadmap";
private static final String GM_MARKER_COLOR = "red";
private static final char GM_MARKER_LABEL = '\0';
private static final boolean GM_SENSOR = false;
private static final int GM_MAP_WIDTH = 600;
private static final int GM_MAP_HEIGHT = 300;
private static final int GM_MAP_ZOOM = 13;

private static GMStaticMap sInstance;

public static GMStaticMap getInstance(Context context) {
if (sInstance == null)
sInstance = new GMStaticMap(context.getApplicationContext());

return sInstance;
}

private final Context mContext;

private final Map<String, Set<GMStaticMapListener>> mQueue;

private GMStaticMap(Context context) {
mContext = context;
mQueue = new HashMap<String, Set<GMStaticMapListener>>();
}

public File requestMap(double lat, double lon, GMStaticMapListener listener) {
String filename = String.format(Locale.US, "gmap_%f_%f.png", lat, lon);
final File dest = new File(mContext.getCacheDir(), filename);

// file exists - return file object
if (dest.isFile())
return dest;

GMStaticUrlBuilder url = new GMStaticUrlBuilder()
.setCenter(lat, lon)
.setMapType(GM_MAP_TYPE)
.setMarker(GM_MARKER_COLOR, GM_MARKER_LABEL, lat, lon)
.setSensor(GM_SENSOR)
.setSize(GM_MAP_WIDTH, GM_MAP_HEIGHT)
.setZoom(GM_MAP_ZOOM);

// queue the download
queueDownload(url.toString(), filename, dest, listener);

return null;
}

private void queueDownload(String url, final String id, final File destination,
GMStaticMapListener listener) {

boolean start = false;

synchronized (mQueue) {
Set<GMStaticMapListener> listeners = mQueue.get(id);
if (listeners == null) {
listeners = new LinkedHashSet<GMStaticMapListener>(1);
mQueue.put(id, listeners);

start = true;
}

listeners.add(listener);
}

if (start)
new HttpDownload(url, destination,
new Runnable() {
public void run() {
completed(id, destination);
}
},
new Runnable() {
public void run() {
error(id, destination);
}
})
.start();
}

public void completed(String id, File destination) {
// TODO check file size

synchronized (mQueue) {
Set<GMStaticMapListener> listeners = mQueue.get(id);
if (listeners != null) {
for (GMStaticMapListener l : listeners)
l.completed(destination);

mQueue.remove(id);
}
}
}

public void error(String id, File destination) {
// delete file
destination.delete();

synchronized (mQueue) {
Set<GMStaticMapListener> listeners = mQueue.get(id);
if (listeners != null) {
for (GMStaticMapListener l : listeners)
l.error(destination);

mQueue.remove(id);
}
}
}

public interface GMStaticMapListener {
public void completed(File destination);
public void error(File destination);
}

}

0 comments on commit 0a8037a

Please sign in to comment.