예제 #1
0
 /** Returns true if there is at least one Feed in the downloads queue. */
 public synchronized boolean isDownloadingFeeds() {
   for (DownloadRequest r : downloads.values()) {
     if (r.getFeedfileType() == Feed.FEEDFILETYPE_FEED) {
       return true;
     }
   }
   return false;
 }
예제 #2
0
 /**
  * Returns true if a filename is available and false if it has already been taken by another
  * requested download.
  */
 private boolean isFilenameAvailable(String path) {
   for (String key : downloads.keySet()) {
     DownloadRequest r = downloads.get(key);
     if (StringUtils.equals(r.getDestination(), path)) {
       if (BuildConfig.DEBUG) Log.d(TAG, path + " is already used by another requested download");
       return false;
     }
   }
   if (BuildConfig.DEBUG) Log.d(TAG, path + " is available as a download destination");
   return true;
 }
예제 #3
0
  /**
   * Starts a new download with the given DownloadRequest. This method should only be used from
   * outside classes if the DownloadRequest was created by the DownloadService to ensure that the
   * data is valid. Use downloadFeed(), downloadImage() or downloadMedia() instead.
   *
   * @param context Context object for starting the DownloadService
   * @param request The DownloadRequest. If another DownloadRequest with the same source URL is
   *     already stored, this method call will return false.
   * @return True if the download request was accepted, false otherwise.
   */
  public synchronized boolean download(Context context, DownloadRequest request) {
    Validate.notNull(context);
    Validate.notNull(request);

    if (downloads.containsKey(request.getSource())) {
      if (BuildConfig.DEBUG) Log.i(TAG, "DownloadRequest is already stored.");
      return false;
    }
    downloads.put(request.getSource(), request);

    Intent launchIntent = new Intent(context, DownloadService.class);
    launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request);
    context.startService(launchIntent);
    EventDistributor.getInstance().sendDownloadQueuedBroadcast();
    return true;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    Downloader downloader = getItem(position);
    DownloadRequest request = downloader.getDownloadRequest();
    // Inflate layout
    if (convertView == null) {
      holder = new Holder();
      LayoutInflater inflater =
          (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.downloadlist_item, parent, false);
      holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
      holder.downloaded = (TextView) convertView.findViewById(R.id.txtvDownloaded);
      holder.percent = (TextView) convertView.findViewById(R.id.txtvPercent);
      holder.progbar = (ProgressBar) convertView.findViewById(R.id.progProgress);
      holder.butSecondary = (ImageButton) convertView.findViewById(R.id.butSecondaryAction);

      convertView.setTag(holder);
    } else {
      holder = (Holder) convertView.getTag();
    }

    if (position == selectedItemIndex) {
      convertView.setBackgroundColor(
          ContextCompat.getColor(
              convertView.getContext(), ThemeUtils.getSelectionBackgroundColor()));
    } else {
      convertView.setBackgroundResource(0);
    }

    holder.title.setText(request.getTitle());

    holder.progbar.setIndeterminate(request.getSoFar() <= 0);

    String strDownloaded = Converter.byteToString(request.getSoFar());
    if (request.getSize() != DownloadStatus.SIZE_UNKNOWN) {
      strDownloaded += " / " + Converter.byteToString(request.getSize());
      holder.percent.setText(request.getProgressPercent() + "%");
      holder.progbar.setProgress(request.getProgressPercent());
      holder.percent.setVisibility(View.VISIBLE);
    } else {
      holder.progbar.setProgress(0);
      holder.percent.setVisibility(View.INVISIBLE);
    }

    holder.downloaded.setText(strDownloaded);

    holder.butSecondary.setFocusable(false);
    holder.butSecondary.setTag(downloader);
    holder.butSecondary.setOnClickListener(butSecondaryListener);

    return convertView;
  }
예제 #5
0
 /** Remove an object from the downloads-list of the requester. */
 public synchronized void removeDownload(DownloadRequest r) {
   if (downloads.remove(r.getSource()) == null) {
     Log.e(TAG, "Could not remove object with url " + r.getSource());
   }
 }