Log calls to conversations

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2015-09-21 10:54:23 -07:00
parent 36b7f767b6
commit 26979b1c62
21 changed files with 189 additions and 67 deletions

View File

@@ -164,9 +164,9 @@ public class ConversationAdapter <V extends View & BindableConversationItem>
String type = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsDatabase.TRANSPORT));
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
if (messageRecord.isGroupAction()) return MESSAGE_TYPE_UPDATE;
else if (messageRecord.isOutgoing()) return MESSAGE_TYPE_OUTGOING;
else return MESSAGE_TYPE_INCOMING;
if (messageRecord.isGroupAction() || messageRecord.isCallLog()) return MESSAGE_TYPE_UPDATE;
else if (messageRecord.isOutgoing()) return MESSAGE_TYPE_OUTGOING;
else return MESSAGE_TYPE_INCOMING;
}
private MessageRecord getMessageRecord(long messageId, Cursor cursor, String type) {

View File

@@ -13,6 +13,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.model.MessageRecord;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.Util;
@@ -26,8 +27,10 @@ public class ConversationUpdateItem extends LinearLayout
private ImageView icon;
private TextView body;
private TextView date;
private Recipient sender;
private MessageRecord messageRecord;
private Locale locale;
public ConversationUpdateItem(Context context) {
super(context);
@@ -43,6 +46,7 @@ public class ConversationUpdateItem extends LinearLayout
this.icon = (ImageView)findViewById(R.id.conversation_update_icon);
this.body = (TextView)findViewById(R.id.conversation_update_body);
this.date = (TextView)findViewById(R.id.conversation_update_date);
setOnClickListener(this);
}
@@ -54,28 +58,45 @@ public class ConversationUpdateItem extends LinearLayout
@NonNull Set<MessageRecord> batchSelected,
boolean groupThread, boolean pushDestination)
{
bind(messageRecord);
bind(messageRecord, locale);
}
private void bind(@NonNull MessageRecord messageRecord) {
private void bind(@NonNull MessageRecord messageRecord, @NonNull Locale locale) {
this.messageRecord = messageRecord;
this.sender = messageRecord.getIndividualRecipient();
this.locale = locale;
this.sender.addListener(this);
if (messageRecord.isGroupAction()) {
icon.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_group_grey600_24dp));
if (messageRecord.isGroupAction()) setGroupRecord(messageRecord);
else if (messageRecord.isCallLog()) setCallRecord(messageRecord);
else throw new AssertionError("Neither group no log.");
}
if (messageRecord.isGroupQuit() && messageRecord.isOutgoing()) {
body.setText(R.string.MessageRecord_left_group);
} else if (messageRecord.isGroupQuit()) {
body.setText(getContext().getString(R.string.ConversationItem_group_action_left, sender.toShortString()));
} else {
GroupUtil.GroupDescription description = GroupUtil.getDescription(getContext(), messageRecord.getBody().getBody());
description.addListener(this);
body.setText(description.toString());
}
private void setCallRecord(MessageRecord messageRecord) {
if (messageRecord.isIncomingCall()) icon.setImageResource(R.drawable.ic_call_received_grey600_24dp);
else if (messageRecord.isOutgoingCall()) icon.setImageResource(R.drawable.ic_call_made_grey600_24dp);
else icon.setImageResource(R.drawable.ic_call_missed_grey600_24dp);
body.setText(messageRecord.getDisplayBody());
date.setText(DateUtils.getExtendedRelativeTimeSpanString(getContext(), locale, messageRecord.getDateReceived()));
date.setVisibility(View.VISIBLE);
}
private void setGroupRecord(MessageRecord messageRecord) {
icon.setImageResource(R.drawable.ic_group_grey600_24dp);
if (messageRecord.isGroupQuit() && messageRecord.isOutgoing()) {
body.setText(R.string.MessageRecord_left_group);
} else if (messageRecord.isGroupQuit()) {
body.setText(getContext().getString(R.string.ConversationItem_group_action_left, sender.toShortString()));
} else {
GroupUtil.GroupDescription description = GroupUtil.getDescription(getContext(), messageRecord.getBody().getBody());
description.addListener(this);
body.setText(description.toString());
}
date.setVisibility(View.GONE);
}
@Override
@@ -88,7 +109,7 @@ public class ConversationUpdateItem extends LinearLayout
Util.runOnMain(new Runnable() {
@Override
public void run() {
bind(messageRecord);
bind(messageRecord, locale);
}
});
}

