use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class PlansViewFragment method downloadAll.
/**
* The actual downloading of the pdf files occurs here. Until the download process is finished
* the listview is disabled and the progressbar is shown.
*/
private void downloadAll() {
final NetUtils netUtils = new NetUtils(getContext());
final PlanFile[] files = PlanFile.values();
final int progressPerFile = 100 / files.length;
Observable.fromArray(files).compose(provider.bindToLifecycle()).subscribeOn(Schedulers.io()).zipWith(Observable.range(1, files.length), (file, i) -> {
try {
String localFile = fileDirectory + '/' + file.getLocalName();
netUtils.downloadToFile(file.getUrl(), localFile);
} catch (IOException e) {
Utils.log(e);
}
return i;
}).observeOn(AndroidSchedulers.mainThread()).subscribe(i -> {
if (i < files.length) {
progressBar.setProgress(i * progressPerFile);
} else {
progressBar.setVisibility(View.GONE);
list.setEnabled(true);
}
});
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class TransportController method getDeparturesFromExternal.
/**
* Get all departures for a station.
*
* @param stationID Station ID, station name might or might not work
* @return List of departures
*/
public static List<Departure> getDeparturesFromExternal(Context context, String stationID) {
List<Departure> result = new ArrayList<>();
try {
String language = LANGUAGE + Locale.getDefault().getLanguage();
// ISO-8859-1 is needed for mvv
String departureQuery = DEPARTURE_QUERY_STATION + UrlEscapers.urlPathSegmentEscaper().escape(stationID);
String query = DEPARTURE_QUERY_CONST + language + '&' + departureQuery;
Utils.logv(query);
NetUtils net = new NetUtils(context);
// Download departures
Optional<JSONObject> departures = net.downloadJson(query);
if (!departures.isPresent()) {
return result;
}
if (departures.get().isNull("departureList")) {
return result;
}
JSONArray arr = departures.get().getJSONArray("departureList");
for (int i = 0; i < arr.length(); i++) {
JSONObject departure = arr.getJSONObject(i);
JSONObject servingLine = departure.getJSONObject("servingLine");
JSONObject time = departure.getJSONObject("dateTime");
Date date = new GregorianCalendar(time.getInt("year"), time.getInt("month") - 1, time.getInt("day"), time.getInt("hour"), time.getInt("minute")).getTime();
result.add(new Departure(servingLine.getString("name"), servingLine.getString("direction"), // Limit symbol length to 3, longer symbols are pointless
String.format("%3.3s", servingLine.getString("symbol")).trim(), departure.getInt("countdown"), date.getTime()));
}
Collections.sort(result, (lhs, rhs) -> lhs.getCountDown() - rhs.getCountDown());
} catch (JSONException e) {
// We got no valid JSON, mvg-live is probably bugged
Utils.log(e, ERROR_INVALID_JSON + DEPARTURE_QUERY);
}
return result;
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class SettingsFragment method populateNewsSources.
private void populateNewsSources() {
PreferenceCategory newsSourcesPreference = (PreferenceCategory) findPreference("card_news_sources");
NewsController newsController = new NewsController(mContext);
List<NewsSources> newsSources = newsController.getNewsSources();
final NetUtils net = new NetUtils(mContext);
for (NewsSources newsSource : newsSources) {
final CheckBoxPreference pref = new CheckBoxPreference(mContext);
pref.setKey("card_news_source_" + newsSource.getId());
pref.setDefaultValue(true);
// Load news source icon in background and set it
final String url = newsSource.getIcon();
if (url != null) {
// Skip News that do not have a image
new Thread(() -> {
final Optional<Bitmap> bmp = net.downloadImageToBitmap(url);
mContext.runOnUiThread(() -> {
if (bmp.isPresent()) {
pref.setIcon(new BitmapDrawable(getResources(), bmp.get()));
}
});
}).start();
}
pref.setTitle(newsSource.getTitle());
if (newsSourcesPreference != null) {
newsSourcesPreference.addPreference(pref);
}
}
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class RoomFinderDetailsActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
net = new NetUtils(this);
mImage = ImageViewTouchFragment.newInstance();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mImage).commit();
room = (RoomFinderRoom) getIntent().getExtras().getSerializable(EXTRA_ROOM_INFO);
if (room == null) {
Utils.showToast(this, "No room information passed");
this.finish();
return;
}
startLoading();
}
Aggregations