<commit_before>/**
 * Filename: ShuttlesFragment.java
 * 
 * The ShuttlesFragment class is an exact copy of the SherlockFragment class
 * the from ActionBarSherlock library except the name has been changed and
 * it extends SupportMapFragment instead of extending Fragment.
 * 
 * Additions to the class definition include:
 * 1. Two custom helper AsyncTask<Void, Void, Boolean> classes
 *    for download the KML and JSON data from shuttles.rpi.edu
 * 2. GoogleMap, String, and LatLng private variables among many
 *    other custom data structures
 * 3. Overridden method onCreateView
 * 
 * Additions/Edits by:
 * Author: <NAME>
 * Date: 4/12/2014
 * 
 * Credits:
 * ShuttlesPositionsDownload's doInBackground() method
 * is heavily based upon <NAME>'s initial implementation
 * of a class with a different name (ShuttleFragment).
 * 
 */

package edu.rpi.rpimobile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import edu.rpi.rpimobile.model.Shuttle;
import edu.rpi.rpimobile.model.ShuttlesRoutes;
import static android.support.v4.app.Watson.OnCreateOptionsMenuListener;
import static android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import static android.support.v4.app.Watson.OnPrepareOptionsMenuListener;

public class ShuttlesFragment extends SupportMapFragment implements OnCreateOptionsMenuListener,
																	OnPrepareOptionsMenuListener,
																	OnOptionsItemSelectedListener
{
    private SherlockFragmentActivity mActivity;
    private GoogleMap map;
    private ShuttlesRoutes routeOverlay;
    private ShuttlesRoutesDownload routesDownloadTask;
    private ShuttlesPositionsDownload positionsDownloadTask;
    private ArrayList<Shuttle> shuttles;
    private ArrayList<Marker> shuttleMarkers;
    
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	/** Upon being selected by the user, this method is called to draw the map */
    {
    	super.onCreateView(inflater, container, savedInstanceState);
    	
    	final double UnionLat = 42.72997;
    	final double UnionLon = -73.676649;
    	final LatLng centerCoords = new LatLng(UnionLat, UnionLon);
    	
    	routeOverlay = new ShuttlesRoutes();
    	shuttles = new ArrayList<Shuttle>();
    	shuttleMarkers = new ArrayList<Marker>();
    	
    	View rootView = inflater.inflate(R.layout.shuttles_fragment, container, false);
    	map = ((SupportMapFragment) mActivity.getSupportFragmentManager().findFragmentById(R.id.shuttlesview)).getMap();
    	map.setMyLocationEnabled(true);
    	map.moveCamera(CameraUpdateFactory.newLatLngZoom(centerCoords, 14));
    	
    	routesDownloadTask = new ShuttlesRoutesDownload();
    	routesDownloadTask.execute();
    	
    	updateShuttlePositionsAsynchronously();
    	
    	return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof SherlockFragmentActivity)) {
            throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
        }
        mActivity = (SherlockFragmentActivity)activity;

        super.onAttach(activity);
    }
    
    @Override
    public void onDestroyView()
    {
    	super.onDestroyView();
    	FragmentManager fm = mActivity.getSupportFragmentManager();
    	FragmentTransaction ft = fm.beginTransaction();
    	ft.remove(fm.findFragmentById(R.id.shuttlesview));
    	ft.commit();
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }

    @Override
    public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
        onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());<commit_msg>Add download cancelling and exception prevention<commit_after>/**
 * Filename: ShuttlesFragment.java
 * 
 * The ShuttlesFragment class is an exact copy of the SherlockFragment class
 * the from ActionBarSherlock library except the name has been changed and
 * it extends SupportMapFragment instead of extending Fragment.
 * 
 * Additions to the class definition include:
 * 1. Two custom helper AsyncTask<Void, Void, Boolean> classes
 *    for download the KML and JSON data from shuttles.rpi.edu
 * 2. GoogleMap, String, and LatLng private variables among many
 *    other custom data structures
 * 3. Overridden method onCreateView
 * 
 * Additions/Edits by:
 * Author: <NAME>
 * Date: 4/12/2014
 * 
 * Credits:
 * ShuttlesPositionsDownload's doInBackground() method
 * is heavily based upon <NAME>'s initial implementation
 * of a class with a different name (ShuttleFragment).
 * 
 */

package edu.rpi.rpimobile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import edu.rpi.rpimobile.model.Shuttle;
import edu.rpi.rpimobile.model.ShuttlesRoutes;
import static android.support.v4.app.Watson.OnCreateOptionsMenuListener;
import static android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import static android.support.v4.app.Watson.OnPrepareOptionsMenuListener;

public class ShuttlesFragment extends SupportMapFragment implements OnCreateOptionsMenuListener,
																	OnPrepareOptionsMenuListener,
																	OnOptionsItemSelectedListener
{
    private SherlockFragmentActivity mActivity;
    private GoogleMap map;
    private ShuttlesRoutes routeOverlay;
    private ShuttlesRoutesDownload routesDownloadTask;
    private ShuttlesPositionsDownload positionsDownloadTask;
    private ArrayList<Shuttle> shuttles;
    private ArrayList<Marker> shuttleMarkers;
    
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	/** Upon being selected by the user, this method is called to draw the map */
    {
    	super.onCreateView(inflater, container, savedInstanceState);
    	
    	final double UnionLat = 42.72997;
    	final double UnionLon = -73.676649;
    	final LatLng centerCoords = new LatLng(UnionLat, UnionLon);
    	
    	routeOverlay = new ShuttlesRoutes();
    	shuttles = new ArrayList<Shuttle>();
    	shuttleMarkers = new ArrayList<Marker>();
    	
    	View rootView = inflater.inflate(R.layout.shuttles_fragment, container, false);
    	map = ((SupportMapFragment) mActivity.getSupportFragmentManager().findFragmentById(R.id.shuttlesview)).getMap();
    	map.setMyLocationEnabled(true);
    	map.moveCamera(CameraUpdateFactory.newLatLngZoom(centerCoords, 14));
    	
    	routesDownloadTask = new ShuttlesRoutesDownload();
    	routesDownloadTask.execute();
    	
    	updateShuttlePositionsAsynchronously();
    	
    	return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof SherlockFragmentActivity)) {
            throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
        }
        mActivity = (SherlockFragmentActivity)activity;

        super.onAttach(activity);
    }
    
    @Override
	public void onStop()
    {
    	super.onStop();
    	if (routesDownloadTask != null && routesDownloadTask.getStatus() == Status.RUNNING)
    	{
    		routesDownloadTask.cancel(true);
    	}
    	if (positionsDownloadTask != null && positionsDownloadTask.getStatus() == Status.RUNNING)
    	{
    		positionsDownloadTask.cancel(true);
    	}
    }
    
    @Override
    public void onDestroyView()
    {
    	super.onDestroyView();
    	FragmentManager fm = mActivity.getSupportFragmentManager();
    	FragmentTransaction ft = fm.beginTransaction();
    	ft.remove(fm.findFragmentById(R.id.shuttlesview));
    	try
    	// avoid RuntimeException: IllegalStateException
    	// caused by calling commit() after saveInstanceState
    	// has been called on the activity already.
    	// (i.e. the user hits the back button to kill
    	//  the app)
    	{
    		ft.commit();
    	}
    	catch (Exception e)
    	{ } // we don't need to do anything. the app is killed anyway!
    	
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }

    @Override
    public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
        onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
{'fraction_non_alphanumeric': 0.09073938803668534, 'fraction_numerical': 0.005681357032708384, 'mean_word_length': 4.302065404475043, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '931127', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3599, 'n_words': 795}