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">
</item>
<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>
+ <string name="city_not_found">city not found</string>
+ <string name="country_not_found">country not found</string>
+ <string name="weather_progress_getdata">Downloading remote data</string>
</resources>
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;
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);
}
}
@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);
}
}
}
-
- 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();
- }
- }
- }
}
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);
}
--- /dev/null
+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;
+ }
+}
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;
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
@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;
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, "");
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();
} 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<Object, Void, WeatherData> {
+ public class WeatherTask extends AsyncTask<Object, Integer, WeatherData> {
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
} 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 {
@Override
protected void onPostExecute(final WeatherData weatherData) {
+ this.newFragment.dismiss();
+
if (weatherData != null) {
try {
this.onPostExecuteThrowable(weatherData);
this.weatherHTTPClient.close();
}
+ @Override
+ protected void onProgressUpdate(final Integer... progress) {
+ }
+
private WeatherData doInBackgroundThrowable(final Object... params)
throws ClientProtocolException, MalformedURLException,
URISyntaxException, IOException, JSONException {