use of org.odk.collect.android.exception.ExternalParamsException in project collect by opendatakit.
the class ExStringWidget method onButtonClick.
@Override
public void onButtonClick(int buttonId) {
String exSpec = getFormEntryPrompt().getAppearanceHint().replaceFirst("^ex[:]", "");
final String intentName = ExternalAppsUtils.extractIntentName(exSpec);
final Map<String, String> exParams = ExternalAppsUtils.extractParameters(exSpec);
final String errorString;
String v = getFormEntryPrompt().getSpecialFormQuestionText("noAppErrorString");
errorString = (v != null) ? v : getContext().getString(R.string.no_app);
Intent i = new Intent(intentName);
if (activityAvailability.isActivityAvailable(i)) {
try {
ExternalAppsUtils.populateParameters(i, exParams, getFormEntryPrompt().getIndex().getReference());
waitForData();
fireActivity(i);
} catch (ExternalParamsException e) {
Timber.d(e);
onException(e.getMessage());
}
} else {
onException(errorString);
}
}
use of org.odk.collect.android.exception.ExternalParamsException in project collect by opendatakit.
the class ExternalAppsUtils method populateParameters.
public static void populateParameters(Intent intent, Map<String, String> exParams, TreeReference reference) throws ExternalParamsException {
FormDef formDef = Collect.getInstance().getFormController().getFormDef();
FormInstance formInstance = formDef.getInstance();
EvaluationContext evaluationContext = new EvaluationContext(formDef.getEvaluationContext(), reference);
if (exParams != null) {
for (Map.Entry<String, String> paramEntry : exParams.entrySet()) {
String paramEntryValue = paramEntry.getValue();
try {
Object result;
if (paramEntryValue.startsWith("'")) {
// but not require an ending quote
if (paramEntryValue.endsWith("'")) {
result = paramEntryValue.substring(1, paramEntryValue.length() - 1);
} else {
result = paramEntryValue.substring(1, paramEntryValue.length());
}
} else if (paramEntryValue.startsWith("/")) {
// treat this is an xpath
XPathPathExpr pathExpr = XPathReference.getPathExpr(paramEntryValue);
XPathNodeset xpathNodeset = pathExpr.eval(formInstance, evaluationContext);
result = XPathFuncExpr.unpack(xpathNodeset);
} else if (paramEntryValue.equals("instanceProviderID()")) {
// instanceProviderID returns -1 if the current instance has not been
// saved to disk already
String path = Collect.getInstance().getFormController().getInstanceFile().getAbsolutePath();
String instanceProviderID = "-1";
Cursor c = new InstancesDao().getInstancesCursorForFilePath(path);
if (c != null && c.getCount() > 0) {
// should only ever be one
c.moveToFirst();
instanceProviderID = c.getString(c.getColumnIndex(InstanceColumns._ID));
}
if (c != null) {
c.close();
}
result = instanceProviderID;
} else {
// treat this is a function
XPathExpression xpathExpression = XPathParseTool.parseXPath(paramEntryValue);
result = xpathExpression.eval(formInstance, evaluationContext);
}
if (result != null && result instanceof Serializable) {
intent.putExtra(paramEntry.getKey(), (Serializable) result);
}
} catch (Exception e) {
throw new ExternalParamsException("Could not evaluate '" + paramEntryValue + "'", e);
}
}
}
}
Aggregations