Map display not quite working
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / ForecastFragment.java
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;
         }