make location manager singleton

This commit is contained in:
maskara 2017-11-07 02:01:14 +05:30
parent d4bafd94e0
commit a59bf1bc4b
2 changed files with 14 additions and 3 deletions

View file

@ -7,6 +7,7 @@ import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import fr.free.nrw.commons.CommonsApplication;
import timber.log.Timber;
public class LocationServiceManager implements LocationListener {
@ -16,11 +17,21 @@ public class LocationServiceManager implements LocationListener {
private LatLng latestLocation;
private Float latestLocationAccuracy;
public LocationServiceManager(Context context) {
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
private static LocationServiceManager locationServiceManager;
private LocationServiceManager() {
Context applicationContext = CommonsApplication.getInstance().getApplicationContext();
this.locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), true);
}
public static LocationServiceManager getInstance() {
if (locationServiceManager == null) {
locationServiceManager = new LocationServiceManager();
}
return locationServiceManager;
}
public boolean isProviderEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

View file

@ -60,7 +60,7 @@ public class NearbyActivity extends NavigationBaseActivity {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
setContentView(R.layout.activity_nearby);
ButterKnife.bind(this);
locationManager = new LocationServiceManager(this);
locationManager = LocationServiceManager.getInstance();
checkLocationPermission();
bundle = new Bundle();
initDrawer();