123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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<LatLng> 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<LatLng> 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;
- }
- }
|