Search in sources :

Example 6 with FormsDao

use of org.odk.collect.android.dao.FormsDao in project collect by opendatakit.

the class DownloadFormsTask method doInBackground.

@Override
protected HashMap<FormDetails, String> doInBackground(ArrayList<FormDetails>... values) {
    ArrayList<FormDetails> toDownload = values[0];
    formsDao = new FormsDao();
    int total = toDownload.size();
    int count = 1;
    Collect.getInstance().getActivityLogger().logAction(this, "downloadForms", String.valueOf(total));
    final HashMap<FormDetails, String> result = new HashMap<>();
    for (FormDetails fd : toDownload) {
        try {
            String message = processOneForm(total, count++, fd);
            result.put(fd, message.isEmpty() ? Collect.getInstance().getString(R.string.success) : message);
        } catch (TaskCancelledException cd) {
            break;
        }
    }
    return result;
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) FormDetails(org.odk.collect.android.logic.FormDetails) HashMap(java.util.HashMap)

Example 7 with FormsDao

use of org.odk.collect.android.dao.FormsDao in project collect by opendatakit.

the class InstanceUploader method isFormAutoDeleteEnabled.

/**
 * @param isFormAutoDeleteOptionEnabled represents whether the auto-delete option is enabled at the app level
 *
 * If the form explicitly sets the auto-delete property, then it overrides the preferences.
 */
private boolean isFormAutoDeleteEnabled(String jrFormId, boolean isFormAutoDeleteOptionEnabled) {
    Cursor cursor = new FormsDao().getFormsCursorForFormId(jrFormId);
    String autoDelete = null;
    if (cursor != null && cursor.moveToFirst()) {
        try {
            int autoDeleteColumnIndex = cursor.getColumnIndex(AUTO_DELETE);
            autoDelete = cursor.getString(autoDeleteColumnIndex);
        } finally {
            cursor.close();
        }
    }
    return autoDelete == null ? isFormAutoDeleteOptionEnabled : Boolean.valueOf(autoDelete);
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) Cursor(android.database.Cursor)

Example 8 with FormsDao

use of org.odk.collect.android.dao.FormsDao in project collect by opendatakit.

the class EncryptionUtils method getEncryptedFormInformation.

/**
 * Retrieve the encryption information for this uri.
 *
 * @param uri either an instance URI (if previously saved) or a form URI
 */
public static EncryptedFormInformation getEncryptedFormInformation(Uri uri, InstanceMetadata instanceMetadata) throws EncryptionException {
    ContentResolver cr = Collect.getInstance().getContentResolver();
    // fetch the form information
    String formId;
    String formVersion;
    PublicKey pk;
    Cursor formCursor = null;
    try {
        if (cr.getType(uri) == InstanceColumns.CONTENT_ITEM_TYPE) {
            // chain back to the Form record...
            String[] selectionArgs = null;
            String selection = null;
            Cursor instanceCursor = null;
            try {
                instanceCursor = cr.query(uri, null, null, null, null);
                if (instanceCursor.getCount() != 1) {
                    String msg = Collect.getInstance().getString(R.string.not_exactly_one_record_for_this_instance);
                    Timber.e(msg);
                    throw new EncryptionException(msg, null);
                }
                instanceCursor.moveToFirst();
                String jrFormId = instanceCursor.getString(instanceCursor.getColumnIndex(InstanceColumns.JR_FORM_ID));
                int idxJrVersion = instanceCursor.getColumnIndex(InstanceColumns.JR_VERSION);
                if (!instanceCursor.isNull(idxJrVersion)) {
                    selectionArgs = new String[] { jrFormId, instanceCursor.getString(idxJrVersion) };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + "=?";
                } else {
                    selectionArgs = new String[] { jrFormId };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + " IS NULL";
                }
            } finally {
                if (instanceCursor != null) {
                    instanceCursor.close();
                }
            }
            formCursor = new FormsDao().getFormsCursor(selection, selectionArgs);
            if (formCursor.getCount() != 1) {
                String msg = Collect.getInstance().getString(R.string.not_exactly_one_blank_form_for_this_form_id);
                Timber.e(msg);
                throw new EncryptionException(msg, null);
            }
            formCursor.moveToFirst();
        } else if (cr.getType(uri) == FormsColumns.CONTENT_ITEM_TYPE) {
            formCursor = cr.query(uri, null, null, null, null);
            if (formCursor.getCount() != 1) {
                String msg = Collect.getInstance().getString(R.string.not_exactly_one_blank_form_for_this_form_id);
                Timber.e(msg);
                throw new EncryptionException(msg, null);
            }
            formCursor.moveToFirst();
        }
        formId = formCursor.getString(formCursor.getColumnIndex(FormsColumns.JR_FORM_ID));
        if (formId == null || formId.length() == 0) {
            String msg = Collect.getInstance().getString(R.string.no_form_id_specified);
            Timber.e(msg);
            throw new EncryptionException(msg, null);
        }
        int idxVersion = formCursor.getColumnIndex(FormsColumns.JR_VERSION);
        int idxBase64RsaPublicKey = formCursor.getColumnIndex(FormsColumns.BASE64_RSA_PUBLIC_KEY);
        formVersion = formCursor.isNull(idxVersion) ? null : formCursor.getString(idxVersion);
        String base64RsaPublicKey = formCursor.isNull(idxBase64RsaPublicKey) ? null : formCursor.getString(idxBase64RsaPublicKey);
        if (base64RsaPublicKey == null || base64RsaPublicKey.length() == 0) {
            // this is legitimately not an encrypted form
            return null;
        }
        byte[] publicKey = Base64.decode(base64RsaPublicKey, Base64.NO_WRAP);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf;
        try {
            kf = KeyFactory.getInstance(RSA_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            String msg = Collect.getInstance().getString(R.string.phone_does_not_support_rsa);
            Timber.e(e, "%s due to %s ", msg, e.getMessage());
            throw new EncryptionException(msg, e);
        }
        try {
            pk = kf.generatePublic(publicKeySpec);
        } catch (InvalidKeySpecException e) {
            String msg = Collect.getInstance().getString(R.string.invalid_rsa_public_key);
            Timber.e(e, "%s due to %s ", msg, e.getMessage());
            throw new EncryptionException(msg, e);
        }
    } finally {
        if (formCursor != null) {
            formCursor.close();
        }
    }
    // instanceID value.
    if (instanceMetadata.instanceId == null) {
        Timber.e("No OpenRosa metadata block or no instanceId defined in that block");
        return null;
    }
    // https://code.google.com/p/opendatakit/issues/detail?id=918
    try {
        Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM, ENCRYPTION_PROVIDER);
    } catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
        String msg;
        if (e instanceof NoSuchAlgorithmException) {
            msg = "No BouncyCastle implementation of symmetric algorithm!";
        } else if (e instanceof NoSuchProviderException) {
            msg = "No BouncyCastle provider implementation of symmetric algorithm!";
        } else {
            msg = "No BouncyCastle provider for padding implementation of symmetric algorithm!";
        }
        Timber.e(msg);
        return null;
    }
    return new EncryptedFormInformation(formId, formVersion, instanceMetadata, pk);
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) PublicKey(java.security.PublicKey) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) EncryptionException(org.odk.collect.android.exception.EncryptionException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

Example 9 with FormsDao

use of org.odk.collect.android.dao.FormsDao in project collect by opendatakit.

the class ResetUtility method resetForms.

private void resetForms() {
    new FormsDao().deleteFormsDatabase();
    File itemsetDbFile = new File(Collect.METADATA_PATH + File.separator + ItemsetDbAdapter.DATABASE_NAME);
    if (deleteFolderContents(Collect.FORMS_PATH) && (!itemsetDbFile.exists() || itemsetDbFile.delete())) {
        failedResetActions.remove(failedResetActions.indexOf(ResetAction.RESET_FORMS));
    }
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) File(java.io.File)

Example 10 with FormsDao

use of org.odk.collect.android.dao.FormsDao in project collect by opendatakit.

the class ViewSentListAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    String formId = getCursor().getString(getCursor().getColumnIndex(InstanceProviderAPI.InstanceColumns.JR_FORM_ID));
    Cursor cursor = new FormsDao().getFormsCursorForFormId(formId);
    boolean formExists = false;
    boolean isFormEncrypted = false;
    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                int base64RSAPublicKeyColumnIndex = cursor.getColumnIndex(FormsProviderAPI.FormsColumns.BASE64_RSA_PUBLIC_KEY);
                String base64RSAPublicKey = cursor.getString(base64RSAPublicKeyColumnIndex);
                isFormEncrypted = base64RSAPublicKey != null && !base64RSAPublicKey.isEmpty();
                formExists = true;
            }
        } finally {
            cursor.close();
        }
    }
    TextView visibilityOffCause = view.findViewById(R.id.text4);
    ImageView visibleOff = view.findViewById(R.id.visible_off);
    Long date = getCursor().getLong(getCursor().getColumnIndex(InstanceProviderAPI.InstanceColumns.DELETED_DATE));
    visibleOff.setScaleX(0.9f);
    visibleOff.setScaleY(0.9f);
    if (date != 0 || !formExists || isFormEncrypted) {
        visibilityOffCause.setVisibility(View.VISIBLE);
        visibleOff.setVisibility(View.VISIBLE);
        if (date != 0) {
            visibilityOffCause.setText(new SimpleDateFormat(context.getString(R.string.deleted_on_date_at_time), Locale.getDefault()).format(new Date(date)));
        } else if (!formExists) {
            visibilityOffCause.setText(context.getString(R.string.deleted_form));
        } else {
            visibilityOffCause.setText(context.getString(R.string.encrypted_form));
        }
    } else {
        visibilityOffCause.setVisibility(View.GONE);
        visibleOff.setVisibility(View.GONE);
    }
    return view;
}
Also used : FormsDao(org.odk.collect.android.dao.FormsDao) TextView(android.widget.TextView) ImageView(android.widget.ImageView) Cursor(android.database.Cursor) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

FormsDao (org.odk.collect.android.dao.FormsDao)12 Cursor (android.database.Cursor)9 ContentValues (android.content.ContentValues)3 Uri (android.net.Uri)3 File (java.io.File)3 DialogInterface (android.content.DialogInterface)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AlertDialog (android.app.AlertDialog)1 ContentResolver (android.content.ContentResolver)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 SQLException (android.database.SQLException)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 PublicKey (java.security.PublicKey)1