diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/DownloadActivity.java b/app/src/main/java/de/apps4ics/mountainnavigation/DownloadActivity.java index c6c8807..d6156b8 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/DownloadActivity.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/DownloadActivity.java @@ -1,20 +1,31 @@ package de.apps4ics.mountainnavigation; import android.app.Activity; +import android.location.Address; import android.location.Location; +import android.os.AsyncTask; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; +import android.view.KeyEvent; import android.view.View; +import android.view.inputmethod.EditorInfo; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.Spinner; import android.widget.TextView; +import org.osmdroid.bonuspack.location.GeocoderNominatim; + +import java.io.IOException; import java.util.Date; import java.util.List; +import java.util.concurrent.ExecutionException; import de.apps4ics.mountainnavigation.handlers.WeatherHandler; @@ -25,32 +36,78 @@ LinearLayout dayLayout; SeekBar dayPicker; TextView dayDisplay; + EditText customLocationInput; Button startDownload; int nrOfForecastDays; String currentSelected; + boolean customLocationFound; + Location customLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download); + customLocationFound = false; + dayLayout = (LinearLayout) findViewById(R.id.forecastNrOfDaysLayout); dayPicker = (SeekBar) findViewById(R.id.forecastNrOfDaysSeek); dayDisplay = (TextView) findViewById(R.id.forecastNrOfDays); + customLocationInput = (EditText) findViewById(R.id.download_custom_location); dayPicker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { nrOfForecastDays = progress; dayDisplay.setText(String.valueOf(nrOfForecastDays)); } - @Override public void onStartTrackingTouch(SeekBar seekBar) { - } - @Override public void onStopTrackingTouch(SeekBar seekBar) { - + } + }); + customLocationInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if(actionId == EditorInfo.IME_ACTION_DONE) { + List
addresses = null; + try { + addresses = new GeocodingTask().execute(new String[]{ customLocationInput.getText().toString() }).get(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + if(addresses.size() == 0) { + customLocationFound = false; + customLocation = null; + //TODO translation + InformDialog informDialog = new InformDialog("Could not find location", "The entered location could not be found. Try some bigger city near your location."); + informDialog.show(getFragmentManager(), "Inform Dialog"); + } else { + Address address = addresses.get(0); + MainActivity.Toaster(address.getLocality() + " found in " + address.getCountryName(), DownloadActivity.this); + customLocation = new Location(""); + customLocation.setLatitude(address.getLatitude()); + customLocation.setLongitude(address.getLongitude()); + customLocationFound = true; + } + return true; + } + return false; + } + }); + customLocationInput.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + customLocationFound = false; + customLocation = null; + } + @Override + public void afterTextChanged(Editable s) { } }); @@ -59,11 +116,20 @@ @Override public void onClick(View v) { WeatherHandler weatherHandler = MainActivity.getWeatherHandler(); - Location l = MainActivity.getLocation(); - if(l == null) { - InformDialog informDialog = new InformDialog(getString(R.string.inform_title), getString(R.string.inform_gps_pos_not_found)); + Location l = null; + if(customLocationInput.getText().length() == 0) { + l = MainActivity.getLocation(); + if(l == null) { + InformDialog informDialog = new InformDialog(getString(R.string.inform_title), getString(R.string.inform_gps_pos_not_found)); + informDialog.show(getFragmentManager(), "Inform Dialog"); + return; + } + } else if(!customLocationFound) { + //TODO translation + InformDialog informDialog = new InformDialog("Custom location not found", "Please enter a proper city name or empty the search field to use current location."); informDialog.show(getFragmentManager(), "Inform Dialog"); - return; + } else { + l = customLocation; } if(currentSelected.equals(getString(R.string.download_forecast_key))) { weatherHandler.getForecast(l, nrOfForecastDays, DownloadActivity.this); @@ -99,6 +165,7 @@ @Override public MyWeather postRetrieve(MyWeather weather) { MainActivity.Toaster("Got current weather for current/stated location", this); + Log.d(MainActivity.TAG, weather.getCity() + " (" + MainActivity.df_full.format(new Date(weather.getTimestamp())) + ")"); return null; } @@ -114,4 +181,19 @@ } return null; } + + private class GeocodingTask extends AsyncTask