Änderungen an der App

Chunkweises Herunterladen von Chatnachrichten
This commit is contained in:
staccatomamba
2016-11-09 11:48:46 +01:00
parent de51aef179
commit 1bd2c4e647
17 changed files with 530 additions and 314 deletions

View File

@@ -5,8 +5,15 @@ 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;
@@ -15,9 +22,13 @@ 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.
@@ -27,7 +38,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
private static final String LOGTAG = "DATABASE_HANDLER";
//DataBase version
private static final int DATABASE_VERSION = 8;
private static final int DATABASE_VERSION = 9;
//Database name
private static final String DATABASE_NAME = "Contacts_Manager";
@@ -209,15 +220,23 @@ public class DatabaseHandler extends SQLiteOpenHelper {
private ContentValues fillValuesForChatMessage(ChatMessage message) {
ContentValues values = new ContentValues();
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, message.ChatText);
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)));
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;
}
@@ -233,7 +252,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT "+ KEY_MESSAGE_ID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_MESSAGE_ID_CHATMESSAGE + " IS NOT NULL";
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);
@@ -249,11 +268,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
}
public ArrayList<String> getExistingTeamChatMessageUUIDs() {
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> 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";
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);
@@ -321,12 +340,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
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 = cursor.getString(4);
String chatText = decryptChatMessage(cursor.getString(4));
boolean isDelivered = cursor.getInt(5) == 1;
int serverseitigeOid = cursor.getInt(6);
boolean isTeam = cursor.getInt(7) == 1;
@@ -373,7 +391,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
cursor.getInt(1),
cursor.getInt(2),
getDateFromString(cursor.getString(3)),
cursor.getString(4),
decryptChatMessage(cursor.getString(4)),
cursor.getInt(5) == 1,
cursor.getInt(6),
cursor.getInt(7) == 1,
@@ -412,7 +430,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
cursor.getInt(1),
cursor.getInt(2),
getDateFromString(cursor.getString(3)),
cursor.getString(4),
decryptChatMessage(cursor.getString(4)),
cursor.getInt(5) == 1,
cursor.getInt(6),
cursor.getInt(7) == 1,
@@ -465,7 +483,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
}
public List<ContactListItem> getAllContacts(){
List<ContactListItem> contactList = new ArrayList<ContactListItem>();
List<ContactListItem> contactList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
@@ -510,7 +528,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
cursor.getInt(1),
cursor.getInt(2),
getDateFromString(cursor.getString(3)),
cursor.getString(4),
decryptChatMessage(cursor.getString(4)),
cursor.getInt(5) == 1,
cursor.getInt(6),
cursor.getInt(7) == 1,
@@ -626,7 +644,13 @@ public class DatabaseHandler extends SQLiteOpenHelper {
new String[] {pMessageId},
null, null, null, null);
return cursor != null && cursor.moveToFirst();
boolean result = cursor != null && cursor.moveToFirst();
if (cursor != null) {
cursor.close();
}
return result;
}
public ArrayList<ChatMessage> getUnsentChatMessages() {
@@ -647,7 +671,7 @@ public class DatabaseHandler extends SQLiteOpenHelper {
cursor.getInt(1),
cursor.getInt(2),
getDateFromString(cursor.getString(3)),
cursor.getString(4),
decryptChatMessage(cursor.getString(4)),
cursor.getInt(5) == 1,
cursor.getInt(6),
cursor.getInt(7) == 1,
@@ -691,18 +715,13 @@ public class DatabaseHandler extends SQLiteOpenHelper {
cursor.getInt(1),
cursor.getInt(2),
getDateFromString(cursor.getString(3)),
cursor.getString(4),
decryptChatMessage(cursor.getString(4)),
cursor.getInt(5) == 1,
cursor.getInt(6),
cursor.getInt(7) == 1,
cursor.getString(8)
);
String blubb = "";
for(int i = 0; i < cursor.getColumnCount(); i++) {
blubb += "" + i + ". " + cursor.getColumnName(i) + "; ";
}
if(cursor.getString(9) != null) {
message.SendDate = getDateFromString(cursor.getString(9));
}
@@ -711,6 +730,24 @@ public class DatabaseHandler extends SQLiteOpenHelper {
} 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;
}
}