From 12ab5412233728baf7124fcf83abc5720b8b4769 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Wed, 2 Apr 2014 05:33:00 +0200 Subject: [PATCH] No time for comments --- AndroidManifest.xml | 5 +- res/drawable-hdpi/ic_action_search.png | Bin 0 -> 650 bytes res/drawable-mdpi/ic_action_search.png | Bin 0 -> 449 bytes res/drawable-xhdpi/ic_action_search.png | Bin 0 -> 827 bytes res/drawable-xxhdpi/ic_action_search.png | Bin 0 -> 1152 bytes res/layout/activity_main.xml | 2 +- res/layout/weather_data_entry_list.xml | 23 +++ res/layout/weather_data_list.xml | 26 +++ res/menu/weather_main_menu.xml | 4 +- res/values/strings.xml | 18 +- .../exampletdd/WeatherInformationActivity.java | 8 +- .../exampletdd/fragment/WeatherDataAdapter.java | 91 ++++++++ .../exampletdd/fragment/WeatherDataEntry.java | 20 ++ .../fragment/WeatherInformationDataFragment.java | 85 ++++---- .../WeatherInformationDataFragmentDeprecated.java | 228 +++++++++++++++++++++ 15 files changed, 446 insertions(+), 64 deletions(-) create mode 100644 res/drawable-hdpi/ic_action_search.png create mode 100644 res/drawable-mdpi/ic_action_search.png create mode 100644 res/drawable-xhdpi/ic_action_search.png create mode 100644 res/drawable-xxhdpi/ic_action_search.png create mode 100644 res/layout/weather_data_entry_list.xml create mode 100644 res/layout/weather_data_list.xml create mode 100644 src/de/example/exampletdd/fragment/WeatherDataAdapter.java create mode 100644 src/de/example/exampletdd/fragment/WeatherDataEntry.java create mode 100644 src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a0f5d7b..8ae62c1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -15,11 +15,11 @@ @@ -29,9 +29,8 @@ + android:parentActivityName="de.example.exampletdd.WeatherInformationActivity"> diff --git a/res/drawable-hdpi/ic_action_search.png b/res/drawable-hdpi/ic_action_search.png new file mode 100644 index 0000000000000000000000000000000000000000..772e3598edb5441a68bb184f9c85e17109ebd6ee GIT binary patch literal 650 zcmV;50(Jd~P)17hKoI8QGX!~*m^_#PXe04cx%%--ZTFyA14Gmmwu;gv|$2 zxQ0c1*h&;@xVvikvml365O(6)A?9dQQ(07}_)*z$d0h1^~N?6?9btxc>Su#Z)2YWI$PL8H2%B-;qg5Cv_Au0jcmS*c76rF1PHD=qcO#5~|BiWF^iQr6!iVZ(}^ zM;hb3iwUjnM5gxke7I33GBwOD%Ciif^)Mr(y#ruCmdd1ci*m&L2_CEpy-ky5vCXMO z(Gt(lm`xSyN=~_Dv8PiI!Wo#5)X|M4wLqBYh3-|a6@i?O8Gud;p$$S90N_BmmB!M$ z;ba6rO%RJ)hhfI%0a$jUvD6)<2Q)n)4-#HSJ(1n<#Kw6PlAcfwzJiCFZ*h9(JO7F7 kpL`a81z-V~r5^zX03bgQxSV;wqW}N^07*qoM6N<$f~DIU2><{9 literal 0 HcmV?d00001 diff --git a/res/drawable-mdpi/ic_action_search.png b/res/drawable-mdpi/ic_action_search.png new file mode 100644 index 0000000000000000000000000000000000000000..4edb1ff92fe4c88a984a299ae6ff643cb6e0cbf2 GIT binary patch literal 449 zcmV;y0Y3hTP)6(JFc6ISQ^2_Z8gK&? zKnh3$Pyh{301bo!NCCP3<^H4r7XSsgfG{hqB`d-vmX_Q-veIaUz4DA5@0g-kVu?Qv zHKk}628~n!8$k2$9q52xrPTdn0ziIb@>n?F0uhb40Fc*6hJO-43nI&NG+qKbp8v+& zN$lIu&Vd{718fp?hHI#{k|Ni<;4MWy65++JbGTNKalezsA9?aSD%B$qhxy)G0`#~?&hQ7DWg!+qj)|D%1X3cJ rV$Ps%4xE82Ipy;Ie~Et^PXPu1!=KTV_gtK200000NkvXXu0mjf`(nR7 literal 0 HcmV?d00001 diff --git a/res/drawable-xhdpi/ic_action_search.png b/res/drawable-xhdpi/ic_action_search.png new file mode 100644 index 0000000000000000000000000000000000000000..19658e4a2acbe30df1aeb743ba5ac5e16a5edd21 GIT binary patch literal 827 zcmV-B1H}A^P))F4ccon= z%S0lPNF)-8M88uiMW>g`Wde^Gv^o6>KYu~1pq-(elv3sB0Kmi-@VJMzXz@CNI6~V4 zjMJb2z{GK-H_)bmT@=vX07Nx705JJAJeCcwp}FS`fY2gd0YWhd0OaRe!_Vy%Me{E0 zRfX^A*zkA-2txQ60)VZ<*Grl})zNi&gP`q(K{z<_bpcCl9s z`rTL%YIGCm_nZ>IUBGuXj+O6huF%JZVu#kWMR4sG0Ibr3<*#Lk+5ezd+6}apKBO)E(gmB7F;Y~X8n9j5|zKL!0da@1;E^~=ciKX zI6Sf01@ua#Jh$6@PqyYy60G#^@OpbtCa{zE^7-T8{=ad1r zH2})YOe=UyF(9z4+yZdq;sW+yrj!}R){~>S!Un+J&J1HFD+KhvlsI*X%nJZjZ$GWw zU>#}l6Gy83&MF-?24%*Adn#sho&3&`YJac;KoQwagAu|u#2v`&_l{(%&U#Rnq9_(e zUL0oyOlJ>Gr7Vi}PYwD0Csm600r&xe263dUu5#@0bB}XGQV20FM!Mb#nJI96#UgMg z$jMFcz2{iP(pXnx==)1*zHtC2{jLL^qdI);RJw`*A@=2{uHh{jOkBH2;o9f>grHIe z7)qFIi>TY2?XTQCMTd|SKu374tuZ=;yNs`99`Ef!kK~*Il%BAHAaDloocYxrgCHoz z0L&c0r~2d-6F6Idz9d-%Si*7)F3<%+J{kal5Xw;j@Ptrj9%Zz_0B8vT|GxG)6b%YM z1B9BQzt^>$$_6SU1I@|ji`4&5Boc{4B9TZW8k7DBFaWjb9TtHJeu)48002ovPDHLk FV1n6#bKn2~ literal 0 HcmV?d00001 diff --git a/res/drawable-xxhdpi/ic_action_search.png b/res/drawable-xxhdpi/ic_action_search.png new file mode 100644 index 0000000000000000000000000000000000000000..a10863887e448e59e0eb34d7d08eb5f273498628 GIT binary patch literal 1152 zcmV-`1b_R9P)No+02}}-E$v#0vapRMt1bOs<(e91z^0$N)!mjH zMGyo*5ClOG1VIo4K@bE%5M*63r;EAWZi)OcreA0BDC8mN_dJf{ZvZfBaAo8<89Qn4 z76s%=$CnR4Vh2W7BRrMIQXZ3zAE!W^B#0{jG&g+u5N1qCg{i|3LBu^7Uxk>Rfe~M) zi#+xcL;(Q*jlZE$-&DCgo@0B`!fVv~Hxhyf8UTeHW&A}0gQ<~wc@;wB=S^v{{-BAw zZnFL@KkpnQ05bluCbM~a0&Ov$?72g*1z`7L|<6>yvBE5F0kcj6zS9{zxP2ELYJ-4Fn<2{gWerk>R+%b7A~{Bk7*?tZBqQ zE0Nc?@)`hzovJ+lV%zu)A*7T^`3K-3E2*4tdyT1g2&alal@qp?9-vDHom8droEfFL z&sxx6@eaVUD$h5OYIe{`^ZLRo0F?PmS{R@&FSu&FVCkC&RJ8?iCdE&t$%v*t0hnnD zF84*wJp>jFuX(Tl60QKsxv1i`i5CE<`qaf=E)qU_KvhbYGfTBu-zBpFUMYDbutm zR*sB6)p%geZIPA{0MvIE8ZVeT_+Gk2TBnGaOcr7XoiTK6YfGblqe(rl9M_2&13<2Q z(S)L79}wg@mMI*b!-~~jr^@{j^essz$gG(1GLdp$0dZ+u;Eqf<5-jS zF8U0Et!!p76l`mXx6YgE2<9qkwtGemiZwS+u(?B~4nhecYqNlhQp`p4-Y!da5UlPI zqeWZ48jR3#|F6&#b!AVd>3-tDKKIKVHKHOAe+<3JwKgm5D=U^D}tJIifqoIfq)VgGOP0Auzv!pl2vf?5jK7N5@=0;|W>ScE*MM1Wn!G>XE|{LS}SZ=r+& zpv!7#s}Mo~&{ftTgaV-83Lz8#C$13UKmc?}N3CW@!2sY2At(TrApGdE89lfxsOEVR z@5~9E*1-muh8l)20RXoU1OT{$(0QS6@Re{!2pQ5)ciEAV$DpAX^qwb_|BFi4fhPzm zzzP897lJalTEk>?g*5=^A(TbdgyQ`bR)HW0f*=TjAP9mW2!bF8f(EL;0t^5du&;5r SwHQ+X0000 + tools:layout="@layout/weather_data_list" /> diff --git a/res/layout/weather_data_entry_list.xml b/res/layout/weather_data_entry_list.xml new file mode 100644 index 0000000..0f3cb7c --- /dev/null +++ b/res/layout/weather_data_entry_list.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/res/layout/weather_data_list.xml b/res/layout/weather_data_list.xml new file mode 100644 index 0000000..e10ceb3 --- /dev/null +++ b/res/layout/weather_data_list.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + diff --git a/res/menu/weather_main_menu.xml b/res/menu/weather_main_menu.xml index 72e7142..30460b2 100644 --- a/res/menu/weather_main_menu.xml +++ b/res/menu/weather_main_menu.xml @@ -1,5 +1,6 @@ + + android:showAsAction="never"> + diff --git a/res/values/strings.xml b/res/values/strings.xml index 5445332..d0c9b0c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -3,6 +3,7 @@ Weather Information Settings + City,country Get weather http://api.openweathermap.org/data/{0}/weather?lat={1}&lon={2} http://api.openweathermap.org/data/{0}/weather?q={1} @@ -12,12 +13,17 @@ City name Latitude Longitude - Sun rise time - Sun set time - Weather description - Temperature - Min temperature - Max temperature + Sun rise time: + Sun set time: + Weather description: + Temperature: + Min temperature: + Max temperature: + Cloudiness: + Rain time: + Rain amount: + Wind speed: + Humidity in %: OK Connection error timeout Impossible to receive weather data. diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index e3c53c4..ae260ee 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -23,14 +23,14 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); - PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, - false); + PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false); final ActionBar actionBar = this.getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); - actionBar.setTitle(this.getResources().getString(R.string.header_action_bar)); + actionBar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO, ActionBar.DISPLAY_USE_LOGO); + // actionBar.setTitle(this.getResources().getString(R.string.header_action_bar)); + actionBar.setDisplayHomeAsUpEnabled(true); // Better using xml files? How to deal with savedInstanceState with xml files? // final WeatherDataFragment weatherDataFragment = new WeatherDataFragment(); diff --git a/src/de/example/exampletdd/fragment/WeatherDataAdapter.java b/src/de/example/exampletdd/fragment/WeatherDataAdapter.java new file mode 100644 index 0000000..71d6cb2 --- /dev/null +++ b/src/de/example/exampletdd/fragment/WeatherDataAdapter.java @@ -0,0 +1,91 @@ +package de.example.exampletdd.fragment; + +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 WeatherDataAdapter extends ArrayAdapter { + private final int resource; + + public WeatherDataAdapter(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 WeatherDataEntry 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/src/de/example/exampletdd/fragment/WeatherDataEntry.java b/src/de/example/exampletdd/fragment/WeatherDataEntry.java new file mode 100644 index 0000000..45dd979 --- /dev/null +++ b/src/de/example/exampletdd/fragment/WeatherDataEntry.java @@ -0,0 +1,20 @@ +package de.example.exampletdd.fragment; + +public class WeatherDataEntry { + private final String header; + private final String body; + + public WeatherDataEntry(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/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java index 9e47ce1..0dcf6db 100644 --- a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -6,15 +6,14 @@ import java.net.URISyntaxException; import java.net.URL; import java.text.DecimalFormat; import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.ArrayList; +import java.util.Collection; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import android.app.Fragment; import android.content.SharedPreferences; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; @@ -23,8 +22,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.EditText; -import android.widget.ImageView; +import android.widget.ListView; import de.example.exampletdd.R; import de.example.exampletdd.activityinterface.ErrorMessage; import de.example.exampletdd.activityinterface.OnClickButtons; @@ -35,35 +33,47 @@ import de.example.exampletdd.parser.JPOSWeatherParser; import de.example.exampletdd.service.WeatherService; public class WeatherInformationDataFragment extends Fragment implements OnClickButtons { + private WeatherDataAdapter mAdapter; private boolean isFahrenheit; - private EditText weatherDescription; - private EditText temperature; - private EditText maxTemperature; - private EditText minTemperature; - private EditText sunRise; - private EditText sunSet; - private ImageView imageIcon; @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { - final View rootView = inflater.inflate(R.layout.fragment_main, + final View rootView = inflater.inflate(R.layout.weather_data_list, container, false); - - this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); - this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); - this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); - this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); - this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); - this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); - this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); - return rootView; } @Override + public void onActivityCreated(final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final ListView listWeatherView = (ListView) this.getActivity().findViewById( + R.id.weather_data_list_view); + + this.mAdapter = new WeatherDataAdapter(this.getActivity(), + R.layout.weather_data_entry_list); + + final Collection entries = new ArrayList(); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_description), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_max), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_tem_min), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_rise), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_sun_set), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_cloudiness), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_time), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_rain_amount), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_wind_speed), "")); + entries.add(new WeatherDataEntry(this.getString(R.string.text_field_humidity), "")); + + this.mAdapter.addAll(entries); + listWeatherView.setAdapter(this.mAdapter); + } + + @Override public void onClickGetWeather(final View v) { final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); @@ -76,10 +86,8 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService); - final EditText cityCountry = (EditText) this.getActivity() - .findViewById(R.id.editTextCity); - weatherTask.execute(cityCountry.getText().toString()); + weatherTask.execute("London,uk"); } public void updateWeatherData(final WeatherData weatherData) { @@ -88,36 +96,15 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB final double tempUnits = this.isFahrenheit ? 0 : 273.15; if (weatherData.getWeather() != null) { - this.weatherDescription.setText(weatherData.getWeather() - .getDescription()); - double conversion = weatherData.getMain().getTemp(); - conversion = conversion - tempUnits; - this.temperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMaxTemp(); - conversion = conversion - tempUnits; - this.maxTemperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMinTemp(); - conversion = conversion - tempUnits; - this.minTemperature.setText(tempFormatter.format(conversion)); + } if (weatherData.getSystem() != null) { - long unixTime = weatherData.getSystem().getSunRiseTime(); - Date unixDate = new Date(unixTime * 1000L); - String dateFormatUnix = dateFormat.format(unixDate); - this.sunRise.setText(dateFormatUnix); - - unixTime = weatherData.getSystem().getSunSetTime(); - unixDate = new Date(unixTime * 1000L); - dateFormatUnix = dateFormat.format(unixDate); - this.sunSet.setText(dateFormatUnix); + } if (weatherData.getIconData() != null) { - final Bitmap icon = BitmapFactory.decodeByteArray( - weatherData.getIconData(), 0, - weatherData.getIconData().length); - this.imageIcon.setImageBitmap(icon); + } } diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java new file mode 100644 index 0000000..939ec12 --- /dev/null +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragmentDeprecated.java @@ -0,0 +1,228 @@ +package de.example.exampletdd.fragment; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.http.client.ClientProtocolException; +import org.json.JSONException; + +import android.app.Fragment; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.http.AndroidHttpClient; +import android.os.AsyncTask; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.ImageView; +import de.example.exampletdd.R; +import de.example.exampletdd.activityinterface.ErrorMessage; +import de.example.exampletdd.activityinterface.OnClickButtons; +import de.example.exampletdd.httpclient.WeatherHTTPClient; +import de.example.exampletdd.model.WeatherData; +import de.example.exampletdd.parser.IJPOSWeatherParser; +import de.example.exampletdd.parser.JPOSWeatherParser; +import de.example.exampletdd.service.WeatherService; + +public class WeatherInformationDataFragmentDeprecated extends Fragment implements OnClickButtons { + private boolean isFahrenheit; + private EditText weatherDescription; + private EditText temperature; + private EditText maxTemperature; + private EditText minTemperature; + private EditText sunRise; + private EditText sunSet; + private ImageView imageIcon; + + + @Override + public View onCreateView(final LayoutInflater inflater, + final ViewGroup container, final Bundle savedInstanceState) { + final View rootView = inflater.inflate(R.layout.fragment_main, + container, false); + + + this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); + this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); + this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); + this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); + this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); + this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); + this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); + + return rootView; + } + + @Override + public void onClickGetWeather(final View v) { + + final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); + final WeatherService weatherService = new WeatherService( + JPOSWeatherParser); + final AndroidHttpClient httpClient = AndroidHttpClient + .newInstance("Android Weather Information Agent"); + final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient( + httpClient); + + final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService); + + final EditText cityCountry = (EditText) this.getActivity() + .findViewById(R.id.editTextCity); + + weatherTask.execute(cityCountry.getText().toString()); + } + + public void updateWeatherData(final WeatherData weatherData) { + final DecimalFormat tempFormatter = new DecimalFormat("#####.#####"); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); + final double tempUnits = this.isFahrenheit ? 0 : 273.15; + + if (weatherData.getWeather() != null) { + this.weatherDescription.setText(weatherData.getWeather() + .getDescription()); + double conversion = weatherData.getMain().getTemp(); + conversion = conversion - tempUnits; + this.temperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMaxTemp(); + conversion = conversion - tempUnits; + this.maxTemperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMinTemp(); + conversion = conversion - tempUnits; + this.minTemperature.setText(tempFormatter.format(conversion)); + } + + if (weatherData.getSystem() != null) { + long unixTime = weatherData.getSystem().getSunRiseTime(); + Date unixDate = new Date(unixTime * 1000L); + String dateFormatUnix = dateFormat.format(unixDate); + this.sunRise.setText(dateFormatUnix); + + unixTime = weatherData.getSystem().getSunSetTime(); + unixDate = new Date(unixTime * 1000L); + dateFormatUnix = dateFormat.format(unixDate); + this.sunSet.setText(dateFormatUnix); + } + + if (weatherData.getIconData() != null) { + final Bitmap icon = BitmapFactory.decodeByteArray( + weatherData.getIconData(), 0, + weatherData.getIconData().length); + this.imageIcon.setImageBitmap(icon); + } + } + + @Override + public void onResume() { + super.onResume(); + + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this.getActivity()); + + final String unitsKey = this.getResources().getString( + R.string.weather_preferences_units_key); + final String units = sharedPreferences.getString(unitsKey, ""); + final String celsius = this.getResources().getString( + R.string.weather_preferences_units_celsius); + if (units.equals(celsius)) { + this.isFahrenheit = false; + } else { + this.isFahrenheit = true; + } + } + + public class WeatherTask extends AsyncTask { + private static final String TAG = "JSONWeatherTask"; + private final WeatherHTTPClient weatherHTTPClient; + private final WeatherService weatherService; + + public WeatherTask(final WeatherHTTPClient weatherHTTPClient, + final WeatherService weatherService) { + this.weatherHTTPClient = weatherHTTPClient; + this.weatherService = weatherService; + } + + @Override + protected WeatherData doInBackground(final Object... params) { + WeatherData weatherData = null; + + try { + weatherData = this.doInBackgroundThrowable(params); + } catch (final ClientProtocolException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final MalformedURLException e) { + Log.e(TAG, "Syntax URL exception: ", e); + } catch (final URISyntaxException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final JSONException e) { + Log.e(TAG, "WeatherService exception: ", e); + } finally { + this.weatherHTTPClient.close(); + } + + return weatherData; + } + + @Override + protected void onPostExecute(final WeatherData weatherData) { + if (weatherData != null) { + WeatherInformationDataFragmentDeprecated.this.updateWeatherData(weatherData); + } else { + ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity()) + .createErrorDialog(R.string.error_dialog_generic_error); + } + + this.weatherHTTPClient.close(); + } + + @Override + protected void onCancelled(final WeatherData weatherData) { + this.onCancelled(); + ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity()) + .createErrorDialog(R.string.error_dialog_connection_tiemout); + + this.weatherHTTPClient.close(); + } + + private WeatherData doInBackgroundThrowable(final Object... params) + throws ClientProtocolException, MalformedURLException, + URISyntaxException, IOException, JSONException { + final String cityCountry = (String) params[0]; + final String urlAPICity = WeatherInformationDataFragmentDeprecated.this.getResources() + .getString(R.string.uri_api_city); + final String APIVersion = WeatherInformationDataFragmentDeprecated.this.getResources() + .getString(R.string.api_version); + String url = this.weatherService.createURIAPICityCountry( + cityCountry, urlAPICity, APIVersion); + + + final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); + + + final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData); + + + final String icon = weatherData.getWeather().getIcon(); + final String urlAPIicon = WeatherInformationDataFragmentDeprecated.this + .getResources().getString(R.string.uri_api_icon); + url = this.weatherService.createURIAPIicon(icon, urlAPIicon); + final byte[] iconData = this.weatherHTTPClient + .retrieveDataFromAPI(new URL(url)).toByteArray(); + weatherData.setIconData(iconData); + + + return weatherData; + } + } +} -- 2.1.4