From: gu.martinm@gmail.com Date: Sat, 30 Aug 2014 20:07:03 +0000 (+0200) Subject: WeatherInformation Android: X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=da58e4fbcb065227e47aa0e48b445846c107b2b2;p=JavaForFun WeatherInformation Android: Overview and specif fragments, new implementation. --- diff --git a/Android/WeatherInformation/AndroidManifest.xml b/Android/WeatherInformation/AndroidManifest.xml index d07af2d..856b325 100644 --- a/Android/WeatherInformation/AndroidManifest.xml +++ b/Android/WeatherInformation/AndroidManifest.xml @@ -76,7 +76,7 @@ diff --git a/Android/WeatherInformation/res/layout-large/weather_main.xml b/Android/WeatherInformation/res/layout-large/weather_main.xml index 78e854d..95e773e 100644 --- a/Android/WeatherInformation/res/layout-large/weather_main.xml +++ b/Android/WeatherInformation/res/layout-large/weather_main.xml @@ -14,9 +14,9 @@ class="de.example.exampletdd.fragment.overview.OverviewFragment" /> + class="de.example.exampletdd.fragment.specific.SpecificFragment" /> \ No newline at end of file diff --git a/Android/WeatherInformation/res/layout/weather_current_data.xml b/Android/WeatherInformation/res/layout/weather_current_data.xml index 3f490ce..4e3b752 100644 --- a/Android/WeatherInformation/res/layout/weather_current_data.xml +++ b/Android/WeatherInformation/res/layout/weather_current_data.xml @@ -13,6 +13,6 @@ android:id="@+id/weather_current_data_fragment" android:layout_width="match_parent" android:layout_height="match_parent" - class="de.example.exampletdd.fragment.current.CurrentDataFragment" /> + class="de.example.exampletdd.fragment.current.CurrentFragment" /> \ No newline at end of file diff --git a/Android/WeatherInformation/res/layout/weather_specific_data.xml b/Android/WeatherInformation/res/layout/weather_specific_data.xml index 2e0d4fe..0594573 100644 --- a/Android/WeatherInformation/res/layout/weather_specific_data.xml +++ b/Android/WeatherInformation/res/layout/weather_specific_data.xml @@ -6,14 +6,14 @@ android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" - tools:context="de.example.exampletdd.WeatherInformationSpecificDataActivity" + tools:context="de.example.exampletdd.SpecificActivity" tools:ignore="MergeRootFrame" > + class="de.example.exampletdd.fragment.specific.SpecificFragment" /> \ No newline at end of file diff --git a/Android/WeatherInformation/src/de/example/exampletdd/SpecificActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/SpecificActivity.java new file mode 100644 index 0000000..d0e131a --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/SpecificActivity.java @@ -0,0 +1,40 @@ +package de.example.exampletdd; + +import android.app.ActionBar; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import de.example.exampletdd.model.GeocodingData; + +public class SpecificActivity extends FragmentActivity { + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.setContentView(R.layout.weather_specific_data); + + final ActionBar actionBar = this.getActionBar(); + + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); + actionBar.setDisplayHomeAsUpEnabled(true); + + } + + @Override + public void onResume() { + super.onResume(); + + final ActionBar actionBar = this.getActionBar(); + + // TODO: retrive data from data base (like I do on WindowsPhone 8) + // 1. Update title. + final GeocodingData geocodingData = new GeocodingData.Builder().build(); + if (geocodingData != null) { + final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) + : geocodingData.getCity(); + final String country = (geocodingData.getCountry() == null) ? this.getString(R.string.country_not_found) + : geocodingData.getCountry(); + actionBar.setTitle(city + "," + country); + } + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationApplication.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationApplication.java index 972899e..7067c3b 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationApplication.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationApplication.java @@ -2,10 +2,12 @@ package de.example.exampletdd; import android.app.Application; import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.model.currentweather.Current; import de.example.exampletdd.model.forecastweather.Forecast; public class WeatherInformationApplication extends Application { private Forecast mForecast; + private Current mCurrent; private GeocodingData mGeocodingData; protected void setGeocodingData(final GeocodingData geocodingData) { @@ -23,4 +25,12 @@ public class WeatherInformationApplication extends Application { public Forecast getForecast() { return this.mForecast; } + + public void setCurrent(final Current current) { + this.mCurrent = current; + } + + public Current getCurrent() { + return this.mCurrent; + } } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java index 73d764b..22591d4 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java @@ -87,22 +87,22 @@ public class WeatherInformationBatch extends IntentService { // 1. Today String urlAPI = WeatherInformationBatch.this.getResources().getString( R.string.uri_api_weather_today); - String url = weatherService.createURIAPITodayWeather(urlAPI, APIVersion, + String url = weatherService.createURIAPICurrent(urlAPI, APIVersion, geocodingData.getLatitude(), geocodingData.getLongitude()); String jsonData = weatherHTTPClient.retrieveDataAsString(new URL(url)); final Current currentWeatherData = weatherService - .retrieveCurrentWeatherDataFromJPOS(jsonData); + .retrieveCurrentFromJPOS(jsonData); final Calendar now = Calendar.getInstance(); currentWeatherData.setDate(now.getTime()); // 2. Forecast urlAPI = WeatherInformationBatch.this.getResources().getString( R.string.uri_api_weather_forecast); - url = weatherService.createURIAPIForecastWeather(urlAPI, APIVersion, + url = weatherService.createURIAPIForecast(urlAPI, APIVersion, geocodingData.getLatitude(), geocodingData.getLongitude(), resultsNumber); jsonData = weatherHTTPClient.retrieveDataAsString(new URL(url)); final Forecast forecastWeatherData = weatherService - .retrieveForecastWeatherDataFromJPOS(jsonData); + .retrieveForecastFromJPOS(jsonData); return new WeatherData(forecastWeatherData, currentWeatherData); } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java deleted file mode 100644 index 425ea59..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.example.exampletdd; - -import android.app.ActionBar; -import android.os.Bundle; -import android.support.v4.app.FragmentActivity; -import de.example.exampletdd.model.GeocodingData; -import de.example.exampletdd.service.ServicePersistenceStorage; - -public class WeatherInformationSpecificDataActivity extends FragmentActivity { - - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - this.setContentView(R.layout.weather_specific_data); - - final ActionBar actionBar = this.getActionBar(); - - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); - actionBar.setDisplayHomeAsUpEnabled(true); - - } - - @Override - public void onResume() { - super.onResume(); - - final ActionBar actionBar = this.getActionBar(); - - final ServicePersistenceStorage weatherServicePersistenceFile = new ServicePersistenceStorage(this); - final GeocodingData geocodingData = weatherServicePersistenceFile.getGeocodingData(); - - if (geocodingData != null) { - final String city = (geocodingData.getCity() == null) ? this - .getString(R.string.city_not_found) : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? this - .getString(R.string.country_not_found) : geocodingData.getCountry(); - actionBar.setTitle(city + "," + country); - - } - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java index df7cd0f..3a4152d 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java @@ -15,24 +15,21 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; -import de.example.exampletdd.fragment.current.CurrentDataFragment; +import de.example.exampletdd.fragment.current.CurrentFragment; import de.example.exampletdd.fragment.overview.OverviewFragment; import de.example.exampletdd.model.GeocodingData; -import de.example.exampletdd.service.ServicePersistenceStorage; public class WeatherTabsActivity extends FragmentActivity { private static final int NUM_ITEMS = 2; - private MyAdapter mAdapter; + private TabsAdapter mAdapter; private ViewPager mPager; - private ServicePersistenceStorage mWeatherServicePersistenceFile; - + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.fragment_pager); - this.mWeatherServicePersistenceFile = new ServicePersistenceStorage(this); - this.mAdapter = new MyAdapter(this.getSupportFragmentManager()); + this.mAdapter = new TabsAdapter(this.getSupportFragmentManager()); this.mPager = (ViewPager)this.findViewById(R.id.pager); this.mPager.setAdapter(this.mAdapter); @@ -79,36 +76,6 @@ public class WeatherTabsActivity extends FragmentActivity { actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener)); actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener)); - - // this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this); - // if (savedInstanceState != null) { - // this.mGeocodingData = (GeocodingData) savedInstanceState - // .getSerializable("GEOCODINGDATA"); - // final GeocodingData lastValue = this.mWeatherServicePersistenceFile.getGeocodingData(); - // } else { - // this.mGeocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); - // final Intent intent = new Intent(this, WeatherInformationBatch.class); - // if (this.mGeocodingData != null) { - // this.startService(intent); - // } - // } - - - - // final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - // final String keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key); - // final String updateTimeRate = sharedPreferences.getString(keyPreference, ""); - // final int timeRate = Integer.valueOf(updateTimeRate); - // Log.i(TAG, "WeatherTabsActivity onCreate, timeRate: " + timeRate); - - // final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); - // TODO: better use some string instead of .class? In case I change the service class - // this could be a problem (I guess) - // final Intent intent = new Intent(this, WeatherInformationBatch.class); - // final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, - // PendingIntent.FLAG_UPDATE_CURRENT); - // alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() - // + (timeRate * 1000), (timeRate * 1000), alarmIntent); } @Override @@ -143,18 +110,12 @@ public class WeatherTabsActivity extends FragmentActivity { } else { } + // TODO: calling again super method? return super.onOptionsItemSelected(item); } @Override protected void onRestoreInstanceState(final Bundle savedInstanceState) { - if (savedInstanceState != null) { - final WeatherInformationApplication application = (WeatherInformationApplication) this - .getApplication(); - application.setGeocodingData((GeocodingData) savedInstanceState - .getSerializable("GEOCODINGDATA")); - } - super.onRestoreInstanceState(savedInstanceState); } @@ -165,18 +126,18 @@ public class WeatherTabsActivity extends FragmentActivity { final ActionBar actionBar = this.getActionBar(); - final ServicePersistenceStorage weatherServicePersistenceFile = new ServicePersistenceStorage(this); - final GeocodingData geocodingData = weatherServicePersistenceFile.getGeocodingData(); - + // TODO: retrive data from data base (like I do on WindowsPhone 8) + // 1. Update title. + final GeocodingData geocodingData = new GeocodingData.Builder().build(); if (geocodingData != null) { - final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) + final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) : geocodingData.getCity(); final String country = (geocodingData.getCountry() == null) ? this.getString(R.string.country_not_found) : geocodingData.getCountry(); - actionBar.setTitle(city + "," + country); + actionBar.setTitle(city + "," + country); } - + // 2. Update forecast tab text. final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key); @@ -190,50 +151,6 @@ public class WeatherTabsActivity extends FragmentActivity { humanValue = "14 DAY FORECAST"; } actionBar.getTabAt(1).setText(humanValue); - - final WeatherInformationApplication application = (WeatherInformationApplication) this - .getApplication(); - if (application.getGeocodingData() != null) { - // If there is previous value. - final GeocodingData geocodingDataFile = this.mWeatherServicePersistenceFile - .getGeocodingData(); - if (!application.getGeocodingData().equals(geocodingDataFile)) { - // Just update when value changed. - application.setGeocodingData(geocodingDataFile); - final Intent intent = new Intent(this, WeatherInformationBatch.class); - this.startService(intent); - } - } else { - // There is no previous value. - application.setGeocodingData(this.mWeatherServicePersistenceFile.getGeocodingData()); - final Intent intent = new Intent(this, WeatherInformationBatch.class); - this.startService(intent); - } - - // if (geocodingData != null) { - // if ((this.mGeocodingData == null) || (!this.mGeocodingData.equals(geocodingData))) { - // Log.i(TAG, "WeatherTabsActivity onResume, startService"); - // this.mGeocodingData = geocodingData; - // final Intent intent = new Intent(this, WeatherInformationBatch.class); - // this.startService(intent); - // } - // } - // - // keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key); - // final String updateTimeRate = sharedPreferences.getString(keyPreference, ""); - // final int timeRate = Integer.valueOf(updateTimeRate); - // if (this.mUpdateTimeRate != timeRate) { - // Log.i(TAG, "WeatherTabsActivity onResume, updateTimeRate: " + timeRate); - // this.mUpdateTimeRate = timeRate; - // final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); - // // TODO: better use some string instead of .class? In case I change the service class - // // this could be a problem (I guess) - // final Intent intent = new Intent(this, WeatherInformationBatch.class); - // final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, - // PendingIntent.FLAG_UPDATE_CURRENT); - // alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() - // + (timeRate * 1000), (timeRate * 1000), alarmIntent); - // } } @Override @@ -245,8 +162,8 @@ public class WeatherTabsActivity extends FragmentActivity { super.onSaveInstanceState(savedInstanceState); } - private class MyAdapter extends FragmentPagerAdapter { - public MyAdapter(final FragmentManager fm) { + private class TabsAdapter extends FragmentPagerAdapter { + public TabsAdapter(final FragmentManager fm) { super(fm); } @@ -258,8 +175,10 @@ public class WeatherTabsActivity extends FragmentActivity { @Override public Fragment getItem(final int position) { if (position == 0) { - return new CurrentDataFragment(); + // TODO: new instance every time I click on tab? + return new CurrentFragment(); } else { + // TODO: new instance every time I click on tab? final Fragment fragment = new OverviewFragment(); return fragment; } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentAdapter.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentAdapter.java new file mode 100644 index 0000000..2606f42 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentAdapter.java @@ -0,0 +1,204 @@ +package de.example.exampletdd.fragment.current; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import de.example.exampletdd.R; + +public class CurrentAdapter extends ArrayAdapter { + private static final int FIRST = 0; + private static final int SECOND = 1; + private static final int THIRD = 2; + private final int[] resources; + + public CurrentAdapter(final Context context, final int[] resources) { + super(context, 0); + + this.resources = resources; + } + + + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + + final View view = this.getWorkingView(position, convertView); + final int viewType = this.getItemViewType(position); + + if (viewType == FIRST) { + + final ViewFirstHolder viewHolder = this.getViewFirstHolder(view); + final CurrentDataEntryFirst entry = (CurrentDataEntryFirst) this + .getItem(position); + viewHolder.picture.setImageBitmap(entry.getPicture()); + viewHolder.tempMax.setText(entry.getTempMax()); + viewHolder.tempMin.setText(entry.getTempMin()); + } else if (viewType == SECOND) { + final ViewSecondHolder viewHolder = this.getViewSecondHolder(view); + final CurrentDataEntrySecond entry = (CurrentDataEntrySecond) this + .getItem(position); + viewHolder.weatherDescription.setText(entry.getWeatherDescription()); + } else if (viewType == THIRD) { + final ViewThirdHolder viewHolder = this.getViewThirdHolder(view); + final CurrentDataEntryFifth entry = (CurrentDataEntryFifth) this + .getItem(position); + viewHolder.humidityValue.setText(entry.getHumidityValue()); + viewHolder.pressureValue.setText(entry.getPressureValue()); + viewHolder.rainValue.setText(entry.getRainValue()); + viewHolder.cloudsValue.setText(entry.getCloudsValue()); + viewHolder.windValue.setText(entry.getWindValue()); + viewHolder.sunRiseTime.setText(entry.getSunRiseTime()); + viewHolder.sunSetTime.setText(entry.getSunSetTime()); + viewHolder.feelsLike.setText(entry.getFeelsLike()); + viewHolder.snowValue.setText(entry.getSnowValue()); + viewHolder.feelsLikeUnits.setText(entry.getFeelsLikeUnits()); + } + + return view; + } + + @Override + public int getItemViewType(final int position) { + int type = 0; + + if (position == 0) { + type = FIRST; + } else if (position == 1) { + type = SECOND; + } else if (position == 2) { + type = THIRD; + } + + return type; + } + + @Override + public int getViewTypeCount() { + return 3; + } + + private View getWorkingView(final int position, final View convertView) { + View workingView = null; + + if (convertView == null) { + final int viewType = this.getItemViewType(position); + final Context context = this.getContext(); + final LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + workingView = inflater.inflate(this.resources[viewType], null); + } else { + workingView = convertView; + } + + return workingView; + } + + private ViewFirstHolder getViewFirstHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewFirstHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewFirstHolder)) { + viewHolder = new ViewFirstHolder(); + + viewHolder.picture = (ImageView) workingView + .findViewById(R.id.weather_current_data_picture); + viewHolder.tempMax = (TextView) workingView + .findViewById(R.id.weather_current_data_temp_max); + viewHolder.tempMin = (TextView) workingView + .findViewById(R.id.weather_current_data_temp_min); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewFirstHolder) tag; + } + + return viewHolder; + } + + private ViewSecondHolder getViewSecondHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewSecondHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewSecondHolder)) { + viewHolder = new ViewSecondHolder(); + + viewHolder.weatherDescription = (TextView) workingView + .findViewById(R.id.weather_current_data_description); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewSecondHolder) tag; + } + + return viewHolder; + } + + private ViewThirdHolder getViewThirdHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewThirdHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewThirdHolder)) { + viewHolder = new ViewThirdHolder(); + + viewHolder.humidityValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_humidity_value); + viewHolder.pressureValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_pressure_value); + viewHolder.rainValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_rain_value); + viewHolder.cloudsValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_clouds_value); + viewHolder.windValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_wind_value); + viewHolder.cloudsValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_clouds_value); + viewHolder.snowValue = (TextView) workingView + .findViewById(R.id.weather_current_now_data_snow_value); + viewHolder.sunRiseTime = (TextView) workingView + .findViewById(R.id.weather_current_now_data_sunrise_value); + viewHolder.sunSetTime = (TextView) workingView + .findViewById(R.id.weather_current_now_data_sunset_value); + viewHolder.feelsLike = (TextView) workingView + .findViewById(R.id.weather_current_now_data_feelslike_value); + viewHolder.feelsLikeUnits = (TextView) workingView + .findViewById(R.id.weather_current_now_data_feelslike_units); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewThirdHolder) tag; + } + + return viewHolder; + } + + + private static class ViewFirstHolder { + public ImageView picture; + public TextView tempMax; + public TextView tempMin; + } + + private static class ViewSecondHolder { + public TextView weatherDescription; + } + + private static class ViewThirdHolder { + public TextView humidityValue; + public TextView pressureValue; + public TextView windValue; + public TextView rainValue; + public TextView cloudsValue; + public TextView snowValue; + public TextView sunRiseTime; + public TextView sunSetTime; + public TextView feelsLike; + public TextView feelsLikeUnits; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFifth.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFifth.java new file mode 100644 index 0000000..dc1d50f --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFifth.java @@ -0,0 +1,71 @@ +package de.example.exampletdd.fragment.current; + +public class CurrentDataEntryFifth { + private final String sunRiseTime; + private final String sunSetTime; + private final String humidityValue; + private final String pressureValue; + private final String windValue; + private final String rainValue; + private final String cloudsValue; + private final String feelsLike; + private final String feelsLikeUnits; + private final String snowValue; + + public CurrentDataEntryFifth(final String sunRiseTime, final String sunSetTime, + final String humidityValue, final String pressureValue, final String windValue, + final String rainValue, final String feelsLike, final String feelsLikeUnits, + final String snowValue, + final String cloudsValue) { + this.sunRiseTime = sunRiseTime; + this.sunSetTime = sunSetTime; + this.humidityValue = humidityValue; + this.pressureValue = pressureValue; + this.windValue = windValue; + this.rainValue = rainValue; + this.feelsLike = feelsLike; + this.feelsLikeUnits = feelsLikeUnits; + this.snowValue = snowValue; + this.cloudsValue = cloudsValue; + } + + public String getSunRiseTime() { + return this.sunRiseTime; + } + + public String getSunSetTime() { + return this.sunSetTime; + } + + public String getFeelsLike() { + return this.feelsLike; + } + + public String getFeelsLikeUnits() { + return this.feelsLikeUnits; + } + + public String getHumidityValue() { + return this.humidityValue; + } + + public String getPressureValue() { + return this.pressureValue; + } + + public String getWindValue() { + return this.windValue; + } + + public String getRainValue() { + return this.rainValue; + } + + public String getCloudsValue() { + return this.cloudsValue; + } + + public String getSnowValue() { + return this.snowValue; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFirst.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFirst.java new file mode 100644 index 0000000..21d6aba --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntryFirst.java @@ -0,0 +1,28 @@ +package de.example.exampletdd.fragment.current; + +import android.graphics.Bitmap; + +public class CurrentDataEntryFirst { + private final Bitmap picture; + private final String tempMax; + private final String tempMin; + + public CurrentDataEntryFirst(final String tempMax, final String tempMin, + final Bitmap picture) { + this.tempMax = tempMax; + this.tempMin = tempMin; + this.picture = picture; + } + + public Bitmap getPicture() { + return this.picture; + } + + public String getTempMax() { + return this.tempMax; + } + + public String getTempMin() { + return this.tempMin; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntrySecond.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntrySecond.java new file mode 100644 index 0000000..8b89ac4 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataEntrySecond.java @@ -0,0 +1,14 @@ +package de.example.exampletdd.fragment.current; + +public class CurrentDataEntrySecond { + private final String weatherDescription; + + public CurrentDataEntrySecond(final String weatherDescription) { + this.weatherDescription = weatherDescription; + } + + public String getWeatherDescription() { + return this.weatherDescription; + } + +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataFragment.java deleted file mode 100644 index 8d51822..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentDataFragment.java +++ /dev/null @@ -1,258 +0,0 @@ -package de.example.exampletdd.fragment.current; - -import java.io.IOException; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -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.DialogFragment; -import android.support.v4.app.ListFragment; -import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; -import android.widget.ListView; -import de.example.exampletdd.R; -import de.example.exampletdd.fragment.ErrorDialogFragment; -import de.example.exampletdd.fragment.overview.IconsList; -import de.example.exampletdd.model.currentweather.Current; -import de.example.exampletdd.service.ServicePersistenceStorage; - -public class CurrentDataFragment extends ListFragment { - private static final String TAG = "WeatherInformationCurrentDataFragment"; - private boolean mIsFahrenheit; - private ServicePersistenceStorage mWeatherServicePersistenceFile; - private BroadcastReceiver mReceiver; - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - this.mWeatherServicePersistenceFile = new ServicePersistenceStorage(this.getActivity()); - - this.mReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(final Context context, final Intent intent) { - // This method will be run in the main thread. - final String action = intent.getAction(); - if (action.equals("de.example.exampletdd.UPDATECURRENTWEATHER")) { - Log.i(TAG, "WeatherInformationCurrentDataFragment Update Weather Data"); - final Current currentWeatherData = - CurrentDataFragment.this.mWeatherServicePersistenceFile - .getCurrentWeatherData(); - if (currentWeatherData != null) { - CurrentDataFragment.this - .updateCurrentWeatherData(currentWeatherData); - } - - } - } - }; - } - - @Override - public void onActivityCreated(final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - final ListView listWeatherView = this.getListView(); - listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE); - - if (savedInstanceState != null) { - // Restore state - final Current currentWeatherData = (Current) savedInstanceState - .getSerializable("CurrentWeatherData"); - - if (currentWeatherData != null) { - try { - this.mWeatherServicePersistenceFile.storeCurrentWeatherData(currentWeatherData); - } catch (final IOException e) { - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(this.getFragmentManager(), "errorDialog"); - } - } - } - - this.setHasOptionsMenu(false); - this.setEmptyText("No data available"); - this.setListShown(true); - this.setListShownNoAnimation(true); - } - - @Override - public void onResume() { - super.onResume(); - - // 1. Register receiver - final IntentFilter filter = new IntentFilter(); - filter.addAction("de.example.exampletdd.UPDATECURRENTWEATHER"); - LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(this.mReceiver, - filter); - - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity()); - - // 2. Update units of measurement. - final String keyPreference = this.getResources().getString( - R.string.weather_preferences_units_key); - final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); - final String celsius = this.getResources().getString( - R.string.weather_preferences_units_celsius); - if (unitsPreferenceValue.equals(celsius)) { - this.mIsFahrenheit = false; - } else { - this.mIsFahrenheit = true; - } - - // 3. Try to restore old information - final Current currentWeatherData = this.mWeatherServicePersistenceFile - .getCurrentWeatherData(); - if (currentWeatherData != null) { - this.updateCurrentWeatherData(currentWeatherData); - } - } - - @Override - public void onPause() { - super.onPause(); - LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.mReceiver); - } - - @Override - public void onSaveInstanceState(final Bundle savedInstanceState) { - - // Save state - final Current currentWeatherData = this.mWeatherServicePersistenceFile - .getCurrentWeatherData(); - - if (currentWeatherData != null) { - savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData); - } - - super.onSaveInstanceState(savedInstanceState); - } - - public void updateCurrentWeatherData(final Current currentWeatherData) { - final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); - tempFormatter.applyPattern("#####.#####"); - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.US); - - final double tempUnits = this.mIsFahrenheit ? 0 : 273.15; - final String symbol = this.mIsFahrenheit ? "ºF" : "ºC"; - - final int[] layouts = new int[3]; - layouts[0] = R.layout.weather_current_data_entry_first; - layouts[1] = R.layout.weather_current_data_entry_second; - layouts[2] = R.layout.weather_current_data_entry_fifth; - final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(), - layouts); - - - String tempMax = ""; - if (currentWeatherData.getMain().getTemp_max() != null) { - double conversion = (Double) currentWeatherData.getMain().getTemp_max(); - conversion = conversion - tempUnits; - tempMax = tempFormatter.format(conversion) + symbol; - } - String tempMin = ""; - if (currentWeatherData.getMain().getTemp_min() != null) { - double conversion = (Double) currentWeatherData.getMain().getTemp_min(); - conversion = conversion - tempUnits; - tempMin = tempFormatter.format(conversion) + symbol; - } - Bitmap picture; - if ((currentWeatherData.getWeather().size() > 0) - && (currentWeatherData.getWeather().get(0).getIcon() != null) - && (IconsList.getIcon(currentWeatherData.getWeather().get(0).getIcon()) != null)) { - final String icon = currentWeatherData.getWeather().get(0).getIcon(); - picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon) - .getResourceDrawable()); - } else { - picture = BitmapFactory.decodeResource(this.getResources(), - R.drawable.weather_severe_alert); - } - final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax, - tempMin, picture); - adapter.add(entryFirst); - - String description = "no description available"; - if (currentWeatherData.getWeather().size() > 0) { - description = currentWeatherData.getWeather().get(0).getDescription(); - } - final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond( - description); - adapter.add(entrySecond); - - String humidityValue = ""; - if ((currentWeatherData.getMain() != null) - && (currentWeatherData.getMain().getHumidity() != null)) { - final double conversion = (Double) currentWeatherData.getMain().getHumidity(); - humidityValue = tempFormatter.format(conversion); - } - String pressureValue = ""; - if ((currentWeatherData.getMain() != null) - && (currentWeatherData.getMain().getPressure() != null)) { - final double conversion = (Double) currentWeatherData.getMain().getPressure(); - pressureValue = tempFormatter.format(conversion); - } - String windValue = ""; - if ((currentWeatherData.getWind() != null) - && (currentWeatherData.getWind().getSpeed() != null)) { - final double conversion = (Double) currentWeatherData.getWind().getSpeed(); - windValue = tempFormatter.format(conversion); - } - String rainValue = ""; - if ((currentWeatherData.getRain() != null) - && (currentWeatherData.getRain().get3h() != null)) { - final double conversion = (Double) currentWeatherData.getRain().get3h(); - rainValue = tempFormatter.format(conversion); - } - String cloudsValue = ""; - if ((currentWeatherData.getClouds() != null) - && (currentWeatherData.getClouds().getAll() != null)) { - final double conversion = (Double) currentWeatherData.getClouds().getAll(); - cloudsValue = tempFormatter.format(conversion); - } - String snowValue = ""; - if ((currentWeatherData.getSnow() != null) - && (currentWeatherData.getSnow().get3h() != null)) { - final double conversion = (Double) currentWeatherData.getSnow().get3h(); - snowValue = tempFormatter.format(conversion); - } - String feelsLike = ""; - if (currentWeatherData.getMain().getTemp() != null) { - double conversion = (Double) currentWeatherData.getMain().getTemp(); - conversion = conversion - tempUnits; - feelsLike = tempFormatter.format(conversion); - } - String sunRiseTime = ""; - if (currentWeatherData.getSys().getSunrise() != null) { - final long unixTime = (Long) currentWeatherData.getSys().getSunrise(); - final Date unixDate = new Date(unixTime * 1000L); - sunRiseTime = dateFormat.format(unixDate); - } - String sunSetTime = ""; - if (currentWeatherData.getSys().getSunset() != null) { - final long unixTime = (Long) currentWeatherData.getSys().getSunset(); - final Date unixDate = new Date(unixTime * 1000L); - sunSetTime = dateFormat.format(unixDate); - } - final WeatherCurrentDataEntryFifth entryFifth = new WeatherCurrentDataEntryFifth( - sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue, - feelsLike, symbol, snowValue, cloudsValue); - adapter.add(entryFifth); - - - this.setListAdapter(adapter); - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java new file mode 100644 index 0000000..cd79d9b --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -0,0 +1,331 @@ +package de.example.exampletdd.fragment.current; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import org.apache.http.client.ClientProtocolException; + +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.support.v4.app.ListFragment; +import android.util.Log; +import android.widget.ListView; + +import com.fasterxml.jackson.core.JsonParseException; + +import de.example.exampletdd.R; +import de.example.exampletdd.WeatherInformationApplication; +import de.example.exampletdd.httpclient.CustomHTTPClient; +import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.model.currentweather.Current; +import de.example.exampletdd.parser.JPOSWeatherParser; +import de.example.exampletdd.service.IconsList; +import de.example.exampletdd.service.ServiceParser; + +public class CurrentFragment extends ListFragment { + private static final String TAG = "CurrentFragment"; + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public void onActivityCreated(final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final ListView listWeatherView = this.getListView(); + listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE); + + if (savedInstanceState != null) { + // Restore UI state + final Current current = (Current) savedInstanceState.getSerializable("Current"); + + // TODO: Could it be better to store in global forecast data even if it is null value? + // So, perhaps do not check for null value and always store in global variable. + if (current != null) { + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + application.setCurrent(current); + } + + // TODO: Why don't I need mListState? + } + + // TODO: Why don't I need Adapter? + + this.setHasOptionsMenu(false); + // TODO: string static resource + this.setEmptyText("No data available"); + this.setListShown(true); + this.setListShownNoAnimation(true); + } + + @Override + public void onResume() { + super.onResume(); + + // TODO: retrive data from data base (like I do on WindowsPhone 8) + final GeocodingData geocodingData = new GeocodingData.Builder().build(); + if (geocodingData == null) { + // Nothing to do. + return; + } + + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + final Current current = application.getCurrent(); + + // TODO: Why don't I need mListState? + + // TODO: Also check whether data is fresh (like I do on WindowsPhone 8) using data base + if (current != null /* && dataIsFresh() */) { + this.updateUI(current); + } else { + // Load remote data (aynchronous) + // Gets the data from the web. + final CurrentTask task = new CurrentTask( + new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")), + new ServiceParser(new JPOSWeatherParser())); + + task.execute(geocodingData); + // TODO: make sure UI thread keeps running in parallel after that. I guess. + } + + // TODO: Overview is doing things with mListState... Why not here? + } + + @Override + public void onSaveInstanceState(final Bundle savedInstanceState) { + + // Save UI state + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + final Current current = application.getCurrent(); + + // TODO: Could it be better to save current data even if it is null value? + // So, perhaps do not check for null value. + if (current != null) { + savedInstanceState.putSerializable("Current", current); + } + + // TODO: Why don't I need mListState? + + super.onSaveInstanceState(savedInstanceState); + } + + private void updateUI(final Current current) { + + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this.getActivity()); + + // TODO: repeating the same code in Overview, Specific and Current!!! + // 1. Update units of measurement. + boolean mIsFahrenheit = false; + final String keyPreference = this.getResources().getString( + R.string.weather_preferences_units_key); + final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + final String celsius = this.getResources().getString( + R.string.weather_preferences_units_celsius); + if (unitsPreferenceValue.equals(celsius)) { + mIsFahrenheit = false; + } else { + mIsFahrenheit = true; + } + final double tempUnits = mIsFahrenheit ? 0 : 273.15; + final String symbol = mIsFahrenheit ? "ºF" : "ºC"; + + + // 2. Formatters + final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); + tempFormatter.applyPattern("#####.#####"); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.US); + + + // 3. Prepare data for UI. + final int[] layouts = new int[3]; + layouts[0] = R.layout.weather_current_data_entry_first; + layouts[1] = R.layout.weather_current_data_entry_second; + layouts[2] = R.layout.weather_current_data_entry_fifth; + final CurrentAdapter adapter = new CurrentAdapter(this.getActivity(), + layouts); + + + String tempMax = ""; + if (current.getMain().getTemp_max() != null) { + double conversion = (Double) current.getMain().getTemp_max(); + conversion = conversion - tempUnits; + tempMax = tempFormatter.format(conversion) + symbol; + } + String tempMin = ""; + if (current.getMain().getTemp_min() != null) { + double conversion = (Double) current.getMain().getTemp_min(); + conversion = conversion - tempUnits; + tempMin = tempFormatter.format(conversion) + symbol; + } + Bitmap picture; + if ((current.getWeather().size() > 0) + && (current.getWeather().get(0).getIcon() != null) + && (IconsList.getIcon(current.getWeather().get(0).getIcon()) != null)) { + final String icon = current.getWeather().get(0).getIcon(); + picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon) + .getResourceDrawable()); + } else { + picture = BitmapFactory.decodeResource(this.getResources(), + R.drawable.weather_severe_alert); + } + final CurrentDataEntryFirst entryFirst = new CurrentDataEntryFirst(tempMax, + tempMin, picture); + adapter.add(entryFirst); + + String description = "no description available"; + if (current.getWeather().size() > 0) { + description = current.getWeather().get(0).getDescription(); + } + final CurrentDataEntrySecond entrySecond = new CurrentDataEntrySecond( + description); + adapter.add(entrySecond); + + String humidityValue = ""; + if ((current.getMain() != null) + && (current.getMain().getHumidity() != null)) { + final double conversion = (Double) current.getMain().getHumidity(); + humidityValue = tempFormatter.format(conversion); + } + String pressureValue = ""; + if ((current.getMain() != null) + && (current.getMain().getPressure() != null)) { + final double conversion = (Double) current.getMain().getPressure(); + pressureValue = tempFormatter.format(conversion); + } + String windValue = ""; + if ((current.getWind() != null) + && (current.getWind().getSpeed() != null)) { + final double conversion = (Double) current.getWind().getSpeed(); + windValue = tempFormatter.format(conversion); + } + String rainValue = ""; + if ((current.getRain() != null) + && (current.getRain().get3h() != null)) { + final double conversion = (Double) current.getRain().get3h(); + rainValue = tempFormatter.format(conversion); + } + String cloudsValue = ""; + if ((current.getClouds() != null) + && (current.getClouds().getAll() != null)) { + final double conversion = (Double) current.getClouds().getAll(); + cloudsValue = tempFormatter.format(conversion); + } + String snowValue = ""; + if ((current.getSnow() != null) + && (current.getSnow().get3h() != null)) { + final double conversion = (Double) current.getSnow().get3h(); + snowValue = tempFormatter.format(conversion); + } + String feelsLike = ""; + if (current.getMain().getTemp() != null) { + double conversion = (Double) current.getMain().getTemp(); + conversion = conversion - tempUnits; + feelsLike = tempFormatter.format(conversion); + } + String sunRiseTime = ""; + if (current.getSys().getSunrise() != null) { + final long unixTime = (Long) current.getSys().getSunrise(); + final Date unixDate = new Date(unixTime * 1000L); + sunRiseTime = dateFormat.format(unixDate); + } + String sunSetTime = ""; + if (current.getSys().getSunset() != null) { + final long unixTime = (Long) current.getSys().getSunset(); + final Date unixDate = new Date(unixTime * 1000L); + sunSetTime = dateFormat.format(unixDate); + } + final CurrentDataEntryFifth entryFifth = new CurrentDataEntryFifth( + sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue, + feelsLike, symbol, snowValue, cloudsValue); + adapter.add(entryFifth); + + + // 4. Update UI. + // TODO: Why am I not doing the same as in OverviewFragment? + this.setListAdapter(adapter); + } + + private class CurrentTask extends AsyncTask { + final CustomHTTPClient weatherHTTPClient; + final ServiceParser weatherService; + + public CurrentTask(final CustomHTTPClient weatherHTTPClient, final ServiceParser weatherService) { + this.weatherHTTPClient = weatherHTTPClient; + this.weatherService = weatherService; + } + + @Override + protected Current doInBackground(final GeocodingData... params) { + Log.i(TAG, "CurrentTask doInBackground"); + Current current = null; + + try { + current = this.doInBackgroundThrowable(params[0], weatherHTTPClient, weatherService); + } catch (final JsonParseException e) { + Log.e(TAG, "CurrentTask doInBackground exception: ", e); + } catch (final ClientProtocolException e) { + Log.e(TAG, "CurrentTask doInBackground exception: ", e); + } catch (final MalformedURLException e) { + Log.e(TAG, "CurrentTask doInBackground exception: ", e); + } catch (final URISyntaxException e) { + Log.e(TAG, "CurrentTask doInBackground exception: ", e); + } catch (final IOException e) { + // logger infrastructure swallows UnknownHostException :/ + Log.e(TAG, "CurrentTask doInBackground exception: " + e.getMessage(), e); + } finally { + weatherHTTPClient.close(); + } + + return current; + } + + private Current doInBackgroundThrowable(final GeocodingData geocodingData, + final CustomHTTPClient HTTPClient, final ServiceParser serviceParser) + throws URISyntaxException, ClientProtocolException, JsonParseException, IOException { + + final String APIVersion = getResources().getString(R.string.api_version); + final String urlAPI = getResources().getString(R.string.uri_api_weather_today); + final String url = weatherService.createURIAPICurrent(urlAPI, APIVersion, + geocodingData.getLatitude(), geocodingData.getLongitude()); + final String jsonData = weatherHTTPClient.retrieveDataAsString(new URL(url)); + final Current current = weatherService + .retrieveCurrentFromJPOS(jsonData); + // TODO: what is this for? I guess I could skip it :/ + final Calendar now = Calendar.getInstance(); + current.setDate(now.getTime()); + + return current; + } + + @Override + protected void onPostExecute(final Current current) { + // Call updateUI on the UI thread. + updateUI(current); + + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + application.setCurrent(current); + + // TODO: update last time update using data base (like I do on Windows Phone 8) + } + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataAdapter.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataAdapter.java deleted file mode 100644 index 78301bd..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataAdapter.java +++ /dev/null @@ -1,204 +0,0 @@ -package de.example.exampletdd.fragment.current; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ImageView; -import android.widget.TextView; -import de.example.exampletdd.R; - -public class WeatherCurrentDataAdapter extends ArrayAdapter { - private static final int FIRST = 0; - private static final int SECOND = 1; - private static final int THIRD = 2; - private final int[] resources; - - public WeatherCurrentDataAdapter(final Context context, final int[] resources) { - super(context, 0); - - this.resources = resources; - } - - - @Override - public View getView(final int position, final View convertView, final ViewGroup parent) { - - final View view = this.getWorkingView(position, convertView); - final int viewType = this.getItemViewType(position); - - if (viewType == FIRST) { - - final ViewFirstHolder viewHolder = this.getViewFirstHolder(view); - final WeatherCurrentDataEntryFirst entry = (WeatherCurrentDataEntryFirst) this - .getItem(position); - viewHolder.picture.setImageBitmap(entry.getPicture()); - viewHolder.tempMax.setText(entry.getTempMax()); - viewHolder.tempMin.setText(entry.getTempMin()); - } else if (viewType == SECOND) { - final ViewSecondHolder viewHolder = this.getViewSecondHolder(view); - final WeatherCurrentDataEntrySecond entry = (WeatherCurrentDataEntrySecond) this - .getItem(position); - viewHolder.weatherDescription.setText(entry.getWeatherDescription()); - } else if (viewType == THIRD) { - final ViewThirdHolder viewHolder = this.getViewThirdHolder(view); - final WeatherCurrentDataEntryFifth entry = (WeatherCurrentDataEntryFifth) this - .getItem(position); - viewHolder.humidityValue.setText(entry.getHumidityValue()); - viewHolder.pressureValue.setText(entry.getPressureValue()); - viewHolder.rainValue.setText(entry.getRainValue()); - viewHolder.cloudsValue.setText(entry.getCloudsValue()); - viewHolder.windValue.setText(entry.getWindValue()); - viewHolder.sunRiseTime.setText(entry.getSunRiseTime()); - viewHolder.sunSetTime.setText(entry.getSunSetTime()); - viewHolder.feelsLike.setText(entry.getFeelsLike()); - viewHolder.snowValue.setText(entry.getSnowValue()); - viewHolder.feelsLikeUnits.setText(entry.getFeelsLikeUnits()); - } - - return view; - } - - @Override - public int getItemViewType(final int position) { - int type = 0; - - if (position == 0) { - type = FIRST; - } else if (position == 1) { - type = SECOND; - } else if (position == 2) { - type = THIRD; - } - - return type; - } - - @Override - public int getViewTypeCount() { - return 3; - } - - private View getWorkingView(final int position, final View convertView) { - View workingView = null; - - if (convertView == null) { - final int viewType = this.getItemViewType(position); - final Context context = this.getContext(); - final LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - workingView = inflater.inflate(this.resources[viewType], null); - } else { - workingView = convertView; - } - - return workingView; - } - - private ViewFirstHolder getViewFirstHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewFirstHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewFirstHolder)) { - viewHolder = new ViewFirstHolder(); - - viewHolder.picture = (ImageView) workingView - .findViewById(R.id.weather_current_data_picture); - viewHolder.tempMax = (TextView) workingView - .findViewById(R.id.weather_current_data_temp_max); - viewHolder.tempMin = (TextView) workingView - .findViewById(R.id.weather_current_data_temp_min); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewFirstHolder) tag; - } - - return viewHolder; - } - - private ViewSecondHolder getViewSecondHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewSecondHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewSecondHolder)) { - viewHolder = new ViewSecondHolder(); - - viewHolder.weatherDescription = (TextView) workingView - .findViewById(R.id.weather_current_data_description); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewSecondHolder) tag; - } - - return viewHolder; - } - - private ViewThirdHolder getViewThirdHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewThirdHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewThirdHolder)) { - viewHolder = new ViewThirdHolder(); - - viewHolder.humidityValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_humidity_value); - viewHolder.pressureValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_pressure_value); - viewHolder.rainValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_rain_value); - viewHolder.cloudsValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_clouds_value); - viewHolder.windValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_wind_value); - viewHolder.cloudsValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_clouds_value); - viewHolder.snowValue = (TextView) workingView - .findViewById(R.id.weather_current_now_data_snow_value); - viewHolder.sunRiseTime = (TextView) workingView - .findViewById(R.id.weather_current_now_data_sunrise_value); - viewHolder.sunSetTime = (TextView) workingView - .findViewById(R.id.weather_current_now_data_sunset_value); - viewHolder.feelsLike = (TextView) workingView - .findViewById(R.id.weather_current_now_data_feelslike_value); - viewHolder.feelsLikeUnits = (TextView) workingView - .findViewById(R.id.weather_current_now_data_feelslike_units); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewThirdHolder) tag; - } - - return viewHolder; - } - - - private static class ViewFirstHolder { - public ImageView picture; - public TextView tempMax; - public TextView tempMin; - } - - private static class ViewSecondHolder { - public TextView weatherDescription; - } - - private static class ViewThirdHolder { - public TextView humidityValue; - public TextView pressureValue; - public TextView windValue; - public TextView rainValue; - public TextView cloudsValue; - public TextView snowValue; - public TextView sunRiseTime; - public TextView sunSetTime; - public TextView feelsLike; - public TextView feelsLikeUnits; - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFifth.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFifth.java deleted file mode 100644 index 2909624..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFifth.java +++ /dev/null @@ -1,71 +0,0 @@ -package de.example.exampletdd.fragment.current; - -public class WeatherCurrentDataEntryFifth { - private final String sunRiseTime; - private final String sunSetTime; - private final String humidityValue; - private final String pressureValue; - private final String windValue; - private final String rainValue; - private final String cloudsValue; - private final String feelsLike; - private final String feelsLikeUnits; - private final String snowValue; - - public WeatherCurrentDataEntryFifth(final String sunRiseTime, final String sunSetTime, - final String humidityValue, final String pressureValue, final String windValue, - final String rainValue, final String feelsLike, final String feelsLikeUnits, - final String snowValue, - final String cloudsValue) { - this.sunRiseTime = sunRiseTime; - this.sunSetTime = sunSetTime; - this.humidityValue = humidityValue; - this.pressureValue = pressureValue; - this.windValue = windValue; - this.rainValue = rainValue; - this.feelsLike = feelsLike; - this.feelsLikeUnits = feelsLikeUnits; - this.snowValue = snowValue; - this.cloudsValue = cloudsValue; - } - - public String getSunRiseTime() { - return this.sunRiseTime; - } - - public String getSunSetTime() { - return this.sunSetTime; - } - - public String getFeelsLike() { - return this.feelsLike; - } - - public String getFeelsLikeUnits() { - return this.feelsLikeUnits; - } - - public String getHumidityValue() { - return this.humidityValue; - } - - public String getPressureValue() { - return this.pressureValue; - } - - public String getWindValue() { - return this.windValue; - } - - public String getRainValue() { - return this.rainValue; - } - - public String getCloudsValue() { - return this.cloudsValue; - } - - public String getSnowValue() { - return this.snowValue; - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFirst.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFirst.java deleted file mode 100644 index 67d0463..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntryFirst.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.example.exampletdd.fragment.current; - -import android.graphics.Bitmap; - -public class WeatherCurrentDataEntryFirst { - private final Bitmap picture; - private final String tempMax; - private final String tempMin; - - public WeatherCurrentDataEntryFirst(final String tempMax, final String tempMin, - final Bitmap picture) { - this.tempMax = tempMax; - this.tempMin = tempMin; - this.picture = picture; - } - - public Bitmap getPicture() { - return this.picture; - } - - public String getTempMax() { - return this.tempMax; - } - - public String getTempMin() { - return this.tempMin; - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntrySecond.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntrySecond.java deleted file mode 100644 index 1d3222d..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherCurrentDataEntrySecond.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.example.exampletdd.fragment.current; - -public class WeatherCurrentDataEntrySecond { - private final String weatherDescription; - - public WeatherCurrentDataEntrySecond(final String weatherDescription) { - this.weatherDescription = weatherDescription; - } - - public String getWeatherDescription() { - return this.weatherDescription; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/IconsList.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/IconsList.java deleted file mode 100644 index ecfb2e9..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/IconsList.java +++ /dev/null @@ -1,143 +0,0 @@ -package de.example.exampletdd.fragment.overview; - -import java.util.HashMap; -import java.util.Map; - -import de.example.exampletdd.R; - -public enum IconsList { - ICON_01d("01d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_clear; - } - }, - ICON_01n("01n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_clear_night; - } - }, - ICON_02d("02d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_few_clouds; - } - }, - ICON_02n("02n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_few_clouds_night; - } - }, - ICON_03d("03d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_few_clouds; - } - }, - ICON_03n("03n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_few_clouds; - } - }, - ICON_04d("04d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_overcast; - } - }, - ICON_04n("04n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_overcast; - } - }, - ICON_09d("09d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_showers; - } - }, - ICON_09n("09n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_showers; - } - }, - ICON_10d("10d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_showers_scattered; - } - }, - ICON_10n("10n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_showers_scattered; - } - }, - ICON_11d("11d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_storm; - } - }, - ICON_11n("11n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_storm; - } - }, - ICON_13d("13d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_snow; - } - }, - ICON_13n("13n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_snow; - } - }, - ICON_50d("50d") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_fog; - } - }, - ICON_50n("50n") { - @Override - public int getResourceDrawable() { - return R.drawable.weather_fog; - } - }; - - private final String icon; - // Map with every enum constant. Class variable initializer. JLS§12.4.2 - // Executed in textual order. - private static final Map codeMap = new HashMap(); - - // Static initializer. JLS§12.4.2 Executed in textual order. - static { - for (final IconsList code : IconsList.values()) { - codeMap.put(code.getIcon(), code); - } - } - - private IconsList(final String icon) { - this.icon = icon; - } - - public static final IconsList getIcon(final String icon) { - return codeMap.get(icon); - } - - private String getIcon() { - return this.icon; - } - - public abstract int getResourceDrawable(); -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index 40b7cbd..a7aa902 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -34,11 +34,12 @@ import com.fasterxml.jackson.core.JsonParseException; import de.example.exampletdd.R; import de.example.exampletdd.WeatherInformationApplication; -import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment; +import de.example.exampletdd.fragment.specific.SpecificFragment; import de.example.exampletdd.httpclient.CustomHTTPClient; import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.forecastweather.Forecast; import de.example.exampletdd.parser.JPOSWeatherParser; +import de.example.exampletdd.service.IconsList; import de.example.exampletdd.service.ServiceParser; public class OverviewFragment extends ListFragment { @@ -61,10 +62,11 @@ public class OverviewFragment extends ListFragment { // Restore UI state final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast"); - // TODO: Could it be better to store in global data forecast even if it is null value? + // TODO: Could it be better to store in global forecast data even if it is null value? // So, perhaps do not check for null value and always store in global variable. if (forecast != null) { - final WeatherInformationApplication application = (WeatherInformationApplication) getActivity().getApplication(); + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); application.setForecast(forecast); } @@ -95,12 +97,13 @@ public class OverviewFragment extends ListFragment { return; } - final WeatherInformationApplication application = (WeatherInformationApplication) getActivity().getApplication(); + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); final Forecast forecast = application.getForecast(); // TODO: Also check whether data is fresh (like I do on WindowsPhone 8) using data base if (forecast != null /* && dataIsFresh() */) { - this.updateForecastWeatherData(forecast); + this.updateUI(forecast); } else { // Load remote data (aynchronous) // Gets the data from the web. @@ -113,6 +116,7 @@ public class OverviewFragment extends ListFragment { } // TODO: could mListState be an old value? It is just updated in onActivityCreated method... :/ + // What is this for? And be careful, it runs at the same time as updateUI!!! :( if (this.mListState != null) { this.getListView().onRestoreInstanceState(this.mListState); } @@ -122,9 +126,12 @@ public class OverviewFragment extends ListFragment { public void onSaveInstanceState(final Bundle savedInstanceState) { // Save UI state - final WeatherInformationApplication application = (WeatherInformationApplication) getActivity().getApplication(); + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); final Forecast forecast = application.getForecast(); + // TODO: Could it be better to save forecast data even if it is null value? + // So, perhaps do not check for null value. if (forecast != null) { savedInstanceState.putSerializable("Forecast", forecast); } @@ -137,41 +144,41 @@ public class OverviewFragment extends ListFragment { @Override public void onListItemClick(final ListView l, final View v, final int position, final long id) { - final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this - .getFragmentManager().findFragmentById(R.id.weather_specific_data__fragment); + final SpecificFragment fragment = (SpecificFragment) this + .getFragmentManager().findFragmentById(R.id.weather_specific_fragment); if (fragment == null) { // handset layout final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO") .setComponent(new ComponentName("de.example.exampletdd", - "de.example.exampletdd.WeatherInformationSpecificDataActivity")); + "de.example.exampletdd.SpecificActivity")); intent.putExtra("CHOSEN_DAY", (int) id); OverviewFragment.this.getActivity().startActivity(intent); } else { // tablet layout - fragment.getWeatherByDay((int) id); + fragment.updateUIByChosenDay((int) id); } } - private void updateForecastWeatherData(final Forecast forecastWeatherData) { + private void updateUI(final Forecast forecastWeatherData) { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); - + // TODO: repeating the same code in Overview, Specific and Current!!! // 1. Update units of measurement. - boolean mIsFahrenheit = false; + boolean isFahrenheit = false; String keyPreference = this.getResources() .getString(R.string.weather_preferences_units_key); final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "Celsius"); final String celsius = this.getResources().getString( R.string.weather_preferences_units_celsius); if (unitsPreferenceValue.equals(celsius)) { - mIsFahrenheit = false; + isFahrenheit = false; } else { - mIsFahrenheit = true; + isFahrenheit = true; } - final double tempUnits = mIsFahrenheit ? 0 : 273.15; - final String symbol = mIsFahrenheit ? "ºF" : "ºC"; + final double tempUnits = isFahrenheit ? 0 : 273.15; + final String symbol = isFahrenheit ? "ºF" : "ºC"; // 2. Update number day forecast. @@ -182,7 +189,7 @@ public class OverviewFragment extends ListFragment { mDayForecast = Integer.valueOf(dayForecast); - // 3. Date format + // 3. Formatters final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); tempFormatter.applyPattern("#####.##"); final SimpleDateFormat dayNameFormatter = new SimpleDateFormat("EEE", Locale.US); @@ -249,7 +256,7 @@ public class OverviewFragment extends ListFragment { } - public class OverviewTask extends AsyncTask { + private class OverviewTask extends AsyncTask { final CustomHTTPClient weatherHTTPClient; final ServiceParser weatherService; @@ -261,26 +268,26 @@ public class OverviewFragment extends ListFragment { @Override protected Forecast doInBackground(final GeocodingData... params) { Log.i(TAG, "OverviewFragment doInBackground"); - Forecast weatherData = null; + Forecast forecast = null; try { - weatherData = this.doInBackgroundThrowable(params[0], weatherHTTPClient, weatherService); + forecast = this.doInBackgroundThrowable(params[0], weatherHTTPClient, weatherService); } catch (final JsonParseException e) { - Log.e(TAG, "doInBackground exception: ", e); + Log.e(TAG, "OverviewTask doInBackground exception: ", e); } catch (final ClientProtocolException e) { - Log.e(TAG, "doInBackground exception: ", e); + Log.e(TAG, "OverviewTask doInBackground exception: ", e); } catch (final MalformedURLException e) { - Log.e(TAG, "doInBackground exception: ", e); + Log.e(TAG, "OverviewTask doInBackground exception: ", e); } catch (final URISyntaxException e) { - Log.e(TAG, "doInBackground exception: ", e); + Log.e(TAG, "OverviewTask doInBackground exception: ", e); } catch (final IOException e) { // logger infrastructure swallows UnknownHostException :/ - Log.e(TAG, "doInBackground exception: " + e.getMessage(), e); + Log.e(TAG, "OverviewTask doInBackground exception: " + e.getMessage(), e); } finally { weatherHTTPClient.close(); } - return weatherData; + return forecast; } private Forecast doInBackgroundThrowable(final GeocodingData geocodingData, @@ -290,19 +297,20 @@ public class OverviewFragment extends ListFragment { final String APIVersion = getResources().getString(R.string.api_version); final String urlAPI = getResources().getString(R.string.uri_api_weather_forecast); // TODO: number as resource - final String url = serviceParser.createURIAPIForecastWeather(urlAPI, APIVersion, + final String url = serviceParser.createURIAPIForecast(urlAPI, APIVersion, geocodingData.getLatitude(), geocodingData.getLongitude(), "14"); final String jsonData = HTTPClient.retrieveDataAsString(new URL(url)); - return serviceParser.retrieveForecastWeatherDataFromJPOS(jsonData); + return serviceParser.retrieveForecastFromJPOS(jsonData); } @Override protected void onPostExecute(final Forecast forecast) { - // Call UpdateUI on the UI thread. - updateForecastWeatherData(forecast); + // Call updateUI on the UI thread. + updateUI(forecast); - final WeatherInformationApplication application = (WeatherInformationApplication) getActivity().getApplication(); + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); application.setForecast(forecast); // TODO: update last time update using data base (like I do on Windows Phone 8) diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificAdapter.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificAdapter.java new file mode 100644 index 0000000..9a8dabf --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificAdapter.java @@ -0,0 +1,223 @@ +package de.example.exampletdd.fragment.specific; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import de.example.exampletdd.R; + +public class SpecificAdapter extends ArrayAdapter { + private static final int FIRST = 0; + private static final int SECOND = 1; + private static final int THIRD = 2; + private static final int FOURTH = 3; + private final int[] resources; + + public SpecificAdapter(final Context context, final int[] resources) { + super(context, 0); + + this.resources = resources; + } + + + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + + final View view = this.getWorkingView(position, convertView); + final int viewType = this.getItemViewType(position); + + if (viewType == FIRST) { + + final ViewFirstHolder viewHolder = this.getViewFirstHolder(view); + final SpecificDataEntryFirst entry = (SpecificDataEntryFirst) this + .getItem(position); + viewHolder.picture.setImageBitmap(entry.getPicture()); + viewHolder.tempMax.setText(entry.getTempMax()); + viewHolder.tempMin.setText(entry.getTempMin()); + } else if (viewType == SECOND) { + final ViewSecondHolder viewHolder = this.getViewSecondHolder(view); + final SpecificDataEntrySecond entry = (SpecificDataEntrySecond) this + .getItem(position); + viewHolder.weatherDescription.setText(entry.getWeatherDescription()); + } else if (viewType == THIRD) { + final ViewThirdHolder viewHolder = this.getViewThirdHolder(view); + final SpecificDataEntryThird entry = (SpecificDataEntryThird) this + .getItem(position); + viewHolder.humidityValue.setText(entry.getHumidityValue()); + viewHolder.pressureValue.setText(entry.getPressureValue()); + viewHolder.rainValue.setText(entry.getRainValue()); + viewHolder.cloudsValue.setText(entry.getCloudsValue()); + viewHolder.windValue.setText(entry.getWindValue()); + } else if (viewType == FOURTH) { + final ViewFourthHolder viewHolder = this.getViewFourthHolder(view); + final SpecificDataEntryFourth entry = (SpecificDataEntryFourth) this + .getItem(position); + viewHolder.dayTemp.setText(entry.getDayTemp()); + viewHolder.morningTemp.setText(entry.getEveTemp()); + viewHolder.eveTemp.setText(entry.getEveTemp()); + viewHolder.nightTemp.setText(entry.getNightTemp()); + } + + return view; + } + + @Override + public int getItemViewType(final int position) { + int type = 0; + + if (position == 0) { + type = FIRST; + } else if (position == 1) { + type = SECOND; + } else if (position == 2) { + type = THIRD; + } else if (position == 3) { + type = FOURTH; + } + + return type; + } + + @Override + public int getViewTypeCount() { + return 4; + } + + private View getWorkingView(final int position, final View convertView) { + View workingView = null; + + if (convertView == null) { + final int viewType = this.getItemViewType(position); + final Context context = this.getContext(); + final LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + workingView = inflater.inflate(this.resources[viewType], null); + } else { + workingView = convertView; + } + + return workingView; + } + + private ViewFirstHolder getViewFirstHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewFirstHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewFirstHolder)) { + viewHolder = new ViewFirstHolder(); + + viewHolder.picture = (ImageView) workingView + .findViewById(R.id.weather_current_data_picture); + viewHolder.tempMax = (TextView) workingView + .findViewById(R.id.weather_current_data_temp_max); + viewHolder.tempMin = (TextView) workingView + .findViewById(R.id.weather_current_data_temp_min); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewFirstHolder) tag; + } + + return viewHolder; + } + + private ViewSecondHolder getViewSecondHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewSecondHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewSecondHolder)) { + viewHolder = new ViewSecondHolder(); + + viewHolder.weatherDescription = (TextView) workingView + .findViewById(R.id.weather_current_data_description); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewSecondHolder) tag; + } + + return viewHolder; + } + + private ViewThirdHolder getViewThirdHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewThirdHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewThirdHolder)) { + viewHolder = new ViewThirdHolder(); + + viewHolder.humidityValue = (TextView) workingView + .findViewById(R.id.weather_current_data_humidity_value); + viewHolder.pressureValue = (TextView) workingView + .findViewById(R.id.weather_current_data_pressure_value); + viewHolder.rainValue = (TextView) workingView + .findViewById(R.id.weather_current_data_rain_value); + viewHolder.cloudsValue = (TextView) workingView + .findViewById(R.id.weather_current_data_clouds_value); + viewHolder.windValue = (TextView) workingView + .findViewById(R.id.weather_current_data_wind_value); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewThirdHolder) tag; + } + + return viewHolder; + } + + private ViewFourthHolder getViewFourthHolder(final View workingView) { + final Object tag = workingView.getTag(); + ViewFourthHolder viewHolder = null; + + if ((null == tag) || !(tag instanceof ViewFourthHolder)) { + viewHolder = new ViewFourthHolder(); + + viewHolder.morningTemp = (TextView) workingView + .findViewById(R.id.weather_morn_temperature); + viewHolder.dayTemp = (TextView) workingView.findViewById(R.id.weather_day_temperature); + viewHolder.eveTemp = (TextView) workingView.findViewById(R.id.weather_eve_temperature); + viewHolder.nightTemp = (TextView) workingView + .findViewById(R.id.weather_night_temperature); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewFourthHolder) tag; + } + + return viewHolder; + } + + + private static class ViewFirstHolder { + public ImageView picture; + public TextView tempMax; + public TextView tempMin; + } + + private static class ViewSecondHolder { + public TextView weatherDescription; + } + + private static class ViewThirdHolder { + public TextView humidityValue; + public TextView pressureValue; + public TextView windValue; + public TextView rainValue; + public TextView cloudsValue; + } + + private static class ViewFourthHolder { + private TextView morningTemp; + private TextView dayTemp; + private TextView eveTemp; + private TextView nightTemp; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFirst.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFirst.java new file mode 100644 index 0000000..ccd50dc --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFirst.java @@ -0,0 +1,28 @@ +package de.example.exampletdd.fragment.specific; + +import android.graphics.Bitmap; + +public class SpecificDataEntryFirst { + private final Bitmap picture; + private final String tempMax; + private final String tempMin; + + public SpecificDataEntryFirst(final String tempMax, final String tempMin, + final Bitmap picture) { + this.tempMax = tempMax; + this.tempMin = tempMin; + this.picture = picture; + } + + public Bitmap getPicture() { + return this.picture; + } + + public String getTempMax() { + return this.tempMax; + } + + public String getTempMin() { + return this.tempMin; + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFourth.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFourth.java new file mode 100644 index 0000000..c0c33ce --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryFourth.java @@ -0,0 +1,33 @@ +package de.example.exampletdd.fragment.specific; + +public class SpecificDataEntryFourth { + private final String morningTemp; + private final String dayTemp; + private final String eveTemp; + private final String nightTemp; + + public SpecificDataEntryFourth(final String morningTemp, final String dayTemp, + final String eveTemp, final String nightTemp) { + this.morningTemp = morningTemp; + this.dayTemp = dayTemp; + this.eveTemp = eveTemp; + this.nightTemp = nightTemp; + } + + public String getMorningTemp() { + return this.morningTemp; + } + + public String getDayTemp() { + return this.dayTemp; + } + + public String getEveTemp() { + return this.eveTemp; + } + + public String getNightTemp() { + return this.nightTemp; + } + +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntrySecond.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntrySecond.java new file mode 100644 index 0000000..6471b17 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntrySecond.java @@ -0,0 +1,14 @@ +package de.example.exampletdd.fragment.specific; + +public class SpecificDataEntrySecond { + private final String weatherDescription; + + public SpecificDataEntrySecond(final String weatherDescription) { + this.weatherDescription = weatherDescription; + } + + public String getWeatherDescription() { + return this.weatherDescription; + } + +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryThird.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryThird.java new file mode 100644 index 0000000..d33ed13 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificDataEntryThird.java @@ -0,0 +1,39 @@ +package de.example.exampletdd.fragment.specific; + +public class SpecificDataEntryThird { + private final String humidityValue; + private final String pressureValue; + private final String windValue; + private final String rainValue; + private final String cloudsValue; + + public SpecificDataEntryThird(final String humidityValue, final String pressureValue, + final String windValue, final String rainValue, final String cloudsValue) { + this.humidityValue = humidityValue; + this.pressureValue = pressureValue; + this.windValue = windValue; + this.rainValue = rainValue; + this.cloudsValue = cloudsValue; + } + + public String getHumidityValue() { + return this.humidityValue; + } + + public String getPressureValue() { + return this.pressureValue; + } + + public String getWindValue() { + return this.windValue; + } + + public String getRainValue() { + return this.rainValue; + } + + public String getCloudsValue() { + return this.cloudsValue; + } + +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificFragment.java new file mode 100644 index 0000000..542749e --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/SpecificFragment.java @@ -0,0 +1,274 @@ +package de.example.exampletdd.fragment.specific; + +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.ListFragment; +import android.widget.ListView; +import de.example.exampletdd.R; +import de.example.exampletdd.WeatherInformationApplication; +import de.example.exampletdd.model.forecastweather.Forecast; +import de.example.exampletdd.service.IconsList; + +public class SpecificFragment extends ListFragment { + private int mChosenDay; + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public void onConfigurationChanged(final Configuration newConfig) { + // I could do the same in onCreate because I allow rotations. + // see: http://stackoverflow.com/a/11286961 + // Anyhow I wanted to try this way just for fun. + + final Bundle extras = this.getActivity().getIntent().getExtras(); + + if (extras != null) { + this.mChosenDay = extras.getInt("CHOSEN_DAY", 0); + } else { + // Always 0 when tablet layout. + this.mChosenDay = 0; + } + } + + @Override + public void onActivityCreated(final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final ListView listWeatherView = this.getListView(); + listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE); + + if (savedInstanceState != null) { + // Restore UI state + final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast"); + + // TODO: Could it be better to store in global data forecast even if it is null value? + // So, perhaps do not check for null value and always store in global variable. + if (forecast != null) { + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + application.setForecast(forecast); + } + + this.mChosenDay = savedInstanceState.getInt("Chosen day"); + // TODO: Why don't I need mListState? + } + + // TODO: Why don't I need Adapter? + + // TODO: string static resource + this.setEmptyText("No data available"); + // TODO: Why is it different to Current and Overview fragments? + this.setListShownNoAnimation(false); + } + + @Override + public void onSaveInstanceState(final Bundle savedInstanceState) { + + // Save UI state + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + final Forecast forecast = application.getForecast(); + + // TODO: Could it be better to save forecast data even if it is null value? + // So, perhaps do not check for null value. + if (forecast != null) { + savedInstanceState.putSerializable("Forecast", forecast); + } + + savedInstanceState.putInt("Chosend day", this.mChosenDay); + + // TODO: Why don't I need mListState? + + super.onSaveInstanceState(savedInstanceState); + } + + /** + * This method is used by tablet layout. + * + * @param chosenDay + */ + public void updateUIByChosenDay(final int chosenDay) { + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + final Forecast forecast = application.getForecast(); + + if (forecast != null) { + this.updateUI(forecast, chosenDay); + } + } + + + 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. + boolean isFahrenheit = false; + final String keyPreference = this.getResources().getString( + R.string.weather_preferences_units_key); + final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); + final String celsius = this.getResources().getString( + R.string.weather_preferences_units_celsius); + if (unitsPreferenceValue.equals(celsius)) { + isFahrenheit = false; + } else { + isFahrenheit = true; + } + final double tempUnits = isFahrenheit ? 0 : 273.15; + final String symbol = isFahrenheit ? "ºF" : "ºC"; + + + // 2. Formatters + final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); + tempFormatter.applyPattern("#####.#####"); + + + // 3. Prepare data for UI. + final int[] layouts = new int[4]; + layouts[0] = R.layout.weather_current_data_entry_first; + layouts[1] = R.layout.weather_current_data_entry_second; + layouts[2] = R.layout.weather_current_data_entry_third; + layouts[3] = R.layout.weather_current_data_entry_fourth; + final SpecificAdapter adapter = new SpecificAdapter(this.getActivity(), layouts); + + + final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData + .getList().get((chosenDay)); + + final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US); + final Calendar calendar = Calendar.getInstance(); + final Long forecastUNIXDate = (Long) forecast.getDt(); + calendar.setTimeInMillis(forecastUNIXDate * 1000L); + final Date date = calendar.getTime(); + this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase()); + + + String tempMax = ""; + if (forecast.getTemp().getMax() != null) { + double conversion = (Double) forecast.getTemp().getMax(); + conversion = conversion - tempUnits; + tempMax = tempFormatter.format(conversion) + symbol; + } + String tempMin = ""; + if (forecast.getTemp().getMin() != null) { + double conversion = (Double) forecast.getTemp().getMin(); + conversion = conversion - tempUnits; + tempMin = tempFormatter.format(conversion) + symbol; + } + Bitmap picture; + if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null) + && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) { + final String icon = forecast.getWeather().get(0).getIcon(); + picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon) + .getResourceDrawable()); + } else { + picture = BitmapFactory.decodeResource(this.getResources(), + R.drawable.weather_severe_alert); + } + final SpecificDataEntryFirst entryFirst = new SpecificDataEntryFirst(tempMax, + tempMin, picture); + adapter.add(entryFirst); + + String description = "no description available"; + if (forecast.getWeather().size() > 0) { + description = forecast.getWeather().get(0).getDescription(); + } + final SpecificDataEntrySecond entrySecond = new SpecificDataEntrySecond( + description); + adapter.add(entrySecond); + + + String humidityValue = ""; + if (forecast.getHumidity() != null) { + final double conversion = (Double) forecast.getHumidity(); + humidityValue = tempFormatter.format(conversion); + } + String pressureValue = ""; + if (forecast.getPressure() != null) { + final double conversion = (Double) forecast.getPressure(); + pressureValue = tempFormatter.format(conversion); + } + String windValue = ""; + if (forecast.getSpeed() != null) { + final double conversion = (Double) forecast.getSpeed(); + windValue = tempFormatter.format(conversion); + } + String rainValue = ""; + if (forecast.getRain() != null) { + final double conversion = (Double) forecast.getRain(); + rainValue = tempFormatter.format(conversion); + } + String cloudsValue = ""; + if (forecast.getRain() != null) { + final double conversion = (Double) forecast.getClouds(); + cloudsValue = tempFormatter.format(conversion); + } + final SpecificDataEntryThird entryThird = new SpecificDataEntryThird( + humidityValue, pressureValue, windValue, rainValue, cloudsValue); + adapter.add(entryThird); + + String tempDay = ""; + if (forecast.getTemp().getDay() != null) { + double conversion = (Double) forecast.getTemp().getDay(); + conversion = conversion - tempUnits; + tempDay = tempFormatter.format(conversion) + symbol; + } + String tempMorn = ""; + if (forecast.getTemp().getMorn() != null) { + double conversion = (Double) forecast.getTemp().getMorn(); + conversion = conversion - tempUnits; + tempMorn = tempFormatter.format(conversion) + symbol; + } + String tempEve = ""; + if (forecast.getTemp().getEve() != null) { + double conversion = (Double) forecast.getTemp().getEve(); + conversion = conversion - tempUnits; + tempEve = tempFormatter.format(conversion) + symbol; + } + String tempNight = ""; + if (forecast.getTemp().getNight() != null) { + double conversion = (Double) forecast.getTemp().getNight(); + conversion = conversion - tempUnits; + tempNight = tempFormatter.format(conversion) + symbol; + } + final SpecificDataEntryFourth entryFourth = new SpecificDataEntryFourth( + tempMorn, tempDay, tempEve, tempNight); + adapter.add(entryFourth); + + + // 4. Update UI. + // TODO: Why am I not doing the same as in OverviewFragment? + this.setListAdapter(adapter); + } + + @Override + public void onResume() { + super.onResume(); + + final WeatherInformationApplication application = + (WeatherInformationApplication) getActivity().getApplication(); + final Forecast forecast = application.getForecast(); + + if (forecast != null) { + this.updateUI(forecast, this.mChosenDay); + } + + // TODO: Overview is doing things with mListState... Why not here? + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataAdapter.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataAdapter.java deleted file mode 100644 index e118a3c..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataAdapter.java +++ /dev/null @@ -1,223 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ImageView; -import android.widget.TextView; -import de.example.exampletdd.R; - -public class WeatherCurrentDataAdapter extends ArrayAdapter { - private static final int FIRST = 0; - private static final int SECOND = 1; - private static final int THIRD = 2; - private static final int FOURTH = 3; - private final int[] resources; - - public WeatherCurrentDataAdapter(final Context context, final int[] resources) { - super(context, 0); - - this.resources = resources; - } - - - @Override - public View getView(final int position, final View convertView, final ViewGroup parent) { - - final View view = this.getWorkingView(position, convertView); - final int viewType = this.getItemViewType(position); - - if (viewType == FIRST) { - - final ViewFirstHolder viewHolder = this.getViewFirstHolder(view); - final WeatherCurrentDataEntryFirst entry = (WeatherCurrentDataEntryFirst) this - .getItem(position); - viewHolder.picture.setImageBitmap(entry.getPicture()); - viewHolder.tempMax.setText(entry.getTempMax()); - viewHolder.tempMin.setText(entry.getTempMin()); - } else if (viewType == SECOND) { - final ViewSecondHolder viewHolder = this.getViewSecondHolder(view); - final WeatherCurrentDataEntrySecond entry = (WeatherCurrentDataEntrySecond) this - .getItem(position); - viewHolder.weatherDescription.setText(entry.getWeatherDescription()); - } else if (viewType == THIRD) { - final ViewThirdHolder viewHolder = this.getViewThirdHolder(view); - final WeatherCurrentDataEntryThird entry = (WeatherCurrentDataEntryThird) this - .getItem(position); - viewHolder.humidityValue.setText(entry.getHumidityValue()); - viewHolder.pressureValue.setText(entry.getPressureValue()); - viewHolder.rainValue.setText(entry.getRainValue()); - viewHolder.cloudsValue.setText(entry.getCloudsValue()); - viewHolder.windValue.setText(entry.getWindValue()); - } else if (viewType == FOURTH) { - final ViewFourthHolder viewHolder = this.getViewFourthHolder(view); - final WeatherCurrentDataEntryFourth entry = (WeatherCurrentDataEntryFourth) this - .getItem(position); - viewHolder.dayTemp.setText(entry.getDayTemp()); - viewHolder.morningTemp.setText(entry.getEveTemp()); - viewHolder.eveTemp.setText(entry.getEveTemp()); - viewHolder.nightTemp.setText(entry.getNightTemp()); - } - - return view; - } - - @Override - public int getItemViewType(final int position) { - int type = 0; - - if (position == 0) { - type = FIRST; - } else if (position == 1) { - type = SECOND; - } else if (position == 2) { - type = THIRD; - } else if (position == 3) { - type = FOURTH; - } - - return type; - } - - @Override - public int getViewTypeCount() { - return 4; - } - - private View getWorkingView(final int position, final View convertView) { - View workingView = null; - - if (convertView == null) { - final int viewType = this.getItemViewType(position); - final Context context = this.getContext(); - final LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - workingView = inflater.inflate(this.resources[viewType], null); - } else { - workingView = convertView; - } - - return workingView; - } - - private ViewFirstHolder getViewFirstHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewFirstHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewFirstHolder)) { - viewHolder = new ViewFirstHolder(); - - viewHolder.picture = (ImageView) workingView - .findViewById(R.id.weather_current_data_picture); - viewHolder.tempMax = (TextView) workingView - .findViewById(R.id.weather_current_data_temp_max); - viewHolder.tempMin = (TextView) workingView - .findViewById(R.id.weather_current_data_temp_min); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewFirstHolder) tag; - } - - return viewHolder; - } - - private ViewSecondHolder getViewSecondHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewSecondHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewSecondHolder)) { - viewHolder = new ViewSecondHolder(); - - viewHolder.weatherDescription = (TextView) workingView - .findViewById(R.id.weather_current_data_description); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewSecondHolder) tag; - } - - return viewHolder; - } - - private ViewThirdHolder getViewThirdHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewThirdHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewThirdHolder)) { - viewHolder = new ViewThirdHolder(); - - viewHolder.humidityValue = (TextView) workingView - .findViewById(R.id.weather_current_data_humidity_value); - viewHolder.pressureValue = (TextView) workingView - .findViewById(R.id.weather_current_data_pressure_value); - viewHolder.rainValue = (TextView) workingView - .findViewById(R.id.weather_current_data_rain_value); - viewHolder.cloudsValue = (TextView) workingView - .findViewById(R.id.weather_current_data_clouds_value); - viewHolder.windValue = (TextView) workingView - .findViewById(R.id.weather_current_data_wind_value); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewThirdHolder) tag; - } - - return viewHolder; - } - - private ViewFourthHolder getViewFourthHolder(final View workingView) { - final Object tag = workingView.getTag(); - ViewFourthHolder viewHolder = null; - - if ((null == tag) || !(tag instanceof ViewFourthHolder)) { - viewHolder = new ViewFourthHolder(); - - viewHolder.morningTemp = (TextView) workingView - .findViewById(R.id.weather_morn_temperature); - viewHolder.dayTemp = (TextView) workingView.findViewById(R.id.weather_day_temperature); - viewHolder.eveTemp = (TextView) workingView.findViewById(R.id.weather_eve_temperature); - viewHolder.nightTemp = (TextView) workingView - .findViewById(R.id.weather_night_temperature); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewFourthHolder) tag; - } - - return viewHolder; - } - - - private static class ViewFirstHolder { - public ImageView picture; - public TextView tempMax; - public TextView tempMin; - } - - private static class ViewSecondHolder { - public TextView weatherDescription; - } - - private static class ViewThirdHolder { - public TextView humidityValue; - public TextView pressureValue; - public TextView windValue; - public TextView rainValue; - public TextView cloudsValue; - } - - private static class ViewFourthHolder { - private TextView morningTemp; - private TextView dayTemp; - private TextView eveTemp; - private TextView nightTemp; - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFirst.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFirst.java deleted file mode 100644 index 732e682..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFirst.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -import android.graphics.Bitmap; - -public class WeatherCurrentDataEntryFirst { - private final Bitmap picture; - private final String tempMax; - private final String tempMin; - - public WeatherCurrentDataEntryFirst(final String tempMax, final String tempMin, - final Bitmap picture) { - this.tempMax = tempMax; - this.tempMin = tempMin; - this.picture = picture; - } - - public Bitmap getPicture() { - return this.picture; - } - - public String getTempMax() { - return this.tempMax; - } - - public String getTempMin() { - return this.tempMin; - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFourth.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFourth.java deleted file mode 100644 index 4aa2e8d..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryFourth.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -public class WeatherCurrentDataEntryFourth { - private final String morningTemp; - private final String dayTemp; - private final String eveTemp; - private final String nightTemp; - - public WeatherCurrentDataEntryFourth(final String morningTemp, final String dayTemp, - final String eveTemp, final String nightTemp) { - this.morningTemp = morningTemp; - this.dayTemp = dayTemp; - this.eveTemp = eveTemp; - this.nightTemp = nightTemp; - } - - public String getMorningTemp() { - return this.morningTemp; - } - - public String getDayTemp() { - return this.dayTemp; - } - - public String getEveTemp() { - return this.eveTemp; - } - - public String getNightTemp() { - return this.nightTemp; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntrySecond.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntrySecond.java deleted file mode 100644 index ca6a073..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntrySecond.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -public class WeatherCurrentDataEntrySecond { - private final String weatherDescription; - - public WeatherCurrentDataEntrySecond(final String weatherDescription) { - this.weatherDescription = weatherDescription; - } - - public String getWeatherDescription() { - return this.weatherDescription; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryThird.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryThird.java deleted file mode 100644 index 62d2f01..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherCurrentDataEntryThird.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -public class WeatherCurrentDataEntryThird { - private final String humidityValue; - private final String pressureValue; - private final String windValue; - private final String rainValue; - private final String cloudsValue; - - public WeatherCurrentDataEntryThird(final String humidityValue, final String pressureValue, - final String windValue, final String rainValue, final String cloudsValue) { - this.humidityValue = humidityValue; - this.pressureValue = pressureValue; - this.windValue = windValue; - this.rainValue = rainValue; - this.cloudsValue = cloudsValue; - } - - public String getHumidityValue() { - return this.humidityValue; - } - - public String getPressureValue() { - return this.pressureValue; - } - - public String getWindValue() { - return this.windValue; - } - - public String getRainValue() { - return this.rainValue; - } - - public String getCloudsValue() { - return this.cloudsValue; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java deleted file mode 100644 index b38ce57..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java +++ /dev/null @@ -1,254 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -import java.io.IOException; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -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.DialogFragment; -import android.support.v4.app.ListFragment; -import android.widget.ListView; -import de.example.exampletdd.R; -import de.example.exampletdd.fragment.ErrorDialogFragment; -import de.example.exampletdd.fragment.overview.IconsList; -import de.example.exampletdd.model.forecastweather.Forecast; -import de.example.exampletdd.service.ServicePersistenceStorage; - -public class WeatherInformationSpecificDataFragment extends ListFragment { - private boolean mIsFahrenheit; - private int mChosenDay; - private ServicePersistenceStorage mWeatherServicePersistenceFile; - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Bundle extras = this.getActivity().getIntent().getExtras(); - - if (extras != null) { - this.mChosenDay = extras.getInt("CHOSEN_DAY", 0); - } else { - this.mChosenDay = 0; - } - - this.mWeatherServicePersistenceFile = new ServicePersistenceStorage( - this.getActivity()); - } - - @Override - public void onActivityCreated(final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - final ListView listWeatherView = this.getListView(); - listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE); - - this.setEmptyText("No data available"); - - this.setListShownNoAnimation(false); - - if (savedInstanceState != null) { - // Restore state - final Forecast forecastWeatherData = (Forecast) savedInstanceState - .getSerializable("ForecastWeatherData"); - - if (forecastWeatherData != null) { - try { - this.mWeatherServicePersistenceFile - .storeForecastWeatherData(forecastWeatherData); - } catch (final IOException e) { - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(this.getFragmentManager(), "errorDialog"); - } - } - - this.mChosenDay = savedInstanceState.getInt("Chosen day"); - } - } - - @Override - public void onSaveInstanceState(final Bundle savedInstanceState) { - - // Save state - final Forecast forecastWeatherData = this.mWeatherServicePersistenceFile - .getForecastWeatherData(); - - if (forecastWeatherData != null) { - savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData); - } - - savedInstanceState.putInt("Chosend day", this.mChosenDay); - - super.onSaveInstanceState(savedInstanceState); - } - - public void getWeatherByDay(final int chosenDay) { - - final Forecast forecastWeatherData = this.mWeatherServicePersistenceFile - .getForecastWeatherData(); - if (forecastWeatherData != null) { - this.updateForecastWeatherData(forecastWeatherData, chosenDay); - } - - } - - - public void updateForecastWeatherData(final Forecast forecastWeatherData, - final int chosenDay) { - final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); - tempFormatter.applyPattern("#####.#####"); - final double tempUnits = this.mIsFahrenheit ? 0 : 273.15; - final String symbol = this.mIsFahrenheit ? "ºF" : "ºC"; - - - final int[] layouts = new int[4]; - layouts[0] = R.layout.weather_current_data_entry_first; - layouts[1] = R.layout.weather_current_data_entry_second; - layouts[2] = R.layout.weather_current_data_entry_third; - layouts[3] = R.layout.weather_current_data_entry_fourth; - final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(), - layouts); - - - final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData - .getList().get((chosenDay)); - - final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US); - final Calendar calendar = Calendar.getInstance(); - final Long forecastUNIXDate = (Long) forecast.getDt(); - calendar.setTimeInMillis(forecastUNIXDate * 1000L); - final Date date = calendar.getTime(); - this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase()); - - - String tempMax = ""; - if (forecast.getTemp().getMax() != null) { - double conversion = (Double) forecast.getTemp().getMax(); - conversion = conversion - tempUnits; - tempMax = tempFormatter.format(conversion) + symbol; - } - String tempMin = ""; - if (forecast.getTemp().getMin() != null) { - double conversion = (Double) forecast.getTemp().getMin(); - conversion = conversion - tempUnits; - tempMin = tempFormatter.format(conversion) + symbol; - } - Bitmap picture; - if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null) - && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) { - final String icon = forecast.getWeather().get(0).getIcon(); - picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon) - .getResourceDrawable()); - } else { - picture = BitmapFactory.decodeResource(this.getResources(), - R.drawable.weather_severe_alert); - } - final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax, - tempMin, picture); - adapter.add(entryFirst); - - String description = "no description available"; - if (forecast.getWeather().size() > 0) { - description = forecast.getWeather().get(0).getDescription(); - } - final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond( - description); - adapter.add(entrySecond); - - - String humidityValue = ""; - if (forecast.getHumidity() != null) { - final double conversion = (Double) forecast.getHumidity(); - humidityValue = tempFormatter.format(conversion); - } - String pressureValue = ""; - if (forecast.getPressure() != null) { - final double conversion = (Double) forecast.getPressure(); - pressureValue = tempFormatter.format(conversion); - } - String windValue = ""; - if (forecast.getSpeed() != null) { - final double conversion = (Double) forecast.getSpeed(); - windValue = tempFormatter.format(conversion); - } - String rainValue = ""; - if (forecast.getRain() != null) { - final double conversion = (Double) forecast.getRain(); - rainValue = tempFormatter.format(conversion); - } - String cloudsValue = ""; - if (forecast.getRain() != null) { - final double conversion = (Double) forecast.getClouds(); - cloudsValue = tempFormatter.format(conversion); - } - final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird( - humidityValue, pressureValue, windValue, rainValue, cloudsValue); - adapter.add(entryThird); - - String tempDay = ""; - if (forecast.getTemp().getDay() != null) { - double conversion = (Double) forecast.getTemp().getDay(); - conversion = conversion - tempUnits; - tempDay = tempFormatter.format(conversion) + symbol; - } - String tempMorn = ""; - if (forecast.getTemp().getMorn() != null) { - double conversion = (Double) forecast.getTemp().getMorn(); - conversion = conversion - tempUnits; - tempMorn = tempFormatter.format(conversion) + symbol; - } - String tempEve = ""; - if (forecast.getTemp().getEve() != null) { - double conversion = (Double) forecast.getTemp().getEve(); - conversion = conversion - tempUnits; - tempEve = tempFormatter.format(conversion) + symbol; - } - String tempNight = ""; - if (forecast.getTemp().getNight() != null) { - double conversion = (Double) forecast.getTemp().getNight(); - conversion = conversion - tempUnits; - tempNight = tempFormatter.format(conversion) + symbol; - } - final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth( - tempMorn, tempDay, tempEve, tempNight); - adapter.add(entryFourth); - - this.setListAdapter(adapter); - } - - @Override - public void onResume() { - super.onResume(); - - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(this.getActivity()); - - // 1. Update units of measurement. - final String keyPreference = this.getResources().getString( - R.string.weather_preferences_units_key); - final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); - final String celsius = this.getResources().getString( - R.string.weather_preferences_units_celsius); - if (unitsPreferenceValue.equals(celsius)) { - this.mIsFahrenheit = false; - } else { - this.mIsFahrenheit = true; - } - - - // 2. Update weather data on display. - final Forecast forecastWeatherData = this.mWeatherServicePersistenceFile - .getForecastWeatherData(); - if (forecastWeatherData != null) { - this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay); - } - } -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataAdapter.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataAdapter.java deleted file mode 100644 index a354c92..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataAdapter.java +++ /dev/null @@ -1,91 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TextView; -import de.example.exampletdd.R; - -public class WeatherSpecificDataAdapter extends ArrayAdapter { - private final int resource; - - public WeatherSpecificDataAdapter(final Context context, final int resource) { - super(context, 0); - - this.resource = resource; - } - - @Override - public View getView(final int position, final View convertView, - final ViewGroup parent) { - - // We need to get the best view (re-used if possible) and then - // retrieve its corresponding ViewHolder, which optimizes lookup - // efficiency - final View view = this.getWorkingView(convertView); - final ViewHolder viewHolder = this.getViewHolder(view); - final WeatherSpecificDataEntry entry = this.getItem(position); - - - // Setting the text view - viewHolder.headerView.setText(entry.getHeader()); - viewHolder.bodyView.setText(entry.getBody()); - - - return view; - } - - private View getWorkingView(final View convertView) { - // The workingView is basically just the convertView re-used if possible - // or inflated new if not possible - View workingView = null; - - if(null == convertView) { - final Context context = this.getContext(); - final LayoutInflater inflater = (LayoutInflater)context.getSystemService - (Context.LAYOUT_INFLATER_SERVICE); - - workingView = inflater.inflate(this.resource, null); - } else { - workingView = convertView; - } - - return workingView; - } - - private ViewHolder getViewHolder(final View workingView) { - // The viewHolder allows us to avoid re-looking up view references - // Since views are recycled, these references will never change - final Object tag = workingView.getTag(); - ViewHolder viewHolder = null; - - - if((null == tag) || !(tag instanceof ViewHolder)) { - viewHolder = new ViewHolder(); - - viewHolder.headerView = (TextView) workingView - .findViewById(R.id.weather_data_entry_header); - viewHolder.bodyView = (TextView) workingView - .findViewById(R.id.weather_data_entry_body); - - workingView.setTag(viewHolder); - - } else { - viewHolder = (ViewHolder) tag; - } - - return viewHolder; - } - - /** - * ViewHolder allows us to avoid re-looking up view references - * Since views are recycled, these references will never change - */ - private static class ViewHolder { - public TextView headerView; - public TextView bodyView; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataEntry.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataEntry.java deleted file mode 100644 index 2d5f492..0000000 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherSpecificDataEntry.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.example.exampletdd.fragment.specific; - -public class WeatherSpecificDataEntry { - private final String header; - private final String body; - - public WeatherSpecificDataEntry(final String header, final String body) { - this.header = header; - this.body = body; - } - - public String getHeader() { - return this.header; - } - - public String getBody() { - return this.body; - } - -} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/service/IconsList.java b/Android/WeatherInformation/src/de/example/exampletdd/service/IconsList.java new file mode 100644 index 0000000..ea53085 --- /dev/null +++ b/Android/WeatherInformation/src/de/example/exampletdd/service/IconsList.java @@ -0,0 +1,143 @@ +package de.example.exampletdd.service; + +import java.util.HashMap; +import java.util.Map; + +import de.example.exampletdd.R; + +public enum IconsList { + ICON_01d("01d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_clear; + } + }, + ICON_01n("01n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_clear_night; + } + }, + ICON_02d("02d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_few_clouds; + } + }, + ICON_02n("02n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_few_clouds_night; + } + }, + ICON_03d("03d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_few_clouds; + } + }, + ICON_03n("03n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_few_clouds; + } + }, + ICON_04d("04d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_overcast; + } + }, + ICON_04n("04n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_overcast; + } + }, + ICON_09d("09d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_showers; + } + }, + ICON_09n("09n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_showers; + } + }, + ICON_10d("10d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_showers_scattered; + } + }, + ICON_10n("10n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_showers_scattered; + } + }, + ICON_11d("11d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_storm; + } + }, + ICON_11n("11n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_storm; + } + }, + ICON_13d("13d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_snow; + } + }, + ICON_13n("13n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_snow; + } + }, + ICON_50d("50d") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_fog; + } + }, + ICON_50n("50n") { + @Override + public int getResourceDrawable() { + return R.drawable.weather_fog; + } + }; + + private final String icon; + // Map with every enum constant. Class variable initializer. JLS§12.4.2 + // Executed in textual order. + private static final Map codeMap = new HashMap(); + + // Static initializer. JLS§12.4.2 Executed in textual order. + static { + for (final IconsList code : IconsList.values()) { + codeMap.put(code.getIcon(), code); + } + } + + private IconsList(final String icon) { + this.icon = icon; + } + + public static final IconsList getIcon(final String icon) { + return codeMap.get(icon); + } + + private String getIcon() { + return this.icon; + } + + public abstract int getResourceDrawable(); +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/service/ServiceParser.java b/Android/WeatherInformation/src/de/example/exampletdd/service/ServiceParser.java index 3b68340..e05a67f 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/service/ServiceParser.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/service/ServiceParser.java @@ -17,17 +17,17 @@ public class ServiceParser { this.JPOSParser = JPOSWeatherParser; } - public Current retrieveCurrentWeatherDataFromJPOS(final String jsonData) + public Current retrieveCurrentFromJPOS(final String jsonData) throws JsonParseException, IOException { return this.JPOSParser.retrieveCurrenFromJPOS(jsonData); } - public Forecast retrieveForecastWeatherDataFromJPOS(final String jsonData) + public Forecast retrieveForecastFromJPOS(final String jsonData) throws JsonParseException, IOException { return this.JPOSParser.retrieveForecastFromJPOS(jsonData); } - public String createURIAPIForecastWeather(final String urlAPI, final String APIVersion, + public String createURIAPIForecast(final String urlAPI, final String APIVersion, final double latitude, final double longitude, final String resultsNumber) { final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); @@ -40,7 +40,7 @@ public class ServiceParser { return formatURIAPI.format(values); } - public String createURIAPITodayWeather(final String urlAPI, final String APIVersion, + public String createURIAPICurrent(final String urlAPI, final String APIVersion, final double latitude, final double longitude) { final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US);