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); }
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; }
@Test public void shouldGetAddressMissingNumber_Number() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", null); assertThat(record.getAddress(NUMBER).toString()).isEqualTo( "Unknown <john@appleseed.com>"); }
@Test public void shouldQuoteLocalPart_Name() throws Exception { PersonRecord record = new PersonRecord(1, null, null, "name with space"); assertThat(record.getAddress(NAME).toString()).isEqualTo( "name with space <\"name with space\"@unknown.email>"); }
@Test public void shouldGetAddressName() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NAME).toString()).isEqualTo( "John Appleseed <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingNumber_NameAndNumber() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", null); assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo( "\"John Appleseed (Unknown)\" <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingNumber_Name() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", null); assertThat(record.getAddress(NAME).toString()).isEqualTo( "John Appleseed <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingEmail_Number() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432"); assertThat(record.getAddress(NUMBER).toString()).isEqualTo( "+141543432 <+141543432@unknown.email>"); }
@Test public void shouldGetAddressMissingName_Name() throws Exception { PersonRecord record = new PersonRecord(1, null, "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NAME).toString()).isEqualTo( "+141543432 <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingName_NameAndNumber() throws Exception { PersonRecord record = new PersonRecord(1, null, "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo( "+141543432 <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingName_Number() throws Exception { PersonRecord record = new PersonRecord(1, null, "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NUMBER).toString()).isEqualTo( "+141543432 <john@appleseed.com>"); }
@Test public void shouldQuoteLocalPart_Number() throws Exception { PersonRecord record = new PersonRecord(1, null, null, "name with space"); assertThat(record.getAddress(NUMBER).toString()).isEqualTo( "name with space <\"name with space\"@unknown.email>"); } }
@Test public void shouldGetAddressNameAndNumber() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo( "\"John Appleseed (+141543432)\" <john@appleseed.com>"); }
@Test public void shouldGetAddressNumber() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", "john@appleseed.com", "+141543432"); assertThat(record.getAddress(NUMBER).toString()).isEqualTo( "+141543432 <john@appleseed.com>"); }
@Test public void shouldGetAddressMissingEmail_NameAndNumber() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432"); assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo( "\"John Appleseed (+141543432)\" <+141543432@unknown.email>"); }
@Test public void shouldGetAddressMissingEmail_Name() throws Exception { PersonRecord record = new PersonRecord(1, "John Appleseed", null, "+141543432"); assertThat(record.getAddress(NAME).toString()).isEqualTo( "John Appleseed <+141543432@unknown.email>"); }
@Test public void shouldQuoteLocalPart_NameAndNumber() throws Exception { PersonRecord record = new PersonRecord(1, null, null, "name with space"); assertThat(record.getAddress(NAME_AND_NUMBER).toString()).isEqualTo( "name with space <\"name with space\"@unknown.email>"); }
@Test public void testConvertMessagesSeenFlagFromMessageStatusWithSMS() 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.MESSAGE_STATUS); 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)).isFalse(); cursor.moveToNext(); res = messageConverter.convertMessages(cursor, DataType.SMS); assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isTrue(); }
@Test public void testConvertMessagesSeenFlagUnreadWithSMS() 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.UNREAD); 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)).isFalse(); cursor.moveToNext(); res = messageConverter.convertMessages(cursor, DataType.SMS); assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isFalse(); }
@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(); }