Search in sources :

Example 1 with AccountJid

use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.

the class MessageManager method onLoad.

@Override
public void onLoad() {
    Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            RealmResults<MessageItem> messagesToSend = realm.where(MessageItem.class).equalTo(MessageItem.Fields.SENT, false).findAll();
            for (MessageItem messageItem : messagesToSend) {
                AccountJid account = messageItem.getAccount();
                UserJid user = messageItem.getUser();
                if (account != null && user != null) {
                    if (getChat(account, user) == null) {
                        createChat(account, user);
                    }
                }
            }
        }
    });
    realm.close();
    NotificationManager.getInstance().registerNotificationProvider(mucPrivateChatRequestProvider);
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) Realm(io.realm.Realm) RealmResults(io.realm.RealmResults)

Example 2 with AccountJid

use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.

the class ReceiptManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza packet) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    final AccountJid account = ((AccountItem) connection).getAccount();
    final Jid from = packet.getFrom();
    if (from == null) {
        return;
    }
    if (!(packet instanceof Message)) {
        return;
    }
    final Message message = (Message) packet;
    if (message.getType() == Message.Type.error) {
        Application.getInstance().runInBackgroundUserRequest(new Runnable() {

            @Override
            public void run() {
                markAsError(account, message);
            }
        });
    } else {
        // TODO setDefaultAutoReceiptMode should be used
        for (ExtensionElement packetExtension : message.getExtensions()) {
            if (packetExtension instanceof DeliveryReceiptRequest) {
                String id = message.getStanzaId();
                if (id == null) {
                    continue;
                }
                Message receipt = new Message(from);
                receipt.addExtension(new DeliveryReceipt(id));
                // the key problem is Thread - smack does not keep it in auto reply
                receipt.setThread(message.getThread());
                try {
                    StanzaSender.sendStanza(account, receipt);
                } catch (NetworkException e) {
                    LogManager.exception(this, e);
                }
            }
        }
    }
}
Also used : AccountJid(com.xabber.android.data.entity.AccountJid) Jid(org.jxmpp.jid.Jid) Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) DeliveryReceiptRequest(org.jivesoftware.smackx.receipts.DeliveryReceiptRequest) AccountJid(com.xabber.android.data.entity.AccountJid) DeliveryReceipt(org.jivesoftware.smackx.receipts.DeliveryReceipt) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) NetworkException(com.xabber.android.data.NetworkException)

Example 3 with AccountJid

use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.

the class NotificationManager method updatePersistentNotification.

