display toast in ui on failed http download

fixes #954
This commit is contained in:
Daniel Gultsch 2015-07-10 13:28:50 +02:00
parent b02ef0c62f
commit 0f5c87ca1f
7 changed files with 105 additions and 36 deletions

View file

@ -3,6 +3,7 @@ package eu.siacs.conversations.http;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.util.Log;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
@ -24,6 +25,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
@ -63,6 +65,10 @@ public class HttpConnection implements Downloadable {
}
public void init(Message message) {
init(message,false);
}
public void init(Message message, boolean interactive) {
this.message = message;
this.message.setDownloadable(this);
try {
@ -92,7 +98,7 @@ public class HttpConnection implements Downloadable {
&& this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
checkFileSize(false);
checkFileSize(true);
} catch (MalformedURLException e) {
this.cancel();
}
@ -180,6 +186,10 @@ public class HttpConnection implements Downloadable {
HttpConnection.this.mXmppConnectionService.getNotificationService().push(message);
return;
} catch (IOException e) {
Log.d(Config.LOGTAG, "io exception in http file size checker: " + e.getMessage());
if (interactive) {
mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
}
cancel();
return;
}
@ -194,11 +204,10 @@ public class HttpConnection implements Downloadable {
}
}
private long retrieveFileSize() throws IOException,
SSLHandshakeException {
private long retrieveFileSize() throws IOException {
Log.d(Config.LOGTAG,"retrieve file size. interactive:"+String.valueOf(interactive));
changeStatus(STATUS_CHECKING);
HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection();
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
connection.setRequestMethod("HEAD");
if (connection instanceof HttpsURLConnection) {
setupTrustManager((HttpsURLConnection) connection, interactive);
@ -235,19 +244,18 @@ public class HttpConnection implements Downloadable {
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER);
} catch (IOException e) {
mXmppConnectionService.showErrorToastInUi(R.string.file_not_found_on_remote_host);
cancel();
}
}
private void download() throws SSLHandshakeException, IOException {
HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection();
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
if (connection instanceof HttpsURLConnection) {
setupTrustManager((HttpsURLConnection) connection, interactive);
}
connection.connect();
BufferedInputStream is = new BufferedInputStream(
connection.getInputStream());
BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
file.getParentFile().mkdirs();
file.createNewFile();
OutputStream os = file.createOutputStream();

View file

@ -17,8 +17,12 @@ public class HttpConnectionManager extends AbstractConnectionManager {
private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
public HttpConnection createNewConnection(Message message) {
return this.createNewConnection(message,false);
}
public HttpConnection createNewConnection(Message message,boolean interactive) {
HttpConnection connection = new HttpConnection(this);
connection.init(message);
connection.init(message,interactive);
this.connections.add(connection);
return connection;
}

View file

@ -234,6 +234,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
private OnConversationUpdate mOnConversationUpdate = null;
private int convChangedListenerCount = 0;
private OnShowErrorToast mOnShowErrorToast = null;
private int showErrorToastListenerCount = 0;
private int unreadCount = -1;
private OnAccountUpdate mOnAccountUpdate = null;
private OnStatusChanged statusListener = new OnStatusChanged() {
@ -685,7 +687,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
public void sendMessage(final Message message) {
sendMessage(message,false);
sendMessage(message, false);
}
private void sendMessage(final Message message, final boolean resend) {
@ -818,7 +820,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
public void resendMessage(final Message message) {
sendMessage(message,true);
sendMessage(message, true);
}
public void fetchRosterFromServer(final Account account) {
@ -1241,6 +1243,32 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
}
public void setOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
synchronized (this) {
if (checkListeners()) {
switchToForeground();
}
this.mOnShowErrorToast = onShowErrorToast;
if (this.showErrorToastListenerCount < 2) {
this.showErrorToastListenerCount++;
}
}
this.mOnShowErrorToast = onShowErrorToast;
}
public void removeOnShowErrorToastListener() {
synchronized (this) {
this.showErrorToastListenerCount--;
if (this.showErrorToastListenerCount <= 0) {
this.showErrorToastListenerCount = 0;
this.mOnShowErrorToast = null;
if (checkListeners()) {
switchToBackground();
}
}
}
}
public void setOnAccountListChangedListener(OnAccountUpdate listener) {
synchronized (this) {
if (checkListeners()) {
@ -1345,7 +1373,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return (this.mOnAccountUpdate == null
&& this.mOnConversationUpdate == null
&& this.mOnRosterUpdate == null
&& this.mOnUpdateBlocklist == null);
&& this.mOnUpdateBlocklist == null
&& this.mOnShowErrorToast == null);
}
private void switchToForeground() {
@ -2196,6 +2225,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return count;
}
public void showErrorToastInUi(int resId) {
if (mOnShowErrorToast != null) {
mOnShowErrorToast.onShowErrorToast(resId);
}
}
public void updateConversationUi() {
if (mOnConversationUpdate != null) {
mOnConversationUpdate.onConversationUpdate();
@ -2529,6 +2565,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void onPushFailed();
}
public interface OnShowErrorToast {
void onShowErrorToast(int resId);
}
public class XmppConnectionBinder extends Binder {
public XmppConnectionService getService() {
return XmppConnectionService.this;

View file

@ -40,6 +40,7 @@ import eu.siacs.conversations.entities.Blockable;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
@ -48,7 +49,7 @@ import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
public class ConversationActivity extends XmppActivity
implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist {
implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
@ -1271,4 +1272,14 @@ public class ConversationActivity extends XmppActivity
public boolean enterIsSend() {
return getPreferences().getBoolean("enter_is_send",false);
}
@Override
public void onShowErrorToast(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
}
});
}
}

View file

@ -284,6 +284,9 @@ public abstract class XmppActivity extends Activity {
if (this instanceof OnUpdateBlocklist) {
this.xmppConnectionService.setOnUpdateBlocklistListener((OnUpdateBlocklist) this);
}
if (this instanceof XmppConnectionService.OnShowErrorToast) {
this.xmppConnectionService.setOnShowErrorToastListener((XmppConnectionService.OnShowErrorToast) this);
}
}
protected void unregisterListeners() {
@ -302,6 +305,9 @@ public abstract class XmppActivity extends Activity {
if (this instanceof OnUpdateBlocklist) {
this.xmppConnectionService.removeOnUpdateBlocklistListener();
}
if (this instanceof XmppConnectionService.OnShowErrorToast) {
this.xmppConnectionService.removeOnShowErrorToastListener();
}
}
@Override
@ -447,14 +453,11 @@ public abstract class XmppActivity extends Activity {
@Override
public void success(Account account) {
xmppConnectionService.databaseBackend
.updateAccount(account);
xmppConnectionService.databaseBackend.updateAccount(account);
xmppConnectionService.sendPresence(account);
if (conversation != null) {
conversation
.setNextEncryption(Message.ENCRYPTION_PGP);
xmppConnectionService.databaseBackend
.updateConversation(conversation);
conversation.setNextEncryption(Message.ENCRYPTION_PGP);
xmppConnectionService.databaseBackend.updateConversation(conversation);
}
}

View file

@ -542,6 +542,8 @@ public class MessageAdapter extends ArrayAdapter<Message> {
Toast.makeText(activity, R.string.not_connected_try_again,
Toast.LENGTH_SHORT).show();
}
} else if (message.treatAsDownloadable() != Message.Decision.NEVER) {
activity.xmppConnectionService.getHttpConnectionManager().createNewConnection(message);
}
}

View file

@ -478,4 +478,5 @@
<string name="none">None</string>
<string name="recently_used">Most recently used</string>
<string name="choose_quick_action">Choose quick action</string>
<string name="file_not_found_on_remote_host">File not found on remote server</string>
</resources>