From: Neil Smith Date: Mon, 27 Oct 2014 19:01:20 +0000 (+0000) Subject: Map display not quite working X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=63521d0a7ff37d2f0239be1af3c675cfd80e9095;p=Sunshine.git Map display not quite working --- diff --git a/app/src/main/java/uk/me/njae/sunshine/ForecastFragment.java b/app/src/main/java/uk/me/njae/sunshine/ForecastFragment.java index 7a52d35..b6a667a 100644 --- a/app/src/main/java/uk/me/njae/sunshine/ForecastFragment.java +++ b/app/src/main/java/uk/me/njae/sunshine/ForecastFragment.java @@ -1,9 +1,11 @@ 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; @@ -25,8 +27,10 @@ import java.io.BufferedReader; 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; @@ -38,6 +42,8 @@ import java.util.List; */ public class ForecastFragment extends Fragment { + private final String LOG_TAG = ForecastFragment.class.getSimpleName(); + private ArrayAdapter mForecastAdapter; public ForecastFragment() { @@ -62,34 +68,60 @@ public class ForecastFragment extends Fragment { // 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 weekForecast = new ArrayList( - 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 weekForecast = new ArrayList( +// Arrays.asList(forecastArray)); + mForecastAdapter = new ArrayAdapter( getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, - weekForecast + new ArrayList() // weekForecast ); View rootView = inflater.inflate(R.layout.fragment_main, container, false); @@ -109,6 +141,11 @@ public class ForecastFragment extends Fragment { return rootView; } + public void onStart() { + super.onStart(); + updateWeather(); + } + public class FetchWeatherTask extends AsyncTask { private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); @@ -147,7 +184,8 @@ public class ForecastFragment extends Fragment { // 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"; @@ -235,10 +273,18 @@ public class ForecastFragment extends Fragment { * 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; } diff --git a/app/src/main/java/uk/me/njae/sunshine/SettingsActivity.java b/app/src/main/java/uk/me/njae/sunshine/SettingsActivity.java index 5907598..4fe29fb 100644 --- a/app/src/main/java/uk/me/njae/sunshine/SettingsActivity.java +++ b/app/src/main/java/uk/me/njae/sunshine/SettingsActivity.java @@ -26,6 +26,8 @@ public class SettingsActivity extends PreferenceActivity // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be // updated when the preference changes. bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key))); + bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key))); + } /** @@ -47,7 +49,7 @@ public class SettingsActivity extends PreferenceActivity @Override public boolean onPreferenceChange(Preference preference, Object value) { - String stringValue = value.toString(); + String stringValue = value.toString().trim(); if (preference instanceof ListPreference) { // For list preferences, look up the correct display value in diff --git a/app/src/main/res/menu/forecastfragment.xml b/app/src/main/res/menu/forecastfragment.xml index 48c6795..2b6caaa 100644 --- a/app/src/main/res/menu/forecastfragment.xml +++ b/app/src/main/res/menu/forecastfragment.xml @@ -2,6 +2,10 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" > + + + + + + @string/pref_units_label_metric + @string/pref_units_label_imperial + + + + @string/pref_units_metric + @string/pref_units_imperial + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f39f245..ecce75a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,7 @@ Hello world! Settings Refresh + Show location DetailActivity diff --git a/app/src/main/res/values/strings_activity_settings.xml b/app/src/main/res/values/strings_activity_settings.xml index 1d02067..2ef8747 100644 --- a/app/src/main/res/values/strings_activity_settings.xml +++ b/app/src/main/res/values/strings_activity_settings.xml @@ -6,29 +6,20 @@ Location Milton Keynes + location General - Enable social recommendations - Recommendations for people to contact based on your message history - - Display name - John Smith - - Add friends to messages - - Always - When possible - Never - - - 1 - 0 - -1 - + Temperature units + units + Metric + Imperial + metric + imperial + metric Data & sync diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 7b55d11..d8e9c67 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -3,12 +3,6 @@ android:layout_height="match_parent" > - -