Soonami

package com.example.android.soonami;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {
	public static final String LOG_TAG = MainActivity.class.getSimpleName();

	private static final String USGS_REQUEST_URL =
		"http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6";

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TsunamiAsyncTask task = new TsunamiAsyncTask();
		task.execute();
	}

	private void updateUi(Event earthquake){
		TextView titleTextView = (TextView) findViewById(R.id.title);
		titleTextView.setText(earthquake.title);

		TextView dateTextView = (TextView) findViewById(R.id.date);
		dateTextView.setText(getDateString(earthquake.time));

		TextView tsunamiTextView = (TextView) findViewById(R.id.tsunami_alert);
		titleTextView.setText(earthquake.title);
	}

	private String getDateString(long timeInMilliseconds){
		SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyy 'at' HH:mm:ss z");
		return formatter.format(timeInMilliseconds);
	}

	private String getTsunamiAlertString(int tsunamiAlert){
		switch (tsunamiAlert){
			case 0:
				return getString(R.string.alert_no);
			case 1:
				return getString(R.string.alert_yes);
			default:
				return getString(R.string.alert_no_available);
		}
	}

	private class TsunamiAsyncTask extends AsyncTask<URL, Void, Event>{

		@Override
		protected Event doInBackground(URL... urls){
			// Create URL object
			URL url = createUrl(USGS_REQUEST_URL);
		} catch (IOException e){
			// todo handle the IOException
		}

		Event earthquake = extractFeatureFromJson(jsonResponse);

		return earthquake;
	}

	@Override
	protected void onPostExecute(Event earthquake){
		if (earthquake == null){
			return;
		}

		updateUi(earthquake);
	}

	private URL createUrl(String stringUrl){
		URL url = null;
		try {
			url = new URL(stringUrl);
		} catch (MalformedURLException exception){
			Log.e(LOG_TAG, "Error with creating URL", exception);
			return null;
		}
		return url;
	}

	private String makeHttpRequest(URL url) throws IOException {
		String jsonResponse = "";
		HttpURLConnection urlConnection = null;
		InputStream inputStream = null;
		try {
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setReadTimeout(10000 /* milliseconds */);
			urlConnection.setConnectTimeout(15000 /* milliseconds */);
			urlConnection.connect();
			inputStream = urlConnection.getInputStream();
			jsonResponse = readFromStream(inputStream);
		} catch (IOException e){
			// todo
		} finally {
			if (urlConnection != null){
				urlConnection.disconnect();
			}
			if (inputStream != null){
				inputStream.close();
			}
		}
		return jsonResponse;
	}

	private String readFromStream(InputStream inputStream) throws IOException {
		StringBuilder output = new StringBuilder();
		if (inputStream != null){
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
			BufferedReader reader = new BufferedReader(inputStreamReader);
			String line = reader.readLine();
			while (line != null){
				output.append(line);
				line = reader.readLine();
			}
		}
		return output.toString();
	}

	private Event extractFeatureFromJson(String earthquakeJSON){
		try {
			JSONObject baseJsonResponse = new JSONObject(earthquakeJSON);
			JSONArray featureArray = baseJsonResponse.getJSONArray("features");

			if (featureArray.length() > 0){
				JSONObject firstFeature = featureArray.getJSONObject(0);
				JSONObject properties = firstFeature.getJSONObject("properties");

				String title = properties.getString("title");
				long time = properties.getLong("time");
				int tsunamiAlert = properties.getInt("tsunami");

				return new Event(title, time, tsunamiAlert);
			}
		} catch (JSONException e){
			Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
		}
		return null;
	}

}