287 lines
11 KiB
Java
287 lines
11 KiB
Java
package util;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.app.Notification;
|
|
import android.app.NotificationManager;
|
|
import android.app.PendingIntent;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.os.Bundle;
|
|
import android.support.v4.app.NotificationCompat;
|
|
import android.util.Log;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Calendar;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Random;
|
|
|
|
import Database.DatabaseHandler;
|
|
import beyondsoft.bewomitarbeiterapp.ChatActivity;
|
|
import beyondsoft.bewomitarbeiterapp.KontaktChatActivity;
|
|
import beyondsoft.bewomitarbeiterapp.R;
|
|
import entities.ChatEntity;
|
|
import entities.ChatMessage;
|
|
import entities.NotificationLog;
|
|
import soapConnection.DataIdentifier;
|
|
import soapConnection.Packet;
|
|
import soapConnection.SoapConnectionManager;
|
|
import tcpConnection.LoginCallback;
|
|
import tcpConnection.SocketManager;
|
|
|
|
/**
|
|
* Created by JettenM on 05.07.2016.
|
|
*/
|
|
public class Util {
|
|
public static final String PREFS_NAME = "BeWoMitarbeiterPrefsFile";
|
|
|
|
public static final String PREFS_SERVERADDRESS_KEY = "serverAddress";
|
|
|
|
public static final String PREFS_TOKEN_KEY = "serverAddress";
|
|
public static final String PREFS_TENANT_KEY = "tenant";
|
|
public static final String PREFS_USERNAME_KEY = "username";
|
|
public static final String PREFS_PASSWORD = "password";
|
|
|
|
public static final String NOTIFICATION_CANCELLED = "notification_cancelled";
|
|
|
|
public static int conversationsCount = 0;
|
|
|
|
public static HashMap<ChatEntity, Integer> entity2MessagesCountRel = new HashMap<>();
|
|
|
|
//TODO: Notifications zählen. Bei Nachrichten verschiedener Unterhaltungen nur noch eine kleine Notification anzeigen mit "Sie haben x Nachrichten in y Unterhaltungen
|
|
public static void SetNotify(String username, String message, int recipientOid, boolean isTeam, String teamMemberOids, String teamMemberNames, boolean isEmployee, Context packageContext) {
|
|
int unreadMessagesCount = entity2MessagesCountRel.size() + 1;
|
|
//TODO: unterscheiden, ob es sich um eine einzelne Benachrichtigung handelt
|
|
|
|
if(SocketManager.notificationEntities.keySet().size() >= 1) {
|
|
conversationsCount = SocketManager.notificationEntities.keySet().size();
|
|
}
|
|
|
|
String mUsername = conversationsCount > 1 ? String.valueOf(conversationsCount) + " Konversationen" : username;
|
|
String mMessage = unreadMessagesCount > 1 ? String.valueOf(unreadMessagesCount) + " Nachrichten" : message;
|
|
|
|
NotificationCompat.Builder mBuilder =
|
|
new NotificationCompat.Builder(packageContext)
|
|
.setSmallIcon(R.drawable.ic_stat_name)
|
|
.setContentTitle(mUsername)
|
|
.setContentText(mMessage)
|
|
.setAutoCancel(true)
|
|
.setDefaults(Notification.DEFAULT_SOUND)
|
|
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) //Notification.Default_Vibrate
|
|
.setLights(Color.YELLOW, 3000, 3000); //Notification.Default_Lights
|
|
|
|
Intent resultIntent = new Intent(packageContext, (conversationsCount > 1 ? KontaktChatActivity.class : ChatActivity.class));
|
|
|
|
if (conversationsCount <= 1) {
|
|
Bundle bundle = new Bundle();
|
|
bundle.putString("Name", username);
|
|
bundle.putInt("RecipientOid", recipientOid);
|
|
bundle.putBoolean("IsTeam", isTeam);
|
|
bundle.putString("TeamMemberOids", teamMemberOids);
|
|
bundle.putString("TeamMemberNames", teamMemberNames);
|
|
bundle.putBoolean("IsEmployee", isEmployee);
|
|
resultIntent.putExtras(bundle);
|
|
}
|
|
|
|
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
|
PendingIntent resultPendingIntent = PendingIntent.getActivity(packageContext.getApplicationContext(), (int) (Math.random() * 100), resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationManager mNotifyMgr = (NotificationManager) packageContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
mBuilder.setDeleteIntent(getDeletedIntent(packageContext, recipientOid, isTeam));
|
|
|
|
mBuilder.setContentIntent(resultPendingIntent);
|
|
|
|
Random random = new Random();
|
|
int notificationId = 1;
|
|
ChatEntity chatEntity = new ChatEntity(recipientOid, isTeam, isEmployee);
|
|
|
|
|
|
|
|
// TODO: in entity2MessagesCountRels einfügen und Anzahl der Nachrichten aktualisieren
|
|
updateMessagesCount(chatEntity, 1);
|
|
|
|
|
|
NotificationLog notificationLog = new NotificationLog(notificationId, Calendar.getInstance().getTime());
|
|
|
|
while(SocketManager.notificationEntities.containsValue(notificationLog)) {
|
|
notificationId = random.nextInt(10000);
|
|
notificationLog = new NotificationLog(notificationId, Calendar.getInstance().getTime());
|
|
}
|
|
|
|
SocketManager.notificationEntities.put(chatEntity, notificationLog);
|
|
|
|
ArrayList<ChatEntity> entities2remove = new ArrayList<>();
|
|
|
|
if(conversationsCount > 1 || unreadMessagesCount > 1) {
|
|
for(Map.Entry<ChatEntity, NotificationLog> entry : SocketManager.notificationEntities.entrySet()) {
|
|
if(!(entry.getKey().equals(chatEntity) && entry.getValue().equals(notificationLog))) {
|
|
mNotifyMgr.cancel(entry.getValue().getNotificationId());
|
|
entities2remove.add(entry.getKey());
|
|
}
|
|
}
|
|
}
|
|
|
|
for(ChatEntity entry : entities2remove) {
|
|
SocketManager.notificationEntities.remove(entry);
|
|
}
|
|
|
|
mNotifyMgr.notify(notificationId, mBuilder.build());
|
|
}
|
|
|
|
protected static PendingIntent getDeletedIntent(Context context, int recipientOid, boolean isTeam) {
|
|
Intent intent = new Intent(context, NotificationBroadcastReceiver.class);
|
|
intent.setAction(Util.NOTIFICATION_CANCELLED);
|
|
|
|
Bundle extras = new Bundle();
|
|
extras.putInt("RecipientOid", recipientOid);
|
|
extras.putBoolean("IsTeam", isTeam);
|
|
intent.putExtras(extras);
|
|
|
|
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
}
|
|
|
|
public static void buildAlert(Context context, String message) {
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(context);
|
|
alert.setMessage(message);
|
|
alert.setTitle("Debug-Info");
|
|
alert.setCancelable(true);
|
|
|
|
alert.setPositiveButton("OK",
|
|
new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
});
|
|
|
|
alert.create().show();
|
|
}
|
|
|
|
public static void reconnectWithServer(boolean isConnected, final Context context) {
|
|
Log.e("UTIL", isConnected ? "Ist mit dem Internet/WLAN verbunden" : "Ist mit keinem Netzwerk verbunden");
|
|
|
|
if(isConnected) {
|
|
try {
|
|
SocketManager.loginCallback = new LoginCallback() {
|
|
@Override
|
|
public void doCallback() {
|
|
Log.e("LOGIN_CALLBACK", "LoginCallback aufgerufen. Verschicke nicht verschickte Nachrichten...");
|
|
|
|
sendUnsentMessages(context);
|
|
|
|
sendStatusRequest();
|
|
|
|
// TODO: Nachrichten herunterladen, sofern man sich in der ChatActivity befindet
|
|
|
|
}
|
|
};
|
|
|
|
SocketManager.connectToServer();
|
|
|
|
Log.i("RECONNECT_WITH_SERVER", "connectToServer aufgerufen...");
|
|
}
|
|
catch(Exception es) {
|
|
Log.e("RECONNECT_WITH_SERVER", "Ausnahme beim Login nach Reconnect");
|
|
es.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void sendUnsentMessages(final Context context) {
|
|
Thread thread = new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
DatabaseHandler databaseHandler = DatabaseHandler.getInstance(context);
|
|
|
|
ArrayList<ChatMessage> unsentMessages = databaseHandler.getUnsentChatMessages();
|
|
|
|
for(ChatMessage cm : unsentMessages) {
|
|
final Packet unsentPacket = new Packet();
|
|
|
|
unsentPacket.ChatDataIdentifier = cm.IsTeam ? DataIdentifier.TEAM : DataIdentifier.MESSAGE;
|
|
unsentPacket.ChatMessage = cm.ChatText;
|
|
unsentPacket.ChatName = SoapConnectionManager.User.getFullName();
|
|
unsentPacket.RecipientPersonOid = cm.RecipientPersonOid;
|
|
unsentPacket.ClientseitigeOid = cm.Id;
|
|
unsentPacket.MessageTimeStamp = cm.InsTs;
|
|
unsentPacket.SenderPersonOid = cm.SenderPersonOid;
|
|
unsentPacket.Tenant = SoapConnectionManager.User.getTenant();
|
|
unsentPacket.IsDelivered = cm.IsDelivered;
|
|
|
|
SocketManager.sendMessage(unsentPacket);
|
|
|
|
Thread.sleep(50);
|
|
}
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
|
|
thread.start();
|
|
}
|
|
|
|
public static void sendStatusRequest() {
|
|
final Packet requestPacket = new Packet();
|
|
|
|
requestPacket.ChatDataIdentifier = DataIdentifier.STATUS_REQUEST;
|
|
requestPacket.ChatName = SoapConnectionManager.User.getFullName();
|
|
requestPacket.SenderPersonOid = SoapConnectionManager.User.getPersonOid().intValue();
|
|
requestPacket.Tenant = SoapConnectionManager.User.getTenant();
|
|
|
|
SocketManager.sendMessage(requestPacket);
|
|
}
|
|
|
|
public static ChatEntity getChatEntityFromOnlineEntitiesByOid(int personOid, boolean isTeam) {
|
|
for(ChatEntity item : SocketManager.onlineEntities) {
|
|
if(item.getOid() == personOid && item.getIsTeam() == isTeam) {
|
|
return item;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static void updateMessagesCount(ChatEntity entity, int value2add) {
|
|
int newValue = value2add;
|
|
|
|
if(entity2MessagesCountRel.containsKey(entity)) {
|
|
newValue = entity2MessagesCountRel.get(entity) + value2add;
|
|
}
|
|
|
|
entity2MessagesCountRel.put(entity, newValue);
|
|
}
|
|
|
|
public static void removeMessagesCount(ChatEntity entity) {
|
|
if(entity2MessagesCountRel.containsKey(entity)) {
|
|
entity2MessagesCountRel.remove(entity);
|
|
}
|
|
}
|
|
|
|
public static Integer[] convertStringArrayToIntegerArray(String[] arrayToConvert) {
|
|
Integer[] result = new Integer[arrayToConvert.length];
|
|
|
|
try {
|
|
for(int i = 0; i < arrayToConvert.length; i++) {
|
|
result[i] = Integer.parseInt(arrayToConvert[i]);
|
|
}
|
|
} catch(NumberFormatException nfe) {
|
|
Log.e("UTIL", nfe.getMessage());
|
|
nfe.printStackTrace();
|
|
|
|
return null;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static boolean contains(final Integer[] array, final int key) {
|
|
return Arrays.asList(array).contains(key);
|
|
}
|
|
}
|