recreate http connection after restart. fixes stale download button. better failing

This commit is contained in:
iNPUTmice 2014-10-14 19:27:49 +02:00
parent 031b739af8
commit 262128c8f9
4 changed files with 95 additions and 26 deletions

View file

@ -10,9 +10,7 @@ import java.net.URL;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -26,6 +24,7 @@ public class HttpConnection implements Downloadable {
private URL mUrl; private URL mUrl;
private Message message; private Message message;
private DownloadableFile file; private DownloadableFile file;
private long mPreviousFileSize = Long.MIN_VALUE;
public HttpConnection(HttpConnectionManager manager) { public HttpConnection(HttpConnectionManager manager) {
this.mHttpConnectionManager = manager; this.mHttpConnectionManager = manager;
@ -44,39 +43,60 @@ public class HttpConnection implements Downloadable {
mUrl = new URL(message.getBody()); mUrl = new URL(message.getBody());
this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false); this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED_CHECKING); message.setStatus(Message.STATUS_RECEIVED_CHECKING);
mXmppConnectionService.updateConversationUi();
checkFileSize(); checkFileSize();
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
this.cancel(); this.cancel();
} }
} }
public void init(Message message, URL url) {
this.message = message;
this.message.setDownloadable(this);
this.mUrl = url;
this.file = mXmppConnectionService.getFileBackend().getConversationsFile(message,false);
this.mPreviousFileSize = message.getImageParams().size;
message.setType(Message.TYPE_IMAGE);
message.setStatus(Message.STATUS_RECEIVED_CHECKING);
mXmppConnectionService.updateConversationUi();
checkFileSize();
}
private void checkFileSize() { private void checkFileSize() {
new Thread(new FileSizeChecker()).start(); new Thread(new FileSizeChecker()).start();
} }
public void cancel() { public void cancel() {
mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED); mXmppConnectionService.markMessage(message, Message.STATUS_RECEPTION_FAILED);
Log.d(Config.LOGTAG,"canceled download"); mHttpConnectionManager.finishConnection(this);
}
private void finish() {
message.setStatus(Message.STATUS_RECEIVED);
mXmppConnectionService.updateMessage(message);
mHttpConnectionManager.finishConnection(this);
} }
private class FileSizeChecker implements Runnable { private class FileSizeChecker implements Runnable {
@Override @Override
public void run() { public void run() {
long size;
try { try {
long size = retrieveFileSize(); size = retrieveFileSize();
file.setExpectedSize(size);
message.setBody(mUrl.toString()+","+String.valueOf(size));
if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
mXmppConnectionService.updateMessage(message);
start();
} else {
message.setStatus(Message.STATUS_RECEIVED_OFFER);
mXmppConnectionService.updateMessage(message);
}
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
return;
}
file.setExpectedSize(size);
message.setBody(mUrl.toString()+","+String.valueOf(size));
if (size <= mHttpConnectionManager.getAutoAcceptFileSize() || size == mPreviousFileSize) {
mXmppConnectionService.updateMessage(message);
start();
} else {
message.setStatus(Message.STATUS_RECEIVED_OFFER);
mXmppConnectionService.updateMessage(message);
} }
} }
@ -107,8 +127,7 @@ public class HttpConnection implements Downloadable {
mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING); mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVING);
download(); download();
updateImageBounds(); updateImageBounds();
message.setStatus(Message.STATUS_RECEIVED); finish();
mXmppConnectionService.updateMessage(message);
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
} }

View file

@ -1,5 +1,6 @@
package eu.siacs.conversations.http; package eu.siacs.conversations.http;
import java.net.URL;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -12,8 +13,6 @@ public class HttpConnectionManager extends AbstractConnectionManager {
public HttpConnectionManager(XmppConnectionService service) { public HttpConnectionManager(XmppConnectionService service) {
super(service); super(service);
} }
private XmppConnectionService mXmppConnectionService;
private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>(); private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>();
@ -24,4 +23,15 @@ public class HttpConnectionManager extends AbstractConnectionManager {
this.connections.add(connection); this.connections.add(connection);
return connection; return connection;
} }
public HttpConnection createNewConnection(Message message, URL url) {
HttpConnection connection = new HttpConnection(this);
connection.init(message,url);
this.connections.add(connection);
return connection;
}
public void finishConnection(HttpConnection connection) {
this.connections.remove(connection);
}
} }

View file

@ -1,12 +1,17 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Message.ImageParams;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;

View file

@ -1,5 +1,7 @@
package eu.siacs.conversations.ui.adapter; package eu.siacs.conversations.ui.adapter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -225,10 +227,19 @@ public class MessageAdapter extends ArrayAdapter<Message> {
viewHolder.messageBody.setVisibility(View.VISIBLE); viewHolder.messageBody.setVisibility(View.VISIBLE);
if (message.getBody() != null) { if (message.getBody() != null) {
if (message.getType() != Message.TYPE_PRIVATE) { if (message.getType() != Message.TYPE_PRIVATE) {
String body = Config.PARSE_EMOTICONS ? UIHelper if (message.getType() == Message.TYPE_IMAGE) {
.transformAsciiEmoticons(message.getMergedBody()) String orign = message.getImageParams().origin;
: message.getMergedBody(); if (orign!=null) {
viewHolder.messageBody.setText(body); viewHolder.messageBody.setText(orign);
} else {
viewHolder.messageBody.setText(message.getBody());
}
} else {
String body = Config.PARSE_EMOTICONS ? UIHelper
.transformAsciiEmoticons(message.getMergedBody())
: message.getMergedBody();
viewHolder.messageBody.setText(body);
}
} else { } else {
String privateMarker; String privateMarker;
if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getStatus() <= Message.STATUS_RECEIVED) {
@ -474,6 +485,8 @@ public class MessageAdapter extends ArrayAdapter<Message> {
displayInfoMessage(viewHolder, R.string.receiving_image); displayInfoMessage(viewHolder, R.string.receiving_image);
} else if (item.getStatus() == Message.STATUS_RECEIVED_CHECKING) { } else if (item.getStatus() == Message.STATUS_RECEIVED_CHECKING) {
displayInfoMessage(viewHolder, R.string.checking_image); displayInfoMessage(viewHolder, R.string.checking_image);
} else if (item.getStatus() == Message.STATUS_RECEPTION_FAILED) {
displayTextMessage(viewHolder, item);
} else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) { } else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) {
viewHolder.image.setVisibility(View.GONE); viewHolder.image.setVisibility(View.GONE);
viewHolder.messageBody.setVisibility(View.GONE); viewHolder.messageBody.setVisibility(View.GONE);
@ -483,10 +496,10 @@ public class MessageAdapter extends ArrayAdapter<Message> {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Downloadable downloadable = item if (!startDonwloadable(item)) {
.getDownloadable(); activity.xmppConnectionService.markMessage(
if (downloadable != null) { item,
downloadable.start(); Message.STATUS_RECEPTION_FAILED);
} }
} }
}); });
@ -527,6 +540,28 @@ public class MessageAdapter extends ArrayAdapter<Message> {
return view; return view;
} }
public boolean startDonwloadable(Message message) {
Downloadable downloadable = message.getDownloadable();
if (downloadable != null) {
downloadable.start();
return true;
} else {
ImageParams params = message.getImageParams();
if (params.origin != null) {
try {
URL url = new URL(params.origin);
activity.xmppConnectionService.getHttpConnectionManager()
.createNewConnection(message, url);
return true;
} catch (MalformedURLException e) {
return false;
}
} else {
return false;
}
}
}
private static class ViewHolder { private static class ViewHolder {
protected LinearLayout message_box; protected LinearLayout message_box;