diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java index c95e06b..78f127a 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java @@ -46,6 +46,7 @@ import com.survivingwithandroid.weather.lib.exception.WeatherLibException; import com.survivingwithandroid.weather.lib.exception.WeatherProviderInstantiationException; import com.survivingwithandroid.weather.lib.model.CurrentWeather; +import com.survivingwithandroid.weather.lib.model.HistoricalWeather; import com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType; import com.survivingwithandroid.weather.lib.request.WeatherRequest; @@ -84,8 +85,14 @@ private Resources res; private FloatingActionButton fab; + private CurrentWeather currentWeather; + private HistoricalWeather histWeather; private WeatherClient weatherClient; private static Integer[] severeWeatherCodes; + private static float minHotTemp; + private static float maxColdTemp; + private static float minWindySpeed; + private boolean showWeatherHints; private DatabaseHandler dbHandler; @@ -186,6 +193,17 @@ weatherConfig.ApiKey = API_KEY; weatherConfig.lang = res.getString(R.string.lang_identifier); + minWindySpeed = 8; //http://www.wettergefahren-fruehwarnung.de/Artikel/beaufort.html + if(weatherConfig.unitSystem.equals(WeatherConfig.UNIT_SYSTEM.I)){ + minHotTemp = 86; //about 30 degree celsius + maxColdTemp = 50; //about 10 degree celsius + } else { + minHotTemp = 30; + maxColdTemp = 10; + } + + showWeatherHints = true; + //TODO split into sever weather and foggy, windy, sunny, ... severeWeatherCodes = new Integer[]{ WeatherCode.TORNADO.getCode(), @@ -453,6 +471,7 @@ weatherClient.getCurrentCondition(new WeatherRequest(lon, lat), new WeatherClient.WeatherEventListener() { @Override public void onWeatherRetrieved(CurrentWeather weather) { + currentWeather = weather; Log.d(TAG, "City [" + weather.weather.location.getCity() + "] Current temp: " + weather.weather.temperature.getTemp()); String title = String.format("Weather for %s in %s", weather.weather.location.getCity(), weather.weather.location.getCountry()); @@ -474,14 +493,24 @@ Log.d(TAG, title + ": " + msg); Log.d(TAG, "isSevereWeather? " + isSevereWeather); if (isSevereWeather) { - String warnMsg = "The waeather around you in " + weather.weather.location.getCity() + " is '" + weather.weather.currentCondition.getWeatherCode().getLabel(getApplicationContext()) + "'!"; + String warnMsg = "The weather around you in " + weather.weather.location.getCity() + " is '" + weather.weather.currentCondition.getWeatherCode().getLabel(getApplicationContext()) + "'!"; InformDialog informAlertDialog = new InformDialog("Weather Warning!", warnMsg); informAlertDialog.show(getFragmentManager(), "Inform Dialog - Weather Warning"); - } else { - String noWarnMsg = "The waeather around you in " + weather.weather.location.getCity() + " is '" + weather.weather.currentCondition.getWeatherCode().getLabel(getApplicationContext()) + "'"; - InformDialog informAlertDialog = new InformDialog("No Weather Warning! =)", noWarnMsg); - informAlertDialog.show(getFragmentManager(), "Inform Dialog - No Weather Warning"); - + if (showWeatherHints) { + if (isSunny()) { + InformDialog informSunnyDialog = new InformDialog("Sun tip", "The weather might be sunny. You should take some suncream with you!"); + informSunnyDialog.show(getFragmentManager(), "Inform Dialog - Sun tip"); + } else if (isCold()) { + String coldMsg; + if (isWindy()) { + coldMsg = "The weather might be cold and windy. You should take some warm and especially windproof clothes with you!"; + } else { + coldMsg = "The weather might be cold. You should take some warm clothes with you!"; + } + InformDialog informSunnyDialog = new InformDialog("Cold tip", coldMsg); + informSunnyDialog.show(getFragmentManager(), "Inform Dialog - Cold tip"); + } + } } } @@ -514,10 +543,59 @@ Log.d(TAG, "onProviderDisabled: " + provider); } - public boolean isSevereWeather(int weatherCode){ + private boolean isSevereWeather(int weatherCode){ return Arrays.asList(severeWeatherCodes).contains(weatherCode); } + //TODO take a headlamp + private boolean isAfterSunset(long time){ + return time > (currentWeather.weather.location.getSunset() * 1000); + } + + //TODO take more to drink, suncream + private boolean isSunny(){ + return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode() || + (currentWeather.weather.temperature.getTemp() >= minHotTemp || currentWeather.weather.temperature.getMaxTemp() >= minHotTemp); + } + + //TODO take more clothes + private boolean isCold(){ + return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.COLD.getCode() || + (currentWeather.weather.temperature.getTemp() <= maxColdTemp || currentWeather.weather.temperature.getMinTemp() <= maxColdTemp); + } + + //TODO take wind stopper clothes + private boolean isWindy(){ + return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.WINDY.getCode() || + (currentWeather.weather.wind.getSpeed() >= minWindySpeed); + } + + private HistoricalWeather getWeather(float lat, float lon, long start, long end){ + Date startDate = new Date(start / 1000); //OWM uses seconds instead of milliseconds + Date endDate = new Date(end / 1000); + histWeather = null; + weatherClient.getHistoricalWeather(new WeatherRequest(lon, lat), startDate, endDate, new WeatherClient.HistoricalWeatherEventListener() { + @Override + public void onWeatherRetrieved(HistoricalWeather historicalWeather) { + //TODO check if it was sunny/rainy and set fountain options based on this + histWeather = historicalWeather; + } + + @Override + public void onWeatherError(WeatherLibException wle) { + Log.e(TAG, "Weather error - parsing data"); + wle.printStackTrace(); + } + + @Override + public void onConnectionError(Throwable t) { + Log.e(TAG, "Connection Error"); + t.printStackTrace(); + } + }); + return histWeather; + } + public class InformDialog extends DialogFragment { private String title; private String msg;