Пример #1
0
 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();
 }
Пример #2
0
  public static void showPlayingNotification(
      final Context context,
      final DownloadServiceImpl downloadService,
      Handler handler,
      MusicDirectory.Entry song) {
    // Set the icon, scrolling text and timestamp
    final Notification notification =
        new Notification(
            R.drawable.stat_notify_playing, song.getTitle(), System.currentTimeMillis());
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

    boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      RemoteViews expandedContentView =
          new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
      setupViews(expandedContentView, context, song, playing);
      notification.bigContentView = expandedContentView;
    }

    RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
    setupViews(smallContentView, context, song, playing);
    notification.contentView = smallContentView;

    Intent notificationIntent = new Intent(context, DownloadActivity.class);
    notification.contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    NotificationManager manager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notificationID, notification);

    // Update widget
    DSubWidgetProvider.getInstance().notifyChange(context, downloadService, true);
  }
Пример #3
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);
    }
  }