diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 19e67ff..23acf97 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,88 +1,102 @@ + + + + + + + + - \ No newline at end of file + diff --git a/res/values/strings.xml b/res/values/strings.xml index 7081266..f55a45a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,90 +1,91 @@ BusTO You\'re using the latest in technology when it comes to respecting your privacy. Search Scan QR Code Bus stop number Bus stop name Insert bus stop number Insert bus stop name %1$s towards %2$s %s (unknown destination) Verify your Internet connection! Seems that no bus stop have this name Error parsing the 5T/GTT website (damn site!) Name too short, type more characters and retry Arrivals at: %1$s Choose the bus stop… Lines: %1$s No timetable found No QR code Unexpected internal error, cannot extract data from GTT/5T website Help About More about News releases Bug submission Source code Licence Meet the author Bus stop is now in your favorites Favorites Favorites Press on a bus stop name to populate this list Delete Rename Rename the bus stop Reset About Tap the bus stop name to add it to the favourites\n\nHow to read timelines:\n   12:56* Real-time arrivals\n   12:56   Scheduled arrivals\n\nPull down to refresh the timetable GOT IT! Welcome!

Thanks for using BusTO, a "politically" independent app useful to move around Torino using a Free/Libre software.

Why use this app?

- You\'ll never be tracked
- You\'ll never see boring ads
- We\'ll always respect your privacy
- Moreover, it\'s lightweight!

How does it work?

This app will show you bus timetables gathering data from www.gtt.to.it or www.5t.torino.it "for personal use".

Who worked on BusTO:
- Valerio Bozzolan main developer.
- Marco Gagino main contributor and icon creator.
- Paolo Alloatti original concept of the icon.
- JSoup web scraper library.
- makovkastar floating buttons.
- Google Material Design icons.
- All the contributors!

Licenses

The app and the related source code are released by Valerio Bozzolan under the terms of the GNU General Public License v3+). So everyone is allowed to use, to study, to improve and to share this app by any kind of means and for any purpose: under the conditions of maintaining this rights and of attributing the original work to Valerio Bozzolan.

Notes

This app has been developed hoping to be useful to everyone but without ANY warranty.

This translation is kindly provided by Riccardo Caniato and Marco Gagino.

