Search in sources :

Example 16 with Channel

use of com.applozic.mobicommons.people.channel.Channel in project Applozic-Android-SDK by AppLozic.

the class ChannelService method getChannelInfo.

/**
 * Gets a channel object for the given client group id.
 *
 * <p>Channel is initially retrieved locally. If it's not found
 * then a server call is done and the channel is synced locally and returned.</p>
 *
 * @param clientGroupId the client group id
 * @return the channel object
 */
@Nullable
public Channel getChannelInfo(@Nullable String clientGroupId) {
    if (TextUtils.isEmpty(clientGroupId)) {
        return null;
    }
    Channel channel = channelDatabaseService.getChannelByClientGroupId(clientGroupId);
    if (channel == null) {
        ChannelFeed channelFeed = channelClientService.getChannelInfo(clientGroupId);
        if (channelFeed != null) {
            channelFeed.setUnreadCount(0);
            ChannelFeed[] channelFeeds = new ChannelFeed[1];
            channelFeeds[0] = channelFeed;
            processChannelFeedList(channelFeeds, false);
            channel = getChannel(channelFeed);
            return channel;
        }
    }
    return channel;
}
Also used : Channel(com.applozic.mobicommons.people.channel.Channel) ChannelFeed(com.applozic.mobicomkit.feed.ChannelFeed) SyncChannelFeed(com.applozic.mobicomkit.sync.SyncChannelFeed) Nullable(androidx.annotation.Nullable)

Example 17 with Channel

use of com.applozic.mobicommons.people.channel.Channel in project Applozic-Android-SDK by AppLozic.

the class MessageClientService method processMessage.

