package fr.univ_tln.trailscommunity.features.session.navigation.location;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
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;

import org.androidannotations.annotations.EService;

/**
 * Project TrailsCommunity.
 * Package fr.univ_tln.trailscommunity.features.session.navigation.
 * File LocationService.java.
 * Created by Ysee on 28/11/2016 - 21:14.
 * www.yseemonnier.com
 * https://github.com/YMonnier
 */

@EService
public class LocationService extends Service implements LocationListener {

    public static final String LOCATION_BROADCAST = "LOCATION_BROADCAST";

    private static final String TAG = LocationService.class.getSimpleName();

    LocationManager locationManager;

    public Location lastKnownLocation = null;


    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");

        //intent = new Intent(BROADCAST_ACTION);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "onStart");
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
        //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
    }

    @Override
    public void onLocationChanged(Location location) {

    }

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

    }

    @Override
    public void onProviderEnabled(String s) {
        Toast.makeText(getBaseContext(), "Gps is turned on!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String s) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "Gps is turned off!! ",
                Toast.LENGTH_SHORT).show();
    }


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

@@ -68,16 +68,41 @@
 ation;%0A%0A
+import android.Manifest;%0A
 import a
@@ -121,16 +121,16 @@
 ervice;%0A
-
 import a
@@ -184,16 +184,58 @@
 Intent;%0A
+import android.content.pm.PackageManager;%0A
 import a
@@ -470,24 +470,70 @@
 n.Nullable;%0A
+import android.support.v4.app.ActivityCompat;%0A
 import andro
@@ -1569,16 +1569,17 @@
 RVICE);%0A
+%0A
         
@@ -1649,33 +1649,33 @@
 K_PROVIDER, 
-4
+1
 000, 0, 
 listener);%0A 
@@ -1658,32 +1658,28 @@
 R, 1000, 0, 
-listener
+this
 );%0A        /
@@ -1669,34 +1669,32 @@
  this);%0A        
-//
 locationManager.
@@ -1750,17 +1750,17 @@
 ER, 
-4
+1
 000, 0, 
 list
@@ -1759,26 +1759,969 @@
  0, 
-listener);
+this);%0A%0A        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) %7B%0A            // TODO: Consider calling%0A            //    ActivityCompat#requestPermissions%0A            // here to request the missing permissions, and then overriding%0A            //   public void onRequestPermissionsResult(int requestCode, String%5B%5D permissions,%0A            //                                          int%5B%5D grantResults)%0A            // to handle the case where the user grants the permission. See the documentation%0A            // for ActivityCompat#requestPermissions for more details.%0A            Log.d(TAG, %22ActivityCompat.checkSelfPermission::OK%22);%0A%0A            return;%0A        %7D else %7B%0A            Log.d(TAG, %22ActivityCompat.checkSelfPermission::NOT OK%22);%0A        %7D
 %0A    %7D%0A%0A
     
@@ -2708,32 +2708,33 @@
        %7D%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2782,25 +2782,77 @@
 location) %7B%0A
+        Log.d(TAG, %22location updated: %22 + location);
 %0A
-
     %7D%0A%0A    @
@@ -2926,17 +2926,73 @@
 ndle) %7B%0A
+        Log.d(TAG, %22onStatusChanged: %22 + s + %22 %7C %22 + i);
 %0A
-
     %7D%0A%0A 
@@ -3218,32 +3218,32 @@
 led(String s) %7B%0A
-
         Intent i
@@ -3304,16 +3304,72 @@
 TINGS);%0A
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A
         

update location service add onProviderEnabled action