/** * Gets the most recent timestamp for given datatype. * @param dataType the data type * @return timestamp * @throws SecurityException if app does not hold necessary permissions */ public long getMostRecentTimestamp(DataType dataType) { return getMostRecentTimestampForQuery(queryBuilder.buildMostRecentQueryForDataType(dataType)); }
public @NonNull Cursor getItemsForDataType(DataType dataType, ContactGroupIds group, int max) { if (LOCAL_LOGV) Log.v(TAG, "getItemsForDataType(type=" + dataType + ", max=" + max + ")"); return performQuery(queryBuilder.buildQueryForDataType(dataType, group, max)); }
public @Nullable Query buildQueryForDataType(DataType type, @Nullable ContactGroupIds groupIds, int max) { switch (type) { case SMS: return getQueryForSMS(groupIds, max); case MMS: return getQueryForMMS(groupIds, max); case CALLLOG: return getQueryForCallLog(max); default: return null; } }
@Before public void before() { initMocks(this); when(dataTypePreferences.getMaxSyncedDate(any(DataType.class))).thenReturn(-1L); builder = new BackupQueryBuilder(dataTypePreferences); }
private Query getQueryForSMS(@Nullable ContactGroupIds groupIds, int max) { return new Query(Consts.SMS_PROVIDER, null, String.format(Locale.ENGLISH, "%s > ? AND %s <> ? %s", Telephony.TextBasedSmsColumns.DATE, Telephony.TextBasedSmsColumns.TYPE, groupSelection(SMS, groupIds)).trim(), new String[] { String.valueOf(preferences.getMaxSyncedDate(SMS)), String.valueOf(Telephony.TextBasedSmsColumns.MESSAGE_TYPE_DRAFT) }, max); }
BackupTask(@NonNull SmsBackupService service) { final Context context = service.getApplicationContext(); this.service = service; this.authPreferences = service.getAuthPreferences(); this.preferences = service.getPreferences(); this.fetcher = new BackupItemsFetcher( context.getContentResolver(), new BackupQueryBuilder(preferences.getDataTypePreferences())); PersonLookup personLookup = new PersonLookup(service.getContentResolver()); this.contactAccessor = new ContactAccessor(); this.converter = new MessageConverter(context, service.getPreferences(), authPreferences.getUserEmail(), personLookup, contactAccessor); if (preferences.isCallLogCalendarSyncEnabled()) { calendarSyncer = new CalendarSyncer( CalendarAccessor.Get.instance(service.getContentResolver()), preferences.getCallLogCalendarId(), personLookup, new CallFormatter(context.getResources()) ); } else { calendarSyncer = null; } this.tokenRefresher = new TokenRefresher(service, new OAuth2Client(authPreferences.getOAuth2ClientId()), authPreferences); }
private Query getQueryForMMS(@Nullable ContactGroupIds group, int max) { long maxSynced = preferences.getMaxSyncedDate(MMS); if (maxSynced > 0) { // NB: max synced date is stored in seconds since epoch in database maxSynced = (long) (maxSynced / 1000d); } return new Query( Consts.MMS_PROVIDER, null, String.format(Locale.ENGLISH, "%s > ? AND %s <> ? %s", Telephony.BaseMmsColumns.DATE, Telephony.BaseMmsColumns.MESSAGE_TYPE, groupSelection(DataType.MMS, group)).trim(), new String[] { String.valueOf(maxSynced), MmsConsts.DELIVERY_REPORT }, max); }
private void mockEmptyQuery() { BackupQueryBuilder.Query query = mock(BackupQueryBuilder.Query.class); when(queryBuilder.buildQueryForDataType(SMS, null, -1)).thenReturn(query); } }
private void mockMostRecentTimestampForType(DataType type, long max) { MatrixCursor cursor = new MatrixCursor(new String[]{"date"}); cursor.addRow(new Object[] { max }); BackupQueryBuilder.Query query = mock(BackupQueryBuilder.Query.class); when(queryBuilder.buildMostRecentQueryForDataType(type)).thenReturn(query); when(resolver.query(any(Uri.class), any(String[].class), any(String.class), any(String[].class), any(String.class))).thenReturn(cursor); }
@Test public void shouldBuildQueryForMMSWithSyncedDate() throws Exception { long nowInSecs = System.currentTimeMillis(); when(dataTypePreferences.getMaxSyncedDate(MMS)).thenReturn(nowInSecs); BackupQueryBuilder.Query query = builder.buildQueryForDataType(MMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content://mms")); assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND m_type <> ?"); assertThat(query.selectionArgs).asList().containsExactly(String.valueOf(nowInSecs / 1000L), "134"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildMostRecentQueryForMMS() throws Exception { BackupQueryBuilder.Query query = builder.buildMostRecentQueryForDataType(MMS); assertThat(query.uri).isEqualTo(Uri.parse("content://mms")); assertThat(query.projection).asList().containsExactly("date"); assertThat(query.selection).isNull(); assertThat(query.selectionArgs).isNull(); assertThat(query.sortOrder).isEqualTo("date DESC LIMIT 1"); }
@Test public void shouldBuildQueryForSMS() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(SMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content://sms")); assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND type <> ?"); assertThat(query.selectionArgs).asList().containsExactly("-1", "3"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildMostRecentQueryForCallLog() throws Exception { BackupQueryBuilder.Query query = builder.buildMostRecentQueryForDataType(CALLLOG); assertThat(query.uri).isEqualTo(Uri.parse("content://call_log/calls")); assertThat(query.projection).asList().containsExactly("date"); assertThat(query.selection).isNull(); assertThat(query.selectionArgs).isNull(); assertThat(query.sortOrder).isEqualTo("date DESC LIMIT 1"); } }
@Test public void shouldBuildQueryForMMS() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(MMS, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content://mms")); assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND m_type <> ?"); assertThat(query.selectionArgs).asList().containsExactly("-1", "134"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildMostRecentQueryForSMS() throws Exception { BackupQueryBuilder.Query query = builder.buildMostRecentQueryForDataType(SMS); assertThat(query.uri).isEqualTo(Uri.parse("content://sms")); assertThat(query.projection).asList().containsExactly("date"); assertThat(query.selection).isEqualTo("type <> ?"); assertThat(query.selectionArgs).asList().containsExactly("3"); assertThat(query.sortOrder).isEqualTo("date DESC LIMIT 1"); }
@Test public void shouldBuildQueryForCallLog() throws Exception { BackupQueryBuilder.Query query = builder.buildQueryForDataType(CALLLOG, null, 200); assertThat(query.uri).isEqualTo(Uri.parse("content://call_log/calls")); assertThat(query.projection).asList().containsExactly("_id", "number", "duration", "date", "type"); assertThat(query.selection).isEqualTo("date > ?"); assertThat(query.selectionArgs).asList().containsExactly("-1"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }
@Test public void shouldBuildQueryForSMSIncludingContactGroup() throws Exception { ContactGroupIds ids = new ContactGroupIds(); ids.add(1L, 20L); BackupQueryBuilder.Query query = builder.buildQueryForDataType(SMS, ids, 200); assertThat(query.uri).isEqualTo(Uri.parse("content://sms")); assertThat(query.projection).isNull(); assertThat(query.selection).isEqualTo("date > ? AND type <> ? AND (type = 2 OR person IN (20))"); assertThat(query.selectionArgs).asList().containsExactly("-1", "3"); assertThat(query.sortOrder).isEqualTo("date LIMIT 200"); }