// Cleanup: private
public void processMessage(Message message, Handler handler, String userDisplayName) throws Exception {
    boolean isBroadcast = (message.getMessageId() == null);
    MobiComUserPreference userPreferences = MobiComUserPreference.getInstance(context);
    message.setSent(Boolean.TRUE);
    message.setSendToDevice(Boolean.FALSE);
    message.setSuUserKeyString(userPreferences.getSuUserKeyString());
    message.processContactIds(context);
    Contact contact = null;
    Channel channel = null;
    boolean isBroadcastOneByOneGroupType = false;
    boolean isOpenGroup = false;
    boolean skipMessage = false;
    if (message.getGroupId() == null) {
        contact = baseContactService.getContactById(message.getContactIds());
    } else {
        channel = ChannelService.getInstance(context).getChannel(message.getGroupId());
        isOpenGroup = (Channel.GroupType.OPEN.getValue().equals(channel.getType()) && !message.hasAttachment());
        isBroadcastOneByOneGroupType = Channel.GroupType.BROADCAST_ONE_BY_ONE.getValue().equals(channel.getType());
    }
    long messageId = -1;
    List<String> fileKeys = new ArrayList<String>();
    String keyString = null;
    String oldMessageKey = null;
    if (!isBroadcastOneByOneGroupType) {
        keyString = UUID.randomUUID().toString();
        oldMessageKey = keyString;
        message.setKeyString(keyString);
        message.setSentToServer(false);
    } else {
        message.setSentToServer(true);
    }
    if (Message.MetaDataType.HIDDEN.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue())) || Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(message.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
        skipMessage = true;
    }
    if (!skipMessage && !isOpenGroup) {
        messageId = messageDatabaseService.createMessage(message);
    }
    if (isBroadcast && !skipMessage) {
        BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
    }
    if (!isBroadcastOneByOneGroupType && message.isUploadRequired()) {
        for (String filePath : message.getFilePaths()) {
            FileMeta thumbnailFileMeta = null;
            String mimeType = FileUtils.getMimeType(filePath);
            if (mimeType != null && mimeType.startsWith("video")) {
                thumbnailFileMeta = uploadVideoThumbnail(filePath, message.getCreatedAtTime(), oldMessageKey);
            }
            try {
                String fileMetaResponse = new FileClientService(context).uploadBlobImage(filePath, handler, oldMessageKey);
                if (fileMetaResponse == null) {
                    if (skipMessage) {
                        return;
                    }
                    if (handler != null) {
                        android.os.Message msg = handler.obtainMessage();
                        msg.what = MobiComConversationService.UPLOAD_COMPLETED;
                        msg.getData().putString("error", "Error while uploading");
                        msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                        msg.sendToTarget();
                    }
                    if (!message.isContactMessage()) {
                        messageDatabaseService.updateCanceledFlag(messageId, 1);
                    }
                    BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
                    return;
                }
                if (ApplozicClient.getInstance(context).isS3StorageServiceEnabled()) {
                    if (!TextUtils.isEmpty(fileMetaResponse)) {
                        message.setFileMetas((FileMeta) GsonUtils.getObjectFromJson(fileMetaResponse, FileMeta.class));
                        if (handler != null) {
                            android.os.Message msg = handler.obtainMessage();
                            msg.what = MobiComConversationService.UPLOAD_COMPLETED;
                            msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                            msg.getData().putString("error", null);
                            msg.sendToTarget();
                        }
                    }
                } else {
                    JsonParser jsonParser = new JsonParser();
                    JsonObject jsonObject = jsonParser.parse(fileMetaResponse).getAsJsonObject();
                    if (jsonObject.has(FILE_META)) {
                        Gson gson = new Gson();
                        message.setFileMetas(gson.fromJson(jsonObject.get(FILE_META), FileMeta.class));
                        if (handler != null) {
                            android.os.Message msg = handler.obtainMessage();
                            msg.what = MobiComConversationService.UPLOAD_COMPLETED;
                            msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                            msg.getData().putString("error", null);
                            msg.sendToTarget();
                        }
                    }
                }
                // update message file-meta with thumbnail data
                if (thumbnailFileMeta != null) {
                    FileMeta messageFileMeta = message.getFileMetas();
                    if (!TextUtils.isEmpty(thumbnailFileMeta.getBlobKeyString())) {
                        messageFileMeta.setThumbnailBlobKey(thumbnailFileMeta.getBlobKeyString());
                    }
                    if (!TextUtils.isEmpty(thumbnailFileMeta.getThumbnailUrl())) {
                        messageFileMeta.setThumbnailUrl(thumbnailFileMeta.getThumbnailUrl());
                    }
                    message.setFileMetas(messageFileMeta);
                }
            } catch (Exception ex) {
                Utils.printLog(context, TAG, "Error uploading file to server: " + filePath);
                if (handler != null) {
                    android.os.Message msg = handler.obtainMessage();
                    msg.what = MobiComConversationService.UPLOAD_COMPLETED;
                    msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                    msg.getData().putString("error", "Error uploading file to server: " + filePath);
                    msg.sendToTarget();
                }
                /*  recentMessageSentToServer.remove(message);*/
                if (!message.isContactMessage() && !skipMessage) {
                    messageDatabaseService.updateCanceledFlag(messageId, 1);
                }
                if (!skipMessage) {
                    BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
                }
                return;
            }
        }
        if (messageId != -1 && !skipMessage) {
            messageDatabaseService.updateMessageFileMetas(messageId, message);
        }
    }
    Message newMessage = new Message();
    newMessage.setTo(message.getTo());
    newMessage.setKeyString(message.getKeyString());
    newMessage.setMessage(message.getMessage());
    newMessage.setFileMetas(message.getFileMetas());
    newMessage.setCreatedAtTime(message.getCreatedAtTime());
    newMessage.setRead(Boolean.TRUE);
    newMessage.setDeviceKeyString(message.getDeviceKeyString());
    newMessage.setSuUserKeyString(message.getSuUserKeyString());
    newMessage.setSent(message.isSent());
    newMessage.setType(message.getType());
    newMessage.setTimeToLive(message.getTimeToLive());
    newMessage.setSource(message.getSource());
    newMessage.setScheduledAt(message.getScheduledAt());
    newMessage.setStoreOnDevice(message.isStoreOnDevice());
    newMessage.setDelivered(message.getDelivered());
    newMessage.setStatus(message.getStatus());
    newMessage.setMetadata(message.getMetadata());
    newMessage.setSendToDevice(message.isSendToDevice());
    newMessage.setContentType(message.getContentType());
    newMessage.setConversationId(message.getConversationId());
    if (message.getGroupId() != null) {
        newMessage.setGroupId(message.getGroupId());
    }
    if (!TextUtils.isEmpty(message.getClientGroupId())) {
        newMessage.setClientGroupId(message.getClientGroupId());
    }
    if (contact != null && !TextUtils.isEmpty(contact.getApplicationId())) {
        newMessage.setApplicationId(contact.getApplicationId());
    } else {
        newMessage.setApplicationId(getApplicationKey(context));
    }
    try {
        if (!isBroadcastOneByOneGroupType) {
            String response = sendMessage(newMessage);
            if (message.hasAttachment() && TextUtils.isEmpty(response) && !message.isContactMessage() && !skipMessage && !isOpenGroup) {
                messageDatabaseService.updateCanceledFlag(messageId, 1);
                if (handler != null) {
                    android.os.Message msg = handler.obtainMessage();
                    msg.what = MobiComConversationService.UPLOAD_COMPLETED;
                    msg.getData().putString("error", "Error uploading file to server");
                    msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                    msg.sendToTarget();
                }
                BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.UPLOAD_ATTACHMENT_FAILED.toString(), message);
            }
            MessageResponse messageResponse = (MessageResponse) GsonUtils.getObjectFromJson(response, MessageResponse.class);
            keyString = messageResponse.getMessageKey();
            if (!TextUtils.isEmpty(keyString)) {
                message.setSentMessageTimeAtServer(Long.parseLong(messageResponse.getCreatedAtTime()));
                message.setConversationId(messageResponse.getConversationId());
                message.setSentToServer(true);
                message.setKeyString(keyString);
                if (contact != null && !TextUtils.isEmpty(userDisplayName) && contact.isUserDisplayUpdateRequired()) {
                    UserService.getInstance(context).updateUserDisplayName(message.getTo(), userDisplayName);
                }
            }
            if (!skipMessage && !isOpenGroup) {
                messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
            }
        } else {
            message.setSentMessageTimeAtServer(message.getCreatedAtTime());
            messageDatabaseService.updateMessage(messageId, message.getSentMessageTimeAtServer(), keyString, message.isSentToServer());
        }
        if (message.isSentToServer()) {
            if (handler != null) {
                android.os.Message msg = handler.obtainMessage();
                msg.what = MobiComConversationService.MESSAGE_SENT;
                msg.getData().putString(MobiComKitConstants.MESSAGE_INTENT_EXTRA, message.getKeyString());
                String messageJson = GsonUtils.getJsonFromObject(message, Message.class);
                msg.getData().putString(MobiComKitConstants.MESSAGE_JSON_INTENT_EXTRA, messageJson);
                msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
                msg.sendToTarget();
            }
        }
        if (!TextUtils.isEmpty(keyString)) {
        // Todo: Handle server message add failure due to internet disconnect.
        } else {
        // Todo: If message type is mtext, tell user that internet is not working, else send update with db id.
        }
        if (!skipMessage || isOpenGroup) {
            BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.MESSAGE_SYNC_ACK_FROM_SERVER.toString(), message);
        }
    } catch (Exception e) {
        if (handler != null) {
            android.os.Message msg = handler.obtainMessage();
            msg.what = MobiComConversationService.UPLOAD_COMPLETED;
            msg.getData().putString(MobiComKitConstants.OLD_MESSAGE_KEY_INTENT_EXTRA, oldMessageKey);
            msg.getData().putString("error", "Error uploading file");
            msg.sendToTarget();
        // handler.onCompleted(new ApplozicException("Error uploading file"));
        }
    }
