Android Open Source - coursera-android-001 Place View Activity

From Project

Back to project page coursera-android-001.


The source code is released under:

MIT License

If you think the Android project coursera-android-001 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 course.labs.contentproviderlab;
//from   www  .  ja v  a  2 s .c o m
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import course.labs.contentproviderlab.provider.PlaceBadgesContract;

public class PlaceViewActivity extends ListActivity implements
    LocationListener, LoaderCallbacks<Cursor> {
  private static final long FIVE_MINS = 5 * 60 * 1000;

  private static String TAG = "Lab-ContentProvider";

  // The last valid location reading
  private Location mLastLocationReading;

  // The ListView's adapter
  // private PlaceViewAdapter mAdapter;
  private PlaceViewAdapter mCursorAdapter;

  // default minimum time between new location readings
  private long mMinTime = 5000;

  // default minimum distance between old and new readings.
  private float mMinDistance = 1000.0f;

  // Reference to the LocationManager
  private LocationManager mLocationManager;

  // A fake location provider used for testing
  private MockLocationProvider mMockLocationProvider;

  protected void onCreate(Bundle savedInstanceState) {

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(PlaceBadgesContract.CONTENT_URI, null, null, null, null);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Set up the app's user interface
        // This class is a ListActivity, so it has its own ListView
        // ListView's adapter should be a PlaceViewAdapter
        ListView listView = getListView();

        // add a footerView to the ListView
        // You can use footer_view.xml to define the footer
        TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, listView, Boolean.FALSE);

        // When the footerView's onClick() method is called, it must issue the
        footerView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // following log call
                log("Entered footerView.OnClickListener.onClick()");

                // footerView must respond to user clicks.
                // Must handle 3 cases:

                boolean isFreshLocation = mLastLocationReading != null && age(mLastLocationReading) < FIVE_MINS;

                if (isFreshLocation) {
                    boolean isInList = mCursorAdapter.intersects(mLastLocationReading);

                    if (isInList) {
                        // 2) The current location has been seen before - issue Toast message.
                        // Issue the following log call:
                        log("You already have this location badge");
                        Toast.makeText(v.getContext(), "You already have this location badge", Toast.LENGTH_LONG).show();
                    } else {
                        // 1) The current location is new - download new Place Badge. Issue the
                        // following log call:
                        log("Starting Place Download");
                        new PlaceDownloaderTask(PlaceViewActivity.this).execute(mLastLocationReading);
                } else {
                    // 3) There is no current location - response is up to you. The best
                    // solution is to disable the footerView until you have a location.
                    // Issue the following log call:
                    log("Location data is not available");
    // Create and set empty PlaceViewAdapter
        // ListView's adapter should be a PlaceViewAdapter called mCursorAdapter
        mCursorAdapter = new PlaceViewAdapter(this, cursor, 0);

    // Initialize a CursorLoader
        getLoaderManager().initLoader(0, null, this);

  protected void onResume() {

    mMockLocationProvider = new MockLocationProvider(
        LocationManager.NETWORK_PROVIDER, this);

        // Check NETWORK_PROVIDER for an existing location reading.
        // Only keep this last reading if it is fresh - less than 5 minutes old.
        mLastLocationReading = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (mLastLocationReading != null && age(mLastLocationReading) > FIVE_MINS) {
            mLastLocationReading = null;

        // register to receive location updates from NETWORK_PROVIDER
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, mMinTime, mMinDistance, this);

  protected void onPause() {

    // Unregister for location updates

  public void addNewPlace(PlaceRecord place) {

    log("Entered addNewPlace()");



  public void onLocationChanged(Location currentLocation) {
        // Handle location updates
        // Cases to consider
        // 1) If there is no last location, keep the current location.
        // 2) If the current location is older than the last location, ignore
        // the current location
        // 3) If the current location is newer than the last locations, keep the
        // current location.
        if (mLastLocationReading == null || currentLocation.getTime() > mLastLocationReading.getTime()) {
            mLastLocationReading = currentLocation;

  public void onProviderDisabled(String provider) {
    // not implemented

  public void onProviderEnabled(String provider) {
    // not implemented

  public void onStatusChanged(String provider, int status, Bundle extras) {
    // not implemented

  public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    log("Entered onCreateLoader()");

    // Create a new CursorLoader and return it
        return new CursorLoader(this, PlaceBadgesContract.CONTENT_URI, null, null, null, null);

  public void onLoadFinished(Loader<Cursor> newLoader, Cursor newCursor) {
    // Swap in the newCursor

  public void onLoaderReset(Loader<Cursor> newLoader) {
    // Swap in a null Cursor

  private long age(Location location) {
    return System.currentTimeMillis() - location.getTime();

  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(, menu);
    return true;

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      ArrayList<PlaceRecord> currData = mCursorAdapter.getList();
      for (int i = 0; i < currData.size(); i++) {
      return true;
      return true;
      mMockLocationProvider.pushLocation(37.422, -122.084);
      return true;
      mMockLocationProvider.pushLocation(0, 0);
      return true;
      mMockLocationProvider.pushLocation(38.996667, -76.9275);
      return true;
      return super.onOptionsItemSelected(item);

  private static void log(String msg) {
    try {
    } catch (InterruptedException e) {
    Log.i(TAG, msg);

Java Source Code List