use of org.odk.collect.android.external.ExternalDataHandler in project collect by opendatakit.
the class FormLoaderTask method doInBackground.
/**
* Initialize {@link FormEntryController} with {@link FormDef} from binary or
* from XML. If given an instance, it will be used to fill the {@link FormDef}.
*/
@Override
protected FECWrapper doInBackground(String... path) {
errorMsg = null;
final String formPath = path[0];
final File formXml = new File(formPath);
final FormDef formDef = createFormDefFromCacheOrXml(formXml);
if (errorMsg != null || formDef == null) {
return null;
}
// set paths to /sdcard/odk/forms/formfilename-media/
final String formFileName = formXml.getName().substring(0, formXml.getName().lastIndexOf("."));
final File formMediaDir = new File(formXml.getParent(), formFileName + "-media");
externalDataManager = new ExternalDataManagerImpl(formMediaDir);
// add external data function handlers
ExternalDataHandler externalDataHandlerPull = new ExternalDataHandlerPull(externalDataManager);
formDef.getEvaluationContext().addFunctionHandler(externalDataHandlerPull);
try {
loadExternalData(formMediaDir);
} catch (Exception e) {
Timber.e(e, "Exception thrown while loading external data");
errorMsg = e.getMessage();
return null;
}
if (isCancelled()) {
// that means that the user has cancelled, so no need to go further
return null;
}
// create FormEntryController from formdef
final FormEntryModel fem = new FormEntryModel(formDef);
final FormEntryController fec = new FormEntryController(fem);
boolean usedSavepoint = false;
try {
Timber.i("Initializing form.");
final long start = System.currentTimeMillis();
usedSavepoint = initializeForm(formDef, fec);
Timber.i("Form initialized in %.3f seconds.", (System.currentTimeMillis() - start) / 1000F);
} catch (RuntimeException e) {
Timber.e(e);
if (e.getCause() instanceof XPathTypeMismatchException) {
// this is a case of
// https://bitbucket.org/m
// .sundt/javarosa/commits/e5d344783e7968877402bcee11828fa55fac69de
// the data are imported, the survey will be unusable
// but we should give the option to the user to edit the form
// otherwise the survey will be TOTALLY inaccessible.
Timber.w("We have a syntactically correct instance, but the data threw an " + "exception inside JR. We should allow editing.");
} else {
errorMsg = e.getMessage();
return null;
}
}
// Remove previous forms
ReferenceManager.instance().clearSession();
processItemSets(formMediaDir);
// This should get moved to the Application Class
if (ReferenceManager.instance().getFactories().length == 0) {
// this is /sdcard/odk
ReferenceManager.instance().addReferenceFactory(new FileReferenceFactory(Collect.ODK_ROOT));
}
// Set jr://... to point to /sdcard/odk/forms/filename-media/
ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://images/", "jr://file/forms/" + formFileName + "-media/"));
ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://image/", "jr://file/forms/" + formFileName + "-media/"));
ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://audio/", "jr://file/forms/" + formFileName + "-media/"));
ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://video/", "jr://file/forms/" + formFileName + "-media/"));
final FormController fc = new FormController(formMediaDir, fec, instancePath == null ? null : new File(instancePath));
if (xpath != null) {
// we are resuming after having terminated -- set index to this
// position...
FormIndex idx = fc.getIndexFromXPath(xpath);
fc.jumpToIndex(idx);
}
if (waitingXPath != null) {
FormIndex idx = fc.getIndexFromXPath(waitingXPath);
fc.setIndexWaitingForData(idx);
}
data = new FECWrapper(fc, usedSavepoint);
return data;
}
Aggregations