Search in sources :

Example 1 with JavaRosaException

use of org.odk.collect.android.exception.JavaRosaException in project collect by opendatakit.

the class FormController method isCurrentQuestionFirstInForm.

public boolean isCurrentQuestionFirstInForm() {
    boolean isFirstQuestion = true;
    FormIndex originalFormIndex = getFormIndex();
    try {
        isFirstQuestion = stepToPreviousScreenEvent() == FormEntryController.EVENT_BEGINNING_OF_FORM && stepToNextScreenEvent() != FormEntryController.EVENT_PROMPT_NEW_REPEAT;
    } catch (JavaRosaException e) {
        Timber.d(e);
    }
    jumpToIndex(originalFormIndex);
    return isFirstQuestion;
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 2 with JavaRosaException

use of org.odk.collect.android.exception.JavaRosaException in project collect by opendatakit.

the class EditFormHierarchyActivity method onItemClick.

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    HierarchyElement h = (HierarchyElement) listView.getItemAtPosition(position);
    FormIndex index = h.getFormIndex();
    if (index == null) {
        goUpLevel();
        return;
    }
    switch(h.getType()) {
        case EXPANDED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "COLLAPSED", h.getFormIndex());
            h.setType(COLLAPSED);
            ArrayList<HierarchyElement> children = h.getChildren();
            for (int i = 0; i < children.size(); i++) {
                formList.remove(position + 1);
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_minimized));
            break;
        case COLLAPSED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "EXPANDED", h.getFormIndex());
            h.setType(EXPANDED);
            ArrayList<HierarchyElement> children1 = h.getChildren();
            for (int i = 0; i < children1.size(); i++) {
                Timber.i("adding child: %s", children1.get(i).getFormIndex());
                formList.add(position + 1 + i, children1.get(i));
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_maximized));
            break;
        case QUESTION:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "QUESTION-JUMP", index);
            Collect.getInstance().getFormController().jumpToIndex(index);
            if (Collect.getInstance().getFormController().indexIsInFieldList()) {
                try {
                    Collect.getInstance().getFormController().stepToPreviousScreenEvent();
                } catch (JavaRosaException e) {
                    Timber.d(e);
                    createErrorDialog(e.getCause().getMessage());
                    return;
                }
            }
            setResult(RESULT_OK);
            finish();
            return;
        case CHILD:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "REPEAT-JUMP", h.getFormIndex());
            Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
            setResult(RESULT_OK);
            refreshView();
            return;
    }
    // Should only get here if we've expanded or collapsed a group
    HierarchyListAdapter itla = new HierarchyListAdapter(this);
    itla.setListItems(formList);
    listView.setAdapter(itla);
    listView.setSelection(position);
}
Also used : HierarchyElement(org.odk.collect.android.logic.HierarchyElement) HierarchyListAdapter(org.odk.collect.android.adapters.HierarchyListAdapter) FormIndex(org.javarosa.core.model.FormIndex) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 3 with JavaRosaException

use of org.odk.collect.android.exception.JavaRosaException in project collect by opendatakit.

the class FormEntryActivity method showNextView.

/**
 * Determines what should be displayed on the screen. Possible options are:
 * a question, an ask repeat dialog, or the submit screen. Also saves
 * answers to the data model after checking constraints.
 */
