Fetch location using lib which gratefully handles no location provider.

master
Tomasz Półgrabia 2016-09-19 13:24:48 +02:00 committed by Tomasz Półgrabia
parent abff2da67f
commit 2033a636ca
3 changed files with 25 additions and 12 deletions

View File

@ -26,6 +26,7 @@ import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto;
import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo;
import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorer.utils.NetUtils;
import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils; import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils;
import java.io.*; import java.io.*;
@ -248,7 +249,7 @@ public class HomeFragment extends Fragment {
LocationManager locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); LocationManager locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); final Location location = NetUtils.getLastKnownLocation(activity);
if (location == null) { if (location == null) {
lg.info("Location still not available"); lg.info("Location still not available");
@ -328,7 +329,7 @@ public class HomeFragment extends Fragment {
MainActivity mainActivity = (MainActivity) getActivity(); MainActivity mainActivity = (MainActivity) getActivity();
LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); final Location location = NetUtils.getLastKnownLocation(activity);
if (location == null) { if (location == null) {
lg.info("Location is still not available"); lg.info("Location is still not available");
mainActivity.hideProgress(); mainActivity.hideProgress();
@ -415,8 +416,7 @@ public class HomeFragment extends Fragment {
lg.warn("Activity should'nt be null. No headless fragment"); lg.warn("Activity should'nt be null. No headless fragment");
return; return;
} }
LocationManager locationService = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); Location currLocation = NetUtils.getLastKnownLocation(activity);
Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity));
lg.trace("Current location: {}, locationInfo: {}", currLocation, locationInfo); lg.trace("Current location: {}, locationInfo: {}", currLocation, locationInfo);
if (currLocation != null && locationInfo != null) { if (currLocation != null && locationInfo != null) {
// update home fragment's location info // update home fragment's location info
@ -446,8 +446,8 @@ public class HomeFragment extends Fragment {
PanoramioCacheDto dto = new PanoramioCacheDto(); PanoramioCacheDto dto = new PanoramioCacheDto();
dto.setPanoramioImages(photos); dto.setPanoramioImages(photos);
LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); Location location = NetUtils.getLastKnownLocation(getActivity());
if (location != null) { if (location != null) {
dto.setLongitude(location.getLongitude()); dto.setLongitude(location.getLongitude());
dto.setLatitude(location.getLatitude()); dto.setLatitude(location.getLatitude());

View File

@ -26,10 +26,7 @@ import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto;
import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject;
import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.*;
import pl.tpolgrabia.urbanexplorer.utils.NumberUtils;
import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback;
import pl.tpolgrabia.urbanexplorer.utils.WikiUtils;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -144,7 +141,7 @@ public class WikiLocationsFragment extends Fragment {
return; return;
} }
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); final Location location = NetUtils.getLastKnownLocation(activity);
if (location == null) { if (location == null) {
lg.info("Sorry, location is still not available"); lg.info("Sorry, location is still not available");
@ -226,7 +223,7 @@ public class WikiLocationsFragment extends Fragment {
lg.warn("Activity shouldn't be null. No headless fragment"); lg.warn("Activity shouldn't be null. No headless fragment");
return; return;
} }
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); final Location location = NetUtils.getLastKnownLocation(activity);
if (location != null) { if (location != null) {
currentLocation.setText("Your current location: (" currentLocation.setText("Your current location: ("
+ location.getLatitude() + location.getLatitude()

View File

@ -2,6 +2,8 @@ package pl.tpolgrabia.urbanexplorer.utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import com.androidquery.AQuery; import com.androidquery.AQuery;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -91,4 +93,18 @@ public class NetUtils {
AppConstants.DEF_HTTP_PROXY_ENABLED); AppConstants.DEF_HTTP_PROXY_ENABLED);
} }
public static Location getLastKnownLocation(Context ctx) {
String locationProvider = LocationUtils.getDefaultLocation(ctx);
if (locationProvider == null) {
lg.info("Location not available");
return null;
}
return getSystemService(ctx).getLastKnownLocation(locationProvider);
}
private static LocationManager getSystemService(Context ctx) {
return (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
}
} }