private void updatePersistentNotification() {
    if (!SettingsManager.eventsPersistent()) {
        return;
    }
    // we do not want to show persistent notification if there are no enabled accounts
    XabberService.getInstance().changeForeground();
    Collection<AccountJid> accountList = AccountManager.getInstance().getEnabledAccounts();
    if (accountList.isEmpty()) {
        return;
    }
    int waiting = 0;
    int connecting = 0;
    int connected = 0;
    for (AccountJid account : accountList) {
        AccountItem accountItem = AccountManager.getInstance().getAccount(account);
        if (accountItem == null) {
            continue;
        }
        ConnectionState state = accountItem.getState();
        LogManager.i(this, "updatePersistentNotification account " + account + " state " + state);
        switch(state) {
            case offline:
                break;
            case waiting:
                waiting++;
                break;
            case connecting:
            case registration:
            case authentication:
                connecting++;
                break;
            case connected:
                connected++;
                break;
        }
    }
    final Intent persistentIntent;
    persistentIntent = ContactListActivity.createPersistentIntent(application);
    if (connected > 0) {
        persistentNotificationBuilder.setColor(persistentNotificationColor);
        persistentNotificationBuilder.setSmallIcon(R.drawable.ic_stat_online);
    } else {
        persistentNotificationBuilder.setColor(NotificationCompat.COLOR_DEFAULT);
        persistentNotificationBuilder.setSmallIcon(R.drawable.ic_stat_offline);
    }
    persistentNotificationBuilder.setContentText(getConnectionState(waiting, connecting, connected, accountList.size()));
    persistentNotificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, persistentIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    notify(PERSISTENT_NOTIFICATION_ID, persistentNotificationBuilder.build());
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ConnectionState(com.xabber.android.data.connection.ConnectionState) SuppressLint(android.annotation.SuppressLint)

Example 4 with AccountJid

use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.

the class XabberAccountManager method createSyncList.

/**
 * @param remoteList is list of settings from cloud
 * @return list of settings for displaying in sync-dialog
 */
public List<XMPPAccountSettings> createSyncList(List<XMPPAccountSettings> remoteList) {
    List<XMPPAccountSettings> resultList = new ArrayList<>();
    // add remote accounts to list
    for (XMPPAccountSettings remoteItem : remoteList) {
        XMPPAccountSettings.SyncStatus status = XMPPAccountSettings.SyncStatus.remote;
        // if account already exist
        AccountJid accountJid = getExistingAccount(remoteItem.getJid());
        if (accountJid != null) {
            AccountItem localItem = AccountManager.getInstance().getAccount(accountJid);
            if (localItem != null) {
                if (remoteItem.getTimestamp() == localItem.getTimestamp())
                    status = XMPPAccountSettings.SyncStatus.localEqualsRemote;
                else if (remoteItem.getTimestamp() > localItem.getTimestamp())
                    status = XMPPAccountSettings.SyncStatus.remoteNewer;
                else {
                    status = XMPPAccountSettings.SyncStatus.localNewer;
                    remoteItem.setTimestamp(localItem.getTimestamp());
                }
                remoteItem.setSyncNotAllowed(localItem.isSyncNotAllowed());
            }
            remoteItem.setStatus(status);
            remoteItem.setSynchronization(isAccountSynchronize(remoteItem.getJid()));
            resultList.add(remoteItem);
        } else if (!remoteItem.isDeleted()) {
            remoteItem.setStatus(status);
            remoteItem.setSynchronization(isAccountSynchronize(remoteItem.getJid()));
            resultList.add(remoteItem);
        }
    }
    // add local accounts to list
    Collection<AccountItem> localAccounts = AccountManager.getInstance().getAllAccountItems();
    for (AccountItem localAccount : localAccounts) {
        String localJid = localAccount.getAccount().getFullJid().asBareJid().toString();
        boolean exist = false;
        for (XMPPAccountSettings remoteItem : remoteList) {
            if (localJid.equals(remoteItem.getJid())) {
                exist = true;
            }
        }
        if (!exist) {
            XMPPAccountSettings localItem = new XMPPAccountSettings(localJid, false, localAccount.getTimestamp());
            localItem.setOrder(remoteList.size() + localAccount.getOrder());
            localItem.setColor(ColorManager.getInstance().convertIndexToColorName(localAccount.getColorIndex()));
            localItem.setStatus(XMPPAccountSettings.SyncStatus.local);
            localItem.setSynchronization(isAccountSynchronize(localItem.getJid()));
            localItem.setSyncNotAllowed(localAccount.isSyncNotAllowed());
            resultList.add(localItem);
        }
    }
    return resultList;
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) ArrayList(java.util.ArrayList)

Example 5 with AccountJid

use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.

the class ContactListPresenter method buildStructure.

/**
 * Do not call directly. Only from Structure Builder
 */
void buildStructure() {
    // listener.hidePlaceholder();
    List<IFlexible> items = new ArrayList<>();
    final Collection<RosterContact> allRosterContacts = RosterManager.getInstance().getAllContacts();
    Map<AccountJid, Collection<UserJid>> blockedContacts = new TreeMap<>();
    for (AccountJid account : AccountManager.getInstance().getEnabledAccounts()) {
        blockedContacts.put(account, BlockingManager.getInstance().getCachedBlockedContacts(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 = false;
    final boolean stayActiveChats = true;
    final boolean showAccounts = SettingsManager.contactsShowAccounts();
    final Comparator<AbstractContact> comparator = SettingsManager.contactsOrder();
    final CommonState commonState = AccountManager.getInstance().getCommonState();
    final AccountJid selectedAccount = AccountManager.getInstance().getSelectedAccount();
    /**
     * Groups.
     */
    final Map<String, GroupConfiguration> groups;
    /**
     * Contacts.
     */
    final List<AbstractContact> contacts;
    /**
     * Chat list on top of contact list.
     */
    final GroupConfiguration chatsGroup;
    /**
     * Whether there is at least one contact.
     */
    boolean hasContacts = false;
    /**
     * Whether there is at least one visible contact.
     */
    boolean hasVisibleContacts = false;
    final Map<AccountJid, AccountConfiguration> accounts = new TreeMap<>();
    for (AccountJid account : AccountManager.getInstance().getEnabledAccounts()) {
        accounts.put(account, null);
    }
    /**
     * List of rooms and active chats grouped by users inside accounts.
     */
    final Map<AccountJid, Map<UserJid, AbstractChat>> abstractChats = new TreeMap<>();
    for (AbstractChat abstractChat : MessageManager.getInstance().getChats()) {
        if ((abstractChat instanceof RoomChat || abstractChat.isActive()) && accounts.containsKey(abstractChat.getAccount())) {
            final AccountJid account = abstractChat.getAccount();
            Map<UserJid, AbstractChat> users = abstractChats.get(account);
            if (users == null) {
                users = new TreeMap<>();
                abstractChats.put(account, users);
            }
            users.put(abstractChat.getUser(), abstractChat);
        }
    }
    if (filterString == null || filterString.isEmpty()) {
        // Create arrays.
        if (showAccounts) {
            groups = null;
            contacts = null;
            for (Map.Entry<AccountJid, 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<>();
            }
        }
        // chats on top
        Collection<AbstractChat> chats = MessageManager.getInstance().getChatsOfEnabledAccount();
        chatsGroup = getChatsGroup(chats, currentChatsState);
        // Build structure.
        for (RosterContact rosterContact : rosterContacts) {
            if (!rosterContact.isEnabled()) {
                continue;
            }
            hasContacts = true;
            final boolean online = rosterContact.getStatusMode().isOnline();
            final AccountJid account = rosterContact.getAccount();
            final Map<UserJid, AbstractChat> users = abstractChats.get(account);
            final AbstractChat abstractChat;
            if (users == null) {
                abstractChat = null;
            } else {
                abstractChat = users.remove(rosterContact.getUser());
            }
            if (selectedAccount != null && !selectedAccount.equals(account)) {
                continue;
            }
            if (ContactListGroupUtils.addContact(rosterContact, online, accounts, groups, contacts, showAccounts, showGroups, showOffline)) {
                hasVisibleContacts = true;
            }
        }
        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 (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;
            ContactListGroupUtils.addContact(abstractContact, group, online, accounts, groups, contacts, showAccounts, showGroups);
        }
        // BUILD STRUCTURE //
        // Remove empty groups, sort and apply structure.
        items.clear();
        items.add(new ToolbarVO(context, this));
        if (hasVisibleContacts) {
            if (currentChatsState == ChatListState.recent) {
                // add recent chats
                int i = 0;
                for (AbstractContact contact : chatsGroup.getAbstractContacts()) {
                    if (i == MAX_RECENT_ITEMS - 1) {
                        if (getAllChatsSize() > MAX_RECENT_ITEMS)
                            items.add(ChatWithButtonVO.convert(contact, this));
                        else
                            items.add(ChatVO.convert(contact, this, null));
                    } else
                        items.add(ChatVO.convert(contact, this, null));
                    i++;
                }
                if (showAccounts) {
                    // boolean isFirst = items.isEmpty();
                    for (AccountConfiguration rosterAccount : accounts.values()) {
                        if (rosterAccount.getTotal() != 0) {
                            if (showGroups) {
                                createContactListWithAccountsAndGroups(items, rosterAccount, showEmptyGroups, comparator);
                            } else {
                                createContactListWithAccounts(items, rosterAccount, comparator);
                            }
                        } else {
                            AccountWithButtonsVO account = AccountWithButtonsVO.convert(rosterAccount, this);
                            ButtonVO button = ButtonVO.convert(rosterAccount, context.getString(R.string.contact_add), ButtonVO.ACTION_ADD_CONTACT);
                            account.addSubItem(button);
                            items.add(account);
                        }
                    }
                } else {
                    if (showGroups) {
                        createContactListWithGroups(items, showEmptyGroups, groups, comparator);
                    } else {
                        createContactList(items, contacts, comparator);
                    }
                }
            } else
                items.addAll(ChatVO.convert(chatsGroup.getAbstractContacts(), this, null));
        }
    } else {
        // Search
        final ArrayList<AbstractContact> baseEntities = getSearchResults(rosterContacts, comparator, abstractChats);
        items.clear();
        items.add(new CategoryVO(context.getString(R.string.category_title_contacts)));
        items.addAll(SettingsManager.contactsShowMessages() ? ExtContactVO.convert(baseEntities, this) : ContactVO.convert(baseEntities, this));
        hasVisibleContacts = baseEntities.size() > 0;
    }
    if (view != null)
        view.onContactListChanged(commonState, hasContacts, hasVisibleContacts, filterString != null);
    if (view != null) {
        if (items.size() == 1 && (filterString == null || filterString.isEmpty())) {
            if (currentChatsState == ChatListState.unread)
                view.showPlaceholder(context.getString(R.string.placeholder_no_unread));
            if (currentChatsState == ChatListState.archived)
                view.showPlaceholder(context.getString(R.string.placeholder_no_archived));
        } else
            view.hidePlaceholder();
        view.updateItems(items);
    }
}
Also used : CommonState(com.xabber.android.data.account.CommonState) RosterContact(com.xabber.android.data.roster.RosterContact) ChatWithButtonVO(com.xabber.android.presentation.ui.contactlist.viewobjects.ChatWithButtonVO) ButtonVO(com.xabber.android.presentation.ui.contactlist.viewobjects.ButtonVO) ArrayList(java.util.ArrayList) RoomChat(com.xabber.android.data.extension.muc.RoomChat) CategoryVO(com.xabber.android.presentation.ui.contactlist.viewobjects.CategoryVO) AccountJid(com.xabber.android.data.entity.AccountJid) ToolbarVO(com.xabber.android.presentation.ui.contactlist.viewobjects.ToolbarVO) ChatContact(com.xabber.android.data.message.ChatContact) AbstractChat(com.xabber.android.data.message.AbstractChat) IFlexible(eu.davidea.flexibleadapter.items.IFlexible) UserJid(com.xabber.android.data.entity.UserJid) GroupConfiguration(com.xabber.android.ui.adapter.contactlist.GroupConfiguration) TreeMap(java.util.TreeMap) AbstractContact(com.xabber.android.data.roster.AbstractContact) RoomContact(com.xabber.android.data.extension.muc.RoomContact) Collection(java.util.Collection) AccountConfiguration(com.xabber.android.ui.adapter.contactlist.AccountConfiguration) AccountWithButtonsVO(com.xabber.android.presentation.ui.contactlist.viewobjects.AccountWithButtonsVO) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

AccountJid (com.xabber.android.data.entity.AccountJid)74 UserJid (com.xabber.android.data.entity.UserJid)38 AccountItem (com.xabber.android.data.account.AccountItem)21 Jid (org.jxmpp.jid.Jid)13 Intent (android.content.Intent)11 ArrayList (java.util.ArrayList)11 View (android.view.View)10 NetworkException (com.xabber.android.data.NetworkException)9 AbstractChat (com.xabber.android.data.message.AbstractChat)7 Message (org.jivesoftware.smack.packet.Message)7 Presence (org.jivesoftware.smack.packet.Presence)7 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)6 TextView (android.widget.TextView)5 StatusMode (com.xabber.android.data.account.StatusMode)5 Resourcepart (org.jxmpp.jid.parts.Resourcepart)5 ImageView (android.widget.ImageView)4 AccountManager (com.xabber.android.data.account.AccountManager)4 RosterContact (com.xabber.android.data.roster.RosterContact)4 ContactVO (com.xabber.android.presentation.ui.contactlist.viewobjects.ContactVO)4 BarPainter (com.xabber.android.ui.color.BarPainter)4