Search in sources :

Example 11 with RosterContact

use of com.xabber.android.data.roster.RosterContact in project xabber-android by redsolution.

the class ContactListPresenter method getSearchResults.

private ArrayList<AbstractContact> getSearchResults(Collection<RosterContact> rosterContacts, Comparator<AbstractContact> comparator, Map<AccountJid, Map<UserJid, AbstractChat>> abstractChats) {
    final ArrayList<AbstractContact> baseEntities = new ArrayList<>();
    // Build structure.
    for (RosterContact rosterContact : rosterContacts) {
        if (!rosterContact.isEnabled()) {
            continue;
        }
        final AccountJid account = rosterContact.getAccount();
        final Map<UserJid, AbstractChat> users = abstractChats.get(account);
        if (users != null) {
            users.remove(rosterContact.getUser());
        }
        if (rosterContact.getName().toLowerCase(locale).contains(filterString)) {
            baseEntities.add(rosterContact);
        }
    }
    for (Map<UserJid, 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 (abstractContact.getName().toLowerCase(locale).contains(filterString)) {
                baseEntities.add(abstractContact);
            }
        }
    }
    Collections.sort(baseEntities, comparator);
    return baseEntities;
}
Also used : RoomContact(com.xabber.android.data.extension.muc.RoomContact) RosterContact(com.xabber.android.data.roster.RosterContact) AbstractChat(com.xabber.android.data.message.AbstractChat) AccountJid(com.xabber.android.data.entity.AccountJid) ArrayList(java.util.ArrayList) UserJid(com.xabber.android.data.entity.UserJid) AbstractContact(com.xabber.android.data.roster.AbstractContact) RoomChat(com.xabber.android.data.extension.muc.RoomChat) ChatContact(com.xabber.android.data.message.ChatContact)

Example 12 with RosterContact

use of com.xabber.android.data.roster.RosterContact 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)

Example 13 with RosterContact

use of com.xabber.android.data.roster.RosterContact in project xabber-android by redsolution.

the class ContactListAdapter method getSearchResults.

private ArrayList<AbstractContact> getSearchResults(Collection<RosterContact> rosterContacts, Comparator<AbstractContact> comparator, Map<String, Map<String, AbstractChat>> abstractChats) {
    final ArrayList<AbstractContact> baseEntities = new ArrayList<>();
    // Build structure.
    for (RosterContact rosterContact : rosterContacts) {
        if (!rosterContact.isEnabled()) {
            continue;
        }
        final String account = rosterContact.getAccount();
        final Map<String, AbstractChat> users = abstractChats.get(account);
        if (users != null) {
            users.remove(rosterContact.getUser());
        }
        if (rosterContact.getName().toLowerCase(locale).contains(filterString)) {
            baseEntities.add(rosterContact);
        }
    }
    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 (abstractContact.getName().toLowerCase(locale).contains(filterString)) {
                baseEntities.add(abstractContact);
            }
        }
    }
    Collections.sort(baseEntities, comparator);
    return baseEntities;
}
Also used : RoomContact(com.xabber.android.data.extension.muc.RoomContact) RosterContact(com.xabber.android.data.roster.RosterContact) AbstractChat(com.xabber.android.data.message.AbstractChat) ArrayList(java.util.ArrayList) AbstractContact(com.xabber.android.data.roster.AbstractContact) RoomChat(com.xabber.android.data.extension.muc.RoomChat) ChatContact(com.xabber.android.data.message.ChatContact)

Example 14 with RosterContact

use of com.xabber.android.data.roster.RosterContact in project xabber-android by redsolution.

the class ContactEditor method editAlias.

private void editAlias() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.edit_alias);
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
    RosterContact rosterContact = RosterManager.getInstance().getRosterContact(getAccount(), getBareAddress());
    input.setText(rosterContact.getName());
    builder.setView(input);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            RosterManager.getInstance().setName(getAccount(), getBareAddress(), input.getText().toString());
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.show();
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) RosterContact(com.xabber.android.data.roster.RosterContact) DialogInterface(android.content.DialogInterface) EntityIntentBuilder(com.xabber.android.data.intent.EntityIntentBuilder)

Example 15 with RosterContact

use of com.xabber.android.data.roster.RosterContact in project xabber-android by redsolution.

the class ContactList method openChat.

/**
     * Open chat with specified contact.
     * <p/>
     * Show dialog to choose account if necessary.
     *
     * @param user
     * @param text can be <code>null</code>.
     */
private void openChat(String user, String text) {
    String bareAddress = Jid.getBareAddress(user);
    ArrayList<BaseEntity> entities = new ArrayList<>();
    for (AbstractChat check : MessageManager.getInstance().getChats()) {
        if (check.isActive() && check.getUser().equals(bareAddress)) {
            entities.add(check);
        }
    }
    if (entities.size() == 1) {
        openChat(entities.get(0), text);
        return;
    }
    entities.clear();
    for (RosterContact check : RosterManager.getInstance().getContacts()) {
        if (check.isEnabled() && check.getUser().equals(bareAddress)) {
            entities.add(check);
        }
    }
    if (entities.size() == 1) {
        openChat(entities.get(0), text);
        return;
    }
    Collection<String> accounts = AccountManager.getInstance().getAccounts();
    if (accounts.isEmpty()) {
        return;
    }
    if (accounts.size() == 1) {
        openChat(new BaseEntity(accounts.iterator().next(), bareAddress), text);
        return;
    }
    AccountChooseDialogFragment.newInstance(bareAddress, text).show(getFragmentManager(), "OPEN_WITH_ACCOUNT");
}
Also used : RosterContact(com.xabber.android.data.roster.RosterContact) AbstractChat(com.xabber.android.data.message.AbstractChat) ArrayList(java.util.ArrayList) BaseEntity(com.xabber.android.data.entity.BaseEntity)

Aggregations

RosterContact (com.xabber.android.data.roster.RosterContact)24 AbstractChat (com.xabber.android.data.message.AbstractChat)11 ArrayList (java.util.ArrayList)8 RoomChat (com.xabber.android.data.extension.muc.RoomChat)6 AccountJid (com.xabber.android.data.entity.AccountJid)5 UserJid (com.xabber.android.data.entity.UserJid)5 RoomContact (com.xabber.android.data.extension.muc.RoomContact)5 ChatContact (com.xabber.android.data.message.ChatContact)5 AbstractContact (com.xabber.android.data.roster.AbstractContact)5 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 EditText (android.widget.EditText)3 CommonState (com.xabber.android.data.account.CommonState)3 EntityIntentBuilder (com.xabber.android.data.intent.EntityIntentBuilder)3 Collection (java.util.Collection)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Intent (android.content.Intent)2 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 BaseEntity (com.xabber.android.data.entity.BaseEntity)2