/*  if (recentMessageSentToServer.size() > 20) {
            recentMessageSentToServer.subList(0, 10).clear();
        }*/
}
Also used : MessageResponse(com.applozic.mobicomkit.feed.MessageResponse) MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) Channel(com.applozic.mobicommons.people.channel.Channel) ArrayList(java.util.ArrayList) FileClientService(com.applozic.mobicomkit.api.attachment.FileClientService) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) ApplozicException(com.applozic.mobicomkit.exception.ApplozicException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Contact(com.applozic.mobicommons.people.contact.Contact) FileMeta(com.applozic.mobicomkit.api.attachment.FileMeta) JsonParser(com.google.gson.JsonParser)

Example 18 with Channel

use of com.applozic.mobicommons.people.channel.Channel in project Applozic-Android-SDK by AppLozic.

the class MobiComMessageService method processMessage.

// Cleanup: private, MessageSendTimer is not used
public Message processMessage(final Message messageToProcess, String tofield, int index) {
    if (Message.MetaDataType.HIDDEN.getValue().equals(messageToProcess.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
        MessageWorker.enqueueWork(context, messageToProcess, null, null);
        return null;
    } else if (Message.MetaDataType.PUSHNOTIFICATION.getValue().equals(messageToProcess.getMetaDataValueForKey(Message.MetaDataType.KEY.getValue()))) {
        BroadcastService.sendNotificationBroadcast(context, messageToProcess, index);
        MessageWorker.enqueueWork(context, messageToProcess, null, null);
        return null;
    }
    Message message = prepareMessage(messageToProcess, tofield);
    // download contacts in advance.
    if (message.getGroupId() != null) {
        Channel channel = ChannelService.getInstance(context).getChannelInfo(message.getGroupId());
        if (channel == null) {
            return null;
        }
    }
    if (message.getContentType() == Message.ContentType.CONTACT_MSG.getValue()) {
        fileClientService.loadContactsvCard(message);
    }
    try {
        List<String> messageKeys = new ArrayList<>();
        if (message.getMetadata() != null && message.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()) != null && !messageDatabaseService.isMessagePresent(message.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()))) {
            messageKeys.add(message.getMetaDataValueForKey(Message.MetaDataType.AL_REPLY.getValue()));
        }
        if (messageKeys != null && messageKeys.size() > 0) {
            Message[] replyMessageList = conversationService.getMessageListByKeyList(messageKeys);
            if (replyMessageList != null) {
                Message replyMessage = replyMessageList[0];
                if (replyMessage != null) {
                    if (replyMessage.hasAttachment() && !(replyMessage.getContentType() == Message.ContentType.TEXT_URL.getValue())) {
                        conversationService.setFilePathifExist(replyMessage);
                    }
                    if (replyMessage.getContentType() == Message.ContentType.CONTACT_MSG.getValue()) {
                        fileClientService.loadContactsvCard(replyMessage);
                    }
                    replyMessage.setReplyMessage(Message.ReplyMessage.HIDE_MESSAGE.getValue());
                    messageDatabaseService.createMessage(replyMessage);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (message.getType().equals(Message.MessageType.MT_INBOX.getValue())) {
        addMTMessage(message, index);
    } else if (message.getType().equals(Message.MessageType.MT_OUTBOX.getValue())) {
        BroadcastService.sendMessageUpdateBroadcast(context, BroadcastService.INTENT_ACTIONS.SYNC_MESSAGE.toString(), message);
        messageDatabaseService.createMessage(message);
        if (!message.getCurrentId().equals(BroadcastService.currentUserId)) {
            MobiComUserPreference.getInstance(context).setNewMessageFlag(true);
        }
        if (message.isVideoNotificationMessage()) {
            Utils.printLog(context, TAG, "Got notifications for Video call...");
            VideoCallNotificationHelper helper = new VideoCallNotificationHelper(context);
            helper.handleVideoCallNotificationMessages(message);
        }
    }
    Utils.printLog(context, TAG, "processing message: " + message);
    return message;
}
Also used : PersonalizedMessage(com.applozic.mobicommons.personalization.PersonalizedMessage) VideoCallNotificationHelper(com.applozic.mobicomkit.api.notification.VideoCallNotificationHelper) Channel(com.applozic.mobicommons.people.channel.Channel) ArrayList(java.util.ArrayList)

Example 19 with Channel

use of com.applozic.mobicommons.people.channel.Channel in project Applozic-Android-SDK by AppLozic.

the class SyncCallService method syncMessageMetadataUpdate.

public synchronized void syncMessageMetadataUpdate(String key, boolean isFromFcm, Message message) {
    Integer groupId = null;
    if (message != null) {
        if (message.getGroupId() != null) {
            groupId = message.getGroupId();
            Channel channel = ChannelService.getInstance(context).getChannel(groupId);
            if (channel != null && channel.isOpenGroup()) {
                if (message.hasAttachment()) {
                    messageDatabaseService.replaceExistingMessage(message);
                }
                BroadcastService.updateMessageMetadata(context, message.getKeyString(), BroadcastService.INTENT_ACTIONS.MESSAGE_METADATA_UPDATE.toString(), null, groupId, true, message.getMetadata());
                return;
            }
        }
    }
    if (!TextUtils.isEmpty(key) && mobiComMessageService.isMessagePresent(key)) {
        if (Utils.isDeviceInIdleState(context)) {
            new ConversationRunnables(context, null, false, false, true);
        } else {
            Utils.printLog(context, TAG, "Syncing updated message metadata from " + (isFromFcm ? "FCM" : "MQTT") + " for message key : " + key);
            ConversationWorker.enqueueWorkMessageMetadataUpdate(context);
        }
    }
}
Also used : Channel(com.applozic.mobicommons.people.channel.Channel) ConversationRunnables(com.applozic.mobicomkit.ConversationRunnables)

Example 20 with Channel

use of com.applozic.mobicommons.people.channel.Channel in project Applozic-Android-SDK by AppLozic.

the class ApplozicConversation method markAsRead.

/**
 * Mark a conversation as read. Either one of the <i>userId</i> or the <i>groupId</i> can be null.
 *
 * @param pairedMessageKey not used. pass null
 * @param userId for one-to-one conversation
 * @param groupId for group conversation
 */
public static void markAsRead(@NonNull Context context, @Nullable String pairedMessageKey, @Nullable String userId, @Nullable Integer groupId) {
    try {
        int unreadCount = 0;
        Contact contact = null;
        Channel channel = null;
        if (userId != null) {
            contact = new AppContactService(context).getContactById(userId);
            unreadCount = contact.getUnreadCount();
            new MessageDatabaseService(context).updateReadStatusForContact(userId);
        } else if (groupId != null && groupId != 0) {
            channel = ChannelService.getInstance(context).getChannelByChannelKey(groupId);
            unreadCount = channel.getUnreadCount();
            new MessageDatabaseService(context).updateReadStatusForChannel(String.valueOf(groupId));
        }
        UserWorker.enqueueWork(context, null, contact, channel, pairedMessageKey, unreadCount, false);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}
Also used : AppContactService(com.applozic.mobicomkit.contact.AppContactService) Channel(com.applozic.mobicommons.people.channel.Channel) ApplozicException(com.applozic.mobicomkit.exception.ApplozicException) Contact(com.applozic.mobicommons.people.contact.Contact) MessageDatabaseService(com.applozic.mobicomkit.api.conversation.database.MessageDatabaseService)

Aggregations

Channel (com.applozic.mobicommons.people.channel.Channel)70 Contact (com.applozic.mobicommons.people.contact.Contact)26 Intent (android.content.Intent)12 ChannelFeed (com.applozic.mobicomkit.feed.ChannelFeed)10 ArrayList (java.util.ArrayList)10 Message (com.applozic.mobicomkit.api.conversation.Message)8 MobiComUserPreference (com.applozic.mobicomkit.api.account.user.MobiComUserPreference)7 ApplozicException (com.applozic.mobicomkit.exception.ApplozicException)7 SyncChannelFeed (com.applozic.mobicomkit.sync.SyncChannelFeed)7 ChannelUserMapper (com.applozic.mobicommons.people.channel.ChannelUserMapper)7 NonNull (androidx.annotation.NonNull)6 FileClientService (com.applozic.mobicomkit.api.attachment.FileClientService)6 Context (android.content.Context)4 Cursor (android.database.Cursor)4 Bitmap (android.graphics.Bitmap)4 FileMeta (com.applozic.mobicomkit.api.attachment.FileMeta)4 ChannelFeedApiResponse (com.applozic.mobicomkit.feed.ChannelFeedApiResponse)4 ChannelUsersFeed (com.applozic.mobicomkit.feed.ChannelUsersFeed)4 Conversation (com.applozic.mobicommons.people.channel.Conversation)4 JSONException (org.json.JSONException)4