Android Open Source - HapiPodcastJ Feed Handler






From Project

Back to project page HapiPodcastJ.

License

The source code is released under:

GNU General Public License

If you think the Android project HapiPodcastJ listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package info.xuluan.podcast.parser;
//from w ww  .j  av  a2  s .c o m
import info.xuluan.podcastj.R;
import info.xuluan.podcast.fetcher.FeedFetcher;
import info.xuluan.podcast.fetcher.Response;
import info.xuluan.podcast.provider.FeedItem;
import info.xuluan.podcast.provider.ItemColumns;
import info.xuluan.podcast.provider.Subscription;
import info.xuluan.podcast.utils.Log;

import java.io.ByteArrayInputStream;


import android.content.ContentResolver;
import android.database.Cursor;

public class FeedHandler  {
  private static final int REPEAT_UPDATE_FEED_COUNT = 3;
  private final Log log = Log.getLog(getClass());
  
  private ContentResolver cr;
  private int max_valid_size;
  
  public FeedHandler(ContentResolver context, int max_valid_sz){
    cr = context;
    max_valid_size = max_valid_sz;
  }

  public int update(Subscription sub){
    FeedParserListener listener = fetchFeed(sub.url,sub.id);
    if ((listener != null) && (listener.resultCode==0))
      return updateFeed(sub, listener);


    updateFail(sub);
    return 0;
  }
  
  public FeedParserListener fetchFeed(String url, long sub_id) {

    FeedFetcher fetcher = new FeedFetcher();
    FeedParserListener listener = new FeedParserListener(max_valid_size);
    FeedParserHandler handler = new FeedParserHandler(listener, sub_id);
    Response response = null;
    try {
      response = fetcher.fetch(url);

      if (response != null)
        FeedParser.getDefault().parse(
            new ByteArrayInputStream(response.getContentAsString().getBytes()), handler);
      else{
        log.debug("response == null");
        listener.resultCode = R.string.network_fail;
        return listener;
      }

    } catch (Exception e) {
      if(listener.getFeedItemsSize()==0){

        listener.resultCode = R.string.feed_format_error;
        return listener;
      }
    }

    log.debug("fetchFeed getFeedItemsSize = "
            + listener.getFeedItemsSize());

    if (listener.getFeedItemsSize() <= 0) {
      listener.resultCode = R.string.no_new_items;
    }else{
      listener.resultCode = 0;
    }
    return listener;
  }
  
  public void updateFail(Subscription sub) {
        
    if(sub.failCount<REPEAT_UPDATE_FEED_COUNT){
      sub.failCount++;
    }else{
      sub.failCount=0;
    }
    sub.update(cr);
  }

  /** Returns the number of episodes added to the channel. */
  public int updateFeed(Subscription subscription, FeedParserListener listener) {
    FeedItem[] feedItems = listener.getSortItems();
    log.debug("fetchFeed getSortItemsSize = "
            + feedItems.length);       
    long update_date = subscription.lastItemUpdated;
    int add_num = 0;

    for (FeedItem item : feedItems) {
      //Ignore items that are older than the most recently added batch
      long d = item.getDate();
      if (d <= subscription.lastItemUpdated) {
        continue;
      }

      if(d>update_date){
        update_date = d;
      }
      addItem(subscription, item);
      add_num++;

    }
    
    subscription.failCount = 0;
    subscription.title = listener.getFeedTitle();
    subscription.description = listener.getFeedDescription();
    subscription.lastItemUpdated = update_date;
    subscription.update(cr);
    log.debug("add url: "+subscription.url+"\n add num = "+add_num);
    return add_num;
    
  }
  
  private void addItem(Subscription subscription, FeedItem item){
    Long sub_id = subscription.id;

    item.sub_id = sub_id;
    if(subscription.autoDownload>0){
      item.status = ItemColumns.ITEM_STATUS_DOWNLOAD_QUEUE;
    }
    
    String where = ItemColumns.SUBS_ID + "=" + sub_id + " and "
        + ItemColumns.RESOURCE + "= '" + item.resource + "'";

    Cursor cursor = cr.query(ItemColumns.URI,
        new String[] { ItemColumns._ID }, where, null, null);

    if (cursor.moveToFirst()) {
    } else {
      item.insert(cr);
    }

    if(cursor!=null)
      cursor.close();      

  }  
}




Java Source Code List

info.xuluan.podcast.AddChannelActivity.java
info.xuluan.podcast.BackupChannelsActivity.java
info.xuluan.podcast.ChannelActivity.java
info.xuluan.podcast.ChannelDetailsActivity.java
info.xuluan.podcast.ChannelsActivity.java
info.xuluan.podcast.DownloadActivity.java
info.xuluan.podcast.EpisodeDetailsActivity.java
info.xuluan.podcast.EpisodeIcons.java
info.xuluan.podcast.EpisodesActivity.java
info.xuluan.podcast.FlingGestureDetector.java
info.xuluan.podcast.Flingable.java
info.xuluan.podcast.HapiActivity.java
info.xuluan.podcast.HapiListActivity.java
info.xuluan.podcast.HapiPreferenceActivity.java
info.xuluan.podcast.HomeActivity.java
info.xuluan.podcast.MainActivity.java
info.xuluan.podcast.PlayerActivity.java
info.xuluan.podcast.PodcastBaseActivity.java
info.xuluan.podcast.PodcastTab.java
info.xuluan.podcast.Pref.java
info.xuluan.podcast.SearchActivity.java
info.xuluan.podcast.StartupActivity.java
info.xuluan.podcast.TabsHelper.java
info.xuluan.podcast.actionbar.ActionBarHelperBase.java
info.xuluan.podcast.actionbar.ActionBarHelperHoneycomb.java
info.xuluan.podcast.actionbar.ActionBarHelperICS.java
info.xuluan.podcast.actionbar.ActionBarHelper.java
info.xuluan.podcast.actionbar.SimpleMenuItem.java
info.xuluan.podcast.actionbar.SimpleMenu.java
info.xuluan.podcast.fetcher.FeedFetcher.java
info.xuluan.podcast.fetcher.Response.java
info.xuluan.podcast.parser.FeedHandler.java
info.xuluan.podcast.parser.FeedParserHandler.java
info.xuluan.podcast.parser.FeedParserListenerInterface.java
info.xuluan.podcast.parser.FeedParserListener.java
info.xuluan.podcast.parser.FeedParser.java
info.xuluan.podcast.parser.OPMLParserHandler.java
info.xuluan.podcast.parser.SearchItem.java
info.xuluan.podcast.provider.FeedItem.java
info.xuluan.podcast.provider.ItemColumns.java
info.xuluan.podcast.provider.PodcastOpenHelper.java
info.xuluan.podcast.provider.PodcastProvider.java
info.xuluan.podcast.provider.SubscriptionColumns.java
info.xuluan.podcast.provider.Subscription.java
info.xuluan.podcast.service.PlayerService.java
info.xuluan.podcast.service.PodcastService.java
info.xuluan.podcast.utils.DialogMenu.java
info.xuluan.podcast.utils.FileUtils.java
info.xuluan.podcast.utils.IconCursorAdapter.java
info.xuluan.podcast.utils.LabeledFrame.java
info.xuluan.podcast.utils.LockHandler.java
info.xuluan.podcast.utils.Log.java
info.xuluan.podcast.utils.SDCardMgr.java
info.xuluan.podcast.utils.StrUtils.java
info.xuluan.podcast.utils.ZipExporter.java
info.xuluan.podcast.utils.ZipImporter.java