Java tutorial
package cl.smartcities.isci.transportinspector.backend; import android.os.Parcel; import android.os.Parcelable; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import cl.smartcities.isci.transportinspector.search.Suggestion; import cl.smartcities.isci.transportinspector.search.SuggestionDispatcher; import cl.smartcities.isci.transportinspector.utils.Constants; /** * This class represents a Bus. It's parcelable to allow its use in Bundles. */ public class Bus implements JSONObjectable, Parcelable, Suggestion { private final String origin; private final String destination; private String route = ""; // Service of the bus (ex: "506v") private String service; // The request send this flag: 1 if contains the info, 0 otherwise. private int valid; // This variable is filled if valid equals to 0. private String descriptionError; /** * The following variables are filled if valid equals to 1. Note that some info takes as * reference a specific bus stop. */ // License plate of the bus (ex: "AAAA00") private String licensePlate; // Time the bus should take to arrive at the BusStop (ex: "Entre 03 y 05 min."). private String time; // Distance to the BusStop. private String distance; // Actual position latitude. private double latitude; // Actual position longitude. private double longitude; // Flag that shows if someone is streaming the trip. private int passengerNumber; // Default bus icon, shows the actual color of service. Used on the dialogs. private int busIcon; // Default map bus icon, shows the actual color and number of service. Used on the map. private int defaultMapBusIcon; // Active map bus icon, shows the actual color of service, number of service and that there is // someone streaming. Used on the map. private int activeMapBusIcon; // Where the bus is going: "up", "down", "left" or "right". private String direction; // Event list from the bus. private ArrayList<Event> events = new ArrayList<>(); private int realDistance; private int color; /** * Constructor for TESTS purposes ONLY. Creates a bus with incomplete info. * DO NOT use on the real app. * * @param pService bus service (ex: "506v") * @param plate bus plate (ex: "GGWP00") * @param pTime time the bus should take to arrive at the BusStop * @param pDistance distance to the BusStop */ public Bus(String pService, String plate, String pTime, String pDistance) { this.valid = 1; this.service = pService; this.licensePlate = plate; this.time = pTime; this.distance = pDistance; this.direction = "left"; this.busIcon = Constants.BUS_LIST.get(0); this.defaultMapBusIcon = Constants.MAP_BUS_LIST.get(0); this.activeMapBusIcon = Constants.ACTIVE_BUS_LIST.get(0); this.latitude = 0; this.longitude = 0; this.passengerNumber = 1; this.origin = ""; this.destination = ""; this.descriptionError = "non"; this.color = Constants.BUS_COLOR.get(0); } public Bus(String service, int color, String origin, String destination) { this.valid = 1; this.service = service; this.licensePlate = Constants.DUMMY_LICENSE_PLATE; this.time = "Menos de 5."; this.distance = "0 mts."; this.direction = "left"; this.busIcon = Constants.BUS_LIST.get(color); this.defaultMapBusIcon = Constants.MAP_BUS_LIST.get(color); this.activeMapBusIcon = Constants.ACTIVE_BUS_LIST.get(color); this.color = Constants.BUS_COLOR.get(color); this.latitude = 0; this.longitude = 0; this.passengerNumber = 1; this.descriptionError = "non"; this.origin = origin; this.destination = destination; this.route = origin + " / " + destination; } public Bus(String pService, String pLicensePlate) { this(pService, pLicensePlate, "Menos de 5.", "0 mts."); } /** * This is the only constructor that should be used to correctly create a Bus * * @param jsonObj JSONObject obtained from the request's answer * @throws JSONException */ public Bus(JSONObject jsonObj) throws JSONException { this.fillData(jsonObj); destination = ""; origin = ""; } /** * Fills the bus data based on the given JSONObject. Note that the JSONObject contains the info * given by the server. Note that the flag "valido" determines wich info is present. * * @throws JSONException */ private void fillData(JSONObject jsonObj) throws JSONException { this.valid = jsonObj.getInt("valido"); this.service = jsonObj.getString("servicio"); if (valid == 0) { this.descriptionError = jsonObj.getString("descripcionError"); this.busIcon = Constants.BUS_LIST.get(0); this.activeMapBusIcon = Constants.ACTIVE_BUS_LIST.get(0); this.defaultMapBusIcon = Constants.MAP_BUS_LIST.get(0); } else if (valid == 1) { this.busIcon = Constants.BUS_LIST.get(jsonObj.getInt("color")); this.activeMapBusIcon = Constants.ACTIVE_BUS_LIST.get(jsonObj.getInt("color")); this.defaultMapBusIcon = Constants.MAP_BUS_LIST.get(jsonObj.getInt("color")); this.color = Constants.BUS_COLOR.get(jsonObj.getInt("color")); this.licensePlate = jsonObj.getString("patente"); this.time = timeToString(jsonObj.getString("tiempoV2")); this.distance = jsonObj.getString("distanciaV2"); this.realDistance = jsonObj.getInt("distanciaMts"); this.direction = jsonObj.getString("sentido"); this.events = new ArrayList<>(); if (!jsonObj.getBoolean("random")) { this.latitude = jsonObj.getDouble("lat"); this.longitude = jsonObj.getDouble("lon"); } else { this.latitude = Double.POSITIVE_INFINITY; this.longitude = Double.POSITIVE_INFINITY; } this.passengerNumber = jsonObj.getInt("tienePasajeros"); try { JSONArray jsonEvents = jsonObj.getJSONArray("eventos"); for (int i = 0; i < jsonEvents.length(); i++) { Event event = new Event(jsonEvents.getJSONObject(i)); this.events.add(event); } } catch (JSONException e) { e.printStackTrace(); } } } /** * Set event list * * @param events new event list */ public void setEvents(ArrayList<Event> events) { this.events = events; } /** * Getters from the class */ public int getValid() { return this.valid; } public String getService() { return this.service; } public ArrayList<Event> getEvents() { return this.events; } public String getLicensePlate() { return this.licensePlate; } public String getTime() { return this.time; } public double getLatitude() { return this.latitude; } public double getLongitude() { return this.longitude; } public String getDirection() { return this.direction; } public String getServiceRoute() { return route; } public int getPassengerNumber() { return this.passengerNumber; } public int getBusIcon() { return this.busIcon; } public int getActiveMapBusIcon() { return this.activeMapBusIcon; } public String getFormattedDistance() { return this.distance; } public int getDistance() { return realDistance; } public String getDescriptionError() { return this.descriptionError; } /** * Gives the amount of reports that the bus has. * * @return the amount of reports */ public int getAmountOfReports() { return this.events.size(); } /** * Returns the reference to the icon to draw in the map. If there are persons streaming in the * bus, returns the active icon. Otherwise returns the default icon. * * @return a reference to the icon to draw */ public int getMapBusIcon() { if (this.passengerNumber >= 1) return this.activeMapBusIcon; else return this.defaultMapBusIcon; } /** * Adds a streaming passenger to the bus */ public void addPassenger() { this.passengerNumber += 1; } /** * Provides a parsed string that represents the time to arrive at the bus stop. */ private String timeToString(String time) { String response = time.toLowerCase(); response = response.substring(0, 1).toUpperCase() + response.substring(1); return response; } public String getFormattedPlate() { String part1 = this.licensePlate.substring(0, 2); String part2 = this.licensePlate.substring(2, 4); String part3 = this.licensePlate.substring(4, 6); return part1 + "" + part2 + "" + part3; } @Override public String toString() { String string = "Bus Object\n"; string += "valid= " + this.valid + "\n"; string += "service= " + this.service + "\n"; string += "licensePlate= " + this.licensePlate + "\n"; string += "time= " + this.time + "\n"; string += "distance= " + this.distance + "\n"; string += "latitude= " + this.latitude + "\n"; string += "longitude= " + this.longitude + "\n"; string += "passengerNumber= " + this.passengerNumber + "\n"; string += "descriptionError= " + this.descriptionError + "\n"; string += "Events\n"; string += ""; for (Event aEvent : this.events) { string += aEvent.getDescription() + "\n"; } return string; } /** * Provides some bus info as JSONObject. If toString from JSONObject is called, you shoud get * something like: {"bus":{"service":"506v","licensePlate":"FLXG33",...}}. * * @return JSONObject with the bus info */ @Override public JSONObject toJSONObject() { JSONObject properties = new JSONObject(); JSONObject bus = new JSONObject(); try { properties.put("service", this.service); properties.put("licensePlate", this.licensePlate); properties.put("latitude", this.latitude); properties.put("longitude", this.longitude); bus.put("bus", properties); } catch (JSONException e) { e.printStackTrace(); } return bus; } public static List<Bus> filterDummyPlateBuses(List<Bus> buses) { ArrayList<Bus> newBuses = new ArrayList<>(); for (Bus b : buses) { if (!b.getLicensePlate().equals(Constants.DUMMY_LICENSE_PLATE)) { newBuses.add(b); } } return newBuses; } /** * The code below is needed to implement Parcelable. Note the the events are NOT captured. */ /** * Allows to recreate a bus from a Parcel */ protected Bus(Parcel in) { valid = in.readInt(); service = in.readString(); licensePlate = in.readString(); time = in.readString(); distance = in.readString(); latitude = in.readDouble(); longitude = in.readDouble(); passengerNumber = in.readInt(); busIcon = in.readInt(); defaultMapBusIcon = in.readInt(); activeMapBusIcon = in.readInt(); direction = in.readString(); descriptionError = in.readString(); destination = in.readString(); origin = in.readString(); color = in.readInt(); realDistance = in.readInt(); } /** * Needed to recreate a bus */ public static final Creator<Bus> CREATOR = new Creator<Bus>() { @Override public Bus createFromParcel(Parcel in) { return new Bus(in); } @Override public Bus[] newArray(int size) { return new Bus[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(valid); dest.writeString(service); dest.writeString(licensePlate); dest.writeString(time); dest.writeString(distance); dest.writeDouble(latitude); dest.writeDouble(longitude); dest.writeInt(passengerNumber); dest.writeInt(busIcon); dest.writeInt(defaultMapBusIcon); dest.writeInt(activeMapBusIcon); dest.writeString(direction); dest.writeString(descriptionError); dest.writeString(destination); dest.writeString(origin); dest.writeInt(color); dest.writeInt(realDistance); } public void setIconsTo(int color) { this.busIcon = Constants.BUS_LIST.get(color); this.defaultMapBusIcon = Constants.MAP_BUS_LIST.get(color); this.activeMapBusIcon = Constants.ACTIVE_BUS_LIST.get(color); } @Override public String getSuggestionTitle() { return getService(); } @Override public String getSuggestionSubtitle() { return getServiceRoute(); } @Override public int getSuggestionIcon() { return getBusIcon(); } @Override public void dispatchCorrectly(SuggestionDispatcher dispatcher) { dispatcher.onSuggestionClick(this); } public int getColor() { return color; } public String getOrigin() { return origin; } public String getDestination() { return destination; } }