Search in sources :

Example 11 with AbstractChat

use of com.xabber.android.data.message.AbstractChat in project xabber-android by redsolution.

the class MessageArchiveManager method onChatReceived.

private void onChatReceived(String account, Chat chat) {
    Boolean modification = modificationRequests.remove(account, chat.getPacketID());
    if (modification == null)
        return;
    ChatStorage chatStorage = chatStorages.get(account, chat.getWith(), chat.getStartString());
    if (chatStorage == null) {
        LogManager.w(this, "Unexpected chat " + chat.getStartString() + " recevied by " + account + " from " + chat.getWith());
        chatStorage = new ChatStorage(chat.getStart());
        chatStorages.put(account, chat.getWith(), chat.getStartString(), chatStorage);
    }
    String bareAddress = Jid.getBareAddress(chat.getWith());
    HeaderSequence sequence;
    if (modification)
        sequence = modificationStorages.get(account);
    else
        sequence = historyStorages.get(account, bareAddress);
    if (sequence == null)
        return;
    AbstractChat abstractChat = MessageManager.getInstance().getOrCreateChat(account, bareAddress);
    for (AbstractMessage abstractMessage : chat.getMessages()) chatStorage.addItem(abstractChat, chat, abstractMessage, TimeManager.getInstance().getServerTimeOffset(account));
    if (chat.getRsm() == null || chat.getRsm().isForwardFinished(chat.getMessages().size())) {
        chatStorage.onItemsReceived(chat.getVersion());
        sequence.pollHeader();
        if (sequence instanceof HistoryStorage)
            if (apply(account, bareAddress, chat.getStartString(), chatStorage, (HistoryStorage) sequence))
                return;
        requestSequence(account, bareAddress, sequence);
    } else {
        requestChat(account, chat, chat.getRsm().getLast(), modification);
    }
}
Also used : AbstractMessage(com.xabber.xmpp.archive.AbstractMessage) AbstractChat(com.xabber.android.data.message.AbstractChat)

Example 12 with AbstractChat

use of com.xabber.android.data.message.AbstractChat in project xabber-android by redsolution.

the class MessageArchiveManager method apply.

/**
     * Apply received messages.
     *
     * @param account
     * @param bareAddress
     * @param tag
     * @param chatStorage
     * @param historyStorage
     * @return Whether enough messages have been received.
     */
private boolean apply(String account, String bareAddress, String tag, ChatStorage chatStorage, HistoryStorage historyStorage) {
    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, bareAddress);
    int newCount = abstractChat.onMessageDownloaded(tag, chatStorage.getItems(), false);
    int incomingCount = 0;
    for (MessageItem messageItem : chatStorage.getItems()) if (messageItem.isIncoming())
        incomingCount += 1;
    chatStorage.onApplied();
    if (historyStorage.enoughMessages(newCount, incomingCount)) {
        historyStorage.onSuccess();
        return true;
    }
    return false;
}
Also used : MessageItem(com.xabber.android.data.message.MessageItem) AbstractChat(com.xabber.android.data.message.AbstractChat)

Example 13 with AbstractChat

use of com.xabber.android.data.message.AbstractChat in project xabber-android by redsolution.

the class RecentChatFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.recent_chats, container, false);
    ArrayList<AbstractChat> activeChats = ((ChatViewer) getActivity()).getChatViewerAdapter().getActiveChats();
    ((ChatListAdapter) getListAdapter()).updateChats(activeChats);
    if (getListAdapter().isEmpty()) {
        Activity activity = getActivity();
        Toast.makeText(activity, R.string.chat_list_is_empty, Toast.LENGTH_LONG).show();
        activity.finish();
    }
    Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_default);
    toolbar.setTitle(R.string.recent_chats);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(getActivity());
        }
    });
    toolbar.inflateMenu(R.menu.recent_chats);
    toolbar.setOnMenuItemClickListener(this);
    toolbar.setBackgroundColor(ColorManager.getInstance().getAccountPainter().getDefaultMainColor());
    return rootView;
}
Also used : ChatListAdapter(com.xabber.android.ui.adapter.ChatListAdapter) AbstractChat(com.xabber.android.data.message.AbstractChat) Activity(android.app.Activity) View(android.view.View) ListView(android.widget.ListView) Toolbar(android.support.v7.widget.Toolbar)

