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 95c1f87..14b9d74 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/WeatherHandler.java @@ -5,10 +5,10 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; +import android.location.Location; import android.util.Log; import com.survivingwithandroid.weather.lib.WeatherClient; -import com.survivingwithandroid.weather.lib.WeatherCode; import com.survivingwithandroid.weather.lib.WeatherConfig; import com.survivingwithandroid.weather.lib.exception.WeatherLibException; import com.survivingwithandroid.weather.lib.exception.WeatherProviderInstantiationException; @@ -21,7 +21,6 @@ import com.survivingwithandroid.weather.lib.request.WeatherRequest; import java.util.ArrayList; -import java.util.Arrays; import java.util.Date; import java.util.List; @@ -100,13 +99,17 @@ } } - /** - * - * @param offset number of days (in the past) to search for result in the cache, only for getCurrentWeatherCached - */ - public MyWeather getCurrentWeather(double lat, double lon, int offset) { + public MyWeather getCurrentWeather() { + return getCurrentWeather(MainActivity.getLocation()); + } + + public MyWeather getCurrentWeather(Location l) { + return getCurrentWeather(l.getLatitude(), l.getLongitude()); + } + + public MyWeather getCurrentWeather(double lat, double lon) { if(MainActivity.hasInternet()) return getCurrentWeatherOnline(lat, lon); - else return getCurrentWeatherCached(lat, lon, offset); + else return getCurrentWeatherCached(lat, lon, 0); } //TODO @@ -121,13 +124,25 @@ return null; } - public List getForecast(double lat, double lon) { - return getForecast(lat, lon, MAX_DAYS_FORECAST, 0); + public List getForecast(int days) { + return getForecast(MainActivity.getLocation(), days); } - public List getForecast(double lat, double lon, int days, int offset) { + public List getForecast(Location l) { + return getForecast(l.getLatitude(), l.getLongitude()); + } + + public List getForecast(Location l, int days) { + return getForecast(l.getLatitude(), l.getLongitude(), days); + } + + public List getForecast(double lat, double lon) { + return getForecast(lat, lon, MAX_DAYS_FORECAST); + } + + public List getForecast(double lat, double lon, int days) { if(MainActivity.hasInternet()) return getForecastOnline(lat, lon, days); - else return getForecastCached(lat, lon, days, offset); + else return getForecastCached(lat, lon, days, 0); } public List getForecastOnline(double lat, double lon, final int days) { @@ -144,7 +159,9 @@ DayForecast curr = foundForecasts.get(i); forecasts[i] = curr; weathers[i] = curr.weather; - weatherList.add(new MyWeather(curr)); + MyWeather newWeather = new MyWeather(curr); + insertIntoDatabase(newWeather); + weatherList.add(newWeather); } Log.d(MainActivity.TAG, "City [" + weathers[0].location.getCity() + "] Current temp: " + weathers[0].temperature.getTemp()); @@ -185,12 +202,20 @@ return null; } - public List getHistoricalWeather(float lat, float lon, long start, long end){ + public List getHistoricalWeather(long start, long end) { + return getHistoricalWeather(MainActivity.getLocation(), start, end); + } + + public List getHistoricalWeather(Location l, long start, long end) { + return getHistoricalWeather(l.getLatitude(), l.getLongitude(), start, end); + } + + public List getHistoricalWeather(double lat, double lon, long start, long end){ if(MainActivity.hasInternet()) return getHistoricalWeatherOnline(lat, lon, start, end); else return getHistoricalWeatherCached(lat, lon, start, end); } - public List getHistoricalWeatherOnline(float lat, float lon, long start, long end) { + public List getHistoricalWeatherOnline(double lat, double 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); @@ -215,12 +240,14 @@ } }); for(HistoricalHourWeather hhw : histWeather.getHoistoricalData()) { - weathers.add(new MyWeather(hhw.weather, hhw.timestamp)); + MyWeather curr = new MyWeather(hhw.weather, hhw.timestamp); + insertIntoDatabase(curr); + weathers.add(curr); } return weathers; } - private List getHistoricalWeatherCached(float lat, float lon, long start, long end) { + private List getHistoricalWeatherCached(double lat, double lon, long start, long end) { return null; }