private boolean updateLocked(){
  boolean isActive=false;
  long nextRetryTimeMillis=Long.MAX_VALUE;
  long now=systemFacade.currentTimeMillis();
  Collection<FileDownloadInfo> allDownloads=downloadsRepository.getAllDownloads();
  updateTotalBytesFor(allDownloads);
  List<DownloadBatch> downloadBatches=batchRepository.retrieveBatchesFor(allDownloads);
  for (  DownloadBatch downloadBatch : downloadBatches) {
    if (downloadBatch.isActive()) {
      isActive=true;
      break;
    }
  }
  for (  DownloadBatch downloadBatch : downloadBatches) {
    if (downloadBatch.isDeleted() || downloadBatch.prune(downloadDeleter)) {
      continue;
    }
    if (!isActive && downloadReadyChecker.canDownload(downloadBatch)) {
      boolean isBatchStartingForTheFirstTime=batchRepository.isBatchStartingForTheFirstTime(downloadBatch.getBatchId());
      if (isBatchStartingForTheFirstTime) {
        handleBatchStartingForTheFirstTime(downloadBatch);
      }
      downloadOrContinueBatch(downloadBatch.getDownloads());
      isActive=true;
    }
 else     if (downloadBatch.scanCompletedMediaIfReady(downloadScanner)) {
      isActive=true;
    }
    nextRetryTimeMillis=downloadBatch.nextActionMillis(now,nextRetryTimeMillis);
  }
  batchRepository.deleteMarkedBatchesFor(allDownloads);
  updateUserVisibleNotification(downloadBatches);
  if (nextRetryTimeMillis > 0 && nextRetryTimeMillis < Long.MAX_VALUE) {
    LLog.v("scheduling start in " + nextRetryTimeMillis + "ms");
    Intent intent=new Intent(Constants.ACTION_RETRY);
    intent.setClass(this,DownloadReceiver.class);
    alarmManager.set(AlarmManager.RTC_WAKEUP,now + nextRetryTimeMillis,PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_ONE_SHOT));
  }
  if (!isActive) {
    moveSubmittedTasksToBatchStatusIfNecessary();
  }
  return isActive;
}

private boolean updateLocked ( ) { boolean isActive = false ; long nextRetryTimeMillis = Long . MAX_VALUE ; long now = systemFacade . currentTimeMillis ( ) ; Collection < FileDownloadInfo > allDownloads = downloadsRepository . getAllDownloads ( ) ; updateTotalBytesFor ( allDownloads ) ; List < DownloadBatch > downloadBatches = batchRepository . retrieveBatchesFor ( allDownloads ) ; for ( DownloadBatch downloadBatch : downloadBatches ) { if ( downloadBatch . isActive ( ) ) { isActive = true ; break ; } } for ( DownloadBatch downloadBatch : downloadBatches ) { if ( downloadBatch . isDeleted ( ) || downloadBatch . prune ( downloadDeleter ) ) { continue ; } if ( ! isActive && downloadReadyChecker . canDownload ( downloadBatch ) ) { boolean isBatchStartingForTheFirstTime = batchRepository . isBatchStartingForTheFirstTime ( downloadBatch . getBatchId ( ) ) ; if ( isBatchStartingForTheFirstTime ) { handleBatchStartingForTheFirstTime ( downloadBatch ) ; } downloadOrContinueBatch ( downloadBatch . getDownloads ( ) ) ; isActive = true ; } else if ( downloadBatch . scanCompletedMediaIfReady ( downloadScanner ) ) { isActive = true ; } nextRetryTimeMillis = downloadBatch . nextActionMillis ( now , nextRetryTimeMillis ) ; } batchRepository . deleteMarkedBatchesFor ( allDownloads ) ; updateUserVisibleNotification ( downloadBatches ) ; if ( nextRetryTimeMillis > 0 && nextRetryTimeMillis < Long . MAX_VALUE ) { LLog . v ( "scheduling start in " + nextRetryTimeMillis + "ms" ) ; Intent intent = new Intent ( Constants . ACTION_RETRY ) ; intent . setClass ( this , DownloadReceiver . class ) ; alarmManager . set ( AlarmManager . RTC_WAKEUP , now + nextRetryTimeMillis , PendingIntent . getBroadcast ( this , 0 , intent , PendingIntent . FLAG_ONE_SHOT ) ) ; } if ( ! isActive ) { moveSubmittedTasksToBatchStatusIfNecessary ( ) ; } return isActive ; }
update { # downloads } to match { downloadprovider } state . depending on current download state it may enqueue { downloadtask } instances , request { downloadscanner } scans , update user - visible notifications , and / or schedule future actions with { alarmmanager } . < p / > should only be called from { # updatethread } as after being requested through { # enqueueupdate ( ) } . for ( downloadinfo info : downloadbatch . getdownloads ( ) ) { if ( info . isdeleted ) { snapshot taken in this update .
