package co.createch.MetroRappid.ui; import android.app.Activity; import android.app.Dialog; import android.content.Intent; import android.content.IntentSender; import android.os.Bundle; import android.support.v4.app.DialogFragment; import de.keyboardsurfer.android.widget.crouton.Crouton; import de.keyboardsurfer.android.widget.crouton.Style; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.location.LocationClient; /** * Created by Seth Gholson on 5/3/14. */ public class BaseLocationActivity extends BaseActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; protected LocationClient mLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationClient = new LocationClient(this,this,this); } @Override protected void onStop() { if(mLocationClient != null) { mLocationClient.disconnect(); } super.onStop(); } @Override protected void onStart() { super.onStart(); if(mLocationClient != null) { mLocationClient.connect(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CONNECTION_FAILURE_RESOLUTION_REQUEST: switch (resultCode) { case Activity.RESULT_OK: break; } } } protected boolean servicesConnected() { // Check that Google Play services is available int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if(errorCode == ConnectionResult.SERVICE_MISSING) { //TODO: Warn user that we were unable to determine location, but continue. return false; } else if (errorCode != ConnectionResult.SUCCESS) { showErrorDialog(errorCode); return false; } return true; } private void showErrorDialog(int errorCode) { Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog( errorCode, this, CONNECTION_FAILURE_RESOLUTION_REQUEST); ErrorDialogFragment errorFragment = new ErrorDialogFragment(); // Set the dialog in the DialogFragment errorFragment.setDialog(errorDialog); // Show the error dialog in the DialogFragment errorFragment.show(getSupportFragmentManager(), "Location Updates"); } @Override public void onConnected(Bundle dataBundle) { // Display the connection status Crouton.makeText(this, "Connected", Style.INFO).show(); } @Override public void onDisconnected() { // Display the connection status Crouton.makeText(this, "Disconnected. Please re-connect.", Style.ALERT).show(); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { if (connectionResult.hasResolution()) { try { // Start an Activity that tries to resolve the error connectionResult.startResolutionForResult( this, CONNECTION_FAILURE_RESOLUTION_REQUEST); /* * Thrown if Google Play services canceled the original * PendingIntent */ } catch (IntentSender.SendIntentException e) { // Log the error e.printStackTrace(); } } else { /* * If no resolution is available, display a dialog to the * user with the error. */ showErrorDialog(connectionResult.getErrorCode()); } } public static class ErrorDialogFragment extends DialogFragment { // Global field to contain the error dialog private Dialog mDialog; // Default constructor. Sets the dialog field to null public ErrorDialogFragment() { super(); mDialog = null; } // Set the dialog to display public void setDialog(Dialog dialog) { mDialog = dialog; } // Return a Dialog to the DialogFragment. @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return mDialog; } } }