Added state when an invalid query (generating a 404 code) is detected by the SyncAdapter.

This commit is contained in:
Dan Galpin
2015-05-24 14:30:51 -07:00
parent 0577edf74c
commit c3375ecabe
5 changed files with 41 additions and 5 deletions

View File

@@ -305,6 +305,9 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa
case SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID: case SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID:
message = R.string.empty_forecast_list_server_error; message = R.string.empty_forecast_list_server_error;
break; break;
case SunshineSyncAdapter.LOCATION_STATUS_INVALID:
message = R.string.empty_forecast_list_invalid_location;
break;
default: default:
if (!Utility.isNetworkAvailable(getActivity()) ) { if (!Utility.isNetworkAvailable(getActivity()) ) {
message = R.string.empty_forecast_list_no_network; message = R.string.empty_forecast_list_no_network;

View File

@@ -113,6 +113,9 @@ public class SettingsActivity extends PreferenceActivity
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if ( key.equals(getString(R.string.pref_location_key)) ) { if ( key.equals(getString(R.string.pref_location_key)) ) {
// we've changed the location
// first clear locationStatus
Utility.resetLocationStatus(this);
SunshineSyncAdapter.syncImmediately(this); SunshineSyncAdapter.syncImmediately(this);
} else if ( key.equals(getString(R.string.pref_units_key)) ) { } else if ( key.equals(getString(R.string.pref_units_key)) ) {
// units have changed. update lists of weather entries accordingly // units have changed. update lists of weather entries accordingly

View File

@@ -277,4 +277,15 @@ public class Utility {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
return sp.getInt(c.getString(R.string.pref_location_status_key), SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN); 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();
}
} }

View File

@@ -71,15 +71,14 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
private static final int INDEX_SHORT_DESC = 3; private static final int INDEX_SHORT_DESC = 3;
@Retention(RetentionPolicy.SOURCE) @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 @interface LocationStatus {}
public static final int LOCATION_STATUS_OK = 0; public static final int LOCATION_STATUS_OK = 0;
public static final int LOCATION_STATUS_SERVER_DOWN = 1; public static final int LOCATION_STATUS_SERVER_DOWN = 1;
public static final int public static final int LOCATION_STATUS_SERVER_INVALID = 2;
LOCATION_STATUS_SERVER_INVALID = 2; public static final int LOCATION_STATUS_UNKNOWN = 3;
public static final int public static final int LOCATION_STATUS_INVALID = 4;
LOCATION_STATUS_UNKNOWN = 3;
public SunshineSyncAdapter(Context context, boolean autoInitialize) { public SunshineSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize); super(context, autoInitialize);
@@ -218,8 +217,27 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
final String OWM_DESCRIPTION = "main"; final String OWM_DESCRIPTION = "main";
final String OWM_WEATHER_ID = "id"; final String OWM_WEATHER_ID = "id";
final String OWM_MESSAGE_CODE = "cod";
try { try {
JSONObject forecastJson = new JSONObject(forecastJsonStr); 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); JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY);

View File

@@ -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_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_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_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> </resources>