Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AppLocationManager.java
Show All 24 Lines | |||||
import android.location.LocationListener; | import android.location.LocationListener; | ||||
import android.location.LocationManager; | import android.location.LocationManager; | ||||
import android.location.LocationProvider; | import android.location.LocationProvider; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import androidx.core.content.ContextCompat; | import androidx.core.content.ContextCompat; | ||||
import androidx.core.location.LocationManagerCompat; | |||||
import androidx.core.location.LocationRequestCompat; | |||||
import it.reyboz.bustorino.util.LocationCriteria; | import it.reyboz.bustorino.util.LocationCriteria; | ||||
import it.reyboz.bustorino.util.Permissions; | import it.reyboz.bustorino.util.Permissions; | ||||
import java.lang.ref.WeakReference; | import java.lang.ref.WeakReference; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.ListIterator; | import java.util.ListIterator; | ||||
/** | /** | ||||
* Singleton class used to access location. Possibly extended with other location sources. | * Singleton class used to access location. Possibly extended with other location sources. | ||||
*/ | */ | ||||
public class AppLocationManager implements LocationListener { | public class AppLocationManager implements LocationListener { | ||||
public static final int LOCATION_GPS_AVAILABLE = 22; | public static final int LOCATION_GPS_AVAILABLE = 22; | ||||
public static final int LOCATION_UNAVAILABLE = -22; | public static final int LOCATION_UNAVAILABLE = -22; | ||||
private final Context appContext; | private final Context appContext; | ||||
private final LocationManager locMan; | private final LocationManager locMan; | ||||
public static final String DEBUG_TAG = "BUSTO LocAdapter"; | public static final String DEBUG_TAG = "BUSTO LocAdapter"; | ||||
private final String BUNDLE_LOCATION = "location"; | private final String BUNDLE_LOCATION = "location"; | ||||
private static AppLocationManager instance; | private static AppLocationManager instance; | ||||
private int oldGPSLocStatus = LOCATION_UNAVAILABLE; | private int oldGPSLocStatus = LOCATION_UNAVAILABLE; | ||||
private int minimum_time_milli = -1; | private int minimum_time_milli = -1; | ||||
private boolean isLocationPermissionGiven = false; | |||||
private final ArrayList<WeakReference<LocationRequester>> requestersRef = new ArrayList<>(); | private final ArrayList<WeakReference<LocationRequester>> requestersRef = new ArrayList<>(); | ||||
private AppLocationManager(Context context) { | private AppLocationManager(Context context) { | ||||
this.appContext = context.getApplicationContext(); | this.appContext = context.getApplicationContext(); | ||||
locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | ||||
isLocationPermissionGiven = checkLocationPermission(context); | boolean isLocationPermissionGiven = checkLocationPermission(context); | ||||
} | } | ||||
public static AppLocationManager getInstance(Context con) { | public static AppLocationManager getInstance(Context con) { | ||||
if(instance==null) instance = new AppLocationManager(con); | if(instance==null) instance = new AppLocationManager(con); | ||||
return instance; | return instance; | ||||
} | } | ||||
public static boolean checkLocationPermission(Context context){ | public static boolean checkLocationPermission(Context context){ | ||||
return ContextCompat.checkSelfPermission(context, | return ContextCompat.checkSelfPermission(context, | ||||
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED; | Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED; | ||||
} | } | ||||
private void requestGPSPositionUpdates() throws SecurityException{ | private void requestGPSPositionUpdates() throws SecurityException{ | ||||
final int timeinterval = (minimum_time_milli>0 && minimum_time_milli<Integer.MAX_VALUE)? minimum_time_milli : 2000; | final int timeinterval = (minimum_time_milli>0 && minimum_time_milli<Integer.MAX_VALUE)? minimum_time_milli : 2000; | ||||
locMan.removeUpdates(this); | locMan.removeUpdates(this); | ||||
if(locMan.getAllProviders().contains("gps")) | |||||
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeinterval, 5, this); | locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeinterval, 5, this); | ||||
/*LocationManagerCompat.requestLocationUpdates(locMan, LocationManager.GPS_PROVIDER, | |||||
new LocationRequestCompat.Builder(timeinterval).setMinUpdateDistanceMeters(5.F).build(),this, ); | |||||
TODO: find a way to do this | |||||
*/ | |||||
} | } | ||||
private void cleanAndUpdateRequesters(){ | private void cleanAndUpdateRequesters(){ | ||||
minimum_time_milli = Integer.MAX_VALUE; | minimum_time_milli = Integer.MAX_VALUE; | ||||
ListIterator<WeakReference<LocationRequester>> iter = requestersRef.listIterator(); | ListIterator<WeakReference<LocationRequester>> iter = requestersRef.listIterator(); | ||||
while(iter.hasNext()){ | while(iter.hasNext()){ | ||||
final LocationRequester cReq = iter.next().get(); | final LocationRequester cReq = iter.next().get(); | ||||
if(cReq==null) iter.remove(); | if(cReq==null) iter.remove(); | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register