public DownloadService getDownloadService() {
   // If service is not available, request it to start and wait for it.
   for (int i = 0; i < 5; i++) {
     DownloadService downloadService = DownloadServiceImpl.getInstance();
     if (downloadService != null) {
       return downloadService;
     }
     Log.w(TAG, "DownloadService not running. Attempting to start it.");
     startService(new Intent(this, DownloadServiceImpl.class));
     Util.sleepQuietly(50L);
   }
   return DownloadServiceImpl.getInstance();
 }
Beispiel #2
0
  @Override
  protected void update() {
    starButton.setVisibility(
        (Util.isOffline(getContext()) || !song.isStarred()) ? View.GONE : View.VISIBLE);
    DownloadService downloadService = DownloadServiceImpl.getInstance();
    if (downloadService == null) {
      return;
    }

    DownloadFile downloadFile = downloadService.forSong(song);
    File partialFile = downloadFile.getPartialFile();

    int leftImage = 0;
    int rightImage = 0;

    if (downloadFile.isWorkDone()) {
      leftImage = downloadFile.shouldSave() ? R.drawable.saved : R.drawable.downloaded;
    }

    if (downloadFile.isDownloading()
        && !downloadFile.isDownloadCancelled()
        && partialFile.exists()) {
      statusTextView.setText(Util.formatLocalizedBytes(partialFile.length(), getContext()));
      rightImage = R.drawable.downloading;
    } else {
      statusTextView.setText(null);
    }
    statusTextView.setCompoundDrawablesWithIntrinsicBounds(leftImage, 0, rightImage, 0);

    boolean playing = downloadService.getCurrentPlaying() == downloadFile;
    if (playing) {
      titleTextView.setCompoundDrawablesWithIntrinsicBounds(
          R.drawable.stat_notify_playing, 0, 0, 0);
    } else {
      titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    }
  }