diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java index f92a41d..341773e 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java @@ -305,6 +305,9 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa case SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID: message = R.string.empty_forecast_list_server_error; break; + case SunshineSyncAdapter.LOCATION_STATUS_INVALID: + message = R.string.empty_forecast_list_invalid_location; + break; default: if (!Utility.isNetworkAvailable(getActivity()) ) { message = R.string.empty_forecast_list_no_network; diff --git a/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java b/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java index 1869c5c..5487595 100644 --- a/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java @@ -113,6 +113,9 @@ public class SettingsActivity extends PreferenceActivity @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if ( key.equals(getString(R.string.pref_location_key)) ) { + // we've changed the location + // first clear locationStatus + Utility.resetLocationStatus(this); SunshineSyncAdapter.syncImmediately(this); } else if ( key.equals(getString(R.string.pref_units_key)) ) { // units have changed. update lists of weather entries accordingly diff --git a/app/src/main/java/com/example/android/sunshine/app/Utility.java b/app/src/main/java/com/example/android/sunshine/app/Utility.java index f4865e7..d547e0d 100644 --- a/app/src/main/java/com/example/android/sunshine/app/Utility.java +++ b/app/src/main/java/com/example/android/sunshine/app/Utility.java @@ -277,4 +277,15 @@ public class Utility { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c); return sp.getInt(c.getString(R.string.pref_location_status_key), SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN); } + + /** + * Resets the location status. (Sets it to SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN) + * @param c Context used to get the SharedPreferences + */ + static public void resetLocationStatus(Context c){ + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c); + SharedPreferences.Editor spe = sp.edit(); + spe.putInt(c.getString(R.string.pref_location_status_key), SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN); + spe.apply(); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java index b87c3d2..0ce1a77 100644 --- a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java +++ b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java @@ -71,15 +71,14 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { private static final int INDEX_SHORT_DESC = 3; @Retention(RetentionPolicy.SOURCE) - @IntDef({LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN, LOCATION_STATUS_SERVER_INVALID, LOCATION_STATUS_UNKNOWN}) + @IntDef({LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN, LOCATION_STATUS_SERVER_INVALID, LOCATION_STATUS_UNKNOWN, LOCATION_STATUS_INVALID}) public @interface LocationStatus {} public static final int LOCATION_STATUS_OK = 0; public static final int LOCATION_STATUS_SERVER_DOWN = 1; - public static final int - LOCATION_STATUS_SERVER_INVALID = 2; - public static final int - LOCATION_STATUS_UNKNOWN = 3; + public static final int LOCATION_STATUS_SERVER_INVALID = 2; + public static final int LOCATION_STATUS_UNKNOWN = 3; + public static final int LOCATION_STATUS_INVALID = 4; public SunshineSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); @@ -218,8 +217,27 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { final String OWM_DESCRIPTION = "main"; final String OWM_WEATHER_ID = "id"; + final String OWM_MESSAGE_CODE = "cod"; + try { JSONObject forecastJson = new JSONObject(forecastJsonStr); + + // do we have an error? + if ( forecastJson.has(OWM_MESSAGE_CODE) ) { + int errorCode = forecastJson.getInt(OWM_MESSAGE_CODE); + + switch (errorCode) { + case HttpURLConnection.HTTP_OK: + break; + case HttpURLConnection.HTTP_NOT_FOUND: + setLocationStatus(getContext(), LOCATION_STATUS_INVALID); + return; + default: + setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN); + return; + } + } + JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 968d787..87d92e0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,5 +117,6 @@ No weather information available. The network is not available to fetch weather data. No weather information available. The server is not returning data. No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine. + No weather information available. The location in settings is not recognized by the weather server. \ No newline at end of file