Skip to content

Commit

Permalink
Fix timestamp of missed call record.
Browse files Browse the repository at this point in the history
Fixes #7647
  • Loading branch information
cascheberg authored and greyson-signal committed Sep 23, 2020
1 parent 18957b1 commit 15ee8c6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void setCallRecord(MessageRecord messageRecord) {
else if (messageRecord.isOutgoingCall()) icon.setImageResource(R.drawable.ic_call_made_grey600_24dp);
else icon.setImageResource(R.drawable.ic_call_missed_grey600_24dp);

date.setText(DateUtils.getExtendedRelativeTimeSpanString(getContext(), locale, messageRecord.getDateReceived()));
date.setText(DateUtils.getExtendedRelativeTimeSpanString(getContext(), locale, messageRecord.getDateSent()));

title.setVisibility(GONE);
date.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public MessageDatabase(Context context, SQLCipherOpenHelper databaseHelper) {

public abstract @NonNull Pair<Long, Long> insertReceivedCall(@NonNull RecipientId address);
public abstract @NonNull Pair<Long, Long> insertOutgoingCall(@NonNull RecipientId address);
public abstract @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address);
public abstract @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address, long timestamp);

public abstract Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type);
public abstract Optional<InsertResult> insertMessageInbox(IncomingTextMessage message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public Pair<Long, Long> updateBundleMessageBody(long messageId, String body) {
}

@Override
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address) {
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address, long timestamp) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,28 +644,28 @@ public boolean hasReceivedAnyCallsSince(long threadId, long timestamp) {

@Override
public @NonNull Pair<Long, Long> insertReceivedCall(@NonNull RecipientId address) {
return insertCallLog(address, Types.INCOMING_CALL_TYPE, false);
return insertCallLog(address, Types.INCOMING_CALL_TYPE, false, System.currentTimeMillis());
}

@Override
public @NonNull Pair<Long, Long> insertOutgoingCall(@NonNull RecipientId address) {
return insertCallLog(address, Types.OUTGOING_CALL_TYPE, false);
return insertCallLog(address, Types.OUTGOING_CALL_TYPE, false, System.currentTimeMillis());
}

@Override
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address) {
return insertCallLog(address, Types.MISSED_CALL_TYPE, true);
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull RecipientId address, long timestamp) {
return insertCallLog(address, Types.MISSED_CALL_TYPE, true, timestamp);
}

