ScannerService extends MortarIntentService { @Override @DebugLog protected void onHandleIntent(Intent intent) { if (!ConnectionUtils.hasInternetConnection(this)) { mNotifHelper.showNoConnection(); return; } status.set(Status.SCANNING); final Subscription notifSubs = Observable.interval(1, 5, TimeUnit.SECONDS, AndroidSchedulers.mainThread()) .subscribe(new Action1<Long>() { @Override public void call(Long aLong) { mNotifHelper.updateNotification(true); } }); if (ACTION_RESCAN.equals(intent.getAction())) { if (intent.hasExtra(EXTRA_LIBRARY_EXTRAS)) { Bundleable b = LibraryExtras.getBundleable(intent.getBundleExtra(EXTRA_LIBRARY_EXTRAS)); if (b != null && (b instanceof Container)) { Container c = (Container)b; scan(c.getUri(), c.getParentUri()); } } else { String authority = intent.getStringExtra(EXTRA_AUTHORITY); List<Pair<Uri, Uri>> topLevel = mIndexDatabase.findTopLevelContainers(authority); for (Pair<Uri,Uri> p : topLevel) { scan(p.first, p.second); } } } else { Bundle extras = intent.getBundleExtra(EXTRA_LIBRARY_EXTRAS); if (extras != null) { Container container = LibraryExtras.getBundleable(extras); if (container != null) { scan(container.getUri(), container.getParentUri()); } else { Timber.e("No container in extras"); } } else { Timber.e("No extras in intent"); } } notifSubs.unsubscribe(); status.set(Status.COMPLETED); stopForeground(true); } ScannerService(); @Override void onCreate(); @Override void onDestroy(); static final String ACTION_RESCAN; static final String ACTION_CONNECTION_RESTORED; static final String EXTRA_AUTHORITY; static final String EXTRA_LIBRARY_EXTRAS; }
@Test public void testScanContainer() throws Exception { Container c = Folder.builder() .setUri(TestData.URI_FOLDER1) .setParentUri(Uri.parse("content: .setName("folder1") .build(); Intent i = new Intent().putExtra(ScannerService.EXTRA_LIBRARY_EXTRAS, LibraryExtras.b() .putBundleable(c).get()); scannerService.onHandleIntent(i); Assertions.assertThat(indexDatabase.findTopLevelContainers(null).size()).isEqualTo(1); Assertions.assertThat(scannerService.numTotal.get()).isEqualTo(5); Assertions.assertThat(scannerService.numProcessed.get()).isEqualTo(5); Assertions.assertThat(scannerService.numError.get()).isEqualTo(0); List<Track> insertedTracks = indexDatabase.getTracks(null, null); Assertions.assertThat(insertedTracks.size()).isEqualTo(5); }