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






From Project

Back to project page coursera-android-001.

License

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.app.ListActivity;
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;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(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();
        registerForContextMenu(listView);
        mAdapter = new PlaceViewAdapter(getApplicationContext());
    
        // add a footerView to the ListView
        // You can use footer_view.xml to define the footer
        listView.setFooterDividersEnabled(Boolean.TRUE);
        TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, listView, Boolean.FALSE);
        listView.addFooterView(footerView);
    
        // When the footerView's onClick() method is called, it must issue the
        footerView.setOnClickListener(new OnClickListener() {
            @Override
            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");
                }
            }
        });

        setListAdapter(mAdapter);
  }

  @Override
  protected void onResume() {
    super.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);
  }

  @Override
  protected void onPause() {
    mMockLocationProvider.shutdown();

    // unregister for location updates
        mLocationManager.removeUpdates(this);
    
    super.onPause();
  }

  // Callback method used by PlaceDownloaderTask
  public void addNewPlace(PlaceRecord place) {
    log("Entered addNewPlace()");
    mAdapter.add(place);
  }

  @Override
  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;
        }
  }

  @Override
  public void onProviderDisabled(String provider) {
    // not implemented
  }

  @Override
  public void onProviderEnabled(String provider) {
    // not implemented
  }

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

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

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.print_badges:
      ArrayList<PlaceRecord> currData = mAdapter.getList();
      for (int i = 0; i < currData.size(); i++) {
        log(currData.get(i).toString());
      }
      return true;
    case R.id.delete_badges:
      mAdapter.removeAllViews();
      return true;
    case R.id.place_one:
      mMockLocationProvider.pushLocation(37.422, -122.084);
      return true;
    case R.id.place_invalid:
      mMockLocationProvider.pushLocation(0, 0);
      return true;
    case R.id.place_two:
      mMockLocationProvider.pushLocation(38.996667, -76.9275);
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

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

}




Java Source Code List

course.labs.GraphicsLab.BubbleActivity.java
course.labs.activitylab.ActivityOne.java
course.labs.activitylab.ActivityTwo.java
course.labs.contentproviderlab.MockLocationProvider.java
course.labs.contentproviderlab.PlaceDownloaderTask.java
course.labs.contentproviderlab.PlaceRecord.java
course.labs.contentproviderlab.PlaceViewActivity.java
course.labs.contentproviderlab.PlaceViewAdapter.java
course.labs.contentproviderlab.provider.PlaceBadgeContentProvider.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.dangerousapp.DangerousActivity.java
course.labs.fragmentslab.FeedFragmentData.java
course.labs.fragmentslab.FeedFragment.java
course.labs.fragmentslab.FriendsFragment.java
course.labs.fragmentslab.MainActivity.java
course.labs.intentslab.ActivityLoaderActivity.java
course.labs.intentslab.ExplicitlyLoadedActivity.java
course.labs.intentslab.mybrowser.MyBrowserActivity.java
course.labs.locationlab.MockLocationProvider.java
course.labs.locationlab.PlaceDownloaderTask.java
course.labs.locationlab.PlaceRecord.java
course.labs.locationlab.PlaceViewActivity.java
course.labs.locationlab.PlaceViewAdapter.java
course.labs.notificationslab.DownloaderTask.java
course.labs.notificationslab.FeedFragment.java
course.labs.notificationslab.FriendsFragment.java
course.labs.notificationslab.MainActivity.java
course.labs.notificationslab.SelectionListener.java
course.labs.notificationslab.TestFrontEndActivity.java
course.labs.permissionslab.ActivityLoaderActivity.java
course.labs.permissionslab.BookmarksActivity.java
course.labs.permissionslab.GoToDangerousActivity.java
course.labs.todomanager.AddToDoActivity.java
course.labs.todomanager.ToDoItem.java
course.labs.todomanager.ToDoListAdapter.java
course.labs.todomanager.ToDoManagerActivity.java