Android Open Source - Realtime-Port-Authority Request Predictions






From Project

Back to project page Realtime-Port-Authority.

License

The source code is released under:

Apache License

If you think the Android project Realtime-Port-Authority listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package rectangledbmi.com.pittsburghrealtimetracker.handlers;
//  ww w .  ja  v a  2s .co m
import android.app.FragmentManager;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.WindowManager;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import rectangledbmi.com.pittsburghrealtimetracker.BusInformationDialog;
import rectangledbmi.com.pittsburghrealtimetracker.R;
import rectangledbmi.com.pittsburghrealtimetracker.handlers.containers.ETAContainer;
import rectangledbmi.com.pittsburghrealtimetracker.hidden.PortAuthorityAPI;
import rectangledbmi.com.pittsburghrealtimetracker.world.Prediction;

/**
 * Gets the predictions from a specific marker's title's id, decides if it's a stop or marker,
 * then it will add it to a special dialog.
 *
 * Created by epicstar on 12/17/14.
 */
public class RequestPredictions extends AsyncTask<String, Void, ETAContainer> {

//    private Marker marker;
//    private GoogleMap mMap;
    private Marker marker;
//    private Set<Integer> busIds;
//    private Set<Integer> stopIds;
    private Set<String> selectedBuses;
//    private FragmentManager fragmentManager;
    private Context context;


/*    /**
     * Initializes the asynctask
     * @param busIds set of id of buses
     * @param stopIds set of id of bus stops
     * @param fragmentManager the fragment manager class from the activity
     * @param context the context of the activity
     */
    public RequestPredictions(/*GoogleMap mMap,
                              Marker marker,
                              Set<Integer> busIds, Set<Integer> stopIds,
                              FragmentManager fragmentManager,
                              Set<String> selectedBuses,*/
                              Context context,
                              Marker marker,
                              Set<String> selectedBuses


    ) {
        this.marker = marker;
//        this.mMap = mMap;
//        this.busIds = busIds;
//        this.stopIds = stopIds;
        this.selectedBuses = selectedBuses;
//        this.fragmentManager = fragmentManager;
        this.context = context;
    }

