|
| 1 | +/* |
| 2 | + * Business Source License 1.0 © 2017 FlowCrypt Limited ([email protected]). |
| 3 | + * Use limitations apply. See https://github.com/FlowCrypt/flowcrypt-android/blob/master/LICENSE |
| 4 | + * Contributors: DenBond7 |
| 5 | + */ |
| 6 | + |
| 7 | +package com.flowcrypt.email.database.dao.source.imap; |
| 8 | + |
| 9 | +import android.content.ContentResolver; |
| 10 | +import android.content.ContentValues; |
| 11 | +import android.content.Context; |
| 12 | +import android.database.Cursor; |
| 13 | +import android.net.Uri; |
| 14 | +import android.provider.BaseColumns; |
| 15 | +import android.support.annotation.NonNull; |
| 16 | + |
| 17 | +import com.flowcrypt.email.api.email.model.AttachmentInfo; |
| 18 | +import com.flowcrypt.email.database.dao.source.BaseDaoSource; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Denis Bondarenko |
| 25 | + * Date: 08.08.2017 |
| 26 | + * Time: 10:41 |
| 27 | + |
| 28 | + */ |
| 29 | + |
| 30 | +public class AttachmentDaoSource extends BaseDaoSource { |
| 31 | + public static final String TABLE_NAME_ATTACHMENT = "attachment"; |
| 32 | + |
| 33 | + public static final String COL_EMAIL = "email"; |
| 34 | + public static final String COL_FOLDER = "folder"; |
| 35 | + public static final String COL_UID = "uid"; |
| 36 | + public static final String COL_NAME = "name"; |
| 37 | + public static final String COL_ENCODED_SIZE_IN_BYTES = "encodedSize"; |
| 38 | + public static final String COL_TYPE = "type"; |
| 39 | + |
| 40 | + public static final String ATTACHMENT_TABLE_SQL_CREATE = "CREATE TABLE IF NOT EXISTS " + |
| 41 | + TABLE_NAME_ATTACHMENT + " (" + |
| 42 | + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + |
| 43 | + COL_EMAIL + " VARCHAR(100) NOT NULL, " + |
| 44 | + COL_FOLDER + " TEXT NOT NULL, " + |
| 45 | + COL_UID + " INTEGER NOT NULL, " + |
| 46 | + COL_NAME + " TEXT NOT NULL, " + |
| 47 | + COL_ENCODED_SIZE_IN_BYTES + " INTEGER DEFAULT 0, " + |
| 48 | + COL_TYPE + " VARCHAR(100) NOT NULL " + ");"; |
| 49 | + |
| 50 | + public static final String CREATE_INDEX_EMAIL_UID_FOLDER_IN_MESSAGES = |
| 51 | + "CREATE INDEX IF NOT EXISTS " + COL_EMAIL + "_" + COL_UID + "_" + COL_FOLDER |
| 52 | + + "_in_" + TABLE_NAME_ATTACHMENT + " ON " + TABLE_NAME_ATTACHMENT + |
| 53 | + " (" + COL_EMAIL + ", " + COL_UID + ", " + COL_FOLDER + ")"; |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getTableName() { |
| 57 | + return TABLE_NAME_ATTACHMENT; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Add a new attachment details to the database. |
| 62 | + * |
| 63 | + * @param context Interface to global information about an application environment. |
| 64 | + * @param email The email that the message linked. |
| 65 | + * @param label The folder label where exists message which contains a current |
| 66 | + * attachment. |
| 67 | + * @param uid The message UID. |
| 68 | + * @param attachmentInfo The attachment details which will be added to the database. |
| 69 | + * @return A {@link Uri} of the created row. |
| 70 | + */ |
| 71 | + public Uri addRow(Context context, String email, String label, long uid, |
| 72 | + AttachmentInfo attachmentInfo) { |
| 73 | + ContentResolver contentResolver = context.getContentResolver(); |
| 74 | + if (attachmentInfo != null && label != null && contentResolver != null) { |
| 75 | + ContentValues contentValues = prepareContentValues(email, label, uid, attachmentInfo); |
| 76 | + return contentResolver.insert(getBaseContentUri(), contentValues); |
| 77 | + } else return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * This method add rows per single transaction. |
| 82 | + * |
| 83 | + * @param context Interface to global information about an application environment. |
| 84 | + * @param email The email that the message linked. |
| 85 | + * @param label The folder label where exists message which contains the current |
| 86 | + * attachments. |
| 87 | + * @param uid The message UID. |
| 88 | + * @param attachmentInfoList The attachments list. |
| 89 | + * @return the number of newly created rows. |
| 90 | + */ |
| 91 | + public int addRows(Context context, String email, String label, long uid, |
| 92 | + List<AttachmentInfo> attachmentInfoList) { |
| 93 | + if (attachmentInfoList != null) { |
| 94 | + ContentResolver contentResolver = context.getContentResolver(); |
| 95 | + ContentValues[] contentValuesArray = new ContentValues[attachmentInfoList.size()]; |
| 96 | + |
| 97 | + for (int i = 0; i < attachmentInfoList.size(); i++) { |
| 98 | + AttachmentInfo attachmentInfo = attachmentInfoList.get(i); |
| 99 | + ContentValues contentValues = |
| 100 | + prepareContentValues(email, label, uid, attachmentInfo); |
| 101 | + |
| 102 | + contentValuesArray[i] = contentValues; |
| 103 | + } |
| 104 | + |
| 105 | + return contentResolver.bulkInsert(getBaseContentUri(), contentValuesArray); |
| 106 | + } else return 0; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Generate an {@link AttachmentInfo} object from the current cursor position. |
| 111 | + * |
| 112 | + * @param cursor The {@link Cursor} which contains an information about an |
| 113 | + * {@link AttachmentInfo} object. |
| 114 | + * @return A generated {@link AttachmentInfo}. |
| 115 | + */ |
| 116 | + public AttachmentInfo getAttachmentInfo(Cursor cursor) { |
| 117 | + return new AttachmentInfo( |
| 118 | + cursor.getString(cursor.getColumnIndex(COL_NAME)), |
| 119 | + cursor.getInt(cursor.getColumnIndex(COL_ENCODED_SIZE_IN_BYTES)), |
| 120 | + cursor.getString(cursor.getColumnIndex(COL_TYPE))); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get all {@link AttachmentInfo} objects from the database for some message. |
| 125 | + * |
| 126 | + * @param email The email that the message linked. |
| 127 | + * @param label The folder label. |
| 128 | + * @param uid The message UID. |
| 129 | + * @return A list of {@link AttachmentInfo} objects. |
| 130 | + */ |
| 131 | + public List<AttachmentInfo> getAttachmentInfoList(Context context, String email, |
| 132 | + String label, long uid) { |
| 133 | + ContentResolver contentResolver = context.getContentResolver(); |
| 134 | + Cursor cursor = contentResolver.query(getBaseContentUri(), |
| 135 | + null, COL_EMAIL + " = ?" + " AND " + COL_FOLDER + " = ?" + " AND " + COL_UID + |
| 136 | + " = ?", new String[]{email, label, String.valueOf(uid)}, null); |
| 137 | + |
| 138 | + List<AttachmentInfo> attachmentInfoList = new ArrayList<>(); |
| 139 | + |
| 140 | + if (cursor != null) { |
| 141 | + while (cursor.moveToNext()) { |
| 142 | + attachmentInfoList.add(getAttachmentInfo(cursor)); |
| 143 | + } |
| 144 | + cursor.close(); |
| 145 | + } |
| 146 | + |
| 147 | + return attachmentInfoList; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Delete cached attachments info for some email and some folder. |
| 152 | + * |
| 153 | + * @param context Interface to global information about an application environment. |
| 154 | + * @param email The email that the message linked. |
| 155 | + * @param label The folder label. |
| 156 | + * @return The number of deleted rows. |
| 157 | + */ |
| 158 | + public int deleteCachedAttachmentInfoOfFolder(Context context, String email, String label) { |
| 159 | + ContentResolver contentResolver = context.getContentResolver(); |
| 160 | + if (email != null && label != null && contentResolver != null) { |
| 161 | + return contentResolver.delete(getBaseContentUri(), COL_EMAIL + " = ? AND " |
| 162 | + + COL_FOLDER + " = ?", new String[]{email, label}); |
| 163 | + } else return -1; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Prepare the content values for insert to the database. |
| 168 | + * |
| 169 | + * @param email The email that the message linked. |
| 170 | + * @param label The folder label. |
| 171 | + * @param uid The message UID. |
| 172 | + * @param attachmentInfo The attachment info which will be added to the database. |
| 173 | + * @return generated {@link ContentValues} |
| 174 | + */ |
| 175 | + @NonNull |
| 176 | + private ContentValues prepareContentValues(String email, String label, long uid, |
| 177 | + AttachmentInfo attachmentInfo) { |
| 178 | + ContentValues contentValues = new ContentValues(); |
| 179 | + contentValues.put(COL_EMAIL, email); |
| 180 | + contentValues.put(COL_FOLDER, label); |
| 181 | + contentValues.put(COL_UID, uid); |
| 182 | + contentValues.put(COL_NAME, attachmentInfo.getName()); |
| 183 | + contentValues.put(COL_ENCODED_SIZE_IN_BYTES, attachmentInfo.getEncodedSize()); |
| 184 | + contentValues.put(COL_TYPE, attachmentInfo.getType()); |
| 185 | + return contentValues; |
| 186 | + } |
| 187 | +} |
0 commit comments