Search in sources :

Example 1 with FormInfo

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

the class ContentResolverHelper method getFormDetails.

public static FormInfo getFormDetails(Uri uri) {
    FormInfo formInfo = null;
    ContentResolver contentResolver = Collect.getInstance().getContentResolver();
    try (Cursor instanceCursor = contentResolver.query(uri, null, null, null, null)) {
        if (instanceCursor != null && instanceCursor.getCount() > 0) {
            instanceCursor.moveToFirst();
            String instancePath = instanceCursor.getString(instanceCursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH));
            String jrFormId = instanceCursor.getString(instanceCursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.JR_FORM_ID));
            int idxJrVersion = instanceCursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.JR_VERSION);
            String jrVersion = instanceCursor.isNull(idxJrVersion) ? null : instanceCursor.getString(idxJrVersion);
            formInfo = new FormInfo(instancePath, jrFormId, jrVersion);
        }
    }
    return formInfo;
}
Also used : FormInfo(org.odk.collect.android.logic.FormInfo) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver)

Example 2 with FormInfo

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

the class FormEntryActivity method onCreate.

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // external intent
    try {
        Collect.createODKDirs();
    } catch (RuntimeException e) {
        createErrorDialog(e.getMessage(), EXIT);
        return;
    }
    setContentView(R.layout.form_entry);
    errorMessage = null;
    beenSwiped = false;
    alertDialog = null;
    currentView = null;
    inAnimation = null;
    outAnimation = null;
    gestureDetector = new GestureDetector(this, this);
    questionHolder = findViewById(R.id.questionholder);
    initToolbar();
    nextButton = findViewById(R.id.form_forward_button);
    nextButton.setOnClickListener(v -> {
        beenSwiped = true;
        showNextView();
    });
    backButton = findViewById(R.id.form_back_button);
    backButton.setOnClickListener(v -> {
        beenSwiped = true;
        showPreviousView();
    });
    String startingXPath = null;
    String waitingXPath = null;
    String instancePath = null;
    boolean newForm = true;
    autoSaved = false;
    allowMovingBackwards = (boolean) AdminSharedPreferences.getInstance().get(KEY_MOVING_BACKWARDS);
    if (savedInstanceState != null) {
        state = savedInstanceState;
        if (savedInstanceState.containsKey(KEY_FORMPATH)) {
            formPath = savedInstanceState.getString(KEY_FORMPATH);
        }
        if (savedInstanceState.containsKey(KEY_INSTANCEPATH)) {
            instancePath = savedInstanceState.getString(KEY_INSTANCEPATH);
        }
        if (savedInstanceState.containsKey(KEY_XPATH)) {
            startingXPath = savedInstanceState.getString(KEY_XPATH);
            Timber.i("startingXPath is: %s", startingXPath);
        }
        if (savedInstanceState.containsKey(KEY_XPATH_WAITING_FOR_DATA)) {
            waitingXPath = savedInstanceState.getString(KEY_XPATH_WAITING_FOR_DATA);
            Timber.i("waitingXPath is: %s", waitingXPath);
        }
        if (savedInstanceState.containsKey(NEWFORM)) {
            newForm = savedInstanceState.getBoolean(NEWFORM, true);
        }
        if (savedInstanceState.containsKey(KEY_ERROR)) {
            errorMessage = savedInstanceState.getString(KEY_ERROR);
        }
        saveName = savedInstanceState.getString(KEY_SAVE_NAME);
        if (savedInstanceState.containsKey(KEY_AUTO_SAVED)) {
            autoSaved = savedInstanceState.getBoolean(KEY_AUTO_SAVED);
        }
    }
    // Dialogs mid form just disappear on rotation.
    if (errorMessage != null) {
        createErrorDialog(errorMessage, EXIT);
        return;
    }
    // Check to see if this is a screen flip or a new form load.
    Object data = getLastCustomNonConfigurationInstance();
    if (data instanceof FormLoaderTask) {
        formLoaderTask = (FormLoaderTask) data;
    } else if (data instanceof SaveToDiskTask) {
        saveToDiskTask = (SaveToDiskTask) data;
    } else if (data == null) {
        if (!newForm) {
            if (getFormController() != null) {
                refreshCurrentView();
            } else {
                Timber.w("Reloading form and restoring state.");
                // we need to launch the form loader to load the form
                // controller...
                formLoaderTask = new FormLoaderTask(instancePath, startingXPath, waitingXPath);
                Collect.getInstance().getActivityLogger().logAction(this, "formReloaded", formPath);
                // TODO: this doesn' work (dialog does not get removed):
                // showDialog(PROGRESS_DIALOG);
                // show dialog before we execute...
                formLoaderTask.execute(formPath);
            }
            return;
        }
        // Not a restart from a screen orientation change (or other).
        Collect.getInstance().setFormController(null);
        supportInvalidateOptionsMenu();
        Intent intent = getIntent();
        if (intent != null) {
            Uri uri = intent.getData();
            String uriMimeType = null;
            if (uri != null) {
                uriMimeType = getContentResolver().getType(uri);
            }
            if (uriMimeType == null && intent.hasExtra(EXTRA_TESTING_PATH)) {
                formPath = intent.getStringExtra(EXTRA_TESTING_PATH);
            } else if (uriMimeType != null && uriMimeType.equals(InstanceColumns.CONTENT_ITEM_TYPE)) {
                // get the formId and version for this instance...
                FormInfo formInfo = ContentResolverHelper.getFormDetails(uri);
                if (formInfo == null) {
                    createErrorDialog(getString(R.string.bad_uri, uri), EXIT);
                    return;
                }
                instancePath = formInfo.getInstancePath();
                Collect.getInstance().getActivityLogger().logAction(this, "instanceLoaded", instancePath);
                String jrFormId = formInfo.getFormID();
                String jrVersion = formInfo.getFormVersion();
                String[] selectionArgs;
                String selection;
                if (jrVersion == null) {
                    selectionArgs = new String[] { jrFormId };
                    selection = FormsColumns.JR_FORM_ID + "=? AND " + FormsColumns.JR_VERSION + " IS NULL";
                } else {
                    selectionArgs = new String[] { jrFormId, jrVersion };
                    selection = FormsColumns.JR_FORM_ID + "=? AND " + FormsColumns.JR_VERSION + "=?";
                }
                int formCount = FormsDaoHelper.getFormsCount(selection, selectionArgs);
                if (formCount < 1) {
                    createErrorDialog(getString(R.string.parent_form_not_present, jrFormId) + ((jrVersion == null) ? "" : "\n" + getString(R.string.version) + " " + jrVersion), EXIT);
                    return;
                } else {
                    formPath = FormsDaoHelper.getFormPath(selection, selectionArgs);
                    // database to fix it.
                    if (formCount > 1) {
                        createErrorDialog(getString(R.string.survey_multiple_forms_error), EXIT);
                        return;
                    }
                }
            } else if (uriMimeType != null && uriMimeType.equals(FormsColumns.CONTENT_ITEM_TYPE)) {
                formPath = ContentResolverHelper.getFormPath(uri);
                if (formPath == null) {
                    createErrorDialog(getString(R.string.bad_uri, uri), EXIT);
                    return;
                } else {
                    // This is the fill-blank-form code path.
                    // See if there is a savepoint for this form that
                    // has never been
                    // explicitly saved
                    // by the user. If there is, open this savepoint
                    // (resume this filled-in
                    // form).
                    // Savepoints for forms that were explicitly saved
                    // will be recovered
                    // when that
                    // explicitly saved instance is edited via
                    // edit-saved-form.
                    final String filePrefix = formPath.substring(formPath.lastIndexOf('/') + 1, formPath.lastIndexOf('.')) + "_";
                    final String fileSuffix = ".xml.save";
                    File cacheDir = new File(Collect.CACHE_PATH);
                    File[] files = cacheDir.listFiles(pathname -> {
                        String name = pathname.getName();
                        return name.startsWith(filePrefix) && name.endsWith(fileSuffix);
                    });
                    // explicitly saved by the user...
                    for (File candidate : files) {
                        String instanceDirName = candidate.getName().substring(0, candidate.getName().length() - fileSuffix.length());
                        File instanceDir = new File(Collect.INSTANCES_PATH + File.separator + instanceDirName);
                        File instanceFile = new File(instanceDir, instanceDirName + ".xml");
                        if (instanceDir.exists() && instanceDir.isDirectory() && !instanceFile.exists()) {
                            // yes! -- use this savepoint file
                            instancePath = instanceFile.getAbsolutePath();
                            break;
                        }
                    }
                }
            } else {
                Timber.e("Unrecognized URI: %s", uri);
                createErrorDialog(getString(R.string.unrecognized_uri, uri), EXIT);
                return;
            }
            formLoaderTask = new FormLoaderTask(instancePath, null, null);
            Collect.getInstance().getActivityLogger().logAction(this, "formLoaded", formPath);
            showDialog(PROGRESS_DIALOG);
            // show dialog before we execute...
            formLoaderTask.execute(formPath);
        }
    }
}
Also used : FormInfo(org.odk.collect.android.logic.FormInfo) GestureDetector(android.view.GestureDetector) Intent(android.content.Intent) SaveToDiskTask(org.odk.collect.android.tasks.SaveToDiskTask) Uri(android.net.Uri) File(java.io.File) FormLoaderTask(org.odk.collect.android.tasks.FormLoaderTask)

Aggregations

FormInfo (org.odk.collect.android.logic.FormInfo)2 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 GestureDetector (android.view.GestureDetector)1 File (java.io.File)1 FormLoaderTask (org.odk.collect.android.tasks.FormLoaderTask)1 SaveToDiskTask (org.odk.collect.android.tasks.SaveToDiskTask)1