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
@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) {
// 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);
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);
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)
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) {
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(
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);
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);
}
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
@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) {
// 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);
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);
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));
}
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);
}
+/**
+ * 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;
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;
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 {
}
}
- 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.
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)
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;
}
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);
--- /dev/null
+/**
+ * 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;
+ }
+}
--- /dev/null
+/**
+ * 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;
+ }
+}
--- /dev/null
+/**
+ * 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);
+}
--- /dev/null
+/**
+ * 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;
+ }
+}
+/**
+ * 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;
public class WidgetIntentService extends IntentService {
private static final String TAG = "WidgetIntentService";
+ private static final String WIDGET_PREFERENCES = "WIDGET_PREFERENCES";
public WidgetIntentService() {
super("WIS-Thread");
@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);
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);
}
}
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());
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(){
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);
+/**
+ * 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;