use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.
the class MobiComConversationService method getMessages.
public synchronized List<Message> getMessages(Long startTime, Long endTime, Contact contact, Channel channel, Integer conversationId, boolean isSkipRead) {
List<Message> messageList = new ArrayList<Message>();
List<Message> cachedMessageList = messageDatabaseService.getMessages(startTime, endTime, contact, channel, conversationId);
boolean isServerCallNotRequired = false;
if (channel != null) {
Channel newChannel = ChannelService.getInstance(context).getChannelByChannelKey(channel.getKey());
isServerCallNotRequired = (newChannel != null && !Channel.GroupType.OPEN.getValue().equals(newChannel.getType()));
} else if (contact != null) {
isServerCallNotRequired = true;
}
if (isServerCallNotRequired && (!cachedMessageList.isEmpty() && (cachedMessageList.size() > 1 || wasServerCallDoneBefore(contact, channel, conversationId)) || (contact == null && channel == null && cachedMessageList.isEmpty() && wasServerCallDoneBefore(contact, channel, conversationId)))) {
Utils.printLog(context, TAG, "cachedMessageList size is : " + cachedMessageList.size());
return cachedMessageList;
}
String data;
try {
data = messageClientService.getMessages(contact, channel, startTime, endTime, conversationId, isSkipRead);
Utils.printLog(context, TAG, "Received response from server for Messages: " + data);
} catch (Exception ex) {
ex.printStackTrace();
return cachedMessageList;
}
if (data == null || TextUtils.isEmpty(data) || data.equals("UnAuthorized Access") || !data.contains("{")) {
// Note: currently not supporting syncing old channel messages from server
if (channel != null && channel.getKey() != null) {
return cachedMessageList;
}
return cachedMessageList;
}
updateServerCallDoneStatus(contact, channel, conversationId);
try {
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).setExclusionStrategies(new AnnotationExclusionStrategy()).create();
JsonParser parser = new JsonParser();
JSONObject jsonObject = new JSONObject(data);
String channelFeedResponse = "";
String conversationPxyResponse = "";
String element = parser.parse(data).getAsJsonObject().get("message").toString();
String userDetailsElement = parser.parse(data).getAsJsonObject().get("userDetails").toString();
if (!TextUtils.isEmpty(userDetailsElement)) {
UserDetail[] userDetails = (UserDetail[]) GsonUtils.getObjectFromJson(userDetailsElement, UserDetail[].class);
processUserDetails(userDetails);
}
if (jsonObject.has("groupFeeds")) {
channelFeedResponse = parser.parse(data).getAsJsonObject().get("groupFeeds").toString();
ChannelFeed[] channelFeeds = (ChannelFeed[]) GsonUtils.getObjectFromJson(channelFeedResponse, ChannelFeed[].class);
ChannelService.getInstance(context).processChannelFeedList(channelFeeds, false);
if (channel != null && !isServerCallNotRequired) {
BroadcastService.sendUpdate(context, BroadcastService.INTENT_ACTIONS.UPDATE_TITLE_SUBTITLE.toString());
}
}
if (jsonObject.has("conversationPxys")) {
conversationPxyResponse = parser.parse(data).getAsJsonObject().get("conversationPxys").toString();
Conversation[] conversationPxy = (Conversation[]) GsonUtils.getObjectFromJson(conversationPxyResponse, Conversation[].class);
ConversationService.getInstance(context).processConversationArray(conversationPxy, channel, contact);
}
Message[] messages = gson.fromJson(element, Message[].class);
MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
if (messages != null && messages.length > 0 && cachedMessageList.size() > 0 && cachedMessageList.get(0).isLocalMessage()) {
if (cachedMessageList.get(0).equals(messages[0])) {
Utils.printLog(context, TAG, "Both messages are same.");
deleteMessage(cachedMessageList.get(0));
}
}
for (Message message : messages) {
if (!message.isCall() || userPreferences.isDisplayCallRecordEnable()) {
// we have to figure out if it is a parsing problem or response from server.
if (message.getTo() == null) {
continue;
}
if (message.hasAttachment() && !(message.getContentType() == Message.ContentType.TEXT_URL.getValue())) {
setFilePathifExist(message);
}
if (message.getContentType() == Message.ContentType.CONTACT_MSG.getValue()) {
FileClientService fileClientService = new FileClientService(context);
fileClientService.loadContactsvCard(message);
}
if (Message.MetaDataType.HIDDEN.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
continue;
}
if (messageDatabaseService.isMessagePresent(message.getKeyString(), Message.ReplyMessage.HIDE_MESSAGE.getValue())) {
messageDatabaseService.updateMessageReplyType(message.getKeyString(), Message.ReplyMessage.NON_HIDDEN.getValue());
} else {
if (isServerCallNotRequired || contact == null && channel == null) {
messageDatabaseService.createMessage(message);
}
}
if (contact == null && channel == null) {
if (message.isHidden()) {
if (message.getGroupId() != null) {
Channel newChannel = ChannelService.getInstance(context).getChannelByChannelKey(message.getGroupId());
if (newChannel != null) {
getMessages(null, null, null, newChannel, null, true);
}
} else {
getMessages(null, null, new Contact(message.getContactIds()), null, null, true);
}
}
}
}
if (!isServerCallNotRequired) {
messageList.add(message);
}
}
if (contact == null && channel == null) {
Intent intent = new Intent(MobiComKitConstants.APPLOZIC_UNREAD_COUNT);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
/* messageList.removeAll(cachedMessageList);
messageList.addAll(cachedMessageList);
Collections.sort(messageList, new Comparator<Message>() {
@Override
public int compare(Message lhs, Message rhs) {
return lhs.getCreatedAtTime().compareTo(rhs.getCreatedAtTime());
}
});*/
List<Message> finalMessageList = messageDatabaseService.getMessages(startTime, endTime, contact, channel, conversationId);
List<String> messageKeys = new ArrayList<>();
for (Message msg : finalMessageList) {
if (msg.getTo() == null) {
continue;
}
if (Message.MetaDataType.HIDDEN.getValue().equals(msg.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(msg.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
continue;
}
if (msg.getMetadata() != null && msg.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()) != null && !messageDatabaseService.isMessagePresent(msg.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()))) {
messageKeys.add(msg.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()));
}
}
if (messageKeys != null && messageKeys.size() > 0) {
Message[] replyMessageList = getMessageListByKeyList(messageKeys);
if (replyMessageList != null) {
for (Message replyMessage : replyMessageList) {
if (replyMessage.getTo() == null) {
continue;
}
if (Message.MetaDataType.HIDDEN.getValue().equals(replyMessage.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(replyMessage.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
continue;
}
if (replyMessage.hasAttachment() && !(replyMessage.getContentType() == Message.ContentType.TEXT_URL.getValue())) {
setFilePathifExist(replyMessage);
}
if (replyMessage.getContentType() == Message.ContentType.CONTACT_MSG.getValue()) {
FileClientService fileClientService = new FileClientService(context);
fileClientService.loadContactsvCard(replyMessage);
}
replyMessage.setReplyMessage(Message.ReplyMessage.HIDE_MESSAGE.getValue());
if (isServerCallNotRequired || contact == null && channel == null) {
messageDatabaseService.createMessage(replyMessage);
}
}
}
}
if (messageList != null && !messageList.isEmpty()) {
Collections.sort(messageList, new Comparator<Message>() {
@Override
public int compare(Message lhs, Message rhs) {
return lhs.getCreatedAtTime().compareTo(rhs.getCreatedAtTime());
}
});
}
return channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType()) ? messageList : finalMessageList;
}
use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.
the class MobiComMessageService method addMTMessage.
public Contact addMTMessage(Message message) {
MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
Contact receiverContact = null;
message.processContactIds(context);
String currentId = message.getCurrentId();
if (message.getGroupId() == null) {
receiverContact = baseContactService.getContactById(message.getContactIds());
}
if (message.getMessage() != null && PersonalizedMessage.isPersonalized(message.getMessage())) {
message.setMessage(PersonalizedMessage.prepareMessageFromTemplate(message.getMessage(), receiverContact));
}
messageDatabaseService.createMessage(message);
// Check if we are........container is already opened...don't send broadcast
boolean isContainerOpened;
if (message.getConversationId() != null && BroadcastService.isContextBasedChatEnabled()) {
if (BroadcastService.currentConversationId == null) {
BroadcastService.currentConversationId = message.getConversationId();
}
isContainerOpened = (currentId.equals(BroadcastService.currentUserId) && message.getConversationId().equals(BroadcastService.currentConversationId));
} else {
isContainerOpened = currentId.equals(BroadcastService.currentUserId);
}
if (message.isVideoNotificationMessage()) {
Utils.printLog(context, TAG, "Got notifications for Video call...");
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
VideoCallNotificationHelper helper = new VideoCallNotificationHelper(context);
helper.handleVideoCallNotificationMessages(message);
} else if (message.isVideoCallMessage()) {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
VideoCallNotificationHelper.buildVideoCallNotification(context, message);
} else if (!isContainerOpened) {
if (message.isConsideredForCount()) {
if (message.getTo() != null && message.getGroupId() == null) {
messageDatabaseService.updateContactUnreadCount(message.getTo());
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
Contact contact = new ContactDatabase(context).getContactById(message.getTo());
if (contact != null && !contact.isNotificationMuted()) {
sendNotification(message);
}
}
if (message.getGroupId() != null && !Message.GroupMessageMetaData.FALSE.getValue().equals(message.getMetaDataValueForKey(Message.GroupMessageMetaData.KEY.getValue()))) {
if (!Message.ContentType.CHANNEL_CUSTOM_MESSAGE.getValue().equals(message.getContentType())) {
messageDatabaseService.updateChannelUnreadCount(message.getGroupId());
}
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
Channel currentChannel = ChannelService.getInstance(context).getChannelInfo(message.getGroupId());
if (currentChannel != null && !currentChannel.isNotificationMuted()) {
sendNotification(message);
}
}
MobiComUserPreference.getInstance(context).setNewMessageFlag(true);
} else {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
}
} else {
BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
}
Utils.printLog(context, TAG, "Updating delivery status: " + message.getPairedMessageKeyString() + ", " + userPreferences.getUserId() + ", " + userPreferences.getContactNumber());
messageClientService.updateDeliveryStatus(message.getPairedMessageKeyString(), userPreferences.getUserId(), userPreferences.getContactNumber());
return receiverContact;
}
use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.
the class MobiComMessageService method processContactFromMessages.
public void processContactFromMessages(List<Message> messages) {
try {
if (!ApplozicClient.getInstance(context).isHandleDisplayName()) {
return;
}
Set<String> userIds = new HashSet<String>();
for (Message msg : messages) {
if (!baseContactService.isContactExists(msg.getContactIds())) {
userIds.add(msg.getContactIds());
}
}
if (userIds.isEmpty()) {
return;
}
try {
Map<String, String> userIdsHashMap = new UserClientService(context).getUserInfo(userIds);
for (Map.Entry<String, String> keyValue : userIdsHashMap.entrySet()) {
Contact contact = new Contact();
contact.setUserId(keyValue.getKey());
contact.setFullName(keyValue.getValue());
contact.setUnreadCount(0);
baseContactService.upsert(contact);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (Exception ex) {
}
}
use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.
the class UserService method updateDisplayNameORImageLink.
public String updateDisplayNameORImageLink(String displayName, String profileImageLink, String localURL, String status, String contactNumber) {
ApiResponse response = userClientService.updateDisplayNameORImageLink(displayName, profileImageLink, status, contactNumber);
if (response == null) {
return null;
}
if (response != null && response.isSuccess()) {
Contact contact = baseContactService.getContactById(MobiComUserPreference.getInstance(context).getUserId());
if (!TextUtils.isEmpty(displayName)) {
contact.setFullName(displayName);
}
if (!TextUtils.isEmpty(profileImageLink)) {
contact.setImageURL(profileImageLink);
}
contact.setLocalImageUrl(localURL);
if (!TextUtils.isEmpty(status)) {
contact.setStatus(status);
}
if (!TextUtils.isEmpty(contactNumber)) {
contact.setContactNumber(contactNumber);
}
baseContactService.upsert(contact);
Contact contact1 = baseContactService.getContactById(MobiComUserPreference.getInstance(context).getUserId());
Utils.printLog(context, "UserService", contact1.getImageURL() + ", " + contact1.getDisplayName() + "," + contact1.getStatus() + "," + contact1.getStatus());
}
return response.getStatus();
}
use of com.applozic.mobicommons.people.contact.Contact in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttIntentService method onHandleWork.
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (intent == null) {
return;
}
boolean subscribe = intent.getBooleanExtra(SUBSCRIBE, false);
if (subscribe) {
ApplozicMqttService.getInstance(getApplicationContext()).subscribe();
}
Contact contact = (Contact) intent.getSerializableExtra(CONTACT);
Channel channel = (Channel) intent.getSerializableExtra(CHANNEL);
boolean subscribeToTyping = intent.getBooleanExtra(SUBSCRIBE_TO_TYPING, false);
if (subscribeToTyping) {
ApplozicMqttService.getInstance(getApplicationContext()).subscribeToTypingTopic(channel);
if (channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType())) {
ApplozicMqttService.getInstance(getApplicationContext()).subscribeToOpenGroupTopic(channel);
}
return;
}
boolean unSubscribeToTyping = intent.getBooleanExtra(UN_SUBSCRIBE_TO_TYPING, false);
if (unSubscribeToTyping) {
ApplozicMqttService.getInstance(getApplicationContext()).unSubscribeToTypingTopic(channel);
if (channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType())) {
ApplozicMqttService.getInstance(getApplicationContext()).unSubscribeToOpenGroupTopic(channel);
}
return;
}
String userKeyString = intent.getStringExtra(USER_KEY_STRING);
String deviceKeyString = intent.getStringExtra(DEVICE_KEY_STRING);
if (!TextUtils.isEmpty(userKeyString) && !TextUtils.isEmpty(deviceKeyString)) {
ApplozicMqttService.getInstance(getApplicationContext()).disconnectPublish(userKeyString, deviceKeyString, "0");
}
boolean connectedStatus = intent.getBooleanExtra(CONNECTED_PUBLISH, false);
if (connectedStatus) {
ApplozicMqttService.getInstance(getApplicationContext()).connectPublish(MobiComUserPreference.getInstance(getApplicationContext()).getSuUserKeyString(), MobiComUserPreference.getInstance(getApplicationContext()).getDeviceKeyString(), "1");
}
if (contact != null) {
boolean stop = intent.getBooleanExtra(STOP_TYPING, false);
if (stop) {
ApplozicMqttService.getInstance(getApplicationContext()).typingStopped(contact, null);
}
}
if (contact != null && (contact.isBlocked() || contact.isBlockedBy())) {
return;
}
if (contact != null || channel != null) {
boolean typing = intent.getBooleanExtra(TYPING, false);
if (typing) {
ApplozicMqttService.getInstance(getApplicationContext()).typingStarted(contact, channel);
} else {
ApplozicMqttService.getInstance(getApplicationContext()).typingStopped(contact, channel);
}
}
}
Aggregations