Search in sources :

Example 6 with Channel

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

the class ConversationUIService method getConversationFragment.

public ConversationFragment getConversationFragment() {
    ConversationFragment conversationFragment = (ConversationFragment) UIService.getFragmentByTag(fragmentActivity, CONVERSATION_FRAGMENT);
    if (conversationFragment == null) {
        Contact contact = ((ConversationActivity) fragmentActivity).getContact();
        Channel channel = ((ConversationActivity) fragmentActivity).getChannel();
        Integer conversationId = ((ConversationActivity) fragmentActivity).getConversationId();
        conversationFragment = ConversationFragment.newInstance(contact, channel, conversationId, null, null);
        ConversationActivity.addFragment(fragmentActivity, conversationFragment, CONVERSATION_FRAGMENT);
    }
    return conversationFragment;
}
Also used : ConversationActivity(com.applozic.mobicomkit.uiwidgets.conversation.activity.ConversationActivity) Channel(com.applozic.mobicommons.people.channel.Channel) MobiComQuickConversationFragment(com.applozic.mobicomkit.uiwidgets.conversation.fragment.MobiComQuickConversationFragment) ConversationFragment(com.applozic.mobicomkit.uiwidgets.conversation.fragment.ConversationFragment) Contact(com.applozic.mobicommons.people.contact.Contact)

Example 7 with Channel

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

the class MobiComConversationService method getMessagesWithNetworkMetaData.

// Cleanup: try to get to default
public synchronized NetworkListDecorator<Message> getMessagesWithNetworkMetaData(Long startTime, Long endTime, Contact contact, Channel channel, Integer conversationId, boolean isSkipRead, boolean isForSearch) {
    if (isForSearch) {
        return getMessagesForParticularThreadWithNetworkMetaData(startTime, endTime, contact, channel, conversationId, 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() && ApplozicClient.getInstance(context).wasServerCallDoneBefore(contact, channel, conversationId) || (contact == null && channel == null && cachedMessageList.isEmpty() && ApplozicClient.getInstance(context).wasServerCallDoneBefore(contact, channel, conversationId)))) {
        Utils.printLog(context, TAG, "cachedMessageList size is : " + cachedMessageList.size());
        return new NetworkListDecorator<>(cachedMessageList, false);
    }
    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 new NetworkListDecorator<>(cachedMessageList, true);
    }
    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 new NetworkListDecorator<>(cachedMessageList, true);
        }
        return new NetworkListDecorator<>(cachedMessageList, true);
    }
    ApplozicClient.getInstance(context).updateServerCallDoneStatus(contact, channel, conversationId);
    // for the try catch
    boolean wasNetworkFail = false;
    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;
                }
                message.setHidden(isHideActionMessage && message.isActionMessage());
                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.hasHideKey()) {
                        if (message.getGroupId() != null) {
                            Channel newChannel = ChannelService.getInstance(context).getChannelByChannelKey(message.getGroupId());
                            if (newChannel != null) {
                                getMessagesWithNetworkMetaData(null, null, null, newChannel, null, true, isForSearch);
                            }
                        } else {
                            getMessagesWithNetworkMetaData(null, null, new Contact(message.getContactIds()), null, null, true, isForSearch);
                        }
                    }
                }
            }
            if (!isServerCallNotRequired && !message.isHidden()) {
                messageList.add(message);
            }
        }
        if (contact == null && channel == null) {
            Intent intent = new Intent(MobiComKitConstants.APPLOZIC_UNREAD_COUNT);
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        }
    } catch (JsonSyntaxException | JSONException jsonException) {
        jsonException.printStackTrace();
        wasNetworkFail = true;
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    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 new NetworkListDecorator<>(channel != null && Channel.GroupType.OPEN.getValue().equals(channel.getType()) ? messageList : finalMessageList, wasNetworkFail);
}
Also used : MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Conversation(com.applozic.mobicommons.people.channel.Conversation) UserDetail(com.applozic.mobicomkit.api.account.user.UserDetail) AnnotationExclusionStrategy(com.applozic.mobicommons.json.AnnotationExclusionStrategy) ChannelFeed(com.applozic.mobicomkit.feed.ChannelFeed) JsonParser(com.google.gson.JsonParser) GsonBuilder(com.google.gson.GsonBuilder) Channel(com.applozic.mobicommons.people.channel.Channel) FileClientService(com.applozic.mobicomkit.api.attachment.FileClientService) JSONException(org.json.JSONException) Intent(android.content.Intent) JSONException(org.json.JSONException) ApplozicException(com.applozic.mobicomkit.exception.ApplozicException) JsonSyntaxException(com.google.gson.JsonSyntaxException) Contact(com.applozic.mobicommons.people.contact.Contact) ArrayAdapterFactory(com.applozic.mobicommons.json.ArrayAdapterFactory) JsonSyntaxException(com.google.gson.JsonSyntaxException) JSONObject(org.json.JSONObject)

Example 8 with Channel

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

the class ChannelService method processChannelFeedForSync.

// Cleanup: private
/**
 * Internal. Do not use.
 */
