Advertisement
Guest User

MainActivity.java

a guest
Feb 24th, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.60 KB | None | 0 0
  1. package com.example.android;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import java.util.Map;
  9.  
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.app.ProgressDialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.location.Address;
  17. import android.location.Geocoder;
  18. import android.location.Location;
  19. import android.location.LocationListener;
  20. import android.location.LocationManager;
  21. import android.os.Bundle;
  22. import android.view.Menu;
  23. import android.view.MenuItem;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.view.WindowManager;
  27. import android.widget.AdapterView;
  28. import android.widget.ArrayAdapter;
  29. import android.widget.Button;
  30. import android.widget.EditText;
  31. import android.widget.ImageButton;
  32. import android.widget.Spinner;
  33. import android.widget.Toast;
  34.  
  35. import com.barjinx.android.MyLocation.LocationResult;
  36.  
  37. public class MainActivity extends Activity implements LocationListener {
  38.  
  39.     public static final String PREFS_NAME = "MyPrefsFile";
  40.     public static final String TAG = MainActivity.class.getSimpleName();
  41.     public static final String EXTRA_MESSAGE = "com.example.android.MESSAGE";
  42.     public static String mUserKey = null;
  43.     public static String mUserName = new String();
  44.     public Location mUserCoordinates = new Location(LocationManager.NETWORK_PROVIDER);
  45.     public static String mZipCode = null;
  46.     public static Boolean mCoordinatesObtained = false;
  47.     public static String mSearchTerm = null;
  48.     public static Boolean mSearchIncludeRepeats = true;
  49.     public static String[] mLanguageParameters = {"en", "es"};
  50.    
  51.     public static ProgressDialog waitForGPS;
  52.    
  53.     private int mSearchType = 0;
  54.    
  55.     @Override
  56.     protected void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.activity_main);
  59.        
  60.         this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
  61.        
  62.         final Map<String, Integer> searchTypes;
  63.         {
  64.             searchTypes = new HashMap<String, Integer>();
  65.             searchTypes.put("Find Me a Game", 0);
  66.             searchTypes.put("Find Me a Bar", 1);
  67.             searchTypes.put("Find Team HotSpots", 2);
  68.         }
  69.        
  70.         final ImageButton btnGeolocation = (ImageButton) findViewById(R.id.btnGeolocation);
  71.         final EditText editTextZipCode = (EditText) findViewById(R.id.editTextZipCode);
  72.         final EditText editTextSearchTerm = (EditText) findViewById(R.id.editTextSearchTerm);
  73.        
  74.         Spinner spinner = (Spinner) findViewById(R.id.spinner);
  75.         ImageButton btnSubmit = (ImageButton) findViewById(R.id.btnSubmit);
  76.        
  77.         ArrayList<String> spinnerArray = new ArrayList<String>();
  78.         spinnerArray.add("Find Me a Game");
  79.         spinnerArray.add("Find Me a Bar");
  80.         spinnerArray.add("Find Team HotSpots");
  81.        
  82.         ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
  83.         spinner.setAdapter(spinnerAdapter);
  84.        
  85.         spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  86.  
  87.             @Override
  88.             public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
  89.                 String selectedSearchString = (String) parent.getItemAtPosition(pos);
  90.                 mSearchType = searchTypes.get(selectedSearchString);
  91.             }
  92.  
  93.             @Override
  94.             public void onNothingSelected(AdapterView<?> arg0) {
  95.             }
  96.         });
  97.        
  98.         btnSubmit.setOnClickListener(new OnClickListener() {
  99.            
  100.             @Override
  101.             public void onClick(View v) {
  102.                
  103.                 if (mZipCode.length() == 5) {
  104.                     mSearchTerm = editTextSearchTerm.getText().toString();
  105.                     mZipCode = editTextZipCode.getText().toString();
  106.                     switch (mSearchType) {
  107.                         case 0:
  108.                             beginSearch(EventsActivity.class);
  109.                             break;
  110.                         case 1:
  111.                             beginSearch(LocationsActivity.class);
  112.                             break;
  113.                         case 2:
  114.                             beginSearch(HotSpotsActivity.class);
  115.                     }
  116.                 } else {
  117.                     fillAllFieldsError();
  118.                 }
  119.                
  120.             }
  121.         });
  122.        
  123.         final Geocoder geocoder = new Geocoder(this, Locale.getDefault());
  124.        
  125.         waitForGPS = new ProgressDialog(this);
  126.         waitForGPS.setIndeterminate(true);
  127.         waitForGPS.setMessage("Determining GPS Location");
  128.         waitForGPS.setMessage("Please give us a few seconds to determine your GPS location.");
  129.         waitForGPS.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  130.         waitForGPS.setCancelable(false);
  131.         waitForGPS.setProgressPercentFormat(null);
  132.         waitForGPS.setProgressNumberFormat(null);
  133.         waitForGPS.show();
  134.        
  135.         LocationResult locationResult = new LocationResult() {
  136.            
  137.             @Override
  138.             public void gotLocation(Location location) {
  139.  
  140.                 mUserCoordinates.setLatitude(location.getLatitude());
  141.                 mUserCoordinates.setLongitude(location.getLongitude());
  142.                 MainActivity.waitForGPS.dismiss();
  143.                 List<Address> address;
  144.                
  145.                 try {
  146.                     address = geocoder.getFromLocation(mUserCoordinates.getLatitude(), mUserCoordinates.getLongitude(), 1);
  147.                     address = geocoder.getFromLocation(address.get(0).getLatitude(), address.get(0).getLongitude(), 1);
  148.                     mZipCode = address.get(0).getPostalCode();
  149.                 } catch (IOException e) {
  150.                     e.printStackTrace();
  151.                 }
  152.             }
  153.         };
  154.        
  155.         MyLocation myLocation = new MyLocation();
  156.         boolean locPossible = myLocation.getLocation(this, locationResult);
  157.        
  158.         btnGeolocation.setOnClickListener(new OnClickListener() {
  159.            
  160.             @Override
  161.             public void onClick(View v) {
  162.                 String currentZipCode = mZipCode;
  163.                 editTextZipCode.setText(currentZipCode);
  164.             }
  165.         });
  166.     }
  167.    
  168.     protected void beginSearch(Class<?> chosenClass) {
  169.         Intent intent = new Intent(MainActivity.this, chosenClass);
  170.         intent.putExtra("userkey", "testuser");
  171.         intent.putExtra("geolat", mUserCoordinates.getLatitude());
  172.         intent.putExtra("geolong", (mUserCoordinates.getLongitude()));
  173.         intent.putExtra("geozip", mZipCode);
  174.         intent.putExtra("searchterm",mSearchTerm);
  175.         intent.putExtra("searchincluderepeats", mSearchIncludeRepeats);
  176.         intent.putExtra("searchbroadcastlanguages", mLanguageParameters);
  177.         startActivity(intent);
  178.     }
  179.  
  180.     protected void alertNoLocationAvailable() {
  181.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  182.         builder.setTitle(getString(R.string.no_location_available_title));
  183.         builder.setMessage(getString(R.string.no_location_available_message));
  184.         builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  185.            
  186.             @Override
  187.             public void onClick(DialogInterface dialog, int which) {
  188.                 dialog.dismiss();
  189.             }
  190.         });
  191.     }
  192.  
  193.     protected void alertGPSDisabled() {
  194.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  195.         builder.setTitle(getString(R.string.enable_gps_title));
  196.         builder.setMessage(getString(R.string.enable_gps_message));
  197.         builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  198.  
  199.             public void onClick(DialogInterface dialog, int which) {
  200.                 dialog.dismiss();
  201.             }
  202.            
  203.         });
  204.         builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  205.            
  206.             public void onClick(DialogInterface dialog, int which) {
  207.                 startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  208.                 dialog.dismiss();
  209.             }
  210.            
  211.         });
  212.         AlertDialog dialog = builder.create();
  213.         dialog.show();
  214.     }  
  215.  
  216.     protected void errorLocation(int errorTitle, int errorMessage) {
  217.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  218.         builder.setTitle(errorTitle);
  219.         builder.setMessage(errorMessage);
  220.         builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  221.            
  222.             @Override
  223.             public void onClick(DialogInterface dialog, int which) {
  224.                 dialog.dismiss();
  225.                
  226.             }
  227.         });
  228.         AlertDialog dialog = builder.create();
  229.         dialog.show();
  230.     }
  231.    
  232.     protected void fillAllFieldsError() {
  233.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  234.         builder.setTitle(R.string.no_fields_error_title);
  235.         builder.setMessage(R.string.no_fields_error_message);
  236.         builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  237.            
  238.             @Override
  239.             public void onClick(DialogInterface dialog, int which) {
  240.                 dialog.dismiss();
  241.             }
  242.         });
  243.        
  244.         AlertDialog dialog = builder.create();
  245.         dialog.show();
  246.     }
  247.    
  248.    
  249.     @Override
  250.     public boolean onCreateOptionsMenu(Menu menu) {
  251.         // Inflate the menu; this adds items to the action bar if it is present.
  252.         getMenuInflater().inflate(R.menu.main, menu);
  253.         return true;
  254.     }
  255.    
  256.    
  257.     @Override
  258.     public boolean onOptionsItemSelected(MenuItem item) {
  259.        
  260.         int selectedMenuItem = item.getItemId();
  261.        
  262.         if (selectedMenuItem == R.id.action_settings) {
  263.             Intent settingsIntent = new Intent(this, SettingsActivity.class);
  264.             startActivity(settingsIntent);
  265.             return true;
  266.         } else if (selectedMenuItem == R.id.send_feedback) {
  267.             Intent sendFeedback = new Intent(android.content.Intent.ACTION_SEND);
  268.             sendFeedback.setType("message/rfc822");
  269.             sendFeedback.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"usersupport@barjinx.com"});
  270.             sendFeedback.putExtra(android.content.Intent.EXTRA_SUBJECT, "Feedback on BarJinx...");
  271.            
  272.             startActivity(Intent.createChooser(sendFeedback, "Send Email"));
  273.            
  274.             //TODO: Create Feedback Activity to send email
  275.            
  276.             return true;
  277.         }
  278.         return false;
  279.     }
  280.    
  281.     @Override
  282.     public void onBackPressed() {
  283.         super.onBackPressed();
  284.         finish();
  285.     }
  286.  
  287.     @Override
  288.     public void onLocationChanged(Location location) {}
  289.  
  290.     @Override
  291.     public void onProviderDisabled(String provider) {}
  292.  
  293.     @Override
  294.     public void onProviderEnabled(String provider) {}
  295.  
  296.     @Override
  297.     public void onStatusChanged(String provider, int status, Bundle extras) {}
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement