package me.shoutto.sdk;

import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * Created by tracyrojas on 9/20/15.
 */
public class LocationServicesClient implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static final String TAG = "LocationServicesClient";
    private StmService stmService;
    private GoogleApiClient googleApiClient;
    private Location lastLocation;
    private LocationRequest locationRequest;
    private double latitude;
    private double longitude;

    // Location updates intervals in sec
    private static int UPDATE_INTERVAL = 15000; // 15 sec
    private static int FASTEST_INTERVAL = 5000; // 5 sec
    private static int DISPLACEMENT = 10; // 10 meters

    public LocationServicesClient(StmService stmService) {
        this.stmService = stmService;
        buildGoogleApiClient();
        createLocationRequest();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        if (lastLocation != null) {
            latitude = lastLocation.getLatitude();
            longitude = lastLocation.getLongitude();
        }

        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.w(TAG, "Google location services was suspended. Cause: " + cause);
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.e(TAG, "Google play location services failed. Check the ConnectionResult error code: "
                + result.getErrorCode());
    }

    @Override
    public void onLocationChanged(Location location) {
        lastLocation = location;
        if (lastLocation != null) {
            latitude = lastLocation.getLatitude();
            longitude = lastLocation.getLongitude();
        }
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void connectToService() {
        googleApiClient.connect();
    }

    public void disconnectFromService() {
        stopLocationUpdates();
        googleApiClient.disconnect();
    }

    protected synchronized void buildGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(stmService)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    protected void createLocationRequest() {
        locationRequest = new LocationRequest();
        locationRequest.setInterval(UPDATE_INTERVAL);
        locationRequest.setFastestInterval(FASTEST_INTERVAL);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setSmallestDisplacement(DISPLACEMENT); // 10 meters
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    }

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
    }
}

@@ -37,51 +37,210 @@
 oid.
-location.Location;%0Aimport android.os.Bundle
+Manifest;%0Aimport android.content.pm.PackageManager;%0Aimport android.location.Location;%0Aimport android.os.Bundle;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.v4.content.ContextCompat
 ;%0Aim
@@ -558,40 +558,42 @@
 %0A * 
-Created by tracyrojas on 9/20/15
+Wrapper class for LocationServices
 .%0A *
@@ -1090,24 +1090,30 @@
 vate static 
+final 
 int UPDATE_I
@@ -1154,24 +1154,30 @@
 vate static 
+final 
 int FASTEST_
@@ -1221,16 +1221,22 @@
  static 
+final 
 int DISP
@@ -1496,24 +1496,184 @@
 tionHint) %7B%0A
+        if (ContextCompat.checkSelfPermission(stmService, Manifest.permission.ACCESS_FINE_LOCATION)%0A                == PackageManager.PERMISSION_GRANTED) %7B%0A    
         last
@@ -1751,32 +1751,36 @@
 lient);%0A        
+   
+ 
 if (lastLocation
@@ -1783,32 +1783,36 @@
 tion != null) %7B%0A
+    
             lati
@@ -1850,32 +1850,36 @@
 ();%0A            
+ 
+   
 longitude = last
@@ -1895,32 +1895,46 @@
 getLongitude();%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2167,16 +2167,25 @@
 nFailed(
+@NonNull 
 Connecti
@@ -2199,24 +2199,75 @@
 t result) %7B%0A
+        Integer errorCode = result.getErrorCode();%0A
         Log.
@@ -2371,35 +2371,25 @@
       + 
-result.getE
+e
 rrorCode
 ());%0A   
@@ -2380,18 +2380,16 @@
 rrorCode
-()
 );%0A    %7D
@@ -3687,32 +3687,192 @@
 tionUpdates() %7B%0A
+        if (ContextCompat.checkSelfPermission(stmService, Manifest.permission.ACCESS_FINE_LOCATION)%0A                == PackageManager.PERMISSION_GRANTED) %7B%0A    
         Location
@@ -3957,24 +3957,34 @@
 est, this);%0A
+        %7D%0A
     %7D%0A%0A    p

Fix warnings in LocationServicesClient class