// Path: app/src/main/java/pt/passarola/model/events/LocationErrorEvent.java
// public class LocationErrorEvent extends ErrorEvent {
// 
//     public LocationErrorEvent(Exception e){
//         super(e);
//     }
// }
// 
// Path: app/src/main/java/pt/passarola/model/events/LocationSuccessEvent.java
// public class LocationSuccessEvent {
// 
//     private final Location location;
// 
//     public LocationSuccessEvent(Location location) {
//         this.location = location;
//     }
// 
//     public Location getLocation() {
//         return location;
//     }
// }
// 
// Path: app/src/main/java/pt/passarola/utils/Utils.java
// public class Utils {
// 
//     /**
//      * Validate if device has GooglePlayServices app installed
//      */
//     public static boolean isGooglePlayServicesAvailable(Context context) {
//         return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == SUCCESS;
//     }
// 
//     /**
//      * Normalize the value of a distance expressed in meters.
//      * 989 meters = 989 meters
//      * 1000 meters = 1.0 km
//      * 1500 meters = 1.5 km
//      *
//      * @param distanceInMeters distance number in meters
//      * @return distance normalized with distance units
//      */
//     public static String getNormalizedDistance(int distanceInMeters){
//         float distance = (float) distanceInMeters;
//         if(distance < 1000){
//             return distanceInMeters + " meters";
//         } else {
//             return String.format("%.02f", distance/1000) + " km";
//         }
//     }
// 
//     /**
//      * Generate a date object from a string
//      *
//      * @param date string date
//      * @return date object that follows the format "yyyy-mm-dd"
//      */
//     @Nullable
//     public static Date getDateFromString(String date){
//         Date result;
//         DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.UK);
// 
//         try {
//             result = dateFormat.parse(date);
//         } catch (ParseException e){
//             result = null;
//         }
// 
//         return result;
//     }
// 
//     /**
//      * Open intent with data
//      *
//      * @param context used to start activity
//      * @param link intent's data
//      */
//     public static void openLink(Context context, String link) {
//         Intent intent = new Intent(Intent.ACTION_VIEW);
//         intent.setData(Uri.parse(link));
//         context.startActivity(intent);
//     }
// 
//     /**
//      * Open contact screen by providing a phone number
//      * @param context used to start activity
//      * @param phoneNumber data to show on contact screen
//      */
//     public static void openContact(Context context, String phoneNumber){
//         Intent intent = new Intent(Intent.ACTION_VIEW);
//         intent.setData(Uri.parse("tel:" + phoneNumber));
//         context.startActivity(intent);
//     }
// 
// }


import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import pt.passarola.model.events.LocationErrorEvent;
import pt.passarola.model.events.LocationSuccessEvent;
import pt.passarola.utils.Utils;
package pt.passarola.services;





/**
 * Created by ruigoncalo on 19/12/15.
 */
public class LocationProvider
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private Context context;
    private BusProvider busProvider;
    private GoogleApiClient googleApiClient;

    public LocationProvider(BusProvider busProvider, Context context) {
        this.busProvider = busProvider;
        this.context = context;
    }

    public void getCurrentLocation() {
        if (isGooglePlayServicesAvailable()) {
            createGoogleApiClient();
            googleApiClient.connect();
        } else {
            postError(new Exception("Google Play Services not available"));
        }
    }

    private boolean isGooglePlayServicesAvailable() {
// Path: app/src/main/java/pt/passarola/model/events/LocationErrorEvent.java
// public class LocationErrorEvent extends ErrorEvent {
// 
//     public LocationErrorEvent(Exception e){
//         super(e);
//     }
// }
// 
// Path: app/src/main/java/pt/passarola/model/events/LocationSuccessEvent.java
// public class LocationSuccessEvent {
// 
//     private final Location location;
// 
//     public LocationSuccessEvent(Location location) {
//         this.location = location;
//     }
// 
//     public Location getLocation() {
//         return location;
//     }
// }
// 
// Path: app/src/main/java/pt/passarola/utils/Utils.java
// public class Utils {
// 
//     /**
//      * Validate if device has GooglePlayServices app installed
//      */
//     public static boolean isGooglePlayServicesAvailable(Context context) {
//         return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == SUCCESS;
//     }
// 
//     /**
//      * Normalize the value of a distance expressed in meters.
//      * 989 meters = 989 meters
//      * 1000 meters = 1.0 km
//      * 1500 meters = 1.5 km
//      *
//      * @param distanceInMeters distance number in meters
//      * @return distance normalized with distance units
//      */
//     public static String getNormalizedDistance(int distanceInMeters){
//         float distance = (float) distanceInMeters;
//         if(distance < 1000){
//             return distanceInMeters + " meters";
//         } else {
//             return String.format("%.02f", distance/1000) + " km";
//         }
//     }
// 
//     /**
//      * Generate a date object from a string
//      *
//      * @param date string date
//      * @return date object that follows the format "yyyy-mm-dd"
//      */
//     @Nullable
//     public static Date getDateFromString(String date){
//         Date result;
//         DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.UK);
// 
//         try {
//             result = dateFormat.parse(date);
//         } catch (ParseException e){
//             result = null;
//         }
// 
//         return result;
//     }
// 
//     /**
//      * Open intent with data
//      *
//      * @param context used to start activity
//      * @param link intent's data
//      */
//     public static void openLink(Context context, String link) {
//         Intent intent = new Intent(Intent.ACTION_VIEW);
//         intent.setData(Uri.parse(link));
//         context.startActivity(intent);
//     }
// 
//     /**
//      * Open contact screen by providing a phone number
//      * @param context used to start activity
//      * @param phoneNumber data to show on contact screen
//      */
//     public static void openContact(Context context, String phoneNumber){
//         Intent intent = new Intent(Intent.ACTION_VIEW);
//         intent.setData(Uri.parse("tel:" + phoneNumber));
//         context.startActivity(intent);
//     }
// 
// }


// Path: app/src/main/java/pt/passarola/services/LocationProvider.java
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import pt.passarola.model.events.LocationErrorEvent;
import pt.passarola.model.events.LocationSuccessEvent;
import pt.passarola.utils.Utils;

package pt.passarola.services;





/**
 * Created by ruigoncalo on 19/12/15.
 */
public class LocationProvider
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private Context context;
    private BusProvider busProvider;
    private GoogleApiClient googleApiClient;

    public LocationProvider(BusProvider busProvider, Context context) {
        this.busProvider = busProvider;
        this.context = context;
    }

    public void getCurrentLocation() {
        if (isGooglePlayServicesAvailable()) {
            createGoogleApiClient();
            googleApiClient.connect();
        } else {
            postError(new Exception("Google Play Services not available"));
        }
    }

    private boolean isGooglePlayServicesAvailable() {
        return Utils.isGooglePlayServicesAvailable(context);