diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java index db38055..2e4055c 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java @@ -70,7 +70,6 @@ private static final int MIN_POI_RELOAD_TIME = 300000; //5min, in ms private static final int MIN_POI_RELOAD_DIST = 500; //0.5km, in m private static int MAX_POIS_AROUND = 20; - private static int MAX_DAYS_FORECAST = 3; public static final int MAX_WIFI_LEVELS = 5; private static ConnectivityManager cm; private static TelephonyManager tm; @@ -86,8 +85,6 @@ private static Resources res; private FloatingActionButton fab; - private static boolean showWeatherHints; - private DatabaseHandler dbHandler; private PoiHandler poiHandler; private WeatherHandler weatherHandler; @@ -158,8 +155,6 @@ initGps(); - showWeatherHints = true; - fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override @@ -474,7 +469,7 @@ if(currentTime - lastWeatherInformation >= MIN_WEATHER_RELOAD_TIME || distance >= MIN_WEATHER_RELOAD_DIST) { lastWeatherInformation = currentTime; - weatherHandler.getForecast(location.getLatitude(), location.getLongitude(), MAX_DAYS_FORECAST); + weatherHandler.getForecast(location.getLatitude(), location.getLongitude()); } if(currentTime - lastPoiInformation >= MIN_POI_RELOAD_TIME || distance >= MIN_POI_RELOAD_DIST) { //5 minutes lastPoiInformation = currentTime; diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java b/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java index b24192b..af804f0 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MyWeather.java @@ -2,9 +2,14 @@ import android.location.Location; +import com.survivingwithandroid.weather.lib.WeatherCode; import com.survivingwithandroid.weather.lib.model.DayForecast; import com.survivingwithandroid.weather.lib.model.Weather; +import java.util.Arrays; + +import de.apps4ics.mountainnavigation.handlers.WeatherHandler; + /** * Created by Vinz on 07.03.2016. */ @@ -13,6 +18,7 @@ private float minTemp; private float maxTemp; private Location location; + private String city; private long sunrise; private long sunset; private int weatherId; //OWM id @@ -25,6 +31,41 @@ private float rain; //rain volume for the last 3 hours private float snow; //snow volume for the last 3 hours + //TODO split into severe weather and foggy, windy, sunny, ... + private static final Integer[] severeWeatherCodes = new Integer[]{ + WeatherCode.TORNADO.getCode(), + WeatherCode.TROPICAL_STORM.getCode(), + WeatherCode.HURRICANE.getCode(), + WeatherCode.SEVERE_THUNDERSTORMS.getCode(), + WeatherCode.THUNDERSTORMS.getCode(), + WeatherCode.MIXED_RAIN_SNOW.getCode(), + WeatherCode.MIXED_RAIN_SLEET.getCode(), + WeatherCode.MIXED_SNOW_SLEET.getCode(), + WeatherCode.FREEZING_DRIZZLE.getCode(), + WeatherCode.FREEZING_RAIN.getCode(), + WeatherCode.HEAVY_SHOWERS.getCode(), + WeatherCode.SNOW_FLURRIES.getCode(), + WeatherCode.LIGHT_SNOW_SHOWERS.getCode(), + WeatherCode.BLOWING_SNOW.getCode(), + WeatherCode.SNOW.getCode(), + WeatherCode.HAIL.getCode(), + WeatherCode.SLEET.getCode(), + WeatherCode.FOGGY.getCode(), + WeatherCode.HAZE.getCode(), + WeatherCode.WINDY.getCode(), + WeatherCode.COLD.getCode(), + WeatherCode.SUNNY.getCode(), + WeatherCode.MIXED_RAIN_AND_HAIL.getCode(), + WeatherCode.ISOLATED_THUNDERSTORMS.getCode(), + WeatherCode.SCATTERED_THUNDERSTORMS.getCode(), + WeatherCode.HEAVY_SNOW.getCode(), + WeatherCode.SCATTERED_SNOW_SHOWERS.getCode(), + WeatherCode.THUNDERSHOWERS.getCode(), + WeatherCode.SNOW_SHOWERS.getCode(), + WeatherCode.ISOLATED_THUDERSHOWERS.getCode(), + WeatherCode.TORNADO.getCode() + }; + public MyWeather(DayForecast dayForecast) { this(dayForecast.weather, dayForecast.timestamp); } @@ -35,6 +76,7 @@ location = new Location(""); location.setLatitude(weather.location.getLatitude()); location.setLongitude(weather.location.getLongitude()); + city = weather.location.getCity(); } minTemp = weather.temperature.getMinTemp() > 0 ? weather.temperature.getMinTemp() : -1; maxTemp = weather.temperature.getMaxTemp() > 0 ? weather.temperature.getMaxTemp() : -1; @@ -51,6 +93,45 @@ snow = weather.snow.getAmmount() > 0 ? weather.snow.getAmmount() : -1; } + public boolean isSevereWeather(int weatherCode){ + return Arrays.asList(severeWeatherCodes).contains(weatherCode); + } + + public boolean isAfterSunset(){ + return isAfterSunset(System.currentTimeMillis()); + } + + public boolean isAfterSunset( long time){ + return time > (getSunset() * 1000) && time < (getSunrise() * 1000); + } + + public boolean isHot(){ + return getWeatherCode() == WeatherCode.SUNNY.getCode() || + getMaxTemp() >= WeatherHandler.getMinHotTemp(); + } + + public boolean isSunny(){ + return getWeatherCode() == WeatherCode.SUNNY.getCode(); + } + + public boolean isCold(){ + return getWeatherCode() == WeatherCode.COLD.getCode() || + getMinTemp() <= WeatherHandler.getMaxColdTemp(); + } + + public boolean isWindy(){ + return getWeatherCode() == WeatherCode.WINDY.getCode() || + (getWindSpeed() >= WeatherHandler.getMinWindySpeed()); + } + + public boolean isDark(long length){ + return isDark(System.currentTimeMillis(), length); + } + + public boolean isDark(long time, long length){ + return isAfterSunset(time) || isAfterSunset(time + length); + } + public int getClouds() { return clouds; } @@ -63,6 +144,8 @@ return location; } + public String getCity() { return city; } + public float getMaxTemp() { return maxTemp; } diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java index 66844c6..95c1f87 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java @@ -41,55 +41,20 @@ private Context context; private WeatherDatabase weatherDb; private WeatherClient weatherClient; - private Weather todaysWeather; - private Weather tomorrowsWeather; private HistoricalWeather histWeather; - private DayForecast todaysForecast; - private DayForecast tomorrowsForecast; + private DayForecast[] forecasts; + private Weather[] weathers; private final static float MIN_HOT_TEMP_DEFAULT = 30.0f; private final static float MAX_COLD_TEMP_DEFAULT = 10.0f; + private final static int MAX_DAYS_FORECAST_DEFAULT = 3; private static float MIN_HOT_TEMP; private static float MAX_COLD_TEMP; - private static float minWindySpeed; + private static int MAX_DAYS_FORECAST; + private static float MIN_WINDY_SPEED; private static Activity activity; private static FragmentManager fragmentManager; - //TODO split into severe weather and foggy, windy, sunny, ... - private static final Integer[] severeWeatherCodes = new Integer[]{ - WeatherCode.TORNADO.getCode(), - WeatherCode.TROPICAL_STORM.getCode(), - WeatherCode.HURRICANE.getCode(), - WeatherCode.SEVERE_THUNDERSTORMS.getCode(), - WeatherCode.THUNDERSTORMS.getCode(), - WeatherCode.MIXED_RAIN_SNOW.getCode(), - WeatherCode.MIXED_RAIN_SLEET.getCode(), - WeatherCode.MIXED_SNOW_SLEET.getCode(), - WeatherCode.FREEZING_DRIZZLE.getCode(), - WeatherCode.FREEZING_RAIN.getCode(), - WeatherCode.HEAVY_SHOWERS.getCode(), - WeatherCode.SNOW_FLURRIES.getCode(), - WeatherCode.LIGHT_SNOW_SHOWERS.getCode(), - WeatherCode.BLOWING_SNOW.getCode(), - WeatherCode.SNOW.getCode(), - WeatherCode.HAIL.getCode(), - WeatherCode.SLEET.getCode(), - WeatherCode.FOGGY.getCode(), - WeatherCode.HAZE.getCode(), - WeatherCode.WINDY.getCode(), - WeatherCode.COLD.getCode(), - WeatherCode.SUNNY.getCode(), - WeatherCode.MIXED_RAIN_AND_HAIL.getCode(), - WeatherCode.ISOLATED_THUNDERSTORMS.getCode(), - WeatherCode.SCATTERED_THUNDERSTORMS.getCode(), - WeatherCode.HEAVY_SNOW.getCode(), - WeatherCode.SCATTERED_SNOW_SHOWERS.getCode(), - WeatherCode.THUNDERSHOWERS.getCode(), - WeatherCode.SNOW_SHOWERS.getCode(), - WeatherCode.ISOLATED_THUDERSHOWERS.getCode(), - WeatherCode.TORNADO.getCode() - }; - public WeatherHandler(Context context) { weatherDb = new WeatherDatabase(context); this.context = context; @@ -102,20 +67,22 @@ //TODO close app? } + Resources res = MainActivity.getRes(); + SharedPreferences sharedPrefs = MainActivity.getSharedPrefs(); + MAX_DAYS_FORECAST = Integer.parseInt(MainActivity.getSharedPrefs().getString( + res.getString(R.string.settings_weather_days_forecast), + String.valueOf(MAX_DAYS_FORECAST_DEFAULT))); WeatherClient.ClientBuilder weatherBuilder = new WeatherClient.ClientBuilder(); WeatherConfig weatherConfig = new WeatherConfig(); weatherConfig.unitSystem = WeatherConfig.UNIT_SYSTEM.M; weatherConfig.ApiKey = API_KEY; - weatherConfig.lang = MainActivity.getRes().getString(R.string.lang_identifier); - weatherConfig.numDays = 2; - - SharedPreferences sharedPrefs = MainActivity.getSharedPrefs(); - Resources res = MainActivity.getRes(); + weatherConfig.lang = res.getString(R.string.lang_identifier); + weatherConfig.numDays = MAX_DAYS_FORECAST; MIN_HOT_TEMP = Float.parseFloat(sharedPrefs.getString(res.getString(R.string.settings_hint_min_hot_temp_key), String.valueOf(MIN_HOT_TEMP_DEFAULT))); MAX_COLD_TEMP = Float.parseFloat(sharedPrefs.getString(res.getString(R.string.settings_hint_max_cold_temp_key), String.valueOf(MAX_COLD_TEMP_DEFAULT))); - minWindySpeed = 8; //http://www.wettergefahren-fruehwarnung.de/Artikel/beaufort.html + MIN_WINDY_SPEED = 8; //http://www.wettergefahren-fruehwarnung.de/Artikel/beaufort.html if(weatherConfig.unitSystem.equals(WeatherConfig.UNIT_SYSTEM.I)){ MIN_HOT_TEMP = celsiusToFahrenheit(MIN_HOT_TEMP); MAX_COLD_TEMP = celsiusToFahrenheit(MAX_COLD_TEMP); @@ -142,46 +109,60 @@ else return getCurrentWeatherCached(lat, lon, offset); } + //TODO private MyWeather getCurrentWeatherOnline(double lat, double lon) { //return new MyWeather(); return null; } + //TODO private MyWeather getCurrentWeatherCached(double lat, double lon, int offset) { //return new MyWeather(); return null; } - public void getForecast(double lat, double lon, int days) { - getForecast(lat, lon, days, 0); + public List getForecast(double lat, double lon) { + return getForecast(lat, lon, MAX_DAYS_FORECAST, 0); } - public void getForecast(double lat, double lon, int days, int offset) { -//TODO Move this to the not-yet-implemented upload method + public List getForecast(double lat, double lon, int days, int offset) { + if(MainActivity.hasInternet()) return getForecastOnline(lat, lon, days); + else return getForecastCached(lat, lon, days, offset); + } + + public List getForecastOnline(double lat, double lon, final int days) { + final List weatherList = new ArrayList<>(); weatherClient.getForecastWeather(new WeatherRequest(lon, lat), new WeatherClient.ForecastWeatherEventListener() { @Override public void onWeatherRetrieved(WeatherForecast forecast) { - todaysForecast = forecast.getForecast(0); - tomorrowsForecast = forecast.getForecast(1); - todaysWeather = todaysForecast.weather; - tomorrowsWeather = tomorrowsForecast.weather; - Log.d(MainActivity.TAG, "City [" + todaysWeather.location.getCity() + "] Current temp: " + todaysWeather.temperature.getTemp()); + List foundForecasts = forecast.getForecast(); + if(foundForecasts.size() == 0) return; + int dayCount = Math.min(foundForecasts.size(), days); + forecasts = new DayForecast[dayCount]; + weathers = new Weather[dayCount]; + for(int i=0; i getForecastOnline(double lat, double lon, int days, int step) { - return null; - } - - public List getForecastCached(double lat, double lon, int days, int step, int offset) { + //TODO + public List getForecastCached(double lat, double lon, int days, int offset) { return null; } public List getHistoricalWeather(float lat, float lon, long start, long end){ - return getHistoricalWeather(lat, lon, start, end, 3); + if(MainActivity.hasInternet()) return getHistoricalWeatherOnline(lat, lon, start, end); + else return getHistoricalWeatherCached(lat, lon, start, end); } - public List getHistoricalWeather(float lat, float lon, long start, long end, int step){ + public List getHistoricalWeatherOnline(float lat, float lon, long start, long end) { ArrayList weathers = new ArrayList<>(); Date startDate = new Date(start / 1000); //OWM uses seconds instead of milliseconds Date endDate = new Date(end / 1000); @@ -240,6 +220,10 @@ return weathers; } + private List getHistoricalWeatherCached(float lat, float lon, long start, long end) { + return null; + } + private void insertIntoDatabase(Weather weather) { insertIntoDatabase(weather, 0); } @@ -252,34 +236,34 @@ weatherDb.insertWeather(weather); } - private void displayHints() { - Resources res = MainActivity.getRes(); + private void displayHints(MyWeather weather) { //TODO add more hints if (MainActivity.showWeatherHints()) { + Resources res = MainActivity.getRes(); List titles = new ArrayList<>(); List descs = new ArrayList<>(); List infos = new ArrayList<>(); - if (isSunny()) { + if (weather.isSunny()) { titles.add(res.getString(R.string.hint_dialog_titles_sunny)); descs.add(res.getString(R.string.hint_dialog_msgs_sunny)); infos.add(""); } - if (isHot()) { + if (weather.isHot()) { titles.add(res.getString(R.string.hint_dialog_titles_hot)); descs.add(res.getString(R.string.hint_dialog_msgs_hot)); infos.add(res.getString(R.string.hint_dialog_info_hot)); } - if (isCold()) { + if (weather.isCold()) { titles.add(res.getString(R.string.hint_dialog_titles_cold)); descs.add(res.getString(R.string.hint_dialog_msgs_cold)); infos.add(""); } - if (isWindy()) { + if (weather.isWindy()) { titles.add(res.getString(R.string.hint_dialog_titles_windy)); descs.add(res.getString(R.string.hint_dialog_msgs_windy)); infos.add(""); } - if (isAfterSunset()) { + if (weather.isAfterSunset()) { titles.add(res.getString(R.string.hint_dialog_titles_sunset)); descs.add(res.getString(R.string.hint_dialog_msgs_sunset)); infos.add(""); @@ -298,53 +282,30 @@ } } - private void displaySevereWeather() { - if (isSevereWeather(todaysWeather.currentCondition.getWeatherCode().getCode())) { + private void displaySevereWeather(MyWeather weather) { + if (weather.isSevereWeather(weather.getWeatherCode())) { Log.d(MainActivity.TAG, "It is severe weather! :("); Resources res = context.getResources(); - String warnMsg = String.format(res.getString(R.string.weather_warning_dialog_msg), todaysWeather.location.getCity(), todaysWeather.currentCondition.getWeatherCode().getLabel(context)); + String warnMsg = String.format(res.getString(R.string.weather_warning_dialog_msg), weather.getCity(), weather.getWeatherCode()); InformDialog informAlertDialog = new InformDialog(res.getString(R.string.weather_warning_dialog_title), warnMsg); informAlertDialog.show(fragmentManager, "Inform Dialog - Weather Warning"); } } - private boolean isSevereWeather(int weatherCode){ - return Arrays.asList(severeWeatherCodes).contains(weatherCode); + public static float getMinHotTemp() { + return MIN_HOT_TEMP; } - private boolean isAfterSunset(){ - return isAfterSunset(System.currentTimeMillis()); + public static float getMinWindySpeed() { + return MIN_WINDY_SPEED; } - private boolean isAfterSunset(long time){ - return time > (todaysWeather.location.getSunset() * 1000) && time < (tomorrowsWeather.location.getSunrise() * 1000); + public static int getMaxDaysForecast() { + return MAX_DAYS_FORECAST; } - private boolean isHot(){ - return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode() || - (todaysWeather.temperature.getTemp() >= MIN_HOT_TEMP || todaysWeather.temperature.getMaxTemp() >= MIN_HOT_TEMP); - } - - private boolean isSunny(){ - return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.SUNNY.getCode(); - } - - private boolean isCold(){ - return todaysWeather.currentCondition.getWeatherCode().getCode() == WeatherCode.COLD.getCode() || - (todaysWeather.temperature.getTemp() <= MAX_COLD_TEMP || todaysWeather.temperature.getMinTemp() <= MAX_COLD_TEMP); - } - - private boolean isWindy(){ - 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); + public static float getMaxColdTemp() { + return MAX_COLD_TEMP; } public static float celsiusToFahrenheit(float c) {