From: Gustavo Martin Morcuende Date: Thu, 20 Nov 2014 01:12:49 +0000 (+0100) Subject: Units conversors: pressure, temperature and wind. X-Git-Tag: weatherinformation-1.0~42 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=35560fabf09f06bec0ba32cf41c884f9a8472d25;p=AndroidWeatherInformation Units conversors: pressure, temperature and wind. --- diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java index 5f7f3ff..eccfddc 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java @@ -58,10 +58,15 @@ import name.gumartinm.weather.information.parser.JPOSCurrentParser; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; import name.gumartinm.weather.information.service.ServiceCurrentParser; +import name.gumartinm.weather.information.service.conversor.PressureUnitsConversor; +import name.gumartinm.weather.information.service.conversor.TempUnitsConversor; +import name.gumartinm.weather.information.service.conversor.UnitsConversor; +import name.gumartinm.weather.information.service.conversor.WindUnitsConversor; import name.gumartinm.weather.information.widget.WidgetProvider; public class CurrentFragment extends Fragment { private static final String TAG = "CurrentFragment"; + private static final String BROADCAST_INTENT_ACTION = "name.gumartinm.weather.information.UPDATECURRENT"; private BroadcastReceiver mReceiver; @Override @@ -108,7 +113,7 @@ public class CurrentFragment extends Fragment { @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); - if (action.equals("name.gumartinm.weather.information.UPDATECURRENT")) { + if (action.equals(BROADCAST_INTENT_ACTION)) { final Current currentRemote = (Current) intent.getSerializableExtra("current"); if (currentRemote != null) { @@ -143,7 +148,7 @@ public class CurrentFragment extends Fragment { // Register receiver final IntentFilter filter = new IntentFilter(); - filter.addAction("name.gumartinm.weather.information.UPDATECURRENT"); + filter.addAction(BROADCAST_INTENT_ACTION); LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext()) .registerReceiver(this.mReceiver, filter); @@ -211,115 +216,16 @@ public class CurrentFragment extends Fragment { super.onPause(); } - private interface UnitsConversor { - - public double doConversion(final double value); - } - private void updateUI(final Current current) { - - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity().getApplicationContext()); - - // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - // 1.1 Temperature - String tempSymbol; - UnitsConversor tempUnitsConversor; - String keyPreference = this.getResources().getString(R.string.weather_preferences_temperature_key); - String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); - String unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); - if (unitsPreferenceValue.equals(values[0])) { - tempSymbol = values[0]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value - 273.15; - } - - }; - } else if (unitsPreferenceValue.equals(values[1])) { - tempSymbol = values[1]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return (value * 1.8) - 459.67; - } - - }; - } else { - tempSymbol = values[2]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value; - } - - }; - } - - // 1.2 Wind - String windSymbol; - UnitsConversor windUnitsConversor; - keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key); - values = this.getResources().getStringArray(R.array.weather_preferences_wind); - unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_wind_meters)); - if (unitsPreferenceValue.equals(values[0])) { - windSymbol = values[0]; - windUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value; - } - }; - } else { - windSymbol = values[1]; - windUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value * 2.237; - } - }; - } - - // 1.3 Pressure - String pressureSymbol; - UnitsConversor pressureUnitsConversor; - keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key); - values = this.getResources().getStringArray(R.array.weather_preferences_pressure); - unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_pressure_pascal)); - if (unitsPreferenceValue.equals(values[0])) { - pressureSymbol = values[0]; - pressureUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value; - } - }; - } else { - pressureSymbol = values[1]; - pressureUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value / 113.25d; - } - }; - } + final UnitsConversor tempUnitsConversor = new TempUnitsConversor(this.getActivity().getApplicationContext()); + final UnitsConversor windConversor = new WindUnitsConversor(this.getActivity().getApplicationContext()); + final UnitsConversor pressureConversor = new PressureUnitsConversor(this.getActivity().getApplicationContext()); // 2. Formatters - final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); - tempFormatter.applyPattern("#####.#####"); + final DecimalFormat numberFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); + numberFormatter.applyPattern("#####.#####"); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.US); @@ -328,13 +234,13 @@ public class CurrentFragment extends Fragment { if (current.getMain().getTemp_max() != null) { double conversion = (Double) current.getMain().getTemp_max(); conversion = tempUnitsConversor.doConversion(conversion); - tempMax = tempFormatter.format(conversion) + tempSymbol; + tempMax = numberFormatter.format(conversion) + tempUnitsConversor.getSymbol(); } String tempMin = ""; if (current.getMain().getTemp_min() != null) { double conversion = (Double) current.getMain().getTemp_min(); conversion = tempUnitsConversor.doConversion(conversion); - tempMin = tempFormatter.format(conversion) + tempSymbol; + tempMin = numberFormatter.format(conversion) + tempUnitsConversor.getSymbol(); } Bitmap picture; if ((current.getWeather().size() > 0) @@ -357,45 +263,45 @@ public class CurrentFragment extends Fragment { if ((current.getMain() != null) && (current.getMain().getHumidity() != null)) { final double conversion = (Double) current.getMain().getHumidity(); - humidityValue = tempFormatter.format(conversion); + humidityValue = numberFormatter.format(conversion); } String pressureValue = ""; if ((current.getMain() != null) && (current.getMain().getPressure() != null)) { double conversion = (Double) current.getMain().getPressure(); - conversion = pressureUnitsConversor.doConversion(conversion); - pressureValue = tempFormatter.format(conversion); + conversion = pressureConversor.doConversion(conversion); + pressureValue = numberFormatter.format(conversion); } String windValue = ""; if ((current.getWind() != null) && (current.getWind().getSpeed() != null)) { double conversion = (Double) current.getWind().getSpeed(); - conversion = windUnitsConversor.doConversion(conversion); - windValue = tempFormatter.format(conversion); + conversion = windConversor.doConversion(conversion); + windValue = numberFormatter.format(conversion); } String rainValue = ""; if ((current.getRain() != null) && (current.getRain().get3h() != null)) { final double conversion = (Double) current.getRain().get3h(); - rainValue = tempFormatter.format(conversion); + rainValue = numberFormatter.format(conversion); } String cloudsValue = ""; if ((current.getClouds() != null) && (current.getClouds().getAll() != null)) { final double conversion = (Double) current.getClouds().getAll(); - cloudsValue = tempFormatter.format(conversion); + cloudsValue = numberFormatter.format(conversion); } String snowValue = ""; if ((current.getSnow() != null) && (current.getSnow().get3h() != null)) { final double conversion = (Double) current.getSnow().get3h(); - snowValue = tempFormatter.format(conversion); + snowValue = numberFormatter.format(conversion); } String feelsLike = ""; if (current.getMain().getTemp() != null) { double conversion = (Double) current.getMain().getTemp(); conversion = tempUnitsConversor.doConversion(conversion); - feelsLike = tempFormatter.format(conversion); + feelsLike = numberFormatter.format(conversion); } String sunRiseTime = ""; if (current.getSys().getSunrise() != null) { @@ -427,10 +333,10 @@ public class CurrentFragment extends Fragment { this.getActivity().getApplicationContext().getString(R.string.text_units_percent)); ((TextView) getActivity().findViewById(R.id.weather_current_pressure_value)).setText(pressureValue); - ((TextView) getActivity().findViewById(R.id.weather_current_pressure_units)).setText(pressureSymbol); + ((TextView) getActivity().findViewById(R.id.weather_current_pressure_units)).setText(pressureConversor.getSymbol()); ((TextView) getActivity().findViewById(R.id.weather_current_wind_value)).setText(windValue); - ((TextView) getActivity().findViewById(R.id.weather_current_wind_units)).setText(windSymbol); + ((TextView) getActivity().findViewById(R.id.weather_current_wind_units)).setText(windConversor.getSymbol()); ((TextView) getActivity().findViewById(R.id.weather_current_rain_value)).setText(rainValue); ((TextView) getActivity().findViewById(R.id.weather_current_rain_units)).setText( @@ -445,7 +351,7 @@ public class CurrentFragment extends Fragment { this.getActivity().getApplicationContext().getString(R.string.text_units_mm3h)); ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_value)).setText(feelsLike); - ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_units)).setText(tempSymbol); + ((TextView) getActivity().findViewById(R.id.weather_current_feelslike_units)).setText(tempUnitsConversor.getSymbol()); ((TextView) getActivity().findViewById(R.id.weather_current_sunrise_value)).setText(sunRiseTime); @@ -530,7 +436,7 @@ public class CurrentFragment extends Fragment { protected void onPostExecute(final Current current) { // Call updateUI on the UI thread. - final Intent currentData = new Intent("name.gumartinm.weather.information.UPDATECURRENT"); + final Intent currentData = new Intent(BROADCAST_INTENT_ACTION); currentData.putExtra("current", current); LocalBroadcastManager.getInstance(this.localContext).sendBroadcastSync(currentData); } diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java index 12130d2..06ac0f0 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java @@ -59,9 +59,12 @@ import name.gumartinm.weather.information.parser.JPOSForecastParser; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; import name.gumartinm.weather.information.service.ServiceForecastParser; +import name.gumartinm.weather.information.service.conversor.TempUnitsConversor; +import name.gumartinm.weather.information.service.conversor.UnitsConversor; public class OverviewFragment extends ListFragment { private static final String TAG = "OverviewFragment"; + private static final String BROADCAST_INTENT_ACTION = "name.gumartinm.weather.information.UPDATEFORECAST"; private BroadcastReceiver mReceiver; @Override @@ -101,7 +104,7 @@ public class OverviewFragment extends ListFragment { @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); - if (action.equals("name.gumartinm.weather.information.UPDATEFORECAST")) { + if (action.equals(BROADCAST_INTENT_ACTION)) { final Forecast forecastRemote = (Forecast) intent.getSerializableExtra("forecast"); if (forecastRemote != null) { @@ -136,7 +139,7 @@ public class OverviewFragment extends ListFragment { // Register receiver final IntentFilter filter = new IntentFilter(); - filter.addAction("name.gumartinm.weather.information.UPDATEFORECAST"); + filter.addAction(BROADCAST_INTENT_ACTION); LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext()) .registerReceiver(this.mReceiver, filter); @@ -203,61 +206,16 @@ public class OverviewFragment extends ListFragment { fragment.updateUIByChosenDay((int) id); } } - - private interface UnitsConversor { - - public double doConversion(final double value); - } private void updateUI(final Forecast forecastWeatherData) { - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity().getApplicationContext()); - - // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - String symbol; - UnitsConversor unitsConversor; - String keyPreference = this.getResources().getString( - R.string.weather_preferences_temperature_key); - final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); - final String unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); - if (unitsPreferenceValue.equals(values[0])) { - symbol = values[0]; - unitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value - 273.15; - } - - }; - } else if (unitsPreferenceValue.equals(values[1])) { - symbol = values[1]; - unitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return (value * 1.8) - 459.67; - } - - }; - } else { - symbol = values[2]; - unitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value; - } - - }; - } - + final UnitsConversor tempUnitsConversor = new TempUnitsConversor(this.getActivity().getApplicationContext()); // 2. Update number day forecast. - keyPreference = this.getResources().getString(R.string.weather_preferences_day_forecast_key); + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this.getActivity().getApplicationContext()); + final String keyPreference = this.getResources().getString(R.string.weather_preferences_day_forecast_key); final String dayForecast = sharedPreferences.getString(keyPreference, "5"); final int mDayForecast = Integer.valueOf(dayForecast); @@ -300,18 +258,20 @@ public class OverviewFragment extends ListFragment { Double maxTemp = null; if (forecast.getTemp().getMax() != null) { maxTemp = (Double) forecast.getTemp().getMax(); - maxTemp = unitsConversor.doConversion(maxTemp); + maxTemp = tempUnitsConversor.doConversion(maxTemp); } Double minTemp = null; if (forecast.getTemp().getMin() != null) { minTemp = (Double) forecast.getTemp().getMin(); - minTemp = unitsConversor.doConversion(minTemp); + minTemp = tempUnitsConversor.doConversion(minTemp); } if ((maxTemp != null) && (minTemp != null)) { + // TODO i18n? + final String tempSymbol = tempUnitsConversor.getSymbol(); entries.add(new OverviewEntry(dayTextName, monthAndDayNumberText, - tempFormatter.format(maxTemp) + symbol, tempFormatter.format(minTemp) + symbol, + tempFormatter.format(maxTemp) + tempSymbol, tempFormatter.format(minTemp) + tempSymbol, picture)); } @@ -403,7 +363,7 @@ public class OverviewFragment extends ListFragment { protected void onPostExecute(final Forecast forecast) { // Call updateUI on the UI thread. - final Intent forecastData = new Intent("name.gumartinm.weather.information.UPDATEFORECAST"); + final Intent forecastData = new Intent(BROADCAST_INTENT_ACTION); forecastData.putExtra("forecast", forecast); LocalBroadcastManager.getInstance(this.localContext).sendBroadcastSync(forecastData); } diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/specific/SpecificFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/specific/SpecificFragment.java index 9cbe49f..a1c861e 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/specific/SpecificFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/specific/SpecificFragment.java @@ -1,3 +1,18 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package name.gumartinm.weather.information.fragment.specific; import java.text.DecimalFormat; @@ -7,11 +22,9 @@ import java.util.Calendar; import java.util.Date; import java.util.Locale; -import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; @@ -23,6 +36,10 @@ import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.model.forecastweather.Forecast; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; +import name.gumartinm.weather.information.service.conversor.PressureUnitsConversor; +import name.gumartinm.weather.information.service.conversor.TempUnitsConversor; +import name.gumartinm.weather.information.service.conversor.UnitsConversor; +import name.gumartinm.weather.information.service.conversor.WindUnitsConversor; public class SpecificFragment extends Fragment { @@ -101,116 +118,17 @@ public class SpecificFragment extends Fragment { } } - private interface UnitsConversor { - - public double doConversion(final double value); - } - private void updateUI(final Forecast forecastWeatherData, final int chosenDay) { - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity()); - - // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - // 1.1 Temperature - String tempSymbol; - UnitsConversor tempUnitsConversor; - String keyPreference = this.getResources().getString( - R.string.weather_preferences_temperature_key); - String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); - String unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); - if (unitsPreferenceValue.equals(values[0])) { - tempSymbol = values[0]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value - 273.15; - } - - }; - } else if (unitsPreferenceValue.equals(values[1])) { - tempSymbol = values[1]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return (value * 1.8) - 459.67; - } - - }; - } else { - tempSymbol = values[2]; - tempUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(final double value) { - return value; - } - - }; - } - - // 1.2 Wind - String windSymbol; - UnitsConversor windUnitsConversor; - keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key); - values = this.getResources().getStringArray(R.array.weather_preferences_wind); - unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_wind_meters)); - if (unitsPreferenceValue.equals(values[0])) { - windSymbol = values[0]; - windUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value; - } - }; - } else { - windSymbol = values[1]; - windUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value * 2.237; - } - }; - } - - // 1.3 Pressure - String pressureSymbol; - UnitsConversor pressureUnitsConversor; - keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key); - values = this.getResources().getStringArray(R.array.weather_preferences_pressure); - unitsPreferenceValue = sharedPreferences.getString( - keyPreference, this.getString(R.string.weather_preferences_pressure_pascal)); - if (unitsPreferenceValue.equals(values[0])) { - pressureSymbol = values[0]; - pressureUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value; - } - }; - } else { - pressureSymbol = values[1]; - pressureUnitsConversor = new UnitsConversor(){ - - @Override - public double doConversion(double value) { - return value / 113.25d; - } - }; - } + final UnitsConversor tempUnitsConversor = new TempUnitsConversor(this.getActivity().getApplicationContext()); + final UnitsConversor windConversor = new WindUnitsConversor(this.getActivity().getApplicationContext()); + final UnitsConversor pressureConversor = new PressureUnitsConversor(this.getActivity().getApplicationContext()); // 2. Formatters - final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); - tempFormatter.applyPattern("#####.#####"); + final DecimalFormat numberFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); + numberFormatter.applyPattern("#####.#####"); // 3. Prepare data for UI. @@ -227,13 +145,13 @@ public class SpecificFragment extends Fragment { if (forecast.getTemp().getMax() != null) { double conversion = (Double) forecast.getTemp().getMax(); conversion = tempUnitsConversor.doConversion(conversion); - tempMax = tempFormatter.format(conversion) + tempSymbol; + tempMax = numberFormatter.format(conversion) + tempUnitsConversor.getSymbol(); } String tempMin = ""; if (forecast.getTemp().getMin() != null) { double conversion = (Double) forecast.getTemp().getMin(); conversion = tempUnitsConversor.doConversion(conversion); - tempMin = tempFormatter.format(conversion) + tempSymbol; + tempMin = numberFormatter.format(conversion) + tempUnitsConversor.getSymbol(); } Bitmap picture; if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null) @@ -254,54 +172,55 @@ public class SpecificFragment extends Fragment { String humidityValue = ""; if (forecast.getHumidity() != null) { final double conversion = (Double) forecast.getHumidity(); - humidityValue = tempFormatter.format(conversion); + humidityValue = numberFormatter.format(conversion); } String pressureValue = ""; if (forecast.getPressure() != null) { double conversion = (Double) forecast.getPressure(); - conversion = pressureUnitsConversor.doConversion(conversion); - pressureValue = tempFormatter.format(conversion); + conversion = pressureConversor.doConversion(conversion); + pressureValue = numberFormatter.format(conversion); } String windValue = ""; if (forecast.getSpeed() != null) { double conversion = (Double) forecast.getSpeed(); - conversion = windUnitsConversor.doConversion(conversion); - windValue = tempFormatter.format(conversion); + conversion = windConversor.doConversion(conversion); + windValue = numberFormatter.format(conversion); } String rainValue = ""; if (forecast.getRain() != null) { final double conversion = (Double) forecast.getRain(); - rainValue = tempFormatter.format(conversion); + rainValue = numberFormatter.format(conversion); } String cloudsValue = ""; if (forecast.getRain() != null) { final double conversion = (Double) forecast.getClouds(); - cloudsValue = tempFormatter.format(conversion); + cloudsValue = numberFormatter.format(conversion); } + final String tempSymbol = tempUnitsConversor.getSymbol(); String tempDay = ""; if (forecast.getTemp().getDay() != null) { double conversion = (Double) forecast.getTemp().getDay(); conversion = tempUnitsConversor.doConversion(conversion); - tempDay = tempFormatter.format(conversion) + tempSymbol; + tempDay = numberFormatter.format(conversion) + tempSymbol; } String tempMorn = ""; if (forecast.getTemp().getMorn() != null) { double conversion = (Double) forecast.getTemp().getMorn(); conversion = tempUnitsConversor.doConversion(conversion); - tempMorn = tempFormatter.format(conversion) + tempSymbol; + tempMorn = numberFormatter.format(conversion) + tempSymbol; } String tempEve = ""; if (forecast.getTemp().getEve() != null) { double conversion = (Double) forecast.getTemp().getEve(); conversion = tempUnitsConversor.doConversion(conversion); - tempEve = tempFormatter.format(conversion) + tempSymbol; + tempEve = numberFormatter.format(conversion) + tempSymbol; } String tempNight = ""; if (forecast.getTemp().getNight() != null) { double conversion = (Double) forecast.getTemp().getNight(); conversion = tempUnitsConversor.doConversion(conversion); - tempNight = tempFormatter.format(conversion) + tempSymbol; + tempNight = numberFormatter.format(conversion) + tempSymbol; } @@ -321,9 +240,9 @@ public class SpecificFragment extends Fragment { final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value); humidityValueView.setText(humidityValue); ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue); - ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol); + ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureConversor.getSymbol()); ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue); - ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol); + ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windConversor.getSymbol()); final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value); rainValueView.setText(rainValue); final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value); diff --git a/app/src/main/java/name/gumartinm/weather/information/service/conversor/PressureUnitsConversor.java b/app/src/main/java/name/gumartinm/weather/information/service/conversor/PressureUnitsConversor.java new file mode 100644 index 0000000..9e63a79 --- /dev/null +++ b/app/src/main/java/name/gumartinm/weather/information/service/conversor/PressureUnitsConversor.java @@ -0,0 +1,68 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package name.gumartinm.weather.information.service.conversor; + + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import name.gumartinm.weather.information.R; + +public class PressureUnitsConversor implements UnitsConversor { + private final Context context; + + public PressureUnitsConversor(final Context context) { + this.context = context; + } + + @Override + public String getSymbol() { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_pressure_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_pressure); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_pressure_pascal)); + + String pressureSymbol; + if (unitsPreferenceValue.equals(values[0])) { + pressureSymbol = values[0]; + + } else { + pressureSymbol = values[1]; + } + + return pressureSymbol; + } + + @Override + public double doConversion(final double value) { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_pressure_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_pressure); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_pressure_pascal)); + + double convertedPressureUnits; + if (unitsPreferenceValue.equals(values[0])) { + convertedPressureUnits = value; + } else { + convertedPressureUnits = value / 113.25d; + } + + return convertedPressureUnits; + } +} diff --git a/app/src/main/java/name/gumartinm/weather/information/service/conversor/TempUnitsConversor.java b/app/src/main/java/name/gumartinm/weather/information/service/conversor/TempUnitsConversor.java new file mode 100644 index 0000000..b4d8b78 --- /dev/null +++ b/app/src/main/java/name/gumartinm/weather/information/service/conversor/TempUnitsConversor.java @@ -0,0 +1,69 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package name.gumartinm.weather.information.service.conversor; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import name.gumartinm.weather.information.R; + +public class TempUnitsConversor implements UnitsConversor { + private final Context context; + + public TempUnitsConversor(final Context context) { + this.context = context; + } + + @Override + public String getSymbol() { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_temperature_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_temperature); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_temperature_celsius)); + String tempSymbol; + if (unitsPreferenceValue.equals(values[0])) { + tempSymbol = values[0]; + } else if (unitsPreferenceValue.equals(values[1])) { + tempSymbol = values[1]; + } else { + tempSymbol = values[2]; + } + + return tempSymbol; + } + + @Override + public double doConversion(double value) { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_temperature_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_temperature); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_temperature_celsius)); + + double convertedTempUnits; + if (unitsPreferenceValue.equals(values[0])) { + convertedTempUnits = value - 273.15; + } else if (unitsPreferenceValue.equals(values[1])) { + convertedTempUnits = (value * 1.8) - 459.67; + } else { + convertedTempUnits = value; + } + + return convertedTempUnits; + } +} diff --git a/app/src/main/java/name/gumartinm/weather/information/service/conversor/UnitsConversor.java b/app/src/main/java/name/gumartinm/weather/information/service/conversor/UnitsConversor.java new file mode 100644 index 0000000..97867dc --- /dev/null +++ b/app/src/main/java/name/gumartinm/weather/information/service/conversor/UnitsConversor.java @@ -0,0 +1,23 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package name.gumartinm.weather.information.service.conversor; + +public interface UnitsConversor { + + public String getSymbol(); + + public double doConversion(final double value); +} diff --git a/app/src/main/java/name/gumartinm/weather/information/service/conversor/WindUnitsConversor.java b/app/src/main/java/name/gumartinm/weather/information/service/conversor/WindUnitsConversor.java new file mode 100644 index 0000000..c6bf8e0 --- /dev/null +++ b/app/src/main/java/name/gumartinm/weather/information/service/conversor/WindUnitsConversor.java @@ -0,0 +1,66 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package name.gumartinm.weather.information.service.conversor; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import name.gumartinm.weather.information.R; + +public class WindUnitsConversor implements UnitsConversor { + private final Context context; + + public WindUnitsConversor(final Context context) { + this.context = context; + } + + @Override + public String getSymbol() { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_wind_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_wind); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_wind_meters)); + + String windSymbol; + if (unitsPreferenceValue.equals(values[0])) { + windSymbol = values[0]; + } else { + windSymbol = values[1]; + } + + return windSymbol; + } + + @Override + public double doConversion(final double value) { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String keyPreference = context.getString(R.string.weather_preferences_wind_key); + final String[] values = context.getResources().getStringArray(R.array.weather_preferences_wind); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, context.getString(R.string.weather_preferences_wind_meters)); + + double convertedWindUnits; + if (unitsPreferenceValue.equals(values[0])) { + convertedWindUnits = value; + } else { + convertedWindUnits = value * 2.237; + } + + return convertedWindUnits; + } +} diff --git a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java index b9bbd44..95e37df 100644 --- a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java +++ b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java @@ -1,3 +1,18 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package name.gumartinm.weather.information.widget; import android.app.IntentService; @@ -37,6 +52,7 @@ import java.util.Locale; public class WidgetIntentService extends IntentService { private static final String TAG = "WidgetIntentService"; + private static final String WIDGET_PREFERENCES = "WIDGET_PREFERENCES"; public WidgetIntentService() { super("WIS-Thread"); @@ -44,7 +60,6 @@ public class WidgetIntentService extends IntentService { @Override protected void onHandleIntent(final Intent intent) { - Log.i(TAG, "onHandleIntent"); final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); final boolean isRefreshAppWidget = intent.getBooleanExtra("refreshAppWidget", false); @@ -59,47 +74,16 @@ public class WidgetIntentService extends IntentService { if (weatherLocation == null) { // Nothing to do. Show error. - final RemoteViews view = this.makeErrorView(appWidgetId); - - final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); - store.removeWidgetCurrentData(appWidgetId); - + final RemoteViews view = this.makeViewOnError(appWidgetId); this.updateWidget(view, appWidgetId); return; } - // TODO: improve this code. Too tired right now... if (!isRefreshAppWidget) { - RemoteViews view; - - final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); - final Current current = store.getWidgetCurrentData(appWidgetId); - if (current != null) { - // Update UI. - view = this.makeView(current, weatherLocation, appWidgetId); - } else { - // Show error. - view = this.makeErrorView(appWidgetId); - - store.removeWidgetCurrentData(appWidgetId); - } + final RemoteViews view = this.makeViewOnNotRefresh(weatherLocation, appWidgetId); this.updateWidget(view, appWidgetId); } else { - RemoteViews view; - - final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); - final Current current = this.getRemoteCurrent(weatherLocation); - if (current != null) { - // Update UI. - view = this.makeView(current, weatherLocation, appWidgetId); - - store.saveWidgetCurrentData(current, appWidgetId); - } else { - // Show error. - view = this.makeErrorView(appWidgetId); - - store.removeWidgetCurrentData(appWidgetId); - } + final RemoteViews view = this.makeViewOnRefresh(weatherLocation,appWidgetId); this.updateWidget(view, appWidgetId); } } @@ -110,6 +94,53 @@ public class WidgetIntentService extends IntentService { store.removeWidgetCurrentData(appWidgetId); } + private RemoteViews makeViewOnError(final int appWidgetId) { + final RemoteViews view = this.makeErrorView(appWidgetId); + + final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); + store.removeWidgetCurrentData(appWidgetId); + + return view; + } + + private RemoteViews makeViewOnNotRefresh(final WeatherLocation weatherLocation, final int appWidgetId) { + RemoteViews view; + + final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); + final Current current = store.getWidgetCurrentData(appWidgetId); + if (current != null) { + // Update UI. + view = this.makeView(current, weatherLocation, appWidgetId); + } else { + // Show error. + view = this.makeErrorView(appWidgetId); + + store.removeWidgetCurrentData(appWidgetId); + } + + return view; + } + + private RemoteViews makeViewOnRefresh(final WeatherLocation weatherLocation, final int appWidgetId) { + RemoteViews view; + + final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); + final Current current = this.getRemoteCurrent(weatherLocation); + if (current != null) { + // Update UI. + view = this.makeView(current, weatherLocation, appWidgetId); + + store.saveWidgetCurrentData(current, appWidgetId); + } else { + // Show error. + view = this.makeErrorView(appWidgetId); + + store.removeWidgetCurrentData(appWidgetId); + } + + return view; + } + private Current getRemoteCurrent(final WeatherLocation weatherLocation) { final ServiceCurrentParser weatherService = new ServiceCurrentParser(new JPOSCurrentParser()); @@ -160,14 +191,13 @@ public class WidgetIntentService extends IntentService { private RemoteViews makeView(final Current current, final WeatherLocation weatherLocation, final int appWidgetId) { - // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. UnitsConversor tempUnitsConversor; String keyPreference = this.getApplicationContext().getString(R.string.widget_preferences_temperature_units_key); String realKeyPreference = keyPreference + "_" + appWidgetId; // What was saved to permanent storage (or default values if it is the first time) - final int tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).getInt(realKeyPreference, 0); + final int tempValue = this.getSharedPreferences(WIDGET_PREFERENCES, Context.MODE_PRIVATE).getInt(realKeyPreference, 0); final String tempSymbol = this.getResources().getStringArray(R.array.weather_preferences_temperature)[tempValue]; if (tempValue == 0) { tempUnitsConversor = new UnitsConversor(){ @@ -203,7 +233,7 @@ public class WidgetIntentService extends IntentService { keyPreference = this.getApplicationContext().getString(R.string.widget_preferences_country_switch_key); realKeyPreference = keyPreference + "_" + appWidgetId; // What was saved to permanent storage (or default values if it is the first time) - final boolean isCountry = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE) + final boolean isCountry = this.getSharedPreferences(WIDGET_PREFERENCES, Context.MODE_PRIVATE) .getBoolean(realKeyPreference, false); diff --git a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetProvider.java b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetProvider.java index acba494..4086740 100644 --- a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetProvider.java +++ b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetProvider.java @@ -1,3 +1,18 @@ +/** + * Copyright 2014 Gustavo Martin Morcuende + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package name.gumartinm.weather.information.widget;