private void showNextView() {
    state = null;
    try {
        FormController formController = getFormController();
        // get constraint behavior preference value with appropriate default
        String constraintBehavior = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_CONSTRAINT_BEHAVIOR);
        if (formController != null && formController.currentPromptIsQuestion()) {
            // if constraint behavior says we should validate on swipe, do so
            if (constraintBehavior.equals(PreferenceKeys.CONSTRAINT_BEHAVIOR_ON_SWIPE)) {
                if (!saveAnswersForCurrentScreen(EVALUATE_CONSTRAINTS)) {
                    // A constraint was violated so a dialog should be showing.
                    beenSwiped = false;
                    return;
                }
            // otherwise, just save without validating (constraints will be validated on
            // finalize)
            } else {
                saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
            }
        }
        View next;
        int originalEvent = formController.getEvent();
        int event = formController.stepToNextScreenEvent();
        // she will stay on the same screen)
        if (originalEvent == event && originalEvent == FormEntryController.EVENT_END_OF_FORM) {
            beenSwiped = false;
            return;
        }
        // Close timer events waiting for an end time
        formController.getTimerLogger().exitView();
        switch(event) {
            case FormEntryController.EVENT_QUESTION:
            case FormEntryController.EVENT_GROUP:
                // create a savepoint
                if ((++viewCount) % SAVEPOINT_INTERVAL == 0) {
                    nonblockingCreateSavePointData();
                }
                next = createView(event, true);
                showView(next, AnimationType.RIGHT);
                break;
            case FormEntryController.EVENT_END_OF_FORM:
            case FormEntryController.EVENT_REPEAT:
            case FormEntryController.EVENT_PROMPT_NEW_REPEAT:
                next = createView(event, true);
                showView(next, AnimationType.RIGHT);
                break;
            case FormEntryController.EVENT_REPEAT_JUNCTURE:
                Timber.i("Repeat juncture: %s", formController.getFormIndex().getReference());
                // skip repeat junctures until we implement them
                break;
            default:
                Timber.w("JavaRosa added a new EVENT type and didn't tell us... shame on them.");
                break;
        }
    } catch (JavaRosaException e) {
        Timber.d(e);
        createErrorDialog(e.getCause().getMessage(), DO_NOT_EXIT);
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) ODKView(org.odk.collect.android.views.ODKView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) FailedConstraint(org.odk.collect.android.logic.FormController.FailedConstraint) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 4 with JavaRosaException

use of org.odk.collect.android.exception.JavaRosaException in project collect by opendatakit.

the class FormEntryActivity method createView.

/**
 * Creates a view given the View type and an event
 *
 * @param advancingPage -- true if this results from advancing through the form
 * @return newly created View
 */
