diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java index 3124b04..00b8ed2 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java @@ -53,8 +53,9 @@ import com.survivingwithandroid.weather.lib.WeatherConfig; 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.DayForecast; import com.survivingwithandroid.weather.lib.model.HistoricalWeather; +import com.survivingwithandroid.weather.lib.model.Weather; import com.survivingwithandroid.weather.lib.model.WeatherForecast; import com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType; import com.survivingwithandroid.weather.lib.request.WeatherRequest; @@ -105,8 +106,11 @@ private Resources res; private FloatingActionButton fab; - private CurrentWeather currentWeather; + private Weather todaysWeather; + private Weather tomorrowsWeather; private HistoricalWeather histWeather; + private DayForecast todaysForecast; + private DayForecast tomorrowsForecast; private WeatherClient weatherClient; private static Integer[] severeWeatherCodes; private static float minHotTemp; @@ -515,28 +519,30 @@ mapController.setCenter(gp); //TODO Move this to the not-yet-implemented upload method - weatherClient.getCurrentCondition(new WeatherRequest(lon, lat), new WeatherClient.WeatherEventListener() { + weatherClient.getForecastWeather(new WeatherRequest(lon, lat), new WeatherClient.ForecastWeatherEventListener() { @Override - public void onWeatherRetrieved(CurrentWeather weather) { - currentWeather = weather; - Log.d(TAG, "City [" + weather.weather.location.getCity() + "] Current temp: " + weather.weather.temperature.getTemp()); + public void onWeatherRetrieved(WeatherForecast forecast) { + todaysForecast = forecast.getForecast(0); + tomorrowsForecast = forecast.getForecast(1); + todaysWeather = todaysForecast.weather; + tomorrowsWeather = tomorrowsForecast.weather; + Log.d(TAG, "City [" + todaysWeather.location.getCity() + "] Current temp: " + todaysWeather.temperature.getTemp()); - String title = String.format("Weather for %s in %s", weather.weather.location.getCity(), weather.weather.location.getCountry()); + String title = String.format("Weather for %s in %s", todaysWeather.location.getCity(), todaysWeather.location.getCountry()); String msg = String.format("%1$.1f%2$s (%3$.1f%4$s/%5$.1f%6$s)\nSunrise: %7$s\nSunset: %8$s\nWeatherCode: %9$d\nWeatherId: %10$d", - weather.weather.temperature.getTemp(), - weather.getUnit().tempUnit, - weather.weather.temperature.getMinTemp(), - weather.getUnit().tempUnit, - weather.weather.temperature.getMaxTemp(), - weather.getUnit().tempUnit, - df_hm.format(new Date(weather.weather.location.getSunrise() * 1000)), - df_hm.format(new Date(weather.weather.location.getSunset() * 1000)), - weather.weather.currentCondition.getWeatherCode().getCode(), - weather.weather.currentCondition.getWeatherId()); - + todaysWeather.temperature.getTemp(), + forecast.getUnit().tempUnit, + todaysWeather.temperature.getMinTemp(), + forecast.getUnit().tempUnit, + todaysWeather.temperature.getMaxTemp(), + forecast.getUnit().tempUnit, + df_hm.format(new Date(todaysWeather.location.getSunrise() * 1000)), + df_hm.format(new Date(todaysWeather.location.getSunset() * 1000)), + todaysWeather.currentCondition.getWeatherCode().getCode(), + todaysWeather.currentCondition.getWeatherId()); //InformDialog informWeatherDialog = new InformDialog(title, msg); //informWeatherDialog.show(getFragmentManager(), "Inform Dialog"); - boolean isSevereWeather = isSevereWeather(weather.weather.currentCondition.getWeatherCode().getCode()); + boolean isSevereWeather = isSevereWeather(todaysWeather.currentCondition.getWeatherCode().getCode()); Log.d(TAG, title + ": " + msg); Log.d(TAG, "isSevereWeather? " + isSevereWeather); //TODO add more hints @@ -574,7 +580,7 @@ } } if (isSevereWeather) { - String warnMsg = "The weather around you in " + weather.weather.location.getCity() + " is '" + weather.weather.currentCondition.getWeatherCode().getLabel(getApplicationContext()) + "'!"; + String warnMsg = "The weather around you in " + todaysWeather.location.getCity() + " is '" + todaysWeather.currentCondition.getWeatherCode().getLabel(getApplicationContext()) + "'!"; InformDialog informAlertDialog = new InformDialog("Weather Warning!", warnMsg); informAlertDialog.show(getFragmentManager(), "Inform Dialog - Weather Warning"); } @@ -592,23 +598,6 @@ t.printStackTrace(); } }); - - weatherClient.getForecastWeather(new WeatherRequest(lon, lat), new WeatherClient.ForecastWeatherEventListener() { - @Override - public void onWeatherRetrieved(WeatherForecast forecast) { - //forecast.getForecast(1).weather.location.getSunrise() - } - - @Override - public void onWeatherError(WeatherLibException wle) { - - } - - @Override - public void onConnectionError(Throwable t) { - - } - }); } @Override @@ -635,26 +624,34 @@ } private boolean isAfterSunset(long time){ - return time > (currentWeather.weather.location.getSunset() * 1000); + return time > (todaysWeather.location.getSunset() * 1000) && time < (tomorrowsWeather.location.getSunrise() * 1000); } private boolean isHot(){ - return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode() || - (currentWeather.weather.temperature.getTemp() >= minHotTemp || currentWeather.weather.temperature.getMaxTemp() >= minHotTemp); + return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode() || + (todaysWeather.temperature.getTemp() >= minHotTemp || todaysWeather.temperature.getMaxTemp() >= minHotTemp); } private boolean isSunny(){ - return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode(); + return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode(); } private boolean isCold(){ - return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.COLD.getCode() || - (currentWeather.weather.temperature.getTemp() <= maxColdTemp || currentWeather.weather.temperature.getMinTemp() <= maxColdTemp); + return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.COLD.getCode() || + (todaysWeather.temperature.getTemp() <= maxColdTemp || todaysWeather.temperature.getMinTemp() <= maxColdTemp); } private boolean isWindy(){ - return currentWeather.weather.currentCondition.getWeatherCode().getCode() == WeatherCode.WINDY.getCode() || - (currentWeather.weather.wind.getSpeed() >= minWindySpeed); + return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.WINDY.getCode() || + (todaysWeather.wind.getSpeed() >= minWindySpeed); + } + + private boolean isDark(long length){ + return isDark(System.currentTimeMillis(), length); + } + + private boolean isDark(long time, long length){ + return isAfterSunset(time) || isAfterSunset(time + length); } private HistoricalWeather getWeather(float lat, float lon, long start, long end){