cl.smartcities.isci.transportinspector.detectionService.StateLocationSender.java Source code

Java tutorial

Introduction

Here is the source code for cl.smartcities.isci.transportinspector.detectionService.StateLocationSender.java

Source

package cl.smartcities.isci.transportinspector.detectionService;

import android.content.SharedPreferences;
import android.location.Location;
import android.util.Log;
import android.util.Pair;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import cl.smartcities.isci.transportinspector.TranSappApplication;
import cl.smartcities.isci.transportinspector.backend.Bus;
import cl.smartcities.isci.transportinspector.serverConnection.ServerConnectionController;
import cl.smartcities.isci.transportinspector.serverConnection.ServerResponseListener;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.GetTokenRequest;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pushRequests.PurgeTokenRequest;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pushRequests.SendMultipleLocationsRequest;

public class StateLocationSender implements ServerResponseListener<String> {
    private ArrayList<Pair<Location, Boolean>> locations;
    private ArrayList<Pair<Location, Boolean>> toSendLocations;
    private String token;
    private Timer timer;
    private final static String TAG = "StateLocationSender";
    private final static long DELAY = 15 * 1000;
    private SharedPreferences prefs;

    public StateLocationSender(Bus bus) {
        locations = new ArrayList<>();
        timer = null;
        prefs = TranSappApplication.getAppSharedPreferences();
        getToken(bus);
    }

    public void addSingleLocation(Location location, boolean b) {
        addLocations(new Pair<>(location, b));
    }

    public void addMultiplesLocations(ArrayList<Pair<Location, Boolean>> other) {
        for (Pair<Location, Boolean> location : other) {
            addLocations(location);
        }
    }

    public void stopSender() {
        Log.d(TAG, "stopSender");
        sendAllLocations();
        PurgeTokenRequest purgeTokenRequest = new PurgeTokenRequest(this.token);
        ServerConnectionController.sendRequest(purgeTokenRequest, new ServerResponseListener<String>() {
            @Override
            public void callback(String response) {
                //Do nothing.
            }

            @Override
            public String getToken() {
                return TAG;
            }
        });
        if (timer != null) {
            timer.cancel();
        }
    }

    private void sendAllLocations() {
        if (!locations.isEmpty()) {
            Log.d(TAG, "size: " + locations.size());
            if (!prefs.getBoolean("NOT_USE_LOCATION_CHANGES", false)) {
                toSendLocations = locations;
                locations = new ArrayList<>();
                SendMultipleLocationsRequest request = new SendMultipleLocationsRequest(token, toSendLocations);
                ServerConnectionController.sendPostRequest(request, new ServerResponseListener<JSONObject>() {
                    @Override
                    public void callback(JSONObject response) {
                    }

                    @Override
                    public String getToken() {
                        return TAG;
                    }
                }, null, null);
                toSendLocations = null;
            } else {
                locations.clear();
            }
        }
    }

    private void getToken(Bus bus) {
        GetTokenRequest tokenRequest = new GetTokenRequest(bus.getService(), bus.getLicensePlate());

        ServerConnectionController.sendRequest(tokenRequest, this);
    }

    @Override
    public void callback(String response) {
        this.token = response;
        Log.d(TAG, "token received");
        sendAllLocations();
        setupTimer(DELAY);
    }

    @Override
    public String getToken() {
        return TAG;
    }

    private void setupTimer(final long delay) {
        class Task extends TimerTask {
            @Override
            public void run() {
                Log.d(TAG, "timeToSend");
                sendAllLocations();
                setupTimer(delay);
            }
        }
        if (timer == null) {
            timer = getNewTimer();
        }
        timer.schedule(new Task(), delay);
    }

    public void addMultiplesLocations(ArrayList<Location> locations, boolean b) {
        for (Location location : locations) {
            addLocations(new Pair<>(location, b));
        }
    }

    public Timer getNewTimer() {
        return new Timer();
    }

    public void addLocations(Pair<Location, Boolean> location) {
        locations.add(location);
    }
}