Search in sources :

Example 1 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method loadPhoneContacts.

public void loadPhoneContacts() {
    mContactMergerExecutor.execute(new Runnable() {

        @Override
        public void run() {
            PhoneHelper.loadPhoneContacts(XmppConnectionService.this, new OnPhoneContactsLoadedListener() {

                @Override
                public void onPhoneContactsLoaded(List<Bundle> phoneContacts) {
                    Log.d(Config.LOGTAG, "start merging phone contacts with roster");
                    for (Account account : accounts) {
                        List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts();
                        for (Bundle phoneContact : phoneContacts) {
                            Jid jid;
                            try {
                                jid = Jid.fromString(phoneContact.getString("jid"));
                            } catch (final InvalidJidException e) {
                                continue;
                            }
                            final Contact contact = account.getRoster().getContact(jid);
                            String systemAccount = phoneContact.getInt("phoneid") + "#" + phoneContact.getString("lookup");
                            contact.setSystemAccount(systemAccount);
                            boolean needsCacheClean = contact.setPhotoUri(phoneContact.getString("photouri"));
                            needsCacheClean |= contact.setSystemName(phoneContact.getString("displayname"));
                            if (needsCacheClean) {
                                getAvatarService().clear(contact);
                            }
                            withSystemAccounts.remove(contact);
                        }
                        for (Contact contact : withSystemAccounts) {
                            contact.setSystemAccount(null);
                            boolean needsCacheClean = contact.setPhotoUri(null);
                            needsCacheClean |= contact.setSystemName(null);
                            if (needsCacheClean) {
                                getAvatarService().clear(contact);
                            }
                        }
                    }
                    Log.d(Config.LOGTAG, "finished merging phone contacts");
                    mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false, true));
                    updateAccountUi();
                }
            });
        }
    });
}
Also used : Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) Bundle(android.os.Bundle) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) OnPhoneContactsLoadedListener(de.pixart.messenger.utils.OnPhoneContactsLoadedListener) Contact(de.pixart.messenger.entities.Contact)

Example 2 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method onOtrSessionEstablished.

public void onOtrSessionEstablished(Conversation conversation) {
    final Account account = conversation.getAccount();
    final Session otrSession = conversation.getOtrSession();
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + " otr session established with " + conversation.getJid() + "/" + otrSession.getSessionID().getUserID());
    conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {

        @Override
        public void onMessageFound(Message message) {
            SessionID id = otrSession.getSessionID();
            try {
                message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
            } catch (InvalidJidException e) {
                return;
            }
            if (message.needsUploading()) {
                mJingleConnectionManager.createNewConnection(message);
            } else {
                MessagePacket outPacket = mMessageGenerator.generateOtrChat(message);
                if (outPacket != null) {
                    mMessageGenerator.addDelay(outPacket, message.getTimeSent());
                    message.setStatus(Message.STATUS_SEND);
                    databaseBackend.updateMessage(message);
                    sendMessagePacket(account, outPacket);
                }
            }
            updateConversationUi();
        }
    });
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Account(de.pixart.messenger.entities.Account) XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation) SessionID(net.java.otr4j.session.SessionID) Session(net.java.otr4j.session.Session)

Example 3 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class Message method fromCursor.

public static Message fromCursor(Cursor cursor, Conversation conversation) {
    Jid jid;
    try {
        String value = cursor.getString(cursor.getColumnIndex(COUNTERPART));
        if (value != null) {
            jid = Jid.fromString(value, true);
        } else {
            jid = null;
        }
    } catch (InvalidJidException e) {
        jid = null;
    } catch (IllegalStateException e) {
        // message too long?
        return null;
    }
    Jid trueCounterpart;
    try {
        String value = cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART));
        if (value != null) {
            trueCounterpart = Jid.fromString(value, true);
        } else {
            trueCounterpart = null;
        }
    } catch (InvalidJidException e) {
        trueCounterpart = null;
    }
    return new Message(conversation, cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(CONVERSATION)), jid, trueCounterpart, cursor.getString(cursor.getColumnIndex(BODY)), cursor.getLong(cursor.getColumnIndex(TIME_SENT)), cursor.getInt(cursor.getColumnIndex(ENCRYPTION)), cursor.getInt(cursor.getColumnIndex(STATUS)), cursor.getInt(cursor.getColumnIndex(TYPE)), cursor.getInt(cursor.getColumnIndex(CARBON)) > 0, cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)), cursor.getString(cursor.getColumnIndex(RELATIVE_FILE_PATH)), cursor.getString(cursor.getColumnIndex(SERVER_MSG_ID)), cursor.getString(cursor.getColumnIndex(FINGERPRINT)), cursor.getInt(cursor.getColumnIndex(READ)) > 0, cursor.getString(cursor.getColumnIndex(EDITED)), cursor.getInt(cursor.getColumnIndex(OOB)) > 0, cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)), ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))), cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0);
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 4 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class XmppActivity method selectPresence.

