diff --git a/res/values-it/strings.xml b/res/values-it/strings.xml --- a/res/values-it/strings.xml +++ b/res/values-it/strings.xml @@ -133,6 +133,7 @@ Sito GTT Sito 5T Torino App Muoversi a Torino + Sconosciuta Cambiamento sorgente orari… Premi a lungo per cambiare la sorgente degli orari diff --git a/res/values/strings.xml b/res/values/strings.xml --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -147,6 +147,7 @@ GTT Website 5T Torino website Muoversi a Torino app + Undetermined Changing arrival times source… Long press to change the source of arrivals diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -22,7 +22,7 @@ --> 0) min = Math.min(min,r.numPassaggi()); } - return min; + if (min == Integer.MAX_VALUE) return 0; + else return min; } //private void mergeRoute } \ No newline at end of file diff --git a/src/it/reyboz/bustorino/backend/Route.java b/src/it/reyboz/bustorino/backend/Route.java --- a/src/it/reyboz/bustorino/backend/Route.java +++ b/src/it/reyboz/bustorino/backend/Route.java @@ -51,7 +51,7 @@ public enum Type { // "long distance" sono gli extraurbani. BUS(1), LONG_DISTANCE_BUS(2), METRO(3), RAILWAY(4), TRAM(5), UNKNOWN(-2); //TODO: decide to give some special parameter to each field - private int code; + private final int code; Type(int code){ this.code = code; } @@ -81,7 +81,7 @@ public enum FestiveInfo{ FESTIVO(1),FERIALE(0),UNKNOWN(-2); - private int code; + private final int code; FestiveInfo(int code){ this.code = code; } @@ -312,7 +312,9 @@ } public void setGtfsId(@Nullable String gtfsId) { - this.gtfsId = gtfsId; + if (gtfsId==null) this.gtfsId = null; + else + this.gtfsId = gtfsId.trim(); } public boolean isBranchIdValid(){ diff --git a/src/it/reyboz/bustorino/backend/mato/MapiArrivalRequest.java b/src/it/reyboz/bustorino/backend/mato/MapiArrivalRequest.java --- a/src/it/reyboz/bustorino/backend/mato/MapiArrivalRequest.java +++ b/src/it/reyboz/bustorino/backend/mato/MapiArrivalRequest.java @@ -47,6 +47,8 @@ private final int timeRange, numberOfDepartures; private final AtomicReference reqRes; + private final String DEBUG_TAG = "BusTO-MAPIArrivalReq"; + public MapiArrivalRequest(String stopName, Date startingTime, int timeRange, int numberOfDepartures, AtomicReference res, @@ -87,7 +89,7 @@ throw new AuthFailureError("Error with JSON enconding",e); } String requestBody = data.toString(); - Log.d("MapiArrivalBusTO", "Request variables: "+ variables); + Log.d(DEBUG_TAG, "Request variables: "+ variables); return requestBody.getBytes(); } @@ -121,18 +123,23 @@ } if (!stopFound){ - Log.w("MapiArrival-Busto", "No stop found: "+p); + Log.w(DEBUG_TAG, "No stop found: "+p); reqRes.set(Fetcher.Result.NOT_FOUND); return Response.error(new VolleyError("Stop not found")); } } catch (JSONException e) { e.printStackTrace(); - Log.e("BusTO:MapiRequest", "Error parsing JSON: "+stringResponse); + Log.e(DEBUG_TAG, "Error parsing JSON: "+stringResponse); reqRes.set(Fetcher.Result.PARSER_ERROR); return Response.error(new VolleyError("Error parsing the response in JSON", e)); } reqRes.set(Fetcher.Result.OK); + /* + for (Route r: p.queryAllRoutes()){ + Log.d(DEBUG_TAG, "route "+r.getGtfsId()+" has "+r.passaggi.size()+" passaggi"); + } + */ return Response.success(p, HttpHeaderParser.parseCacheHeaders(response)); } diff --git a/src/it/reyboz/bustorino/backend/mato/MatoAPIFetcher.kt b/src/it/reyboz/bustorino/backend/mato/MatoAPIFetcher.kt --- a/src/it/reyboz/bustorino/backend/mato/MatoAPIFetcher.kt +++ b/src/it/reyboz/bustorino/backend/mato/MatoAPIFetcher.kt @@ -20,16 +20,14 @@ import android.content.Context import android.util.Log import com.android.volley.toolbox.RequestFuture +import it.reyboz.bustorino.BuildConfig import it.reyboz.bustorino.backend.* import org.json.JSONObject import java.util.* +import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.TimeoutException - -import java.util.concurrent.ExecutionException - - +import java.util.concurrent.atomic.AtomicReference open class MatoAPIFetcher(val minNumPassaggi: Int) : ArrivalsFetcher { @@ -37,22 +35,24 @@ set(value) { field = value!!.applicationContext } - constructor(): this(3) + constructor(): this(2) override fun ReadArrivalTimesAll(stopID: String?, res: AtomicReference?): Palina { stopID!! - val future = RequestFuture.newFuture() + val now = Calendar.getInstance().time - var numMinutes = 30 + var numMinutes = 0 var palina = Palina(stopID) var numPassaggi = 0 var trials = 0 + val numDepartures = 4 while (numPassaggi < minNumPassaggi && trials < 4) { - - numMinutes += 15 - val request = MapiArrivalRequest(stopID, now, numMinutes * 60, 10, res, future, future) + //numDepartures+=2 + numMinutes += 20 + val future = RequestFuture.newFuture() + val request = MapiArrivalRequest(stopID, now, numMinutes * 60, numDepartures, res, future, future) if (appContext == null || res == null) { Log.e("BusTO:MatoAPIFetcher", "ERROR: Given null context or null result ref") return Palina(stopID) @@ -64,8 +64,14 @@ try { val palinaResult = future.get(5, TimeUnit.SECONDS) if (palinaResult!=null) { + if (BuildConfig.DEBUG) + for (r in palinaResult.queryAllRoutes()){ + Log.d(DEBUG_TAG, "route " + r.gtfsId + " has " + r.passaggi.size + " passaggi: "+ r.passaggiToString) + } palina = palinaResult - numPassaggi = palina.totalNumberOfPassages + numPassaggi = palina.minNumberOfPassages + } else{ + Log.d(DEBUG_TAG, "Result palina is null") } } catch (e: InterruptedException) { e.printStackTrace() @@ -176,7 +182,7 @@ for (i in 0 until routesStoppingJSON.length()){ val routeBaseInfo = routesStoppingJSON.getJSONObject(i) val r = Route(routeBaseInfo.getString("shortName"), Route.Type.UNKNOWN,"") - r.gtfsId = routeBaseInfo.getString("gtfsId").trim() + r.setGtfsId(routeBaseInfo.getString("gtfsId").trim()) baseRoutes.add(r) } @@ -192,11 +198,12 @@ val patternJSON = routesStopTimes.getJSONObject(i) val mRoute = parseRouteStoptimesJSON(patternJSON) + //Log.d("BusTO-MapiFetcher") //val directionId = patternJSON.getJSONObject("pattern").getInt("directionId") //TODO: use directionId palina.addRoute(mRoute) for (r in baseRoutes) { - if (palina.gtfsID != null && r.gtfsId.equals(palina.gtfsID)) { + if (mRoute.gtfsId != null && r.gtfsId.equals(mRoute.gtfsId)) { baseRoutes.remove(r) break } @@ -239,7 +246,7 @@ routeType, passages, ) - route.gtfsId = gtfsId + route.setGtfsId(gtfsId) return route } diff --git a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java --- a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java +++ b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java @@ -141,10 +141,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_arrivals, container, false); - messageTextView = (TextView) root.findViewById(R.id.messageTextView); - addToFavorites = (ImageButton) root.findViewById(R.id.addToFavorites); - resultsListView = (ListView) root.findViewById(R.id.resultsListView); - timesSourceTextView = (TextView) root.findViewById(R.id.timesSourceTextView); + messageTextView = root.findViewById(R.id.messageTextView); + addToFavorites = root.findViewById(R.id.addToFavorites); + resultsListView = root.findViewById(R.id.resultsListView); + timesSourceTextView = root.findViewById(R.id.timesSourceTextView); timesSourceTextView.setOnLongClickListener(view -> { if(!fetchersChangeRequestPending){ rotateFetchers(); @@ -265,11 +265,7 @@ * @return the list of the fetchers */ public ArrayList getCurrentFetchers(){ - ArrayList v = new ArrayList(); - for (ArrivalsFetcher fetcher: fetchers){ - v.add(fetcher); - } - return v; + return new ArrayList<>(this.fetchers); } public ArrivalsFetcher[] getCurrentFetchersAsArray(){ ArrivalsFetcher[] arr = new ArrivalsFetcher[fetchers.size()]; @@ -329,7 +325,7 @@ break; case UNDETERMINED: //Don't show the view - source_txt = ""; + source_txt = getString(R.string.undetermined_source); break; default: throw new IllegalStateException("Unexpected value: " + source); @@ -344,8 +340,12 @@ if (count>10) Log.w(DEBUG_TAG, "Tried to update the source fetcher but it didn't work"); final String base_message = getString(R.string.times_source_fmt, source_txt); - timesSourceTextView.setVisibility(View.VISIBLE); timesSourceTextView.setText(base_message); + if (p.getTotalNumberOfPassages() > 0) { + timesSourceTextView.setVisibility(View.VISIBLE); + } else { + timesSourceTextView.setVisibility(View.INVISIBLE); + } fetchersChangeRequestPending = false; } diff --git a/src/it/reyboz/bustorino/middleware/AsyncArrivalsSearcher.java b/src/it/reyboz/bustorino/middleware/AsyncArrivalsSearcher.java --- a/src/it/reyboz/bustorino/middleware/AsyncArrivalsSearcher.java +++ b/src/it/reyboz/bustorino/middleware/AsyncArrivalsSearcher.java @@ -26,12 +26,9 @@ import android.os.AsyncTask; import androidx.annotation.NonNull; -import androidx.core.content.ContextCompat; import android.util.Log; -import android.widget.Toast; -import it.reyboz.bustorino.R; import it.reyboz.bustorino.backend.*; import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; import it.reyboz.bustorino.data.AppDataProvider; @@ -41,7 +38,6 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.Calendar; @@ -55,7 +51,7 @@ private static final String DEBUG_TAG = TAG; private boolean failedAll = false; - private final AtomicReference res; + private final AtomicReference finalResultRef; private String query; WeakReference helperRef; private final ArrayList otherActivities = new ArrayList<>(); @@ -68,7 +64,7 @@ public AsyncArrivalsSearcher(FragmentHelper fh, @NonNull ArrivalsFetcher[] fetchers, Context context) { helperRef = new WeakReference<>(fh); fh.setLastTaskRef(this); - res = new AtomicReference<>(); + finalResultRef = new AtomicReference<>(); this.context = context.getApplicationContext(); this.replaceFragment = true; @@ -82,7 +78,7 @@ @Override protected Palina doInBackground(String... params) { RecursionHelper r = new RecursionHelper<>(theFetchers); - Palina result = null; + Palina resultPalina = null; FragmentHelper fh = helperRef.get(); ArrayList results = new ArrayList<>(theFetchers.length); //If the FragmentHelper is null, that means the activity doesn't exist anymore @@ -160,8 +156,8 @@ } publishProgress(resRef.get()); //TODO: find a way to avoid overloading the user with toasts - if (result == null){ - result = p; + if (resultPalina == null && f instanceof MatoAPIFetcher && p.queryAllRoutes().size() > 0){ + resultPalina = p; } //find if it went well results.add(resRef.get()); @@ -177,7 +173,9 @@ return p; } + finalResultRef.set(resRef.get()); } + /* boolean emptyResults = true; for (Fetcher.Result re: results){ if (!re.equals(Fetcher.Result.EMPTY_RESULT_SET)) { @@ -185,10 +183,13 @@ break; } } + + */ //at this point, we are sure that the result has been negative failedAll=true; - return result; + + return resultPalina; } @Override @@ -208,7 +209,7 @@ protected void onPostExecute(Palina p) { FragmentHelper fh = helperRef.get(); - if(failedAll || p == null || fh == null){ + if(p == null || fh == null){ //everything went bad if(fh!=null) fh.toggleSpinner(false); cancel(true);