/*
 * Copyright 2013 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.client;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.BatteryManager;
import android.preference.PreferenceManager;
import android.util.Log;

import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;

public class PositionProvider implements LostApiClient.ConnectionCallbacks, LocationListener {

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

    private static final int MINIMUM_INTERVAL = 1000;

    public interface PositionListener {
        void onPositionUpdate(Position position);
    }

    private final PositionListener listener;

    private final Context context;
    private SharedPreferences preferences;
    private LostApiClient apiClient;

    private String deviceId;
    private long interval;
    private double distance;
    private double angle;

    private Location lastLocation;

    public PositionProvider(Context context, PositionListener listener) {
        this.context = context;
        this.listener = listener;

        preferences = PreferenceManager.getDefaultSharedPreferences(context);

        deviceId = preferences.getString(MainFragment.KEY_DEVICE, "undefined");
        interval = Long.parseLong(preferences.getString(MainFragment.KEY_INTERVAL, "600")) * 1000;
        distance = Integer.parseInt(preferences.getString(MainFragment.KEY_DISTANCE, "0"));
        angle = Integer.parseInt(preferences.getString(MainFragment.KEY_ANGLE, "0"));
    }

    public void startUpdates() {
        apiClient = new LostApiClient.Builder(context).addConnectionCallbacks(this).build();
        apiClient.connect();
    }

    private int getPriority(String accuracy) {
        switch (accuracy) {
            case "high":
                return LocationRequest.PRIORITY_HIGH_ACCURACY;
            case "low":
                return LocationRequest.PRIORITY_LOW_POWER;
            default:
                return LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
        }
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onConnected() {
        LocationRequest request = LocationRequest.create()
                .setPriority(getPriority(preferences.getString(MainFragment.KEY_ACCURACY, "medium")))
                .setInterval(distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval);

        LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null && (lastLocation == null
                || location.getTime() - lastLocation.getTime() >= interval
                || distance > 0 && DistanceCalculator.distance(location.getLatitude(), location.getLongitude(), lastLocation.getLatitude(), lastLocation.getLongitude()) >= distance
                || angle > 0 && Math.abs(location.getBearing() - lastLocation.getBearing()) >= angle)) {
            Log.i(TAG, "location new");
            lastLocation = location;
            listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel(context)));
        } else {
            Log.i(TAG, location != null ? "location ignored" : "location nil");
        }
    }

    @Override
    public void onConnectionSuspended() {
        Log.i(TAG, "lost client suspended");
    }

    public void stopUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(apiClient, this);

        apiClient.disconnect();
    }

    public static double getBatteryLevel(Context context) {
        Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if (batteryIntent != null) {
            int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
            return (level * 100.0) / scale;
        }
        return 0;
    }

}

@@ -1821,16 +1821,46 @@
 ation;%0A%0A
+    private boolean started;%0A%0A
     publ
@@ -1921,24 +1921,24 @@
 listener) %7B%0A
-
         this
@@ -2460,32 +2460,56 @@
 tartUpdates() %7B%0A
+        started = true;%0A
         apiClien
@@ -3059,24 +3059,51 @@
 nnected() %7B%0A
+        if (started) %7B%0A    
         Loca
@@ -3149,16 +3149,20 @@
 reate()%0A
+    
         
@@ -3267,24 +3267,28 @@
             
+  
+  
 .setInterval
@@ -3346,25 +3346,27 @@
 erval);%0A
-%0A
+   
         
 Location
@@ -3349,32 +3349,33 @@
 al);%0A           
+ 
 LocationServices
@@ -3442,16 +3442,79 @@
  this);%0A
+        %7D else %7B%0A            apiClient.disconnect();%0A        %7D%0A
     %7D%0A%0A 
@@ -4418,32 +4418,75 @@
 stopUpdates() %7B%0A
+        if (apiClient.isConnected()) %7B%0A    
         Location
@@ -4551,17 +4551,20 @@
  this);%0A
-%0A
+    
         
@@ -4579,32 +4579,67 @@
 t.disconnect();%0A
+        %7D%0A        started = false;%0A
     %7D%0A%0A    publi

Fix lost API connection logic