private void importCallLog(final Message message) throws MessagingException, IOException { if (LOCAL_LOGV) Log.v(TAG, "importCallLog(" + message + ")"); final ContentValues values = converter.messageToContentValues(message); if (!callLogExists(values)) { final Uri uri = resolver.insert(Consts.CALLLOG_PROVIDER, values); if (uri != null) callLogIds.add(uri.getLastPathSegment()); } else { if (LOCAL_LOGV) Log.d(TAG, "ignoring call log"); } }
@Subscribe public void canceled(CancelEvent canceled) { cancel(canceled.mayInterruptIfRunning()); }
publishProgress(LOGIN); imapStore.checkSettings(); publishProgress(CALC); for (; currentRestoredItem < itemsToRestoreCount && !isCancelled(); currentRestoredItem++) { DataType dataType = importMessage(msgs.get(currentRestoredItem)); publishProgress(new RestoreState(RESTORE, currentRestoredItem, itemsToRestoreCount, 0, 0, dataType, null)); if (currentRestoredItem % 50 == 0) { updateAllThreadsIfAnySmsRestored(); } else { Log.d(TAG, "nothing to restore"); return new RestoreState(isCancelled() ? CANCELED_RESTORE : FINISHED_RESTORE, currentRestoredItem, itemsToRestoreCount, null, null); } catch (XOAuth2AuthenticationFailedException e) { return handleAuthError(config, currentRestoredItem, e); } catch (AuthenticationFailedException e) { return transition(SmsSyncState.ERROR, e); } catch (MessagingException e) { Log.e(TAG, ERROR, e); updateAllThreadsIfAnySmsRestored(); return transition(SmsSyncState.ERROR, e); } catch (IllegalStateException e) {
importCallLog(message); break; case SMS: importSms(message); break; default:
new RestoreTask(this, converter, getContentResolver(), new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences)).execute(config);
@Before public void before() throws MessagingException { initMocks(this); config = new RestoreConfig(store, 0, true, false, false, -1, 0); when(service.getApplicationContext()).thenReturn(RuntimeEnvironment.application); when(service.getState()).thenReturn(state); when(service.getPreferences()).thenReturn(new Preferences(RuntimeEnvironment.application)); when(store.getFolder(any(DataType.class), any(DataTypePreferences.class))).thenReturn(folder); task = new RestoreTask(service, converter, resolver, tokenRefresher); }
@Test public void shouldRestoreItems() throws Exception { Date now = new Date(); List<ImapMessage> messages = new ArrayList<ImapMessage>(); ContentValues values = new ContentValues(); values.put(Telephony.TextBasedSmsColumns.TYPE, Telephony.TextBasedSmsColumns.MESSAGE_TYPE_INBOX); values.put(Telephony.TextBasedSmsColumns.DATE, now.getTime()); ImapMessage mockMessage = mock(ImapMessage.class); when(mockMessage.getFolder()).thenReturn(folder); when(converter.getDataType(mockMessage)).thenReturn(DataType.SMS); when(converter.messageToContentValues(mockMessage)).thenReturn(values); messages.add(mockMessage); when(folder.getMessages(anyInt(), anyBoolean(), any(Date.class))).thenReturn(messages); when(resolver.insert(Consts.SMS_PROVIDER, values)).thenReturn(Uri.parse("content://sms/123")); task.doInBackground(config); verify(resolver).insert(Consts.SMS_PROVIDER, values); verify(resolver).delete(Uri.parse("content://sms/conversations/-1"), null, null); assertThat(service.getPreferences().getDataTypePreferences().getMaxSyncedDate(DataType.SMS)).isEqualTo(now.getTime()); assertThat(task.getSmsIds()).containsExactly("123"); verify(store).closeFolders(); } }
@Test public void shouldVerifyStoreSettings() throws Exception { task.doInBackground(config); verify(store).checkSettings(); }
@Test public void shouldCloseFolders() throws Exception { task.doInBackground(config); verify(store).closeFolders(); }
@Test public void shouldAcquireAndReleaseLocksDuringRestore() throws Exception { task.doInBackground(config); verify(service).acquireLocks(); verify(service).releaseLocks(); }