diff --git a/Android/BewoMitarbeiterApp/app/app.iml b/Android/BewoMitarbeiterApp/app/app.iml index a8696e47b..c0810e81b 100644 --- a/Android/BewoMitarbeiterApp/app/app.iml +++ b/Android/BewoMitarbeiterApp/app/app.iml @@ -100,8 +100,6 @@ - - diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/Database/DatabaseHandler.java b/Android/BewoMitarbeiterApp/app/src/main/java/Database/DatabaseHandler.java index 6f447fe09..350f83797 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/Database/DatabaseHandler.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/Database/DatabaseHandler.java @@ -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 getExistingTeamChatMessageUUIDs() { - ArrayList result = new ArrayList(); + 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"; + 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 getAllContacts(){ - List contactList = new ArrayList(); + List 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 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; } } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/BeWoChatApplication.java b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/BeWoChatApplication.java index dc279b5c5..58ce13fa8 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/BeWoChatApplication.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/BeWoChatApplication.java @@ -164,16 +164,6 @@ public class BeWoChatApplication extends Application { ConnectivityReceiver.connectivityReceiverListener = listener; } - public static void writeExceptionToLog(Throwable ex) { - String logText = "Ausnahme: " + ex.toString() + "; Message: " + ex.getMessage() + "; Cause: " + ex.getCause(); - for(StackTraceElement ste : ex.getStackTrace()) { - String steAsString = ste.toString(); - logText += "\t" + steAsString; - } - - BeWoLog.writeToLogFile(logText); - } - public BeWoChatApplication() { defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); @@ -183,7 +173,7 @@ public class BeWoChatApplication extends Application { Log.e("UNCAUGHT_EXCEPTION", "Eine unbehandelte Ausnahme ist aufgetreten!\n" + ex.getMessage()); ex.printStackTrace(); - writeExceptionToLog(ex); + BeWoLog.writeExceptionToLog(ex); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -323,7 +313,7 @@ public class BeWoChatApplication extends Application { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); - writeExceptionToLog(e); + BeWoLog.writeExceptionToLog(e); } } } @@ -368,7 +358,7 @@ public class BeWoChatApplication extends Application { } } - public static void sendUnsentMessages(final Context context) { + public static synchronized void sendUnsentMessages(final Context context) { Thread thread = new Thread(new Runnable() { @Override public void run() { diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/ChatActivity.java b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/ChatActivity.java index da41ae1fd..d30b8feda 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/ChatActivity.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/ChatActivity.java @@ -22,6 +22,7 @@ import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; @@ -99,6 +100,7 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei private final static String LOGTAG = "CHAT_ACTIVITY"; + private int mLastFirstVisibleItem = 0; @Override public void onNetworkConnectionChanged(boolean isConnected) { @@ -132,20 +134,47 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei listView = (ListView)findViewById(R.id.list_msg); -// listView.setOnScrollListener(new AbsListView.OnScrollListener() { -// -// @Override -// public void onScrollStateChanged(AbsListView view, int scrollState) { -// if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { -// Log.i("ON_SCROLL_STATE_CHANGED", "Berühre das Ende"); -// } -// } -// -// @Override -// public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { + listView.setOnScrollListener(new AbsListView.OnScrollListener() { + + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + int fvp = view.getFirstVisiblePosition(); + int cc = view.getChildCount(); + int lvp = view.getLastVisiblePosition(); + + ChatMessage oldest = chatMessages.get(view.getLastVisiblePosition()); + + String scrollStateName = ""; + switch (scrollState) { + case 0: + scrollStateName = "IDLE"; + break; + case 1: + scrollStateName = "TOUCH SCROLL"; + break; + case 2: + scrollStateName = "FLING"; + break; + } + + scrollStateName = String.format(Locale.GERMAN, "%1$12s", scrollStateName); + + String fvpStr = String.format(Locale.GERMAN, "%1$3d", fvp); + String lvpStr = String.format(Locale.GERMAN, "%1$3d", lvp); + String ccStr = String.format(Locale.GERMAN, "%1$3d", cc); + + Log.i("ON_SCROLL_STATE_CHANGED", "ScrollState: " + scrollStateName + "; FirstVisiblePosition: " + fvpStr + " LastVisiblePosition: " + lvpStr + "; ChildCount: " + ccStr + "; Id der ältesten Nachricht: " + oldest.MessageId); + + if(fvp == 0) { + //TODO: Nachichten nachladen + } + } + + @Override + public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // Log.i("ON_SCROLL", "Scrolle... firstVisibleItem: " + firstVisibleItem + "; visibleItemCount: " + visibleItemCount + "; totalItemCount" + totalItemCount); -// } -// }); + } + }); Bundle zielkorb = getIntent().getExtras(); recipientName = zielkorb.getString("Name"); @@ -161,7 +190,7 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei ReloadChatView(recipientName, recipientPersonOid, image, isTeam, teamMemberOids, teamMemberNames, isEmployee); } - private void ReloadChatView(final String contactName, final int recipientOid, final byte[] contactImage, boolean pIsTeam, String pTeamMemberOids, String pTeamMemberNames, boolean pIsEmployee) { + public void ReloadChatView(final String contactName, final int recipientOid, final byte[] contactImage, boolean pIsTeam, String pTeamMemberOids, String pTeamMemberNames, boolean pIsEmployee) { chatMessages = new ArrayList<>(); recipientPersonOid = recipientOid; @@ -354,13 +383,34 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei } } - ArrayList propertyInfos = new ArrayList<>(); - propertyInfos.add(SoapConnectionManager.BuildProperty("pSenderPersonOid", SoapConnectionManager.getUser().getPersonOid(), Long.class)); - propertyInfos.add(SoapConnectionManager.BuildProperty("pRecipientPersonOid", recipientPersonOid, Long.class)); - propertyInfos.add(SoapConnectionManager.BuildProperty("pExceptions", pExceptions, String.class)); - propertyInfos.add(SoapConnectionManager.BuildProperty("pIsForTeam", isTeam, Boolean.class)); + // TODO: Anfang Test ---------------------------------------------------------------------------------------------------------------------------------------------------- + String newestMessageId = ""; - final JsonSoapPrimitiveRequest request = new JsonSoapPrimitiveRequest(SoapCalls.GET_ALL_CHAT_MESSAGES, propertyInfos); + if(test.size() > 0) { + newestMessageId = test.get(test.size() - 1); + } + + Log.i("CHUNKS", "MessageId der Nachricht, die nicht geladen wird: " + newestMessageId); + + ArrayList propertyInfos2 = new ArrayList<>(); + propertyInfos2.add(SoapConnectionManager.BuildProperty("messageId", newestMessageId, String.class)); + propertyInfos2.add(SoapConnectionManager.BuildProperty("pRecipientPersonOid", recipientPersonOid, Long.class)); + propertyInfos2.add(SoapConnectionManager.BuildProperty("pSenderPersonOid", SoapConnectionManager.getUser().getPersonOid(), Long.class)); + propertyInfos2.add(SoapConnectionManager.BuildProperty("pIsForTeam", isTeam, Boolean.class)); + propertyInfos2.add(SoapConnectionManager.BuildProperty("pExceptions", pExceptions, String.class)); + + final JsonSoapPrimitiveRequest request = new JsonSoapPrimitiveRequest(SoapCalls.LOAD_CHATMESSAGES_CHUNKWISE, propertyInfos2); +// spiceManager.execute(request, new JsonSoapChunkRequestListener()); + + // TODO: Ende Test ------------------------------------------------------------------------------------------------------------------------------------------------------ + +// ArrayList propertyInfos = new ArrayList<>(); +// propertyInfos.add(SoapConnectionManager.BuildProperty("pSenderPersonOid", SoapConnectionManager.getUser().getPersonOid(), Long.class)); +// propertyInfos.add(SoapConnectionManager.BuildProperty("pRecipientPersonOid", recipientPersonOid, Long.class)); +// propertyInfos.add(SoapConnectionManager.BuildProperty("pExceptions", pExceptions, String.class)); +// propertyInfos.add(SoapConnectionManager.BuildProperty("pIsForTeam", isTeam, Boolean.class)); +// +// final JsonSoapPrimitiveRequest request = new JsonSoapPrimitiveRequest(SoapCalls.GET_ALL_CHAT_MESSAGES, propertyInfos); final Callback corruptedSpiceCallback = new Callback() { @Override @@ -484,27 +534,6 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei ChatActivity.this.startActivity(dokuActivity); return true; - -// case R.id.action_show_unsent_messages: -// -// String message = ""; -// ArrayList unsentMessages = DatabaseHandler.getInstance(ChatActivity.this).getUnsentChatMessages(); -// for(ChatMessage cm : unsentMessages) { -// String sendDateStr = ""; -// -// if(cm.SendDate != null) { -// SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.GERMAN); -// sendDateStr = dateFormat.format(cm.SendDate); -// } else { -// sendDateStr = "null"; -// } -// -// message += cm.MessageId + "; " + cm.IsDelivered + "; " + sendDateStr + "\n"; -// } -// -// Util.buildAlert(ChatActivity.this, message); -// -// return true; default: return super.onOptionsItemSelected(item); } @@ -681,20 +710,14 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei BeWoLog.writeToLogFile("Erneuter Login war erfolgreich. (ChatActivity->ReLoginRequestListener->onRequestSuccess)"); - Bundle zielkorb = new Bundle(); - zielkorb.putString("Name", recipientName); - zielkorb.putInt("RecipientOid", recipientPersonOid); - zielkorb.putByteArray("Bild", image); - zielkorb.putBoolean("IsTeam", isTeam); - zielkorb.putString("TeamMemberOids", teamMemberOids); - zielkorb.putString("TeamMemberNames", teamMemberNames); - zielkorb.putBoolean("IsEmployee", isEmployee); - if(abc != null) { if(abc.contains(";")) { - Intent chatActivity = new Intent(getApplicationContext(), ChatActivity.class); - chatActivity.putExtras(zielkorb); - startActivity(chatActivity); + runOnUiThread(new Runnable() { + @Override + public void run() { + ReloadChatView(recipientName, recipientPersonOid, image, isTeam, teamMemberOids, teamMemberNames, isEmployee); + } + }); } else { BeWoLog.writeToLogFile("Der erneute Login ist fehlgeschlagen. Die Credentials waren inkorrekt. (ChatActivity->ReLoginRequestListener->onRequestSuccess)"); @@ -710,6 +733,61 @@ public class ChatActivity extends ActionBarActivity implements ConnectivityRecei } } + private final class JsonSoapChunkRequestListener implements RequestListener { + + @Override + public void onRequestFailure(SpiceException spiceException) { + BeWoLog.writeToLogFile("Das Chunkweise Laden der Nachrichten ist fehlgeschlagen. (ChatActivity->JsonSoapChunkRequestListener->onRequestFailure)"); + + runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(ChatActivity.this, "Ein Fehler ist beim chunkweise Laden der Nachrichten auftegreten.", Toast.LENGTH_LONG).show(); + } + }); + + Log.e("GET_ALL_MESSAGES", "Das Chunkweise Laden der Nachrichten ist gescheitert: " + spiceException.getMessage()); + } + + @Override + public void onRequestSuccess(SoapPrimitive soapPrimitive) { + BeWoLog.writeToLogFile("Das chunkweise Laden der Nachrichten war erfolgreich. (ChatActivty, wo sonst?)"); + + BeWoChatApplication.lastOnlineTS = Calendar.getInstance().getTime(); + + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(ChatMessage.class, new ChatMessageDeserializer()); + + Gson gson = builder.create(); + + Type listType = new TypeToken>(){}.getType(); + + final ArrayList result = gson.fromJson(soapPrimitive.toString(), listType); + + String abc = "Chunkweise geladenen Nachrichten:\n"; + for(ChatMessage m : result) { + abc += m.MessageId + ": " + m.ChatText; + + if(result.indexOf(m) != (result.size() - 1)) { + abc += "\n"; + } + } + + Log.i("CHUNKS", abc); + + databaseHandler.addChatMessages(result); + + listView.post(new Runnable() { + @Override + public void run() { + chatMessages.addAll(result); + adapter.sort(DATE_COMPARATOR); + adapter.notifyDataSetChanged(); + } + }); + } + } + public static final Comparator DATE_COMPARATOR = new Comparator() { @Override public int compare(ChatMessage lhs, ChatMessage rhs) { diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/KontaktChatActivity.java b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/KontaktChatActivity.java index 3aaff2ccb..80780a5c8 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/KontaktChatActivity.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/KontaktChatActivity.java @@ -149,7 +149,7 @@ public class KontaktChatActivity extends ActionBarActivity implements Connectivi @Override public void onRefresh() { - Log.i(LOGTAG, "Führe onRefresh durch"); + Log.i(LOGTAG, "Föhre onRefresh durch"); updateContactList(); } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/LoginActivity.java b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/LoginActivity.java index a9672d017..5232b4845 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/LoginActivity.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/LoginActivity.java @@ -44,6 +44,7 @@ import java.util.Calendar; import javax.crypto.NoSuchPaddingException; import Database.DatabaseHandler; +import entities.ChatMessage; import soapConnection.ApplicationUser; import soapConnection.JsonSoapPrimitiveRequest; import soapConnection.SoapCalls; @@ -382,6 +383,13 @@ public class LoginActivity extends ActionBarActivity { } } + DatabaseHandler dh = DatabaseHandler.getInstance(LoginActivity.this); + ArrayList unsentMessages = dh.getUnsentChatMessages(); + Log.i("LOGIN_ACTIVITY", "Anzahl nicht verschickter Nachrichten: " + unsentMessages.size()); + for(ChatMessage cm : unsentMessages) { + Log.i("LOGIN_ACTIVITY", cm.MessageId + ": " + cm.ChatText); + } + SocketManager.tenant = mTenantView.getText().toString(); SocketManager.initialLoginCallback = new Callback() { diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/SplashScreenActivity.java b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/SplashScreenActivity.java index 6dfa69da4..5991eeba6 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/SplashScreenActivity.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/beyondsoft/bewomitarbeiterapp/SplashScreenActivity.java @@ -3,9 +3,12 @@ package beyondsoft.bewomitarbeiterapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.util.Log; import java.util.logging.Handler; +import util.BeWoLog; + /** * Created by bib on 02.05.2016. */ @@ -18,6 +21,12 @@ public class SplashScreenActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); + if(!isTaskRoot()) { + BeWoLog.writeToLogFile("Splash Screen gestartet, er ist aber nicht TaskRoot. Die Activity wird deshalb beendet."); + finish(); + return; + } + Thread logoTimer = new Thread() { public void run(){ try{ diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/ApplicationUser.java b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/ApplicationUser.java index 005329a56..0f4282626 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/ApplicationUser.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/ApplicationUser.java @@ -1,12 +1,14 @@ package soapConnection; +import util.BeWoLog; + /** * Created by JettenM on 05.07.2016. */ public class ApplicationUser { private String fullName; private String username; - private Long employeeOid; + private Long employeeOid; //TODO: redundant? private Long customerOid; private Long personOid; private String tenant; @@ -56,9 +58,23 @@ public class ApplicationUser { } public ApplicationUser(String fullName, String username, Long employeeOid, Long personOid) { + BeWoLog.writeToLogFile("Rufe den Konstruktor von ApplicationUser mit folgenden Werten auf: fullName: " + fullName + "; username: " + username + "; employeeOid: " + employeeOid + "; personOid: " + personOid); + this.fullName = fullName; this.username = username; this.employeeOid = employeeOid; this.personOid = personOid; } + + @Override + public String toString() { + String fn = (fullName == null ? "fullName ist NULL" : fullName); + String un = (username == null ? "username ist NULL" : username); + String eo = (employeeOid == null ? "employeeOid ist NULL" : employeeOid.toString()); + String co = (customerOid == null ? "customerOid ist NULL" : customerOid.toString()); + String po = (personOid == null ? "personOid ist NULL" : personOid.toString()); + String tn = (tenant == null ? "tenant ist NULL" : tenant); + + return "fullName: " + fn + "; username: " + un + "; employeeOid: " + eo + "; customerOid: " + co + "; personOid: " + po + "; tenant: " + tn + ""; + } } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/Packet.java b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/Packet.java index 6701c41d6..cc36bb1e6 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/Packet.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/Packet.java @@ -6,6 +6,7 @@ import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Locale; @@ -98,7 +99,7 @@ public class Packet { IsDelivered = Integer.parseInt(actualMessage.substring(chatMessageLength + chatNameLength + 19, chatMessageLength + chatNameLength + 20)) != 0; - writeToLog("Konstruktor"); +// writeToLog("Konstruktor"); } public byte[] getDataStream() { @@ -139,76 +140,81 @@ public class Packet { "\n\tMessageId: " + MessageId; } - public static int analyzingPackets(String packetString) { - int packetCount = 0; +// private static final String ANALYZERLOGTAG = "PACKET_ANALYZER"; + public static ArrayList analyzePackets(String packetString) { + int messagesCharCount = packetString.length(); + ArrayList packets = new ArrayList<>(); + while (messagesCharCount > 0) { +// Log.i(ANALYZERLOGTAG, "Packet Nr. " + (packetCount + 1)); + String[] gesplittet = packetString.split(";"); - String[] gesplittet = packetString.split(";"); +// Log.i(ANALYZERLOGTAG, "Zu analysierender String: " + packetString); - Log.i("PACKET_ANALYZER", "Anzahl Semikola: " + (gesplittet.length - 1)); - Log.i("PACKET_ANALYZER", "Zu analysierender String: " + packetString); + int senderPersonOidLength = gesplittet[0].length(); + int recipientPersonOidLength = gesplittet[1].length(); + int chatNameLength = gesplittet[2].length(); + int chatMessageLength = gesplittet[3].length(); + int chatIdentifierLength = gesplittet[4].length(); + int serverseitigeOidLength = gesplittet[5].length(); + int clientseitigeOidLength = gesplittet[6].length(); + int tenantLength = gesplittet[7].length(); + int isEmployeeLength = gesplittet[8].length(); + int messageIdLength = gesplittet[9].length(); - //z.B. Header: 11;3;15;17;0;2657;43;demoapp4;1;5a50002f-464b-4763-91e2-d261463477e0; +// Log.i(ANALYZERLOGTAG, "SenderPersonOidLength: " + senderPersonOidLength + "\n" + +// "recipientPersonOidLength: " + recipientPersonOidLength + "\n" + +// "chatNameLength: " + chatNameLength + "\n" + +// "chatMessageLength: " + chatMessageLength + "\n" + +// "chatIdentifierLength: " + chatIdentifierLength + "\n" + +// "serverseitigeOidLength: " + serverseitigeOidLength + "\n" + +// "clientseitigeOidLength: " + clientseitigeOidLength + "\n" + +// "tenantLength: " + tenantLength + "\n" + +// "isEmployeeLength: " + isEmployeeLength + "\n" + +// "messageIdLength: " + messageIdLength + "\n" + +// "actualChatNameLength: " + Integer.parseInt(gesplittet[2])); - // 11;3;15;17;0;2657;43;demoapp4;1;5a50002f-464b-4763-91e2-d261463477e0;Elliot Aldersonnoch eine antwort03.11.2016 14:14:150 - // 11;3;15;17;0;2657;43;demoapp4;1;5a50002f-464b-4763-91e2-d261463477e0;Elliot Alderson --> 84 Zeichen - // noch eine antwort --> 17 Zeichen - // 03.11.2016 14:14:150 --> 20 Zeichen --> 84 + 17 + 20 = 121 + int headerLength = + 10 + + senderPersonOidLength + + recipientPersonOidLength + + chatNameLength + + chatMessageLength + + chatIdentifierLength + + serverseitigeOidLength + + clientseitigeOidLength + + tenantLength + + isEmployeeLength + + messageIdLength + + Integer.parseInt(gesplittet[2]); - int senderPersonOidLength = gesplittet[0].length(); // 2 - int recipientPersonOidLength = gesplittet[1].length(); // 1 - int chatNameLength = gesplittet[2].length(); // 2 - int chatMessageLength = gesplittet[3].length(); // 2 - int chatIdentifierLength = gesplittet[4].length(); // 1 - int serverseitigeOidLength = gesplittet[5].length(); // 4 - int clientseitigeOidLength = gesplittet[6].length(); // 2 - int tenantLength = gesplittet[7].length(); // 8 - int isEmployeeLength = gesplittet[8].length(); // 1 - int messageIdLength = gesplittet[9].length(); // 36 +// Log.i(ANALYZERLOGTAG, "Testausgabe: " + packetString.substring(0, headerLength)); - int headerLength = gesplittet.length - 1 + - senderPersonOidLength + - recipientPersonOidLength + - chatNameLength + - chatMessageLength + - chatIdentifierLength + - serverseitigeOidLength + - clientseitigeOidLength + - tenantLength + - isEmployeeLength + - messageIdLength + - Integer.parseInt(gesplittet[2]); + int actualChatMessageLength = Integer.parseInt(gesplittet[3]); - Log.i("PACKET_ANALYZER", "Testausgabe: " + packetString.substring(0, headerLength)); + int firstMessageLength = headerLength + actualChatMessageLength + 20; - /* - SenderPersonOid = Integer.parseInt(senderPersonOidString); - RecipientPersonOid = Integer.parseInt(recipientPersonOidString); - ServerseitigeOid = Integer.parseInt(serverseitigeOidString); - ClientseitigeOid = Integer.parseInt(clientseitigeOidString); - IsEmployee = Integer.parseInt(isEmployeeString) == 1; +// Log.i(ANALYZERLOGTAG, "Header-Length: " + headerLength); +// Log.i(ANALYZERLOGTAG, "Länge der ersten Nachricht: " + firstMessageLength); +// Log.i(ANALYZERLOGTAG, "Länge des packetStrings: " + packetString.length()); + String firstMessage = packetString.substring(0, firstMessageLength); +// Log.i(ANALYZERLOGTAG, "Eigentliche Nachricht: " + (actualChatMessageLength > 0 ? firstMessage.substring(headerLength, headerLength + actualChatMessageLength) : "<>")); +// Log.i(ANALYZERLOGTAG, "Header: " + firstMessage.substring(0, headerLength)); +// +// Log.i(ANALYZERLOGTAG, "Erste Nachricht: " + firstMessage); +// +// Log.i(ANALYZERLOGTAG, "Länge erste Nachricht: " + firstMessage.length() + ". Länge gesamter packetString: " + packetString.length()); - Logout von Elliot Alderson: + packets.add(new Packet(firstMessage)); - SenderPersonOid RecipientPersonOid ChatNameLength ChatMessageLength ChatIdentifier ServerseitigeOid ClientseitigeOid Tenant IsEmployee MessageId ChatName TimeStamp IsDelivered - 11; 0; 15; 0; 5; 0; 0; ; 1; ; Elliot Alderson 03.11.2016 15:00:05 0 + messagesCharCount -= firstMessage.length(); + packetString = packetString.substring(firstMessageLength); +// Log.i(ANALYZERLOGTAG, "Reduzierter packetString: " + packetString); + } - - */ - - int actualChatNameLength = Integer.parseInt(gesplittet[2]); // 15 bei Elliot Alderson - - int actualChatMessageLength = Integer.parseInt(gesplittet[3]); - // + 20 -> 19 für den Timestamp und 1 für IsDelivered - int firstMessageLength = headerLength + actualChatMessageLength + 20; - - Log.i("PACKET_ANALYZER", "Header-Length: " + headerLength); - Log.i("PACKET_ANALYZER", "Länge der ersten Nachricht: " + firstMessageLength); - Log.i("PACKET_ANALYZER", "Erste Nachricht: " + packetString.substring(0, firstMessageLength)); - - return packetCount; + return packets; } } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapCalls.java b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapCalls.java index 3ce0d67bf..5efba1dcd 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapCalls.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapCalls.java @@ -13,25 +13,27 @@ public class SoapCalls { public static String CHECK_MESSAGE_IDS = "CheckChatMessagesByUUIDs"; + public static String LOAD_CHATMESSAGES_CHUNKWISE = "LoadChatMessagesChunkwise"; + //TODO: "app4.bewoplaner.de";// - public static String DESTINATION_ADDRESS = "app4.bewoplaner.de"; - public static int DESTINATION_PORT = 5000; - - public static String TOKEN_CHECK_URL = "https://" + DESTINATION_ADDRESS + "/mobil/main/checktoken?token="; - - public static String WEB_DOKU_URL = "https://app1.bewoplaner.de/mobil/login/1234567890"; - - public static String WSDL_TARGET_NAME = "bliblablubb.org/"; - public static String SOAP_ADDRESS = "https://" + DESTINATION_ADDRESS + "/BeWoPlanerAndroid/AndroidSoapService.asmx"; - - -// public static String DESTINATION_ADDRESS = "192.168.1.103"; +// public static String DESTINATION_ADDRESS = "app4.bewoplaner.de"; // public static int DESTINATION_PORT = 5000; // -// public static String TOKEN_CHECK_URL = "http://" + DESTINATION_ADDRESS + "/bewoplanermobil/main/checktoken?token="; +// public static String TOKEN_CHECK_URL = "https://" + DESTINATION_ADDRESS + "/mobil/main/checktoken?token="; // -// public static String WEB_DOKU_URL = "http://" + DESTINATION_ADDRESS + "/bewoplanermobil/login/demo"; +// public static String WEB_DOKU_URL = "https://app1.bewoplaner.de/mobil/login/1234567890"; // -// static String WSDL_TARGET_NAME = "bliblablubb.org/"; -// static String SOAP_ADDRESS = "http://" + DESTINATION_ADDRESS + "/BeWoPlanerAndroid/AndroidSoapService.asmx"; +// public static String WSDL_TARGET_NAME = "bliblablubb.org/"; +// public static String SOAP_ADDRESS = "https://" + DESTINATION_ADDRESS + "/BeWoPlanerAndroid/AndroidSoapService.asmx"; + + + public static String DESTINATION_ADDRESS = "192.168.1.103"; + public static int DESTINATION_PORT = 5000; + + public static String TOKEN_CHECK_URL = "http://" + DESTINATION_ADDRESS + "/bewoplanermobil/main/checktoken?token="; + + public static String WEB_DOKU_URL = "http://" + DESTINATION_ADDRESS + "/bewoplanermobil/login/demo"; + + static String WSDL_TARGET_NAME = "bliblablubb.org/"; + static String SOAP_ADDRESS = "http://" + DESTINATION_ADDRESS + "/BeWoPlanerAndroid/AndroidSoapService.asmx"; } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapConnectionManager.java b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapConnectionManager.java index a70f804c7..efec239d7 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapConnectionManager.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/soapConnection/SoapConnectionManager.java @@ -13,6 +13,8 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; +import util.BeWoLog; + /** * Created by JettenM on 05.07.2016. */ @@ -24,6 +26,7 @@ public class SoapConnectionManager { } public static void setUser(ApplicationUser pUser) { + BeWoLog.writeToLogFile("User wird gesetzt: " + (pUser == null ? "NULL" : pUser.toString())); user = pUser; } @@ -51,7 +54,10 @@ public class SoapConnectionManager { return (SoapPrimitive) envelope.getResponse(); } catch(IOException |XmlPullParserException exception) { - Log.e(LOGTAG, exception.getMessage()); + if(exception.getMessage() != null) { + Log.e(LOGTAG, exception.getMessage()); + BeWoLog.writeExceptionToLog(exception); + } } return null; diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/tcpConnection/SocketManager.java b/Android/BewoMitarbeiterApp/app/src/main/java/tcpConnection/SocketManager.java index 637e4e767..a9daf118b 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/tcpConnection/SocketManager.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/tcpConnection/SocketManager.java @@ -97,7 +97,7 @@ public class SocketManager { BeWoChatApplication.lastOnlineTS = Calendar.getInstance().getTime(); } catch(Exception e) { - BeWoChatApplication.writeExceptionToLog(e); + BeWoLog.writeExceptionToLog(e); if(socketConnectionFailedCallback != null) { socketConnectionFailedCallback.doCallback(); @@ -128,7 +128,7 @@ public class SocketManager { loginOnServer(); } catch(Exception exception) { - BeWoChatApplication.writeExceptionToLog(exception); + BeWoLog.writeExceptionToLog(exception); socket = null; try { @@ -139,7 +139,7 @@ public class SocketManager { } }, 11000); } catch(Exception exception2) { - BeWoChatApplication.writeExceptionToLog(exception2); + BeWoLog.writeExceptionToLog(exception2); exception2.printStackTrace(); if(connectionFailureCallback != null) { connectionFailureCallback.doCallback(); @@ -163,7 +163,7 @@ public class SocketManager { new Thread(new MessageSender(packet.getDataStream())).start(); //TODO: komplett beenden } catch(Exception exception) { - BeWoChatApplication.writeExceptionToLog(exception); + BeWoLog.writeExceptionToLog(exception); exception.printStackTrace(); } } else { @@ -183,7 +183,7 @@ public class SocketManager { new Thread(new MessageSender(packet.getDataStream())).start(); } catch(Exception exception) { - BeWoChatApplication.writeExceptionToLog(exception); + BeWoLog.writeExceptionToLog(exception); exception.printStackTrace(); } } else { @@ -224,7 +224,7 @@ public class SocketManager { BeWoLog.writeToLogFile("SendDate wurde erfolgreich aktualisiert: " + (affectedRows == 1)); } } catch (IOException e) { - BeWoChatApplication.writeExceptionToLog(e); + BeWoLog.writeExceptionToLog(e); try { Packet packet = new Packet(new String(packetToSend, "UTF-8")); @@ -244,7 +244,7 @@ public class SocketManager { SocketManager.connectToServer("MessageSender beim gescheiterten Versenden einer Nachricht."); } catch (UnsupportedEncodingException e1) { - BeWoChatApplication.writeExceptionToLog(e1); + BeWoLog.writeExceptionToLog(e1); e1.printStackTrace(); } @@ -296,142 +296,145 @@ public class SocketManager { stream.read(message, 0, size); try { - Packet.analyzingPackets(new String(message, "UTF-8")); + ArrayList packets = Packet.analyzePackets(new String(message, "UTF-8")); + Log.i(LOGTAG, "Anzahl packets: " + packets.size()); - chatPacket = new Packet(new String(message, "UTF-8")); - } catch(Exception e) { - BeWoChatApplication.writeExceptionToLog(e); - e.printStackTrace(); - } + for(Packet chatPacket : packets) { + if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.LOGIN_SUCCESS)) { + loginInProgress = false; - if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.LOGIN_SUCCESS)) { - loginInProgress = false; - - BeWoLog.writeToLogFile("Der Login am TCP-Server war erfolgreich. (SocketManager)"); - Log.i(LOGTAG, "Der Login war erfolgreich"); - if(initialLoginCallback != null) { - initialLoginCallback.doCallback(); - } - - if(loginCallback != null) { - loginCallback.doCallback(); - } - - if(loginCallback2 != null) { - loginCallback2.doCallback(); - } - - if(loginCallbackAfterConnectionLost != null) { - loginCallbackAfterConnectionLost.doCallback(); - } - } - - try { - BeWoLog.writeToLogFile("Erhalte Packet mit Identifier: " + chatPacket.ChatDataIdentifier.name()); - Log.i("SOCKET_MANAGER", "Erhalte Packet mit Identifier: " + chatPacket.ChatDataIdentifier.name()); - - switch (chatPacket.ChatDataIdentifier) { - case KEEP_ALIVE: - - BeWoLog.writeToLogFile("KeepAlive-Antwort erhalten. (SocketManger)"); - - break; - case MESSAGE: - BeWoLog.writeToLogFile("Empfange Nacahricht(" + chatPacket.MessageId + "). (SocketManager)"); - Log.i(LOGTAG, "Empfange Nacahricht"); - - databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, false, chatPacket.MessageId)); - - break; - - case TEAM: - BeWoLog.writeToLogFile("Empfange Team-Nacahricht(" + chatPacket.MessageId + "). (SocketManager)"); - databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, true, chatPacket.MessageId)); - - break; - - case MESSAGE_RESPONSE: - BeWoLog.writeToLogFile("Empfange Response für Message: " + chatPacket.MessageId + "; (SocketManager)"); - ChatMessage cm = databaseHandler.getChatMessageById(chatPacket.ClientseitigeOid); - - cm.MessageId = chatPacket.MessageId; - cm.IsDelivered = true; - - int affectedRows = databaseHandler.updateChatMessage(cm); - BeWoLog.writeToLogFile("Durch das Aktualisieren der ChatMessage bei Response betroffene Datensätze: " + affectedRows + ". (SocketManager -> MESSAGE_RESPONSE)"); - - break; - case STATUS_NOTIFICATION_LOGIN: - BeWoLog.writeToLogFile("Benutzer " + chatPacket.ChatName + " ist jetzt online. (SocketManager)"); - if(!SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) { - SocketManager.onlinePersonOids.add(chatPacket.SenderPersonOid); + BeWoLog.writeToLogFile("Der Login am TCP-Server war erfolgreich. (SocketManager)"); + Log.i(LOGTAG, "Der Login war erfolgreich"); + if(initialLoginCallback != null) { + initialLoginCallback.doCallback(); } - boolean isAlreadyLoggedIn = false; - for(ChatEntity entity : SocketManager.onlineEntities) - { - if(entity.getOid() == chatPacket.SenderPersonOid) { - isAlreadyLoggedIn = true; + if(loginCallback != null) { + loginCallback.doCallback(); + } + + if(loginCallback2 != null) { + loginCallback2.doCallback(); + } + + if(loginCallbackAfterConnectionLost != null) { + loginCallbackAfterConnectionLost.doCallback(); + } + } + + try { + BeWoLog.writeToLogFile("Erhalte Packet mit Identifier: " + chatPacket.ChatDataIdentifier.name()); + Log.i("SOCKET_MANAGER", "Erhalte Packet mit Identifier: " + chatPacket.ChatDataIdentifier.name()); + + switch (chatPacket.ChatDataIdentifier) { + case KEEP_ALIVE: + + BeWoLog.writeToLogFile("KeepAlive-Antwort erhalten. (SocketManger)"); + + break; + case MESSAGE: + BeWoLog.writeToLogFile("Empfange Nacahricht(" + chatPacket.MessageId + "). (SocketManager)"); + Log.i(LOGTAG, "Empfange Nacahricht"); + + databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, false, chatPacket.MessageId)); + + break; + + case TEAM: + BeWoLog.writeToLogFile("Empfange Team-Nacahricht(" + chatPacket.MessageId + "). (SocketManager)"); + databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, true, chatPacket.MessageId)); + + break; + + case MESSAGE_RESPONSE: + BeWoLog.writeToLogFile("Empfange Response für Message: " + chatPacket.MessageId + "; (SocketManager)"); + ChatMessage cm = databaseHandler.getChatMessageById(chatPacket.ClientseitigeOid); + + cm.MessageId = chatPacket.MessageId; + cm.IsDelivered = true; + + int affectedRows = databaseHandler.updateChatMessage(cm); + BeWoLog.writeToLogFile("Durch das Aktualisieren der ChatMessage bei Response betroffene Datensätze: " + affectedRows + ". (SocketManager -> MESSAGE_RESPONSE)"); + + break; + case STATUS_NOTIFICATION_LOGIN: + BeWoLog.writeToLogFile("Benutzer " + chatPacket.ChatName + " ist jetzt online. (SocketManager)"); + if(!SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) { + SocketManager.onlinePersonOids.add(chatPacket.SenderPersonOid); + } + + boolean isAlreadyLoggedIn = false; + for(ChatEntity entity : SocketManager.onlineEntities) + { + if(entity.getOid() == chatPacket.SenderPersonOid) { + isAlreadyLoggedIn = true; + break; + } + } + + if(!isAlreadyLoggedIn) { + SocketManager.onlineEntities.add(new ChatEntity(chatPacket.SenderPersonOid, false, chatPacket.IsEmployee)); + } + + break; + case STATUS_NOTIFICATION_LOGOUT: + BeWoLog.writeToLogFile("Benutzer " + chatPacket.ChatName + " ist jetzt offline. (SocketManager)"); + if(SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) { + SocketManager.onlinePersonOids.remove(Integer.valueOf(chatPacket.SenderPersonOid)); + } + + ChatEntity entity2delete = Util.getChatEntityFromOnlineEntitiesByOid(chatPacket.SenderPersonOid, chatPacket.ChatDataIdentifier == DataIdentifier.TEAM); + + SocketManager.onlineEntities.remove(entity2delete); + + break; + + case STATUS_NOTIFICATION_LOGIN_BROADCAST: + BeWoLog.writeToLogFile("Aktualisiere Onlinestatusliste. (SocketManager)"); + String[] onlinePersons = chatPacket.ChatMessage.split(","); + + for (String onlinePerson : onlinePersons) { + + int personOid = Integer.parseInt(onlinePerson); + + if(!SocketManager.onlinePersonOids.contains(personOid)) { + SocketManager.onlinePersonOids.add(personOid); + } + } + + break; + + case STATUS_REQUEST: + BeWoLog.writeToLogFile("Empfange eingeloggte User. (SocketManager)"); + String contactsCsv = chatPacket.ChatMessage; + + String[] stringOids = contactsCsv.split(","); + + SocketManager.onlinePersonOids.clear(); + + for(String item : stringOids) { + SocketManager.onlinePersonOids.add(Integer.valueOf(item)); + } + break; - } } + } catch(Exception e) { + BeWoLog.writeExceptionToLog(e); + e.printStackTrace(); + } - if(!isAlreadyLoggedIn) { - SocketManager.onlineEntities.add(new ChatEntity(chatPacket.SenderPersonOid, false, chatPacket.IsEmployee)); - } - - break; - case STATUS_NOTIFICATION_LOGOUT: - BeWoLog.writeToLogFile("Benutzer " + chatPacket.ChatName + " ist jetzt offline. (SocketManager)"); - if(SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) { - SocketManager.onlinePersonOids.remove(Integer.valueOf(chatPacket.SenderPersonOid)); - } - - ChatEntity entity2delete = Util.getChatEntityFromOnlineEntitiesByOid(chatPacket.SenderPersonOid, chatPacket.ChatDataIdentifier == DataIdentifier.TEAM); - - SocketManager.onlineEntities.remove(entity2delete); - - break; - - case STATUS_NOTIFICATION_LOGIN_BROADCAST: - BeWoLog.writeToLogFile("Aktualisiere Onlinestatusliste. (SocketManager)"); - String[] onlinePersons = chatPacket.ChatMessage.split(","); - - for (String onlinePerson : onlinePersons) { - - int personOid = Integer.parseInt(onlinePerson); - - if(!SocketManager.onlinePersonOids.contains(personOid)) { - SocketManager.onlinePersonOids.add(personOid); - } - } - - break; - - case STATUS_REQUEST: - BeWoLog.writeToLogFile("Empfange eingeloggte User. (SocketManager)"); - String contactsCsv = chatPacket.ChatMessage; - - String[] stringOids = contactsCsv.split(","); - - SocketManager.onlinePersonOids.clear(); - - for(String item : stringOids) { - SocketManager.onlinePersonOids.add(Integer.valueOf(item)); - } - - break; + if(uiHandler != null) { + uiHandler.updateUserInterface(chatPacket); + } else { + Log.e("ERROR_SOCKETLISTENER", "uiHandler ist null"); + } } + } catch(Exception e) { - BeWoChatApplication.writeExceptionToLog(e); + BeWoLog.writeExceptionToLog(e); e.printStackTrace(); } - if(uiHandler != null) { - uiHandler.updateUserInterface(chatPacket); - } else { - Log.e("ERROR_SOCKETLISTENER", "uiHandler ist null"); - } } } else { BeWoLog.writeToLogFile("Socket ist null. (SocketListener)"); @@ -441,7 +444,7 @@ public class SocketManager { } } } catch(Exception exception) { - BeWoChatApplication.writeExceptionToLog(exception); + BeWoLog.writeExceptionToLog(exception); exception.printStackTrace(); } } @@ -473,7 +476,7 @@ public class SocketManager { return res1.toString(); } } catch(Exception ex) { - BeWoChatApplication.writeExceptionToLog(ex); + BeWoLog.writeExceptionToLog(ex); Log.e("GET_MAC_ADDRESS", ex.getMessage()); } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/util/BeWoLog.java b/Android/BewoMitarbeiterApp/app/src/main/java/util/BeWoLog.java index d1aadb83b..ff60d7a3e 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/util/BeWoLog.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/util/BeWoLog.java @@ -60,4 +60,14 @@ public class BeWoLog { return logFile.delete(); } + + public static void writeExceptionToLog(Throwable ex) { + String logText = "Ausnahme: " + ex.toString() + "; Message: " + ex.getMessage() + "; Cause: " + ex.getCause(); + for(StackTraceElement ste : ex.getStackTrace()) { + String steAsString = ste.toString(); + logText += "\t" + steAsString; + } + + writeToLogFile(logText); + } } diff --git a/Android/BewoMitarbeiterApp/app/src/main/java/util/SecurityUtils.java b/Android/BewoMitarbeiterApp/app/src/main/java/util/SecurityUtils.java index 106b6f340..b6a6964dd 100644 --- a/Android/BewoMitarbeiterApp/app/src/main/java/util/SecurityUtils.java +++ b/Android/BewoMitarbeiterApp/app/src/main/java/util/SecurityUtils.java @@ -27,8 +27,7 @@ import javax.crypto.spec.SecretKeySpec; */ public class SecurityUtils { private static final byte[] iV16 = { (byte)35, (byte)138, (byte)177, (byte)253, (byte)227, (byte)63, (byte)2, (byte)27, (byte)9, (byte)17, (byte)192, (byte)230, (byte)1, (byte)24, (byte)165, (byte)99 }; - - public static final byte[] key = { (byte)174, (byte)130, (byte)219, (byte)185, (byte)185, (byte)221, (byte)96, (byte)50, (byte)37, (byte)212, (byte)81, (byte)121, (byte)71, (byte)206, (byte)130, (byte)153}; + private static final byte[] key = { (byte)174, (byte)130, (byte)219, (byte)185, (byte)185, (byte)221, (byte)96, (byte)50, (byte)37, (byte)212, (byte)81, (byte)121, (byte)71, (byte)206, (byte)130, (byte)153 }; private static final IvParameterSpec ivSpec16 = new IvParameterSpec(iV16); private static final String transformation = "AES/CBC/PKCS5Padding"; diff --git a/BeWoChatServer/Server.cs b/BeWoChatServer/Server.cs index 6c4e3aba1..484745595 100644 --- a/BeWoChatServer/Server.cs +++ b/BeWoChatServer/Server.cs @@ -249,7 +249,7 @@ namespace BeWoChatServer Monitor.Enter(_Lock); - var notificationPacketLogout2 = new Packet(dataPacket.Socket, dataPacket.ClientName, dataPacket.SenderPersonOid, 0, dataPacket.IsEmployee) { DataIdentifier = DataIdentifier.StatusNotificationLogout }; + var notificationPacketLogout2 = new Packet(dataPacket.Socket, dataPacket.ClientName, dataPacket.SenderPersonOid, 0, dataPacket.IsEmployee) { DataIdentifier = DataIdentifier.StatusNotificationLogout, Tenant = dataPacket.Tenant}; LoginData wtf = null; var wasAbleToRemoveClient = clientList.TryRemove(dataPacket.Socket, out wtf); @@ -519,7 +519,7 @@ namespace BeWoChatServer private static void SendNotificationPacket(Packet dataPacket, DataIdentifier dataIdentifier) { - var notificationPacket = new Packet(dataPacket.Socket, dataPacket.ClientName, dataPacket.SenderPersonOid, 0, dataPacket.IsEmployee) { DataIdentifier = DataIdentifier.StatusNotificationLogout }; + var notificationPacket = new Packet(dataPacket.Socket, dataPacket.ClientName, dataPacket.SenderPersonOid, 0, dataPacket.IsEmployee) { DataIdentifier = DataIdentifier.StatusNotificationLogout, Tenant = dataPacket.Tenant}; Console.WriteLine(DateTime.Now + ": Sende Statusänderung(" + dataIdentifier + ") an alle anderen Clients"); foreach (var client in clientList.Where(w => w.Value.PersonOid != dataPacket.SenderPersonOid)) diff --git a/BeWoPlanerAndroid/AndroidSoapService.asmx.cs b/BeWoPlanerAndroid/AndroidSoapService.asmx.cs index 839b0e5fb..dae703a33 100644 --- a/BeWoPlanerAndroid/AndroidSoapService.asmx.cs +++ b/BeWoPlanerAndroid/AndroidSoapService.asmx.cs @@ -193,6 +193,24 @@ namespace BeWoPlanerAndroid return JsonConvert.SerializeObject(existingMessageIds); } + [WebMethod(EnableSession = true)] + public string LoadChatMessagesChunkwise(string messageId, long pRecipientPersonOid, long pSenderPersonOid, bool pIsForTeam, string pExceptions) + { + var exceptions = new List(); + + if (pExceptions.Length > 0) + { + var splittedString = pExceptions.Split(';'); + exceptions = splittedString.ToList(); + } + + var jsonMessages = new List(); + + var liste = DAOFactory.SearchDAO.LoadChatMessagesChunkwise(messageId, pRecipientPersonOid, pSenderPersonOid, pIsForTeam, exceptions).DoForEach(dfe => jsonMessages.Add(new ChatMessageForJson(dfe.EmpfaengerPersonOid, dfe.SenderPersonOid, dfe.IsDelivered, dfe.IstGelesen, dfe.TeamOid, dfe.ChatText, dfe.Uhrzeit, dfe.Oid, dfe.InsTs, dfe.MessageId))); + + return JsonConvert.SerializeObject(jsonMessages); + } + public class ChatPerson { public long Oid { get; set; } diff --git a/Data/Access/SearchDAO.cs b/Data/Access/SearchDAO.cs index 04e11b93a..e851a916c 100644 --- a/Data/Access/SearchDAO.cs +++ b/Data/Access/SearchDAO.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Text; + using BeWo.Data.Entities; + using BS.Shared.Extensions; + using NHibernate.Criterion; using NHibernate.SqlCommand; using BS.Shared.Core; using BS.Shared; -using BS.Shared.DataContracts.Compact; -using NHibernate.Linq.Functions; -using NHibernate.Transform; + using Login = BeWo.Data.Entities.Login; namespace BeWo.Data.Access @@ -1619,8 +1619,8 @@ namespace BeWo.Data.Access Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pRecipientOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pSenderOid)))) - .Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))) - .Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); + .Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))) + .Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); if(pForTeam) { @@ -1673,6 +1673,7 @@ namespace BeWo.Data.Access return c.List(); } + //TODO: Anpassen. Ist fehlerhaft. public IEnumerable FindNewestChatMessages(long pRecipientPersonOid) { var query2 = Session.CreateSQLQuery(string.Format("SELECT MAX({0}) FROM chatmessages WHERE ({1} = {2} OR {3} = {2}) AND {4} IS NULL OR {4} IS NOT NULL AND {1} IN (SELECT t.{0} FROM Team t JOIN employee2team e ON e.{4} = t.{0}) GROUP BY {1}, {3}", @@ -1692,5 +1693,40 @@ namespace BeWo.Data.Access return liste.Select(s => s.MessageId); } + + public IEnumerable LoadChatMessagesChunkwise(string pMessageId, long pRecipientOid, long pSenderOid, bool pForTeam, List pExceptions) + { + var c = CreateCriteriaIsActive() + .Add(Restrictions.Or( + Restrictions.And( + Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pSenderOid), + Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pRecipientOid)), + Restrictions.And( + Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pRecipientOid), + Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pSenderOid)))); + + c.Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); + + if (pForTeam) + { + c = CreateCriteriaIsActive() + .Add(Restrictions.Eq(ChatMessage.PropertyName_TeamOid, pRecipientOid)); + } + + if (!string.IsNullOrEmpty(pMessageId)) + { + var c1 = CreateCriteriaIsActive() + .Add(Restrictions.Eq(ChatMessage.PropertyName_MessageId, pMessageId)); + var lastLoadedChatMessage = c1.UniqueResult(); + + c.Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))); + c.Add(Restrictions.Gt(BeWoEntityBase.PropertyName_InsTs, lastLoadedChatMessage.InsTs)); + } + + c.AddOrder(Order.Desc(BeWoEntityBase.PropertyName_InsTs)); + c.SetMaxResults(15); + + return c.List(); + } } }