private View createView(int event, boolean advancingPage) {
    FormController formController = getFormController();
    setTitle(formController.getFormTitle());
    formController.getTimerLogger().logTimerEvent(TimerLogger.EventTypes.FEC, event, formController.getFormIndex().getReference(), advancingPage, true);
    switch(event) {
        case FormEntryController.EVENT_BEGINNING_OF_FORM:
            return createViewForFormBeginning(event, true, formController);
        case FormEntryController.EVENT_END_OF_FORM:
            View endView = View.inflate(this, R.layout.form_entry_end, null);
            ((TextView) endView.findViewById(R.id.description)).setText(getString(R.string.save_enter_data_description, formController.getFormTitle()));
            // checkbox for if finished or ready to send
            final CheckBox instanceComplete = endView.findViewById(R.id.mark_finished);
            instanceComplete.setChecked(InstancesDaoHelper.isInstanceComplete(true));
            if (!(boolean) AdminSharedPreferences.getInstance().get(AdminKeys.KEY_MARK_AS_FINALIZED)) {
                instanceComplete.setVisibility(View.GONE);
            }
            // edittext to change the displayed name of the instance
            final EditText saveAs = endView.findViewById(R.id.save_name);
            // disallow carriage returns in the name
            InputFilter returnFilter = new InputFilter() {

                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    for (int i = start; i < end; i++) {
                        if (Character.getType((source.charAt(i))) == Character.CONTROL) {
                            return "";
                        }
                    }
                    return null;
                }
            };
            saveAs.setFilters(new InputFilter[] { returnFilter });
            if (formController.getSubmissionMetadata().instanceName == null) {
                // no meta/instanceName field in the form -- see if we have a
                // name for this instance from a previous save attempt...
                String uriMimeType = null;
                Uri instanceUri = getIntent().getData();
                if (instanceUri != null) {
                    uriMimeType = getContentResolver().getType(instanceUri);
                }
                if (saveName == null && uriMimeType != null && uriMimeType.equals(InstanceColumns.CONTENT_ITEM_TYPE)) {
                    Cursor instance = null;
                    try {
                        instance = getContentResolver().query(instanceUri, null, null, null, null);
                        if (instance != null && instance.getCount() == 1) {
                            instance.moveToFirst();
                            saveName = instance.getString(instance.getColumnIndex(InstanceColumns.DISPLAY_NAME));
                        }
                    } finally {
                        if (instance != null) {
                            instance.close();
                        }
                    }
                }
                if (saveName == null) {
                    // last resort, default to the form title
                    saveName = formController.getFormTitle();
                }
                // present the prompt to allow user to name the form
                TextView sa = endView.findViewById(R.id.save_form_as);
                sa.setVisibility(View.VISIBLE);
                saveAs.setText(saveName);
                saveAs.setEnabled(true);
                saveAs.setVisibility(View.VISIBLE);
                saveAs.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void afterTextChanged(Editable s) {
                        saveName = String.valueOf(s);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }
                });
            } else {
                // if instanceName is defined in form, this is the name -- no
                // revisions
                // display only the name, not the prompt, and disable edits
                saveName = formController.getSubmissionMetadata().instanceName;
                TextView sa = endView.findViewById(R.id.save_form_as);
                sa.setVisibility(View.GONE);
                saveAs.setText(saveName);
                saveAs.setEnabled(false);
                saveAs.setVisibility(View.VISIBLE);
            }
            // override the visibility settings based upon admin preferences
            if (!(boolean) AdminSharedPreferences.getInstance().get(AdminKeys.KEY_SAVE_AS)) {
                saveAs.setVisibility(View.GONE);
                TextView sa = endView.findViewById(R.id.save_form_as);
                sa.setVisibility(View.GONE);
            }
            // Create 'save' button
            endView.findViewById(R.id.save_exit_button).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Collect.getInstance().getActivityLogger().logInstanceAction(this, "createView.saveAndExit", instanceComplete.isChecked() ? "saveAsComplete" : "saveIncomplete");
                    // Form is marked as 'saved' here.
                    if (saveAs.getText().length() < 1) {
                        ToastUtils.showShortToast(R.string.save_as_error);
                    } else {
                        saveDataToDisk(EXIT, instanceComplete.isChecked(), saveAs.getText().toString());
                    }
                }
            });
            if (showNavigationButtons) {
                backButton.setEnabled(allowMovingBackwards);
                nextButton.setEnabled(false);
            }
            return endView;
        case FormEntryController.EVENT_QUESTION:
        case FormEntryController.EVENT_GROUP:
        case FormEntryController.EVENT_REPEAT:
            releaseOdkView();
            // should only be a group here if the event_group is a field-list
            try {
                FormEntryPrompt[] prompts = formController.getQuestionPrompts();
                FormEntryCaption[] groups = formController.getGroupsForCurrentIndex();
                odkView = new ODKView(this, prompts, groups, advancingPage);
                Timber.i("Created view for group %s %s", (groups.length > 0 ? groups[groups.length - 1].getLongText() : "[top]"), (prompts.length > 0 ? prompts[0].getQuestionText() : "[no question]"));
            } catch (RuntimeException e) {
                Timber.e(e);
                // this is badness to avoid a crash.
                try {
                    event = formController.stepToNextScreenEvent();
                    createErrorDialog(e.getMessage(), DO_NOT_EXIT);
                } catch (JavaRosaException e1) {
                    Timber.d(e1);
                    createErrorDialog(e.getMessage() + "\n\n" + e1.getCause().getMessage(), DO_NOT_EXIT);
                }
                return createView(event, advancingPage);
            }
            // Makes a "clear answer" menu pop up on long-click
            for (QuestionWidget qw : odkView.getWidgets()) {
                if (!qw.getFormEntryPrompt().isReadOnly()) {
                    // we want to enable paste option after long click on the EditText
                    if (qw instanceof StringWidget) {
                        for (int i = 0; i < qw.getChildCount(); i++) {
                            if (!(qw.getChildAt(i) instanceof EditText)) {
                                registerForContextMenu(qw.getChildAt(i));
                            }
                        }
                    } else {
                        registerForContextMenu(qw);
                    }
                }
            }
            if (showNavigationButtons) {
                backButton.setEnabled(!formController.isCurrentQuestionFirstInForm() && allowMovingBackwards);
                nextButton.setEnabled(true);
            }
            return odkView;
        case FormEntryController.EVENT_PROMPT_NEW_REPEAT:
            createRepeatDialog();
            return new EmptyView(this);
        default:
            Timber.e("Attempted to create a view that does not exist.");
            // this is badness to avoid a crash.
            try {
                event = formController.stepToNextScreenEvent();
                createErrorDialog(getString(R.string.survey_internal_error), EXIT);
            } catch (JavaRosaException e) {
                Timber.d(e);
                createErrorDialog(e.getCause().getMessage(), EXIT);
            }
            return createView(event, advancingPage);
    }
}
Also used : FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) ODKView(org.odk.collect.android.views.ODKView) FormEntryCaption(org.javarosa.form.api.FormEntryCaption) Cursor(android.database.Cursor) Uri(android.net.Uri) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) StringWidget(org.odk.collect.android.widgets.StringWidget) QuestionWidget(org.odk.collect.android.widgets.QuestionWidget) FormController(org.odk.collect.android.logic.FormController) EditText(android.widget.EditText) InputFilter(android.text.InputFilter) ODKView(org.odk.collect.android.views.ODKView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) Spanned(android.text.Spanned) FailedConstraint(org.odk.collect.android.logic.FormController.FailedConstraint) CheckBox(android.widget.CheckBox) OnClickListener(android.view.View.OnClickListener) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 5 with JavaRosaException

