Search in sources :

Example 1 with ExternalParamsException

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);
    }
}
Also used : ExternalParamsException(org.odk.collect.android.exception.ExternalParamsException) Intent(android.content.Intent)

Example 2 with ExternalParamsException

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);
            }
        }
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) Serializable(java.io.Serializable) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) Cursor(android.database.Cursor) ExternalParamsException(org.odk.collect.android.exception.ExternalParamsException) ExternalParamsException(org.odk.collect.android.exception.ExternalParamsException) InstancesDao(org.odk.collect.android.dao.InstancesDao) FormDef(org.javarosa.core.model.FormDef) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ExternalParamsException (org.odk.collect.android.exception.ExternalParamsException)2 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Serializable (java.io.Serializable)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 FormDef (org.javarosa.core.model.FormDef)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 XPathNodeset (org.javarosa.xpath.XPathNodeset)1 XPathExpression (org.javarosa.xpath.expr.XPathExpression)1 XPathPathExpr (org.javarosa.xpath.expr.XPathPathExpr)1 InstancesDao (org.odk.collect.android.dao.InstancesDao)1