-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed traffic infos with localised date parser
- Loading branch information
Showing
6 changed files
with
711 additions
and
563 deletions.
There are no files selected for viewing
330 changes: 168 additions & 162 deletions
330
app/src/main/java/com/therolf/optymoNext/controller/activities/Main/TrafficController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,168 @@ | ||
package com.therolf.optymoNext.controller.activities.Main; | ||
|
||
import android.os.AsyncTask; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.ExpandableListView; | ||
import android.widget.ProgressBar; | ||
import android.widget.Toast; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import com.therolf.optymoNext.R; | ||
import com.therolf.optymoNext.controller.global.Utility; | ||
import com.therolf.optymoNext.vue.adapters.TrafficAdapter; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
|
||
import java.util.ArrayList; | ||
|
||
@SuppressWarnings("unused") | ||
public class TrafficController implements ExpandableListView.OnGroupExpandListener, ExpandableListView.OnGroupCollapseListener { | ||
|
||
private AppCompatActivity context; | ||
|
||
private ArrayList<TrafficAdapter.TrafficInfo> data = new ArrayList<>(); | ||
private TrafficAdapter trafficAdapter; | ||
private ExpandableListView listView; | ||
|
||
private ProgressBar progressBar; | ||
|
||
private TrafficInfoRequest request; | ||
|
||
TrafficController(AppCompatActivity context) { | ||
this.context = context; | ||
|
||
trafficAdapter = new TrafficAdapter(context, R.string.main_traffic_info, data); | ||
listView = context.findViewById(R.id.main_traffic_info_listview); | ||
listView.setAdapter(trafficAdapter); | ||
listView.setOnGroupExpandListener(this); | ||
listView.setOnGroupCollapseListener(this); | ||
|
||
progressBar = context.findViewById(R.id.main_traffic_info_progressbar); | ||
progressBar.setVisibility(View.GONE); | ||
} | ||
|
||
@Override | ||
public void onGroupExpand(int groupPosition) { | ||
if(request != null && !request.isCancelled()) { | ||
request.cancel(true); | ||
} | ||
|
||
request = new TrafficInfoRequest(this); | ||
request.execute(); | ||
} | ||
|
||
@Override | ||
public void onGroupCollapse(int groupPosition) { | ||
// cancel request | ||
if(request != null && !request.isCancelled()) { | ||
request.cancel(true); | ||
} | ||
|
||
// notify update | ||
context.runOnUiThread(() -> { | ||
data.clear(); | ||
trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(listView); | ||
}); | ||
} | ||
|
||
public static class TrafficInfoRequest extends AsyncTask<Void, Void, String> { | ||
|
||
private TrafficController tc; | ||
|
||
TrafficInfoRequest(TrafficController tc) { | ||
this.tc = tc; | ||
} | ||
|
||
@Override | ||
protected String doInBackground(Void... voids) { | ||
String result = null; | ||
|
||
// clear data | ||
tc.data.clear(); | ||
|
||
// notify update | ||
tc.context.runOnUiThread(() -> { | ||
tc.progressBar.setVisibility(View.VISIBLE); // visible visibility | ||
tc.trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(tc.listView); | ||
}); | ||
|
||
// read traffic RSS feed URL | ||
try { | ||
result = Utility.readUrl("https://www.optymo.fr/infos_trafic/feed"); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(String page) { | ||
super.onPostExecute(page); | ||
|
||
// gone visibility | ||
tc.context.runOnUiThread(() -> tc.progressBar.setVisibility(View.GONE)); | ||
|
||
if(page == null) { | ||
Toast.makeText(tc.context, R.string.error_occured, Toast.LENGTH_SHORT).show(); | ||
return; | ||
} | ||
|
||
Document document = Jsoup.parse(page), dateDocument; | ||
Elements items = document.getElementsByTag("item"), dates; | ||
String body, date, lines, content, url, itemHtml; | ||
for(Element item : items) { | ||
// Log.d("optmyonext", item.html()); | ||
dateDocument = Jsoup.parse(item.getElementsByTag("description").get(0).text()); | ||
body = dateDocument.body().text(); | ||
|
||
int lineIndex = body.indexOf("Lignes"); | ||
|
||
if(lineIndex == -1) { | ||
date = body; | ||
lines = ""; | ||
} else { | ||
date = body.substring(0, lineIndex); | ||
lines = body.substring(lineIndex); | ||
} | ||
content = item.getElementsByTag("title").get(0).text(); | ||
|
||
itemHtml = item.html(); | ||
url = itemHtml.substring(itemHtml.indexOf("<link>")+6, itemHtml.indexOf("<description>")); | ||
url = url.replace("</link>", ""); | ||
Log.d("optmyonext", "url:" + url); | ||
|
||
// add string | ||
tc.data.add(new TrafficAdapter.TrafficInfo(date, lines, content, url)); | ||
} | ||
|
||
// notify update | ||
tc.context.runOnUiThread(() -> { | ||
tc.trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(tc.listView); | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onCancelled() { | ||
super.onCancelled(); | ||
|
||
// gone visibility | ||
tc.context.runOnUiThread(() -> tc.progressBar.setVisibility(View.GONE)); | ||
} | ||
} | ||
} | ||
package com.therolf.optymoNext.controller.activities.Main; | ||
|
||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.widget.ExpandableListView; | ||
import android.widget.ProgressBar; | ||
import android.widget.Toast; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import com.android.volley.Request; | ||
import com.android.volley.RequestQueue; | ||
import com.android.volley.toolbox.StringRequest; | ||
import com.android.volley.toolbox.Volley; | ||
import com.therolf.optymoNext.R; | ||
import com.therolf.optymoNext.controller.global.Utility; | ||
import com.therolf.optymoNext.vue.adapters.TrafficAdapter; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
|
||
@SuppressWarnings("unused") | ||
public class TrafficController implements ExpandableListView.OnGroupExpandListener, ExpandableListView.OnGroupCollapseListener { | ||
|
||
private AppCompatActivity context; | ||
|
||
private ArrayList<TrafficAdapter.TrafficInfo> data = new ArrayList<>(); | ||
private TrafficAdapter trafficAdapter; | ||
private ExpandableListView listView; | ||
|
||
private static final String TRAFFIC_REQUEST_TAG = "request_traffic"; | ||
private StringRequest trafficRequest; | ||
private RequestQueue trafficQueue; | ||
|
||
private static final Comparator<String> LINE_COMPARATOR = (o1, o2) -> { | ||
try { | ||
int i1 = Integer.parseInt(o1); | ||
int i2 = Integer.parseInt(o2); | ||
|
||
return Integer.compare(i1, i2); | ||
} catch (NumberFormatException e) { | ||
e.printStackTrace(); | ||
return 0; | ||
} | ||
}; | ||
|
||
private static final Comparator<TrafficAdapter.TrafficInfo> ALERT_COMPARATOR = (o1, o2) -> -o1.getPublicationDate().compareTo(o2.getPublicationDate()); | ||
|
||
private static final String TRAFFIC_URL = "https://app.mecatran.com/utw/ws/alerts/active/belfort?preferredLang=fr&includeRoutes=true&includeStops=true&apiKey=76350b70682e051e6709782b551f5437173f1957"; | ||
private static final String TRAFFIC_PAGE_ROOT = "https://www.optymo.fr/infos_trafic/#"; | ||
|
||
// routes color and text color https://app.mecatran.com/utw/ws/gtfs/routes/belfort?includeAgencies=true&apiKey=76350b70682e051e6709782b551f5437173f1957 | ||
|
||
private ProgressBar progressBar; | ||
|
||
TrafficController(AppCompatActivity context) { | ||
this.context = context; | ||
|
||
trafficAdapter = new TrafficAdapter(context, R.string.main_traffic_info, data); | ||
listView = context.findViewById(R.id.main_traffic_info_listview); | ||
listView.setAdapter(trafficAdapter); | ||
listView.setOnGroupExpandListener(this); | ||
listView.setOnGroupCollapseListener(this); | ||
|
||
progressBar = context.findViewById(R.id.main_traffic_info_progressbar); | ||
progressBar.setVisibility(View.GONE); | ||
|
||
this.trafficQueue = Volley.newRequestQueue(context); | ||
this.trafficRequest = null; | ||
} | ||
|
||
@Override | ||
public void onGroupExpand(int groupPosition) { | ||
trafficQueue.add(this.request()); | ||
} | ||
|
||
private void hideProgressbar() { | ||
this.context.runOnUiThread(() -> this.progressBar.setVisibility(View.GONE)); | ||
} | ||
|
||
private void requestCancel() { | ||
if(this.trafficRequest != null && !this.trafficRequest.isCanceled()) { | ||
this.trafficRequest.cancel(); | ||
} | ||
} | ||
|
||
private StringRequest request() { | ||
this.requestCancel(); | ||
|
||
this.data.clear(); | ||
this.context.runOnUiThread(() -> { | ||
this.progressBar.setVisibility(View.VISIBLE); // visible visibility | ||
this.trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(this.listView); | ||
}); | ||
|
||
this.trafficRequest = new StringRequest(Request.Method.GET, TRAFFIC_URL, (response) -> { | ||
response = new String(response.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); | ||
|
||
this.hideProgressbar(); | ||
|
||
try { | ||
JSONArray result = new JSONArray(response); | ||
|
||
String title, formattedActiveRange, lines, url, publicationDate, from, to; | ||
JSONArray routes; | ||
ArrayList<String> linesList = new ArrayList<>(); | ||
for(int alertIndex = 0; alertIndex < result.length(); ++alertIndex) { | ||
JSONObject alert = result.getJSONObject(alertIndex); | ||
|
||
title = alert.getString("title"); | ||
title = title.substring(0,1).toUpperCase()+title.substring(1); | ||
formattedActiveRange = alert.getString("formattedActiveRange"); | ||
url = TRAFFIC_PAGE_ROOT + alert.getString("id"); | ||
publicationDate = alert.getJSONObject("apiPublication").getString("dateTime"); | ||
|
||
routes = alert.getJSONArray("routes"); | ||
linesList.clear(); | ||
for (int routeIndex = 0; routeIndex < routes.length(); ++routeIndex) { | ||
linesList.add(routes.getJSONObject(routeIndex).getString("shortName")); | ||
} | ||
Collections.sort(linesList, LINE_COMPARATOR); | ||
lines = TextUtils.join(", ", linesList); | ||
|
||
// eventually add this to the data array list | ||
TrafficAdapter.TrafficInfo infos = new TrafficAdapter.TrafficInfo(formattedActiveRange, lines, title, url, publicationDate); | ||
infos.setFrom(alert.optString("activeFrom")); | ||
infos.setTo(alert.optString("activeTo")); | ||
this.data.add(infos); | ||
} | ||
} catch (JSONException e) { | ||
Toast.makeText(this.context, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); | ||
e.printStackTrace(); | ||
} | ||
|
||
Collections.sort(this.data, ALERT_COMPARATOR); | ||
|
||
// notify update | ||
this.context.runOnUiThread(() -> { | ||
this.trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(this.listView); | ||
}); | ||
}, error -> { | ||
this.hideProgressbar(); | ||
error.printStackTrace(); | ||
}); | ||
|
||
return this.trafficRequest; | ||
} | ||
|
||
@Override | ||
public void onGroupCollapse(int groupPosition) { | ||
this.requestCancel(); | ||
|
||
// notify update | ||
context.runOnUiThread(() -> { | ||
data.clear(); | ||
trafficAdapter.notifyDataSetChanged(); | ||
Utility.setListViewHeightBasedOnChildren(listView); | ||
}); | ||
} | ||
} |
Oops, something went wrong.