Search in sources :

Example 1 with FormDownloaderListener

use of org.odk.collect.android.listeners.FormDownloaderListener in project collect by opendatakit.

the class ServerFormDownloader method downloadForm.

@Override
public void downloadForm(ServerFormDetails form, @Nullable ProgressReporter progressReporter, @Nullable Supplier<Boolean> isCancelled) throws FormDownloadException {
    Form formOnDevice;
    try {
        formOnDevice = formsRepository.getOneByMd5Hash(validateHash(form.getHash()));
    } catch (IllegalArgumentException e) {
        throw new FormDownloadException.FormWithNoHash();
    }
    if (formOnDevice != null) {
        if (formOnDevice.isDeleted()) {
            formsRepository.restore(formOnDevice.getDbId());
        }
    } else {
        List<Form> allSameFormIdVersion = formsRepository.getAllByFormIdAndVersion(form.getFormId(), form.getFormVersion());
        if (!allSameFormIdVersion.isEmpty() && !form.getDownloadUrl().contains("/draft.xml")) {
            analytics.logEventWithParam(DOWNLOAD_SAME_FORMID_VERSION_DIFFERENT_HASH, "form", AnalyticsUtils.getFormHash(form.getFormId(), form.getFormName()));
        }
    }
    File tempDir = new File(cacheDir, "download-" + UUID.randomUUID().toString());
    tempDir.mkdirs();
    try {
        FormDownloaderListener stateListener = new ProgressReporterAndSupplierStateListener(progressReporter, isCancelled);
        processOneForm(form, stateListener, tempDir, formsDirPath, formMetadataParser);
    } catch (FormSourceException e) {
        throw new FormDownloadException.FormSourceError(e);
    } finally {
        try {
            deleteDirectory(tempDir);
        } catch (IOException ignored) {
        // ignored
        }
    }
}
Also used : FormSourceException(org.odk.collect.forms.FormSourceException) FormDownloaderListener(org.odk.collect.android.listeners.FormDownloaderListener) Form(org.odk.collect.forms.Form) IOException(java.io.IOException) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 FormDownloaderListener (org.odk.collect.android.listeners.FormDownloaderListener)1 Form (org.odk.collect.forms.Form)1 FormSourceException (org.odk.collect.forms.FormSourceException)1 MediaFile (org.odk.collect.forms.MediaFile)1