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 63f2e3c..b87c3d2 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 @@ -146,6 +146,7 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { if (buffer.length() == 0) { // Stream was empty. No point in parsing. + setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN); return; } forecastJsonStr = buffer.toString(); @@ -154,9 +155,11 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { Log.e(LOG_TAG, "Error ", e); // If the code didn't successfully get the weather data, there's no point in attempting // to parse it. + setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); + setLocationStatus(getContext(), LOCATION_STATUS_SERVER_INVALID); } finally { if (urlConnection != null) { urlConnection.disconnect(); @@ -316,12 +319,13 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { notifyWeather(); } - Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted"); + setLocationStatus(getContext(), LOCATION_STATUS_OK); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); + setLocationStatus(getContext(), LOCATION_STATUS_SERVER_INVALID); } } @@ -547,4 +551,17 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { public static void initializeSyncAdapter(Context context) { getSyncAccount(context); } + + /** + * Sets the location status into shared preference. This function should not be called from + * the UI thread because it uses commit to write to the shared preferences. + * @param c Context to get the PreferenceManager from. + * @param locationStatus The IntDef value to set + */ + static private void setLocationStatus(Context c, @LocationStatus int locationStatus){ + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c); + SharedPreferences.Editor spe = sp.edit(); + spe.putInt(c.getString(R.string.pref_location_status_key), locationStatus); + spe.commit(); + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62bcc2e..9ed4172 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,6 +42,9 @@ location + + loc-status + 94043