Android Open Source - campus Place Details Loader






From Project

Back to project page campus.

License

The source code is released under:

GNU General Public License

If you think the Android project campus 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 lecho.app.campus.loader;
//from www.j ava2  s  .co m
import java.util.ArrayList;
import java.util.List;

import lecho.app.campus.dao.DaoSession;
import lecho.app.campus.dao.Faculty;
import lecho.app.campus.dao.FacultyDao;
import lecho.app.campus.dao.Place;
import lecho.app.campus.dao.PlaceDao;
import lecho.app.campus.dao.PlaceUnitDao;
import lecho.app.campus.dao.Unit;
import lecho.app.campus.dao.UnitDao;
import lecho.app.campus.utils.Config;
import lecho.app.campus.utils.DatabaseHelper;
import lecho.app.campus.utils.PlaceDetails;
import lecho.app.campus.utils.UnitsGroup;
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

/**
 * Loads Place details from database, name, symbol, faculties etc. Loader doesn't watch for changes in database, there's
 * no need for that in this app.
 * 
 * Implementation based on sample from android sdk documentation.
 * 
 * @author Lecho
 * 
 */
public class PlaceDetailsLoader extends AsyncTaskLoader<PlaceDetails> {
  private static final String TAG = "PlaceDetailsLoader";
  private static final String QUERY_UNITS_BY_PLACE_ORDER_BY_FACULTY = "left join " + PlaceUnitDao.TABLENAME
      + " PU on PU." + PlaceUnitDao.Properties.UnitId.columnName + "=T." + UnitDao.Properties.Id.columnName
      + " left join " + FacultyDao.TABLENAME + " F on F." + FacultyDao.Properties.Id.columnName + "=T."
      + UnitDao.Properties.FacultyId.columnName + " where PU." + PlaceUnitDao.Properties.PlaceId.columnName
      + "=? order by ifnull(F." + FacultyDao.Properties.ShortName.columnName + ",'zzz') asc, T."
      + UnitDao.Properties.Name.columnName + " asc";

  private Long mPlaceId;
  private DaoSession mDaoSession;
  private PlaceDetails mData;

  public PlaceDetailsLoader(Context context, Long placeId) {
    super(context);
    mPlaceId = placeId;
  }

  @Override
  public PlaceDetails loadInBackground() {
    if (Config.DEBUG) {
      Log.d(TAG, "Loading place details");
    }
    mDaoSession = DatabaseHelper.getDaoSession(getContext());
    PlaceDao placeDao = mDaoSession.getPlaceDao();
    UnitDao unitDao = mDaoSession.getUnitDao();
    Place place = placeDao.load(mPlaceId);
    if (null == place) {
      Log.e(TAG, "Could not load place details - null place for id: " + mPlaceId);
      return null;
    }
    String[] args = new String[] { Long.toString(place.getId()) };
    List<Unit> units = unitDao.queryDeep(QUERY_UNITS_BY_PLACE_ORDER_BY_FACULTY, args);
    List<UnitsGroup> unitsGroups = groupUnits(units);
    PlaceDetails placeDetails = new PlaceDetails(place, unitsGroups);
    return placeDetails;
  }

  /**
   * Groups units by faculty name
   * @param units
   * @return
   */
  private List<UnitsGroup> groupUnits(List<Unit> units) {
    List<UnitsGroup> unitsGroups = new ArrayList<UnitsGroup>();
    if (units.isEmpty()) {
      return unitsGroups;
    }
    Faculty currentFaculty = units.get(0).getFaculty();
    String currentFacultyName;
    if (null != currentFaculty) {
      currentFacultyName = currentFaculty.getShortName();
    } else {
      currentFacultyName = "";
    }
    List<Unit> group = new ArrayList<Unit>();
    for (Unit unit : units) {
      String newFacultyName;
      Faculty newFaculty = unit.getFaculty();
      if (null != newFaculty) {
        newFacultyName = newFaculty.getShortName();
      } else {
        newFacultyName = "";
      }
      if (!currentFacultyName.equals(newFacultyName)) {
        // if new unit has different faculty - start new group.
        unitsGroups.add(new UnitsGroup(currentFaculty, group));
        currentFaculty = newFaculty;
        currentFacultyName = newFacultyName;
        group = new ArrayList<Unit>();
      }
      group.add(unit);
    }
    // Add last group if it's not empty.
    if (!group.isEmpty()) {
      unitsGroups.add(new UnitsGroup(currentFaculty, group));
    }
    return unitsGroups;
  }

