package com.example.Maps012;


import android.app.Service;
import android.content.BroadcastReceiver;
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;
import android.util.Log;
import android.widget.Toast;

// http://www.truiton.com/2014/09/android-service-broadcastreceiver-example/

// https://androidexperinz.wordpress.com/2012/02/14/communication-between-service-and-activity-part-1/



/**
 * Created by filipp on 6/16/2016.
 */
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.getLongitude()+" "+location.getLatitude() +" "+location.getSpeed() +" "+location.getAltitude()  +" "+location.getAccuracy());
                    
                    // location.getAccuracy()
                    
                    sendBroadcast(i);
                }

                @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);
        
       // Criteria criteria = new Criteria();
       // criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
       // criteria.setAccuracy(Criteria.ACCURACY_HIGH); // Choose your accuracy requirement.
       // 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);

        //noinspection MissingPermission
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);

    }

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