notify only after image has been received over http. introduced mini grace for catching up with SM or offline messages
This commit is contained in:
parent
d73a77643d
commit
cbc3d9bd6f
|
@ -11,6 +11,7 @@ public final class Config {
|
||||||
public static final int PING_TIMEOUT = 10;
|
public static final int PING_TIMEOUT = 10;
|
||||||
public static final int CONNECT_TIMEOUT = 90;
|
public static final int CONNECT_TIMEOUT = 90;
|
||||||
public static final int CARBON_GRACE_PERIOD = 60;
|
public static final int CARBON_GRACE_PERIOD = 60;
|
||||||
|
public static final int MINI_GRACE_PERIOD = 750;
|
||||||
|
|
||||||
public static final int AVATAR_SIZE = 192;
|
public static final int AVATAR_SIZE = 192;
|
||||||
public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP;
|
public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP;
|
||||||
|
|
|
@ -5,9 +5,7 @@ import java.net.URL;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
|
||||||
public class Message extends AbstractEntity {
|
public class Message extends AbstractEntity {
|
||||||
|
@ -145,19 +143,6 @@ public class Message extends AbstractEntity {
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getReadableBody(Context context) {
|
|
||||||
if (encryption == ENCRYPTION_PGP) {
|
|
||||||
return context.getText(R.string.encrypted_message_received)
|
|
||||||
.toString();
|
|
||||||
} else if (encryption == ENCRYPTION_DECRYPTION_FAILED) {
|
|
||||||
return context.getText(R.string.decryption_failed).toString();
|
|
||||||
} else if (type == TYPE_IMAGE) {
|
|
||||||
return context.getText(R.string.image_file).toString();
|
|
||||||
} else {
|
|
||||||
return body.trim();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getTimeSent() {
|
public long getTimeSent() {
|
||||||
return timeSent;
|
return timeSent;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class HttpConnection implements Downloadable {
|
||||||
private Message message;
|
private Message message;
|
||||||
private DownloadableFile file;
|
private DownloadableFile file;
|
||||||
private int mStatus = Downloadable.STATUS_UNKNOWN;
|
private int mStatus = Downloadable.STATUS_UNKNOWN;
|
||||||
|
private boolean acceptedAutomatically = false;
|
||||||
|
|
||||||
public HttpConnection(HttpConnectionManager manager) {
|
public HttpConnection(HttpConnectionManager manager) {
|
||||||
this.mHttpConnectionManager = manager;
|
this.mHttpConnectionManager = manager;
|
||||||
|
@ -99,6 +100,9 @@ public class HttpConnection implements Downloadable {
|
||||||
message.setDownloadable(null);
|
message.setDownloadable(null);
|
||||||
mHttpConnectionManager.finishConnection(this);
|
mHttpConnectionManager.finishConnection(this);
|
||||||
mXmppConnectionService.updateConversationUi();
|
mXmppConnectionService.updateConversationUi();
|
||||||
|
if (acceptedAutomatically) {
|
||||||
|
mXmppConnectionService.getNotificationService().push(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void changeStatus(int status) {
|
private void changeStatus(int status) {
|
||||||
|
@ -151,6 +155,8 @@ public class HttpConnection implements Downloadable {
|
||||||
size = retrieveFileSize();
|
size = retrieveFileSize();
|
||||||
} catch (SSLHandshakeException e) {
|
} catch (SSLHandshakeException e) {
|
||||||
changeStatus(STATUS_OFFER_CHECK_FILESIZE);
|
changeStatus(STATUS_OFFER_CHECK_FILESIZE);
|
||||||
|
HttpConnection.this.acceptedAutomatically = false;
|
||||||
|
HttpConnection.this.mXmppConnectionService.getNotificationService().push(message);
|
||||||
return;
|
return;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
cancel();
|
cancel();
|
||||||
|
@ -158,9 +164,12 @@ public class HttpConnection implements Downloadable {
|
||||||
}
|
}
|
||||||
file.setExpectedSize(size);
|
file.setExpectedSize(size);
|
||||||
if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
|
if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
|
||||||
|
HttpConnection.this.acceptedAutomatically = true;
|
||||||
new Thread(new FileDownloader(interactive)).start();
|
new Thread(new FileDownloader(interactive)).start();
|
||||||
} else {
|
} else {
|
||||||
changeStatus(STATUS_OFFER);
|
changeStatus(STATUS_OFFER);
|
||||||
|
HttpConnection.this.acceptedAutomatically = false;
|
||||||
|
HttpConnection.this.mXmppConnectionService.getNotificationService().push(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -487,6 +487,7 @@ public class MessageParser extends AbstractParser implements
|
||||||
if (message.bodyContainsDownloadable()) {
|
if (message.bodyContainsDownloadable()) {
|
||||||
this.mXmppConnectionService.getHttpConnectionManager()
|
this.mXmppConnectionService.getHttpConnectionManager()
|
||||||
.createNewConnection(message);
|
.createNewConnection(message);
|
||||||
|
notify = false;
|
||||||
}
|
}
|
||||||
notify = notify && !conversation.isMuted();
|
notify = notify && !conversation.isMuted();
|
||||||
if (notify) {
|
if (notify) {
|
||||||
|
|
|
@ -13,14 +13,17 @@ import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.app.TaskStackBuilder;
|
import android.support.v4.app.TaskStackBuilder;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
|
|
||||||
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
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.ui.ConversationActivity;
|
import eu.siacs.conversations.ui.ConversationActivity;
|
||||||
|
|
||||||
|
@ -30,9 +33,10 @@ public class NotificationService {
|
||||||
|
|
||||||
private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
|
private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
|
||||||
|
|
||||||
public int NOTIFICATION_ID = 0x2342;
|
public static int NOTIFICATION_ID = 0x2342;
|
||||||
private Conversation mOpenConversation;
|
private Conversation mOpenConversation;
|
||||||
private boolean mIsInForeground;
|
private boolean mIsInForeground;
|
||||||
|
private long mLastNotification;
|
||||||
|
|
||||||
public NotificationService(XmppConnectionService service) {
|
public NotificationService(XmppConnectionService service) {
|
||||||
this.mXmppConnectionService = service;
|
this.mXmppConnectionService = service;
|
||||||
|
@ -58,7 +62,8 @@ public class NotificationService {
|
||||||
}
|
}
|
||||||
Account account = message.getConversation().getAccount();
|
Account account = message.getConversation().getAccount();
|
||||||
updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
|
updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
|
||||||
&& !account.inGracePeriod());
|
&& !account.inGracePeriod()
|
||||||
|
&& !this.inMiniGracePeriod(account));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -89,6 +94,9 @@ public class NotificationService {
|
||||||
if (notifications.size() == 0) {
|
if (notifications.size() == 0) {
|
||||||
notificationManager.cancel(NOTIFICATION_ID);
|
notificationManager.cancel(NOTIFICATION_ID);
|
||||||
} else {
|
} else {
|
||||||
|
if (notify) {
|
||||||
|
this.markLastNotification();
|
||||||
|
}
|
||||||
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
|
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
|
||||||
mXmppConnectionService);
|
mXmppConnectionService);
|
||||||
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
mBuilder.setSmallIcon(R.drawable.ic_notification);
|
||||||
|
@ -103,19 +111,17 @@ public class NotificationService {
|
||||||
mBuilder.setContentTitle(conversation.getName());
|
mBuilder.setContentTitle(conversation.getName());
|
||||||
StringBuilder text = new StringBuilder();
|
StringBuilder text = new StringBuilder();
|
||||||
for (int i = 0; i < messages.size(); ++i) {
|
for (int i = 0; i < messages.size(); ++i) {
|
||||||
text.append(messages.get(i).getReadableBody(
|
text.append(getReadableBody(messages.get(i)));
|
||||||
mXmppConnectionService));
|
|
||||||
if (i != messages.size() - 1) {
|
if (i != messages.size() - 1) {
|
||||||
text.append("\n");
|
text.append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
|
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
|
||||||
.bigText(text.toString()));
|
.bigText(text.toString()));
|
||||||
mBuilder.setContentText(messages.get(0).getReadableBody(
|
mBuilder.setContentText(getReadableBody(messages.get(0)));
|
||||||
mXmppConnectionService));
|
|
||||||
if (notify) {
|
if (notify) {
|
||||||
mBuilder.setTicker(messages.get(messages.size() - 1)
|
mBuilder.setTicker(getReadableBody(messages
|
||||||
.getReadableBody(mXmppConnectionService));
|
.get(messages.size() - 1)));
|
||||||
}
|
}
|
||||||
mBuilder.setContentIntent(createContentIntent(conversation
|
mBuilder.setContentIntent(createContentIntent(conversation
|
||||||
.getUuid()));
|
.getUuid()));
|
||||||
|
@ -135,11 +141,8 @@ public class NotificationService {
|
||||||
if (messages.size() > 0) {
|
if (messages.size() > 0) {
|
||||||
conversation = messages.get(0).getConversation();
|
conversation = messages.get(0).getConversation();
|
||||||
String name = conversation.getName();
|
String name = conversation.getName();
|
||||||
style.addLine(Html.fromHtml("<b>"
|
style.addLine(Html.fromHtml("<b>" + name + "</b> "
|
||||||
+ name
|
+ getReadableBody(messages.get(0))));
|
||||||
+ "</b> "
|
|
||||||
+ messages.get(0).getReadableBody(
|
|
||||||
mXmppConnectionService)));
|
|
||||||
names.append(name);
|
names.append(name);
|
||||||
names.append(", ");
|
names.append(", ");
|
||||||
}
|
}
|
||||||
|
@ -175,6 +178,26 @@ public class NotificationService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getReadableBody(Message message) {
|
||||||
|
if (message.getDownloadable() != null
|
||||||
|
&& (message.getDownloadable().getStatus() == Downloadable.STATUS_OFFER || message
|
||||||
|
.getDownloadable().getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE)) {
|
||||||
|
return mXmppConnectionService.getText(
|
||||||
|
R.string.image_offered_for_download).toString();
|
||||||
|
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
||||||
|
return mXmppConnectionService.getText(
|
||||||
|
R.string.encrypted_message_received).toString();
|
||||||
|
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
|
||||||
|
return mXmppConnectionService.getText(R.string.decryption_failed)
|
||||||
|
.toString();
|
||||||
|
} else if (message.getType() == Message.TYPE_IMAGE) {
|
||||||
|
return mXmppConnectionService.getText(R.string.image_file)
|
||||||
|
.toString();
|
||||||
|
} else {
|
||||||
|
return message.getBody().trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private PendingIntent createContentIntent(String conversationUuid) {
|
private PendingIntent createContentIntent(String conversationUuid) {
|
||||||
TaskStackBuilder stackBuilder = TaskStackBuilder
|
TaskStackBuilder stackBuilder = TaskStackBuilder
|
||||||
.create(mXmppConnectionService);
|
.create(mXmppConnectionService);
|
||||||
|
@ -234,4 +257,14 @@ public class NotificationService {
|
||||||
.getDisplayMetrics();
|
.getDisplayMetrics();
|
||||||
return ((int) (dp * metrics.density));
|
return ((int) (dp * metrics.density));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void markLastNotification() {
|
||||||
|
this.mLastNotification = SystemClock.elapsedRealtime();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean inMiniGracePeriod(Account account) {
|
||||||
|
int miniGrace = account.getStatus() == Account.STATUS_ONLINE ? Config.MINI_GRACE_PERIOD
|
||||||
|
: Config.MINI_GRACE_PERIOD * 2;
|
||||||
|
return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,14 +78,14 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> {
|
||||||
if (message.getType() == Message.TYPE_IMAGE
|
if (message.getType() == Message.TYPE_IMAGE
|
||||||
|| message.getDownloadable() != null) {
|
|| message.getDownloadable() != null) {
|
||||||
Downloadable d = message.getDownloadable();
|
Downloadable d = message.getDownloadable();
|
||||||
if (d != null) {
|
|
||||||
mLastMessage.setVisibility(View.VISIBLE);
|
|
||||||
imagePreview.setVisibility(View.GONE);
|
|
||||||
if (conversation.isRead()) {
|
if (conversation.isRead()) {
|
||||||
mLastMessage.setTypeface(null, Typeface.ITALIC);
|
mLastMessage.setTypeface(null, Typeface.ITALIC);
|
||||||
} else {
|
} else {
|
||||||
mLastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
|
mLastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
|
||||||
}
|
}
|
||||||
|
if (d != null) {
|
||||||
|
mLastMessage.setVisibility(View.VISIBLE);
|
||||||
|
imagePreview.setVisibility(View.GONE);
|
||||||
if (d.getStatus() == Downloadable.STATUS_CHECKING) {
|
if (d.getStatus() == Downloadable.STATUS_CHECKING) {
|
||||||
mLastMessage.setText(R.string.checking_image);
|
mLastMessage.setText(R.string.checking_image);
|
||||||
} else if (d.getStatus() == Downloadable.STATUS_DOWNLOADING) {
|
} else if (d.getStatus() == Downloadable.STATUS_DOWNLOADING) {
|
||||||
|
@ -99,6 +99,8 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> {
|
||||||
} else {
|
} else {
|
||||||
mLastMessage.setText("");
|
mLastMessage.setText("");
|
||||||
}
|
}
|
||||||
|
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
||||||
|
mLastMessage.setText(R.string.encrypted_message_received);
|
||||||
} else {
|
} else {
|
||||||
mLastMessage.setVisibility(View.GONE);
|
mLastMessage.setVisibility(View.GONE);
|
||||||
imagePreview.setVisibility(View.VISIBLE);
|
imagePreview.setVisibility(View.VISIBLE);
|
||||||
|
|
Loading…
Reference in New Issue