Search in sources :

Example 6 with NetUtils

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);
        }
    });
}
Also used : NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) IOException(java.io.IOException)

Example 7 with NetUtils

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

Example 8 with NetUtils

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);
        }
    }
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) Optional(com.google.common.base.Optional) PreferenceCategory(android.support.v7.preference.PreferenceCategory) CheckBoxPreference(android.support.v7.preference.CheckBoxPreference) NewsController(de.tum.in.tumcampusapp.component.ui.news.NewsController) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 9 with NetUtils

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

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