Get involved! :)

]]>
Cannot add to favorites (storage full or corrupted database?)! View on a map ListFragment - BusTO + it.reyboz.bustorino.preferences
diff --git a/src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java b/src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java index ba9682e..bdae276 100644 --- a/src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java +++ b/src/it/reyboz/bustorino/backend/FiveTAPIFetcher.java @@ -1,275 +1,333 @@ /* BusTO - Backend components Copyright (C) 2018 Fabio Mazza This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import android.support.annotation.Nullable; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.concurrent.atomic.AtomicReference; public class FiveTAPIFetcher implements ArrivalsFetcher{ private static final String SECRET_KEY="759C97DC7D115966C30FD9169BB200D9"; private static final String DEBUG_NAME = "FiveTAPIFetcher"; final static LinkedList apiDays = new LinkedList<>(Arrays.asList("dom","lun","mar","mer","gio","ven","sab")); @Override public Palina ReadArrivalTimesAll(String stopID, AtomicReference res) { //set the date for the request as now Palina p = new Palina(stopID); //request parameters String response = performAPIRequest(QueryType.ARRIVALS,stopID,res); - if(response==null){ - //an error has occured, details in res.get() + + if(response==null) { + if(res.get()==result.SERVER_ERROR_404) { + Log.w(DEBUG_NAME,"Got 404, either the server failed, or the stop was not found, or the hack is not working anymore"); + res.set(result.EMPTY_RESULT_SET); + }; return p; } /* Slight problem: "longName": ==> DESCRIPTION "name": "13N", "departures": [ { "arrivalTimeInt": 1272, "time": "21:12", "rt": false }] "lineType": "URBANO" ==> URBANO can be either bus or tram or METRO */ JSONArray arr; try{ arr = new JSONArray(response); String type; Route.Type routetype; for(int i =0; i getDirectionsForStop(String stopID, AtomicReference res) { String response = performAPIRequest(QueryType.DETAILS,stopID,res); if(response == null) return null; ArrayList routes = new ArrayList<>(10); try { JSONArray lines =new JSONArray(response); for(int i=0; i 1) { String secondo = exploded[exploded.length-2]; if (secondo.contains("festivo")) { - festivo = 1; + festivo = Route.FestiveInfo.FESTIVO; } else if (secondo.contains("feriale")) { - festivo = 0; + festivo = Route.FestiveInfo.FERIALE; } else if(secondo.contains("lun. - ven")) { serviceDays = Route.reduced_week; } else if(secondo.contains("sab - fest.")){ serviceDays = Route.weekend; - festivo = 1; + festivo = Route.FestiveInfo.FESTIVO; } else { Log.d(DEBUG_NAME,"Parsing details of line "+lineName+" branchid "+branchid+":\n\t"+ "Couldn't find a the service days\n"+ "Description: "+secondo+","+description ); } if(exploded.length>2){ switch (exploded[exploded.length-3].trim()) { case "bus": t = Route.Type.BUS; break; case "tram": //never happened, but if it could happen you can get it t = Route.Type.TRAM; break; default: //nothing } } - } + } else //only one piece + if(description.contains("festivo")){ + festivo = Route.FestiveInfo.FESTIVO; + } else if(description.contains("feriale")){ + festivo = Route.FestiveInfo.FERIALE; + } if(lineName.trim().equals("10")|| lineName.trim().equals("15")) t= Route.Type.TRAM; + if(direction.contains("-")){ + // + direction = direction.split("-")[1]; + } Route r = new Route(lineName.trim(),direction.trim(),t,null); if(serviceDays.length>0) r.serviceDays = serviceDays; - r.festivo = Route.FestiveInfo.returnTypefromCode(festivo); + r.festivo = festivo; r.branchid = branchid; r.description = description.trim(); r.setStopsList(Arrays.asList(stops.split(","))); routes.add(r); - res.set(result.OK); } + + res.set(result.OK); + } catch (JSONException e) { res.set(result.PARSER_ERROR); e.printStackTrace(); return null; } return routes; } + public List getAllStopsFromGTT(AtomicReference res){ + + String response = performAPIRequest(QueryType.STOPS_ALL,null,res); + ArrayList stopslist = null; + try{ + JSONArray stops = new JSONArray(response); + stopslist = new ArrayList<>(stops.length()); + for (int i=0;i res){ Date d = new Date(); URL u; Hashtable param = new Hashtable<>(); try { String address = getURLForOperation(t,stopID); //Log.d(DEBUG_NAME,"The address to query is: "+address); param.put("TOKEN",getAccessToken(address,d)); param.put("TIMESTAMP",String.valueOf(d.getTime())); param.put("Accept-Encoding","gzip"); param.put("Connection","Keep-Alive"); u = new URL(address); } catch (UnsupportedEncodingException | NoSuchAlgorithmException |MalformedURLException e) { e.printStackTrace(); res.set(result.PARSER_ERROR); return null; } String response = networkTools.queryURL(u,res,param); - if(response==null) { - if(res.get()==result.SERVER_ERROR_404) { - Log.w(DEBUG_NAME,"Got 404, either the server failed, or the stop was not found, or the hack is not working anymore"); - res.set(result.EMPTY_RESULT_SET); - }; - } + return response; } /** * Get the Token needed to access the API * @param URL the URL of the request * @return token * @throws NoSuchAlgorithmException if the system doesn't support MD5 * @throws UnsupportedEncodingException if we made mistakes in writing utf-8 */ private static String getAccessToken(String URL,Date d) throws NoSuchAlgorithmException,UnsupportedEncodingException{ MessageDigest md = MessageDigest.getInstance("MD5"); String strippedQuery = URL.replace("http://www.5t.torino.it/proxyws",""); //return the time in milliseconds long timeMilli = d.getTime(); StringBuilder sb = new StringBuilder(); sb.append(strippedQuery); sb.append(timeMilli); sb.append(SECRET_KEY); String stringToBeHashed = sb.toString(); //Log.d(DEBUG_NAME,"Hashing string: "+stringToBeHashed); md.reset(); byte[] data = md.digest(stringToBeHashed.getBytes("UTF-8")); sb = new StringBuilder(); for (byte b : data){ sb.append(String.format("%02x",b)); } String result = sb.toString(); //Log.d(DEBUG_NAME,"getting token:\n\treduced URL: "+strippedQuery+"\n\ttimestamp: "+timeMilli+"\nTOKEN:"+result.toLowerCase()); return result.toLowerCase(); } /** * Get the right url for the operation you are doing, to be fed into the queryURL method * @param t type of operation * @param stopID stop on which you are working on * @return the Url to go to * @throws UnsupportedEncodingException if it cannot be converted to utf-8 */ public static String getURLForOperation(QueryType t,@Nullable String stopID) throws UnsupportedEncodingException { final StringBuilder sb = new StringBuilder(); sb.append("http://www.5t.torino.it/proxyws/ws2.1/rest/stops/"); switch (t){ case ARRIVALS: sb.append(URLEncoder.encode(stopID,"utf-8")); sb.append("/departures"); break; case DETAILS: sb.append(URLEncoder.encode(stopID,"utf-8")); sb.append("/branches/details"); break; case STOPS_ALL: sb.append("all"); break; case STOPS_VERSION: sb.append("version"); break; } return sb.toString(); } - enum QueryType { + public enum QueryType { ARRIVALS, DETAILS,STOPS_ALL, STOPS_VERSION } } diff --git a/src/it/reyboz/bustorino/backend/FiveTNormalizer.java b/src/it/reyboz/bustorino/backend/FiveTNormalizer.java index 8653cc1..a1f0e23 100644 --- a/src/it/reyboz/bustorino/backend/FiveTNormalizer.java +++ b/src/it/reyboz/bustorino/backend/FiveTNormalizer.java @@ -1,249 +1,286 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; /** * Converts some weird stop IDs found on the 5T website to the form used everywhere else (including GTT website). *

* A stop ID in normalized form is:
* - a string containing a number without leading zeros
* - a string beginning with "ST" and then a number
* - whatever the GTT website uses.
*

* A bus route ID in normalized form is:
* - a string containing a number and optionally: "B", "CS", "CD", "N", "S", "C" at the end
* - the string "METRO"
* - "ST1" or "ST2" (Star 1 and Star 2)
* - a string beginning with E, N, S or W, followed by two digits (with a leading zero)
* - "RV2", "OB1"
* - "CAN" or "FTC" (railway lines)
* - ...screw it, let's just hope all the websites and APIs return something sane as route IDs.
*

* This class exists because Java doesn't support traits.
*
* Note: this class also just became useless, as 5T now uses the same format as GTT website. */ public abstract class FiveTNormalizer { public static String FiveTNormalizeRoute(String RouteID) { while (RouteID.startsWith("0")) { RouteID = RouteID.substring(1); } return RouteID; } // public static String FiveTNormalizeStop(String StopID) { // StopID = FiveTNormalizeRoute(StopID); // // is this faster than a regex? // if (StopID.length() == 5 && StopID.startsWith("ST") && Character.isLetter(StopID.charAt(2)) && Character.isLetter(StopID.charAt(3)) && Character.isLetter(StopID.charAt(4))) { // switch (StopID) { // case "STFER": // return "8210"; // case "STPAR": // return "8211"; // case "STMAR": // return "8212"; // case "STMAS": // return "8213"; // case "STPOS": // return "8214"; // case "STMGR": // return "8215"; // case "STRIV": // return "8216"; // case "STRAC": // return "8217"; // case "STBER": // return "8218"; // case "STPDA": // return "8219"; // case "STDOD": // return "8220"; // case "STPSU": // return "8221"; // case "STVIN": // return "8222"; // case "STREU": // return "8223"; // case "STPNU": // return "8224"; // case "STMCI": // return "8225"; // case "STNIZ": // return "8226"; // case "STDAN": // return "8227"; // case "STCAR": // return "8228"; // case "STSPE": // return "8229"; // case "STLGO": // return "8230"; // } // } // return StopID; // } // // public static String NormalizedToFiveT(final String StopID) { // if(StopID.startsWith("82") && StopID.length() == 4) { // switch (StopID) { // case "8230": // return "STLGO"; // case "8229": // return "STSPE"; // case "8228": // return "STCAR"; // case "8227": // return "STDAN"; // case "8226": // return "STNIZ"; // case "8225": // return "STMCI"; // case "8224": // return "STPNU"; // case "8223": // return "STREU"; // case "8222": // return "STVIN"; // case "8221": // return "STPSU"; // case "8220": // return "STDOD"; // case "8219": // return "STPDA"; // case "8218": // return "STBER"; // case "8217": // return "STRAC"; // case "8216": // return "STRIV"; // case "8215": // return "STMGR"; // case "8214": // return "STPOS"; // case "8213": // return "STMAS"; // case "8212": // return "STMAR"; // case "8211": // return "STPAR"; // case "8210": // return "STFER"; // } // } // // return StopID; // } public static Route.Type decodeType(final String routename, final String bacino) { if(routename.equals("METRO")) { return Route.Type.METRO; } else if(routename.equals("79")) { return Route.Type.RAILWAY; } switch (bacino) { case "U": return Route.Type.BUS; case "F": return Route.Type.RAILWAY; case "E": return Route.Type.LONG_DISTANCE_BUS; default: return Route.Type.BUS; } } + //TODO: DO THE OPPOSITE FOR FIVETAPIFETCHER /** * Converts a route ID from internal format to display format, returns null if it has the same name. * * @param routeID ID in "internal" and normalized format * @return string with display name, null if unchanged */ public static String routeInternalToDisplay(final String routeID) { if(routeID.length() == 3 && routeID.charAt(2) == 'B') { return routeID.substring(0,2).concat("/"); } switch(routeID) { case "1C": return "1 Chieri"; case "1N": return "1 Nichelino"; case "OB1": return "1 Orbassano"; case "2C": return "2 Chieri"; case "RV2": return "2 Rivalta"; case "CO1": return "Circolare Collegno"; case "SE1": // I wonder why GTT calls this "SE1" while other absurd names have a human readable name too. return "1 Settimo"; case "79": return "Cremagliera Sassi-Superga"; case "W01": return "Night Buster 1 Arancio"; case "N10": return "Night Buster 10 Gialla"; case "W15": return "Night Buster 15 Rosa"; case "S18": return "Night Buster 18 Blu"; case "S04": return "Night Buster 4 Azzurra"; case "N4": return "Night Buster 4 Rossa"; case "N57": return "Night Buster 57 Oro"; case "W60": return "Night Buster 60 Argento"; case "E68": return "Night Buster 68 Verde"; case "S05": return "Night Buster 5 Viola"; case "ST1": return "Star 1"; case "ST2": return "Star 2"; case "4N": return "4 Navetta"; case "10N": return "10 Navetta"; case "13N": return "13 Navetta"; case "35N": return "35 Navetta"; case "36N": return "36 Navetta"; case "36S": return "36 Speciale"; case "38S": return "38 Speciale"; case "44S": return "44 Scolastico"; case "46N": return "46 Navetta"; default: return null; } } + public static String routeDisplayToInternal(String displayName){ + if(displayName.trim().charAt(displayName.length()-1)=='/'){ + return displayName.replace(" ","").replace("/","B"); + } + switch (displayName.trim().toLowerCase()){ + case "star 1": + return "ST1"; + case "star 2": + return "ST2"; + case "night buster 1 arancio": + return "W01"; + case "night buster 10 gialla": + return "N10"; + case "night buster 15 rosa": + return "W15"; + case "night buster 18 blu": + return "S18"; + case "night buster 4 azzurra": + return "S04"; + case "night buster 4 rossa": + return "N4"; + case "night buster 57 oro": + return "N57"; + case "night buster 60 argento": + return "W60"; + case "night buster 68 verde": + return "E68"; + case "night buster 5 viola": + return "S05"; + case "1 nichelino": + return "1N"; + default: + return displayName.trim(); + } + } + } diff --git a/src/it/reyboz/bustorino/backend/Route.java b/src/it/reyboz/bustorino/backend/Route.java index bb1ca23..cfbd774 100644 --- a/src/it/reyboz/bustorino/backend/Route.java +++ b/src/it/reyboz/bustorino/backend/Route.java @@ -1,258 +1,262 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.backend; import android.support.annotation.NonNull; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.List; public class Route implements Comparable { final static int[] reduced_week = {Calendar.MONDAY,Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,Calendar.FRIDAY}; final static int[] feriali = {Calendar.MONDAY,Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,Calendar.FRIDAY,Calendar.SATURDAY}; final static int[] weekend = {Calendar.SUNDAY,Calendar.SATURDAY}; final static int BRANCHID_MISSING = -1; public final String name; public String destinazione; public final List passaggi; public final Type type; public String description; //ordered list of stops, from beginning to end of line private List stopsList = null; public int branchid = BRANCHID_MISSING; public int[] serviceDays ={}; //0=>feriale, 1=>festivo -2=>unknown public FestiveInfo festivo = FestiveInfo.UNKNOWN; public enum Type { // "long distance" sono gli extraurbani. BUS, LONG_DISTANCE_BUS, METRO, RAILWAY, TRAM } public enum FestiveInfo{ FESTIVO(1),FERIALE(0),UNKNOWN(-2); private int code; FestiveInfo(int code){ this.code = code; } public int getCode() { return code; } public static FestiveInfo returnTypefromCode(int i){ switch (i){ case -2: return UNKNOWN; case 0: return FERIALE; case 1: return FESTIVO; default: return UNKNOWN; } } } /** * Constructor. * * @param name route ID * @param destinazione terminus\end of line * @param type bus, long distance bus, underground, and so on * @param passaggi timetable, a good choice is an ArrayList of size 6 * @see Palina Palina.addRoute() method */ public Route(String name, String destinazione, Type type, List passaggi) { this.name = name; this.destinazione = destinazione; this.passaggi = passaggi; this.type = type; this.description = null; } /** * Constructor used by the new Api * @param name stop Name * @param t optional type * @param description line rough description */ public Route(String name,Type t,String description){ this.name = name; this.type = t; this.passaggi = new ArrayList<>(); this.destinazione = null; this.description = description; } /** * Exactly what it says on the tin. * * @return times from the timetable */ public List getPassaggi() { return this.passaggi; } public void setStopsList(List stopsList) { this.stopsList = Collections.unmodifiableList(stopsList); } public List getStopsList(){ return this.stopsList; } /** * Adds a time (passaggio) to the timetable for this route * * @param TimeGTT time in GTT format (e.g. "11:22*") */ public void addPassaggio(String TimeGTT) { this.passaggi.add(new Passaggio(TimeGTT)); } public static String getPassageString(String input,boolean realtime){ String time = input.trim(); if(time.contains("*")){ if(realtime) return time; else return time.substring(0,time.length()-1); } else{ if(realtime) return time.concat("*"); else return time; } } @Override public int compareTo(@NonNull Route other) { int res; int thisAsInt, otherAsInt; // sorting by numbers alone yields a far more "natural" result (36N goes before 2024, 95B next to 95, and the like) thisAsInt = networkTools.failsafeParseInt(this.name.replaceAll("[^0-9]", "")); otherAsInt = networkTools.failsafeParseInt(other.name.replaceAll("[^0-9]", "")); // compare. // numeric route IDs (names) if(thisAsInt != 0 && otherAsInt != 0) { res = thisAsInt - otherAsInt; if(res != 0) { return res; } } else { // non-numeric res = this.name.compareTo(other.name); if (res != 0) { return res; } } // try comparing their destination if(this.destinazione!=null){ res = this.destinazione.compareTo(other.destinazione); if(res != 0) { return res; } } //compare the lines if(this.stopsList!=null && other.stopsList!=null){ int d = this.stopsList.size()-other.stopsList.size(); if(d!=0) return d; else { //the two have the same number of stops } } // probably useless, but... last attempt. if(this.type != other.type) { // ordinal() is evil or whatever, who cares. return this.type.ordinal() - other.type.ordinal(); } return 0; } public boolean isBranchIdValid(){ return branchid!=BRANCHID_MISSING; } @Override public boolean equals(Object obj) { if(obj instanceof Route){ Route r = (Route) obj; boolean result = false; if(this.name.equals(r.name) && this.branchid == r.branchid){ if(this.stopsList!=null && r.stopsList!=null){ int d = this.stopsList.size()-r.stopsList.size(); if(d!=0) { result = false; } else { result = true; for(int j=0; j. */ package it.reyboz.bustorino.backend; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import java.net.URLEncoder; import java.util.List; import java.util.Locale; public class Stop implements Comparable { // remove "final" in case you need to set these from outside the parser\scrapers\fetchers public final @NonNull String ID; private @Nullable String name; private @Nullable String username; public @Nullable String location; public final @Nullable Route.Type type; private @Nullable List routesThatStopHere; private final @Nullable Double lat; private final @Nullable Double lon; // leave this non-final private @Nullable String routesThatStopHereString = null; + private @Nullable String absurdGTTPlaceName = null; /** * Hey, look, method overloading! */ public Stop(final @Nullable String name, final @NonNull String ID, @Nullable final String location, @Nullable final Route.Type type, @Nullable final List routesThatStopHere) { this.ID = ID; this.name = name; this.username = null; this.location = (location != null && location.length() == 0) ? null : location; this.type = type; this.routesThatStopHere = routesThatStopHere; this.lat = null; this.lon = null; } /** * Hey, look, method overloading! */ public Stop(final @NonNull String ID) { this.ID = ID; this.name = null; this.username = null; this.location = null; this.type = null; this.routesThatStopHere = null; this.lat = null; this.lon = null; } /** * Constructor that sets EVERYTHING. */ public Stop(@NonNull String ID, @Nullable String name, @Nullable String userName, @Nullable String location, @Nullable Route.Type type, @Nullable List routesThatStopHere, @Nullable Double lat, @Nullable Double lon) { this.ID = ID; this.name = name; this.username = userName; this.location = location; this.type = type; this.routesThatStopHere = routesThatStopHere; this.lat = lat; this.lon = lon; } + public @Nullable String routesThatStopHereToString() { // M E M O I Z A T I O N if(this.routesThatStopHereString != null) { return this.routesThatStopHereString; } // no string yet? build it! return buildString(); } + @Nullable + public String getAbsurdGTTPlaceName() { + return absurdGTTPlaceName; + } + + public void setAbsurdGTTPlaceName(@NonNull String absurdGTTPlaceName) { + this.absurdGTTPlaceName = absurdGTTPlaceName; + } + public void setRoutesThatStopHere(@Nullable List routesThatStopHere) { this.routesThatStopHere = routesThatStopHere; } private @Nullable String buildString() { // no routes => no string if(this.routesThatStopHere == null || this.routesThatStopHere.size() == 0) { return null; } StringBuilder sb = new StringBuilder(); int i, lenMinusOne = routesThatStopHere.size() - 1; for (i = 0; i < lenMinusOne; i++) { sb.append(routesThatStopHere.get(i)).append(", "); } // last one: sb.append(routesThatStopHere.get(i)); this.routesThatStopHereString = sb.toString(); return this.routesThatStopHereString; } @Override public int compareTo(@NonNull Stop other) { int res; int thisAsInt = networkTools.failsafeParseInt(this.ID); int otherAsInt = networkTools.failsafeParseInt(other.ID); // numeric stop IDs if(thisAsInt != 0 && otherAsInt != 0) { return thisAsInt - otherAsInt; } else { // non-numeric res = this.ID.compareTo(other.ID); if (res != 0) { return res; } } // try with name, then if(this.name != null && other.name != null) { res = this.name.compareTo(other.name); } // and give up return res; } /** * Sets a name. * * @param name stop name as string (not null) */ public final void setStopName(@NonNull String name) { this.name = name; } /** * Sets user name. Empty string is converted to null. * * @param name a string of non-zero length, or null */ public final void setStopUserName(@Nullable String name) { if(name == null) { this.username = null; } else if(name.length() == 0) { this.username = null; } else { this.username = name; } } /** * Returns stop name or username (if set).
* - empty string means "already searched everywhere, can't find it"
* - null means "didn't search, yet. Maybe you should try."
* - string means "here's the name.", obviously.
* * @return string if known, null if still unknown */ public final @Nullable String getStopDisplayName() { if(this.username == null) { return this.name; } else { return this.username; } } /** * Same as getStopDisplayName, only returns default name.
* I'd use an @see tag, but Android Studio is incapable of understanding that getStopDefaultName * refers to the method exactly above this one and not some arcane and esoteric unknown symbol. */ public final @Nullable String getStopDefaultName() { return this.name; } /** * Same as getStopDisplayName, only returns user name.
* Also, never an empty string. */ public final @Nullable String getStopUserName() { return this.username; } /** * Gets username and name from other stop if they exist, sets itself accordingly. * * @param other another Stop * @return did we actually set/change anything? */ public final boolean mergeNameFrom(Stop other) { boolean ret = false; if(other.name != null) { if(this.name == null || !this.name.equals(other.name)) { this.name = other.name; ret = true; } } if(other.username != null) { if(this.username == null || !this.username.equals(other.username)) { this.username = other.username; ret = true; } } return ret; } public final @Nullable String getGeoURL() { if(this.lat == null || this.lon == null) { return null; } // Android documentation suggests US for machine readable output (use dot as decimal separator) return String.format(Locale.US, "geo:%f,%f", this.lat, this.lon); } public final @Nullable String getGeoURLWithAddress() { String url = getGeoURL(); if(url == null) { return null; } if(this.location != null) { try { String addThis = "?q=".concat(URLEncoder.encode(this.location, "utf-8")); return url.concat(addThis); } catch (Exception ignored) {} } return url; } } diff --git a/src/it/reyboz/bustorino/middleware/AppDataProvider.java b/src/it/reyboz/bustorino/middleware/AppDataProvider.java new file mode 100644 index 0000000..9eca718 --- /dev/null +++ b/src/it/reyboz/bustorino/middleware/AppDataProvider.java @@ -0,0 +1,134 @@ +/* + BusTO (middleware) + Copyright (C) 2018 Fabio Mazza + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ +package it.reyboz.bustorino.middleware; + +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; + +import java.net.URI; + +public class AppDataProvider extends ContentProvider { + + public static final String AUTHORITY = "it.reyboz.bustorino.provider"; + private static final int STOP_OP = 1; + private static final int LINE_OP = 2; + private static final int BRANCH_OP = 3; + private static final int FAVORITES_OP =4; + private static final int MANY_STOPS = 5; + private static final int ADD_UPDATE_BRANCHES = 6; + private static final int LINE_INSERT_OP = 7; + + private NextGenDB appDBHelper; + private SQLiteDatabase db; + + public AppDataProvider() { + } + private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); + + static { + /* + * The calls to addURI() go here, for all of the content URI patterns that the provider + * should recognize. + */ + + sUriMatcher.addURI(AUTHORITY, "stop/#", STOP_OP); + sUriMatcher.addURI(AUTHORITY,"stops",MANY_STOPS); + + /* + * Sets the code for a single row to 2. In this case, the "#" wildcard is + * used. "content://com.example.app.provider/table3/3" matches, but + * "content://com.example.app.provider/table3 doesn't. + */ + sUriMatcher.addURI(AUTHORITY, "line/#/", LINE_OP); + sUriMatcher.addURI(AUTHORITY,"branch/#",BRANCH_OP); + sUriMatcher.addURI(AUTHORITY,"line/insert",LINE_INSERT_OP); + + sUriMatcher.addURI(AUTHORITY,"updatebranches/",ADD_UPDATE_BRANCHES); + } + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + // Implement this to handle requests to delete one or more rows. + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public String getType(Uri uri) { + // TODO: Implement this to handle requests for the MIME type of the data + // at the given URI. + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + // TODO: Implement this to handle requests to insert a new row. + //throw new UnsupportedOperationException("Not yet implemented"); + db = appDBHelper.getWritableDatabase(); + Uri finalUri = null; + long last_rowid; + switch (sUriMatcher.match(uri)){ + case ADD_UPDATE_BRANCHES: + String line_name = values.getAsString(NextGenDB.Contract.LinesTable.COLUMN_NAME); + if(line_name==null) throw new IllegalArgumentException("No line name given"); + values.remove(NextGenDB.Contract.LinesTable.COLUMN_NAME); + Cursor c = db.query(NextGenDB.Contract.LinesTable.TABLE_NAME, + new String[]{NextGenDB.Contract.LinesTable._ID},NextGenDB.Contract.LinesTable.COLUMN_NAME+"like ?s", + new String[]{line_name},null,null,null); + long lineid = c.getInt(0); + c.close(); + values.put(NextGenDB.Contract.BranchesTable.COL_LINE,lineid); + long rowid = db.insertWithOnConflict(NextGenDB.Contract.BranchesTable.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_REPLACE); + finalUri= Uri.parse("content://"+AUTHORITY+"/branches/"+rowid); + break; + case MANY_STOPS: + last_rowid = db.insertOrThrow(NextGenDB.Contract.StopsTable.TABLE_NAME,null,values); + finalUri = ContentUris.withAppendedId(uri,last_rowid); + break; + default: + throw new IllegalArgumentException("Invalid parameters"); + } + return finalUri; + } + + @Override + public boolean onCreate() { + // TODO: Implement this to initialize your content provider on startup. + appDBHelper = new NextGenDB(getContext()); + return false; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder) { + // TODO: Implement this to handle query requests from clients. + throw new UnsupportedOperationException("Not yet implemented"); + } + + @Override + public int update(Uri uri, ContentValues values, String selection, + String[] selectionArgs) { + // TODO: Implement this to handle requests to update one or more rows. + throw new UnsupportedOperationException("Not yet implemented"); + } + + +} diff --git a/src/it/reyboz/bustorino/middleware/AsyncDataDownload.java b/src/it/reyboz/bustorino/middleware/AsyncDataDownload.java index 205fd86..c3d1e9b 100644 --- a/src/it/reyboz/bustorino/middleware/AsyncDataDownload.java +++ b/src/it/reyboz/bustorino/middleware/AsyncDataDownload.java @@ -1,264 +1,268 @@ /* BusTO (middleware) Copyright (C) 2018 Fabio Mazza This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.middleware; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.util.Log; import it.reyboz.bustorino.R; import it.reyboz.bustorino.backend.*; import it.reyboz.bustorino.fragments.FragmentHelper; import java.lang.ref.WeakReference; import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** * This should be used to download data, but not to display it */ public class AsyncDataDownload extends AsyncTask{ private static final String TAG = "BusTO-DataDownload"; private boolean failedAll = false; private AtomicReference res; private RequestType t; private String query; WeakReference helperRef; public AsyncDataDownload(RequestType type,FragmentHelper fh) { t = type; helperRef = new WeakReference<>(fh); res = new AtomicReference<>(); } @Override protected Object doInBackground(String... params) { RecursionHelper r; boolean success=false; Object result; switch (t){ case ARRIVALS: r = new RecursionHelper<>(new ArrivalsFetcher[] {new FiveTAPIFetcher(),new GTTJSONFetcher(), new FiveTScraperFetcher()}); break; case STOPS: r = new RecursionHelper<>(new StopsFinderByName[] {new GTTStopsFetcher(), new FiveTStopsFetcher()}); break; default: //TODO put error message - cancel(true); return null; } FragmentHelper fh = helperRef.get(); //If the FragmentHelper is null, that means the activity doesn't exist anymore if (fh == null){ return null; } //Log.d(TAG,"refresh layout reference is: "+fh.isRefreshLayoutReferenceTrue()); while(r.valid()) { if(this.isCancelled()) { return null; } //get the data from the fetcher switch (t){ case ARRIVALS: ArrivalsFetcher f = (ArrivalsFetcher) r.getAndMoveForward(); Stop lastSearchedBusStop = fh.getLastSuccessfullySearchedBusStop(); Palina p; String stopID; if(params.length>0) stopID=params[0]; //(it's a Palina) else if(lastSearchedBusStop!=null) stopID = lastSearchedBusStop.ID; //(it's a Palina) else { publishProgress(Fetcher.result.QUERY_TOO_SHORT); return null; } p= f.ReadArrivalTimesAll(stopID,res); publishProgress(res.get()); - //Try to find the name of the stop inside StopsDB - fh.openStopsDB(); - if(p.getStopDisplayName() == null){ - p.setStopName(fh.getStopNamefromDB(p.ID)); - } - fh.closeDBIfNeeded(); + if(f instanceof FiveTAPIFetcher){ AtomicReference gres = new AtomicReference<>(); - List branches = ((FiveTAPIFetcher) f).getDirectionsForStop(params[0],gres); - if(res.get() == Fetcher.result.OK){ + List branches = ((FiveTAPIFetcher) f).getDirectionsForStop(stopID,gres); + if(gres.get() == Fetcher.result.OK){ p.addInfoFromRoutes(branches); } //put updated values into Database } //TODO: use ContentProvider when ready - /* + if(lastSearchedBusStop != null && res.get()== Fetcher.result.OK) { // check that we don't have the same stop if(!lastSearchedBusStop.ID.equals(p.ID)) { // remove it, get new name - getNameOrGetRekt(); + //getNameOrGetRekt(); + //TODO } else { // searched and it's the same String sn = lastSearchedBusStop.getStopDisplayName(); if(sn == null) { // something really bad happened, start from scratch - getNameOrGetRekt(); + //getNameOrGetRekt(); + //TODO } else { // "merge" Stop over Palina and we're good to go p.mergeNameFrom(lastSearchedBusStop); } } } else if(res.get()== Fetcher.result.OK) { // we haven't searched anything yet - getNameOrGetRekt(); + //getNameOrGetRekt(); } - */ + + //Try to find the name of the stop inside StopsDB + + if(p.getStopDisplayName() == null){ + fh.openStopsDB(); + p.setStopName(fh.getStopNamefromDB(p.ID)); + fh.closeDBIfNeeded(); + } + result = p; Log.d(TAG,"Using the ArrivalsFetcher: "+f.getClass()); //TODO: find a way to avoid overloading the user with toasts break; case STOPS: StopsFinderByName finder = (StopsFinderByName) r.getAndMoveForward(); List resultList= finder.FindByName(params[0], this.res); //it's a List fh.openStopsDB(); for (Stop stop : resultList){ if(stop.location == null) stop.location = fh.getLocationFromDB(stop); stop.setRoutesThatStopHere(fh.getStopRoutesFromDB(stop.ID)); } fh.closeDBIfNeeded(); Log.d(TAG,"Using the StopFinderByName: "+finder.getClass()); query =params[0]; result = resultList; //dummy result break; default: result = null; } //find if it went well if(res.get()== Fetcher.result.OK) { return result; } } //at this point, we are sure that the result has been negative failedAll=true; return null; } @Override protected void onProgressUpdate(Fetcher.result... values) { FragmentHelper fh = helperRef.get(); if (fh!=null) for (Fetcher.result r : values){ //TODO: make Toast fh.showErrorMessage(r); } else { Log.w(TAG,"We had to show some progress but activity was destroyed"); } } @Override protected void onPostExecute(Object o) { FragmentHelper fh = helperRef.get(); if(failedAll || o == null || fh == null){ //everything went bad if(fh!=null) fh.toggleSpinner(false); cancel(true); return; } switch (t){ case ARRIVALS: Palina palina = (Palina) o; fh.createOrUpdateStopFragment(palina); break; case STOPS: //this should never be a problem List stopList = (List) o; if(query!=null) fh.createFragmentFor(stopList,query); else Log.e(TAG,"QUERY NULL, COULD NOT CREATE FRAGMENT"); break; case DBUPDATE: break; } } @Override protected void onCancelled() { FragmentHelper fh = helperRef.get(); if (fh!=null) fh.toggleSpinner(false); } @Override protected void onPreExecute() { FragmentHelper fh = helperRef.get(); if (fh!=null) fh.toggleSpinner(true); } public enum RequestType { ARRIVALS,STOPS,DBUPDATE } /** * Run this in a background thread.
* Sets a stop name for this.palina, guaranteed not to be null! **/ //TODO:Implement this /* private void getNameOrGetRekt(Palina p) { String nameMaybe; SQLiteDatabase udb = uDB.getReadableDatabase(); // does it already have a name (for fetchers that support it, or already got from favorites)? nameMaybe = p.getStopDisplayName(); if(nameMaybe != null && nameMaybe.length() > 0) { return; } // ok, let's search favorites. String usernameMaybe = UserDB.getStopUserName(udb, this.p.ID); if(usernameMaybe != null && usernameMaybe.length() > 0) { p.setStopUserName(usernameMaybe); return; } // let's try StopsDB, then. db.openIfNeeded(); nameMaybe = db.getNameFromID(this.p.ID); db.closeIfNeeded(); if(nameMaybe != null && nameMaybe.length() > 0) { p.setStopName(nameMaybe); return; } // no name to be found anywhere, don't bother searching it next time p.setStopName(""); }*/ } diff --git a/src/it/reyboz/bustorino/middleware/DatabaseUpdateService.java b/src/it/reyboz/bustorino/middleware/DatabaseUpdateService.java new file mode 100644 index 0000000..503abfd --- /dev/null +++ b/src/it/reyboz/bustorino/middleware/DatabaseUpdateService.java @@ -0,0 +1,121 @@ +/* + BusTO (middleware) + Copyright (C) 2018 Fabio Mazza + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ +package it.reyboz.bustorino.middleware; + +import android.app.IntentService; +import android.content.Intent; +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; +import it.reyboz.bustorino.R; +import it.reyboz.bustorino.backend.Fetcher; +import it.reyboz.bustorino.backend.FiveTAPIFetcher; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.concurrent.atomic.AtomicReference; + +/** + * An {@link IntentService} subclass for handling asynchronous task requests in + * a service on a separate handler thread. + */ +public class DatabaseUpdateService extends IntentService { + // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS + private static final String ACTION_UPDATE = "it.reyboz.bustorino.middleware.action.UPDATE_DB"; + private static final String DB_VERSION = "NextGenDB.GTTVersion"; + private static final String DEBUG_TAG = "DatabaseService_BusTO"; + // TODO: Rename parameters + private static final String TRIAL = "it.reyboz.bustorino.middleware.extra.TRIAL"; + private static final int MAX_TRIALS = 5; + public DatabaseUpdateService() { + super("DatabaseUpdateService"); + } + + /** + * Starts this service to perform action Foo with the given parameters. If + * the service is already performing a task this action will be queued. + * + * @see IntentService + */ + public static void startDBUpdate(Context context) { + startDBUpdate(context,0); + } + public static void startDBUpdate(Context con, int trial){ + Intent intent = new Intent(con, DatabaseUpdateService.class); + intent.setAction(ACTION_UPDATE); + intent.putExtra(TRIAL,trial); + con.startService(intent); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent != null) { + final String action = intent.getAction(); + if (ACTION_UPDATE.equals(action)) { + final int trial = intent.getIntExtra(TRIAL,-1); + if(!isUpdateNeeded(trial)) return; + + } + } + } + + public void performDBUpdate(){ + + } + private boolean isUpdateNeeded(int trial){ + SharedPreferences shPr = getSharedPreferences(getString(R.string.mainSharedPreferences),MODE_PRIVATE); + int versionDB = shPr.getInt(DB_VERSION,-1); + if(versionDB==-1) return true; + AtomicReference gres = new AtomicReference<>(); + String networkRequest = FiveTAPIFetcher.performAPIRequest(FiveTAPIFetcher.QueryType.STOPS_VERSION,null,gres); + if(networkRequest == null){ + if(gres.get()!= Fetcher.result.PARSER_ERROR){ + restartDBUpdateifPossible(trial); + } + return false; + } + + boolean needed; + try { + JSONObject resp = new JSONObject(networkRequest); + int ver = resp.getInt("id"); + if(ver>versionDB) { + SharedPreferences.Editor editor = shPr.edit(); + editor.putInt(DB_VERSION, ver); + //TODO: add the version date maybe? + editor.apply(); + needed = true; + } else { + needed = false; + } + + } catch (JSONException e) { + e.printStackTrace(); + restartDBUpdateifPossible(trial); + needed = false; + } + return needed; + } + private void restartDBUpdateifPossible(int currentTrial){ + if (currentTrial. + */ +package it.reyboz.bustorino.middleware; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.provider.BaseColumns; + +import static it.reyboz.bustorino.middleware.NextGenDB.Contract.BranchesTable.*; + +public class NextGenDB extends SQLiteOpenHelper{ + public static final String DATABASE_NAME = "bustodatabase.db"; + public static final int DATABASE_VERSION = 1; + //Some generating Strings + private static final String SQL_CREATE_LINES_TABLE="CREATE TABLE "+Contract.LinesTable.TABLE_NAME+" ("+ + Contract.LinesTable._ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Contract.LinesTable.COLUMN_NAME+" TEXT, "+ + Contract.LinesTable.COLUMN_FAKE_DESCRIPTION+" TEXT, "+Contract.LinesTable.COLUMN_BACINO+" TEXT )"; + private static final String SQL_CREATE_BRANCH_TABLE="CREATE TABLE "+Contract.BranchesTable.TABLE_NAME+" ("+ + Contract.BranchesTable._ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Contract.BranchesTable.COL_BRANCHID +" INTEGER, "+ + Contract.BranchesTable.COL_LINE +" INTEGER, "+ Contract.BranchesTable.COL_DESCRIPTION +" TEXT, "+ + Contract.BranchesTable.COL_DIRECTION+" TEXT, "+ + //SERVICE DAYS: 0 => FERIALE,1=>FESTIVO,-1=>UNKNOWN,add others if necessary + Contract.BranchesTable.COL_FESTIVO +" INTEGER, "+ + //DAYS COLUMNS. IT'S SO TEDIOUS I TRIED TO KILL MYSELF + COL_LUN+" INTEGER, "+COL_MAR+" INTEGER, "+COL_MER+" INTEGER, "+COL_GIO+" INTEGER, "+COL_VEN+" INTEGER, "+ + COL_SAB+" INTEGER, "+COL_DOM+" INTEGER, "+ + "FOREIGN KEY("+ Contract.BranchesTable.COL_LINE +") references "+ Contract.LinesTable.TABLE_NAME+"("+ Contract.LinesTable._ID+") " + +")"; + private static final String SQL_CREATE_CONNECTIONS_TABLE="CREATE TABLE "+Contract.ConnectionsTable.TABLE_NAME+" ("+ + Contract.ConnectionsTable.COLUMN_BRANCH+" INTEGER, "+ Contract.ConnectionsTable.COLUMN_STOP_ID+" TEXT, "+ + Contract.ConnectionsTable.COLUMN_ORDER+" INTEGER, "+ + "PRIMARY KEY ("+ Contract.ConnectionsTable.COLUMN_BRANCH+","+ Contract.ConnectionsTable.COLUMN_STOP_ID + ") ,"+ + "FOREIGN KEY("+ Contract.ConnectionsTable.COLUMN_BRANCH+") references "+ Contract.BranchesTable.TABLE_NAME+"("+ Contract.BranchesTable.COL_BRANCHID +") ,"+ + "FOREIGN KEY("+ Contract.ConnectionsTable.COLUMN_STOP_ID+") references "+ Contract.StopsTable.TABLE_NAME+"("+ Contract.StopsTable.COL_ID +") " + +")"; + private static final String SQL_CREATE_STOPS_TABLE="CREATE TABLE "+Contract.StopsTable.TABLE_NAME+" ("+ + Contract.StopsTable.COL_ID+" TEXT PRIMARY KEY, "+ Contract.StopsTable.COL_TYPE+" TEXT, "+Contract.StopsTable.COL_LAT+" REAL NOT NULL, "+ + Contract.StopsTable.COL_LONG+" REAL NOT NULL, "+ Contract.StopsTable.COL_NAME+" TEXT NOT NULL, "+ + Contract.StopsTable.COL_LOCATION+" TEXT, "+Contract.StopsTable.COL_PLACE+" TEXT, "+ + Contract.StopsTable.COL_LINES_STOPPING +" TEXT )"; + + + public NextGenDB(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(SQL_CREATE_LINES_TABLE); + db.execSQL(SQL_CREATE_STOPS_TABLE); + //tables with constraints + db.execSQL(SQL_CREATE_BRANCH_TABLE); + db.execSQL(SQL_CREATE_CONNECTIONS_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } + + @Override + public void onConfigure(SQLiteDatabase db) { + super.onConfigure(db); + db.execSQL("PRAGMA foreign_keys=ON"); + + } + + public static final class Contract{ + //Ok, I get it, it really is a pain in the ass.. + // But it's the only way to have maintainable code + + public static final class LinesTable implements BaseColumns{ + //The fields + public static final String TABLE_NAME = "lines"; + public static final String COLUMN_NAME = "name"; + public static final String COLUMN_FAKE_DESCRIPTION = "line_description"; + public static final String COLUMN_BACINO = "bacino"; + } + public static final class BranchesTable implements BaseColumns{ + public static final String TABLE_NAME = "branches"; + public static final String COL_BRANCHID = "branchid"; + public static final String COL_LINE = "lineid"; + public static final String COL_DESCRIPTION = "branch_description"; + public static final String COL_DIRECTION = "direzione"; + public static final String COL_FESTIVO = "festivo"; + public static final String COL_LUN="lun"; + public static final String COL_MAR="mar"; + public static final String COL_MER="mer"; + public static final String COL_GIO="gio"; + public static final String COL_VEN="ven"; + public static final String COL_SAB="sab"; + public static final String COL_DOM="dom"; + + } + public static final class ConnectionsTable { + public static final String TABLE_NAME = "connections"; + public static final String COLUMN_BRANCH = "branchid"; + public static final String COLUMN_STOP_ID = "stopid"; + static final String COLUMN_ORDER = "order"; + } + public static final class StopsTable { + public static final String TABLE_NAME = "stops"; + public static final String COL_ID = "id"; //integer + public static final String COL_TYPE = "type"; + public static final String COL_NAME = "name"; + public static final String COL_LAT = "lat"; + public static final String COL_LONG = "longitude"; + public static final String COL_LOCATION = "type"; + public static final String COL_PLACE = "placeName"; + public static final String COL_LINES_STOPPING = "lines"; + + } + } +}