use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class TransportController method getStationsFromExternal.
/**
* Find stations by station name prefix
*
* @param prefix Name prefix
* @return List of StationResult
*/
public static List<StationResult> getStationsFromExternal(Context context, String prefix) {
prefix = Utils.escapeUmlauts(prefix);
try {
String language = LANGUAGE + Locale.getDefault().getLanguage();
// ISO-8859-1 is needed for mvv
String stationQuery = STATION_SEARCH_QUERY + UrlEscapers.urlPathSegmentEscaper().escape(prefix);
String query = STATION_SEARCH_CONST + language + '&' + stationQuery;
Utils.log(query);
NetUtils net = new NetUtils(context);
// Download possible stations
Optional<JSONObject> jsonObj = net.downloadJsonObject(query, CacheManager.VALIDITY_DO_NOT_CACHE, true);
if (!jsonObj.isPresent()) {
return Collections.emptyList();
}
List<StationResult> results = new ArrayList<>();
JSONObject stopfinder = jsonObj.get().getJSONObject("stopFinder");
// Possible values for points: Object, Array or null
JSONArray pointsArray = stopfinder.optJSONArray(POINTS);
if (pointsArray == null) {
JSONObject points = stopfinder.optJSONObject(POINTS);
if (points == null) {
return Collections.emptyList();
}
JSONObject point = points.getJSONObject("point");
addStationResult(results, point);
} else {
for (int i = 0; i < pointsArray.length(); i++) {
JSONObject point = pointsArray.getJSONObject(i);
addStationResult(results, point);
}
}
// Sort by quality
Collections.sort(results, (lhs, rhs) -> rhs.getQuality() - lhs.getQuality());
return results;
} catch (JSONException e) {
Utils.log(e, ERROR_INVALID_JSON + STATION_SEARCH);
}
return Collections.emptyList();
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class ChatActivity method showQRCode.
private void showQRCode() {
String url = "http://chart.apis.google.com/chart?cht=qr&chs=500x500&chld=M&choe=UTF-8&chl=" + UrlEscapers.urlPathSegmentEscaper().escape(currentChatRoom.getName());
final ImageView qrCode = new ImageView(this);
new NetUtils(this).loadAndSetImage(url, qrCode);
new AlertDialog.Builder(this).setTitle(R.string.add_chat_member).setView(qrCode).setPositiveButton(android.R.string.ok, null).show();
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class NewsController method downloadFromExternal.
/**
* Download news from external interface (JSON)
*
* @param force True to force download over normal sync period, else false
* @throws JSONException parsing could fail
*/
public void downloadFromExternal(boolean force) throws JSONException {
SyncManager sync = new SyncManager(mContext);
if (!force && !sync.needSync(this, TIME_TO_SYNC)) {
return;
}
NetUtils net = new NetUtils(mContext);
// Load all news sources
Optional<JSONArray> jsonArray = net.downloadJsonArray(NEWS_SOURCES_URL, CacheManager.VALIDITY_ONE_MONTH, force);
if (jsonArray.isPresent()) {
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsSourcesDao.insert(new NewsSources(obj.getInt(Const.JSON_SOURCE), obj.getString(Const.JSON_TITLE), obj.has(Const.JSON_ICON) ? obj.getString(Const.JSON_ICON) : ""));
}
}
// Load all news since the last sync
jsonArray = net.downloadJsonArray(NEWS_URL + getLastId(), CacheManager.VALIDITY_ONE_DAY, force);
// Delete all too old items
cleanupDb();
if (!jsonArray.isPresent()) {
return;
}
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsDao.insert(new News(obj.getString(Const.JSON_NEWS), obj.getString(Const.JSON_TITLE), obj.getString(Const.JSON_LINK), obj.getString(Const.JSON_SRC), obj.getString(Const.JSON_IMAGE), DateUtils.getDateTime(obj.getString(Const.JSON_DATE)), DateUtils.getDateTime(obj.getString(Const.JSON_CREATED)), 0));
}
sync.replaceIntoDb(this);
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class StudyRoomGroupManager method downloadFromExternal.
public void downloadFromExternal() throws JSONException {
Optional<JSONObject> jsonObject = new NetUtils(mContext).downloadJsonObject(STUDYROOM_URL, CacheManager.VALIDITY_DO_NOT_CACHE, true);
if (!jsonObject.isPresent()) {
return;
}
dao.removeCache();
groupDao.removeCache();
List<StudyRoomGroup> groups = getAllFromJson(jsonObject.get());
for (StudyRoomGroup group : groups) {
groupDao.insert(group);
for (StudyRoom room : group.getRooms()) {
room.setStudyRoomGroup(group.getId());
dao.insert(room);
}
}
new SyncManager(mContext).replaceIntoDb(this);
}
use of de.tum.in.tumcampusapp.utils.NetUtils in project TumCampusApp by TCA-Team.
the class KinoDetailsFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
View rootView = inflater.inflate(R.layout.fragment_kinodetails_section, container, false);
LinearLayout root = rootView.findViewById(R.id.layout);
// position in database
int position = getArguments().getInt(Const.POSITION);
KinoLocalRepository.db = TcaDb.getInstance(context);
kinoViewModel = new KinoViewModel(KinoLocalRepository.INSTANCE, KinoRemoteRepository.INSTANCE, disposable);
context = root.getContext();
net = new NetUtils(context);
kinoViewModel.getKinoByPosition(position).subscribe(kino1 -> {
kino = kino1;
showDetails(root);
});
return rootView;
}
Aggregations