Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onebusaway.android.app.Application;
Expand Down Expand Up @@ -54,34 +51,6 @@ public class LocationUtilsTest extends ObaTestCase {
public static final long FRESH_LOCATION_THRESHOLD_MS = 1000 * 60 * 60 * 24;
// Within last 24 hours - see #737

/**
* GoogleApiClient being used for Location Services
*/
GoogleApiClient mGoogleApiClient;

@Before
public void before() {
super.before();

// Init Google Play Services as early as possible in the Fragment lifecycle to give it time
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
if (api.isGooglePlayServicesAvailable(getTargetContext())
== ConnectionResult.SUCCESS) {
mGoogleApiClient = LocationUtils.getGoogleApiClientWithCallbacks(getTargetContext());
mGoogleApiClient.connect();
}
}

@After
public void after() {
super.after();

// Tear down GoogleApiClient
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}

@Test
public void testLocationComparisonByTime() {
boolean result;
Expand Down Expand Up @@ -201,12 +170,13 @@ public void testLocationApiV1() {
// Make sure we're not running on an emulator, since we'll get a null location there
if (!TestUtils.isRunningOnEmulator() && PermissionUtils.hasGrantedAllPermissions(getTargetContext(), LOCATION_PERMISSIONS)) {
/**
* Test without Google Play Services - should be a Location API v1 location.
* Typically this is "gps" or "network", but some devices (e.g., HTC EVO LTE)
* have custom Android framework providers such as "hybrid" that might should up here.
* So, we can't test for "gps" or "network" specifically.
* Retrieves the last known location from any available source - the fused provider
* when Google Play Services is present, otherwise the framework Location API v1
* providers. For API v1 this is typically "gps" or "network", but some devices
* (e.g., HTC EVO LTE) have custom framework providers such as "hybrid" that might
* show up here. So, we can't test for a specific provider.
*/
loc = Application.getLastKnownLocation(getTargetContext(), null);
loc = Application.getLastKnownLocation(getTargetContext());
/**
* On devices that behave correctly the following non-null test should pass. However, it's
* possible that it can fail on some devices (e.g., on a fresh reboot on a device without
Expand All @@ -231,7 +201,7 @@ public void testLocationServices() {
/**
* Could return either a fused or Location API v1 location
*/
loc = Application.getLastKnownLocation(getTargetContext(), mGoogleApiClient);
loc = Application.getLastKnownLocation(getTargetContext());
assertNotNull(loc);
Log.d(TAG,
"Location Provider for Location Services test is '" + loc.getProvider() + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.onebusaway.android.map.googlemapsv2;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
Expand Down Expand Up @@ -271,8 +270,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

// If we have a recent location, show this while we're waiting on the LocationHelper
Location l = Application
.getLastKnownLocation(getActivity(), mLocationHelper.getGoogleApiClient());
Location l = Application.getLastKnownLocation(getActivity());
if (l != null) {
final long TIME_THRESHOLD = TimeUnit.MINUTES.toMillis(5);
if (System.currentTimeMillis() - l.getTime() < TIME_THRESHOLD) {
Expand Down Expand Up @@ -736,8 +734,7 @@ public void onRegionTaskFinished(boolean currentRegionChanged) {
return;
}

Location l = Application
.getLastKnownLocation(getActivity(), mLocationHelper.getGoogleApiClient());
Location l = Application.getLastKnownLocation(getActivity());
// If the region changed, and we don't have a location or the map center is still (0,0),
// then zoom to the region (or location if we have it)
Location mapCenter = getMapCenterAsLocation();
Expand Down Expand Up @@ -816,12 +813,7 @@ public boolean setMyLocation(boolean useDefaultZoom, boolean animateToLocation)
return false;
}

GoogleApiClient apiClient = null;
if (mLocationHelper != null) {
apiClient = mLocationHelper.getGoogleApiClient();
}

Location lastLocation = Application.getLastKnownLocation(getActivity(), apiClient);
Location lastLocation = Application.getLastKnownLocation(getActivity());
if (lastLocation == null) {
if (!PermissionUtils.hasGrantedAtLeastOnePermission(Application.get(), LOCATION_PERMISSIONS)) {
if (!PreferenceUtils.userDeniedLocationPermission()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
Expand Down Expand Up @@ -177,17 +176,14 @@ public int getAppLaunchCount() {
* always be called.
*
* @param cxt The Context being used, or null if one isn't available
* @param client The GoogleApiClient being used to obtain fused provider updates, or null if
* one
* isn't available
* @return the last known location that the application has seen, or null if we haven't seen a
* location yet
*/
public static synchronized Location getLastKnownLocation(Context cxt, GoogleApiClient client) {
public static synchronized Location getLastKnownLocation(Context cxt) {
if (mLastKnownLocation == null) {
// Try to get a last known location from the location providers
try {
mLastKnownLocation = getLocation2(cxt, client);
mLastKnownLocation = getLocation2(cxt);
} catch (SecurityException e) {
Log.e(TAG, "User may have denied location permission - " + e);
}
Expand Down Expand Up @@ -234,60 +230,26 @@ public static Float getMagneticDeclination() {
}
}

/**
* We need to provide the API for a location used to disambiguate stop IDs in case of
* collision,
* or to provide multiple results in the case multiple agencies. But we really don't need it to
* be very accurate.
* <p/>
* Note that the GoogleApiClient must already have been initialized and connected prior to
* calling
* this method, since GoogleApiClient.connect() is asynchronous and doesn't connect before it
* returns,
* which requires additional initialization time (prior to calling this method)
*
* @param client an initialized and connected GoogleApiClient, or null if Google Play Services
* isn't available
* @return a recent location, considering both Google Play Services (if available) and the
* Android Location API
*/
private static Location getLocation(Context cxt, GoogleApiClient client) {
Location last = getLocation2(cxt, client);
if (last != null) {
return last;
} else {
return LocationUtils.getDefaultSearchCenter();
}
}

/**
* Returns a location, considering both Google Play Services (if available) and the Android
* Location API
* <p/>
* Note that the GoogleApiClient must already have been initialized and connected prior to
* calling
* this method, since GoogleApiClient.connect() is asynchronous and doesn't connect before it
* returns,
* which requires additional initialization time (prior to calling this method)
*
* @param client an initialized and connected GoogleApiClient, or null if Google Play Services
* isn't available
* @return a recent location, considering both Google Play Services (if available) and the
* Android Location API
* @throws SecurityException if the user has remove location permissions
*/
private static Location getLocation2(Context cxt, GoogleApiClient client)
private static Location getLocation2(Context cxt)
throws SecurityException {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
Location playServices = null;
if (client != null &&
cxt != null &&
if (cxt != null &&
api.isGooglePlayServicesAvailable(cxt)
== ConnectionResult.SUCCESS
&& client.isConnected()) {
== ConnectionResult.SUCCESS) {
FusedLocationProviderClient fusedClient = getFusedLocationProviderClient(cxt);
Task<Location> task = fusedClient.getLastLocation();
if (task.isComplete()) {
// isSuccessful() (not isComplete()) - a task that completed with a failure would
// throw RuntimeExecutionException from getResult()
if (task.isSuccessful()) {
playServices = task.getResult();
Log.d(TAG, "Got location from Google Play Services, testing against API v1...");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@
*/
package org.onebusaway.android.map;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;

import org.onebusaway.android.util.LocationUtils;
import org.onebusaway.android.util.UIUtils;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -40,26 +34,12 @@ public abstract class BaseMapController implements MapModeController,

private MapWatcher mMapWatcher;

/**
* GoogleApiClient being used for Location Services
*/
private GoogleApiClient mGoogleApiClient;

public BaseMapController() {

}

public BaseMapController(Callback callback) {
mCallback = callback;
GoogleApiAvailability api = GoogleApiAvailability.getInstance();

// Init Google Play Services as early as possible in the Fragment lifecycle to give it time
if (api.isGooglePlayServicesAvailable(mCallback.getActivity())
== ConnectionResult.SUCCESS) {
Context context = mCallback.getActivity();
mGoogleApiClient = LocationUtils.getGoogleApiClientWithCallbacks(context);
mGoogleApiClient.connect();
}
createLoader();
}

Expand Down Expand Up @@ -130,21 +110,11 @@ public void destroy() {
@Override
public void onPause() {
watchMap(false);

// Tear down GoogleApiClient
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}

@Override
public void onResume() {
watchMap(true);

// Make sure GoogleApiClient is connected, if available
if (mGoogleApiClient != null && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.onebusaway.android.map;

import com.google.android.gms.common.api.GoogleApiClient;

import org.onebusaway.android.app.Application;
import org.onebusaway.android.io.ObaApi;
Expand Down Expand Up @@ -145,11 +144,6 @@ public class StopMapController extends BaseMapController implements

private MapWatcher mMapWatcher;

/**
* GoogleApiClient being used for Location Services
*/
GoogleApiClient mGoogleApiClient;

public StopMapController(Callback callback) {
super(callback);
}
Expand Down Expand Up @@ -221,8 +215,7 @@ public void onLoadFinished(Loader<StopsResponse> loader,
//We need to also make sure the list of stops is empty, otherwise we screen out valid responses
//TODO - After above issue #59 is resolved, we should also only do this check on OBA server
//versions below the version number in which this is fixed.
Location myLocation = Application.getLastKnownLocation(mCallback.getActivity(),
mGoogleApiClient);
Location myLocation = Application.getLastKnownLocation(mCallback.getActivity());
if (myLocation != null && Application.get().getCurrentRegion() != null) {
boolean inRegion = true; // Assume user is in region unless we detect otherwise
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
import android.os.Handler;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.firebase.analytics.FirebaseAnalytics;

import org.onebusaway.android.R;
import org.onebusaway.android.app.Application;
import org.onebusaway.android.io.ObaAnalytics;
import org.onebusaway.android.io.elements.ObaRegion;
import org.onebusaway.android.util.LocationUtils;
import org.onebusaway.android.util.RegionUtils;
import org.onebusaway.android.util.UIUtils;

Expand Down Expand Up @@ -82,11 +78,6 @@ public interface Callback {

private final boolean mShowProgressDialog;

/**
* GoogleApiClient being used for Location Services
*/
GoogleApiClient mGoogleApiClient;

private FirebaseAnalytics mFirebaseAnalytics;

/**
Expand All @@ -103,11 +94,6 @@ public ObaRegionsTask(Context context, List<ObaRegionsTask.Callback> callbacks,
mCallbacks = callbacks;
mForceReload = force;
mShowProgressDialog = showProgressDialog;
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
if (api.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
mGoogleApiClient = LocationUtils.getGoogleApiClientWithCallbacks(context);
mGoogleApiClient.connect();
}
mFirebaseAnalytics = FirebaseAnalytics.getInstance(context);
}

Expand Down Expand Up @@ -149,8 +135,7 @@ protected void onPostExecute(ArrayList<ObaRegion> results) {

if (settings
.getBoolean(mContext.getString(R.string.preference_key_auto_select_region), true)) {
// Pass in the GoogleApiClient initialized in constructor
Location myLocation = Application.getLastKnownLocation(mContext, mGoogleApiClient);
Location myLocation = Application.getLastKnownLocation(mContext);

ObaRegion closestRegion = RegionUtils.getClosestRegion(results, myLocation, true);

Expand Down Expand Up @@ -192,11 +177,6 @@ protected void onPostExecute(ArrayList<ObaRegion> results) {
}
}

// Tear down Location Services client
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}

super.onPostExecute(results);
}

Expand Down
Loading
Loading