From 0998fe94753578a7fa72be1591af26a6be35cfc3 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 4 Apr 2014 04:54:46 +0200 Subject: [PATCH] No time for coments --- res/values/arrays.xml | 50 +++++ res/values/strings.xml | 6 +- res/xml/weather_preferences.xml | 9 + .../fragment/WeatherInformationDataFragment.java | 37 +++- .../WeatherInformationDataFragmentDeprecated.java | 228 --------------------- .../WeatherInformationPreferencesFragment.java | 25 ++- .../example/exampletdd/service/WeatherService.java | 5 +- 7 files changed, 116 insertions(+), 244 deletions(-) delete mode 100644 src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java diff --git a/res/values/arrays.xml b/res/values/arrays.xml index e38881b..b9aedcf 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -8,4 +8,54 @@ Celsius Fahrenheit + + ar + bg + zh_cn + zh_tw + cz + nl + en + fi + fr + gl + de + it + mk + pl + pt + ro + ru + sk + sp + se + tr + ua + vi + + + Arabic + Bulgarian + Chinese Simplified + Chinese Traditional + Czech + Dutch + English + Finnish + French + Galician + German + Italian + Macedonian + Polish + Portuguese + Romanian + Russian + Slovak + Spanish + Swedish + Turkish + Ukrainian + Vietnamese + diff --git a/res/values/strings.xml b/res/values/strings.xml index 45d1148..8a39ec6 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7,7 +7,7 @@ Get weather Get weather http://api.openweathermap.org/data/{0}/weather?lat={1}&lon={2} - http://api.openweathermap.org/data/{0}/weather?q={1} + http://api.openweathermap.org/data/{0}/weather?q={1}&lang={2} http://openweathermap.org/img/w/{0}.png 2.5 London,uk @@ -33,6 +33,8 @@ weather_preferences_units Fahrenheit Celsius - Units of measurement + Unit of measurement for temperature + weather_preferences_language + Language diff --git a/res/xml/weather_preferences.xml b/res/xml/weather_preferences.xml index 3e2cbe0..4d5e35c 100644 --- a/res/xml/weather_preferences.xml +++ b/res/xml/weather_preferences.xml @@ -9,5 +9,14 @@ android:selectable="true" android:persistent="true" android:defaultValue="Celsius" /> + diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java index e955e93..32eb712 100644 --- a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -46,6 +46,7 @@ import de.example.exampletdd.service.WeatherService; public class WeatherInformationDataFragment extends Fragment implements OnClickButtons { private boolean isFahrenheit; + private String language; private static final String WEATHER_DATA_FILE = "weatherdata.file"; private static final String TAG = "WeatherInformationDataFragment"; @@ -54,6 +55,13 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB super.onCreate(savedInstanceState); this.getActivity().deleteFile(WEATHER_DATA_FILE); + + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this.getActivity()); + final String keyPreference = this.getResources().getString( + R.string.weather_preferences_language_key); + this.language = sharedPreferences.getString( + keyPreference, ""); } @Override @@ -161,21 +169,31 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB public void onResume() { super.onResume(); - final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); - final String unitsKey = this.getResources().getString( + String keyPreference = this.getResources().getString( R.string.weather_preferences_units_key); - final String units = sharedPreferences.getString(unitsKey, ""); + final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); final String celsius = this.getResources().getString( R.string.weather_preferences_units_celsius); - if (units.equals(celsius)) { + if (unitsPreferenceValue.equals(celsius)) { this.isFahrenheit = false; } else { this.isFahrenheit = true; } + keyPreference = this.getResources().getString( + R.string.weather_preferences_language_key); + final String languagePreferenceValue = sharedPreferences.getString( + keyPreference, ""); + if (!languagePreferenceValue.equals(this.language)) { + this.language = languagePreferenceValue; + this.onClickGetWeather(); + + return; + } + WeatherData weatherData = null; try { weatherData = this.restoreDataFromFile(); @@ -259,13 +277,22 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB private WeatherData doInBackgroundThrowable(final Object... params) throws ClientProtocolException, MalformedURLException, URISyntaxException, IOException, JSONException { + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(WeatherInformationDataFragment.this + .getActivity()); + + final String keyPreference = WeatherInformationDataFragment.this + .getActivity().getString( + R.string.weather_preferences_language_key); + final String languagePreferenceValue = sharedPreferences.getString(keyPreference, ""); + final String cityCountry = (String) params[0]; final String urlAPICity = WeatherInformationDataFragment.this.getResources() .getString(R.string.uri_api_city); final String APIVersion = WeatherInformationDataFragment.this.getResources() .getString(R.string.api_version); String url = this.weatherService.createURIAPICityCountry( - cityCountry, urlAPICity, APIVersion); + cityCountry, urlAPICity, APIVersion, languagePreferenceValue); final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java deleted file mode 100644 index 8322661..0000000 --- a/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java +++ /dev/null @@ -1,228 +0,0 @@ -package de.example.exampletdd.fragment; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.apache.http.client.ClientProtocolException; -import org.json.JSONException; - -import android.app.Fragment; -import android.content.SharedPreferences; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; -import android.os.AsyncTask; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.EditText; -import android.widget.ImageView; -import de.example.exampletdd.R; -import de.example.exampletdd.activityinterface.ErrorMessage; -import de.example.exampletdd.activityinterface.OnClickButtons; -import de.example.exampletdd.httpclient.WeatherHTTPClient; -import de.example.exampletdd.model.WeatherData; -import de.example.exampletdd.parser.IJPOSWeatherParser; -import de.example.exampletdd.parser.JPOSWeatherParser; -import de.example.exampletdd.service.WeatherService; - -public class WeatherInformationDataFragmentDeprecated extends Fragment implements OnClickButtons { - private boolean isFahrenheit; - private EditText weatherDescription; - private EditText temperature; - private EditText maxTemperature; - private EditText minTemperature; - private EditText sunRise; - private EditText sunSet; - private ImageView imageIcon; - - - @Override - public View onCreateView(final LayoutInflater inflater, - final ViewGroup container, final Bundle savedInstanceState) { - final View rootView = inflater.inflate(R.layout.fragment_main, - container, false); - - - this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); - this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); - this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); - this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); - this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); - this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); - this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); - - return rootView; - } - - @Override - public void onClickGetWeather() { - - final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); - final WeatherService weatherService = new WeatherService( - JPOSWeatherParser); - final AndroidHttpClient httpClient = AndroidHttpClient - .newInstance("Android Weather Information Agent"); - final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient( - httpClient); - - final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService); - - final EditText cityCountry = (EditText) this.getActivity() - .findViewById(R.id.editTextCity); - - weatherTask.execute(cityCountry.getText().toString()); - } - - public void updateWeatherData(final WeatherData weatherData) { - final DecimalFormat tempFormatter = new DecimalFormat("#####.#####"); - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); - final double tempUnits = this.isFahrenheit ? 0 : 273.15; - - if (weatherData.getWeather() != null) { - this.weatherDescription.setText(weatherData.getWeather() - .getDescription()); - double conversion = weatherData.getMain().getTemp(); - conversion = conversion - tempUnits; - this.temperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMaxTemp(); - conversion = conversion - tempUnits; - this.maxTemperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMinTemp(); - conversion = conversion - tempUnits; - this.minTemperature.setText(tempFormatter.format(conversion)); - } - - if (weatherData.getSystem() != null) { - long unixTime = weatherData.getSystem().getSunRiseTime(); - Date unixDate = new Date(unixTime * 1000L); - String dateFormatUnix = dateFormat.format(unixDate); - this.sunRise.setText(dateFormatUnix); - - unixTime = weatherData.getSystem().getSunSetTime(); - unixDate = new Date(unixTime * 1000L); - dateFormatUnix = dateFormat.format(unixDate); - this.sunSet.setText(dateFormatUnix); - } - - if (weatherData.getIconData() != null) { - final Bitmap icon = BitmapFactory.decodeByteArray( - weatherData.getIconData(), 0, - weatherData.getIconData().length); - this.imageIcon.setImageBitmap(icon); - } - } - - @Override - public void onResume() { - super.onResume(); - - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity()); - - final String unitsKey = this.getResources().getString( - R.string.weather_preferences_units_key); - final String units = sharedPreferences.getString(unitsKey, ""); - final String celsius = this.getResources().getString( - R.string.weather_preferences_units_celsius); - if (units.equals(celsius)) { - this.isFahrenheit = false; - } else { - this.isFahrenheit = true; - } - } - - public class WeatherTask extends AsyncTask { - private static final String TAG = "JSONWeatherTask"; - private final WeatherHTTPClient weatherHTTPClient; - private final WeatherService weatherService; - - public WeatherTask(final WeatherHTTPClient weatherHTTPClient, - final WeatherService weatherService) { - this.weatherHTTPClient = weatherHTTPClient; - this.weatherService = weatherService; - } - - @Override - protected WeatherData doInBackground(final Object... params) { - WeatherData weatherData = null; - - try { - weatherData = this.doInBackgroundThrowable(params); - } catch (final ClientProtocolException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final MalformedURLException e) { - Log.e(TAG, "Syntax URL exception: ", e); - } catch (final URISyntaxException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final JSONException e) { - Log.e(TAG, "WeatherService exception: ", e); - } finally { - this.weatherHTTPClient.close(); - } - - return weatherData; - } - - @Override - protected void onPostExecute(final WeatherData weatherData) { - if (weatherData != null) { - WeatherInformationDataFragmentDeprecated.this.updateWeatherData(weatherData); - } else { - ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity()) - .createErrorDialog(R.string.error_dialog_generic_error); - } - - this.weatherHTTPClient.close(); - } - - @Override - protected void onCancelled(final WeatherData weatherData) { - this.onCancelled(); - ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity()) - .createErrorDialog(R.string.error_dialog_connection_tiemout); - - this.weatherHTTPClient.close(); - } - - private WeatherData doInBackgroundThrowable(final Object... params) - throws ClientProtocolException, MalformedURLException, - URISyntaxException, IOException, JSONException { - final String cityCountry = (String) params[0]; - final String urlAPICity = WeatherInformationDataFragmentDeprecated.this.getResources() - .getString(R.string.uri_api_city); - final String APIVersion = WeatherInformationDataFragmentDeprecated.this.getResources() - .getString(R.string.api_version); - String url = this.weatherService.createURIAPICityCountry( - cityCountry, urlAPICity, APIVersion); - - - final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); - - - final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData); - - - final String icon = weatherData.getWeather().getIcon(); - final String urlAPIicon = WeatherInformationDataFragmentDeprecated.this - .getResources().getString(R.string.uri_api_icon); - url = this.weatherService.createURIAPIicon(icon, urlAPIicon); - final byte[] iconData = this.weatherHTTPClient - .retrieveDataFromAPI(new URL(url)).toByteArray(); - weatherData.setIconData(iconData); - - - return weatherData; - } - } -} diff --git a/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java index d48747c..5e43c0c 100644 --- a/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java @@ -17,13 +17,17 @@ implements OnSharedPreferenceChangeListener { // Load the preferences from an XML resource this.addPreferencesFromResource(R.xml.weather_preferences); - final String unitsKey = this.getResources().getString( + String keyPreference = this.getActivity().getString( R.string.weather_preferences_units_key); - final Preference connectionPref = this.findPreference(unitsKey); - this.getPreferenceManager().getSharedPreferences() - .getString(unitsKey, ""); + Preference connectionPref = this.findPreference(keyPreference); + connectionPref.setSummary(this.getPreferenceManager() + .getSharedPreferences().getString(keyPreference, "")); + + keyPreference = this.getActivity().getString( + R.string.weather_preferences_language_key); + connectionPref = this.findPreference(keyPreference); connectionPref.setSummary(this.getPreferenceManager() - .getSharedPreferences().getString(unitsKey, "")); + .getSharedPreferences().getString(keyPreference, "")); } @Override @@ -44,10 +48,17 @@ implements OnSharedPreferenceChangeListener { @Override public void onSharedPreferenceChanged( final SharedPreferences sharedPreferences, final String key) { - final String unitsKey = this.getResources().getString( + String keyValue = this.getActivity().getString( R.string.weather_preferences_units_key); - if (key.equals(unitsKey)) { + if (key.equals(keyValue)) { + final Preference connectionPref = this.findPreference(key); + connectionPref.setSummary(sharedPreferences.getString(key, "")); + } + + keyValue = this.getActivity().getString( + R.string.weather_preferences_language_key); + if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); connectionPref.setSummary(sharedPreferences.getString(key, "")); } diff --git a/src/de/example/exampletdd/service/WeatherService.java b/src/de/example/exampletdd/service/WeatherService.java index ed393ca..356288b 100644 --- a/src/de/example/exampletdd/service/WeatherService.java +++ b/src/de/example/exampletdd/service/WeatherService.java @@ -33,12 +33,13 @@ public class WeatherService { } public String createURIAPICityCountry(final String cityCountry, - final String urlAPI, final String APIVersion) { + final String urlAPI, final String APIVersion, final String units) { final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.ENGLISH); - final Object[] values = new Object[2]; + final Object[] values = new Object[3]; values[0] = APIVersion; values[1] = cityCountry; + values[2] = units; return formatURIAPI.format(values); } -- 2.1.4