Added state when an invalid query (generating a 404 code) is detected by the SyncAdapter.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -117,5 +117,6 @@
|
||||
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string>
|
||||
<string name="empty_forecast_list_server_down">No weather information available. The server is not returning data.</string>
|
||||
<string name="empty_forecast_list_server_error">No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine.</string>
|
||||
<string name="empty_forecast_list_invalid_location">No weather information available. The location in settings is not recognized by the weather server.</string>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user