Example 14 with AbstractChat

use of com.xabber.android.data.message.AbstractChat in project xabber-android by redsolution.

the class ChatViewerFragment method setUpOptionsMenu.

private void setUpOptionsMenu(Menu menu) {
    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);
    if (abstractChat instanceof RoomChat) {
        RoomState chatState = ((RoomChat) abstractChat).getState();
        if (chatState == RoomState.available) {
            menu.findItem(R.id.action_list_of_occupants).setVisible(true);
        }
        if (chatState == RoomState.unavailable) {
            menu.findItem(R.id.action_join_conference).setVisible(true);
        } else {
            menu.findItem(R.id.action_invite_to_chat).setVisible(true);
            if (chatState == RoomState.error) {
                menu.findItem(R.id.action_authorization_settings).setVisible(true);
            } else {
                menu.findItem(R.id.action_leave_conference).setVisible(true);
            }
        }
    }
    if (abstractChat instanceof RegularChat) {
        menu.findItem(R.id.action_view_contact).setVisible(true);
        menu.findItem(R.id.action_close_chat).setVisible(true);
        menu.findItem(R.id.action_block_contact).setVisible(true);
    }
}
Also used : AbstractChat(com.xabber.android.data.message.AbstractChat) RoomChat(com.xabber.android.data.extension.muc.RoomChat) RegularChat(com.xabber.android.data.message.RegularChat) RoomState(com.xabber.android.data.extension.muc.RoomState)

Example 15 with AbstractChat

use of com.xabber.android.data.message.AbstractChat in project xabber-android by redsolution.

the class ContactListAdapter method onChange.