public void selectPresence(final Conversation conversation, final PresenceSelector.OnPresenceSelected listener) {
    final Contact contact = conversation.getContact();
    if (conversation.hasValidOtrSession()) {
        SessionID id = conversation.getOtrSession().getSessionID();
        Jid jid;
        try {
            jid = Jid.fromString(id.getAccountID() + "/" + id.getUserID());
        } catch (InvalidJidException e) {
            jid = null;
        }
        conversation.setNextCounterpart(jid);
        listener.onPresenceSelected();
    } else if (!contact.showInRoster()) {
        showAddToRosterDialog(conversation);
    } else {
        final Presences presences = contact.getPresences();
        if (presences.size() == 0) {
            if (!contact.getOption(Contact.Options.TO) && !contact.getOption(Contact.Options.ASKING) && contact.getAccount().getStatus() == Account.State.ONLINE) {
                showAskForPresenceDialog(contact);
            } else if (!contact.getOption(Contact.Options.TO) || !contact.getOption(Contact.Options.FROM)) {
                PresenceSelector.warnMutualPresenceSubscription(this, conversation, listener);
            } else {
                conversation.setNextCounterpart(null);
                listener.onPresenceSelected();
            }
        } else if (presences.size() == 1) {
            String presence = presences.toResourceArray()[0];
            try {
                conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
            } catch (InvalidJidException e) {
                conversation.setNextCounterpart(null);
            }
            listener.onPresenceSelected();
        } else {
            PresenceSelector.showPresenceSelectionDialog(this, conversation, listener);
        }
    }
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) Presences(de.pixart.messenger.entities.Presences) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) SessionID(net.java.otr4j.session.SessionID) Contact(de.pixart.messenger.entities.Contact)

Example 5 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class XmppActivity method showPresenceSelectionDialog.

private void showPresenceSelectionDialog(Presences presences, final Conversation conversation, final OnPresenceSelected listener) {
    final Contact contact = conversation.getContact();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.choose_presence));
    final String[] resourceArray = presences.toResourceArray();
    Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
    final Map<String, String> resourceTypeMap = typeAndName.first;
    final Map<String, String> resourceNameMap = typeAndName.second;
    final String[] readableIdentities = new String[resourceArray.length];
    final AtomicInteger selectedResource = new AtomicInteger(0);
    for (int i = 0; i < resourceArray.length; ++i) {
        String resource = resourceArray[i];
        if (resource.equals(contact.getLastResource())) {
            selectedResource.set(i);
        }
        String type = resourceTypeMap.get(resource);
        String name = resourceNameMap.get(resource);
        if (type != null) {
            if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
                readableIdentities[i] = PresenceSelector.translateType(this, type);
            } else if (name != null) {
                if (Collections.frequency(resourceNameMap.values(), name) == 1 || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
                    readableIdentities[i] = PresenceSelector.translateType(this, type) + "  (" + name + ")";
                } else {
                    readableIdentities[i] = PresenceSelector.translateType(this, type) + " (" + name + " / " + resource + ")";
                }
            } else {
                readableIdentities[i] = PresenceSelector.translateType(this, type) + " (" + resource + ")";
            }
        } else {
            readableIdentities[i] = resource;
        }
    }
    builder.setSingleChoiceItems(readableIdentities, selectedResource.get(), (dialog, which) -> selectedResource.set(which));
    builder.setNegativeButton(R.string.cancel, null);
    builder.setPositiveButton(R.string.ok, (dialog, which) -> {
        try {
            Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]);
            conversation.setNextCounterpart(next);
        } catch (InvalidJidException e) {
            conversation.setNextCounterpart(null);
        }
        listener.onPresenceSelected();
    });
    builder.create().show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Jid(de.pixart.messenger.xmpp.jid.Jid) Builder(android.app.AlertDialog.Builder) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Builder(android.app.AlertDialog.Builder) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Contact(de.pixart.messenger.entities.Contact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Aggregations

InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)33 Jid (de.pixart.messenger.xmpp.jid.Jid)25 Account (de.pixart.messenger.entities.Account)14 Conversation (de.pixart.messenger.entities.Conversation)13 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)5 AlertDialog (android.support.v7.app.AlertDialog)4 Contact (de.pixart.messenger.entities.Contact)4 Message (de.pixart.messenger.entities.Message)4 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)4 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)3 ArrayList (java.util.ArrayList)3 SuppressLint (android.annotation.SuppressLint)2 Bundle (android.os.Bundle)2 SpannableString (android.text.SpannableString)2 Pair (android.util.Pair)2 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)2 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)2