package Database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Base64; import android.util.Log; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import javax.crypto.NoSuchPaddingException; import beyondsoft.bewomitarbeiterapp.ContactListItem; import entities.ChatMessage; import soapConnection.SoapConnectionManager; import util.BeWoLog; import util.SecurityUtils; /** * Created by bib on 07.06.2016. */ public class DatabaseHandler extends SQLiteOpenHelper { private static final String LOGTAG = "DATABASE_HANDLER"; //DataBase version private static final int DATABASE_VERSION = 10; //Database name private static final String DATABASE_NAME = "Contacts_Manager"; //Table Name private static final String TABLE_CONTACTS = "Contactlist"; private static final String TABLE_CHATMESSAGE = "ChatMessage"; private static final String TABLE_DATABASEOWNER = "DatabaseOwner"; //Table Columns name private static final String KEY_ID_OWNER = "id"; private static final String KEY_OWNER_PERSON_OID = "ownerpersonoid"; // Contacts-Tabelle private static final String KEY_ID ="id"; private static final String KEY_KONTAKT_NAME = "kontaktname"; private static final String KEY_PERSONOID = "personoid"; private static final String KEY_PICTURE_BLOB = "bild"; private static final String KEY_IS_TEAM = "isteam"; private static final String KEY_TEAM_MEMBER_OIDS = "teammemberoids"; private static final String KEY_TEAM_MEMBER_NAMES = "teammembernames"; private static final String KEY_VERSION = "version"; private static final String KEY_IS_EMPLOYEE = "isemployee"; private static final String[] CONTACTS_COLUMNS = new String[] { KEY_ID, KEY_KONTAKT_NAME, KEY_PERSONOID, KEY_PICTURE_BLOB, KEY_IS_TEAM, KEY_TEAM_MEMBER_OIDS, KEY_TEAM_MEMBER_NAMES, KEY_VERSION, KEY_IS_EMPLOYEE }; // ChatMessage-Tabelle private static final String KEY_ID_CHATMESSAGE = "id"; private static final String KEY_SENDER_PERSON_OID_CHATMESSAGE = "senderpersonoid"; private static final String KEY_RECIPIENT_PERSON_OID_CHATMESSAGE = "recipientpersonoid"; private static final String KEY_INSTS_CHATMESSAGE = "insts"; private static final String KEY_CHAT_TEXT_CHATMESSAGE = "chattext"; private static final String KEY_IS_DELIVERED_CHATMESSAGE = "isdelivered"; private static final String KEY_SERVERSEITIGE_OID_CHATMESSAGE = "serverseitigeoid"; private static final String KEY_IS_TEAM_CHATMESSAGE = "isteam"; private static final String KEY_MESSAGE_ID_CHATMESSAGE = "messageid"; private static final String KEY_SENDDATE_CHATMESSAGE = "senddate"; private static final String[] CHAT_MESSAGE_COLUMNS = new String[] { KEY_ID_CHATMESSAGE, KEY_SENDER_PERSON_OID_CHATMESSAGE, KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, KEY_INSTS_CHATMESSAGE, KEY_CHAT_TEXT_CHATMESSAGE, KEY_IS_DELIVERED_CHATMESSAGE, KEY_SERVERSEITIGE_OID_CHATMESSAGE, KEY_IS_TEAM_CHATMESSAGE, KEY_MESSAGE_ID_CHATMESSAGE, KEY_SENDDATE_CHATMESSAGE }; private static DatabaseHandler mInstance = null; public static DatabaseHandler getInstance(Context context) { if(mInstance == null) { mInstance = new DatabaseHandler(context.getApplicationContext()); } return mInstance; } private DatabaseHandler(Context context){ super(context,DATABASE_NAME, null, DATABASE_VERSION); } public void clearDatabase() { SQLiteDatabase db = this.getWritableDatabase(); String deleteQuery = "DELETE FROM " + TABLE_DATABASEOWNER; db.execSQL(deleteQuery); deleteQuery = "DELETE FROM " + TABLE_CHATMESSAGE; db.execSQL(deleteQuery); deleteQuery = "DELETE FROM " + TABLE_CONTACTS; db.execSQL(deleteQuery); } public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_KONTAKT_NAME + " TEXT,"+ KEY_PERSONOID + " INTEGER," + KEY_PICTURE_BLOB + " BLOB, " + KEY_IS_TEAM + " INTEGER, " + KEY_TEAM_MEMBER_NAMES + " TEXT, " + KEY_TEAM_MEMBER_OIDS + " TEXT, " + KEY_VERSION + " INTEGER, " + KEY_IS_EMPLOYEE + " INTEGER)"; db.execSQL(CREATE_CONTACTS_TABLE); String createChatMessageTable = "CREATE TABLE " + TABLE_CHATMESSAGE + "(" + KEY_ID_CHATMESSAGE + " INTEGER PRIMARY KEY," + KEY_SENDER_PERSON_OID_CHATMESSAGE + " INTEGER," + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + " INTEGER," + KEY_INSTS_CHATMESSAGE + " DATETIME DEFAULT CURRENT_TIMESTAMP," + KEY_CHAT_TEXT_CHATMESSAGE + " TEXT," + KEY_IS_DELIVERED_CHATMESSAGE + " INTEGER," + KEY_SERVERSEITIGE_OID_CHATMESSAGE + " INTEGER DEFAULT NULL," + KEY_IS_TEAM_CHATMESSAGE + " INTEGER DEFAULT 0," + KEY_MESSAGE_ID_CHATMESSAGE + " TEXT," + KEY_SENDDATE_CHATMESSAGE + " DATETIME DEFAULT NULL)"; db.execSQL(createChatMessageTable); String createOwnerTable = "CREATE TABLE " + TABLE_DATABASEOWNER + " (" + KEY_ID_OWNER + " INTEGER PRIMARY KEY," + KEY_OWNER_PERSON_OID + " INTEGER" + ")"; db.execSQL(createOwnerTable); } public void insertOwnerOid(int ownerOid) { SQLiteDatabase writableDatabase = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_ID_OWNER, ownerOid); writableDatabase.insertOrThrow(TABLE_DATABASEOWNER, null, values); } public int getOwnerOid() { SQLiteDatabase readableDatabase = this.getReadableDatabase(); String selectQuery = "SELECT * FROM " + TABLE_DATABASEOWNER; Cursor cursor = readableDatabase.rawQuery(selectQuery, null); int ownerOid = 0; if(cursor.moveToFirst()) { ownerOid = cursor.getInt(0); } cursor.close(); return ownerOid; } public void addChatMessages(ArrayList messages) { SQLiteDatabase db = this.getWritableDatabase(); try { db.beginTransaction(); for(ChatMessage message : messages) { db.insertOrThrow(TABLE_CHATMESSAGE, null, fillValuesForChatMessage(message)); } db.setTransactionSuccessful(); } finally { if(db.inTransaction()) { db.endTransaction(); } } } public long addChatMessage(ChatMessage message) { SQLiteDatabase writableDatabase = this.getWritableDatabase(); return writableDatabase.insertOrThrow(TABLE_CHATMESSAGE, null, fillValuesForChatMessage(message)); } private ContentValues fillValuesForChatMessage(ChatMessage message) { ContentValues values = new ContentValues(); ByteArrayOutputStream chatMessageOutputStream = new ByteArrayOutputStream(); try { SecurityUtils.encrypt(message.ChatText, chatMessageOutputStream); values.put(KEY_SENDER_PERSON_OID_CHATMESSAGE, message.SenderPersonOid); values.put(KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, message.RecipientPersonOid); values.put(KEY_INSTS_CHATMESSAGE, getDateTimeString(message.InsTs)); values.put(KEY_CHAT_TEXT_CHATMESSAGE, Base64.encodeToString(chatMessageOutputStream.toByteArray(), Base64.DEFAULT)); values.put(KEY_IS_DELIVERED_CHATMESSAGE, message.IsDelivered ? 1 : 0); values.put(KEY_SERVERSEITIGE_OID_CHATMESSAGE, message.ServerseitigeOid); values.put(KEY_IS_TEAM_CHATMESSAGE, message.IsTeam ? 1 : 0); values.put(KEY_MESSAGE_ID_CHATMESSAGE, message.MessageId); values.put(KEY_SENDDATE_CHATMESSAGE, (message.SendDate == null ? null : getDateTimeString(message.SendDate))); } catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) { e.printStackTrace(); } return values; } public int updateChatMessage(ChatMessage message) { SQLiteDatabase writableDatabase = this.getWritableDatabase(); return writableDatabase.update(TABLE_CHATMESSAGE, fillValuesForChatMessage(message), KEY_ID_CHATMESSAGE + " = ?", new String[] { String.valueOf(message.Id)}); } public ArrayList getExistingChatMessageUUIDs() { ArrayList result = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT "+ KEY_MESSAGE_ID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_MESSAGE_ID_CHATMESSAGE + " IS NOT NULL ORDER BY " + KEY_INSTS_CHATMESSAGE + " DESC"; Cursor cursor = db.rawQuery(selectQuery, null); if(cursor.moveToFirst()) { do { result.add(cursor.getString(0)); } while(cursor.moveToNext()); } cursor.close(); return result; } public ArrayList getExistingTeamChatMessageUUIDs() { ArrayList result = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT " + KEY_MESSAGE_ID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_MESSAGE_ID_CHATMESSAGE + " IS NOT NULL AND " + KEY_IS_TEAM_CHATMESSAGE + " = 1 ORDER BY " + KEY_INSTS_CHATMESSAGE + " DESC"; Cursor cursor = db.rawQuery(selectQuery, null); if(cursor.moveToFirst()) { do { result.add(cursor.getString(0)); } while(cursor.moveToNext()); } cursor.close(); return result; } private String getDateTimeString(Date date) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); return dateFormat.format(date); } private Date getDateFromString(String dateString) { Date result = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); try { result = df.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return result; } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHATMESSAGE); db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATABASEOWNER); onCreate(db); } public void addContact(ContactListItem contact) { SQLiteDatabase writableDatabase = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_KONTAKT_NAME, contact.getChatName()); values.put(KEY_PERSONOID, contact.getPersonOid()); values.put(KEY_PICTURE_BLOB, contact.getPicture()); values.put(KEY_IS_TEAM, contact.isTeam); values.put(KEY_TEAM_MEMBER_NAMES, contact.teamMemberNames); values.put(KEY_TEAM_MEMBER_OIDS, contact.teamMemberOids); values.put(KEY_VERSION, contact.getVersion()); values.put(KEY_IS_EMPLOYEE, contact.getIsEmployee()); writableDatabase.insertOrThrow(TABLE_CONTACTS, null, values); } public ChatMessage getChatMessageById(int id) { SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query( TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, KEY_ID_CHATMESSAGE + " =? ", new String[]{String.valueOf(id)}, null, null, null, null); if(cursor != null && cursor.moveToFirst()){ int messageId = cursor.getInt(0); int senderPersonOid = cursor.getInt(1); int recipientPersonOid = cursor.getInt(2); Date insTs = getDateFromString(cursor.getString(3)); String chatText = decryptChatMessage(cursor.getString(4)); boolean isDelivered = cursor.getInt(5) == 1; int serverseitigeOid = cursor.getInt(6); boolean isTeam = cursor.getInt(7) == 1; String messageUUID = cursor.getString(8); Date sendDate = null; if(cursor.getString(9) != null) { sendDate = getDateFromString(cursor.getString(9)); } cursor.close(); ChatMessage result = new ChatMessage(messageId, senderPersonOid, recipientPersonOid, insTs, chatText, isDelivered, serverseitigeOid, isTeam, messageUUID); result.SendDate = sendDate; return result; } return null; } public ArrayList getMessagesForRecipient(int recipientOid) { ArrayList messages = new ArrayList<>(); SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query( TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, KEY_IS_TEAM_CHATMESSAGE + " = 0 AND ((" + KEY_SENDER_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=?) OR (" + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_SENDER_PERSON_OID_CHATMESSAGE + "=?))" , new String[] {String.valueOf(SoapConnectionManager.getUser().getPersonOid()), String.valueOf(recipientOid), String.valueOf(SoapConnectionManager.getUser().getPersonOid()), String.valueOf(recipientOid)}, null, null, null, null); if(cursor.moveToFirst()) { do{ ChatMessage message = new ChatMessage( cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), getDateFromString(cursor.getString(3)), decryptChatMessage(cursor.getString(4)), cursor.getInt(5) == 1, cursor.getInt(6), cursor.getInt(7) == 1, cursor.getString(8) ); if(cursor.getString(9) != null) { message.SendDate = getDateFromString(cursor.getString(9)); } messages.add(message); } while(cursor.moveToNext()); } cursor.close(); return messages; } public ArrayList getMessagesForTeam(int teamOid) { ArrayList messages = new ArrayList<>(); SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query( TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_IS_TEAM_CHATMESSAGE + "= 1", new String[] {String.valueOf(teamOid)}, null, null, null, null); if(cursor.moveToFirst()) { do{ ChatMessage message = new ChatMessage( cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), getDateFromString(cursor.getString(3)), decryptChatMessage(cursor.getString(4)), cursor.getInt(5) == 1, cursor.getInt(6), cursor.getInt(7) == 1, cursor.getString(8) ); if(cursor.getString(9) != null) { message.SendDate = getDateFromString(cursor.getString(9)); } messages.add(message); } while(cursor.moveToNext()); } cursor.close(); return messages; } public ContactListItem getContact(int id) { SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query(TABLE_CONTACTS, CONTACTS_COLUMNS, KEY_ID + "=?", new String[]{String.valueOf(id)}, null,null,null,null); if(cursor != null) { cursor.moveToFirst(); int contactId = cursor.getInt(0); String contactName = cursor.getString(1); int contactPersonOid = cursor.getInt(2); byte[] contactPicture = cursor.getBlob(3); boolean contactIsTeam = cursor.getInt(4) == 1; String teamMemberOids = cursor.getString(5); String teamMemberNames = cursor.getString(6); int contactVersion = cursor.getInt(7); boolean isEmployee = cursor.getInt(8) == 1; ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, contactPicture, contactIsTeam, teamMemberOids, teamMemberNames, contactVersion, isEmployee); cursor.close(); return contact; } return null; } public List getAllContacts(){ List contactList = new ArrayList<>(); String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase writableDatabase = this.getWritableDatabase(); Cursor cursor = writableDatabase.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { ContactListItem contact = new ContactListItem(); contact.setId(cursor.getInt(0)); contact.setChatName(cursor.getString(1)); contact.setPersonOid(cursor.getInt(2)); contact.setPicture(cursor.getBlob(3)); contact.isTeam = cursor.getInt(4) == 1; contact.teamMemberNames = cursor.getString(5); contact.teamMemberOids = cursor.getString(6); contact.setVersion(cursor.getInt(7)); contact.setIsEmployee(cursor.getInt(8) == 1); contactList.add(contact); } while (cursor.moveToNext()); } cursor.close(); return contactList; } public ArrayList getAllChatMessages() { ArrayList result = new ArrayList<>(); String selectQuery = "SELECT * FROM " + TABLE_CHATMESSAGE; SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.rawQuery(selectQuery, null); if(cursor.moveToFirst()) { do { ChatMessage message = new ChatMessage( cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), getDateFromString(cursor.getString(3)), decryptChatMessage(cursor.getString(4)), cursor.getInt(5) == 1, cursor.getInt(6), cursor.getInt(7) == 1, cursor.getString(8) ); if(cursor.getString(9) != null) { message.SendDate = getDateFromString(cursor.getString(9)); } result.add(message); } while(cursor.moveToNext()); cursor.close(); } return result; } public int updateContact(ContactListItem contact) { SQLiteDatabase writableDatabase = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_KONTAKT_NAME, contact.getChatName()); values.put(KEY_PERSONOID, contact.getPersonOid()); values.put(KEY_PICTURE_BLOB, contact.getPicture()); values.put(KEY_IS_TEAM, (contact.isTeam ? 1 : 0)); values.put(KEY_TEAM_MEMBER_NAMES, contact.teamMemberNames); values.put(KEY_TEAM_MEMBER_OIDS, contact.teamMemberOids); values.put(KEY_VERSION, contact.getVersion()); values.put(KEY_IS_EMPLOYEE, contact.getIsEmployee()); return writableDatabase.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(contact.getId()) }); } public void deleteContacts(ContactListItem contact){ SQLiteDatabase writableDatabase = this.getWritableDatabase(); writableDatabase.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getId()) }); } public int getContactsCount(){ String countQuery = "SELECT COUNT(*) FROM " + TABLE_CONTACTS; SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.rawQuery(countQuery, null); int result = 0; if(cursor.moveToFirst()) { result = cursor.getInt(0); } cursor.close(); return result; } public byte[] getUserImage(Integer personOid) { SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query(TABLE_CONTACTS, new String[] {KEY_PICTURE_BLOB}, KEY_PERSONOID + " = " + personOid, null, null, null, null); byte[] picture = null; if(cursor != null && cursor.moveToFirst()) { picture = cursor.getBlob(0); cursor.close(); } return picture; } public ContactListItem getContactByPersonOid(int personOid, boolean isTeam) { SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query(TABLE_CONTACTS, CONTACTS_COLUMNS, KEY_PERSONOID + "=? AND " + KEY_IS_TEAM + "=?", new String[]{String.valueOf(personOid), String.valueOf((isTeam ? 1 : 0))}, null,null,null,null); if(cursor != null && cursor.moveToFirst()) { int contactId = cursor.getInt(0); String contactName = cursor.getString(1); int contactPersonOid = cursor.getInt(2); byte[] contactPicture = cursor.getBlob(3); boolean contactIsTeam = cursor.getInt(4) == 1; String teamMemberOids = cursor.getString(5); String teamMemberNames = cursor.getString(6); int contactVersion = cursor.getInt(7); boolean isEmployee = cursor.getInt(8) == 1; ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, contactPicture, contactIsTeam, teamMemberOids, teamMemberNames, contactVersion, isEmployee); cursor.close(); return contact; } else { Log.i(LOGTAG, "Keinen Kontakt mit PersonOid " + personOid + " gefunden."); } return null; } public boolean GetIsMessageInDatabase(String pMessageId) { SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query(TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, KEY_MESSAGE_ID_CHATMESSAGE + " =? ", new String[] {pMessageId}, null, null, null, null); boolean result = cursor != null && cursor.moveToFirst(); if (cursor != null) { cursor.close(); } return result; } public ArrayList getUnsentChatMessages() { ArrayList messages = new ArrayList<>(); SQLiteDatabase readableDatabase = this.getReadableDatabase(); Cursor cursor = readableDatabase.query( TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, KEY_IS_DELIVERED_CHATMESSAGE + " =? AND " + KEY_SENDER_PERSON_OID_CHATMESSAGE + " =? ", new String[] {String.valueOf(0), String.valueOf(SoapConnectionManager.getUser().getPersonOid())}, null, null, null, null); if(cursor.moveToFirst()) { do{ ChatMessage message = new ChatMessage( cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), getDateFromString(cursor.getString(3)), decryptChatMessage(cursor.getString(4)), cursor.getInt(5) == 1, cursor.getInt(6), cursor.getInt(7) == 1, cursor.getString(8) ); if(cursor.getString(9) != null) { message.SendDate = getDateFromString(cursor.getString(9)); } messages.add(message); } while(cursor.moveToNext()); } cursor.close(); return messages; } public ArrayList getChatMessagesByMessageIds(ArrayList messageIds) { ArrayList result = new ArrayList<>(); SQLiteDatabase readableDatabase = this.getReadableDatabase(); String whereClause = ""; for(int i = 0; i < messageIds.size(); i++) { whereClause = whereClause + KEY_MESSAGE_ID_CHATMESSAGE + " = '" + messageIds.get(i) + "' "; if(i != (messageIds.size() - 1)) { whereClause += " OR "; } } Cursor cursor = readableDatabase.query(TABLE_CHATMESSAGE, CHAT_MESSAGE_COLUMNS, whereClause, null, null, null, null); if(cursor.moveToFirst()) { do{ ChatMessage message = new ChatMessage( cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), getDateFromString(cursor.getString(3)), decryptChatMessage(cursor.getString(4)), cursor.getInt(5) == 1, cursor.getInt(6), cursor.getInt(7) == 1, cursor.getString(8) ); if(cursor.getString(9) != null) { message.SendDate = getDateFromString(cursor.getString(9)); } result.add(message); } while(cursor.moveToNext()); } cursor.close(); return result; } private String decryptChatMessage(String encryptedChatMessage) { String result = ""; byte[] chatMessageByteArray = Base64.decode(encryptedChatMessage, Base64.DEFAULT); ByteArrayInputStream chatMessageStream = new ByteArrayInputStream(chatMessageByteArray); try { result = SecurityUtils.decrypt(chatMessageStream).toString(); } catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) { BeWoLog.writeExceptionToLog(e); e.printStackTrace(); } return result; } }