use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.
the class ContactListActivity 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(UserJid user, String text) {
UserJid bareAddress = user.getBareUserJid();
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();
Collection<AccountJid> enabledAccounts = AccountManager.getInstance().getEnabledAccounts();
RosterManager rosterManager = RosterManager.getInstance();
for (AccountJid accountJid : enabledAccounts) {
RosterContact rosterContact = rosterManager.getRosterContact(accountJid, user);
if (rosterContact != null && rosterContact.isEnabled()) {
entities.add(rosterContact);
}
}
if (entities.size() == 1) {
openChat(entities.get(0), text);
return;
}
if (enabledAccounts.isEmpty()) {
return;
}
if (enabledAccounts.size() == 1) {
openChat(rosterManager.getBestContact(enabledAccounts.iterator().next(), bareAddress), text);
return;
}
AccountChooseDialogFragment.newInstance(bareAddress, text).show(getFragmentManager(), "OPEN_WITH_ACCOUNT");
}
use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.
the class ContactListActivity method showContactSubscriptionDialog.
private void showContactSubscriptionDialog() {
Intent intent = getIntent();
AccountJid account = getRoomInviteAccount(intent);
UserJid user = getRoomInviteUser(intent);
if (account != null && user != null) {
ContactSubscriptionDialog.newInstance(account, user).show(getFragmentManager(), ContactSubscriptionDialog.class.getName());
}
}
use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.
the class ContactListActivity method showMucInviteDialog.
private void showMucInviteDialog() {
Intent intent = getIntent();
AccountJid account = getRoomInviteAccount(intent);
UserJid user = getRoomInviteUser(intent);
if (account != null && user != null) {
MucInviteDialog.newInstance(account, user).show(getFragmentManager(), MucInviteDialog.class.getName());
}
}
use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.
the class MessageManager method onStanza.
@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
if (stanza.getFrom() == null) {
return;
}
AccountJid account = connection.getAccount();
final UserJid user;
try {
user = UserJid.from(stanza.getFrom()).getBareUserJid();
} catch (UserJid.UserJidCreateException e) {
return;
}
boolean processed = false;
for (AbstractChat chat : chats.getNested(account.toString()).values()) {
if (chat.onPacket(user, stanza, false)) {
processed = true;
break;
}
}
final AbstractChat chat = getChat(account, user);
if (chat != null && stanza instanceof Message) {
if (chat.isPrivateMucChat() && !chat.isPrivateMucChatAccepted()) {
if (mucPrivateChatRequestProvider.get(chat.getAccount(), chat.getUser()) == null) {
mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
}
}
return;
}
if (!processed && stanza instanceof Message) {
final Message message = (Message) stanza;
final String body = message.getBody();
if (body == null) {
return;
}
// check for spam
if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, user) == null) {
String thread = ((Message) stanza).getThread();
if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.authCaptcha) {
// check if this message is captcha-answer
Captcha captcha = CaptchaManager.getInstance().getCaptcha(account, user);
if (captcha != null) {
// attempt limit overhead
if (captcha.getAttemptCount() > CaptchaManager.CAPTCHA_MAX_ATTEMPT_COUNT) {
// remove this captcha
CaptchaManager.getInstance().removeCaptcha(account, user);
// discard subscription
try {
PresenceManager.getInstance().discardSubscription(account, user);
} catch (NetworkException e) {
e.printStackTrace();
}
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_many_attempts));
return;
}
if (body.equals(captcha.getAnswer())) {
// captcha solved successfully
// remove this captcha
CaptchaManager.getInstance().removeCaptcha(account, user);
// show auth
PresenceManager.getInstance().handleSubscriptionRequest(account, user);
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_correct));
return;
} else {
// captcha solved unsuccessfully
// increment attempt count
captcha.setAttemptCount(captcha.getAttemptCount() + 1);
// send warning-message
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_incorrect));
return;
}
} else {
// no captcha exist and user not from roster
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
// and skip received message as spam
return;
}
} else {
// if message from not-roster user
// send a warning message to sender
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
// and skip received message as spam
return;
}
}
if (message.getType() == Message.Type.chat && MUCManager.getInstance().hasRoom(account, user.getJid().asEntityBareJidIfPossible())) {
try {
createPrivateMucChat(account, user.getJid().asFullJidIfPossible()).onPacket(user, stanza, false);
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
return;
}
for (ExtensionElement packetExtension : message.getExtensions()) {
if (packetExtension instanceof MUCUser) {
return;
}
}
createChat(account, user).onPacket(user, stanza, false);
}
}
use of com.xabber.android.data.entity.AccountJid in project xabber-android by redsolution.
the class MessageManager method setVisibleChat.
/**
* Sets currently visible chat.
*/
public void setVisibleChat(BaseEntity visibleChat) {
AbstractChat chat = getChat(visibleChat.getAccount(), visibleChat.getUser());
if (chat == null) {
chat = createChat(visibleChat.getAccount(), visibleChat.getUser());
} else {
final AccountJid account = chat.getAccount();
final UserJid user = chat.getUser();
MessageDatabaseManager.getInstance().getRealmUiThread().executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<MessageItem> unreadMessages = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, account.toString()).equalTo(MessageItem.Fields.USER, user.toString()).equalTo(MessageItem.Fields.READ, false).findAll();
List<MessageItem> unreadMessagesList = new ArrayList<>(unreadMessages);
for (MessageItem messageItem : unreadMessagesList) {
messageItem.setRead(true);
}
}
});
}
this.visibleChat = chat;
}
Aggregations