diff --git a/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java b/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java
index 6a0005f..fd4456e 100644
--- a/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java
+++ b/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java
@@ -1,134 +1,138 @@
/*
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 android.support.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
public class GTTJSONFetcher implements ArrivalsFetcher {
// @Override
// public Palina ReadArrivalTimesRoute(String stopID, String routeID, AtomicReference res) {
// return this.ReadArrivalTimesAll(routeID, res); // TODO: implement this when it's needed (never?)
// }
@Override @NonNull
public Palina ReadArrivalTimesAll(String routeID, AtomicReference res) {
URL url;
Palina p = new Palina();
JSONArray json;
int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale...
JSONObject thisroute;
JSONArray passaggi;
try {
url = new URL("http://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransiti&palina=" + routeID + "&realtime=true");
} catch (MalformedURLException e) {
res.set(result.PARSER_ERROR);
return p;
}
json = queryURL(url, res);
if(json == null) {
return p;
}
howManyRoutes = json.length();
if(howManyRoutes == 0) {
res.set(result.EMPTY_RESULT_SET);
return p;
}
try {
for(i = 0; i < howManyRoutes; i++) {
thisroute = json.getJSONObject(i);
pos = p.addRoute(thisroute.getString("Linea"), thisroute.getString("Direzione"));
passaggi = thisroute.getJSONArray("PassaggiRT");
howManyPassaggi = passaggi.length();
- for(j = 0; j < howManyPassaggi; i++) {
+ for(j = 0; j < howManyPassaggi; j++) {
p.addPassaggio(passaggi.getString(j).concat("*"), pos);
}
passaggi = thisroute.getJSONArray("Passaggi"); // now the non-real-time ones
howManyPassaggi = passaggi.length();
- for(j = 0; j < howManyPassaggi; i++) {
+ for(j = 0; j < howManyPassaggi; j++) {
p.addPassaggio(passaggi.getString(j), pos);
}
}
} catch (JSONException e) {
res.set(result.PARSER_ERROR);
return p;
}
res.set(result.OK);
return p;
}
@Nullable
private JSONArray queryURL(URL url, AtomicReference res) {
HttpURLConnection urlConnection;
+ InputStream in = null;
JSONArray json;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch(IOException e) {
res.set(result.SERVER_OFFLINE); // TODO: can we assume this is CLIENT_OFFLINE?
return null;
}
try {
- InputStream in = urlConnection.getInputStream();
+ in = urlConnection.getInputStream();
json = new JSONArray(streamToString(in));
} catch (Exception e) {
res.set(result.PARSER_ERROR);
return null;
} finally {
+ try {
+ if (in != null) {in.close();} // this is getting ureadable quite fast.
+ } catch(Exception ignored) {}
urlConnection.disconnect();
}
try {
// returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops!
json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array.
} catch(JSONException e) {
res.set(result.EMPTY_RESULT_SET);
return null;
}
return json;
}
- // https://stackoverflow.com/a/14585883
+ // https://stackoverflow.com/a/5445161
private static String streamToString(InputStream is) {
- Scanner s = new Scanner(is); // TODO: test this
+ Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");;
return s.hasNext() ? s.next() : "";
}
}
diff --git a/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java b/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java
index 4d338bf..81843b8 100644
--- a/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java
+++ b/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java
@@ -1,62 +1,63 @@
/*
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.middleware;
import it.reyboz.bustorino.backend.ArrivalsFetcher;
import it.reyboz.bustorino.backend.Fetcher;
import it.reyboz.bustorino.backend.Palina;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import java.util.concurrent.atomic.AtomicReference;
public class AsyncArrivalsFetcherAll extends AsyncTask {
private final ArrivalsFetcher af;
private AtomicReference result;
private final String stopID;
private final AsyncArrivalsFetcherAllCallback callback;
public AsyncArrivalsFetcherAll(@NonNull ArrivalsFetcher af, @NonNull String stopID, @NonNull AsyncArrivalsFetcherAllCallback callback) {
this.af = af;
this.stopID = stopID;
this.callback = callback;
+ this.result = new AtomicReference<>();
}
@Override protected Palina doInBackground(Void... useless) {
return af.ReadArrivalTimesAll(this.stopID, this.result); // TODO: reuse the same Palina object and update it
}
@Override protected void onPostExecute(Palina p) {
if(!this.isCancelled()) { // is this really needed?
this.callback.call(p, result.get(), this.stopID);
}
}
/**
* Android doesn't really support JDK 8, so we have to use this hack\workaround.
*
* I've already tested 4 different solutions, this is the cleanest-looking so far...
*
* The only purpose of this interface is to glue together the AsyncArrivalsFetcherAll and whatver ActivityMain needs to do after obraining results, change parameters liberally if needed.
*/
public interface AsyncArrivalsFetcherAllCallback {
void call(Palina p, Fetcher.result res, String stopID);
}
}
\ No newline at end of file
diff --git a/src/it/reyboz/bustorino/middleware/PalinaAdapter.java b/src/it/reyboz/bustorino/middleware/PalinaAdapter.java
index 7f9720d..3b18a08 100644
--- a/src/it/reyboz/bustorino/middleware/PalinaAdapter.java
+++ b/src/it/reyboz/bustorino/middleware/PalinaAdapter.java
@@ -1,103 +1,105 @@
/*
BusTO - Arrival times for Turin public transports.
Copyright (C) 2014 Valerio Bozzolan
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.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import it.reyboz.bustorino.R;
import it.reyboz.bustorino.backend.Palina;
import it.reyboz.bustorino.backend.Passaggio;
import it.reyboz.bustorino.backend.Route;
/**
* This once was a ListView Adapter for BusLine[].
*
* Thanks to Framentos developers for the guide:
* http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/#
*
* @author Valerio Bozzolan
* @author Ludovico Pavesi
*/
public class PalinaAdapter extends ArrayAdapter {
private TextView entryView = null;
private TextView busLineIconTextView = null;
private TextView busLinePassagesTextView = null;
private TextView busLineVehicleIcon = null;
- private View someRandomConvertView = null;
+ private View theViewToUse = null;
private static int layout = R.layout.entry_bus_line_passage;
public PalinaAdapter(Context context, Palina p) {
// TODO: find a more efficient way if there's one
super(context, layout, p.queryAllRoutes());
}
/**
* Some parts taken from the AdapterBusLines class, some parts inspired by this tutorial:
* http://www.simplesoft.it/android/guida-agli-adapter-e-le-listview-in-android.html
* And some other bits and bobs TIRATI FUORI DAL NULLA CON L'INTUIZIONE INTELLETTUALE PERCHÉ
* SEMBRA CHE NESSUNO ABBIA LA MINIMA IDEA DI COME FUNZIONA UN ADAPTER SU ANDROID.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
- if(this.someRandomConvertView == null) {
+ if(this.theViewToUse == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layout, null);
- this.someRandomConvertView = convertView;
+ this.theViewToUse = convertView;
}
} else {
- this.someRandomConvertView = convertView;
+ this.theViewToUse = convertView;
}
+ // ------------------- /!\ Don't use convertView beyond this point /!\ ---------------------
+
if(this.entryView == null) {
// I'd be surprised if this doesn't catch fire as soon as it runs.
- this.entryView = (TextView) this.someRandomConvertView.findViewById(R.id.busLineNames);
- this.busLineIconTextView = (TextView) this.someRandomConvertView.findViewById(R.id.busLineIcon);
- this.busLinePassagesTextView = (TextView) this.someRandomConvertView.findViewById(R.id.busLineNames);;
- this.busLineVehicleIcon = (TextView) this.someRandomConvertView.findViewById(R.id.vehicleIcon); // Vehicle icon
+ this.entryView = (TextView) this.theViewToUse.findViewById(R.id.busLineNames);
+ this.busLineIconTextView = (TextView) this.theViewToUse.findViewById(R.id.busLineIcon);
+ this.busLinePassagesTextView = (TextView) this.theViewToUse.findViewById(R.id.busLineNames);;
+ this.busLineVehicleIcon = (TextView) this.theViewToUse.findViewById(R.id.vehicleIcon); // Vehicle icon
}
Route route = getItem(position);
// Take the TextView from layout and set the busLine name
// TODO: pezza temporanea da sistemare
busLineIconTextView.setText(route.name + " > " + route.destinazione);
List passaggi = route.passaggi;
if(passaggi.size() == 0) {
this.busLinePassagesTextView.setText(R.string.no_passages);
this.busLineVehicleIcon.setVisibility(View.INVISIBLE);
} else {
String resultString = "";
for(Passaggio passaggio : passaggi) {
// "+" calls concat() and some other stuff internally, this should be faster
resultString = resultString.concat(passaggio.toString()).concat(" ");
}
this.busLinePassagesTextView.setText(resultString);
}
- return convertView;
+ return this.theViewToUse;
}
}