protected void saveIncomingRecords(List<String> idsToDownload, int startProgress) throws IOException, JSONException, HttpException { String subStatusFormat = "Downloading Record %s of" + idsToDownload.size(); int counter = 0; setProgressAndNotify(context.getString(R.string.synchronize_step_3), startProgress); for (String idToDownload : idsToDownload) { T incomingRecord = recordSyncService.getRecord(idToDownload); if (isCancelled()) { break; } try { if (repository.exists(incomingRecord.getUniqueId())) { repository.update(incomingRecord); } else { repository.createOrUpdate(incomingRecord); } recordSyncService.setMedia(incomingRecord); setProgressAndNotify(String.format(subStatusFormat, ++counter), startProgress); startProgress += 1; } catch (Exception e) { Log.e("SyncAllDataTask", "Error syncing record", e); throw new RuntimeException(e); } } }
protected void setProgressAndNotify(String statusText, int progress) { if (!isCancelled()) { RapidFtrApplication.getApplicationInstance() .showProgressNotification( recordSyncService.getNotificationId(), context.getString(R.string.sync_title), statusText, maxProgress, progress, false); } }
void sendRecordsToServer(List<T> recordsToSyncWithServer) throws IOException, JSONException, HttpException { setProgressAndNotify(context.getString(R.string.synchronize_step_2), formSectionProgress); String subStatusFormat = "Uploading Record %s of " + recordsToSyncWithServer.size(); int counter = 0; int startProgress = formSectionProgress; for (T baseModel : recordsToSyncWithServer) { if (isCancelled()) { break; } recordSyncService.sync(baseModel, currentUser); setProgressAndNotify(String.format(subStatusFormat, ++counter), startProgress); startProgress += 1; } }
@Override protected Boolean doInBackground(Object... notRelevant) { try { sync(); return true; } catch (HttpException e) { Log.e("SyncAllDataTask", "HTTPError in sync", e); String message = RapidFtrApplication.getApplicationInstance().getString(R.string.session_timeout); if (e.getMessage() != null && e.getMessage().trim().length() > 0) { message = e.getMessage(); } publishProgress(message); return false; } catch (Exception e) { Log.e("SyncAllDataTask", "Error in sync", e); publishProgress(context.getString(R.string.sync_error)); return false; } }