BeWoMitarbeiterapp: Socket-Management überarbeitet
This commit is contained in:
2
Android/BewoMitarbeiterApp/.idea/misc.xml
generated
2
Android/BewoMitarbeiterApp/.idea/misc.xml
generated
@@ -37,7 +37,7 @@
|
|||||||
<ConfirmationsSetting value="0" id="Add" />
|
<ConfirmationsSetting value="0" id="Add" />
|
||||||
<ConfirmationsSetting value="0" id="Remove" />
|
<ConfirmationsSetting value="0" id="Remove" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|||||||
@@ -64,14 +64,6 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
|
||||||
@@ -80,6 +72,14 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
|
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
|
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
|
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
|
||||||
@@ -114,8 +114,8 @@
|
|||||||
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
|
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
|
||||||
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
|
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
|
||||||
<orderEntry type="library" exported="" name="commons-lang3-3.3.2" level="project" />
|
<orderEntry type="library" exported="" name="commons-lang3-3.3.2" level="project" />
|
||||||
<orderEntry type="library" exported="" name="commons-io-1.3.2" level="project" />
|
|
||||||
<orderEntry type="library" exported="" name="robospice-1.4.14" level="project" />
|
<orderEntry type="library" exported="" name="robospice-1.4.14" level="project" />
|
||||||
|
<orderEntry type="library" exported="" name="commons-io-1.3.2" level="project" />
|
||||||
<orderEntry type="library" exported="" name="robospice-cache-1.4.14" level="project" />
|
<orderEntry type="library" exported="" name="robospice-cache-1.4.14" level="project" />
|
||||||
<orderEntry type="module" module-name="volley" exported="" />
|
<orderEntry type="module" module-name="volley" exported="" />
|
||||||
<orderEntry type="library" exported="" name="android-android-21" level="project" />
|
<orderEntry type="library" exported="" name="android-android-21" level="project" />
|
||||||
|
|||||||
@@ -62,8 +62,8 @@
|
|||||||
android:name=".LoginActivity"
|
android:name=".LoginActivity"
|
||||||
android:label="@string/title_activity_login">
|
android:label="@string/title_activity_login">
|
||||||
</activity>
|
</activity>
|
||||||
<activity android:name=".TokenActivity">
|
<!--<activity android:name=".TokenActivity">
|
||||||
</activity>
|
</activity>-->
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name="com.octo.android.robospice.UncachedSpiceService"
|
android:name="com.octo.android.robospice.UncachedSpiceService"
|
||||||
|
|||||||
@@ -42,17 +42,10 @@ import com.octo.android.robospice.request.listener.RequestListener;
|
|||||||
|
|
||||||
import org.ksoap2.serialization.SoapPrimitive;
|
import org.ksoap2.serialization.SoapPrimitive;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.net.Socket;
|
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
@@ -67,51 +60,40 @@ import entities.ChatMessage;
|
|||||||
import soapConnection.DataIdentifier;
|
import soapConnection.DataIdentifier;
|
||||||
import soapConnection.Packet;
|
import soapConnection.Packet;
|
||||||
|
|
||||||
|
import tcpConnection.SocketManager;
|
||||||
|
import tcpConnection.UIHandler;
|
||||||
import util.SecurityUtils;
|
import util.SecurityUtils;
|
||||||
import util.Util;
|
import util.Util;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by bib on 03.05.2016.
|
* Created by bib on 03.05.2016.
|
||||||
*/
|
*/
|
||||||
public class ChatActivity extends ActionBarActivity{
|
public class ChatActivity extends ActionBarActivity {
|
||||||
|
|
||||||
private static final String LOG_TAG = "CHAT";
|
private static final String LOG_TAG = "CHAT";
|
||||||
|
|
||||||
private DatabaseHandler databaseHandler;
|
private DatabaseHandler databaseHandler;
|
||||||
|
|
||||||
//View Variablen
|
|
||||||
protected TextView chat;
|
protected TextView chat;
|
||||||
protected EditText text;
|
protected EditText text;
|
||||||
protected Button senden;
|
protected Button senden;
|
||||||
|
|
||||||
//Server Variablen
|
|
||||||
public static Socket socket;
|
|
||||||
DataOutputStream socketOutputStream;
|
|
||||||
|
|
||||||
public static Thread listen;
|
|
||||||
public static SocketListener socketListener;
|
|
||||||
|
|
||||||
String saveCompleteMessage;
|
|
||||||
|
|
||||||
//Design View
|
|
||||||
protected ListView listView;
|
protected ListView listView;
|
||||||
public static List<ChatMessage> chatMessages;
|
public static List<ChatMessage> chatMessages;
|
||||||
public static ArrayAdapter<ChatMessage> adapter;
|
public static ArrayAdapter<ChatMessage> adapter;
|
||||||
|
|
||||||
File file;
|
File file;
|
||||||
File UnsentFile = null;
|
|
||||||
|
|
||||||
protected String recipientName;
|
protected String recipientName;
|
||||||
protected int recipientPersonOid;
|
protected int recipientPersonOid;
|
||||||
|
|
||||||
private SpiceManager spiceManager = new SpiceManager(UncachedSpiceService.class);
|
private SpiceManager spiceManager = new SpiceManager(UncachedSpiceService.class);
|
||||||
|
|
||||||
|
private UIHandler chatUIHandler;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
Log.i("CHAT_ON_CREATE", "Kreiere...");
|
|
||||||
|
|
||||||
setContentView(R.layout.chat);
|
setContentView(R.layout.chat);
|
||||||
|
|
||||||
ActionBar action = getSupportActionBar();
|
ActionBar action = getSupportActionBar();
|
||||||
@@ -122,11 +104,9 @@ public class ChatActivity extends ActionBarActivity{
|
|||||||
LayoutInflater inflate = LayoutInflater.from(this);
|
LayoutInflater inflate = LayoutInflater.from(this);
|
||||||
View mCustomView = inflate.inflate(R.layout.custom_menuebar, null);
|
View mCustomView = inflate.inflate(R.layout.custom_menuebar, null);
|
||||||
|
|
||||||
// Initialiesiere die Variablen
|
|
||||||
TextView titlename = (TextView)mCustomView.findViewById(R.id.PersonalName);
|
TextView titlename = (TextView)mCustomView.findViewById(R.id.PersonalName);
|
||||||
ImageView titlepic = (ImageView)mCustomView.findViewById(R.id.PersonalPic);
|
ImageView titlepic = (ImageView)mCustomView.findViewById(R.id.PersonalPic);
|
||||||
|
|
||||||
//get Daten aus vorheriger Activity
|
|
||||||
Bundle zielkorb = getIntent().getExtras();
|
Bundle zielkorb = getIntent().getExtras();
|
||||||
recipientName = zielkorb.getString("Name");
|
recipientName = zielkorb.getString("Name");
|
||||||
recipientPersonOid = zielkorb.getInt("RecipientOid");
|
recipientPersonOid = zielkorb.getInt("RecipientOid");
|
||||||
@@ -134,7 +114,6 @@ public class ChatActivity extends ActionBarActivity{
|
|||||||
byte[] bildArray = zielkorb.getByteArray("Bild");
|
byte[] bildArray = zielkorb.getByteArray("Bild");
|
||||||
Bitmap customerImage;
|
Bitmap customerImage;
|
||||||
|
|
||||||
//ein try catch wäre vorteilhaft || Später
|
|
||||||
if(bildArray != null) {
|
if(bildArray != null) {
|
||||||
customerImage = BitmapFactory.decodeByteArray(bildArray, 0, bildArray.length);
|
customerImage = BitmapFactory.decodeByteArray(bildArray, 0, bildArray.length);
|
||||||
titlepic.setImageBitmap(GetCircleBitmap(customerImage));
|
titlepic.setImageBitmap(GetCircleBitmap(customerImage));
|
||||||
@@ -158,329 +137,97 @@ public class ChatActivity extends ActionBarActivity{
|
|||||||
adapter = new MessageAdapter(this, R.layout.item_chat_left, chatMessages);
|
adapter = new MessageAdapter(this, R.layout.item_chat_left, chatMessages);
|
||||||
listView.setAdapter(adapter);
|
listView.setAdapter(adapter);
|
||||||
|
|
||||||
|
chatUIHandler = new UIHandler() {
|
||||||
|
@Override
|
||||||
|
public void updateUserInterface(final Packet chatPacket) {
|
||||||
|
try {
|
||||||
|
if(!chatPacket.ChatDataIdentifier.equals(DataIdentifier.MESSAGE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
listView.post(new Runnable() {
|
||||||
socket = KontaktChatActivity.socket;
|
@Override
|
||||||
|
public void run() {
|
||||||
|
chatPacket.writeToLog();
|
||||||
|
|
||||||
KontaktChatActivity.CloseSocketListener();
|
ChatMessage message = new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, false, chatPacket.ServerseitigeOid);
|
||||||
MainActivityMitarbeiter.CloseSocketListener();
|
|
||||||
|
|
||||||
socketOutputStream = new DataOutputStream(socket.getOutputStream());
|
databaseHandler.addChatMessage(message);
|
||||||
|
|
||||||
socketListener = new SocketListener();
|
chatMessages.add(message);
|
||||||
listen = new Thread(socketListener);
|
adapter.notifyDataSetChanged();
|
||||||
listen.start();
|
}
|
||||||
} catch(Exception e){
|
});
|
||||||
Log.e("CHAT_ERROR", "Fehler bei der Socket-Erstellung: " + e.getMessage());
|
} catch(Exception pe) {
|
||||||
}
|
pe.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
text = (EditText)findViewById(R.id.editText);
|
text = (EditText)findViewById(R.id.editText);
|
||||||
|
|
||||||
senden.setOnClickListener(new View.OnClickListener() {
|
senden.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
|
||||||
if(!text.getText().toString().isEmpty()) {
|
if(!text.getText().toString().isEmpty()) {
|
||||||
new Thread(new MessageSender()).start();
|
|
||||||
} else {
|
|
||||||
Toast.makeText(ChatActivity.this, "Sie müssen schon Text eingeben ", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Thread t1 = new Thread(new RequestSender());
|
|
||||||
t1.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
Log.i("CHAT_ON_RESUME", "Fahre fort...");
|
|
||||||
|
|
||||||
if(socketListener != null && !socketListener.isRunning) {
|
|
||||||
socketListener.isRunning = true;
|
|
||||||
Log.i("CHAT_ON_RESUME", "Listener läuft wieder");
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onResume();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop(){
|
|
||||||
Log.i("CHAT_ON_STOP", "Stoppe...");
|
|
||||||
super.onStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onDestroy(){
|
|
||||||
Log.i("CHAT_ON_DESTROY", "Zerstöre...");
|
|
||||||
|
|
||||||
CloseSocketListener();
|
|
||||||
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RequestSender implements Runnable {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
if(socket != null ) {
|
|
||||||
Packet p = new Packet();
|
|
||||||
|
|
||||||
p.ChatDataIdentifier = DataIdentifier.OLD_MESSAGE;
|
|
||||||
p.RecipientPersonOid = recipientPersonOid;
|
|
||||||
|
|
||||||
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
|
||||||
|
|
||||||
byte[] tenantByteArray = Base64.decode(settings.getString(Util.PREFS_TENANT_KEY, null), Base64.DEFAULT);
|
|
||||||
ByteArrayInputStream tenantStream = new ByteArrayInputStream(tenantByteArray);
|
|
||||||
|
|
||||||
p.Tenant = SecurityUtils.decrypt(tenantStream).toString();
|
|
||||||
|
|
||||||
socketOutputStream.write(p.GetDataStream());
|
|
||||||
socketOutputStream.flush();
|
|
||||||
}
|
|
||||||
} catch(IOException s){
|
|
||||||
Log.v("Status: ","Senden einer Anfrage, alle vergangenen Nachrichten zu laden konnte nicht ausgeführt werden.");
|
|
||||||
|
|
||||||
s.printStackTrace();
|
|
||||||
} catch (NoSuchPaddingException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MessageSender implements Runnable{
|
|
||||||
public void run(){
|
|
||||||
try{
|
|
||||||
if(socket != null ) {
|
|
||||||
|
|
||||||
Calendar c = Calendar.getInstance();
|
Calendar c = Calendar.getInstance();
|
||||||
final Date now = c.getTime();
|
Date now = c.getTime();
|
||||||
|
|
||||||
final Packet p = new Packet();
|
Packet packet = new Packet();
|
||||||
p.ChatMessage = text.getText().toString();
|
packet.ChatMessage = text.getText().toString();
|
||||||
p.ChatDataIdentifier = DataIdentifier.MESSAGE;
|
packet.ChatDataIdentifier = DataIdentifier.MESSAGE;
|
||||||
p.RecipientPersonOid = recipientPersonOid;
|
packet.RecipientPersonOid = recipientPersonOid;
|
||||||
p.MessageTimeStamp = now;
|
packet.MessageTimeStamp = now;
|
||||||
|
|
||||||
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
||||||
|
|
||||||
byte[] tenantByteArray = Base64.decode(settings.getString(Util.PREFS_TENANT_KEY, null), Base64.DEFAULT);
|
byte[] tenantByteArray = Base64.decode(settings.getString(Util.PREFS_TENANT_KEY, null), Base64.DEFAULT);
|
||||||
ByteArrayInputStream tenantStream = new ByteArrayInputStream(tenantByteArray);
|
ByteArrayInputStream tenantStream = new ByteArrayInputStream(tenantByteArray);
|
||||||
|
|
||||||
p.Tenant = SecurityUtils.decrypt(tenantStream).toString();
|
try {
|
||||||
|
packet.Tenant = SecurityUtils.decrypt(tenantStream).toString();
|
||||||
|
} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
final ChatMessage message = new ChatMessage(p.SenderPersonOid, p.RecipientPersonOid, p.MessageTimeStamp, p.ChatMessage, false, 0);
|
packet.writeToLog();
|
||||||
|
|
||||||
databaseHandler.addChatMessage(message);
|
final ChatMessage message = new ChatMessage(packet.SenderPersonOid, packet.RecipientPersonOid, packet.MessageTimeStamp, packet.ChatMessage, false, 0);
|
||||||
|
|
||||||
socketOutputStream.write(p.GetDataStream());
|
long coid = databaseHandler.addChatMessage(message);
|
||||||
socketOutputStream.flush();
|
|
||||||
|
packet.ClientseitigeOid = Long.valueOf(coid).intValue();
|
||||||
|
|
||||||
|
SocketManager.sendMessage(packet);
|
||||||
|
|
||||||
listView.post(new Runnable() {
|
listView.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
|
|
||||||
chatMessages.add(message);
|
chatMessages.add(message);
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
|
|
||||||
text.setText("");
|
text.setText("");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
Toast.makeText(ChatActivity.this, "Sie müssen schon Text eingeben ", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Log.i("SEND_CHAT_MESSAGE","Socket ist null");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}catch(IOException s){
|
|
||||||
Log.v("Status: ","Senden einer Nachricht konnte nicht ausgeführt werden. Save wird ausgeführt");
|
|
||||||
|
|
||||||
SaveAfterLoseConnection(saveCompleteMessage);
|
|
||||||
s.printStackTrace();
|
|
||||||
} catch (NoSuchPaddingException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void CloseSocketListener() {
|
@Override
|
||||||
if(listen != null && listen.isAlive()) {
|
public void onResume() {
|
||||||
try {
|
SocketManager.uiHandler = chatUIHandler;
|
||||||
socketListener.terminate();
|
|
||||||
listen.join();
|
super.onResume();
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erhalte Messages all the time
|
public void onDestroy(){
|
||||||
public class SocketListener implements Runnable{
|
Log.i("CHAT_ON_DESTROY", "Zerstöre...");
|
||||||
Packet chatPacket;
|
|
||||||
|
|
||||||
private volatile boolean isRunning = true;
|
super.onDestroy();
|
||||||
|
|
||||||
public void terminate() {
|
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run(){
|
|
||||||
try{
|
|
||||||
while(isRunning){
|
|
||||||
if (socket != null) {
|
|
||||||
if(socket.isConnected()) {
|
|
||||||
final ListView listV = (ListView) findViewById(R.id.list_msg);
|
|
||||||
|
|
||||||
InputStream stream = socket.getInputStream();
|
|
||||||
|
|
||||||
int size = stream.available();
|
|
||||||
|
|
||||||
if(size == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder total = new StringBuilder(stream.available());
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
while(counter < size) {
|
|
||||||
|
|
||||||
char c = (char) stream.read();
|
|
||||||
total.append(c);
|
|
||||||
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
chatPacket = new Packet(total.toString());
|
|
||||||
|
|
||||||
Log.i("CHAT_ACTIVIY_LISTENER", "Empfange Nachricht... " + chatPacket.ChatMessage);
|
|
||||||
|
|
||||||
if(!chatPacket.ChatDataIdentifier.equals(DataIdentifier.MESSAGE)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
listV.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
ChatMessage message = new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, false, chatPacket.ServerseitigeOid);
|
|
||||||
|
|
||||||
databaseHandler.addChatMessage(message);
|
|
||||||
|
|
||||||
chatMessages.add(message);
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch(Exception pe) {
|
|
||||||
Log.e("CONVERTING JSON: ", pe.getMessage());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Log.i("SOCKETLISTENER/CHAT", "Nicht mit dem Socket verbunden");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("Status ","Verbindung zum Server wurde seitens dem Server getrennt: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SaveAfterLoseConnection(String UnsendMessage){
|
|
||||||
CreateCustomFileForUnsentMessage();
|
|
||||||
try{
|
|
||||||
FileOutputStream stream = new FileOutputStream(UnsentFile,true);
|
|
||||||
|
|
||||||
try{
|
|
||||||
stream.write(UnsendMessage.getBytes());
|
|
||||||
}finally {
|
|
||||||
stream.close();
|
|
||||||
}
|
|
||||||
}catch (Exception e){
|
|
||||||
Log.v("Fehler",e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SendAfterGetConnection(){
|
|
||||||
//ist eine UnsendMessage File vorhanden
|
|
||||||
if(UnsentFile != null && socket.isConnected()){
|
|
||||||
try{
|
|
||||||
FileInputStream stream = new FileInputStream(UnsentFile);
|
|
||||||
InputStreamReader in = new InputStreamReader(stream);
|
|
||||||
BufferedReader readLn = new BufferedReader(in);
|
|
||||||
|
|
||||||
String line = readLn.readLine();
|
|
||||||
String allLines="";
|
|
||||||
|
|
||||||
while (line != null) {
|
|
||||||
allLines += line;
|
|
||||||
|
|
||||||
String[] splitt = line.split("/");
|
|
||||||
|
|
||||||
if(splitt.length >1 ) {
|
|
||||||
String[] ArtDate = splitt[1].split("%");
|
|
||||||
|
|
||||||
final String message = splitt[0];
|
|
||||||
final boolean art = Boolean.valueOf(ArtDate[0]);
|
|
||||||
final String datetime = ArtDate[1];
|
|
||||||
|
|
||||||
//für jede Gelesene Line Sende Nachricht
|
|
||||||
new Thread(new SendUnsentMessage(message)).start();
|
|
||||||
}
|
|
||||||
line = readLn.readLine();
|
|
||||||
}
|
|
||||||
stream.close();
|
|
||||||
in.close();
|
|
||||||
UnsentFile.delete();
|
|
||||||
}catch(Exception fe){
|
|
||||||
Log.v("Status","Ein problem ist im BEreich Unsend Ms Aufgetreten");
|
|
||||||
}
|
|
||||||
//recreate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendUnsentMessage implements Runnable{
|
|
||||||
|
|
||||||
String al;
|
|
||||||
|
|
||||||
SendUnsentMessage(String unMS){
|
|
||||||
this.al = unMS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
byte[] ts = al.getBytes();
|
|
||||||
try {
|
|
||||||
if (socket != null && socketOutputStream != null) {
|
|
||||||
|
|
||||||
//try catch bei catch SaveAfterLoseConnection();
|
|
||||||
socketOutputStream.write(ts);
|
|
||||||
socketOutputStream.flush();
|
|
||||||
}
|
|
||||||
}catch(Exception xs){
|
|
||||||
Log.e("SendUnsentMessage", xs.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CreateCustomFileForUnsentMessage(){
|
|
||||||
try{
|
|
||||||
File pfad = getBaseContext().getFilesDir();
|
|
||||||
|
|
||||||
//Für external Save
|
|
||||||
//File pfad = getBaseContext().getExternalFilesDir(null);
|
|
||||||
|
|
||||||
//Erstelle das File Objekt
|
|
||||||
UnsentFile = new File(pfad,recipientName+"_Unsend"+".txt");
|
|
||||||
|
|
||||||
}catch(Exception e){
|
|
||||||
Log.v("Fehler"," Das erstellen/öffnen einer Datei ist Derzeit nicht möglich");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by bib on 03.05.2016.
|
* Created by bib on 03.05.2016.
|
||||||
@@ -30,7 +31,7 @@ import java.io.InputStreamReader;
|
|||||||
public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final ContactListItem[] values;
|
public final ArrayList<ContactListItem> values;
|
||||||
|
|
||||||
private Bitmap bitmaping;
|
private Bitmap bitmaping;
|
||||||
ImageView imageView;
|
ImageView imageView;
|
||||||
@@ -43,7 +44,7 @@ public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
|||||||
|
|
||||||
Thread getPic;
|
Thread getPic;
|
||||||
|
|
||||||
public KontaktAdapter(Context context, ContactListItem[] values) {
|
public KontaktAdapter(Context context, ArrayList<ContactListItem> values) {
|
||||||
super(context, R.layout.kontakt_list_chat, values);
|
super(context, R.layout.kontakt_list_chat, values);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.values = values;
|
this.values = values;
|
||||||
@@ -60,7 +61,7 @@ public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
|||||||
ImageView onlinestats = (ImageView)rowView.findViewById(R.id.status);
|
ImageView onlinestats = (ImageView)rowView.findViewById(R.id.status);
|
||||||
imageView = (ImageView) rowView.findViewById(R.id.logo);
|
imageView = (ImageView) rowView.findViewById(R.id.logo);
|
||||||
|
|
||||||
textView.setText(values[position].getChatName());
|
textView.setText(values.get(position).getChatName());
|
||||||
|
|
||||||
|
|
||||||
// if(values[position].getChatName().equals("Demo")) {
|
// if(values[position].getChatName().equals("Demo")) {
|
||||||
@@ -71,10 +72,10 @@ public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
//ab hier fehlerhaft
|
//ab hier fehlerhaft
|
||||||
if(values[position].getPicture() != null) {
|
if(values.get(position).getPicture() != null) {
|
||||||
|
|
||||||
//Konvert byte[] to Image
|
//Konvert byte[] to Image
|
||||||
Bitmap i = BitmapFactory.decodeByteArray(values[position].getPicture(),0,values[position].getPicture().length);
|
Bitmap i = BitmapFactory.decodeByteArray(values.get(position).getPicture(),0,values.get(position).getPicture().length);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -109,7 +110,7 @@ public class KontaktAdapter extends ArrayAdapter<ContactListItem> {
|
|||||||
// else if(values[position].getArt().equals("3"))
|
// else if(values[position].getArt().equals("3"))
|
||||||
// onlinestats.setImageResource(R.drawable.offline);
|
// onlinestats.setImageResource(R.drawable.offline);
|
||||||
|
|
||||||
onlinestats.setImageResource((values[position].isOnline ? R.drawable.online : R.drawable.offline));
|
onlinestats.setImageResource((values.get(position).isOnline ? R.drawable.online : R.drawable.offline));
|
||||||
|
|
||||||
return rowView;
|
return rowView;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ import org.ksoap2.serialization.PropertyInfo;
|
|||||||
import org.ksoap2.serialization.SoapPrimitive;
|
import org.ksoap2.serialization.SoapPrimitive;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -43,6 +41,8 @@ import soapConnection.JsonSoapPrimitiveRequest;
|
|||||||
import soapConnection.Packet;
|
import soapConnection.Packet;
|
||||||
import soapConnection.SoapCalls;
|
import soapConnection.SoapCalls;
|
||||||
import soapConnection.SoapConnectionManager;
|
import soapConnection.SoapConnectionManager;
|
||||||
|
import tcpConnection.SocketManager;
|
||||||
|
import tcpConnection.UIHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by bib on 02.05.2016.
|
* Created by bib on 02.05.2016.
|
||||||
@@ -54,19 +54,16 @@ public class KontaktChatActivity extends ListActivity {
|
|||||||
|
|
||||||
private ArrayList<Integer> onlinePersonOids = new ArrayList<>();
|
private ArrayList<Integer> onlinePersonOids = new ArrayList<>();
|
||||||
|
|
||||||
public static Socket socket;
|
private UIHandler kontaktChatUIHandler;
|
||||||
|
|
||||||
public static Thread listen;
|
|
||||||
public static SocketListener socketListener;
|
|
||||||
|
|
||||||
public ListView listView;
|
public ListView listView;
|
||||||
|
|
||||||
|
ArrayList<ContactListItem> contactList = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
Log.i("KONTACT_CHAT_CREATE", "Kreiere...");
|
|
||||||
|
|
||||||
databaseHandler = new DatabaseHandler(this);
|
databaseHandler = new DatabaseHandler(this);
|
||||||
|
|
||||||
//TODO: nicht löschen, sondern updaten...
|
//TODO: nicht löschen, sondern updaten...
|
||||||
@@ -83,21 +80,66 @@ public class KontaktChatActivity extends ListActivity {
|
|||||||
|
|
||||||
listView = getListView();
|
listView = getListView();
|
||||||
|
|
||||||
try {
|
kontaktChatUIHandler = new UIHandler() {
|
||||||
socket = MainActivityMitarbeiter.socket;
|
@Override
|
||||||
|
public void updateUserInterface(final Packet chatPacket) {
|
||||||
|
try {
|
||||||
|
switch(chatPacket.ChatDataIdentifier) {
|
||||||
|
case MESSAGE:
|
||||||
|
ChatMessage message = new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, false, chatPacket.ServerseitigeOid);
|
||||||
|
|
||||||
MainActivityMitarbeiter.CloseSocketListener();
|
databaseHandler.addChatMessage(message);
|
||||||
ChatActivity.CloseSocketListener();
|
|
||||||
|
|
||||||
// Thread t = new Thread(new SocketListener());
|
SetNotify(chatPacket.ChatName, chatPacket.ChatMessage, chatPacket.SenderPersonOid);
|
||||||
// t.start();
|
break;
|
||||||
|
case STATUS_NOTIFICATION_LOGIN:
|
||||||
|
if(!onlinePersonOids.contains(chatPacket.SenderPersonOid)) {
|
||||||
|
onlinePersonOids.add(chatPacket.SenderPersonOid);
|
||||||
|
|
||||||
socketListener = new SocketListener();
|
runOnUiThread(new Runnable() {
|
||||||
listen = new Thread(socketListener);
|
@Override
|
||||||
listen.start();
|
public void run() {
|
||||||
} catch(Exception e){
|
KontaktAdapter ka =((KontaktAdapter) listView.getAdapter());
|
||||||
Log.e("CHAT_ERROR", "Fehler bei der Socket-Erstellung: " + e.getMessage());
|
|
||||||
}
|
for(ContactListItem item : ka.values) {
|
||||||
|
if(item.getPersonOid() == chatPacket.SenderPersonOid) {
|
||||||
|
item.isOnline = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ka.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case STATUS_NOTIFICATION_LOGOUT:
|
||||||
|
if(onlinePersonOids.contains(chatPacket.SenderPersonOid)) {
|
||||||
|
onlinePersonOids.remove(Integer.valueOf(chatPacket.SenderPersonOid));
|
||||||
|
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
KontaktAdapter ka =((KontaktAdapter) listView.getAdapter());
|
||||||
|
|
||||||
|
for(ContactListItem item : ka.values) {
|
||||||
|
if(item.getPersonOid() == chatPacket.SenderPersonOid) {
|
||||||
|
item.isOnline = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ka.notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch(Exception pe) {
|
||||||
|
pe.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -152,113 +194,22 @@ public class KontaktChatActivity extends ListActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
Log.i("KONTAKT_CHAT_ON_RESUME", "Fahre fort...");
|
SocketManager.uiHandler = kontaktChatUIHandler;
|
||||||
|
|
||||||
if(socketListener != null && !socketListener.isRunning) {
|
|
||||||
socketListener.isRunning = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onResume();
|
super.onResume();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDestroy(){
|
|
||||||
Log.i("KONTAKT_CHAT_ON_DESTROY", "Zerstöre...");
|
|
||||||
|
|
||||||
CloseSocketListener();
|
|
||||||
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop(){
|
public void onStop(){
|
||||||
Log.i("KONTAKT_CHAT_ON_STOP", "Stoppe...");
|
|
||||||
|
|
||||||
spiceManager.shouldStop();
|
spiceManager.shouldStop();
|
||||||
super.onStop();
|
super.onStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
Log.i("KONTAKT_CHAT_ON_START", "Starte...");
|
|
||||||
|
|
||||||
super.onStart();
|
super.onStart();
|
||||||
spiceManager.start(this);
|
spiceManager.start(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SocketListener implements Runnable {
|
|
||||||
Packet chatPacket;
|
|
||||||
|
|
||||||
private volatile boolean isRunning = true;
|
|
||||||
|
|
||||||
public void terminate() {
|
|
||||||
|
|
||||||
isRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
while(isRunning) {
|
|
||||||
if (socket != null) {
|
|
||||||
if (socket.isConnected()) {
|
|
||||||
InputStream stream = socket.getInputStream();
|
|
||||||
|
|
||||||
int size = stream.available();
|
|
||||||
|
|
||||||
if(size == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder total = new StringBuilder(stream.available());
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
while(counter < size) {
|
|
||||||
|
|
||||||
char c = (char) stream.read();
|
|
||||||
total.append(c);
|
|
||||||
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
chatPacket = new Packet(total.toString());
|
|
||||||
|
|
||||||
Log.i("KONTAKT_CHAT_LISTENER", "Empfange Nachricht... " + chatPacket.ChatMessage);
|
|
||||||
|
|
||||||
if(!chatPacket.ChatDataIdentifier.equals(DataIdentifier.MESSAGE)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChatMessage message = new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, false, chatPacket.ServerseitigeOid);
|
|
||||||
|
|
||||||
databaseHandler.addChatMessage(message);
|
|
||||||
|
|
||||||
SetNotify(chatPacket.ChatName, chatPacket.ChatMessage, chatPacket.RecipientPersonOid);
|
|
||||||
|
|
||||||
} catch(Exception pe) {
|
|
||||||
Log.e("KONTAKT_CHAT_LISTENER_R", pe.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("KONTAKT_CHAT_LISTENER_L", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void CloseSocketListener() {
|
|
||||||
if(listen != null && listen.isAlive()) {
|
|
||||||
try {
|
|
||||||
socketListener.terminate();
|
|
||||||
listen.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class JsonSoapPrimitiveRequestListener implements RequestListener<SoapPrimitive> {
|
private final class JsonSoapPrimitiveRequestListener implements RequestListener<SoapPrimitive> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -299,18 +250,14 @@ public class KontaktChatActivity extends ListActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ContactListItem[] Klientel = lc.toArray(new ContactListItem[lc.size()]);
|
contactList = new ArrayList<>(lc);
|
||||||
|
|
||||||
setListAdapter(new KontaktAdapter(KontaktChatActivity.this, Klientel));
|
setListAdapter(new KontaktAdapter(KontaktChatActivity.this, contactList));
|
||||||
databaseHandler.close();
|
databaseHandler.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetNotify(String username, String message, int recipientOid){
|
public void SetNotify(String username, String message, int recipientOid){
|
||||||
|
|
||||||
//Convertiert in Pic Resource in ein Bitmap
|
|
||||||
//BitmapFactory.decodeResource(getResources(), R.drawable.bslogo)
|
|
||||||
|
|
||||||
NotificationCompat.Builder mBuilder =
|
NotificationCompat.Builder mBuilder =
|
||||||
new NotificationCompat.Builder(this)
|
new NotificationCompat.Builder(this)
|
||||||
.setSmallIcon(R.drawable.ic_stat_name)
|
.setSmallIcon(R.drawable.ic_stat_name)
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package beyondsoft.bewomitarbeiterapp;
|
|||||||
import android.animation.Animator;
|
import android.animation.Animator;
|
||||||
import android.animation.AnimatorListenerAdapter;
|
import android.animation.AnimatorListenerAdapter;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@@ -29,8 +27,6 @@ import com.android.volley.Response;
|
|||||||
import com.android.volley.VolleyError;
|
import com.android.volley.VolleyError;
|
||||||
import com.android.volley.toolbox.StringRequest;
|
import com.android.volley.toolbox.StringRequest;
|
||||||
import com.android.volley.toolbox.Volley;
|
import com.android.volley.toolbox.Volley;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.octo.android.robospice.SpiceManager;
|
import com.octo.android.robospice.SpiceManager;
|
||||||
import com.octo.android.robospice.UncachedSpiceService;
|
import com.octo.android.robospice.UncachedSpiceService;
|
||||||
import com.octo.android.robospice.persistence.exception.SpiceException;
|
import com.octo.android.robospice.persistence.exception.SpiceException;
|
||||||
@@ -41,9 +37,7 @@ import org.ksoap2.serialization.SoapPrimitive;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
@@ -57,6 +51,7 @@ import soapConnection.ApplicationUser;
|
|||||||
import soapConnection.JsonSoapPrimitiveRequest;
|
import soapConnection.JsonSoapPrimitiveRequest;
|
||||||
import soapConnection.SoapCalls;
|
import soapConnection.SoapCalls;
|
||||||
import soapConnection.SoapConnectionManager;
|
import soapConnection.SoapConnectionManager;
|
||||||
|
import tcpConnection.SocketManager;
|
||||||
import util.SecurityUtils;
|
import util.SecurityUtils;
|
||||||
import util.Util;
|
import util.Util;
|
||||||
|
|
||||||
@@ -158,20 +153,17 @@ public class LoginActivity extends ActionBarActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
|
|
||||||
super.onStart();
|
super.onStart();
|
||||||
spiceManager.start(this);
|
spiceManager.start(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStop() {
|
protected void onStop() {
|
||||||
|
|
||||||
spiceManager.shouldStop();
|
spiceManager.shouldStop();
|
||||||
super.onStop();
|
super.onStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void attemptLogin() {
|
private void attemptLogin() {
|
||||||
|
|
||||||
mTenantView.setError(null);
|
mTenantView.setError(null);
|
||||||
mChatCodeView.setError(null);
|
mChatCodeView.setError(null);
|
||||||
mUsernameView.setError(null);
|
mUsernameView.setError(null);
|
||||||
@@ -284,10 +276,6 @@ public class LoginActivity extends ActionBarActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson gson = new GsonBuilder().create();
|
|
||||||
|
|
||||||
//boolean loginResult = Boolean.valueOf(soapPrimitive.toString());
|
|
||||||
//Employee loggedInUser = gson.fromJson(soapPrimitive.toString(), Employee.class);
|
|
||||||
String abc = soapPrimitive.toString();
|
String abc = soapPrimitive.toString();
|
||||||
|
|
||||||
if(abc != null) {
|
if(abc != null) {
|
||||||
@@ -321,6 +309,11 @@ public class LoginActivity extends ActionBarActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SocketManager.tenant = mTenantView.getText().toString();
|
||||||
|
|
||||||
|
SocketManager.connectToServer();
|
||||||
|
SocketManager.LoginOnServer();
|
||||||
|
|
||||||
Intent mainActivityMitarbeiter = new Intent(LoginActivity.this, MainActivityMitarbeiter.class);
|
Intent mainActivityMitarbeiter = new Intent(LoginActivity.this, MainActivityMitarbeiter.class);
|
||||||
LoginActivity.this.startActivity(mainActivityMitarbeiter);
|
LoginActivity.this.startActivity(mainActivityMitarbeiter);
|
||||||
}
|
}
|
||||||
@@ -332,7 +325,6 @@ public class LoginActivity extends ActionBarActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void showLoginUIErrors(boolean token, boolean tenant, boolean username, boolean password) {
|
private void showLoginUIErrors(boolean token, boolean tenant, boolean username, boolean password) {
|
||||||
|
|
||||||
mChatCodeView.setError(token ? getString(R.string.error_invalid_input) : null);
|
mChatCodeView.setError(token ? getString(R.string.error_invalid_input) : null);
|
||||||
|
|
||||||
mTenantView.setError(tenant ? getString(R.string.error_invalid_input) : null);
|
mTenantView.setError(tenant ? getString(R.string.error_invalid_input) : null);
|
||||||
@@ -343,7 +335,6 @@ public class LoginActivity extends ActionBarActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void clearSharedPreferences() {
|
private void clearSharedPreferences() {
|
||||||
|
|
||||||
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
||||||
settings.edit().clear().commit();
|
settings.edit().clear().commit();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,35 +5,24 @@ import android.app.Notification;
|
|||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Base64;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import soapConnection.DataIdentifier;
|
import soapConnection.DataIdentifier;
|
||||||
import soapConnection.Packet;
|
import soapConnection.Packet;
|
||||||
import soapConnection.SoapCalls;
|
|
||||||
import soapConnection.SoapConnectionManager;
|
import soapConnection.SoapConnectionManager;
|
||||||
|
|
||||||
import util.SecurityUtils;
|
import tcpConnection.SocketManager;
|
||||||
import util.Util;
|
import tcpConnection.UIHandler;
|
||||||
|
|
||||||
|
|
||||||
public class MainActivityMitarbeiter extends Activity {
|
public class MainActivityMitarbeiter extends Activity {
|
||||||
@@ -42,26 +31,20 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
protected Button chatbutton;
|
protected Button chatbutton;
|
||||||
protected Button logoutbutton;
|
protected Button logoutbutton;
|
||||||
|
|
||||||
public static Socket socket;
|
|
||||||
DataOutputStream dataOutputStream;
|
|
||||||
|
|
||||||
//Name, der Zum Server gesendet wird
|
|
||||||
protected String loggedInUserName;
|
protected String loggedInUserName;
|
||||||
|
|
||||||
public static Thread listen;
|
private UIHandler mainUIHandler;
|
||||||
public static SocketListener socketListener;
|
|
||||||
|
|
||||||
int loggedInUserPersonOid;
|
int loggedInUserPersonOid;
|
||||||
|
|
||||||
private ArrayList<Integer> onlinePersonOids;
|
private ArrayList<Integer> onlinePersonOids;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main_activity_mitarbeiter);
|
setContentView(R.layout.activity_main_activity_mitarbeiter);
|
||||||
|
|
||||||
Log.i("MAIN_CREATE", "Kreiere...");
|
|
||||||
|
|
||||||
setzeButtonListener();
|
setzeButtonListener();
|
||||||
|
|
||||||
loggedInUserName = SoapConnectionManager.User.getFullName();
|
loggedInUserName = SoapConnectionManager.User.getFullName();
|
||||||
@@ -70,7 +53,24 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
|
|
||||||
onlinePersonOids = new ArrayList<>();
|
onlinePersonOids = new ArrayList<>();
|
||||||
|
|
||||||
StartServerConnection();
|
mainUIHandler = new UIHandler() {
|
||||||
|
@Override
|
||||||
|
public void updateUserInterface(Packet chatPacket) {
|
||||||
|
if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.MESSAGE)) {
|
||||||
|
Log.i("MAIN_ACTIVIY_LISTENER", "Empfange Nachricht... <<" + chatPacket.ChatMessage + ">>");
|
||||||
|
chatPacket.writeToLog();
|
||||||
|
SetNotify(chatPacket.ChatName, chatPacket.ChatMessage, chatPacket.SenderPersonOid);
|
||||||
|
} else if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.STATUS_NOTIFICATION_LOGIN_BROADCAST)) {
|
||||||
|
|
||||||
|
String[] bitch = chatPacket.ChatMessage.split(",");
|
||||||
|
onlinePersonOids = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String aBitch : bitch) {
|
||||||
|
onlinePersonOids.add(Integer.parseInt(aBitch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -111,11 +111,11 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
chatbutton.setOnClickListener(new View.OnClickListener() {
|
chatbutton.setOnClickListener(new View.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
|
||||||
if(socket == null || !socket.isConnected()) {
|
// if(socket == null || !socket.isConnected()) {
|
||||||
Toast.makeText(getApplicationContext(), "Der Chatserver ist zurzeit nicht erreichbar. \nBitte versuchen Sie es später erneut.", Toast.LENGTH_LONG).show();
|
// Toast.makeText(getApplicationContext(), "Der Chatserver ist zurzeit nicht erreichbar. \nBitte versuchen Sie es später erneut.", Toast.LENGTH_LONG).show();
|
||||||
|
//
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
|
|
||||||
@@ -133,17 +133,19 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
logoutbutton.setOnClickListener(new View.OnClickListener() {
|
logoutbutton.setOnClickListener(new View.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
|
||||||
if(socket != null && socket.isConnected()) {
|
// if(socket != null && socket.isConnected()) {
|
||||||
try {
|
// try {
|
||||||
Packet p = new Packet();
|
// Packet p = new Packet();
|
||||||
|
//
|
||||||
|
// p.ChatDataIdentifier = DataIdentifier.LOGOUT;
|
||||||
|
//
|
||||||
|
// new Thread(new MessageSender(p.getDataStream())).start();
|
||||||
|
// } catch(Exception e) {
|
||||||
|
// Log.e("SendAfterGetConnection", "Fehler beim Logout: " + e.getMessage());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
p.ChatDataIdentifier = DataIdentifier.LOGOUT;
|
SocketManager.Logout();
|
||||||
|
|
||||||
new Thread(new MessageSender(p.GetDataStream())).start();
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("SendAfterGetConnection", "Fehler beim Logout: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
@@ -151,182 +153,6 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartServerConnection(){
|
|
||||||
|
|
||||||
new Thread(new OpenConnection()).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class OpenConnection implements Runnable{
|
|
||||||
public void run(){
|
|
||||||
try {
|
|
||||||
socket = new Socket(SoapCalls.DESTINATION_ADDRESS, SoapCalls.DESTINATION_PORT);
|
|
||||||
|
|
||||||
SendAfterGetConnection();
|
|
||||||
|
|
||||||
dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
|
||||||
|
|
||||||
socketListener = new SocketListener();
|
|
||||||
listen = new Thread(socketListener);
|
|
||||||
listen.start();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e("Connection: ","Konnte nicht aufgebaut werden. Es wird erneut versucht. Nachricht: " + e.getMessage());
|
|
||||||
|
|
||||||
socket = null;
|
|
||||||
try {
|
|
||||||
new Timer().schedule(new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Log.i("Status", "Starte jetzt den Neu versuch");
|
|
||||||
StartServerConnection();
|
|
||||||
}
|
|
||||||
}, 120000); //----> Versuche beim Scheitern nach 2 Minuten eine Neue Verbindung aufzubauen
|
|
||||||
}catch(Exception s){
|
|
||||||
Log.e("Fehler", s.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void CloseSocketListener() {
|
|
||||||
if(listen != null && listen.isAlive()) {
|
|
||||||
try {
|
|
||||||
socketListener.terminate();
|
|
||||||
listen.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SocketListener implements Runnable {
|
|
||||||
|
|
||||||
Packet chatPacket;
|
|
||||||
|
|
||||||
private volatile boolean isRunning = true;
|
|
||||||
|
|
||||||
public void terminate() {
|
|
||||||
isRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
while (isRunning) {
|
|
||||||
if (socket != null) {
|
|
||||||
if (socket.isConnected()) {
|
|
||||||
|
|
||||||
InputStream stream = socket.getInputStream();
|
|
||||||
|
|
||||||
int size = stream.available();
|
|
||||||
|
|
||||||
if(size == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder total = new StringBuilder(stream.available());
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
while(counter < size) {
|
|
||||||
|
|
||||||
char c = (char) stream.read();
|
|
||||||
|
|
||||||
total.append(c);
|
|
||||||
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
chatPacket = new Packet(total.toString());
|
|
||||||
|
|
||||||
Log.i("MAIN_ACTIVIY_LISTENER", "Empfange Nachricht... " + chatPacket.ChatMessage);
|
|
||||||
|
|
||||||
if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.MESSAGE)) {
|
|
||||||
SetNotify(chatPacket.ChatName, chatPacket.ChatMessage, chatPacket.RecipientPersonOid);
|
|
||||||
} else if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.STATUS_NOTIFICATION_LOGIN_BROADCAST)) {
|
|
||||||
|
|
||||||
String[] bitch = chatPacket.ChatMessage.split(",");
|
|
||||||
onlinePersonOids = new ArrayList<>();
|
|
||||||
|
|
||||||
for (String aBitch : bitch) {
|
|
||||||
onlinePersonOids.add(Integer.parseInt(aBitch));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.i("MAIN_ACTIVITY", "Erhaltene Nachricht: " + chatPacket.ChatMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("Listener/Main", "" + e);
|
|
||||||
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ConnectionClose implements Runnable{
|
|
||||||
public void run(){
|
|
||||||
try {
|
|
||||||
if (dataOutputStream != null) {
|
|
||||||
dataOutputStream.flush();
|
|
||||||
dataOutputStream.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (socket != null) {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.i("MAIN CONNECTION CLOSE","Socket succesfully closed");
|
|
||||||
}catch(IOException se){
|
|
||||||
Log.e("MAIN ERROR:", se.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SendAfterGetConnection(){
|
|
||||||
|
|
||||||
if(socket.isConnected()) {
|
|
||||||
try {
|
|
||||||
Packet p = new Packet();
|
|
||||||
|
|
||||||
SharedPreferences settings = getSharedPreferences(Util.PREFS_NAME, 0);
|
|
||||||
|
|
||||||
byte[] tenantByteArray = Base64.decode(settings.getString(Util.PREFS_TENANT_KEY, null), Base64.DEFAULT);
|
|
||||||
ByteArrayInputStream tenantStream = new ByteArrayInputStream(tenantByteArray);
|
|
||||||
|
|
||||||
p.Tenant = SecurityUtils.decrypt(tenantStream).toString();
|
|
||||||
p.ChatDataIdentifier = DataIdentifier.LOGIN;
|
|
||||||
|
|
||||||
new Thread(new MessageSender(p.GetDataStream())).start();
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("SendAfterGetConnection", "Fehler beim Senden einer Nachricht an den Server: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MessageSender implements Runnable {
|
|
||||||
|
|
||||||
byte[] packetToSend;
|
|
||||||
|
|
||||||
public MessageSender(byte[] packet2Send) {
|
|
||||||
packetToSend = packet2Send;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
if(socket != null && dataOutputStream != null) {
|
|
||||||
|
|
||||||
dataOutputStream.write(packetToSend);
|
|
||||||
dataOutputStream.flush();
|
|
||||||
}
|
|
||||||
} catch(Exception e) {
|
|
||||||
Log.e("MessageSender", "Fehler beim Senden der TestMessage " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetNotify(String username, String message, int recipientOid){
|
public void SetNotify(String username, String message, int recipientOid){
|
||||||
|
|
||||||
//Convertiert in Pic Resource in ein Bitmap
|
//Convertiert in Pic Resource in ein Bitmap
|
||||||
@@ -364,38 +190,8 @@ public class MainActivityMitarbeiter extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
|
SocketManager.uiHandler = mainUIHandler;
|
||||||
KontaktChatActivity.CloseSocketListener();
|
|
||||||
ChatActivity.CloseSocketListener();
|
|
||||||
|
|
||||||
Log.i("ON_RESUME_MAIN", "socketListener ist null: " + (socketListener == null) + "; listen-thread ist null: " + (listen == null));
|
|
||||||
|
|
||||||
if(socketListener != null) {
|
|
||||||
Log.i("ON_RESUME_MAIN", "socketListener läuft: " + socketListener.isRunning);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(socket != null) {
|
|
||||||
Log.i("ON_RESUME_MAIN", "socket ist verbunden: " + socket.isConnected());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(socketListener != null && !socketListener.isRunning) {
|
|
||||||
socketListener.isRunning = true;
|
|
||||||
Log.i("ON_RESUME_MAIN", "socketListener läuft: " + socketListener.isRunning);
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onResume();
|
super.onResume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop(){
|
|
||||||
|
|
||||||
super.onStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onDestroy(){
|
|
||||||
Thread t = new Thread(new ConnectionClose());
|
|
||||||
t.start();
|
|
||||||
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ public class Packet {
|
|||||||
IsDelivered = Integer.parseInt(actualMessage.substring(chatMessageLength + chatNameLength + 19, chatMessageLength + chatNameLength + 20)) != 0;
|
IsDelivered = Integer.parseInt(actualMessage.substring(chatMessageLength + chatNameLength + 19, chatMessageLength + chatNameLength + 20)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] GetDataStream() {
|
public byte[] getDataStream() {
|
||||||
|
|
||||||
String dataStreamAsString = "" + SenderPersonOid + ";" + RecipientPersonOid + ";" + ChatName.length() + ";" + ChatMessage.length()+ ";" + ChatDataIdentifier.getValue() + ";" + ServerseitigeOid + ";" + ClientseitigeOid + ";" + Tenant + ";";
|
String dataStreamAsString = "" + SenderPersonOid + ";" + RecipientPersonOid + ";" + ChatName.length() + ";" + ChatMessage.length()+ ";" + ChatDataIdentifier.getValue() + ";" + ServerseitigeOid + ";" + ClientseitigeOid + ";" + Tenant + ";";
|
||||||
|
|
||||||
@@ -104,13 +104,13 @@ public class Packet {
|
|||||||
try {
|
try {
|
||||||
blaaah = dataStreamAsString.getBytes("UTF8");
|
blaaah = dataStreamAsString.getBytes("UTF8");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
Log.e("Packet.GetDataStream", e.getMessage());
|
Log.e("Packet.getDataStream", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return blaaah;
|
return blaaah;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteToLog() {
|
public void writeToLog() {
|
||||||
Log.i("PACKET_LOG", "SenderPersonOid: " + SenderPersonOid + "\nRecipientPersonOid: " + RecipientPersonOid + "\nChatDataIdentifier: " + ChatDataIdentifier.name() + "\nChatName: " + ChatName + "\nChatMessage: " + ChatMessage + "\nTenant: " + Tenant + "\nIsDelivered: " + IsDelivered + "\nMessageTimeStamp: " + MessageTimeStamp + "\nServerseitigeOid: " + ServerseitigeOid + "\nClientseitigeOid: " + ClientseitigeOid);
|
Log.i("PACKET_LOG", "SenderPersonOid: " + SenderPersonOid + "\nRecipientPersonOid: " + RecipientPersonOid + "\nChatDataIdentifier: " + ChatDataIdentifier.name() + "\nChatName: " + ChatName + "\nChatMessage: " + ChatMessage + "\nTenant: " + Tenant + "\nIsDelivered: " + IsDelivered + "\nMessageTimeStamp: " + MessageTimeStamp + "\nServerseitigeOid: " + ServerseitigeOid + "\nClientseitigeOid: " + ClientseitigeOid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,217 @@
|
|||||||
|
package tcpConnection;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import soapConnection.DataIdentifier;
|
||||||
|
import soapConnection.Packet;
|
||||||
|
import soapConnection.SoapCalls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by JettenM on 04.08.2016.
|
||||||
|
*/
|
||||||
|
public class SocketManager {
|
||||||
|
|
||||||
|
private static final String TAG = "SOCKET_MANAGER";
|
||||||
|
|
||||||
|
public static String tenant;
|
||||||
|
public static Socket socket;
|
||||||
|
public static Thread listenerThread;
|
||||||
|
public static SocketListener socketListener;
|
||||||
|
public static UIHandler uiHandler;
|
||||||
|
public static DataOutputStream dataOutputStream;
|
||||||
|
|
||||||
|
public static void connectToServer(){
|
||||||
|
new Thread(new OpenConnection()).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendMessage(Packet packet) {
|
||||||
|
new Thread(new MessageSender(packet.getDataStream())).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class OpenConnection implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
socket = new Socket(SoapCalls.DESTINATION_ADDRESS, SoapCalls.DESTINATION_PORT);
|
||||||
|
|
||||||
|
LoginOnServer();
|
||||||
|
|
||||||
|
dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
|
|
||||||
|
socketListener = new SocketListener();
|
||||||
|
listenerThread = new Thread(socketListener);
|
||||||
|
listenerThread.start();
|
||||||
|
} catch(Exception exception) {
|
||||||
|
socket = null;
|
||||||
|
try {
|
||||||
|
new Timer().schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
connectToServer();
|
||||||
|
}
|
||||||
|
}, 30000);
|
||||||
|
} catch(Exception exception2) {
|
||||||
|
exception2.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeSocketListener() {
|
||||||
|
if(listenerThread != null && listenerThread.isAlive()) {
|
||||||
|
try {
|
||||||
|
socketListener.terminate();
|
||||||
|
listenerThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Logout() {
|
||||||
|
if(socket != null && socket.isConnected()) {
|
||||||
|
try {
|
||||||
|
Packet packet = new Packet();
|
||||||
|
|
||||||
|
packet.ChatDataIdentifier = DataIdentifier.LOGOUT;
|
||||||
|
|
||||||
|
new Thread(new MessageSender(packet.getDataStream())).start();
|
||||||
|
|
||||||
|
} catch(Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LoginOnServer() {
|
||||||
|
if(socket != null && socket.isConnected()) {
|
||||||
|
try {
|
||||||
|
Packet packet = new Packet();
|
||||||
|
packet.Tenant = tenant;
|
||||||
|
|
||||||
|
packet.ChatDataIdentifier = DataIdentifier.LOGIN;
|
||||||
|
|
||||||
|
new Thread(new MessageSender(packet.getDataStream())).start();
|
||||||
|
|
||||||
|
} catch(Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ConnectionClose implements Runnable{
|
||||||
|
public void run(){
|
||||||
|
try {
|
||||||
|
if (dataOutputStream != null) {
|
||||||
|
dataOutputStream.flush();
|
||||||
|
dataOutputStream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (socket != null) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}catch(IOException se){
|
||||||
|
se.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MessageSender implements Runnable {
|
||||||
|
byte[] packetToSend;
|
||||||
|
|
||||||
|
public MessageSender(byte[] packet2Send) {
|
||||||
|
packetToSend = packet2Send;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if(!socket.isConnected()) {
|
||||||
|
Log.e("MESSAGE_SENDER", "Socket ist nicht verbunden");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(socket != null && dataOutputStream != null) {
|
||||||
|
try {
|
||||||
|
dataOutputStream.write(packetToSend);
|
||||||
|
dataOutputStream.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SocketListener implements Runnable {
|
||||||
|
|
||||||
|
Packet chatPacket;
|
||||||
|
|
||||||
|
private volatile boolean isRunning = true;
|
||||||
|
|
||||||
|
public void terminate() {
|
||||||
|
isRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (isRunning) {
|
||||||
|
if (socket != null) {
|
||||||
|
if (socket.isConnected()) {
|
||||||
|
|
||||||
|
InputStream stream = socket.getInputStream();
|
||||||
|
|
||||||
|
int size = stream.available();
|
||||||
|
|
||||||
|
if(size == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder total = new StringBuilder(stream.available());
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
byte[] message = new byte[size];
|
||||||
|
|
||||||
|
stream.read(message, 0, size);
|
||||||
|
|
||||||
|
// while(counter < size) {
|
||||||
|
//
|
||||||
|
// char c = (char) stream.read();
|
||||||
|
//
|
||||||
|
// total.append(c);
|
||||||
|
//
|
||||||
|
// counter++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
try {
|
||||||
|
//chatPacket = new Packet(total.toString());
|
||||||
|
chatPacket = new Packet(new String(message, "UTF-8"));
|
||||||
|
|
||||||
|
Log.i(TAG, "Empfange Nachricht... " + chatPacket.ChatMessage);
|
||||||
|
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(uiHandler != null) {
|
||||||
|
uiHandler.updateUserInterface(chatPacket);
|
||||||
|
} else {
|
||||||
|
Log.e("ERROR_SOCKETLISTENER", "uiHandler ist null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package tcpConnection;
|
||||||
|
|
||||||
|
import soapConnection.Packet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by JettenM on 04.08.2016.
|
||||||
|
*/
|
||||||
|
public interface UIHandler {
|
||||||
|
void updateUserInterface(Packet chatPacket);
|
||||||
|
}
|
||||||
@@ -201,6 +201,8 @@ namespace BeWoChatServer
|
|||||||
{
|
{
|
||||||
recipient.Key.Send(dataPacket.GetDataStream());
|
recipient.Key.Send(dataPacket.GetDataStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Oid zurückschicken
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user