private @NonNull Pair<Long, Long> insertCallLog(@NonNull RecipientId recipientId, long type, boolean unread) {
private @NonNull Pair<Long, Long> insertCallLog(@NonNull RecipientId recipientId, long type, boolean unread, long timestamp) {
Recipient recipient = Recipient.resolved(recipientId);
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);

ContentValues values = new ContentValues(6);
values.put(RECIPIENT_ID, recipientId.serialize());
values.put(ADDRESS_DEVICE_ID, 1);
values.put(DATE_RECEIVED, System.currentTimeMillis());
values.put(DATE_SENT, System.currentTimeMillis());
values.put(DATE_SENT, timestamp);
values.put(READ, unread ? 0 : 1);
values.put(TYPE, type);
values.put(THREAD_ID, threadId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public boolean isPush() {
}

public long getTimestamp() {
if (isPush() && getDateSent() < getDateReceived()) {
if ((isPush() || isCallLog()) && getDateSent() < getDateReceived()) {
return getDateSent();
}
return getDateReceived();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -202,6 +201,7 @@ public class WebRtcCallService extends Service implements CallManager.Observer,
@Nullable private RemotePeer busyPeer;
@Nullable private RemotePeer preJoinPeer;
@Nullable private SparseArray<RemotePeer> peerMap;
private long callStartTimestamp;

@Nullable private EglBase eglBase;
@Nullable private BroadcastVideoSink localSink;
Expand Down Expand Up @@ -422,7 +422,7 @@ private void handleReceivedOffer(Intent intent) {
Log.i(TAG, "PSTN line is busy.");
intent.putExtra(EXTRA_BROADCAST, true);
handleSendBusy(intent);
insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, serverReceivedTimestamp);
return;
}

Expand All @@ -431,11 +431,12 @@ private void handleReceivedOffer(Intent intent) {
intent.putExtra(EXTRA_BROADCAST, true);
intent.putExtra(EXTRA_HANGUP_TYPE, HangupMessage.Type.NEED_PERMISSION.getCode());
handleSendHangup(intent);
insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, serverReceivedTimestamp);
return;
}

peerMap.append(remotePeer.hashCode(), remotePeer);
callStartTimestamp = serverReceivedTimestamp;

This comment has been minimized.

Copy link
@cascheberg

cascheberg Sep 24, 2020

Author Contributor

In my original PR #10025 I had added the timestamp to the map to avoid race conditions in case of multiple missed calls, see #10036.

Log.i(TAG, "add remotePeer callId: " + remotePeer.getCallId() + " key: " + remotePeer.hashCode());

isRemoteVideoOffer = offerType == OfferMessage.Type.VIDEO_CALL;
Expand Down Expand Up @@ -523,6 +524,7 @@ private void handleOutgoingCall(Intent intent) {
EventBus.getDefault().removeStickyEvent(WebRtcViewModel.class);

peerMap.append(remotePeer.hashCode(), remotePeer);
callStartTimestamp = System.currentTimeMillis();
Log.i(TAG, "add remotePeer callId: " + remotePeer.getCallId() + " key: " + remotePeer.hashCode());

initializeVideo();
Expand Down Expand Up @@ -552,8 +554,8 @@ private void handleIsInCallQuery(Intent intent) {
}
}

private void insertMissedCall(@NonNull RemotePeer remotePeer, boolean signal) {
Pair<Long, Long> messageAndThreadId = DatabaseFactory.getSmsDatabase(this).insertMissedCall(remotePeer.getId());
private void insertMissedCall(@NonNull RemotePeer remotePeer, boolean signal, long timestamp) {
Pair<Long, Long> messageAndThreadId = DatabaseFactory.getSmsDatabase(this).insertMissedCall(remotePeer.getId(), timestamp);
ApplicationDependencies.getMessageNotifier().updateNotification(this, messageAndThreadId.second(), signal);
}

Expand All @@ -572,7 +574,7 @@ private void handleDenyCall(Intent intent) {

try {
callManager.hangup();
DatabaseFactory.getSmsDatabase(this).insertMissedCall(activePeer.getId());
DatabaseFactory.getSmsDatabase(this).insertMissedCall(activePeer.getId(), System.currentTimeMillis());
terminate(activePeer);
} catch (CallException e) {
callFailure("hangup() failed: ", e);
Expand Down Expand Up @@ -1166,7 +1168,7 @@ private void handleReceivedOfferExpired(Intent intent) {

Log.i(TAG, "handleReceivedOfferExpired(): call_id: " + remotePeer.getCallId());

insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, callStartTimestamp);

terminate(remotePeer);
}
Expand Down Expand Up @@ -1195,7 +1197,7 @@ private void handleReceivedOfferWhileActive(Intent intent) {
stopForeground(true);
}

insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, callStartTimestamp);

terminate(remotePeer);
}
Expand All @@ -1216,7 +1218,7 @@ private void handleEndedRemoteHangup(Intent intent) {

boolean incomingBeforeAccept = remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING;
if (incomingBeforeAccept) {
insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, callStartTimestamp);
}

terminate(remotePeer);
Expand Down Expand Up @@ -1303,7 +1305,7 @@ private void handleEndedRemoteGlare(Intent intent) {

boolean incomingBeforeAccept = remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING;
if (incomingBeforeAccept) {
insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, callStartTimestamp);
}

terminate(remotePeer);
Expand All @@ -1319,7 +1321,7 @@ private void handleEndedFailure(Intent intent) {
}

if (remotePeer.getState() == CallState.ANSWERING || remotePeer.getState() == CallState.LOCAL_RINGING) {
insertMissedCall(remotePeer, true);
insertMissedCall(remotePeer, true, callStartTimestamp);
}

terminate(remotePeer);
Expand Down Expand Up @@ -1383,7 +1385,7 @@ private void initializeVideo() {
camera.setEnabled(false);
camera.dispose();
}

camera = new Camera(WebRtcCallService.this, WebRtcCallService.this, eglBase, localCameraState.getActiveDirection());
localCameraState = camera.getCameraState();
});
Expand Down

0 comments on commit 15ee8c6

Please sign in to comment.