Moved out location service manager from nearby activity

This commit is contained in:
maskara 2017-04-02 19:59:59 +05:30
parent 9e4cc80ceb
commit 2e063a3dcf
7 changed files with 92 additions and 92 deletions

View file

@ -0,0 +1,41 @@
package fr.free.nrw.commons.location;
public class LatLng {
public final double latitude;
public final double longitude;
public LatLng(double latitude, double longitude) {
if(-180.0D <= longitude && longitude < 180.0D) {
this.longitude = longitude;
} else {
this.longitude = ((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D;
}
this.latitude = Math.max(-90.0D, Math.min(90.0D, latitude));
}
public int hashCode() {
boolean var1 = true;
byte var2 = 1;
long var3 = Double.doubleToLongBits(this.latitude);
int var5 = 31 * var2 + (int)(var3 ^ var3 >>> 32);
var3 = Double.doubleToLongBits(this.longitude);
var5 = 31 * var5 + (int)(var3 ^ var3 >>> 32);
return var5;
}
public boolean equals(Object o) {
if(this == o) {
return true;
} else if(!(o instanceof LatLng)) {
return false;
} else {
LatLng var2 = (LatLng)o;
return Double.doubleToLongBits(this.latitude) == Double.doubleToLongBits(var2.latitude) && Double.doubleToLongBits(this.longitude) == Double.doubleToLongBits(var2.longitude);
}
}
public String toString() {
return "lat/lng: (" + this.latitude + "," + this.longitude + ")";
}
}

View file

@ -0,0 +1,76 @@
package fr.free.nrw.commons.location;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LocationServiceManager implements LocationListener {
public static final String TAG = "LocationServiceManager";
private String provider;
private LocationManager locationManager;
private LatLng mLatestLocation;
public LocationServiceManager(Context context) {
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), true);
}
public LatLng getLatestLocation() {
return mLatestLocation;
}
/**
* Registers a LocationManager to listen for current location
*/
public void registerLocationManager() {
try {
locationManager.requestLocationUpdates(provider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(provider);
//Location works, just need to 'send' GPS coords via emulator extended controls if testing on emulator
Log.d(TAG, "Checking for location...");
if (location != null) {
this.onLocationChanged(location);
}
} catch (IllegalArgumentException e) {
Log.e(TAG, "Illegal argument exception", e);
} catch (SecurityException e) {
Log.e(TAG, "Security exception", e);
}
}
public void unregisterLocationManager() {
try {
locationManager.removeUpdates(this);
} catch (SecurityException e) {
Log.e(TAG, "Security exception", e);
}
}
@Override
public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
Log.d(TAG, "Latitude: " + String.valueOf(currentLatitude) + " Longitude: " + String.valueOf(currentLongitude));
mLatestLocation = new LatLng(currentLatitude, currentLongitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, provider + "'s status changed to " + status);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "Provider " + provider + " enabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "Provider " + provider + " disabled");
}
}