No time for coments
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 4 Apr 2014 02:54:46 +0000 (04:54 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 4 Apr 2014 02:55:31 +0000 (04:55 +0200)
res/values/arrays.xml
res/values/strings.xml
res/xml/weather_preferences.xml
src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java
src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java [deleted file]
src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java
src/de/example/exampletdd/service/WeatherService.java

index e38881b..b9aedcf 100644 (file)
@@ -8,4 +8,54 @@
         <item>Celsius</item>
         <item>Fahrenheit</item>
     </string-array>
+    <string-array name="weather_preferences_language_value">
+        <item>ar</item>
+        <item>bg</item>
+        <item>zh_cn</item>
+        <item>zh_tw</item>
+        <item>cz</item>
+        <item>nl</item>
+        <item>en</item>
+        <item>fi</item>
+        <item>fr</item>
+        <item>gl</item>
+        <item>de</item>
+        <item>it</item>
+        <item>mk</item>
+        <item>pl</item>
+        <item>pt</item>
+        <item>ro</item>
+        <item>ru</item>
+        <item>sk</item>
+        <item>sp</item>
+        <item>se</item>
+        <item>tr</item>
+        <item>ua</item>
+        <item>vi</item>
+    </string-array>
+    <string-array name="weather_preferences_language_human_value">
+        <item>Arabic</item>
+        <item>Bulgarian</item>
+        <item>Chinese Simplified</item>
+        <item>Chinese Traditional</item>
+        <item>Czech</item>
+        <item>Dutch</item>
+        <item>English</item>
+        <item>Finnish</item>
+        <item>French</item>
+        <item>Galician</item>
+        <item>German</item>
+        <item>Italian</item>
+        <item>Macedonian</item>
+        <item>Polish</item>
+        <item>Portuguese</item>
+        <item>Romanian</item>
+        <item>Russian</item>
+        <item>Slovak</item>
+        <item>Spanish</item>
+        <item>Swedish</item>
+        <item>Turkish</item>
+        <item>Ukrainian</item>
+        <item>Vietnamese</item>
+    </string-array>
 </resources>
index 45d1148..8a39ec6 100644 (file)
@@ -7,7 +7,7 @@
     <string name="action_get_weather">Get weather</string>
     <string name="button_weather">Get weather</string>
     <string name="uri_api_coord">http://api.openweathermap.org/data/{0}/weather?lat={1}&amp;lon={2}</string>
-    <string name="uri_api_city">http://api.openweathermap.org/data/{0}/weather?q={1}</string>
+    <string name="uri_api_city">http://api.openweathermap.org/data/{0}/weather?q={1}&amp;lang={2}</string>
     <string name="uri_api_icon">http://openweathermap.org/img/w/{0}.png</string>
     <string name="api_version">2.5</string>
     <string name="text_default_city">London,uk</string>
@@ -33,6 +33,8 @@
     <string name="weather_preferences_units_key">weather_preferences_units</string>
     <string name="weather_preferences_units_fahrenheit">Fahrenheit</string>
     <string name="weather_preferences_units_celsius">Celsius</string>
-    <string name="weather_preferences_units">Units of measurement</string>
+    <string name="weather_preferences_units">Unit of measurement for temperature</string>
+    <string name="weather_preferences_language_key">weather_preferences_language</string>
+    <string name="weather_preferences_language">Language</string>
 
 </resources>
index 3e2cbe0..4d5e35c 100644 (file)
@@ -9,5 +9,14 @@
         android:selectable="true"
         android:persistent="true"
         android:defaultValue="Celsius" />
+    <ListPreference
+        android:entries="@array/weather_preferences_language_human_value"
+        android:entryValues="@array/weather_preferences_language_value"
+        android:key="@string/weather_preferences_language_key"
+        android:title="@string/weather_preferences_language"
+        android:defaultValue="en"
+        android:persistent="true"
+        android:selectable="true"
+        android:summary="English"/>
 
 </PreferenceScreen>
index e955e93..32eb712 100644 (file)
@@ -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 (file)
index 8322661..0000000
+++ /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<Object, Void, WeatherData> {
-        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;
-        }
-    }
-}
index d48747c..5e43c0c 100644 (file)
@@ -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, ""));
         }
index ed393ca..356288b 100644 (file)
@@ -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);
     }