Back to project page connexus-android.
The source code is released under:
Apache License
If you think the Android project connexus-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 Zach Whaley, Trevor Latson */*ww w . j a va 2 s . c om*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ginger.connexus.activity; import ginger.connexus.R; import ginger.connexus.util.AccountUtils; import android.app.Activity; import android.app.Dialog; import android.content.Intent; import android.content.IntentSender; import android.location.Location; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.MenuItem; 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; public abstract class BaseActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { /* * Define a request code to send to Google Play services This code is * returned in Activity.onActivityResult */ private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; private boolean mConnected = false; private LocationClient mLocationClient; protected void startLocationClient() { // Connect the client. mLocationClient.connect(); } protected void stopLocationClient() { // Disconnecting the client invalidates it. mLocationClient.disconnect(); } public Location getLocation() { return mConnected ? mLocationClient.getLastLocation() : null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!AccountUtils.isAuthenticated(this)) { AccountUtils.startAuthenticationFlow(this, getIntent()); finish(); } /* * Create a new location client, using the enclosing class to handle * callbacks. */ mLocationClient = new LocationClient(this, this, this); } @Override protected void onStop() { stopLocationClient(); super.onStop(); } @Override public void onConnected(Bundle dataBundle) { mConnected = true; } @Override public void onDisconnected() { mConnected = false; } /* * Called by Location Services if the attempt to Location Services fails. */ @Override public void onConnectionFailed(ConnectionResult connectionResult) { /* * Google Play services can resolve some errors it detects. If the error * has a resolution, try sending an Intent to start a Google Play * services activity that can resolve error. */ 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. */ // Get the error code int errorCode = connectionResult.getErrorCode(); // Get the error dialog from Google Play services Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, this, CONNECTION_FAILURE_RESOLUTION_REQUEST); // If Google Play services can provide an error dialog if (errorDialog != null) { // Create a new DialogFragment for the error dialog 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"); } } } // Define a DialogFragment that displays the error dialog 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; } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_logout: AccountUtils.signOut(this); AccountUtils.startAuthenticationFlow(this, getIntent()); return true; default: return super.onOptionsItemSelected(item); } } /* * Handle results returned to the FragmentActivity by Google Play services */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Decide what to do based on the original request code if (requestCode == CONNECTION_FAILURE_RESOLUTION_REQUEST) { // If the result code is Activity.RESULT_OK, try to connect again if (resultCode == Activity.RESULT_OK) { // Try the request again return; } } super.onActivityResult(requestCode, resultCode, data); } }