use of org.odk.collect.android.exception.JavaRosaException in project collect by opendatakit.

the class ViewFormHierarchyActivity method onItemClick.

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    HierarchyElement h = (HierarchyElement) listView.getItemAtPosition(position);
    FormIndex index = h.getFormIndex();
    if (index == null) {
        goUpLevel();
        return;
    }
    switch(h.getType()) {
        case EXPANDED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "COLLAPSED", h.getFormIndex());
            h.setType(COLLAPSED);
            ArrayList<HierarchyElement> children = h.getChildren();
            for (int i = 0; i < children.size(); i++) {
                formList.remove(position + 1);
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_minimized));
            break;
        case COLLAPSED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "EXPANDED", h.getFormIndex());
            h.setType(EXPANDED);
            ArrayList<HierarchyElement> children1 = h.getChildren();
            for (int i = 0; i < children1.size(); i++) {
                Timber.i("adding child: %s", children1.get(i).getFormIndex());
                formList.add(position + 1 + i, children1.get(i));
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_maximized));
            break;
        case QUESTION:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "QUESTION-JUMP", index);
            Collect.getInstance().getFormController().jumpToIndex(index);
            if (Collect.getInstance().getFormController().indexIsInFieldList()) {
                try {
                    Collect.getInstance().getFormController().stepToPreviousScreenEvent();
                } catch (JavaRosaException e) {
                    Timber.d(e);
                    createErrorDialog(e.getCause().getMessage());
                    return;
                }
            }
            setResult(RESULT_OK);
            return;
        case CHILD:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "REPEAT-JUMP", h.getFormIndex());
            Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
            setResult(RESULT_OK);
            refreshView();
            return;
    }
    // Should only get here if we've expanded or collapsed a group
    HierarchyListAdapter itla = new HierarchyListAdapter(this);
    itla.setListItems(formList);
    listView.setAdapter(itla);
    listView.setSelection(position);
}
Also used : HierarchyElement(org.odk.collect.android.logic.HierarchyElement) HierarchyListAdapter(org.odk.collect.android.adapters.HierarchyListAdapter) FormIndex(org.javarosa.core.model.FormIndex) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Aggregations

JavaRosaException (org.odk.collect.android.exception.JavaRosaException)10 FormController (org.odk.collect.android.logic.FormController)6 FormIndex (org.javarosa.core.model.FormIndex)5 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 FailedConstraint (org.odk.collect.android.logic.FormController.FailedConstraint)3 ODKView (org.odk.collect.android.views.ODKView)3 Uri (android.net.Uri)2 OnClickListener (android.view.View.OnClickListener)2 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)2 HierarchyListAdapter (org.odk.collect.android.adapters.HierarchyListAdapter)2 GDriveConnectionException (org.odk.collect.android.exception.GDriveConnectionException)2 HierarchyElement (org.odk.collect.android.logic.HierarchyElement)2 DialogInterface (android.content.DialogInterface)1 Cursor (android.database.Cursor)1 Bundle (android.os.Bundle)1 Editable (android.text.Editable)1 InputFilter (android.text.InputFilter)1