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

View File

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

View File

@ -234,6 +234,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this); private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
private OnConversationUpdate mOnConversationUpdate = null; private OnConversationUpdate mOnConversationUpdate = null;
private int convChangedListenerCount = 0; private int convChangedListenerCount = 0;
private OnShowErrorToast mOnShowErrorToast = null;
private int showErrorToastListenerCount = 0;
private int unreadCount = -1; private int unreadCount = -1;
private OnAccountUpdate mOnAccountUpdate = null; private OnAccountUpdate mOnAccountUpdate = null;
private OnStatusChanged statusListener = new OnStatusChanged() { private OnStatusChanged statusListener = new OnStatusChanged() {
@ -685,7 +687,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void sendMessage(final Message message) { public void sendMessage(final Message message) {
sendMessage(message,false); sendMessage(message, false);
} }
private void sendMessage(final Message message, final boolean resend) { 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) { public void resendMessage(final Message message) {
sendMessage(message,true); sendMessage(message, true);
} }
public void fetchRosterFromServer(final Account account) { 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) { public void setOnAccountListChangedListener(OnAccountUpdate listener) {
synchronized (this) { synchronized (this) {
if (checkListeners()) { if (checkListeners()) {
@ -1345,7 +1373,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return (this.mOnAccountUpdate == null return (this.mOnAccountUpdate == null
&& this.mOnConversationUpdate == null && this.mOnConversationUpdate == null
&& this.mOnRosterUpdate == null && this.mOnRosterUpdate == null
&& this.mOnUpdateBlocklist == null); && this.mOnUpdateBlocklist == null
&& this.mOnShowErrorToast == null);
} }
private void switchToForeground() { private void switchToForeground() {
@ -2196,6 +2225,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
return count; return count;
} }
public void showErrorToastInUi(int resId) {
if (mOnShowErrorToast != null) {
mOnShowErrorToast.onShowErrorToast(resId);
}
}
public void updateConversationUi() { public void updateConversationUi() {
if (mOnConversationUpdate != null) { if (mOnConversationUpdate != null) {
mOnConversationUpdate.onConversationUpdate(); mOnConversationUpdate.onConversationUpdate();
@ -2529,6 +2565,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void onPushFailed(); public void onPushFailed();
} }
public interface OnShowErrorToast {
void onShowErrorToast(int resId);
}
public class XmppConnectionBinder extends Binder { public class XmppConnectionBinder extends Binder {
public XmppConnectionService getService() { public XmppConnectionService getService() {
return XmppConnectionService.this; 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.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; 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.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;
@ -48,7 +49,7 @@ import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
public class ConversationActivity extends XmppActivity 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"; public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
@ -1271,4 +1272,14 @@ public class ConversationActivity extends XmppActivity
public boolean enterIsSend() { public boolean enterIsSend() {
return getPreferences().getBoolean("enter_is_send",false); 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) { if (this instanceof OnUpdateBlocklist) {
this.xmppConnectionService.setOnUpdateBlocklistListener((OnUpdateBlocklist) this); this.xmppConnectionService.setOnUpdateBlocklistListener((OnUpdateBlocklist) this);
} }
if (this instanceof XmppConnectionService.OnShowErrorToast) {
this.xmppConnectionService.setOnShowErrorToastListener((XmppConnectionService.OnShowErrorToast) this);
}
} }
protected void unregisterListeners() { protected void unregisterListeners() {
@ -302,6 +305,9 @@ public abstract class XmppActivity extends Activity {
if (this instanceof OnUpdateBlocklist) { if (this instanceof OnUpdateBlocklist) {
this.xmppConnectionService.removeOnUpdateBlocklistListener(); this.xmppConnectionService.removeOnUpdateBlocklistListener();
} }
if (this instanceof XmppConnectionService.OnShowErrorToast) {
this.xmppConnectionService.removeOnShowErrorToastListener();
}
} }
@Override @Override
@ -447,14 +453,11 @@ public abstract class XmppActivity extends Activity {
@Override @Override
public void success(Account account) { public void success(Account account) {
xmppConnectionService.databaseBackend xmppConnectionService.databaseBackend.updateAccount(account);
.updateAccount(account);
xmppConnectionService.sendPresence(account); xmppConnectionService.sendPresence(account);
if (conversation != null) { if (conversation != null) {
conversation conversation.setNextEncryption(Message.ENCRYPTION_PGP);
.setNextEncryption(Message.ENCRYPTION_PGP); xmppConnectionService.databaseBackend.updateConversation(conversation);
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.makeText(activity, R.string.not_connected_try_again,
Toast.LENGTH_SHORT).show(); 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="none">None</string>
<string name="recently_used">Most recently used</string> <string name="recently_used">Most recently used</string>
<string name="choose_quick_action">Choose quick action</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> </resources>