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

View file

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