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;
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;
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.
}
// 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 = "";
--- /dev/null
+/**
+ * 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
+++ /dev/null
-/**
- * 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
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);
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);
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
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) {
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());
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;
},
// 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;
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() {
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;
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);
<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>