/* * Copyright (C) 2013 The WLANAudit project contributors. * * 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 es.glasspixel.wlanaudit.util; import android.app.Dialog; 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.util.Log; import com.example.android.location.LocationUtils; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; public class GMSLocationServicesWrapper implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static String TAG = GMSLocationServicesWrapper.class.getName(); /** * Context of the activity which delegates on this wrapper */ private FragmentActivity mActivity; /** * API Client for Google Play Services Location Services */ private GoogleApiClient mGoogleApiClient; /** * Flag to indicate if we're connected to Google Play Services */ private boolean mConnectedToGms = false; /** * Connection callback listener to which callbacks will be forwarded */ private GoogleApiClient.ConnectionCallbacks mConnectionCallbacksListener; public GMSLocationServicesWrapper(FragmentActivity activity) { mActivity = activity; mGoogleApiClient = new GoogleApiClient.Builder(activity).addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } public GMSLocationServicesWrapper(FragmentActivity activity, GoogleApiClient.ConnectionCallbacks connectionListener) { this(activity); mConnectionCallbacksListener = connectionListener; } /** * Gives the last known location from the Fused Location Provider * @return The last known location if there is any in the provider, null otherwise */ public Location getLastLocation() { if (mConnectedToGms) { return LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); } else { return null; } } public void connect() { mGoogleApiClient.connect(); } public void disconnect() { mGoogleApiClient.disconnect(); } @Override public void onConnected(Bundle bundle) { Log.i(TAG, "Connected to Google Play Services"); mConnectedToGms = true; if(mConnectionCallbacksListener != null) mConnectionCallbacksListener.onConnected(bundle); } @Override public void onConnectionSuspended(int i) { Log.i(TAG, "Disconnected from Google Play Services"); mConnectedToGms = false; if(mConnectionCallbacksListener != null) mConnectionCallbacksListener.onConnectionSuspended(i); } @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( mActivity, LocationUtils.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()); } } /** * Verify that Google Play services is available before making a request. * * @return true if Google Play services is available, otherwise false */ public boolean servicesConnected() { // Check that Google Play services is available int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mActivity); // If Google Play services is available if (ConnectionResult.SUCCESS == resultCode) { // Continue return true; // Google Play services was not available for some reason } else { // Display an error dialog Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, mActivity, 0); if (dialog != null) { ErrorDialogFragment errorFragment = new ErrorDialogFragment(); errorFragment.setDialog(dialog); errorFragment.show(mActivity.getSupportFragmentManager(), LocationUtils.APPTAG); } return false; } } /** * Show a dialog returned by Google Play services for the * connection error code * * @param errorCode An error code returned from onConnectionFailed */ private void showErrorDialog(int errorCode) { // Get the error dialog from Google Play services Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog( errorCode, mActivity, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST); // If Google Play services can provide an error dialog if (errorDialog != null) { // Create a new DialogFragment in which to show 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(mActivity.getSupportFragmentManager(), LocationUtils.APPTAG); } } /** * Define a DialogFragment to display the error dialog generated in * showErrorDialog. */ 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 * * @param dialog An error dialog */ public void setDialog(Dialog dialog) { mDialog = dialog; } /* * This method must return a Dialog to the DialogFragment. */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return mDialog; } } }