use of com.fsck.k9.mail.Address in project sms-backup-plus by jberkel.
the class MessageConverter method messageToContentValues.
@NonNull
public ContentValues messageToContentValues(final Message message) throws IOException, MessagingException {
if (message == null)
throw new MessagingException("message is null");
final ContentValues values = new ContentValues();
switch(getDataType(message)) {
case SMS:
if (message.getBody() == null)
throw new MessagingException("body is null");
InputStream is = MimeUtility.decodeBody(message.getBody());
if (is == null) {
throw new MessagingException("body.getInputStream() is null for " + message.getBody());
}
final String body = IOUtils.toString(is);
final String address = Headers.get(message, Headers.ADDRESS);
values.put(Telephony.TextBasedSmsColumns.BODY, body);
values.put(Telephony.TextBasedSmsColumns.ADDRESS, address);
values.put(Telephony.TextBasedSmsColumns.TYPE, Headers.get(message, Headers.TYPE));
values.put(Telephony.TextBasedSmsColumns.PROTOCOL, Headers.get(message, Headers.PROTOCOL));
values.put(Telephony.TextBasedSmsColumns.SERVICE_CENTER, Headers.get(message, Headers.SERVICE_CENTER));
values.put(Telephony.TextBasedSmsColumns.DATE, Headers.get(message, Headers.DATE));
values.put(Telephony.TextBasedSmsColumns.STATUS, Headers.get(message, Headers.STATUS));
values.put(Telephony.TextBasedSmsColumns.THREAD_ID, threadHelper.getThreadId(mContext, address));
values.put(Telephony.TextBasedSmsColumns.READ, mMarkAsReadOnRestore ? "1" : Headers.get(message, Headers.READ));
break;
case CALLLOG:
values.put(CallLog.Calls.NUMBER, Headers.get(message, Headers.ADDRESS));
values.put(CallLog.Calls.TYPE, Integer.valueOf(Headers.get(message, Headers.TYPE)));
values.put(CallLog.Calls.DATE, Headers.get(message, Headers.DATE));
values.put(CallLog.Calls.DURATION, Long.valueOf(Headers.get(message, Headers.DURATION)));
values.put(CallLog.Calls.NEW, 0);
PersonRecord record = mPersonLookup.lookupPerson(Headers.get(message, Headers.ADDRESS));
if (!record.isUnknown()) {
values.put(CallLog.Calls.CACHED_NAME, record.getName());
values.put(CallLog.Calls.CACHED_NUMBER_TYPE, -2);
}
break;
default:
throw new MessagingException("don't know how to restore " + getDataType(message));
}
return values;
}
use of com.fsck.k9.mail.Address in project sms-backup-plus by jberkel.
the class MessageGenerator method messageFromMapSms.
@Nullable
private Message messageFromMapSms(Map<String, String> msgMap) throws MessagingException {
final String address = msgMap.get(Telephony.TextBasedSmsColumns.ADDRESS);
if (TextUtils.isEmpty(address))
return null;
PersonRecord record = personLookup.lookupPerson(address);
if (!includePersonInBackup(record, DataType.SMS))
return null;
final Message msg = new MimeMessage();
msg.setSubject(getSubject(DataType.SMS, record));
setBody(msg, new TextBody(msgMap.get(Telephony.TextBasedSmsColumns.BODY)));
final int messageType = toInt(msgMap.get(Telephony.TextBasedSmsColumns.TYPE));
if (Telephony.TextBasedSmsColumns.MESSAGE_TYPE_INBOX == messageType) {
// Received message
msg.setFrom(record.getAddress(addressStyle));
msg.setRecipient(Message.RecipientType.TO, userAddress);
} else {
// Sent message
msg.setRecipient(Message.RecipientType.TO, record.getAddress(addressStyle));
msg.setFrom(userAddress);
}
Date sentDate;
try {
// TODO: should probably be TextBasedSmsColumns.DATE_SENT
sentDate = new Date(Long.valueOf(msgMap.get(Telephony.TextBasedSmsColumns.DATE)));
} catch (NumberFormatException n) {
Log.e(TAG, ERROR_PARSING_DATE, n);
sentDate = new Date();
}
headerGenerator.setHeaders(msg, msgMap, DataType.SMS, address, record, sentDate, messageType);
return msg;
}
use of com.fsck.k9.mail.Address in project sms-backup-plus by jberkel.
the class MmsSupport method getDetails.
public MmsDetails getDetails(Uri mmsUri, AddressStyle style) {
Cursor cursor = resolver.query(Uri.withAppendedPath(mmsUri, "addr"), null, null, null, null);
// TODO: this is probably not the best way to determine if a message is inbound or outbound
boolean inbound = true;
final List<String> recipients = new ArrayList<String>();
while (cursor != null && cursor.moveToNext()) {
final String address = cursor.getString(cursor.getColumnIndex("address"));
// final int type = addresses.getInt(addresses.getColumnIndex("type"));
if (MmsConsts.INSERT_ADDRESS_TOKEN.equals(address)) {
inbound = false;
} else {
recipients.add(address);
}
}
if (cursor != null)
cursor.close();
List<PersonRecord> records = new ArrayList<PersonRecord>(recipients.size());
List<Address> addresses = new ArrayList<Address>(recipients.size());
if (!recipients.isEmpty()) {
for (String s : recipients) {
PersonRecord record = personLookup.lookupPerson(s);
records.add(record);
addresses.add(record.getAddress(style));
}
}
return new MmsDetails(inbound, recipients, records, addresses);
}
use of com.fsck.k9.mail.Address in project sms-backup-plus by jberkel.
the class MessageConverterTest method testConvertMessagesSeenFlagReadWithSMS.
@Test
public void testConvertMessagesSeenFlagReadWithSMS() throws Exception {
MatrixCursor cursor = new MatrixCursor(new String[] { Telephony.TextBasedSmsColumns.ADDRESS, Telephony.TextBasedSmsColumns.READ });
cursor.addRow(new Object[] { "foo", "0" });
cursor.addRow(new Object[] { "foo", "1" });
cursor.moveToFirst();
PersonRecord record = mock(PersonRecord.class);
when(personLookup.lookupPerson(any(String.class))).thenReturn(record);
when(record.getAddress(any(AddressStyle.class))).thenReturn(new Address("foo"));
when(preferences.getMarkAsReadType()).thenReturn(MarkAsReadTypes.READ);
messageConverter = new MessageConverter(RuntimeEnvironment.application, preferences, "foo@example.com", personLookup, contactAccessor);
ConversionResult res = messageConverter.convertMessages(cursor, DataType.SMS);
assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isTrue();
cursor.moveToNext();
res = messageConverter.convertMessages(cursor, DataType.SMS);
assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isTrue();
}
use of com.fsck.k9.mail.Address in project k-9 by k9mail.
the class RecipientLoader method fillContactDataFromAddresses.
private void fillContactDataFromAddresses(Address[] addresses, List<Recipient> recipients, Map<String, Recipient> recipientMap) {
for (Address address : addresses) {
if (isSupportedEmailAddress(address.getAddress())) {
// TODO actually query contacts - not sure if this is possible in a single query tho :(
Recipient recipient = new Recipient(address);
recipients.add(recipient);
recipientMap.put(address.getAddress(), recipient);
}
}
}
Aggregations