@Override
public void onChange() {
    synchronized (refreshLock) {
        refreshRequested = false;
        refreshInProgress = true;
        handler.removeCallbacks(this);
    }
    final Collection<RosterContact> allRosterContacts = RosterManager.getInstance().getContacts();
    Map<String, Collection<String>> blockedContacts = new TreeMap<>();
    for (String account : AccountManager.getInstance().getAccounts()) {
        blockedContacts.put(account, BlockingManager.getInstance().getBlockedContacts(account));
    }
    final Collection<RosterContact> rosterContacts = new ArrayList<>();
    for (RosterContact contact : allRosterContacts) {
        if (blockedContacts.containsKey(contact.getAccount())) {
            if (!blockedContacts.get(contact.getAccount()).contains(contact.getUser())) {
                rosterContacts.add(contact);
            }
        }
    }
    final boolean showOffline = SettingsManager.contactsShowOffline();
    final boolean showGroups = SettingsManager.contactsShowGroups();
    final boolean showEmptyGroups = SettingsManager.contactsShowEmptyGroups();
    final boolean showActiveChats = SettingsManager.contactsShowActiveChats();
    final boolean stayActiveChats = SettingsManager.contactsStayActiveChats();
    final boolean showAccounts = SettingsManager.contactsShowAccounts();
    final Comparator<AbstractContact> comparator = SettingsManager.contactsOrder();
    final CommonState commonState = AccountManager.getInstance().getCommonState();
    final String selectedAccount = AccountManager.getInstance().getSelectedAccount();
    /**
         * Groups.
         */
    final Map<String, GroupConfiguration> groups;
    /**
         * Contacts.
         */
    final List<AbstractContact> contacts;
    /**
         * List of active chats.
         */
    final GroupConfiguration activeChats;
    /**
         * Whether there is at least one contact.
         */
    boolean hasContacts = false;
    /**
         * Whether there is at least one visible contact.
         */
    boolean hasVisibleContacts = false;
    final Map<String, AccountConfiguration> accounts = new TreeMap<>();
    for (String account : AccountManager.getInstance().getAccounts()) {
        accounts.put(account, null);
    }
    /**
         * List of rooms and active chats grouped by users inside accounts.
         */
    final Map<String, Map<String, AbstractChat>> abstractChats = new TreeMap<>();
    for (AbstractChat abstractChat : MessageManager.getInstance().getChats()) {
        if ((abstractChat instanceof RoomChat || abstractChat.isActive()) && accounts.containsKey(abstractChat.getAccount())) {
            final String account = abstractChat.getAccount();
            Map<String, AbstractChat> users = abstractChats.get(account);
            if (users == null) {
                users = new TreeMap<>();
                abstractChats.put(account, users);
            }
            users.put(abstractChat.getUser(), abstractChat);
        }
    }
    if (filterString == null) {
        // Create arrays.
        if (showAccounts) {
            groups = null;
            contacts = null;
            for (Entry<String, AccountConfiguration> entry : accounts.entrySet()) {
                entry.setValue(new AccountConfiguration(entry.getKey(), GroupManager.IS_ACCOUNT, GroupManager.getInstance()));
            }
        } else {
            if (showGroups) {
                groups = new TreeMap<>();
                contacts = null;
            } else {
                groups = null;
                contacts = new ArrayList<>();
            }
        }
        if (showActiveChats) {
            activeChats = new GroupConfiguration(GroupManager.NO_ACCOUNT, GroupManager.ACTIVE_CHATS, GroupManager.getInstance());
        } else {
            activeChats = null;
        }
        // Build structure.
        for (RosterContact rosterContact : rosterContacts) {
            if (!rosterContact.isEnabled()) {
                continue;
            }
            hasContacts = true;
            final boolean online = rosterContact.getStatusMode().isOnline();
            final String account = rosterContact.getAccount();
            final Map<String, AbstractChat> users = abstractChats.get(account);
            final AbstractChat abstractChat;
            if (users == null) {
                abstractChat = null;
            } else {
                abstractChat = users.remove(rosterContact.getUser());
            }
            if (showActiveChats && abstractChat != null && abstractChat.isActive()) {
                activeChats.setNotEmpty();
                hasVisibleContacts = true;
                if (activeChats.isExpanded()) {
                    activeChats.addAbstractContact(rosterContact);
                }
                activeChats.increment(online);
                if (!stayActiveChats || (!showAccounts && !showGroups)) {
                    continue;
                }
            }
            if (selectedAccount != null && !selectedAccount.equals(account)) {
                continue;
            }
            if (addContact(rosterContact, online, accounts, groups, contacts, showAccounts, showGroups, showOffline)) {
                hasVisibleContacts = true;
            }
        }
        for (Map<String, AbstractChat> users : abstractChats.values()) {
            for (AbstractChat abstractChat : users.values()) {
                final AbstractContact abstractContact;
                if (abstractChat instanceof RoomChat) {
                    abstractContact = new RoomContact((RoomChat) abstractChat);
                } else {
                    abstractContact = new ChatContact(abstractChat);
                }
                if (showActiveChats && abstractChat.isActive()) {
                    activeChats.setNotEmpty();
                    hasVisibleContacts = true;
                    if (activeChats.isExpanded()) {
                        activeChats.addAbstractContact(abstractContact);
                    }
                    activeChats.increment(false);
                    if (!stayActiveChats || (!showAccounts && !showGroups)) {
                        continue;
                    }
                }
                if (selectedAccount != null && !selectedAccount.equals(abstractChat.getAccount())) {
                    continue;
                }
                final String group;
                final boolean online;
                if (abstractChat instanceof RoomChat) {
                    group = GroupManager.IS_ROOM;
                    online = abstractContact.getStatusMode().isOnline();
                } else if (MUCManager.getInstance().isMucPrivateChat(abstractChat.getAccount(), abstractChat.getUser())) {
                    group = GroupManager.IS_ROOM;
                    online = abstractContact.getStatusMode().isOnline();
                } else {
                    group = GroupManager.NO_GROUP;
                    online = false;
                }
                hasVisibleContacts = true;
                addContact(abstractContact, group, online, accounts, groups, contacts, showAccounts, showGroups);
            }
        }
        hasActiveChats = activeChats != null && activeChats.getTotal() > 0;
        // Remove empty groups, sort and apply structure.
        baseEntities.clear();
        if (hasVisibleContacts) {
            if (showActiveChats) {
                if (!activeChats.isEmpty()) {
                    if (showAccounts || showGroups) {
                        baseEntities.add(activeChats);
                    }
                    activeChats.sortAbstractContacts(ComparatorByChat.COMPARATOR_BY_CHAT);
                    baseEntities.addAll(activeChats.getAbstractContacts());
                }
            }
            if (showAccounts) {
                boolean isFirst = baseEntities.isEmpty();
                for (AccountConfiguration rosterAccount : accounts.values()) {
                    if (isFirst) {
                        isFirst = false;
                    } else {
                        baseEntities.add(new AccountTopSeparator(null, null));
                    }
                    baseEntities.add(rosterAccount);
                    if (showGroups) {
                        if (rosterAccount.isExpanded()) {
                            for (GroupConfiguration rosterConfiguration : rosterAccount.getSortedGroupConfigurations()) {
                                if (showEmptyGroups || !rosterConfiguration.isEmpty()) {
                                    baseEntities.add(rosterConfiguration);
                                    rosterConfiguration.sortAbstractContacts(comparator);
                                    baseEntities.addAll(rosterConfiguration.getAbstractContacts());
                                }
                            }
                        }
                    } else {
                        rosterAccount.sortAbstractContacts(comparator);
                        baseEntities.addAll(rosterAccount.getAbstractContacts());
                    }
                    if (rosterAccount.getTotal() > 0 && !rosterAccount.isExpanded()) {
                        baseEntities.add(new AccountBottomSeparator(rosterAccount.getAccount(), null));
                    }
                }
            } else {
                if (showGroups) {
                    for (GroupConfiguration rosterConfiguration : groups.values()) {
                        if (showEmptyGroups || !rosterConfiguration.isEmpty()) {
                            baseEntities.add(rosterConfiguration);
                            rosterConfiguration.sortAbstractContacts(comparator);
                            baseEntities.addAll(rosterConfiguration.getAbstractContacts());
                        }
                    }
                } else {
                    Collections.sort(contacts, comparator);
                    baseEntities.addAll(contacts);
                }
            }
        }
    } else {
        // Search
        final ArrayList<AbstractContact> baseEntities = getSearchResults(rosterContacts, comparator, abstractChats);
        this.baseEntities.clear();
        this.baseEntities.addAll(baseEntities);
        hasVisibleContacts = baseEntities.size() > 0;
    }
    super.onChange();
    listener.onContactListChanged(commonState, hasContacts, hasVisibleContacts, filterString != null);
    synchronized (refreshLock) {
        nextRefresh = new Date(new Date().getTime() + REFRESH_INTERVAL);
        refreshInProgress = false;
        // Just to be sure.
        handler.removeCallbacks(this);
        if (refreshRequested) {
            handler.postDelayed(this, REFRESH_INTERVAL);
        }
    }
}
Also used : CommonState(com.xabber.android.data.account.CommonState) RosterContact(com.xabber.android.data.roster.RosterContact) ArrayList(java.util.ArrayList) RoomChat(com.xabber.android.data.extension.muc.RoomChat) ChatContact(com.xabber.android.data.message.ChatContact) AbstractChat(com.xabber.android.data.message.AbstractChat) TreeMap(java.util.TreeMap) AbstractContact(com.xabber.android.data.roster.AbstractContact) Date(java.util.Date) RoomContact(com.xabber.android.data.extension.muc.RoomContact) Collection(java.util.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

AbstractChat (com.xabber.android.data.message.AbstractChat)23 NetworkException (com.xabber.android.data.NetworkException)4 RoomChat (com.xabber.android.data.extension.muc.RoomChat)4 View (android.view.View)3 RoomContact (com.xabber.android.data.extension.muc.RoomContact)3 ChatContact (com.xabber.android.data.message.ChatContact)3 RegularChat (com.xabber.android.data.message.RegularChat)3 AbstractContact (com.xabber.android.data.roster.AbstractContact)3 RosterContact (com.xabber.android.data.roster.RosterContact)3 ArrayList (java.util.ArrayList)3 MessageManager (com.xabber.android.data.message.MessageManager)2 Map (java.util.Map)2 OtrException (net.java.otr4j.OtrException)2 Message (org.jivesoftware.smack.packet.Message)2 Activity (android.app.Activity)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 Editable (android.text.Editable)1