Skip to content

Commit

Permalink
Priority to GPS (+logging)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdeconinck committed May 3, 2023
1 parent 73cadd2 commit c5055fd
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions Main/src/main/java/info/deconinck/inclinometer/GpsTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import androidx.core.app.ActivityCompat;

/**
* Taken from sample at https://www.howtodoandroid.com/get-current-location-android/
* Taken from sample at <a href="https://www.howtodoandroid.com/get-current-location-android/">https://www.howtodoandroid.com/get-current-location-android/</a>
*/
public class GpsTracker extends Service implements LocationListener {

Expand Down Expand Up @@ -68,28 +68,6 @@ public Location getLocation() {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
//check the network permission
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d(TAG, "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}

// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
Expand All @@ -115,10 +93,35 @@ public Location getLocation() {
}
}
}
}
else {
// Note: if app is to be used indoors, might be better to enable network location updates even if GPS is enabled
// get location from Network Provider
if (isNetworkEnabled) {
//check the network permission
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
catch (Exception e) {
Log.e(TAG, "getLocation: ", e);
}

return location;
Expand Down

0 comments on commit c5055fd

Please sign in to comment.