Search in sources :

Example 6 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class FormDownloadList method formsDownloadingComplete.

@Override
public void formsDownloadingComplete(HashMap<FormDetails, String> result) {
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(null);
    }
    if (progressDialog.isShowing()) {
        // should always be true here
        progressDialog.dismiss();
    }
    Set<FormDetails> keys = result.keySet();
    StringBuilder b = new StringBuilder();
    for (FormDetails k : keys) {
        b.append(k.getFormName() + " (" + ((k.getFormVersion() != null) ? (this.getString(R.string.version) + ": " + k.getFormVersion() + " ") : "") + "ID: " + k.getFormID() + ") - " + result.get(k));
        b.append("\n\n");
    }
    createAlertDialog(getString(R.string.download_forms_result), b.toString().trim(), EXIT);
}
Also used : FormDetails(org.odk.collect.android.logic.FormDetails)

Example 7 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class DownloadFormListTask method doInBackground.

@Override
protected HashMap<String, FormDetails> doInBackground(Void... values) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext());
    String downloadListUrl = settings.getString(PreferenceKeys.KEY_SERVER_URL, Collect.getInstance().getString(R.string.default_server_url));
    // NOTE: /formlist must not be translated! It is the well-known path on the server.
    String formListUrl = Collect.getInstance().getApplicationContext().getString(R.string.default_odk_formlist);
    String downloadPath = settings.getString(PreferenceKeys.KEY_FORMLIST_URL, formListUrl);
    downloadListUrl += downloadPath;
    Collect.getInstance().getActivityLogger().logAction(this, formListUrl, downloadListUrl);
    // We populate this with available forms from the specified server.
    // <formname, details>
    HashMap<String, FormDetails> formList = new HashMap<String, FormDetails>();
    // get shared HttpContext so that authentication and cookies are retained.
    HttpContext localContext = Collect.getInstance().getHttpContext();
    HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
    DocumentFetchResult result = WebUtils.getXmlDocument(downloadListUrl, localContext, httpclient);
    // If we can't get the document, return the error, cancel the task
    if (result.errorMessage != null) {
        if (result.responseCode == 401) {
            formList.put(DL_AUTH_REQUIRED, new FormDetails(result.errorMessage));
        } else {
            formList.put(DL_ERROR_MSG, new FormDetails(result.errorMessage));
        }
        return formList;
    }
    if (result.isOpenRosaResponse) {
        // Attempt OpenRosa 1.0 parsing
        Element xformsElement = result.doc.getRootElement();
        if (!xformsElement.getName().equals("xforms")) {
            String error = "root element is not <xforms> : " + xformsElement.getName();
            Timber.e("Parsing OpenRosa reply -- %s", error);
            formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
            return formList;
        }
        String namespace = xformsElement.getNamespace();
        if (!isXformsListNamespacedElement(xformsElement)) {
            String error = "root element namespace is incorrect:" + namespace;
            Timber.e("Parsing OpenRosa reply -- %s", error);
            formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
            return formList;
        }
        int elements = xformsElement.getChildCount();
        for (int i = 0; i < elements; ++i) {
            if (xformsElement.getType(i) != Element.ELEMENT) {
                // e.g., whitespace (text)
                continue;
            }
            Element xformElement = xformsElement.getElement(i);
            if (!isXformsListNamespacedElement(xformElement)) {
                // someone else's extension?
                continue;
            }
            String name = xformElement.getName();
            if (!name.equalsIgnoreCase("xform")) {
                // someone else's extension?
                continue;
            }
            // this is something we know how to interpret
            String formId = null;
            String formName = null;
            String version = null;
            String majorMinorVersion = null;
            String description = null;
            String downloadUrl = null;
            String manifestUrl = null;
            String hash = null;
            // don't process descriptionUrl
            int fieldCount = xformElement.getChildCount();
            for (int j = 0; j < fieldCount; ++j) {
                if (xformElement.getType(j) != Element.ELEMENT) {
                    // whitespace
                    continue;
                }
                Element child = xformElement.getElement(j);
                if (!isXformsListNamespacedElement(child)) {
                    // someone else's extension?
                    continue;
                }
                String tag = child.getName();
                switch(tag) {
                    case "formID":
                        formId = XFormParser.getXMLText(child, true);
                        if (formId != null && formId.length() == 0) {
                            formId = null;
                        }
                        break;
                    case "name":
                        formName = XFormParser.getXMLText(child, true);
                        if (formName != null && formName.length() == 0) {
                            formName = null;
                        }
                        break;
                    case "version":
                        version = XFormParser.getXMLText(child, true);
                        if (version != null && version.length() == 0) {
                            version = null;
                        }
                        break;
                    case "majorMinorVersion":
                        majorMinorVersion = XFormParser.getXMLText(child, true);
                        if (majorMinorVersion != null && majorMinorVersion.length() == 0) {
                            majorMinorVersion = null;
                        }
                        break;
                    case "descriptionText":
                        description = XFormParser.getXMLText(child, true);
                        if (description != null && description.length() == 0) {
                            description = null;
                        }
                        break;
                    case "downloadUrl":
                        downloadUrl = XFormParser.getXMLText(child, true);
                        if (downloadUrl != null && downloadUrl.length() == 0) {
                            downloadUrl = null;
                        }
                        break;
                    case "manifestUrl":
                        manifestUrl = XFormParser.getXMLText(child, true);
                        if (manifestUrl != null && manifestUrl.length() == 0) {
                            manifestUrl = null;
                        }
                        break;
                    case "hash":
                        hash = XFormParser.getXMLText(child, true);
                        if (hash != null && hash.length() == 0) {
                            hash = null;
                        }
                        break;
                }
            }
            if (formId == null || downloadUrl == null || formName == null) {
                String error = "Forms list entry " + Integer.toString(i) + " has missing or empty tags: formID, name, or downloadUrl";
                Timber.e("Parsing OpenRosa reply -- %s", error);
                formList.clear();
                formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
                return formList;
            }
            boolean isNewerFormVersionAvailable = false;
            boolean areNewerMediaFilesAvailable = false;
            if (isThisFormAlreadyDownloaded(formId)) {
                isNewerFormVersionAvailable = isNewerFormVersionAvailable(DownloadFormsTask.getMd5Hash(hash));
                if (!isNewerFormVersionAvailable && manifestUrl != null) {
                    List<MediaFile> newMediaFiles = downloadMediaFileList(manifestUrl);
                    if (newMediaFiles != null && newMediaFiles.size() > 0) {
                        areNewerMediaFilesAvailable = areNewerMediaFilesAvailable(formId, version, newMediaFiles);
                    }
                }
            }
            formList.put(formId, new FormDetails(formName, downloadUrl, manifestUrl, formId, (version != null) ? version : majorMinorVersion, isNewerFormVersionAvailable, areNewerMediaFilesAvailable));
        }
    } else {
        // Aggregate 0.9.x mode...
        // populate HashMap with form names and urls
        Element formsElement = result.doc.getRootElement();
        int formsCount = formsElement.getChildCount();
        String formId = null;
        for (int i = 0; i < formsCount; ++i) {
            if (formsElement.getType(i) != Element.ELEMENT) {
                // whitespace
                continue;
            }
            Element child = formsElement.getElement(i);
            String tag = child.getName();
            if (tag.equals("formID")) {
                formId = XFormParser.getXMLText(child, true);
                if (formId != null && formId.length() == 0) {
                    formId = null;
                }
            }
            if (tag.equalsIgnoreCase("form")) {
                String formName = XFormParser.getXMLText(child, true);
                if (formName != null && formName.length() == 0) {
                    formName = null;
                }
                String downloadUrl = child.getAttributeValue(null, "url");
                downloadUrl = downloadUrl.trim();
                if (downloadUrl != null && downloadUrl.length() == 0) {
                    downloadUrl = null;
                }
                if (downloadUrl == null || formName == null) {
                    String error = "Forms list entry " + Integer.toString(i) + " is missing form name or url attribute";
                    Timber.e("Parsing OpenRosa reply -- %s", error);
                    formList.clear();
                    formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_legacy_formlist_failed, error)));
                    return formList;
                }
                formList.put(formName, new FormDetails(formName, downloadUrl, null, formId, null, false, false));
                formId = null;
            }
        }
    }
    return formList;
}
Also used : MediaFile(org.odk.collect.android.logic.MediaFile) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) Element(org.kxml2.kdom.Element) HttpContext(org.opendatakit.httpclientandroidlib.protocol.HttpContext) FormDetails(org.odk.collect.android.logic.FormDetails) DocumentFetchResult(org.odk.collect.android.utilities.DocumentFetchResult) HttpClient(org.opendatakit.httpclientandroidlib.client.HttpClient)

