Added a status for the current location in preferences and tracking for it in the SyncAdapter.

This commit is contained in:
Dan Galpin
2015-05-24 14:19:31 -07:00
parent a8ff5df119
commit d4c9c26277
2 changed files with 21 additions and 1 deletions

View File

@@ -146,6 +146,7 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
if (buffer.length() == 0) { if (buffer.length() == 0) {
// Stream was empty. No point in parsing. // Stream was empty. No point in parsing.
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN);
return; return;
} }
forecastJsonStr = buffer.toString(); forecastJsonStr = buffer.toString();
@@ -154,9 +155,11 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
Log.e(LOG_TAG, "Error ", e); Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting // If the code didn't successfully get the weather data, there's no point in attempting
// to parse it. // to parse it.
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace(); e.printStackTrace();
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_INVALID);
} finally { } finally {
if (urlConnection != null) { if (urlConnection != null) {
urlConnection.disconnect(); urlConnection.disconnect();
@@ -316,12 +319,13 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
notifyWeather(); notifyWeather();
} }
Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted"); Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted");
setLocationStatus(getContext(), LOCATION_STATUS_OK);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace(); e.printStackTrace();
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_INVALID);
} }
} }
@@ -547,4 +551,17 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
public static void initializeSyncAdapter(Context context) { public static void initializeSyncAdapter(Context context) {
getSyncAccount(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();
}
} }

View File

@@ -42,6 +42,9 @@
<!-- Key name for storing location in SharedPreferences [CHAR LIMIT=NONE] --> <!-- Key name for storing location in SharedPreferences [CHAR LIMIT=NONE] -->
<string name="pref_location_key" translatable="false">location</string> <string name="pref_location_key" translatable="false">location</string>
<!-- Key name for storing location status in SharedPreferences -->
<string name="pref_location_status_key" translatable="false">loc-status</string>
<!-- Default postal code for location preference [CHAR LIMIT=NONE] --> <!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
<string name="pref_location_default" translatable="false">94043</string> <string name="pref_location_default" translatable="false">94043</string>