diff --git a/res/layout/activity_main.xml b/res/layout/activity_main.xml
deleted file mode 100644
index e8f1666..0000000
--- a/res/layout/activity_main.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
diff --git a/res/layout/bus_stop_entry.xml b/res/layout/bus_stop_entry.xml
new file mode 100644
index 0000000..193f7a3
--- /dev/null
+++ b/res/layout/bus_stop_entry.xml
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/res/layout/fragment_main.xml b/res/layout/fragment_main.xml
deleted file mode 100644
index 6218a87..0000000
--- a/res/layout/fragment_main.xml
+++ /dev/null
@@ -1,49 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/res/layout/main_activity.xml b/res/layout/main_activity.xml
index 3bd86ab..72d8f65 100644
--- a/res/layout/main_activity.xml
+++ b/res/layout/main_activity.xml
@@ -1,57 +1,69 @@
+ android:layout_below="@+id/searchTitleTextView"
+ android:onClick="searchClick"
+ android:text="@string/search" />
-
+ android:layout_below="@+id/busStationName" >
+
+
+ android:indeterminate="true" />
+
+
diff --git a/res/values-it/strings.xml b/res/values-it/strings.xml
index 8e2d945..f463fce 100644
--- a/res/values-it/strings.xml
+++ b/res/values-it/strings.xml
@@ -1,13 +1,13 @@
BusTO
Ciao mondo!
Impostazioni
Cerca
Inserisci il numero della fermata.
Errore di rete. Verifica l\'accesso ad Internet
Nessun arrivo previsto per questa fermata :(
- Passaggi linea %1$s:
+ Passaggi fermata %1$s:
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ff3967a..58aeff8 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1,13 +1,13 @@
BusTO
Hello world!
Settings
Search
Please insert the number of the bus stop.
Network Error. Is network available?
No arrival times for this station :(
- Passages line %1$s:
+ Passages bus stop %1$s:
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 845fb57..cda87fc 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -1,20 +1,20 @@
-
+
\ No newline at end of file
diff --git a/src/it/reyboz/bustorino/GTTSiteSucker.java b/src/it/reyboz/bustorino/GTTSiteSucker.java
index afad109..5c3c8c8 100644
--- a/src/it/reyboz/bustorino/GTTSiteSucker.java
+++ b/src/it/reyboz/bustorino/GTTSiteSucker.java
@@ -1,127 +1,184 @@
package it.reyboz.bustorino;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import android.util.Log;
public class GTTSiteSucker {
+ public static String arrivalTimesByLineQuery(String busStop) {
+ return "http://www.5t.torino.it/5t/trasporto/arrival-times-byline.jsp?action=getTransitsByLine&shortName=" + busStop;
+ }
+
+ /**
+ * A time passage is a time with the answer of: Is this in real time?
+ * @author boz
+ */
public static class TimePassage {
private String time;
private boolean isInRealTime;
public TimePassage(String time, boolean isInRealTime) {
this.time = time;
this.isInRealTime = isInRealTime;
}
public String getTime() {
return time;
}
public boolean isInRealTime() {
return isInRealTime;
}
}
+ /**
+ * An ArrivalsAtBusStop has informations of the buses that pass through it (like line numbers)
+ * @author boz
+ */
public static class ArrivalsAtBusStop {
private int codLineaGTT;
private String lineaGTT;
private ArrayList timesPassages;
public ArrivalsAtBusStop() {
timesPassages = new ArrayList();
}
public void setCodLineaGTT(int codLineaGTT) {
this.codLineaGTT = codLineaGTT;
}
public void setLineaGTT(String lineaGTT) {
this.lineaGTT = lineaGTT;
}
public void addTimePassage(String time, boolean isInRealTime) {
timesPassages.add(new TimePassage(time, isInRealTime));
}
public int getCodLineaGTT() {
return codLineaGTT;
}
public String getLineaGTT() {
return lineaGTT;
}
public ArrayList getTimePassages() {
return timesPassages;
}
public String toString() {
return codLineaGTT + " " + lineaGTT;
}
+
+ public String getTimePassagesString() {
+ String out = "";
+ for(TimePassage timePassage:timesPassages) {
+ out += timePassage.getTime() + (timePassage.isInRealTime() ? "*" : "") + " ";
+ }
+ if(timesPassages.size() == 0) {
+ out = ":(";
+ }
+ return out;
+ }
}
- public static String arrivalTimesByLineQuery(String busStop) {
- return "http://www.5t.torino.it/5t/trasporto/arrival-times-byline.jsp?action=getTransitsByLine&shortName=" + busStop;
+ /**
+ * A BusStop has information on the bus stop (bus stop name) and of all the buses that pass through it
+ * @author boz
+ */
+ public static class BusStop {
+ private String stationName;
+ private ArrivalsAtBusStop[] arrivalsAtBusStop;
+
+ BusStop(String stationName, ArrivalsAtBusStop[] arrivalsAtBusStop) {
+ this.stationName = stationName;
+ this.arrivalsAtBusStop = arrivalsAtBusStop;
+ }
+
+ public void setNomeUmano(String stationName) {
+ this.stationName = stationName;
+ }
+
+ public String getStationName() {
+ return stationName;
+ }
+
+ public ArrivalsAtBusStop[] getArrivalsAtBusStop() {
+ return arrivalsAtBusStop;
+ }
}
/**
- * Workaround to get all informations from the 5T website
+ * API/Workaround to get all informations from the 5T website
*
* @param html
* @author Valerio Bozzolan
+ * @return BusStop
*/
- public static ArrivalsAtBusStop[] arrivalTimesBylineHTMLSucker(String html) {
+ public static BusStop arrivalTimesBylineHTMLSucker(String html) {
ArrayList arrivalsAtBusStop = new ArrayList();
Document doc = Jsoup.parse(html);
for (Element tr : doc.getElementsByTag("tr")) {
ArrivalsAtBusStop arrivalAtBusStop = new ArrivalsAtBusStop();
boolean codLineaGTTfound = false;
+
for (Element td : tr.children()) {
String tdContent = td.html();
if (tdContent.isEmpty()) {
continue;
}
if (!codLineaGTTfound) {
Element tdURL = td.select("a").first();
String lineaGTT = tdURL.html();
String codLineaGTT = "";
Matcher matcher = Pattern.compile("([0-9])+").matcher(
tdURL.attr("href"));
if (matcher.find()) {
codLineaGTT = matcher.group();
}
arrivalAtBusStop.setLineaGTT(lineaGTT);
arrivalAtBusStop.setCodLineaGTT(Integer
.parseInt(codLineaGTT));
codLineaGTTfound = true;
continue;
}
- // Look for "*"
+ // Look for "*" (aka "prodotto surgelato")
boolean frozenProduct = !td.select("i").isEmpty();
if(frozenProduct) {
td.select("i").remove();
tdContent = td.html();
}
-
- Log.d("miao", "Prodotto surgelato: " + ((frozenProduct) ? "Yeppa" : "Nopely nope"));
arrivalAtBusStop.addTimePassage(tdContent, frozenProduct);
- Log.d("miao td", "td: " + tdContent);
}
arrivalsAtBusStop.add(arrivalAtBusStop);
}
- return (ArrivalsAtBusStop[]) arrivalsAtBusStop
- .toArray(new ArrivalsAtBusStop[] {});
+
+ // Sucking bus station name (e.g.: POZZO STRADA)
+ String nomeUmano = "";
+ Element tagNomeUmano = doc.select("span").first();
+ if(tagNomeUmano != null) {
+ nomeUmano = tagNomeUmano.html();
+ }
+ Matcher matcher = Pattern.compile("nbsp;(.*)").matcher(
+ nomeUmano);
+ if (matcher.find()) {
+ nomeUmano = matcher.group(1);
+ Log.d("miao", "nome umano:" + nomeUmano);
+ }
+
+ return new BusStop(nomeUmano, (ArrivalsAtBusStop[]) arrivalsAtBusStop.toArray(new ArrivalsAtBusStop[] {}));
}
}
diff --git a/src/it/reyboz/bustorino/MainActivity.java b/src/it/reyboz/bustorino/MainActivity.java
index 6235b93..b92e530 100644
--- a/src/it/reyboz/bustorino/MainActivity.java
+++ b/src/it/reyboz/bustorino/MainActivity.java
@@ -1,90 +1,107 @@
package it.reyboz.bustorino;
+import java.util.ArrayList;
+import java.util.HashMap;
+
import it.reyboz.bustorino.GTTSiteSucker.ArrivalsAtBusStop;
-import it.reyboz.bustorino.GTTSiteSucker.TimePassage;
+import it.reyboz.bustorino.GTTSiteSucker.BusStop;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
-import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
+import android.widget.ListView;
import android.widget.ProgressBar;
+import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
+ private TextView busStationName;
private EditText bus_stop_number_editText;
- private TextView result_textView;
private ProgressBar annoyingFedbackProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
- bus_stop_number_editText = (EditText)findViewById(R.id.busStopNumberEditText);
- result_textView = (TextView)findViewById(R.id.result_textView);
- annoyingFedbackProgressBar = (ProgressBar)findViewById(R.id.annoyingFedbackProgress);
+ busStationName = (TextView) findViewById(R.id.busStationName);
+ bus_stop_number_editText = (EditText) findViewById(R.id.busStopNumberEditText);
+ annoyingFedbackProgressBar = (ProgressBar) findViewById(R.id.annoyingFedbackProgress);
annoyingFedbackProgressBar.setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
-
+
/**
* Catch the Async action!
+ *
* @author boz
*/
public class MyAsyncWget extends AsyncWget {
protected void onPostExecute(String result) {
- ArrivalsAtBusStop[] arrivalsAtBusStop = GTTSiteSucker.arrivalTimesBylineHTMLSucker(result);
- String out = "";
- for(ArrivalsAtBusStop arrivalAtBusStop:arrivalsAtBusStop) {
- out += String.format(getResources().getString(R.string.passages), arrivalAtBusStop.getLineaGTT()) + "\n";
- for(TimePassage timePassage:arrivalAtBusStop.getTimePassages()) {
- out += "-\t" + timePassage.getTime() + (timePassage.isInRealTime() ? "*" : "") + "\n";
- }
-
- if(arrivalAtBusStop.getTimePassages().size() == 0) {
- out += "\t :( \n";
- }
-
- out += "\n";
+ ArrayList> data = new ArrayList>();
+
+ BusStop busStop = GTTSiteSucker.arrivalTimesBylineHTMLSucker(result);
+
+ ArrivalsAtBusStop[] arrivalsAtBusStop = busStop.getArrivalsAtBusStop();
+
+ for (ArrivalsAtBusStop arrivalAtBusStop : arrivalsAtBusStop) {
+ HashMap personMap = new HashMap();
+ personMap.put("icon", arrivalAtBusStop.getLineaGTT());
+ personMap.put("line-name", arrivalAtBusStop.getTimePassagesString());
+ data.add(personMap);
}
- if(arrivalsAtBusStop.length == 0) {
- Toast.makeText(getApplicationContext(), R.string.no_arrival_times, Toast.LENGTH_SHORT).show();
+ if (arrivalsAtBusStop.length == 0) {
+ Toast.makeText(getApplicationContext(),
+ R.string.no_arrival_times, Toast.LENGTH_SHORT).show();
+ } else {
+ busStationName.setText(String.format(getResources().getString(R.string.passages), busStop.getStationName()));
}
+ String[] from = { "icon", "line-name" };
+ int[] to = { R.id.busLineIcon, R.id.busLine };
+ SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
+ data,
+ R.layout.bus_stop_entry,
+ from, to);
+
+ ListView tpm = ((ListView) findViewById(R.id.listView1));
+ tpm.setAdapter(adapter);
+
annoyingFedbackProgressBar.setVisibility(View.INVISIBLE);
- result_textView.setText(out);
}
}
public void searchClick(View v) {
if (!NetworkTools.isConnected(this)) {
NetworkTools.showNetworkError(this);
} else {
annoyingFedbackProgressBar.setVisibility(View.VISIBLE);
- new MyAsyncWget().execute(GTTSiteSucker.arrivalTimesByLineQuery(bus_stop_number_editText.getText().toString()));
+ new MyAsyncWget().execute(GTTSiteSucker
+ .arrivalTimesByLineQuery(bus_stop_number_editText.getText()
+ .toString()));
}
}
}