From 5f05c090196029fd1f8adfb0b549f99385a25249 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sat, 22 Nov 2014 19:57:27 +0100 Subject: [PATCH] OpenWeatherMap works better with an application ID See: http://openweathermap.org/appid --- .../information/activity/MainTabsActivity.java | 23 ++++++- .../fragment/APIKeyNoticeDialogFragment.java | 70 ++++++++++++++++++++++ .../information/fragment/ErrorDialogFragment.java | 60 ------------------- .../fragment/current/CurrentFragment.java | 14 +++-- .../fragment/overview/OverviewFragment.java | 16 +++-- .../fragment/preferences/PreferencesFragment.java | 25 ++++++++ .../notification/NotificationIntentService.java | 11 ++-- .../weather/information/service/IconsList.java | 24 ++++---- .../information/widget/WidgetIntentService.java | 10 +++- app/src/main/res/values/strings.xml | 11 ++++ 10 files changed, 177 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/name/gumartinm/weather/information/fragment/APIKeyNoticeDialogFragment.java delete mode 100644 app/src/main/java/name/gumartinm/weather/information/fragment/ErrorDialogFragment.java diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java index 7fd351c..1cd242c 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java @@ -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 index 0000000..b177841 --- /dev/null +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/APIKeyNoticeDialogFragment.java @@ -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 index 5cec3ef..0000000 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/ErrorDialogFragment.java +++ /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 diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java index 0d04dc4..ad67dc4 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java @@ -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); diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java index e102f80..5fe33ec 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java @@ -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); diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/preferences/PreferencesFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/preferences/PreferencesFragment.java index 3acd7f3..6c09d3f 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/preferences/PreferencesFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/preferences/PreferencesFragment.java @@ -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) { diff --git a/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java b/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java index 64502dd..45536d9 100644 --- a/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java +++ b/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java @@ -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()); diff --git a/app/src/main/java/name/gumartinm/weather/information/service/IconsList.java b/app/src/main/java/name/gumartinm/weather/information/service/IconsList.java index ee9d9e6..08e6875 100644 --- a/app/src/main/java/name/gumartinm/weather/information/service/IconsList.java +++ b/app/src/main/java/name/gumartinm/weather/information/service/IconsList.java @@ -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() { diff --git a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java index 6eaa678..b511e84 100644 --- a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java +++ b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java @@ -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); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c1be468..987eb64 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -91,6 +91,17 @@ atm pascal standard atmosphere + weather_preferences_app_id_key + API Key + OpenWeatherMap API Key + value not found + + + api_key_notice_preference_key + OpenWeatherMap API Key + 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. + OK + Cancel Settings -- 2.1.4