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 Map<String, String> mockMessage(String address, PersonRecord record) { Map<String, String> map = new HashMap<String, String>(); map.put(Telephony.TextBasedSmsColumns.ADDRESS, address); when(personLookup.lookupPerson(eq(address))).thenReturn(record); return map; }
private Map<String, String> mockCalllogMessage(String address, int type, PersonRecord record) { Map<String, String> map = new HashMap<String, String>(); map.put(CallLog.Calls.NUMBER, address); map.put(CallLog.Calls.TYPE, String.valueOf(type)); when(personLookup.lookupPerson(eq(address))).thenReturn(record); return map; } }
public void syncCalendar(ConversionResult result) { enableSync(); if (result.type != DataType.CALLLOG) return; for (Map<String, String> m : result.getMapList()) { try { final int duration = Integer.parseInt(m.get(CallLog.Calls.DURATION)); final int callType = Integer.parseInt(m.get(CallLog.Calls.TYPE)); final String number = m.get(CallLog.Calls.NUMBER); final Date then = new Date(Long.valueOf(m.get(CallLog.Calls.DATE))); final PersonRecord record = personLookup.lookupPerson(number); // insert into calendar calendarAccessor.addEntry( calendarId, then, duration, callFormatter.callTypeString(callType, record.getName()), callFormatter.formatForCalendar(callType, record.getNumber(), duration)); } catch (NumberFormatException e) { Log.w(TAG, "error", e); } } }
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 shouldIgnoreIllegalArgumentException() { // https://github.com/jberkel/sms-backup-plus/issues/870 when(resolver.query(any(Uri.class), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull())) .thenThrow(new IllegalArgumentException("column 'data1' does not exist")); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record.isUnknown()).isTrue(); }
values.put(CallLog.Calls.NEW, 0); PersonRecord record = personLookup.lookupPerson(Headers.get(message, Headers.ADDRESS)); if (!record.isUnknown()) { values.put(CallLog.Calls.CACHED_NAME, record.getName());
return null; PersonRecord record = personLookup.lookupPerson(address); if (!includePersonInBackup(record, DataType.CALLLOG)) return null;
@Test public void shouldLookupExistingPersonWithEmail() throws Exception { when(resolver.query( eq(Uri.parse("content://com.android.contacts/phone_lookup/1234")), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); when(resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Email.DATA }, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { String.valueOf(1) }, ContactsContract.CommonDataKinds.Email.IS_PRIMARY + " DESC")) .thenReturn(email("foo@test.com") ); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.getEmail()).isEqualTo("foo@test.com"); }
@Test public void shouldLookupExistingPersonUsingGmailAsPrimaryEmail() throws Exception { when(resolver.query( eq(Uri.parse("content://com.android.contacts/phone_lookup/1234")), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); when(resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Email.DATA }, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { String.valueOf(1) }, ContactsContract.CommonDataKinds.Email.IS_PRIMARY + " DESC")) .thenReturn(email("foo@test.com", "foo@gmail.com") ); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.getEmail()).isEqualTo("foo@gmail.com"); }
@Test public void shouldSyncCalendar() throws Exception { ConversionResult result = new ConversionResult(DataType.CALLLOG); final String NUMBER = "12345"; final String NAME = "Foo"; final int DURATION = 10; final int TYPE = 1; Date callTime = new Date(); result.add(new MimeMessage(), message(DURATION, TYPE, NUMBER, callTime)); result.add(new MimeMessage(), message(DURATION, TYPE, NUMBER, callTime)); when(callFormatter.callTypeString(TYPE, NAME)).thenReturn("title1"); when(callFormatter.formatForCalendar(TYPE, NUMBER, DURATION)).thenReturn("title2"); when(personLookup.lookupPerson(NUMBER)).thenReturn(new PersonRecord(1, NAME, "foo@bar", NUMBER)); syncer.syncCalendar(result); verify(accessor, times(2)).addEntry(eq(CALENDAR_ID), eq(callTime), eq(DURATION), eq("title1"), eq("title2")); }
@Test public void shouldLookupUnknownPerson() throws Exception { when(resolver.query(any(Uri.class), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull())).thenReturn(null); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.isUnknown()).isTrue(); assertThat(record.getEmail()).isEqualTo("1234@unknown.email"); assertThat(record.getName()).isEqualTo("1234"); }
@Test public void shouldLookupExistingPerson() throws Exception { when(resolver.query( eq(Uri.parse("content://com.android.contacts/phone_lookup/1234")), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.isUnknown()).isFalse(); assertThat(record.getEmail()).isEqualTo("1234@unknown.email"); assertThat(record.getName()).isEqualTo("Testor Test"); }
@Test public void testMessageToContentValuesWithCalllogFromUnknownPerson() throws Exception { PersonRecord record = new PersonRecord(-1, null, null, null); when(personLookup.lookupPerson("+12121")).thenReturn(record); final ContentValues values = messageConverter.messageToContentValues(createCallLogMessage()); assertThat(values.containsKey(CallLog.Calls.CACHED_NAME)).isFalse(); assertThat(values.containsKey(CallLog.Calls.CACHED_NUMBER_TYPE)).isFalse(); }
@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 testMessageToContentValuesWithCalllog() throws Exception { PersonRecord record = new PersonRecord(1, "The name", "email@foo.com", "+1234"); when(personLookup.lookupPerson("+12121")).thenReturn(record); final ContentValues values = messageConverter.messageToContentValues(createCallLogMessage()); assertThat(values.getAsString(CallLog.Calls.NUMBER)).isEqualTo("+12121"); assertThat(values.getAsString(CallLog.Calls.TYPE)).isEqualTo("3"); assertThat(values.getAsString(CallLog.Calls.DATE)).isEqualTo("1419163218194"); assertThat(values.getAsLong(CallLog.Calls.DURATION)).isEqualTo(44L); assertThat(values.getAsInteger(CallLog.Calls.NEW)).isEqualTo(0); assertThat(values.getAsString(CallLog.Calls.CACHED_NAME)).isEqualTo("The name"); assertThat(values.getAsInteger(CallLog.Calls.CACHED_NUMBER_TYPE)).isEqualTo(-2); }
@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(); }