OpenWeatherMap works better with an application ID
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 22 Nov 2014 18:57:27 +0000 (19:57 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 22 Nov 2014 18:57:27 +0000 (19:57 +0100)
See: http://openweathermap.org/appid

app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java
app/src/main/java/name/gumartinm/weather/information/fragment/APIKeyNoticeDialogFragment.java [new file with mode: 0644]
app/src/main/java/name/gumartinm/weather/information/fragment/ErrorDialogFragment.java [deleted file]
app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java
app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java
app/src/main/java/name/gumartinm/weather/information/fragment/preferences/PreferencesFragment.java
app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java
app/src/main/java/name/gumartinm/weather/information/service/IconsList.java
app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java
app/src/main/res/values/strings.xml

index 7fd351c..1cd242c 100644 (file)
@@ -22,6 +22,7 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.support.v4.app.DialogFragment;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.app.FragmentManager;
@@ -31,6 +32,7 @@ import android.view.Menu;
 import android.view.MenuItem;
 
 import name.gumartinm.weather.information.R;
+import name.gumartinm.weather.information.fragment.APIKeyNoticeDialogFragment;
 import name.gumartinm.weather.information.fragment.current.CurrentFragment;
 import name.gumartinm.weather.information.fragment.overview.OverviewFragment;
 import name.gumartinm.weather.information.model.DatabaseQueries;
@@ -142,6 +144,25 @@ public class MainTabsActivity extends FragmentActivity {
     public void onResume() {
         super.onResume();
 
+        final SharedPreferences sharedPreferences = PreferenceManager
+                .getDefaultSharedPreferences(this.getApplicationContext());
+
+        final String APPID = sharedPreferences.getString(this.getString(R.string.weather_preferences_app_id_key), "");
+
+        final String noticeKeyPreference = this.getString(R.string.api_id_key_notice_preference_key);
+        final boolean notice = sharedPreferences.getBoolean(noticeKeyPreference, true);
+
+        if (notice && APPID.isEmpty()) {
+            final FragmentManager fm = this.getSupportFragmentManager();
+            final Fragment buttonsFragment = fm.findFragmentByTag("noticeDialog");
+            if (buttonsFragment == null) {
+                final DialogFragment newFragment = APIKeyNoticeDialogFragment.newInstance(R.string.api_id_key_notice_title);
+                newFragment.setRetainInstance(true);
+                newFragment.setCancelable(false);
+                newFragment.show(fm, "noticeDialog");
+            }
+        }
+
         final ActionBar actionBar = this.getActionBar();
         
         // 1. Update title.
@@ -159,8 +180,6 @@ public class MainTabsActivity extends FragmentActivity {
         }
 
         // 2. Update forecast tab text.
-        final SharedPreferences sharedPreferences = PreferenceManager
-                .getDefaultSharedPreferences(this.getApplicationContext());
         final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key);
         final String value = sharedPreferences.getString(keyPreference, "");
         String humanValue = "";
diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/APIKeyNoticeDialogFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/APIKeyNoticeDialogFragment.java
new file mode 100644 (file)
index 0000000..b177841
--- /dev/null
@@ -0,0 +1,70 @@
+/**
+ * Copyright 2014 Gustavo Martin Morcuende
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package name.gumartinm.weather.information.fragment;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.support.v4.app.DialogFragment;
+
+import name.gumartinm.weather.information.R;
+
+public class APIKeyNoticeDialogFragment extends DialogFragment {
+
+    public static APIKeyNoticeDialogFragment newInstance(final int title) {
+        final APIKeyNoticeDialogFragment frag = new APIKeyNoticeDialogFragment();
+        final Bundle args = new Bundle();
+
+        args.putInt("title", title);
+        frag.setArguments(args);
+
+        return frag;
+    }
+
+    @Override
+    public Dialog onCreateDialog(final Bundle savedInstanceState) {
+        final int title = this.getArguments().getInt("title");
+
+        return new AlertDialog.Builder(this.getActivity())
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setTitle(title)
+                .setMessage(this.getString(R.string.api_id_key_notice_message))
+                .setNegativeButton(this.getString(R.string.api_id_key_notice_cancel_button), null)
+                .setPositiveButton(this.getString(R.string.api_id_key_notice_ok_button),
+                        new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(final DialogInterface dialog, final int whichButton) {
+                            // Save response to permanent storage
+                            final SharedPreferences.Editor prefs = PreferenceManager
+                                .getDefaultSharedPreferences(getActivity().getApplicationContext()).edit();
+                                prefs.putBoolean(getActivity().getString(R.string.api_id_key_notice_preference_key), false);
+                                prefs.commit();
+                        }
+                    })
+                .create();
+    }
+    
+    @Override
+    public void onDestroyView() {
+       if (getDialog() != null && getRetainInstance()) {
+               getDialog().setDismissMessage(null);
+       }
+       super.onDestroyView();
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/ErrorDialogFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/ErrorDialogFragment.java
deleted file mode 100644 (file)
index 5cec3ef..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * Copyright 2014 Gustavo Martin Morcuende
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package name.gumartinm.weather.information.fragment;
-
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.content.DialogInterface;
-import android.os.Bundle;
-import android.support.v4.app.DialogFragment;
-
-public class ErrorDialogFragment extends DialogFragment {
-
-    public static ErrorDialogFragment newInstance(final int title) {
-        final ErrorDialogFragment frag = new ErrorDialogFragment();
-        final Bundle args = new Bundle();
-
-        args.putInt("title", title);
-        frag.setArguments(args);
-
-        return frag;
-    }
-
-    @Override
-    public Dialog onCreateDialog(final Bundle savedInstanceState) {
-        final int title = this.getArguments().getInt("title");
-
-        return new AlertDialog.Builder(this.getActivity())
-        .setIcon(android.R.drawable.ic_dialog_alert)
-        .setTitle(title)
-        .setPositiveButton(android.R.string.ok,
-                new DialogInterface.OnClickListener() {
-            @Override
-            public void onClick(final DialogInterface dialog,
-                    final int whichButton) {
-
-            }
-        }).create();
-    }
-    
-    @Override
-    public void onDestroyView() {
-       if (getDialog() != null && getRetainInstance()) {
-               getDialog().setDismissMessage(null);
-       }
-       super.onDestroyView();
-    }
-}
\ No newline at end of file
index 0d04dc4..ad67dc4 100644 (file)
@@ -422,10 +422,16 @@ public class CurrentFragment extends Fragment {
 
         private Current doInBackgroundThrowable(final double latitude, final double longitude)
                         throws URISyntaxException, ClientProtocolException, JsonParseException, IOException {
-
-               final String APIVersion = localContext.getResources().getString(R.string.api_version);
-            final String urlAPI = localContext.getResources().getString(R.string.uri_api_weather_today);
-            final String url = weatherService.createURIAPICurrent(urlAPI, APIVersion, latitude, longitude);
+            final SharedPreferences sharedPreferences = PreferenceManager
+                    .getDefaultSharedPreferences(localContext.getApplicationContext());
+            final String APPID = sharedPreferences.getString(localContext.getString(R.string.weather_preferences_app_id_key), "");
+
+               final String APIVersion = localContext.getString(R.string.api_version);
+            final String urlAPI = localContext.getString(R.string.uri_api_weather_today);
+            String url = weatherService.createURIAPICurrent(urlAPI, APIVersion, latitude, longitude);
+            if (!APPID.isEmpty()) {
+                url = url.concat("&APPID=" + APPID);
+            }
             final String jsonData = HTTPClient.retrieveDataAsString(new URL(url));
 
             return weatherService.retrieveCurrentFromJPOS(jsonData);
index e102f80..5fe33ec 100644 (file)
@@ -348,10 +348,18 @@ public class OverviewFragment extends ListFragment {
 
         private Forecast doInBackgroundThrowable(final double latitude, final double longitude)
                         throws URISyntaxException, ClientProtocolException, JsonParseException, IOException {
-
-            final String APIVersion = localContext.getResources().getString(R.string.api_version);
-            final String urlAPI = localContext.getResources().getString(R.string.uri_api_weather_forecast);
-            final String url = weatherService.createURIAPIForecast(urlAPI, APIVersion, latitude, longitude, localContext.getString(R.string.weather_preferences_day_forecast_fourteen_day));
+            final SharedPreferences sharedPreferences = PreferenceManager
+                    .getDefaultSharedPreferences(localContext.getApplicationContext());
+            final String APPID = sharedPreferences.getString(localContext.getString(R.string.weather_preferences_app_id_key), "");
+
+            final String APIVersion = localContext.getString(R.string.api_version);
+            final String urlAPI = localContext.getString(R.string.uri_api_weather_forecast);
+            String url = weatherService.createURIAPIForecast(
+                    urlAPI, APIVersion, latitude, longitude,
+                    localContext.getString(R.string.weather_preferences_day_forecast_fourteen_day));
+            if (!APPID.isEmpty()) {
+                url = url.concat("&APPID=" + APPID);
+            }
             final String jsonData = HTTPClient.retrieveDataAsString(new URL(url));
 
             return weatherService.retrieveForecastFromJPOS(jsonData);
index 3acd7f3..6c09d3f 100644 (file)
@@ -169,6 +169,17 @@ public class PreferencesFragment extends PreferenceFragment implements OnSharedP
             humanValue = humanValues[2];
         }
         connectionPref.setSummary(humanValue);
+
+        // APPID
+        keyPreference = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_app_id_key);
+        connectionPref = this.findPreference(keyPreference);
+        value = this.getPreferenceManager().getSharedPreferences()
+                .getString(keyPreference, this.getString(R.string.weather_preferences_app_id_text_empty_key));
+        if (value.isEmpty()) {
+            value = this.getString(R.string.weather_preferences_app_id_text_empty_key);
+        }
+        connectionPref.setSummary(value);
     }
 
     @Override
@@ -357,6 +368,20 @@ public class PreferencesFragment extends PreferenceFragment implements OnSharedP
             connectionPref.setSummary(humanValue);
             return;
         }
+
+        // APPID
+        keyValue = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_app_id_key);
+        if (key.equals(keyValue)) {
+            final Preference connectionPref = this.findPreference(key);
+            String value = sharedPreferences.getString(key, this.getString(R.string.weather_preferences_app_id_text_empty_key));
+            if (value.isEmpty()) {
+                value = this.getString(R.string.weather_preferences_app_id_text_empty_key);
+            }
+
+            connectionPref.setSummary(value);
+            return;
+        }
     }
 
     private void updateNotification(final String updateTimeRate) {
index 64502dd..45536d9 100644 (file)
@@ -101,21 +101,24 @@ public class NotificationIntentService extends IntentService {
             final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService)
                     throws ClientProtocolException, MalformedURLException, URISyntaxException,
                     JsonParseException, IOException {
+        final SharedPreferences sharedPreferences = PreferenceManager
+                .getDefaultSharedPreferences(this.getApplicationContext());
+        final String APPID = sharedPreferences.getString(this.getString(R.string.weather_preferences_app_id_key), "");
 
         final String APIVersion = this.getResources().getString(R.string.api_version);
 
         final String urlAPI = this.getResources().getString(R.string.uri_api_weather_today);
-        final String url = weatherService.createURIAPICurrent(urlAPI, APIVersion,
+        String url = weatherService.createURIAPICurrent(urlAPI, APIVersion,
                 weatherLocation.getLatitude(), weatherLocation.getLongitude());
+        if (!APPID.isEmpty()) {
+            url = url.concat("&APPID=" + APPID);
+        }
         final String jsonData = HTTPClient.retrieveDataAsString(new URL(url));
 
         return weatherService.retrieveCurrentFromJPOS(jsonData);
     }
     
     private void showNotification(final Current current, final WeatherLocation weatherLocation) {
-        final SharedPreferences sharedPreferences = PreferenceManager
-                .getDefaultSharedPreferences(this.getApplicationContext());
-
                // 1. Update units of measurement.
         final UnitsConversor tempUnitsConversor = new TempUnitsConversor(this.getApplicationContext());
 
index ee9d9e6..08e6875 100644 (file)
@@ -21,7 +21,10 @@ import java.util.HashMap;
 import java.util.Map;
 
 public enum IconsList {
-    ICON_01d("01d") {
+    // TODO: I am sometimes receiving this code, there is no documentation about it on the
+    // openweathermap site.... But it exists!!! Some day, try to find out more information about it.
+    // see: http://openweathermap.org/img/w/01dd.png
+    ICON_01dd("01dd") {
         @Override
         public int getResourceDrawable() {
             return R.drawable.weather_clear;
@@ -29,8 +32,14 @@ public enum IconsList {
     },
     // TODO: I am sometimes receiving this code, there is no documentation about it on the
     // openweathermap site.... But it exists!!! Some day, try to find out more information about it.
-    // see: http://openweathermap.org/img/w/01dd.png
-    ICON_01dd("01dd") {
+    // see: http://openweathermap.org/img/w/10dd.png
+    ICON_10dd("10dd") {
+        @Override
+        public int getResourceDrawable() {
+            return R.drawable.weather_showers_scattered;
+        }
+    },
+    ICON_01d("01d") {
         @Override
         public int getResourceDrawable() {
             return R.drawable.weather_clear;
@@ -90,15 +99,6 @@ public enum IconsList {
             return R.drawable.weather_showers;
         }
     },
-    // TODO: I am sometimes receiving this code, there is no documentation about it on the
-    // openweathermap site.... But it exists!!! Some day, try to find out more information about it.
-    // see: http://openweathermap.org/img/w/10dd.png
-    ICON_10dd("10dd") {
-        @Override
-        public int getResourceDrawable() {
-            return R.drawable.weather_showers_scattered;
-        }
-    },
     ICON_10d("10d") {
         @Override
         public int getResourceDrawable() {
index 6eaa678..b511e84 100644 (file)
@@ -20,10 +20,12 @@ import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.net.http.AndroidHttpClient;
+import android.preference.PreferenceManager;
 import android.support.v4.app.TaskStackBuilder;
 import android.util.Log;
 import android.view.View;
@@ -172,12 +174,18 @@ public class WidgetIntentService extends IntentService {
                        final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService)
                                        throws ClientProtocolException, MalformedURLException, URISyntaxException,
                                        JsonParseException, IOException {
+        final SharedPreferences sharedPreferences = PreferenceManager
+                .getDefaultSharedPreferences(this.getApplicationContext());
+        final String APPID = sharedPreferences.getString(this.getString(R.string.weather_preferences_app_id_key), "");
 
                final String APIVersion = this.getResources().getString(R.string.api_version);
 
                final String urlAPI = this.getResources().getString(R.string.uri_api_weather_today);
-               final String url = weatherService.createURIAPICurrent(urlAPI, APIVersion,
+               String url = weatherService.createURIAPICurrent(urlAPI, APIVersion,
                                weatherLocation.getLatitude(), weatherLocation.getLongitude());
+        if (!APPID.isEmpty()) {
+            url = url.concat("&APPID=" + APPID);
+        }
                final String jsonData = HTTPClient.retrieveDataAsString(new URL(url));
 
                return weatherService.retrieveCurrentFromJPOS(jsonData);
index c1be468..987eb64 100644 (file)
     <string name="weather_preferences_pressure_standard_atm">atm</string>
     <string name="weather_preferences_pressure_human_value_pascal">pascal</string>
     <string name="weather_preferences_pressure_human_value_standard_atm">standard atmosphere</string>
+    <string name="weather_preferences_app_id_key">weather_preferences_app_id_key</string>
+    <string name="weather_preferences_app_id">API Key</string>
+    <string name="weather_preferences_app_id_text">OpenWeatherMap API Key</string>
+    <string name="weather_preferences_app_id_text_empty_key">value not found</string>
+
+    <!-- API key Notice Dialog -->
+    <string name="api_id_key_notice_preference_key">api_key_notice_preference_key</string>
+    <string name="api_id_key_notice_title">OpenWeatherMap API Key</string>
+    <string name="api_id_key_notice_message">OpenWeatherMap works better with an application ID.\n\nThis key can be added in the preferences window.\n\nTo obtain an API Key for OpenWeatherMap register on the OpenWeatherMap website.\n\nPress OK button if you don\'t want to be noticed again.</string>
+    <string name="api_id_key_notice_ok_button">OK</string>
+    <string name="api_id_key_notice_cancel_button">Cancel</string>
 
     <!-- Widget Preferences Activity -->
     <string name="widget_preferences_action_settings">Settings</string>