Example 8 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class DownloadFormsTask method doInBackground.

@Override
protected HashMap<FormDetails, String> doInBackground(ArrayList<FormDetails>... values) {
    ArrayList<FormDetails> toDownload = values[0];
    formsDao = new FormsDao();
    int total = toDownload.size();
    int count = 1;
    Collect.getInstance().getActivityLogger().logAction(this, "downloadForms", String.valueOf(total));
    final HashMap<FormDetails, String> result = new HashMap<>();
    for (FormDetails fd : toDownload) {
        try {
            String message = processOneForm(total, count++, fd);
            result.put(fd, message.isEmpty() ? Collect.getInstance().getString(R.string.success) : message);
        } catch (TaskCancelledException cd) {
            break;
        }
    }
    return result;
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) FormDetails(org.odk.collect.android.logic.FormDetails) HashMap(java.util.HashMap)

Aggregations

FormDetails (org.odk.collect.android.logic.FormDetails)8 HashMap (java.util.HashMap)5 DownloadFormListTask (org.odk.collect.android.tasks.DownloadFormListTask)2 DownloadFormsTask (org.odk.collect.android.tasks.DownloadFormsTask)2 SharedPreferences (android.content.SharedPreferences)1 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 SparseBooleanArray (android.util.SparseBooleanArray)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 Button (android.widget.Button)1 ListView (android.widget.ListView)1 ArrayList (java.util.ArrayList)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1 Element (org.kxml2.kdom.Element)1 FormsDao (org.odk.collect.android.dao.FormsDao)1 MediaFile (org.odk.collect.android.logic.MediaFile)1 MockedServerTest (org.odk.collect.android.test.MockedServerTest)1