do not attempt to download images when not connected
This commit is contained in:
parent
3372e50155
commit
0605390144
|
@ -268,5 +268,6 @@
|
|||
<string name="using_account">using account %s</string>
|
||||
<string name="checking_image">Checking image on HTTP host</string>
|
||||
<string name="image_file_deleted">The image file has been deleted</string>
|
||||
<string name="not_connected_try_again">You are not connected. Try again later</string>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -12,7 +12,7 @@ public interface Downloadable {
|
|||
public static final int STATUS_DOWNLOADING = 0x204;
|
||||
public static final int STATUS_DELETED = 0x205;
|
||||
|
||||
public void start();
|
||||
public boolean start();
|
||||
|
||||
public int getStatus();
|
||||
|
||||
|
|
|
@ -12,7 +12,9 @@ import javax.net.ssl.HttpsURLConnection;
|
|||
import android.content.Intent;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.entities.Downloadable;
|
||||
import eu.siacs.conversations.entities.DownloadableFile;
|
||||
import eu.siacs.conversations.entities.Message;
|
||||
|
@ -35,9 +37,14 @@ public class HttpConnection implements Downloadable {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
changeStatus(STATUS_DOWNLOADING);
|
||||
new Thread(new FileDownloader()).start();
|
||||
public boolean start() {
|
||||
if (mXmppConnectionService.hasInternetConnection()) {
|
||||
changeStatus(STATUS_DOWNLOADING);
|
||||
new Thread(new FileDownloader()).start();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void init(Message message) {
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package eu.siacs.conversations.http;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import eu.siacs.conversations.entities.Message;
|
||||
import eu.siacs.conversations.entities.Message.ImageParams;
|
||||
import eu.siacs.conversations.services.AbstractConnectionManager;
|
||||
import eu.siacs.conversations.services.XmppConnectionService;
|
||||
|
||||
|
|
|
@ -13,12 +13,10 @@ import android.content.Intent;
|
|||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.PowerManager;
|
||||
import android.os.SystemClock;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.TaskStackBuilder;
|
||||
import android.text.Html;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.R;
|
||||
import eu.siacs.conversations.entities.Account;
|
||||
import eu.siacs.conversations.entities.Conversation;
|
||||
|
|
|
@ -346,15 +346,10 @@ public class XmppConnectionService extends Service {
|
|||
}
|
||||
}
|
||||
this.wakeLock.acquire();
|
||||
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
boolean isConnected = activeNetwork != null
|
||||
&& activeNetwork.isConnected();
|
||||
|
||||
|
||||
for (Account account : accounts) {
|
||||
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
|
||||
if (!isConnected) {
|
||||
if (!hasInternetConnection()) {
|
||||
account.setStatus(Account.STATUS_NO_INTERNET);
|
||||
if (statusListener != null) {
|
||||
statusListener.onStatusChanged(account);
|
||||
|
@ -412,6 +407,13 @@ public class XmppConnectionService extends Service {
|
|||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
public boolean hasInternetConnection() {
|
||||
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
return activeNetwork != null && activeNetwork.isConnected();
|
||||
}
|
||||
|
||||
@SuppressLint("TrulyRandom")
|
||||
@Override
|
||||
|
@ -1873,8 +1875,8 @@ public class XmppConnectionService extends Service {
|
|||
private class DeletedDownloadable implements Downloadable {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
return;
|
||||
public boolean start() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -102,7 +102,8 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
}
|
||||
boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI
|
||||
&& message.getMergedStatus() <= Message.STATUS_RECEIVED;
|
||||
if (message.getType() == Message.TYPE_IMAGE) {
|
||||
if (message.getType() == Message.TYPE_IMAGE
|
||||
|| message.getDownloadable() != null) {
|
||||
ImageParams params = message.getImageParams();
|
||||
if (params.size != 0) {
|
||||
filesize = params.size / 1024 + " KB";
|
||||
|
@ -262,6 +263,20 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
viewHolder.messageBody.setTextIsSelectable(true);
|
||||
}
|
||||
|
||||
private void displayDownloadableMessage(ViewHolder viewHolder,
|
||||
final Message message) {
|
||||
viewHolder.image.setVisibility(View.GONE);
|
||||
viewHolder.messageBody.setVisibility(View.GONE);
|
||||
viewHolder.download_button.setVisibility(View.VISIBLE);
|
||||
viewHolder.download_button.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startDonwloadable(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayImageMessage(ViewHolder viewHolder,
|
||||
final Message message) {
|
||||
if (viewHolder.download_button != null) {
|
||||
|
@ -474,20 +489,11 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
} else if (d != null
|
||||
&& d.getStatus() == Downloadable.STATUS_CHECKING) {
|
||||
displayInfoMessage(viewHolder, R.string.checking_image);
|
||||
} else if (d != null && d.getStatus() == Downloadable.STATUS_DELETED) {
|
||||
} else if (d != null
|
||||
&& d.getStatus() == Downloadable.STATUS_DELETED) {
|
||||
displayInfoMessage(viewHolder, R.string.image_file_deleted);
|
||||
} else if (d != null && d.getStatus() == Downloadable.STATUS_OFFER) {
|
||||
viewHolder.image.setVisibility(View.GONE);
|
||||
viewHolder.messageBody.setVisibility(View.GONE);
|
||||
viewHolder.download_button.setVisibility(View.VISIBLE);
|
||||
viewHolder.download_button
|
||||
.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startDonwloadable(item);
|
||||
}
|
||||
});
|
||||
displayDownloadableMessage(viewHolder, item);
|
||||
} else if ((item.getEncryption() == Message.ENCRYPTION_DECRYPTED)
|
||||
|| (item.getEncryption() == Message.ENCRYPTION_NONE)
|
||||
|| (item.getEncryption() == Message.ENCRYPTION_OTR)) {
|
||||
|
@ -525,13 +531,13 @@ public class MessageAdapter extends ArrayAdapter<Message> {
|
|||
return view;
|
||||
}
|
||||
|
||||
public boolean startDonwloadable(Message message) {
|
||||
public void startDonwloadable(Message message) {
|
||||
Downloadable downloadable = message.getDownloadable();
|
||||
if (downloadable != null) {
|
||||
downloadable.start();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
if (!downloadable.start()) {
|
||||
Toast.makeText(activity, R.string.not_connected_try_again,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -875,17 +875,20 @@ public class JingleConnection implements Downloadable {
|
|||
return this.transport;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (mJingleStatus == JINGLE_STATUS_INITIATED) {
|
||||
new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sendAccept();
|
||||
}
|
||||
}).start();
|
||||
public boolean start() {
|
||||
if (account.getStatus() == Account.STATUS_ONLINE) {
|
||||
if (mJingleStatus == JINGLE_STATUS_INITIATED) {
|
||||
new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sendAccept();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
Log.d(Config.LOGTAG, "status (" + mJingleStatus + ") was not ok");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue