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;
}
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);
}
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);
}
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));
}
}
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;
}
Aggregations