package com.example.meinwald.ui.area; import android.Manifest; import android.app.AlertDialog; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.util.Log; import com.example.meinwald.BuildConfig; import com.google.android.gms.maps.model.LatLng; import java.util.ArrayList; import java.util.List; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; //import static com.name.name.MapsActivity.NOTIFICATION_ID; public class LocationService extends Service implements LocationListener { private static final String TAG = "LocationService"; private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; private boolean mLocationPermissionGranted; Location location; // location double latitude; // latitude double longitude; // longitude private List positions; // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 5 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 1 * 1; // 5 seconds // Declaring a Location Manager protected LocationManager mLocationManager; public LocationService(Context context) { if (BuildConfig.DEBUG) { Log.d(TAG, "LocationService(Context context)"); } this.mContext = context; this.positions = new ArrayList<>(); this.mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); getLocationPermission(); try { // getting GPS status isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled Log.d(TAG,"No permission to get location from any provider!"); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { } } else { Log.d(TAG,"Permission to get location from any provider!"); this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.d(TAG,"No permission to get location from network provider!"); } if (BuildConfig.DEBUG) { Log.d(TAG, "getLocation() requestLocationUpdates() from GPS"); } mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (mLocationManager != null) { location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { positions.add(new LatLng(location.getLatitude(), location.getLongitude())); if (BuildConfig.DEBUG) { Log.d(TAG, "getLocation() new location added"); } } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled && mLocationPermissionGranted) { if (location == null) { if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.d(TAG,"No permission to get location from gps provider!"); } if (BuildConfig.DEBUG) { Log.d(TAG, "getLocation() requestLocationUpdates() from GPS"); } mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (mLocationManager != null) { location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { positions.add(new LatLng(location.getLatitude(), location.getLongitude())); if (BuildConfig.DEBUG) { Log.d(TAG, "getLocation() new location added"); } } } } } } } catch (Exception e) { e.printStackTrace(); } } public void getLocation() { if (BuildConfig.DEBUG) { Log.d(TAG, "getLocation()"); } } /** * Stop using GPS listener * Calling this function will stop using GPS in your app * */ public void stopUsingGPS(){ if(mLocationManager != null){ mLocationManager.removeUpdates(LocationService.this); } } /** * Function to get latitude * */ public double getLatitude(){ if(location != null){ latitude = location.getLatitude(); } // return latitude return latitude; } public List getPositions() { if (BuildConfig.DEBUG) { Log.d(TAG, "getPositions() count: " + this.positions.size()); } return this.positions; } /** * Function to get longitude * */ public double getLongitude(){ if(location != null){ longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * @return boolean * */ public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog * On pressing Settings button will lauch Settings Options * */ public void showSettingsAlert(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title alertDialog.setTitle("GPS is settings"); // Setting Dialog Message alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing Settings button alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } /** * Request location permission, so that we can get the location of the device. * The result of the permission request is handled by a callback, * onRequestPermissionsResult. * */ private void getLocationPermission() { if (ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationPermissionGranted = true; } else { mLocationPermissionGranted = false; } } @Override public void onLocationChanged(Location location) { if (BuildConfig.DEBUG) { Log.d(TAG, "onLocationChanged(), new location: " + location.toString()); } positions.add(new LatLng(location.getLatitude(), location.getLongitude())); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public IBinder onBind(Intent arg0) { return null; } }