Android Open Source - sloop Data Collection Loader






From Project

Back to project page sloop.

License

The source code is released under:

NON-LICENSE The Sloop data-browser source code is hereby released into the Public Domain. The original author, David Megginson, Megginson Technologies Ltd., and Acclar Open Ltd. provide no warranty:...

If you think the Android project sloop 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 com.megginson.sloop.ui;
/*from  ww w  . java 2  s .  c o m*/
import android.content.AsyncTaskLoader;
import android.content.Context;

import com.megginson.sloop.model.DataCollection;
import com.megginson.sloop.model.impl.DataCollectionIO;
import com.megginson.sloop.model.impl.DataCollectionIO.RedirectException;

/**
 * Load a {@link DataCollectionImpl} in a separate thread.
 * 
 * @author David Megginson
 */
public class DataCollectionLoader extends AsyncTaskLoader<DataCollectionResult> {

  // Poor-man's cache

  private static String sLastUrl = null;

  private static DataCollection sLastDataCollection = null;

  /**
   * URL of the data collection to be loaded.
   */
  private String mUrl = null;
  
  /**
   * Should we force load, even if we already have the data?
   */
  private boolean mForceLoad = false;

  /**
   * Last data collection loaded.
   */
  private DataCollection mDataCollection = null;

  public DataCollectionLoader(Context context) {
    super(context);
  }

  /**
   * Get the URL for the next load.
   * 
   * @return the URL for the next load, as a string, or null if none has been
   *         set.
   */
  public String getURL() {
    return mUrl;
  }

  /**
   * Set the URL for the next load.
   * 
   * @param url
   *            the URL for the next load, as a string.
   */
  public void setURL(String url) {
    if (url != mUrl) {
      mDataCollection = null;
      mUrl = url;
    }
  }
  
  public boolean isForceLoad() {
    return mForceLoad;
  }
  
  public void setForceLoad(boolean forceLoad) {
    mForceLoad = forceLoad;
  }

  @Override
  protected void onStartLoading() {
    // TODO Auto-generated method stub
    if (mDataCollection != null) {
      deliverResult(new DataCollectionResult(mDataCollection));
    }
    if (takeContentChanged() || mDataCollection == null) {
      forceLoad();
    }
    super.onStartLoading();
  }

  @Override
  public DataCollectionResult loadInBackground() {
    if (mDataCollection == null) {
      try {
        // FIXME poor excuse for caching
        if (!mForceLoad && mUrl.equals(sLastUrl)) {
          mDataCollection = sLastDataCollection;
        } else {
          mDataCollection = DataCollectionIO.readCSV(mUrl, getContext().getContentResolver());
        }
      } catch (RedirectException e) {
        return new DataCollectionResult(e.getRedirectUrl());
      } catch (Throwable t) {
        return new DataCollectionResult(t);
      }

      // FIXME poor excuse for caching
      sLastDataCollection = mDataCollection;
      sLastUrl = mUrl;
    }
    return new DataCollectionResult(mDataCollection);
  }

}




Java Source Code List

com.megginson.sloop.activities.ActivitiesUtil.java
com.megginson.sloop.activities.AddressActionProvider.java
com.megginson.sloop.activities.BookmarkEditActivity.java
com.megginson.sloop.activities.BookmarkListActivity.java
com.megginson.sloop.activities.InfoBarFragment.java
com.megginson.sloop.activities.MainActivity.java
com.megginson.sloop.activities.MainDisplayFragment.java
com.megginson.sloop.activities.TextFilterFragment.java
com.megginson.sloop.activities.package-info.java
com.megginson.sloop.model.Bookmark.java
com.megginson.sloop.model.DataCollection.java
com.megginson.sloop.model.DataEntry.java
com.megginson.sloop.model.DataRecord.java
com.megginson.sloop.model.Util.java
com.megginson.sloop.model.ValueFilter.java
com.megginson.sloop.model.impl.ContainsStringFilter.java
com.megginson.sloop.model.impl.DataCollectionIO.java
com.megginson.sloop.model.impl.DataCollectionImpl.java
com.megginson.sloop.model.impl.DataEntryImpl.java
com.megginson.sloop.model.impl.DataRecordImpl.java
com.megginson.sloop.model.impl.EqualsStringFilter.java
com.megginson.sloop.model.impl.package-info.java
com.megginson.sloop.model.package-info.java
com.megginson.sloop.ui.BookmarkListAdapter.java
com.megginson.sloop.ui.DataCollectionLoader.java
com.megginson.sloop.ui.DataCollectionPagerAdapter.java
com.megginson.sloop.ui.DataCollectionResult.java
com.megginson.sloop.ui.DataRecordFragment.java
com.megginson.sloop.ui.DataRecordListAdapter.java
com.megginson.sloop.ui.package-info.java