866 lines
32 KiB
Java
866 lines
32 KiB
Java
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.graphics.Bitmap;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Color;
|
|
import android.graphics.Paint;
|
|
import android.graphics.PorterDuff;
|
|
import android.graphics.PorterDuffXfermode;
|
|
import android.graphics.Rect;
|
|
import android.graphics.RectF;
|
|
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 = 12;
|
|
|
|
//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";
|
|
private static final String TABLE_IMAGE2PERSON = "Image2Person";
|
|
|
|
//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_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_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
|
|
};
|
|
|
|
|
|
// Image2Person-Tabelle
|
|
private static final String KEY_ID_IMAGE2PERSON = "id";
|
|
private static final String KEY_IMAGE_IMAGE2PERSON = "image";
|
|
private static final String KEY_PERSONOID_IMAGE2PERSON = "personoid";
|
|
private static final String KEY_VERSION_IMAGE2PERSON = "version";
|
|
|
|
private static final String[] IMAGE2PERSON_COLUMNS = new String[] {
|
|
KEY_ID_IMAGE2PERSON,
|
|
KEY_IMAGE_IMAGE2PERSON,
|
|
KEY_PERSONOID_IMAGE2PERSON,
|
|
KEY_VERSION_IMAGE2PERSON
|
|
};
|
|
|
|
|
|
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);
|
|
|
|
deleteQuery = "DELETE FROM " + TABLE_IMAGE2PERSON;
|
|
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_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);
|
|
|
|
String createImage2PersonTable = "CREATE TABLE " + TABLE_IMAGE2PERSON + " (" +
|
|
KEY_ID_IMAGE2PERSON + " INTEGER PRIMARY KEY," +
|
|
KEY_IMAGE_IMAGE2PERSON + " BLOB, " +
|
|
KEY_PERSONOID_IMAGE2PERSON + " INTEGER, " +
|
|
KEY_VERSION_IMAGE2PERSON + " INTEGER)";
|
|
|
|
db.execSQL(createImage2PersonTable);
|
|
}
|
|
|
|
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<ChatMessage> 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();
|
|
|
|
Log.i("SAVING_MESSAGE", "MessageId: " + message.MessageId);
|
|
|
|
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<String> getExistingChatMessageUUIDs(int recipientPersonOid, int senderPersonOid) {
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
String selectQuery = "SELECT "+ KEY_MESSAGE_ID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE (" + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + " = " + recipientPersonOid + " AND " + KEY_SENDER_PERSON_OID_CHATMESSAGE + " = " + senderPersonOid + " OR " + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + " = " + senderPersonOid + " AND " + KEY_SENDER_PERSON_OID_CHATMESSAGE + " = " + recipientPersonOid + ") AND " + KEY_MESSAGE_ID_CHATMESSAGE + " IS NOT NULL AND " + KEY_IS_TEAM_CHATMESSAGE + " = 0 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<String> getExistingTeamChatMessageUUIDs(int recipientPersonOid, int senderPersonOid) {
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
String selectQuery = "SELECT " + KEY_MESSAGE_ID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + " = " + recipientPersonOid + " AND " + KEY_MESSAGE_ID_CHATMESSAGE + " IS NOT NULL AND " + KEY_IS_TEAM_CHATMESSAGE + " = 1 ORDER BY " + KEY_INSTS_CHATMESSAGE + " DESC";
|
|
|
|
Log.i("SELECT", selectQuery);
|
|
|
|
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);
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_IMAGE2PERSON);
|
|
|
|
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_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<ChatMessage> getMessagesForRecipient(int recipientOid) {
|
|
ArrayList<ChatMessage> 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<ChatMessage> getMessagesForTeam(int teamOid) {
|
|
ArrayList<ChatMessage> 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);
|
|
boolean contactIsTeam = cursor.getInt(3) == 1;
|
|
String teamMemberOids = cursor.getString(4);
|
|
String teamMemberNames = cursor.getString(5);
|
|
int contactVersion = cursor.getInt(6);
|
|
boolean isEmployee = cursor.getInt(7) == 1;
|
|
|
|
ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, contactIsTeam, teamMemberOids, teamMemberNames, contactVersion, isEmployee);
|
|
|
|
cursor.close();
|
|
|
|
return contact;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public List<ContactListItem> getAllContacts(){
|
|
List<ContactListItem> 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.isTeam = cursor.getInt(3) == 1;
|
|
contact.teamMemberNames = cursor.getString(4);
|
|
contact.teamMemberOids = cursor.getString(5);
|
|
contact.setVersion(cursor.getInt(6));
|
|
contact.setIsEmployee(cursor.getInt(7) == 1);
|
|
|
|
contactList.add(contact);
|
|
} while (cursor.moveToNext());
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
return contactList;
|
|
}
|
|
|
|
public ArrayList<ChatMessage> getAllChatMessages() {
|
|
ArrayList<ChatMessage> 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_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 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);
|
|
boolean contactIsTeam = cursor.getInt(3) == 1;
|
|
String teamMemberOids = cursor.getString(4);
|
|
String teamMemberNames = cursor.getString(5);
|
|
int contactVersion = cursor.getInt(6);
|
|
boolean isEmployee = cursor.getInt(7) == 1;
|
|
|
|
ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, 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<ChatMessage> getUnsentChatMessages() {
|
|
ArrayList<ChatMessage> 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<ChatMessage> getChatMessagesByMessageIds(ArrayList<String> messageIds) {
|
|
ArrayList<ChatMessage> 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;
|
|
}
|
|
|
|
public boolean isImageExistingForPerson(int personOid) {
|
|
SQLiteDatabase readableDatabase = this.getReadableDatabase();
|
|
|
|
Cursor cursor = readableDatabase.query(TABLE_IMAGE2PERSON,
|
|
IMAGE2PERSON_COLUMNS,
|
|
KEY_PERSONOID_IMAGE2PERSON + " = ?",
|
|
new String[] {String.valueOf(personOid)},
|
|
null, null, null, null);
|
|
|
|
boolean result = cursor != null && cursor.moveToFirst();
|
|
|
|
if(cursor != null) {
|
|
cursor.close();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void deleteUserImage(int personOid) {
|
|
SQLiteDatabase writableDatabase = this.getWritableDatabase();
|
|
writableDatabase.delete(TABLE_IMAGE2PERSON, KEY_PERSONOID_IMAGE2PERSON + " = ?", new String[] {String.valueOf(personOid)});
|
|
}
|
|
|
|
public void insertUserImage(int personOid, byte[] image) {
|
|
SQLiteDatabase writableDatabase = this.getWritableDatabase();
|
|
|
|
ContentValues values = new ContentValues();
|
|
values.put(KEY_PERSONOID_IMAGE2PERSON, personOid);
|
|
values.put(KEY_IMAGE_IMAGE2PERSON, image);
|
|
|
|
// wenn personoid vorhanden, dann update, ansonsten insert
|
|
if(isImageExistingForPerson(personOid)) {
|
|
writableDatabase.update(TABLE_IMAGE2PERSON, values, KEY_PERSONOID_IMAGE2PERSON + " = ?", new String[] {String.valueOf(personOid)});
|
|
} else {
|
|
writableDatabase.insertOrThrow(TABLE_IMAGE2PERSON, null, values);
|
|
}
|
|
}
|
|
|
|
public void updateUserImage(int personOid, byte[] image) {
|
|
SQLiteDatabase writableDatabase = this.getWritableDatabase();
|
|
|
|
ContentValues values = new ContentValues();
|
|
values.put(KEY_PERSONOID_IMAGE2PERSON, personOid);
|
|
values.put(KEY_IMAGE_IMAGE2PERSON, image);
|
|
|
|
writableDatabase.update(TABLE_IMAGE2PERSON, values, KEY_PERSONOID_IMAGE2PERSON + " = ?", new String[] {String.valueOf(personOid)});
|
|
}
|
|
|
|
public byte[] getUserImage(Integer personOid) {
|
|
SQLiteDatabase readableDatabase = this.getReadableDatabase();
|
|
|
|
Cursor cursor = readableDatabase.query(TABLE_IMAGE2PERSON, new String[] {KEY_IMAGE_IMAGE2PERSON}, KEY_PERSONOID_IMAGE2PERSON + " = " + personOid, null, null, null, null);
|
|
|
|
byte[] picture = null;
|
|
|
|
if(cursor != null && cursor.moveToFirst()) {
|
|
picture = cursor.getBlob(0);
|
|
|
|
cursor.close();
|
|
}
|
|
|
|
return picture;
|
|
}
|
|
|
|
private class CircleBitmapCreator implements Runnable{
|
|
Bitmap bitmap;
|
|
CircleBitmapCreator(Bitmap _bitmap){
|
|
this.bitmap = _bitmap;
|
|
}
|
|
|
|
public void run(){
|
|
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
final Canvas canvas = new Canvas(output);
|
|
|
|
final int color = Color.RED;
|
|
final Paint paint = new Paint();
|
|
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
|
|
final RectF rectF = new RectF(rect);
|
|
|
|
paint.setAntiAlias(true);
|
|
canvas.drawARGB(0, 0, 0, 0);
|
|
paint.setColor(color);
|
|
canvas.drawOval(rectF, paint);
|
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
canvas.drawBitmap(bitmap, rect, rect, paint);
|
|
|
|
bitmap.recycle();
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
|
byte[] result = stream.toByteArray();
|
|
|
|
// TODO: In Datenbank speichern
|
|
}
|
|
}
|
|
}
|