public void processChannelFeedForSync(ChannelFeed channelFeed) {
    if (channelFeed != null) {
        Set<String> memberUserIds = channelFeed.getMembersName();
        Set<String> userIds = new HashSet<>();
        Channel channel = getChannel(channelFeed);
        if (channelDatabaseService.isChannelPresent(channel.getKey())) {
            channelDatabaseService.updateChannel(channel);
            channelDatabaseService.deleteChannelUserMappers(channel.getKey());
        } else {
            channelDatabaseService.addChannel(channel);
        }
        if (memberUserIds != null && memberUserIds.size() > 0) {
            for (String userId : memberUserIds) {
                ChannelUserMapper channelUserMapper = new ChannelUserMapper(channelFeed.getId(), userId);
                channelUserMapper.setParentKey(channelFeed.getParentKey());
                channelDatabaseService.addChannelUserMapper(channelUserMapper);
                if (!baseContactService.isContactExists(userId)) {
                    userIds.add(userId);
                }
            }
            if (userIds != null && userIds.size() > 0) {
                userService.processUserDetailsByUserIds(userIds);
            }
        }
        if (channelFeed.getGroupUsers() != null && channelFeed.getGroupUsers().size() > 0) {
            for (ChannelUsersFeed channelUsers : channelFeed.getGroupUsers()) {
                if (channelUsers.getRole() != null) {
                    channelDatabaseService.updateRoleInChannelUserMapper(channelFeed.getId(), channelUsers.getUserId(), channelUsers.getRole());
                }
            }
        }
        if (channelFeed.getChildKeys() != null && channelFeed.getChildKeys().size() > 0) {
            processChildGroupKeysForChannelSync(channelFeed.getChildKeys());
        }
        if (channel.isDeleted() && ApplozicClient.getInstance(context).isSkipDeletedGroups()) {
            BroadcastService.sendConversationDeleteBroadcast(context, BroadcastService.INTENT_ACTIONS.DELETE_CONVERSATION.toString(), null, channel.getKey(), "success");
        }
    }
}
Also used : ChannelUsersFeed(com.applozic.mobicomkit.feed.ChannelUsersFeed) Channel(com.applozic.mobicommons.people.channel.Channel) ChannelUserMapper(com.applozic.mobicommons.people.channel.ChannelUserMapper) HashSet(java.util.HashSet)

Example 9 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 channel key.
 *
 * <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 key the channel key
 * @return the channel object
 */
@Nullable
public Channel getChannelInfo(@Nullable Integer key) {
    if (key == null) {
        return null;
    }
    Channel channel = channelDatabaseService.getChannelByChannelKey(key);
    if (channel == null) {
        ChannelFeed channelFeed = channelClientService.getChannelInfo(key);
        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 10 with Channel

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

the class ChannelService method processChannelFeed.

// Cleanup: private
/**
 * Internal method. Do not use.
 */
public void processChannelFeed(ChannelFeed channelFeed, boolean isUserDetails) {
    if (channelFeed != null) {
        Set<String> memberUserIds = null;
        if (channelFeed.getMembersName() != null) {
            memberUserIds = channelFeed.getMembersName();
        } else {
            memberUserIds = channelFeed.getContactGroupMembersId();
        }
        Channel channel = getChannel(channelFeed);
        if (channelDatabaseService.isChannelPresent(channel.getKey())) {
            channelDatabaseService.updateChannel(channel);
        } else {
            channelDatabaseService.addChannel(channel);
        }
        if (channelFeed.getConversationPxy() != null) {
            channelFeed.getConversationPxy().setGroupId(channelFeed.getId());
            ConversationService.getInstance(context).addConversation(channelFeed.getConversationPxy());
        }
        if (memberUserIds != null && memberUserIds.size() > 0) {
            for (String userId : memberUserIds) {
                ChannelUserMapper channelUserMapper = new ChannelUserMapper(channelFeed.getId(), userId);
                channelUserMapper.setParentKey(channelFeed.getParentKey());
                if (channelDatabaseService.isChannelUserPresent(channelFeed.getId(), userId)) {
                    channelDatabaseService.updateChannelUserMapper(channelUserMapper);
                } else {
                    channelDatabaseService.addChannelUserMapper(channelUserMapper);
                }
            }
        }
        if (isUserDetails) {
            userService.processUserDetail(channelFeed.getUsers());
        }
        if (channelFeed.getGroupUsers() != null && channelFeed.getGroupUsers().size() > 0) {
            for (ChannelUsersFeed channelUsers : channelFeed.getGroupUsers()) {
                if (channelUsers.getRole() != null) {
                    channelDatabaseService.updateRoleInChannelUserMapper(channelFeed.getId(), channelUsers.getUserId(), channelUsers.getRole());
                }
            }
        }
        if (channelFeed.getChildKeys() != null && channelFeed.getChildKeys().size() > 0) {
            processChildGroupKeys(channelFeed.getChildKeys());
        }
    }
}
Also used : ChannelUsersFeed(com.applozic.mobicomkit.feed.ChannelUsersFeed) Channel(com.applozic.mobicommons.people.channel.Channel) ChannelUserMapper(com.applozic.mobicommons.people.channel.ChannelUserMapper)

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