View File

@@ -19,6 +19,10 @@ public interface MmsSmsColumns {
// Base Types
protected static final long BASE_TYPE_MASK = 0x1F;
protected static final long INCOMING_CALL_TYPE = 1;
protected static final long OUTGOING_CALL_TYPE = 2;
protected static final long MISSED_CALL_TYPE = 3;
protected static final long BASE_INBOX_TYPE = 20;
protected static final long BASE_OUTBOX_TYPE = 21;
protected static final long BASE_SENDING_TYPE = 22;
@@ -150,6 +154,22 @@ public interface MmsSmsColumns {
return (type & KEY_EXCHANGE_IDENTITY_UPDATE_BIT) != 0;
}
public static boolean isCallLog(long type) {
return type == INCOMING_CALL_TYPE || type == OUTGOING_CALL_TYPE || type == MISSED_CALL_TYPE;
}
public static boolean isIncomingCall(long type) {
return type == INCOMING_CALL_TYPE;
}
public static boolean isOutgoingCall(long type) {
return type == OUTGOING_CALL_TYPE;
}
public static boolean isMissedCall(long type) {
return type == MISSED_CALL_TYPE;
}
public static boolean isGroupUpdate(long type) {
return (type & GROUP_UPDATE_BIT) != 0;
}

View File

@@ -22,6 +22,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.support.annotation.NonNull;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.util.Log;
@@ -343,6 +344,45 @@ public class SmsDatabase extends MessagingDatabase {
return new Pair<>(newMessageId, record.getThreadId());
}
public @NonNull Pair<Long, Long> insertReceivedCall(@NonNull String number) {
return insertCallLog(number, Types.INCOMING_CALL_TYPE, false);
}
public @NonNull Pair<Long, Long> insertOutgoingCall(@NonNull String number) {
return insertCallLog(number, Types.OUTGOING_CALL_TYPE, false);
}
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull String number) {
return insertCallLog(number, Types.MISSED_CALL_TYPE, true);
}
private @NonNull Pair<Long, Long> insertCallLog(@NonNull String number, long type, boolean unread) {
Recipients recipients = RecipientFactory.getRecipientsFromString(context, number, true);
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
ContentValues values = new ContentValues(6);
values.put(ADDRESS, number);
values.put(ADDRESS_DEVICE_ID, 1);
values.put(DATE_RECEIVED, System.currentTimeMillis());
values.put(DATE_SENT, System.currentTimeMillis());
values.put(READ, unread ? 0 : 1);
values.put(TYPE, type);
values.put(THREAD_ID, threadId);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
long messageId = db.insert(TABLE_NAME, null, values);
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
jobManager.add(new TrimThreadJob(context, threadId));
if (unread) {
DatabaseFactory.getThreadDatabase(context).setUnread(threadId);
}
return new Pair<>(messageId, threadId);
}
protected Pair<Long, Long> insertMessageInbox(IncomingTextMessage message, long type) {
if (message.isPreKeyBundle()) {
type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;

View File

@@ -95,6 +95,22 @@ public abstract class DisplayRecord {
return isGroupUpdate() || isGroupQuit();
}
public boolean isCallLog() {
return SmsDatabase.Types.isCallLog(type);
}
public boolean isIncomingCall() {
return SmsDatabase.Types.isIncomingCall(type);
}
public boolean isOutgoingCall() {
return SmsDatabase.Types.isOutgoingCall(type);
}
public boolean isMissedCall() {
return SmsDatabase.Types.isMissedCall(type);
}
public static class Body {
private final String body;
private final boolean plaintext;

View File

@@ -115,6 +115,12 @@ public abstract class MessageRecord extends DisplayRecord {
return emphasisAdded(context.getString(R.string.MessageRecord_left_group));
} else if (isGroupQuit()) {
return emphasisAdded(context.getString(R.string.ConversationItem_group_action_left, getIndividualRecipient().toShortString()));
} else if (isIncomingCall()) {
return emphasisAdded(String.format("%s called you", getIndividualRecipient().toShortString()));
} else if (isOutgoingCall()) {
return emphasisAdded(String.format("Called %s", getIndividualRecipient().toShortString()));
} else if (isMissedCall()) {
return emphasisAdded(String.format("Missed call from %s", getIndividualRecipient().toShortString()));
} else if (getBody().getBody().length() > MAX_DISPLAY_LENGTH) {
return new SpannableString(getBody().getBody().substring(0, MAX_DISPLAY_LENGTH));
}