package uk.me.njae.sunshine;
import android.content.Intent;
+import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
+import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
*/
public class ForecastFragment extends Fragment {
+ private final String LOG_TAG = ForecastFragment.class.getSimpleName();
+
private ArrayAdapter<String> mForecastAdapter;
public ForecastFragment() {
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
- FetchWeatherTask weatherTask = new FetchWeatherTask();
- weatherTask.execute("2642465");
+ updateWeather();
return true;
}
+ if (id == R.id.action_show_location) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
+ String location = preferences.getString(getString(R.string.pref_location_key),
+ getString(R.string.pref_location_default));
+ Intent intent = new Intent(Intent.ACTION_VIEW);
+ Uri geoLocation;
+ try {
+ geoLocation = Uri.parse("geo:0,0?q=" + URLEncoder.encode(location, "UTF-8"));
+ intent.setData(geoLocation);
+ } catch (UnsupportedEncodingException e) {
+ Log.e(LOG_TAG, "Error ", e);
+ }
+ if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
+ startActivity(intent);
+ }
+
+ }
return super.onOptionsItemSelected(item);
}
+ private void updateWeather() {
+ FetchWeatherTask weatherTask = new FetchWeatherTask();
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
+ String location = preferences.getString(getString(R.string.pref_location_key),
+ getString(R.string.pref_location_default));
+ // weatherTask.execute("2642465");
+ weatherTask.execute(location);
+ }
+
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
- String[] forecastArray = {
- "Today - Sunny - 10/10",
- "Tomorrow - Cloudy - 11/11",
- "Tuesday - Snow - 12/12",
- "Wednesday - Rain - 13/13",
- "Thursday - Hail - 14/14",
- "Friday - Scorchio - 15/15",
- "Saturday - Fog - 16/16"
- };
-
- List<String> weekForecast = new ArrayList<String>(
- Arrays.asList(forecastArray));
+// String[] forecastArray = {
+// "Today - Sunny - 10/10",
+// "Tomorrow - Cloudy - 11/11",
+// "Tuesday - Snow - 12/12",
+// "Wednesday - Rain - 13/13",
+// "Thursday - Hail - 14/14",
+// "Friday - Scorchio - 15/15",
+// "Saturday - Fog - 16/16"
+// };
+//
+// List<String> weekForecast = new ArrayList<String>(
+// Arrays.asList(forecastArray));
+
mForecastAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
- weekForecast
+ new ArrayList<String>() // weekForecast
);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
+ public void onStart() {
+ super.onStart();
+ updateWeather();
+ }
+
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
- final String QUERY_PARAM = "id";
+ // final String QUERY_PARAM = "id"; // use this if using a location ID
+ final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
* Prepare the weather high/lows for presentation.
*/
private String formatHighLows(double high, double low) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
+ String units = preferences.getString(getString(R.string.pref_units_key),
+ getString(R.string.pref_units_default));
// For presentation, assume the user doesn't care about tenths of a degree.
long roundedHigh = Math.round(high);
long roundedLow = Math.round(low);
+ if (units.equals(getString(R.string.pref_units_imperial))) {
+ roundedHigh = Math.round(high * 9 / 5 + 32);
+ roundedLow = Math.round(low * 9 / 5 + 32);
+ }
+
String highLowStr = roundedHigh + "/" + roundedLow;
return highLowStr;
}
<string name="pref_location_label">Location</string>
<string name="pref_location_default" translatable="false">Milton Keynes</string>
+ <!--<string name="pref_location_default" translatable="false">642465</string> -->
<string name="pref_location_key">location</string>
<!-- Example General settings -->
<string name="pref_header_general">General</string>
- <string name="pref_title_social_recommendations">Enable social recommendations</string>
- <string name="pref_description_social_recommendations">Recommendations for people to contact based on your message history</string>
-
- <string name="pref_title_display_name">Display name</string>
- <string name="pref_default_display_name">John Smith</string>
-
- <string name="pref_title_add_friends_to_messages">Add friends to messages</string>
- <string-array name="pref_example_list_titles">
- <item>Always</item>
- <item>When possible</item>
- <item>Never</item>
- </string-array>
- <string-array name="pref_example_list_values">
- <item>1</item>
- <item>0</item>
- <item>-1</item>
- </string-array>
+ <string name="pref_units_title">Temperature units</string>
+ <string name="pref_units_key">units</string>
+ <string name="pref_units_label_metric">Metric</string>
+ <string name="pref_units_label_imperial">Imperial</string>
+ <string name="pref_units_metric">metric</string>
+ <string name="pref_units_imperial">imperial</string>
+ <string name="pref_units_default">metric</string>
<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data & sync</string>