    /**
     * This is the background thread...
     *
     * Makes sure that:
     * * we are looking at a stop or bus id
     * * get the info of the bus or stop id
     * * return this into an ETAContainer
     *
     * @return an ETA Container that contains the dialog's title and message
     */
    @Override
    protected ETAContainer doInBackground(String... params) {
//        System.out.println("marker title: " + marker[0]);
        String markerTitle = params[0];
        String message = "";
//        String snippet = null;
        try {
            URL url = null;
            int id = Integer.parseInt(markerTitle.substring(markerTitle.indexOf("(") + 1, markerTitle.indexOf(")")));
            int sw = 0;
            if (params[0].contains("INBOUND") || params[0].contains("OUTBOUND")) {
                Log.i("stop_id", Integer.toString(id));
                Log.i("selected buses", selectedBuses.toString());

                url = PortAuthorityAPI.getStopPredictions(id, selectedBuses);
                Log.i("url", url.toString());
                Log.i("prediction_type", "stop");

                sw = 1; //looking at a bus id
            } else {
                Log.i("prediction_type", "bus");
                url = PortAuthorityAPI.getBusPredictions(id);
//                sw = 0; // we are looking at a bus id
            }
//            System.out.println(url);
//            Log.i("predictions_url", url.toString());
            if(url != null) {
                PredictionsXMLPullParser predictionsXMLPullParser = new PredictionsXMLPullParser(url, context);
                List<Prediction> predictions = predictionsXMLPullParser.createPredictionList();
                StringBuilder st = new StringBuilder();
//                ConcurrentHashMap<String, StringBuilder> busPredictions = new ConcurrentHashMap<>();
                LinkedList<String> stopPredictions = new LinkedList<>();

                if(predictions != null) {
                    int i = 0;
                    for(Prediction prediction : predictions) {
                        Log.i("time", prediction.getPrdtm().split(" ")[1]);
                        SimpleDateFormat date = new SimpleDateFormat("hh:mm a");

                        StringBuilder addString = new StringBuilder(date.format(new SimpleDateFormat("HH:mm").parse(prediction.getPrdtm().split(" ")[1])));
//                        System.out.println(addString);

                        if(sw == 0) { // bus dialog that displays stops
                          /*  StringBuilder tempString = busPredictions.putIfAbsent(prediction.getStpnm(), addString);
                            if(tempString != null) {
                                tempString.append("\t");
                                tempString.append(addString);
                            }*/
                            stopPredictions.add("(" + prediction.getStpid() + ")" + prediction.getStpnm() + ": " + addString);
//                            message = createMessage(busPredictions, sw);
//                            System.out.println(busPredictions.get(prediction.getStpid()));
                        } else if (sw == 1) { // stop dialog that displays routes
                            Log.i("delayed", prediction.getDly());
                            stopPredictions.add(prediction.getRt() + " (" + prediction.getVid() + "): " + addString + (prediction.getDly().equals("true") ? " - delayed" : ""));
                        }
                        if(++i == 8)
                            break;
                    }
//                    if(sw == 0) {
                        message = createMessage(stopPredictions);
//                    } else if(sw == 1) {
//                        message = createMessage(busPredictions, sw);
//                    }
//                    System.out.println(message);
                    return new ETAContainer(markerTitle, message);
                }
            }
            

        } catch(MalformedURLException e) {
            Log.i("HELLO", e.getMessage());
        } catch (XmlPullParserException | IOException e) {
            Log.e("XML_ERROR", e.getMessage());
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * TODO: improve this to be arranged by stop time especially when looking at a bus's ETAs probably will have to use a different structure for that case
     *
     * takes the message from the Predictions Pull Parser and turns it into a readable message (for now)
     *
     * @param idTimes
     * @param sw
     * @return
     */
    public String createMessage(ConcurrentHashMap<String, StringBuilder> idTimes, int sw) {
        StringBuilder st = new StringBuilder();
        Set<Map.Entry<String, StringBuilder>> idEntries = idTimes.entrySet();
        int i = 0;
        for(Map.Entry<String, StringBuilder> info : idEntries) {
            if(i == 5 && sw == 0){ break; }
            i++;
//            System.out.println("key: " + info.getKey());
//            System.out.println("value " + info.getValue());
            st.append(info.getKey());
            st.append(":\n  ");
            st.append(info.getValue());
            st.append("\n");
        }

        return st.toString();
    }

    public String createMessage(LinkedList<String> stringLinkedList) {
        StringBuilder st = new StringBuilder();

        for(String string : stringLinkedList) {
            st.append(string);
            st.append("\n");
        }

        return st.toString();
    }

    protected void onPostExecute(ETAContainer container) {
        if(container != null) {
//            showDialog(container.getMessage(), container.getTitle());
//        if(snippet != null && snippet.length() > 0)
            if(!container.getMessage().isEmpty())
                marker.setSnippet(container.getMessage());
            else
                marker.setSnippet(context.getResources().getString(R.string.predictions_not_available));
            marker.showInfoWindow();
        }

    }

//    public void showDialog(String message, String title) {
//        BusInformationDialog busInfoDialog = new BusInformationDialog();
//        busInfoDialog.setMessage(message);
//        busInfoDialog.setTitle(title);
//        busInfoDialog.setStyle(R.style.Base_Theme_AppCompat_Light_Dialog, 10);
//        busInfoDialog.setCancelable(true);
//        busInfoDialog.show(fragmentManager, "ETAs");
////        busInfoDialog.show();
//    }
}




Java Source Code List

.Globals.java
.PortAuthorityRealtime.java
rectangledbmi.com.pittsburghrealtimetracker.AboutActivity.java
rectangledbmi.com.pittsburghrealtimetracker.ApplicationTest.java
rectangledbmi.com.pittsburghrealtimetracker.BusInformationDialog.java
rectangledbmi.com.pittsburghrealtimetracker.NavigationDrawerFragment.java
rectangledbmi.com.pittsburghrealtimetracker.SelectTransit.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusNotRunningException.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusSaxHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.BusXMLPullParser.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.InputSave.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.PredictionsXMLPullParser.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestLine.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestPredictions.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestRoutes.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RequestTask.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.RouteSaxHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.TransitSAXHandler.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.containers.ETAContainer.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.containers.RequestLineContainer.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.CheckableRelativeLayout.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.ColoredArrayAdapter.java
rectangledbmi.com.pittsburghrealtimetracker.handlers.extend.ETAWindowAdapter.java
rectangledbmi.com.pittsburghrealtimetracker.world.Bus.java
rectangledbmi.com.pittsburghrealtimetracker.world.LineInfo.java
rectangledbmi.com.pittsburghrealtimetracker.world.Prediction.java
rectangledbmi.com.pittsburghrealtimetracker.world.Route.java
rectangledbmi.com.pittsburghrealtimetracker.world.TransitStop.java