diff --git a/android/AkvoRSR/AndroidManifest.xml b/android/AkvoRSR/AndroidManifest.xml index d710dcf2..857998b3 100644 --- a/android/AkvoRSR/AndroidManifest.xml +++ b/android/AkvoRSR/AndroidManifest.xml @@ -1,8 +1,8 @@ 0) { unsentCount++; - } else + } else { otherCount++; + } cursor.moveToNext(); } } @@ -1097,7 +1098,7 @@ public Organisation findOrganisation(String _id) { /** * creates or updates a user in the db * - * @param org + * @param org the organisation data to be updated * @return */ public void saveOrganisation(Organisation org) { @@ -1156,6 +1157,11 @@ public void clearAllData() { executeSql("delete from " + COUNTRY_TABLE); } + /** + * lists all Countries + * + * @return a Cursor containing all countries + */ public Cursor listAllCountries() { Cursor cursor = database.query(COUNTRY_TABLE, null, @@ -1166,6 +1172,18 @@ public Cursor listAllCountries() { null); return cursor; } + + + public int getCountryCount() { + Cursor cursor = listAllCountries(); + int c = 0; + if (cursor != null) { + c = cursor.getCount(); + cursor.close(); + } + return c; + } + /** * saves or updates a Country in the db diff --git a/android/AkvoRSR/src/org/akvo/rsr/up/service/GetProjectDataService.java b/android/AkvoRSR/src/org/akvo/rsr/up/service/GetProjectDataService.java index f290cc2a..c415b414 100644 --- a/android/AkvoRSR/src/org/akvo/rsr/up/service/GetProjectDataService.java +++ b/android/AkvoRSR/src/org/akvo/rsr/up/service/GetProjectDataService.java @@ -83,8 +83,7 @@ protected void onHandleIntent(Intent intent) { String.format(ConstantUtil.FETCH_PROJ_URL_PATTERN, id))); broadcastProgress(0, ++i, projects); } - if (mFetchCountries) { - // TODO: rarely changes, so only fetch countries if we never did that + if (mFetchCountries && ad.getCountryCount() == 0) { // rarely changes, so only fetch countries if we never did that dl.fetchCountryListRestApiPaged(this, new URL(SettingsUtil.host(this) + String.format(ConstantUtil.FETCH_COUNTRIES_URL))); } diff --git a/android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java b/android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java index 33eb3975..53b9fa3c 100644 --- a/android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java +++ b/android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java @@ -36,9 +36,8 @@ public class ConstantUtil { public static final String POST_UPDATE_URL = "/rest/v1/project_update/?format=xml"; public static final String FETCH_UPDATE_URL_PATTERN = "/rest/v1/project_update/?format=xml&project=%s";//use default limit public static final String VERIFY_UPDATE_PATTERN = "/rest/v1/project_update/?format=xml&uuid=%s&limit=2"; - public static final String FETCH_PROJ_URL_PATTERN = "/rest/v1/project_up/%s/?format=xml&image_thumb_name=up&image_thumb_up_width=100"; //ask for thumbnail size -// public static final String FETCH_COUNTRIES_URL = "/api/v1/country/?format=xml&limit=0"; - public static final String FETCH_COUNTRIES_URL = "/rest/v1/country/?format=xml"; + public static final String FETCH_PROJ_URL_PATTERN = "/rest/v1/project_up/%s/?format=xml&image_thumb_name=up&image_thumb_up_width=100"; //now asks for thumbnail size + public static final String FETCH_COUNTRIES_URL = "/rest/v1/country/?format=xml&limit=50"; //very small objects - get many at a time public static final String FETCH_USER_URL_PATTERN = "/api/v1/user/%s/?format=xml&depth=1"; public static final String FETCH_ORG_URL_PATTERN = "/api/v1/organisation/%s/?format=xml&depth=0"; public static final int MAX_IMAGE_UPLOAD_SIZE = 2000000; //Nginx POST limit is 3MB, B64 encoding expands 33% and there may be long text diff --git a/android/AkvoRSR/src/org/akvo/rsr/up/util/Downloader.java b/android/AkvoRSR/src/org/akvo/rsr/up/util/Downloader.java index 618ea328..06582823 100644 --- a/android/AkvoRSR/src/org/akvo/rsr/up/util/Downloader.java +++ b/android/AkvoRSR/src/org/akvo/rsr/up/util/Downloader.java @@ -267,9 +267,9 @@ public Date fetchUpdateListRestApiPaged(Context ctx, URL url) throws ParserConfi url = null;//we are done } else { //Vanilla case is 403 forbidden on an auth failure - Log.e(TAG, "Fetch update list HTTP error code:" + code); + Log.e(TAG, "Fetch update list HTTP error code:" + code + ' ' + h.message()); Log.e(TAG, h.body()); - throw new FailedFetchException("Unexpected server response " + code); + throw new FailedFetchException("Unexpected server response " + code + ' ' + h.message()); } } Log.i(TAG, "Grand total of " + total + " updates"); diff --git a/android/AkvoRSR/src/org/akvo/rsr/up/xml/CountryRestListHandler.java b/android/AkvoRSR/src/org/akvo/rsr/up/xml/CountryRestListHandler.java index 2111bf08..6fd05a74 100644 --- a/android/AkvoRSR/src/org/akvo/rsr/up/xml/CountryRestListHandler.java +++ b/android/AkvoRSR/src/org/akvo/rsr/up/xml/CountryRestListHandler.java @@ -123,7 +123,7 @@ public void startElement(String namespaceURI, String localName, buffer = ""; if (depth == 1 && localName.equals("next")) { this.in_next = true; - } else if (depth == 1 && localName.equals("list-item")) { + } else if (depth == 2 && localName.equals("list-item")) { this.in_country = true; currentCountry = new Country(); } else if (in_country)