MessageConverter { public @NonNull 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(context, address)); values.put(Telephony.TextBasedSmsColumns.READ, markAsReadOnRestore ? "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 = personLookup.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; } MessageConverter(Context context,
                            Preferences preferences,
                            String userEmail,
                            PersonLookup personLookup,
                            ContactAccessor contactAccessor); @NonNull ConversionResult convertMessages(final Cursor cursor, DataType dataType); @NonNull ContentValues messageToContentValues(final Message message); DataType getDataType(Message message);  }
@Test(expected = MessagingException.class) public void testMessageToContentValuesWithNullMessageThrowsMessagingException() throws Exception { messageConverter.messageToContentValues(null); }
@Test(expected = MessagingException.class) public void testMessageToContentValuesWithUnknownMessageTypeThrowsException() throws Exception { final String message = "Subject: Call with +12121\n" + "From: +12121 <+12121@unknown.email>\n" + "To: test@example.com\n" + "MIME-Version: 1.0\n" + "Content-Type: text/plain;\n" + " charset=utf-8\n" + "References: <3j20u1wmbcyik9lw0yaf8bfc.+12121@sms-backup-plus.local>\n" + "Message-ID: <5c0e190205376da44656936fd7d9900c@sms-backup-plus.local>\n" + "X-smssync-datatype: INVALID\n" + "Content-Transfer-Encoding: quoted-printable\n" + "\n" + "Some Text"; final MimeMessage mimeMessage = MimeMessage.parseMimeMessage(new ByteArrayInputStream(message.getBytes()), true); messageConverter.messageToContentValues(mimeMessage); }
@Test public void testMessageToContentValuesWithSMS() throws Exception { final ContentValues values = messageConverter.messageToContentValues(createSMSMessage()); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.ADDRESS)).isEqualTo("+121332"); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.TYPE)).isEqualTo("2"); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.PROTOCOL)).isNull(); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.SERVICE_CENTER)).isNull(); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.DATE)).isEqualTo("1420759456762"); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.STATUS)).isEqualTo("-1"); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.THREAD_ID)).isNull(); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.READ)).isEqualTo("1"); assertThat(values.getAsString(Telephony.TextBasedSmsColumns.BODY)).isEqualTo("DasßAsß"); }
@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 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(); }