Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.Spinner;
- import android.widget.Toast;
- import com.barjinx.android.MyLocation.LocationResult;
- public class MainActivity extends Activity implements LocationListener {
- public static final String PREFS_NAME = "MyPrefsFile";
- public static final String TAG = MainActivity.class.getSimpleName();
- public static final String EXTRA_MESSAGE = "com.example.android.MESSAGE";
- public static String mUserKey = null;
- public static String mUserName = new String();
- public Location mUserCoordinates = new Location(LocationManager.NETWORK_PROVIDER);
- public static String mZipCode = null;
- public static Boolean mCoordinatesObtained = false;
- public static String mSearchTerm = null;
- public static Boolean mSearchIncludeRepeats = true;
- public static String[] mLanguageParameters = {"en", "es"};
- public static ProgressDialog waitForGPS;
- private int mSearchType = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- final Map<String, Integer> searchTypes;
- {
- searchTypes = new HashMap<String, Integer>();
- searchTypes.put("Find Me a Game", 0);
- searchTypes.put("Find Me a Bar", 1);
- searchTypes.put("Find Team HotSpots", 2);
- }
- final ImageButton btnGeolocation = (ImageButton) findViewById(R.id.btnGeolocation);
- final EditText editTextZipCode = (EditText) findViewById(R.id.editTextZipCode);
- final EditText editTextSearchTerm = (EditText) findViewById(R.id.editTextSearchTerm);
- Spinner spinner = (Spinner) findViewById(R.id.spinner);
- ImageButton btnSubmit = (ImageButton) findViewById(R.id.btnSubmit);
- ArrayList<String> spinnerArray = new ArrayList<String>();
- spinnerArray.add("Find Me a Game");
- spinnerArray.add("Find Me a Bar");
- spinnerArray.add("Find Team HotSpots");
- ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
- spinner.setAdapter(spinnerAdapter);
- spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
- String selectedSearchString = (String) parent.getItemAtPosition(pos);
- mSearchType = searchTypes.get(selectedSearchString);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- });
- btnSubmit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (mZipCode.length() == 5) {
- mSearchTerm = editTextSearchTerm.getText().toString();
- mZipCode = editTextZipCode.getText().toString();
- switch (mSearchType) {
- case 0:
- beginSearch(EventsActivity.class);
- break;
- case 1:
- beginSearch(LocationsActivity.class);
- break;
- case 2:
- beginSearch(HotSpotsActivity.class);
- }
- } else {
- fillAllFieldsError();
- }
- }
- });
- final Geocoder geocoder = new Geocoder(this, Locale.getDefault());
- waitForGPS = new ProgressDialog(this);
- waitForGPS.setIndeterminate(true);
- waitForGPS.setMessage("Determining GPS Location");
- waitForGPS.setMessage("Please give us a few seconds to determine your GPS location.");
- waitForGPS.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- waitForGPS.setCancelable(false);
- waitForGPS.setProgressPercentFormat(null);
- waitForGPS.setProgressNumberFormat(null);
- waitForGPS.show();
- LocationResult locationResult = new LocationResult() {
- @Override
- public void gotLocation(Location location) {
- mUserCoordinates.setLatitude(location.getLatitude());
- mUserCoordinates.setLongitude(location.getLongitude());
- MainActivity.waitForGPS.dismiss();
- List<Address> address;
- try {
- address = geocoder.getFromLocation(mUserCoordinates.getLatitude(), mUserCoordinates.getLongitude(), 1);
- address = geocoder.getFromLocation(address.get(0).getLatitude(), address.get(0).getLongitude(), 1);
- mZipCode = address.get(0).getPostalCode();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- MyLocation myLocation = new MyLocation();
- boolean locPossible = myLocation.getLocation(this, locationResult);
- btnGeolocation.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String currentZipCode = mZipCode;
- editTextZipCode.setText(currentZipCode);
- }
- });
- }
- protected void beginSearch(Class<?> chosenClass) {
- Intent intent = new Intent(MainActivity.this, chosenClass);
- intent.putExtra("userkey", "testuser");
- intent.putExtra("geolat", mUserCoordinates.getLatitude());
- intent.putExtra("geolong", (mUserCoordinates.getLongitude()));
- intent.putExtra("geozip", mZipCode);
- intent.putExtra("searchterm",mSearchTerm);
- intent.putExtra("searchincluderepeats", mSearchIncludeRepeats);
- intent.putExtra("searchbroadcastlanguages", mLanguageParameters);
- startActivity(intent);
- }
- protected void alertNoLocationAvailable() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(getString(R.string.no_location_available_title));
- builder.setMessage(getString(R.string.no_location_available_message));
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- }
- protected void alertGPSDisabled() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(getString(R.string.enable_gps_title));
- builder.setMessage(getString(R.string.enable_gps_message));
- builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
- dialog.dismiss();
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- protected void errorLocation(int errorTitle, int errorMessage) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(errorTitle);
- builder.setMessage(errorMessage);
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- protected void fillAllFieldsError() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.no_fields_error_title);
- builder.setMessage(R.string.no_fields_error_message);
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int selectedMenuItem = item.getItemId();
- if (selectedMenuItem == R.id.action_settings) {
- Intent settingsIntent = new Intent(this, SettingsActivity.class);
- startActivity(settingsIntent);
- return true;
- } else if (selectedMenuItem == R.id.send_feedback) {
- Intent sendFeedback = new Intent(android.content.Intent.ACTION_SEND);
- sendFeedback.setType("message/rfc822");
- sendFeedback.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback on BarJinx...");
- startActivity(Intent.createChooser(sendFeedback, "Send Email"));
- //TODO: Create Feedback Activity to send email
- return true;
- }
- return false;
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- finish();
- }
- @Override
- public void onLocationChanged(Location location) {}
- @Override
- public void onProviderDisabled(String provider) {}
- @Override
- public void onProviderEnabled(String provider) {}
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement