public @NonNull ConversionResult convertMessages(final Cursor cursor, DataType dataType) throws MessagingException { final Map<String, String> msgMap = getMessageMap(cursor); final Message m = messageGenerator.messageForDataType(msgMap, dataType); final ConversionResult result = new ConversionResult(dataType); if (m != null) { m.setFlag(Flag.SEEN, markAsSeen(dataType, msgMap)); result.add(m, msgMap); } return result; }
private @Nullable 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; }
public @Nullable Message messageForDataType(Map<String, String> msgMap, DataType dataType) throws MessagingException { switch (dataType) { case SMS: return messageFromMapSms(msgMap); case MMS: return messageFromMapMms(msgMap); case CALLLOG: return messageFromMapCallLog(msgMap); default: return null; } }
@Test public void shouldOnlyIncludePeopleFromContactIdsIfSpecified() throws Exception { MessageGenerator generator = new MessageGenerator(RuntimeEnvironment.application, me, AddressStyle.NAME, headerGenerator, personLookup, false, groupIds, mmsSupport, CallLogTypes.EVERYTHING, dataTypePreferences ); PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); map.put(Telephony.TextBasedSmsColumns.TYPE, "1"); when(groupIds.contains(record)).thenReturn(false); assertThat(generator.messageForDataType(map, DataType.SMS)).isNull(); when(groupIds.contains(record)).thenReturn(true); assertThat(generator.messageForDataType(map, DataType.SMS)).isNotNull(); }
Log.w(TAG, "no recipients found"); return null; } else if (!includeInBackup(DataType.MMS, details.records)) { Log.w(TAG, "no recipients included"); return null; msg.setSubject(getSubject(DataType.MMS, details.getRecipient())); sentDate = new Date(); final int msg_box = toInt(msgMap.get("msg_box")); headerGenerator.setHeaders(msg, msgMap, DataType.MMS, details.address, details.getRecipient(), sentDate, msg_box); MimeMultipart body = MimeMultipart.newInstance();
@Before public void before() { initMocks(this); me = new Address("mine@mine.com", "me"); generator = new MessageGenerator(RuntimeEnvironment.application, me, AddressStyle.NAME, headerGenerator, personLookup, false, null, mmsSupport, CallLogTypes.EVERYTHING, dataTypePreferences ); }
private boolean includeInBackup(DataType type, Iterable<PersonRecord> records) { for (PersonRecord r : records) { if (includePersonInBackup(r, type)) { return true; } } return false; }
public MessageConverter(Context context, Preferences preferences, String userEmail, PersonLookup personLookup, ContactAccessor contactAccessor) { this.context = context; markAsReadType = preferences.getMarkAsReadType(); this.personLookup = personLookup; markAsReadOnRestore = preferences.getMarkAsReadOnRestore(); String referenceUid = preferences.getReferenceUid(); if (referenceUid == null) { referenceUid = generateReferenceValue(); preferences.setReferenceUid(referenceUid); } final ContactGroup backupContactGroup = preferences.getBackupContactGroup(); ContactGroupIds allowedIds = contactAccessor.getGroupContactIds(context.getContentResolver(), backupContactGroup); if (LOCAL_LOGV) Log.v(TAG, "whitelisted ids for backup: " + allowedIds); messageGenerator = new MessageGenerator(context, new Address(userEmail), preferences.getEmailAddressStyle(), new HeaderGenerator(referenceUid, App.getVersionCode(context)), personLookup, preferences.getMailSubjectPrefix(), allowedIds, new MmsSupport(context.getContentResolver(), personLookup), preferences.getCallLogType(), preferences.getDataTypePreferences()); }
@Test public void testShouldReturnNullIfMessageHasNoAddress() throws Exception { Map<String, String> map = new HashMap<String, String>(); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNull(); }
private @Nullable Message messageFromMapCallLog(Map<String, String> msgMap) throws MessagingException { final String address = msgMap.get(CallLog.Calls.NUMBER); final int callType = toInt(msgMap.get(CallLog.Calls.TYPE)); if (!includePersonInBackup(record, DataType.CALLLOG)) return null; msg.setSubject(getSubject(DataType.CALLLOG, record)); toInt(msgMap.get(CallLog.Calls.DURATION));
@Test public void shouldGenerateCorrectHeaders() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); Date date = new Date(); map.put(Telephony.TextBasedSmsColumns.DATE, String.valueOf(date.getTime())); map.put(Telephony.TextBasedSmsColumns.TYPE, "0"); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNotNull(); verify(headerGenerator).setHeaders(any(Message.class), any(Map.class), eq(DataType.SMS), anyString(), eq(record), eq(date), eq(0)); }
@Test public void testShouldGenerateMMSMessageWithCorrectEncoding() throws Exception { PersonRecord personRecord = new PersonRecord(1, "Foo Bar", "foo@bar.com", "1234"); MmsSupport.MmsDetails details = new MmsSupport.MmsDetails(true, "foo", personRecord, new Address("foo@bar.com")); when(mmsSupport.getDetails(any(Uri.class), any(AddressStyle.class))).thenReturn(details); Message msg = generator.messageForDataType(mockMessage("1234", personRecord), DataType.MMS); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_7BIT }); }
@Test public void testShouldGenerateSubjectWithNameForMMS() throws Exception { PersonRecord personRecord = new PersonRecord(1, "Foo Bar", "foo@bar.com", "1234"); MmsSupport.MmsDetails details = new MmsSupport.MmsDetails(true, "foo", personRecord, new Address("foo@bar.com")); when(mmsSupport.getDetails(any(Uri.class), any(AddressStyle.class))).thenReturn(details); Message msg = generator.messageForDataType(mockMessage("1234", personRecord), DataType.MMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Foo Bar"); }
@Test public void testShouldGenerateSubjectWithNameForSMS() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Test Testor"); }
@Test public void testShouldUseNumberIfNameIsUnknown() throws Exception { PersonRecord record = new PersonRecord(-1, null, null, "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with 1234"); }
@Test public void testShouldGenerateSubjectWithNameAndNumberForSMS() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Test Testor"); }
@Test public void shouldGenerateCorrectFromHeaderWithUsersEmailAddress() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getFrom()[0]).isEqualTo(me); }
@Test public void testShouldGenerateCallLogMessageWithCorrectEncoding() throws Exception { PersonRecord record = new PersonRecord(-1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockCalllogMessage("1234", OUTGOING_TYPE, record), CALLLOG); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_QUOTED_PRINTABLE }); }
@Test public void shouldGenerateCorrectToHeaderWhenUserisRecipient() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); map.put(Telephony.TextBasedSmsColumns.TYPE, "1"); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getFrom()[0].toString()) .isEqualTo("Test Testor <test@test.com>"); assertThat(msg.getRecipients(Message.RecipientType.TO)[0]).isEqualTo(me); }
@Test public void testShouldGenerateSMSMessageWithCorrectEncoding() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_QUOTED_PRINTABLE }); }