package com.example.Maps013;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
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.support.annotation.Nullable;

public class GPS_Service extends Service {

    private LocationListener listener;
    private LocationManager locationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

            listener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Intent i = new Intent("location_update");
                    i.putExtra("coordinates",location.getLatitude()+" "+location.getLongitude()+" "+location.getAltitude()+" "+location.getSpeed());
                    sendBroadcast(i);
                    
                    //location.getAccuracy()
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {
                        Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                }
            };

        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

        //noinspection MissingPermission
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,listener);
        
        
        //LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        //Criteria criteria = new Criteria();
        //criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
        //criteria.setAccuracy(Criteria.ACCURACY_HIGH); // ACCURACY_FINE); // Choose your accuracy requirement.
        
        //criteria.setSpeedRequired(true);
        //criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
        //criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
        //criteria.setSpeedRequired(true); // Chose if speed for first location fix is required.
        //criteria.setAltitudeRequired(false); // Choose if you use altitude.
        //criteria.setBearingRequired(false); // Choose if you use bearing.
        //criteria.setCostAllowed(false); // Choose if this provider can waste money :-)
     
        // Provide your criteria and flag enabledOnly that tells
        // LocationManager only to return active providers.
        //locationManager.getBestProvider(criteria, true);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(locationManager != null){
            //noinspection MissingPermission
            locationManager.removeUpdates(listener);
        }
    }
}