From: gu.martinm@gmail.com Date: Tue, 8 Apr 2014 07:36:50 +0000 (+0200) Subject: No time for comments X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=8d8bf403bf06a3679f13387e37b37b853345250b;p=JavaForFun No time for comments --- diff --git a/Android/WeatherInformation/res/menu/weather_main_menu.xml b/Android/WeatherInformation/res/menu/weather_main_menu.xml index 3b6359d..33a8e65 100644 --- a/Android/WeatherInformation/res/menu/weather_main_menu.xml +++ b/Android/WeatherInformation/res/menu/weather_main_menu.xml @@ -33,7 +33,9 @@ android:visible="true" android:checkable="false" android:enabled="true" - android:checked="false"> + android:checked="false" + android:title="@string/action_map" + android:titleCondensed="@string/action_map"> diff --git a/Android/WeatherInformation/res/values/strings.xml b/Android/WeatherInformation/res/values/strings.xml index f0475fe..ce95f6e 100644 --- a/Android/WeatherInformation/res/values/strings.xml +++ b/Android/WeatherInformation/res/values/strings.xml @@ -39,5 +39,8 @@ Unit of measurement for temperature weather_preferences_language Language + city not found + country not found + Downloading remote data diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationActivity.java index ccfae03..8c19117 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationActivity.java @@ -21,10 +21,8 @@ import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.WeatherInformationDataFragment; import de.example.exampletdd.model.GeocodingData; -import de.example.exampletdd.model.WeatherData; public class WeatherInformationActivity extends Activity implements ErrorMessage { - private static final String WEATHER_DATA_FILE = "weatherdata.file"; private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; private static final String TAG = "WeatherInformationActivity"; private GetWeather mGetWeather; @@ -113,9 +111,9 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage Log.e(TAG, "onCreate exception: ", e); } if (geocodingData != null) { - final String city = (geocodingData.getCity() == null) ? "city not found" + final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? "country not found" + final String country = (geocodingData.getCountry() == null) ? this.getString(R.string.country_not_found) : geocodingData.getCountry(); actionBar.setTitle(city + "," + country); } @@ -123,56 +121,6 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage } @Override - public void onRestoreInstanceState(final Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - - final ActionBar actionBar = this.getActionBar(); - - GeocodingData geocodingData = null; - try { - geocodingData = this.restoreGeocodingDataFromFile(); - } catch (final StreamCorruptedException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final FileNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final ClassNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } - if (geocodingData != null) { - final String city = (geocodingData.getCity() == null) ? "city not found" - : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? "country not found" - : geocodingData.getCountry(); - actionBar.setTitle(city + "," + country); - } - - WeatherData weatherData = null; - try { - weatherData = this.restoreWeatherDataFromFile(); - } catch (final StreamCorruptedException e) { - Log.e(TAG, "onResume exception: ", e); - } catch (final FileNotFoundException e) { - Log.e(TAG, "onResume exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "onResume exception: ", e); - } catch (final ClassNotFoundException e) { - Log.e(TAG, "onResume exception: ", e); - } - - if (weatherData != null) { - this.mGetWeather.updateWeatherData(weatherData); - } - } - - @Override - public void onSaveInstanceState(final Bundle savedInstanceState) { - - super.onSaveInstanceState(savedInstanceState); - } - - @Override public void createErrorDialog(final int title) { final DialogFragment newFragment = ErrorDialogFragment .newInstance(title); @@ -200,21 +148,4 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage } } } - - private WeatherData restoreWeatherDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = this.openFileInput(WEATHER_DATA_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (WeatherData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java index 348a236..8d7e8bb 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java @@ -87,9 +87,9 @@ public class WeatherInformationMapActivity extends Activity { final TextView cityCountry = (TextView) WeatherInformationMapActivity.this .findViewById(R.id.weather_map_citycountry_data); - final String city = (geocodingData.getCity() == null) ? "city not found" + final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? "country not found" + final String country = (geocodingData.getCountry() == null) ? this.getString(R.string.country_not_found) : geocodingData.getCountry(); cityCountry.setText(city + "," + country); } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/ProgressDialogFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/ProgressDialogFragment.java new file mode 100644 index 0000000..9251c26 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/ProgressDialogFragment.java @@ -0,0 +1,53 @@ +package de.example.exampletdd.fragment; + +import android.app.Dialog; +import android.app.DialogFragment; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.view.KeyEvent; + +public class ProgressDialogFragment extends DialogFragment { + + public static ProgressDialogFragment newInstance(final int title) { + return newInstance(title, null); + } + + public static ProgressDialogFragment newInstance(final int title, + final String message) { + final ProgressDialogFragment frag = new ProgressDialogFragment(); + final Bundle args = new Bundle(); + + args.putInt("title", title); + args.putString("message", message); + frag.setArguments(args); + return frag; + } + + @Override + public Dialog onCreateDialog(final Bundle savedInstanceState) { + final int title = this.getArguments().getInt("title"); + final String message = this.getArguments().getString("message"); + + final ProgressDialog dialog = new ProgressDialog(this.getActivity()); + dialog.setIcon(android.R.drawable.ic_dialog_info); + if (title != 0) { + dialog.setTitle(title); + } + if (message != null) { + dialog.setMessage(message); + } + dialog.setCancelable(false); + dialog.setIndeterminate(true); + dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { + + @Override + public final boolean onKey(final DialogInterface dialog, + final int keyCode, final KeyEvent event) { + return false; + } + }); + + return dialog; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java index adf14fe..daefa98 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -22,6 +22,7 @@ import java.util.Locale; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; +import android.app.DialogFragment; import android.app.Fragment; import android.content.Context; import android.content.SharedPreferences; @@ -91,6 +92,43 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath adapter.addAll(entries); listWeatherView.setAdapter(adapter); + + if (savedInstanceState != null) { + // Restore state + final WeatherData weatherData = (WeatherData) savedInstanceState + .getSerializable("weatherData"); + try { + this.storeWeatherDataToFile(weatherData); + } catch (final IOException e) { + ((ErrorMessage) WeatherInformationDataFragment.this + .getActivity()) + .createErrorDialog(R.string.error_dialog_generic_error); + } + } + } + + @Override + public void onSaveInstanceState(final Bundle savedInstanceState) { + + // Save state + WeatherData weatherData = null; + try { + weatherData = this.restoreWeatherDataFromFile(); + } catch (final StreamCorruptedException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final FileNotFoundException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final ClassNotFoundException e) { + Log.e(TAG, "onResume exception: ", e); + } + + if (weatherData != null) { + savedInstanceState.putSerializable("weatherData", weatherData); + } + + super.onSaveInstanceState(savedInstanceState); } @Override @@ -127,9 +165,9 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath @Override public void updateWeatherData(final WeatherData weatherData) { - final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); + final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); tempFormatter.applyPattern("#####.#####"); - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.US); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.getDefault()); final double tempUnits = this.mIsFahrenheit ? 0 : 273.15; @@ -194,6 +232,7 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); + // 1. Update units of measurement. String keyPreference = this.getResources().getString( R.string.weather_preferences_units_key); final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); @@ -205,17 +244,8 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath this.mIsFahrenheit = true; } - keyPreference = this.getResources().getString( - R.string.weather_preferences_language_key); - final String languagePreferenceValue = sharedPreferences.getString( - keyPreference, ""); - if (!languagePreferenceValue.equals(this.mLanguage)) { - this.mLanguage = languagePreferenceValue; - this.getWeather(); - - return; - } + // 2. Update current data on display. WeatherData weatherData = null; try { weatherData = this.restoreWeatherDataFromFile(); @@ -228,21 +258,41 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath } catch (final ClassNotFoundException e) { Log.e(TAG, "onResume exception: ", e); } - if (weatherData != null) { this.updateWeatherData(weatherData); } + + + // 3. If language changed, try to retrieve new data for new language + // (new strings with the chosen language) + keyPreference = this.getResources().getString( + R.string.weather_preferences_language_key); + final String languagePreferenceValue = sharedPreferences.getString( + keyPreference, ""); + if (!languagePreferenceValue.equals(this.mLanguage)) { + this.mLanguage = languagePreferenceValue; + this.getWeather(); + } } - public class WeatherTask extends AsyncTask { + public class WeatherTask extends AsyncTask { private static final String TAG = "WeatherTask"; private final WeatherHTTPClient weatherHTTPClient; private final WeatherService weatherService; + private final DialogFragment newFragment; public WeatherTask(final WeatherHTTPClient weatherHTTPClient, final WeatherService weatherService) { this.weatherHTTPClient = weatherHTTPClient; this.weatherService = weatherService; + this.newFragment = ProgressDialogFragment + .newInstance(R.string.weather_progress_getdata); + } + + @Override + protected void onPreExecute() { + this.newFragment.show(WeatherInformationDataFragment.this.getActivity() + .getFragmentManager(), "errorDialog"); } @Override @@ -258,7 +308,8 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath } catch (final URISyntaxException e) { Log.e(TAG, "doInBackground exception: ", e); } catch (final IOException e) { - Log.e(TAG, "doInBackground exception: ", e); + // logger infrastructure swallows UnknownHostException :/ + Log.e(TAG, "doInBackground exception: " + e.getMessage(), e); } catch (final JSONException e) { Log.e(TAG, "doInBackground exception: ", e); } finally { @@ -270,6 +321,8 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath @Override protected void onPostExecute(final WeatherData weatherData) { + this.newFragment.dismiss(); + if (weatherData != null) { try { this.onPostExecuteThrowable(weatherData); @@ -296,6 +349,10 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath this.weatherHTTPClient.close(); } + @Override + protected void onProgressUpdate(final Integer... progress) { + } + private WeatherData doInBackgroundThrowable(final Object... params) throws ClientProtocolException, MalformedURLException, URISyntaxException, IOException, JSONException {