Search in sources :

Example 1 with NetUtils

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();
}
Also used : NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Example 2 with NetUtils

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();
}
Also used : NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) ImageView(android.widget.ImageView)

Example 3 with NetUtils

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);
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) News(de.tum.in.tumcampusapp.component.ui.news.model.News) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) JSONArray(org.json.JSONArray)

Example 4 with NetUtils

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);
}
Also used : StudyRoom(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) StudyRoomGroup(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)

Example 5 with NetUtils

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;
}
Also used : NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) LinearLayout(android.widget.LinearLayout)

Aggregations

NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)9 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 ImageView (android.widget.ImageView)2 NewsSources (de.tum.in.tumcampusapp.component.ui.news.model.NewsSources)2 SyncManager (de.tum.in.tumcampusapp.utils.sync.SyncManager)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 CheckBoxPreference (android.support.v7.preference.CheckBoxPreference)1 PreferenceCategory (android.support.v7.preference.PreferenceCategory)1 View (android.view.View)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 Optional (com.google.common.base.Optional)1 NewsController (de.tum.in.tumcampusapp.component.ui.news.NewsController)1 News (de.tum.in.tumcampusapp.component.ui.news.model.News)1 StudyRoom (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom)1 StudyRoomGroup (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)1 Departure (de.tum.in.tumcampusapp.component.ui.transportation.model.efa.Departure)1