  /**
   * Called when there is new data to deliver to the client. The super class will take care of delivering it; the
   * implementation here just adds a little more logic.
   */
  @Override
  public void deliverResult(PlaceDetails data) {
    if (Config.DEBUG) {
      Log.d(TAG, "Delivering place details");
    }
    if (isReset()) {
      // An async query came in while the loader is stopped. We
      // don't need the result.
      if (data != null) {
        onReleaseResources(data);
      }
    }
    PlaceDetails oldData = mData;
    mData = data;

    if (isStarted()) {
      // If the Loader is currently started, we can immediately
      // deliver its results.
      super.deliverResult(data);
    }

    // At this point we can release the resources associated with
    // 'oldData' if needed; now that the new result is delivered we
    // know that it is no longer in use.
    if (oldData != null && oldData != data) {
      onReleaseResources(oldData);
    }
  }

  /**
   * Handles a request to start the Loader.
   */
  @Override
  protected void onStartLoading() {
    if (mData != null) {
      // If we currently have a result available, deliver it
      // immediately.
      deliverResult(mData);
    } else {
      forceLoad();
    }
  }

  /**
   * Handles a request to stop the Loader.
   */
  @Override
  protected void onStopLoading() {
    // Attempt to cancel the current load task if possible.
    cancelLoad();
  }

  /**
   * Handles a request to cancel a load.
   */
  @Override
  public void onCanceled(PlaceDetails data) {
    super.onCanceled(data);

    // At this point we can release the resources associated with 'apps'
    // if needed.
    onReleaseResources(data);
  }

  /**
   * Handles a request to completely reset the Loader.
   */
  @Override
  protected void onReset() {
    super.onReset();

    // Ensure the loader is stopped
    onStopLoading();

    // At this point we can release the resources associated with 'apps'
    // if needed.
    if (mData != null) {
      onReleaseResources(mData);
      mData = null;
    }
  }

  /**
   * Helper function to take care of releasing resources associated with an actively loaded data set.
   */
  protected void onReleaseResources(PlaceDetails data) {
  }
}




Java Source Code List

lecho.app.campus.activity.AboutAppActivity.java
lecho.app.campus.activity.CampusMapActivity.java
lecho.app.campus.activity.GalleryActivity.java
lecho.app.campus.activity.LegalInfoActivity.java
lecho.app.campus.activity.PlaceDetailsActivity.java
lecho.app.campus.adapter.GalleryPagesFragmentAdapter.java
lecho.app.campus.adapter.MarkerInfoWindowAdapter.java
lecho.app.campus.adapter.NavigationDrawerAdapter.java
lecho.app.campus.adapter.PlaceDetailsFragmentAdapter.java
lecho.app.campus.adapter.SearchResultViewAdapter.java
lecho.app.campus.adapter.SearchSuggestionAdapter.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.CategoryDao.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.Category.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoMaster.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.DaoSession.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.FacultyDao.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Faculty.java
lecho.app.campus.dao.Main.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategoryDao.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceCategory.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFacultyDao.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceFaculty.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnitDao.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.PlaceUnit.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.Place.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.UnitDao.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.dao.Unit.java
lecho.app.campus.fragment.GalleryPageFragment.java
lecho.app.campus.fragment.PlaceDetailsFragment.java
lecho.app.campus.fragment.dialog.NoInternetConnectionDialogFragment.java
lecho.app.campus.fragment.dialog.PlayServicesErrorDialogFragment.java
lecho.app.campus.loader.PlaceDetailsLoader.java
lecho.app.campus.loader.PlacesLoader.java
lecho.app.campus.provider.SearchSuggestionProvider.java
lecho.app.campus.service.PopulateDBIntentService.java
lecho.app.campus.utils.ABSMenuItemConverter.java
lecho.app.campus.utils.BitmapAsyncTask.java
lecho.app.campus.utils.Config.java
lecho.app.campus.utils.DataParser.java
lecho.app.campus.utils.DatabaseHelper.java
lecho.app.campus.utils.ImagesDirAsyncTask.java
lecho.app.campus.utils.NavigationDrawerItem.java
lecho.app.campus.utils.PlaceDetails.java
lecho.app.campus.utils.PlacesList.java
lecho.app.campus.utils.UnitsGroup.java
lecho.app.campus.utils.Utils.java
lecho.app.campus.view.CheckableDrawerItem.java
lecho.app.campus.view.UnitsGroupLayout.java
lecho.app.campus.view.ZoomImageView.java
lecho.app.campus.view.ZoomViewPager.java