package edu.purdue.app.gpshelper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpConnection;
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.protocol.BasicHttpContext;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
// So yeah... this is just a micro AWS EC2 instance i have set up...
String SERVER_HOST = "http://50.17.239.94/";
EditText etExtraNotes;
LocationManager locationManager;
LocationProvider gpsProvider;
Spinner spin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Populate the spinner with our choices
spin = (Spinner) findViewById(R.id.spinner_location_type);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.location_type_choices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
// Set the click listener for the button
Button buLog = (Button) findViewById(R.id.bu_log);
buLog.setOnClickListener(this);
// Set up the edit text for extra notes
etExtraNotes = (EditText) findViewById(R.id.et_extra_data);
// Set up the location manager for use later
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.bu_log:
// Check if the user has GPS. If the don't, then just ignore the click.
Location currentLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLoc == null) {
Toast.makeText(this, "Your device does not appear to have GPS available or enabled.", Toast.LENGTH_LONG)
.show();
return;
}
// Otherwise, get the information from the location
double latitude = currentLoc.getLatitude();
double longitude = currentLoc.getLongitude();
double accuracy = currentLoc.getAccuracy();
// Altitude, just in case someone on the international space station steals our app
double altitude = currentLoc.getAltitude();
// Create the URL for the server
// Not 100% sure how this will work yet, but I figure we can just ping a URL with all the
// location data attached then figure it out on the server-side of things.
final String urlStr = (SERVER_HOST + "log" +
"?type=" + getResources().getStringArray(R.array.location_type_choices)[spin.getSelectedItemPosition()] +
"&lat=" + latitude +
"&lng=" + longitude +
"&acc=" + accuracy +
"&alt=" + altitude +
"&etx=" + etExtraNotes.getText()).replace(" ", "_");
// Create the Toasts that will eventually be shown.
final Toast successToast = Toast.makeText(this, "Log sent successfully.", Toast.LENGTH_SHORT);
final Toast failToast = Toast.makeText(this, "Something went wrong. Call Ghostbusters NOW.", Toast.LENGTH_SHORT);
new Thread(new Runnable(){
@Override
public void run() {
try {
HttpClient http = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlStr);
HttpResponse response = http.execute(httpGet, new BasicHttpContext());
if (response.getStatusLine().getStatusCode() == 200) {
successToast.show();
} else {
failToast.show();
}
} catch (MalformedURLException e) {
failToast.show();
e.printStackTrace();
} catch (IOException e) {
failToast.show();
e.printStackTrace();
}
}
}).start();
break;
}
}
}