Skip to content

Commit

Permalink
Acquire a wake lock for each sent message/receipt
Browse files Browse the repository at this point in the history
There were cases where the OS didn't give enough time to confirm a
message because the device went back to sleep. This will ensure
that outgoing messages and receipts are correctly received by
the server.

Thanks to @webratte for helping me debugging this.

Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Nov 26, 2017
1 parent 7636553 commit cdd4e96
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void processStanza(Stanza packet) {

synchronized (waitingReceipt) {
String id = packet.getStanzaId();
Long _msgId = waitingReceipt.remove(id);
Long _msgId = waitingReceipt.get(id);
long msgId = (_msgId != null) ? _msgId : 0;
ContentResolver cr = getContext().getContentResolver();

Expand All @@ -71,8 +71,6 @@ public void processStanza(Stanza packet) {
values.put(Messages.STATUS, Messages.STATUS_CONFIRMED);
cr.update(ContentUris.withAppendedId(Messages.CONTENT_URI, msgId),
values, selectionIncoming, null);

waitingReceipt.remove(id);
}

if (msgId > 0) {
Expand Down Expand Up @@ -100,7 +98,9 @@ else if (id != null) {
cr.update(msg, values, selectionOutgoing, null);
}

// remove the packet from the waiting list
// this will also release the wake lock
waitingReceipt.remove(id);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.kontalk.service.msgcenter;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.lang.annotation.ElementType;
Expand All @@ -32,10 +32,8 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -78,6 +76,7 @@
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;
import org.spongycastle.openpgp.PGPException;

import android.accounts.Account;
import android.app.AlarmManager;
Expand Down Expand Up @@ -159,7 +158,7 @@
import org.kontalk.util.MessageUtils;
import org.kontalk.util.Preferences;
import org.kontalk.util.SystemUtils;
import org.spongycastle.openpgp.PGPException;
import org.kontalk.util.WakefulHashMap;

import static org.kontalk.ui.MessagingNotification.NOTIFICATION_ID_FOREGROUND;

Expand Down Expand Up @@ -509,7 +508,7 @@ protected void log(String logMessage, Throwable throwable) {
/**
* Messages waiting for server receipt (packetId: internalStorageId).
*/
Map<String, Long> mWaitingReceipt = new HashMap<>();
WakefulHashMap<String, Long> mWaitingReceipt;

private RegenerateKeyPairListener mKeyPairRegenerator;
private ImportKeyPairListener mKeyPairImporter;
Expand Down Expand Up @@ -767,6 +766,11 @@ public void onCreate() {
// this will trigger create/upgrade
mRosterStore.getWritableDatabase();

// waiting receipt list
// also used for keeping the device on while waiting for message delivery
mWaitingReceipt = new WakefulHashMap<>(this, PowerManager
.PARTIAL_WAKE_LOCK, Kontalk.TAG);

// create the global wake lock
PowerManager pwr = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pwr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Kontalk.TAG);
Expand Down
Loading

0 comments on commit cdd4e96

Please sign in to comment.