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.locationlab;
//from w w  w . j a va  2 s.  c  o m
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
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;

public class PlaceViewActivity extends ListActivity implements LocationListener {
  private static final long FIVE_MINS = 5 * 60 * 1000;

  private static String TAG = "Lab-Location";

  private Location mLastLocationReading;
  private PlaceViewAdapter mAdapter;

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

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

  private LocationManager mLocationManager;

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

  protected void onCreate(Bundle savedInstanceState) {

        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();
        mAdapter = new PlaceViewAdapter(getApplicationContext());
        // 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 = mAdapter.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");


  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

  // Callback method used by PlaceDownloaderTask
  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

  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 = mAdapter.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