public static Status getStatus(Account account, String authority) { return getStatus(account, authority, false); }
@Implementation protected static boolean isSyncActive(Account account, String authority) { ShadowContentResolver.Status status = getStatus(account, authority); // TODO: this means a sync is *perpetually* active after one request return status != null && status.syncRequests > 0; }
@Implementation protected static List<PeriodicSync> getPeriodicSyncs(Account account, String authority) { return getStatus(account, authority, true).syncs; }
@Implementation protected static void setIsSyncable(Account account, String authority, int syncable) { getStatus(account, authority, true).state = syncable; }
@Implementation protected static void setSyncAutomatically(Account account, String authority, boolean sync) { getStatus(account, authority, true).syncAutomatically = sync; }
@Implementation protected static int getIsSyncable(Account account, String authority) { return getStatus(account, authority, true).state; }
@Implementation protected static boolean getSyncAutomatically(Account account, String authority) { return getStatus(account, authority, true).syncAutomatically; }
@Implementation protected static void removePeriodicSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority); if (status != null) { for (int i = 0; i < status.syncs.size(); ++i) { if (isBundleEqual(extras, status.syncs.get(i).extras)) { status.syncs.remove(i); break; } } } }
@Implementation protected static void cancelSync(Account account, String authority) { Status status = getStatus(account, authority); if (status != null) { status.syncRequests = 0; if (status.syncExtras != null) { status.syncExtras.clear(); } // This may be too much, as the above should be sufficient. if (status.syncs != null) { status.syncs.clear(); } } }
@Implementation protected static void requestSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority, true); status.syncRequests++; status.syncExtras = extras; }
@Implementation protected static void addPeriodicSync( Account account, String authority, Bundle extras, long pollFrequency) { validateSyncExtrasBundle(extras); removePeriodicSync(account, authority, extras); getStatus(account, authority, true) .syncs .add(new PeriodicSync(account, authority, extras, pollFrequency)); }
private Bundle getLastSyncExtras() { return ShadowContentResolver.getStatus(createSyncAccount(), SyncContentProvider.PROVIDER_AUTHORITY).syncExtras; }
@Test public void testSyncDisabled() { PreferenceManager.getDefaultSharedPreferences(service) .edit().clear().apply(); syncScheduler.scheduleSync(service, "1"); assertNull(ShadowContentResolver.getStatus(createSyncAccount(), SyncContentProvider.PROVIDER_AUTHORITY)); }
@Test public void shouldKeepTrackOfSyncRequests() { ShadowContentResolver.Status status = ShadowContentResolver.getStatus(a, AUTHORITY, true); assertThat(status).isNotNull(); assertThat(status.syncRequests).isEqualTo(0); ContentResolver.requestSync(a, AUTHORITY, new Bundle()); assertThat(status.syncRequests).isEqualTo(1); assertThat(status.syncExtras).isNotNull(); }
@Test public void testSyncDeferred() throws IOException { Call<HackerNewsItem> call = mock(Call.class); when(call.execute()).thenThrow(IOException.class); when(TestRestServiceFactory.hnRestService.cachedItem(any())).thenReturn(call); when(TestRestServiceFactory.hnRestService.networkItem(any())).thenReturn(call); syncPreferences.edit().putBoolean("1", true).putBoolean("2", true).apply(); syncScheduler.scheduleSync(service, null); adapter.onPerformSync(mock(Account.class), getLastSyncExtras(), null, null, null); ShadowContentResolver.Status syncStatus = ShadowContentResolver.getStatus( new Account("Materialistic", BuildConfig.APPLICATION_ID), SyncContentProvider.PROVIDER_AUTHORITY); assertThat(syncStatus.syncRequests).isEqualTo(3); // original + 2 deferred }
@Implementation public static boolean isSyncActive(Account account, String authority) { ShadowContentResolver.Status status = getStatus(account, authority); // TODO: this means a sync is *perpetually* active after one request return status != null && status.syncRequests > 0; }
@Implementation public static int getIsSyncable(Account account, String authority) { return getStatus(account, authority, true).state; }
@Implementation public static boolean isSyncActive(Account account, String authority) { ShadowContentResolver.Status status = getStatus(account, authority); // TODO: this means a sync is *perpetually* active after one request return status != null && status.syncRequests > 0; }
@Implementation public static void removePeriodicSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority); if (status != null) { for (int i = 0; i < status.syncs.size(); ++i) { if (isBundleEqual(extras, status.syncs.get(i).extras)) { status.syncs.remove(i); break; } } } }
@Implementation public static void addPeriodicSync(Account account, String authority, Bundle extras, long pollFrequency) { validateSyncExtrasBundle(extras); removePeriodicSync(account, authority, extras); getStatus(account, authority, true).syncs.add(new PeriodicSync(account, authority, extras, pollFrequency)); }