예제 #1
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
 protected void onStop() {
   super.onStop();
   EventDistributor.getInstance().unregister(listener);
 }
  @Override
  protected void showFeedInformation(final Feed feed, final Map<String, String> alternateFeedUrls) {
    super.showFeedInformation(feed, alternateFeedUrls);
    setContentView(R.layout.listview_activity);

    this.feed = feed;
    this.selectedDownloadUrl = feed.getDownload_url();
    EventDistributor.getInstance().register(listener);
    ListView listView = (ListView) findViewById(R.id.listview);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View header = inflater.inflate(R.layout.onlinefeedview_header, listView, false);
    listView.addHeaderView(header);

    listView.setAdapter(new FeedItemlistDescriptionAdapter(this, 0, feed.getItems()));

    ImageView cover = (ImageView) header.findViewById(R.id.imgvCover);
    TextView title = (TextView) header.findViewById(R.id.txtvTitle);
    TextView author = (TextView) header.findViewById(R.id.txtvAuthor);
    TextView description = (TextView) header.findViewById(R.id.txtvDescription);
    Spinner spAlternateUrls = (Spinner) header.findViewById(R.id.spinnerAlternateUrls);

    subscribeButton = (Button) header.findViewById(R.id.butSubscribe);

    if (feed.getImage() != null && StringUtils.isNotBlank(feed.getImage().getDownload_url())) {
      Glide.with(this)
          .load(feed.getImage().getDownload_url())
          .placeholder(R.color.light_gray)
          .error(R.color.light_gray)
          .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
          .fitCenter()
          .dontAnimate()
          .into(cover);
    }

    title.setText(feed.getTitle());
    author.setText(feed.getAuthor());
    description.setText(feed.getDescription());

    subscribeButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            try {
              Feed f = new Feed(selectedDownloadUrl, new Date(0), feed.getTitle());
              f.setPreferences(feed.getPreferences());
              DefaultOnlineFeedViewActivity.this.feed = f;

              DownloadRequester.getInstance().downloadFeed(DefaultOnlineFeedViewActivity.this, f);
            } catch (DownloadRequestException e) {
              e.printStackTrace();
              DownloadRequestErrorDialogCreator.newRequestErrorDialog(
                  DefaultOnlineFeedViewActivity.this, e.getMessage());
            }
            setSubscribeButtonState(feed);
          }
        });

    if (alternateFeedUrls.isEmpty()) {
      spAlternateUrls.setVisibility(View.GONE);
    } else {
      spAlternateUrls.setVisibility(View.VISIBLE);

      final List<String> alternateUrlsList = new ArrayList<String>();
      final List<String> alternateUrlsTitleList = new ArrayList<String>();

      alternateUrlsList.add(feed.getDownload_url());
      alternateUrlsTitleList.add(feed.getTitle());

      alternateUrlsList.addAll(alternateFeedUrls.keySet());
      for (String url : alternateFeedUrls.keySet()) {
        alternateUrlsTitleList.add(alternateFeedUrls.get(url));
      }
      ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(
              this, android.R.layout.simple_spinner_item, alternateUrlsTitleList);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spAlternateUrls.setAdapter(adapter);
      spAlternateUrls.setOnItemSelectedListener(
          new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              selectedDownloadUrl = alternateUrlsList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
          });
    }
    setSubscribeButtonState(feed);
  }