Map display not quite working
authorNeil Smith <neil.git@njae.me.uk>
Mon, 27 Oct 2014 19:01:20 +0000 (19:01 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 27 Oct 2014 19:01:20 +0000 (19:01 +0000)
app/src/main/java/uk/me/njae/sunshine/ForecastFragment.java
app/src/main/java/uk/me/njae/sunshine/SettingsActivity.java
app/src/main/res/menu/forecastfragment.xml
app/src/main/res/values/arrays.xml [new file with mode: 0644]
app/src/main/res/values/strings.xml
app/src/main/res/values/strings_activity_settings.xml
app/src/main/res/xml/pref_general.xml

index 7a52d35612768eb3b4e359c313260e0d4b73a5b4..b6a667ad4485614f3e2be437050887c27c4d6d50 100644 (file)
@@ -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<String> 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<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);
@@ -109,6 +141,11 @@ public class ForecastFragment extends Fragment {
         return rootView;
     }
 
+    public void onStart() {
+        super.onStart();
+        updateWeather();
+    }
+
     public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
 
         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;
         }
index 59075986424b48d9174283bd3a6d5ae29745729e..4fe29fbe8a73b7b87f9870d868385a4c97309c78 100644 (file)
@@ -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
index 48c679508b90ce0eb5fbfab4fa7d4d0a0fd846ac..2b6caaae190e3cd8ff60635b7da8e8650c7e4ec5 100644 (file)
@@ -2,6 +2,10 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     tools:context=".MainActivity" >
+    <item android:id="@+id/action_show_location"
+        android:title="@string/action_show_location"
+        android:orderInCategory="50"
+        app:showAsAction="never" />
     <item android:id="@+id/action_refresh"
         android:title="@string/action_refresh"
         android:orderInCategory="50"
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
new file mode 100644 (file)
index 0000000..817a855
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<resources>
+
+<string-array name="pref_units_labels">
+    <item>@string/pref_units_label_metric</item>
+    <item>@string/pref_units_label_imperial</item>
+</string-array>
+
+<string-array name="pref_units_values">
+    <item>@string/pref_units_metric</item>
+    <item>@string/pref_units_imperial</item>
+</string-array>
+</resources>
\ No newline at end of file
index f39f245c787de15815a593f37b2cef8543a10eb9..ecce75ae1436b08f191311646458d9e23102390d 100644 (file)
@@ -5,6 +5,7 @@
     <string name="hello_world">Hello world!</string>
     <string name="action_settings">Settings</string>
     <string name="action_refresh">Refresh</string>
+    <string name="action_show_location">Show location</string>
     <string name="title_activity_detail">DetailActivity</string>
 
 
index 1d0206777239f10e2d2da6454ecda7afa5bdd8d4..2ef87477e603a0cadab78816bb8b25d89797edee 100644 (file)
@@ -6,29 +6,20 @@
 
     <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 &amp; sync</string>
index 7b55d11311fc43353ef296256cc5e4e323904acf..d8e9c673abebf03e607fd6e40041887c0440ba45 100644 (file)
@@ -3,12 +3,6 @@
     android:layout_height="match_parent"
     >
 
-    <CheckBoxPreference
-        android:key="example_checkbox"
-        android:title="@string/pref_title_social_recommendations"
-        android:summary="@string/pref_description_social_recommendations"
-        android:defaultValue="true" />
-
     <!-- NOTE: EditTextPreference accepts EditText attributes. -->
     <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
     <EditTextPreference
          dismiss it. -->
     <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
     <ListPreference
-        android:key="example_list"
-        android:title="@string/pref_title_add_friends_to_messages"
-        android:defaultValue="-1"
-        android:entries="@array/pref_example_list_titles"
-        android:entryValues="@array/pref_example_list_values"
+        android:key="@string/pref_units_key"
+        android:title="@string/pref_units_title"
+        android:defaultValue="@string/pref_units_default"
+        android:entries="@array/pref_units_labels"
+        android:entryValues="@array/pref_units_values"
+        android:summary="%s"
         android:negativeButtonText="@null"
         android:positiveButtonText="@null" />