use of org.awesomeapp.messenger.model.Presence in project Zom-Android by zom.
the class XmppConnection method setPresence.
private void setPresence(Jid from, int presenceType) {
Presence p = null;
Contact contact = mContactListManager.getContact(from.asBareJid().toString());
if (contact == null)
return;
p = new Presence(presenceType, "", null, null, Presence.CLIENT_TYPE_MOBILE);
if (from.hasResource())
p.setResource(from.getResourceOrEmpty().toString());
if (presenceType == Presence.AVAILABLE)
p.setLastSeen(new Date());
else if (contact.getPresence() != null)
p.setLastSeen(contact.getPresence().getLastSeen());
contact.setPresence(p);
Collection<Contact> contactsUpdate = new ArrayList<Contact>();
contactsUpdate.add(contact);
mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
}
use of org.awesomeapp.messenger.model.Presence in project Zom-Android by zom.
the class XmppConnection method handleChatState.
private void handleChatState(String from, String chatStateXml) throws RemoteException {
Presence p = null;
Contact contact = mContactListManager.getContact(from);
if (contact == null)
return;
boolean isTyping = false;
// handle is-typing, probably some indication on screen
if (chatStateXml.contains("'error'") || chatStateXml.contains("'cancel'")) {
// do nothing
} else if (chatStateXml.contains(ChatState.active.toString())) {
p = new Presence(Presence.AVAILABLE, "", null, null, Presence.CLIENT_TYPE_MOBILE);
p.setLastSeen(new Date());
} else if (chatStateXml.contains(ChatState.inactive.toString())) {
p = new Presence(Presence.AWAY, "", null, null, Presence.CLIENT_TYPE_MOBILE);
p.setLastSeen(new Date());
} else if (chatStateXml.contains(ChatState.composing.toString())) {
p = new Presence(Presence.AVAILABLE, "", null, null, Presence.CLIENT_TYPE_MOBILE);
isTyping = true;
p.setLastSeen(new Date());
} else if (chatStateXml.contains(ChatState.inactive.toString()) || chatStateXml.contains(ChatState.paused.toString())) {
} else if (chatStateXml.contains(ChatState.gone.toString())) {
}
IChatSession csa = mSessionManager.getAdapter().getChatSession(from);
if (csa != null)
csa.setContactTyping(contact, isTyping);
if (p != null) {
String[] presenceParts = from.split("/");
if (presenceParts.length > 1)
p.setResource(presenceParts[1]);
contact.setPresence(p);
Collection<Contact> contactsUpdate = new ArrayList<Contact>();
contactsUpdate.add(contact);
mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
}
}
use of org.awesomeapp.messenger.model.Presence in project Zom-Android by zom.
the class XmppConnection method makePresencePacket.
private org.jivesoftware.smack.packet.Presence makePresencePacket(Presence presence) {
String statusText = presence.getStatusText();
org.jivesoftware.smack.packet.Presence.Type type = org.jivesoftware.smack.packet.Presence.Type.available;
org.jivesoftware.smack.packet.Presence.Mode mode = org.jivesoftware.smack.packet.Presence.Mode.available;
int priority = mPriority;
final int status = presence.getStatus();
if (status == Presence.AWAY) {
priority = 10;
mode = org.jivesoftware.smack.packet.Presence.Mode.away;
} else if (status == Presence.IDLE) {
priority = 15;
mode = org.jivesoftware.smack.packet.Presence.Mode.away;
} else if (status == Presence.DO_NOT_DISTURB) {
priority = 5;
mode = org.jivesoftware.smack.packet.Presence.Mode.dnd;
} else if (status == Presence.OFFLINE) {
priority = 0;
type = org.jivesoftware.smack.packet.Presence.Type.unavailable;
statusText = "Offline";
}
// The user set priority is the maximum allowed
if (priority > mPriority)
priority = mPriority;
org.jivesoftware.smack.packet.Presence packet = new org.jivesoftware.smack.packet.Presence(type, statusText, priority, mode);
try {
byte[] avatar = DatabaseUtils.getAvatarBytesFromAddress(mContext.getContentResolver(), mUser.getAddress().getBareAddress());
if (avatar != null) {
VCardTempXUpdatePresenceExtension vcardExt = new VCardTempXUpdatePresenceExtension(avatar);
packet.addExtension(vcardExt);
}
} catch (Exception e) {
debug(TAG, "error upading presence with avatar hash", e);
}
return packet;
}
use of org.awesomeapp.messenger.model.Presence in project Zom-Android by zom.
the class XmppConnection method handlePresenceChanged.
private Contact handlePresenceChanged(org.jivesoftware.smack.packet.Presence presence) {
if (// our presence isn't really valid
presence == null)
return null;
if (TextUtils.isEmpty(presence.getFrom()))
return null;
if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.error)
return null;
if (// ignore presence from yourself
presence.getFrom().toString().startsWith(mUser.getAddress().getBareAddress()))
return null;
XmppAddress xaddress = new XmppAddress(presence.getFrom().toString());
Presence p = new Presence(parsePresence(presence), presence.getStatus(), null, null, Presence.CLIENT_TYPE_MOBILE, null, xaddress.getResource());
// this is only persisted in memory
p.setPriority(presence.getPriority());
if (mContactListManager == null)
// we may have logged out
return null;
Contact contact = mContactListManager.getContact(xaddress.getBareAddress());
BareJid jid = presence.getFrom().asBareJid();
if (presence.getFrom().hasResource())
p.setResource(presence.getFrom().getResourceOrEmpty().toString());
if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.subscribe) {
debug(TAG, "got subscribe request: " + presence.getFrom());
try {
handleSubscribeRequest(presence.getFrom());
} catch (Exception e) {
Log.e(TAG, "remote exception on subscription handling", e);
}
} else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.subscribed) {
debug(TAG, "got subscribed confirmation: " + presence.getFrom());
try {
if (contact == null) {
XmppAddress xAddr = new XmppAddress(presence.getFrom().toString());
contact = new Contact(xAddr, xAddr.getUser(), Imps.Contacts.TYPE_NORMAL);
mContactListManager.doAddContactToListAsync(contact, getContactListManager().getDefaultContactList(), false);
}
if (contact.getSubscriptionStatus() == Imps.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING) {
mContactListManager.approveSubscriptionRequest(contact);
mContactListManager.getSubscriptionRequestListener().onSubscriptionApproved(contact, mProviderId, mAccountId);
}
if (getOmemo().getManager().contactSupportsOmemo(jid)) {
getOmemo().getManager().requestDeviceListUpdateFor(jid);
getOmemo().getManager().buildSessionsWith(jid);
}
} catch (Exception e) {
Log.e(TAG, "remote exception on subscription handling", e);
}
} else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.unsubscribe) {
debug(TAG, "got unsubscribe request: " + presence.getFrom());
// TBD how to handle this
// mContactListManager.getSubscriptionRequestListener().onUnSubScriptionRequest(contact);
} else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.unsubscribed) {
debug(TAG, "got unsubscribe request: " + presence.getFrom());
try {
mContactListManager.getSubscriptionRequestListener().onSubscriptionDeclined(contact, mProviderId, mAccountId);
} catch (RemoteException e) {
Log.e(TAG, "remote exception on subscription handling", e);
}
}
// this is typical presence, let's get the latest/highest priority
debug(TAG, "got presence: " + presence.getFrom() + "=" + presence.getType());
if (contact != null) {
if (contact.getPresence() != null) {
Presence pOld = contact.getPresence();
if (pOld != null && pOld.getLastSeen() != null && p.getLastSeen() == null)
p.setLastSeen(pOld.getLastSeen());
if (pOld == null || pOld.getResource() == null) {
contact.setPresence(p);
} else if (// if the same resource as the existing one, then update it
pOld.getResource() != null && pOld.getResource().equals(p.getResource())) {
contact.setPresence(p);
} else if (// if priority is higher, then override
p.getPriority() >= pOld.getPriority()) {
contact.setPresence(p);
}
} else
contact.setPresence(p);
if (contact.getPresence().getLastSeen() == null) {
getLastSeen(contact);
}
ExtensionElement packetExtension = presence.getExtension("x", "vcard-temp:x:update");
if (packetExtension != null) {
StandardExtensionElement o = (StandardExtensionElement) packetExtension;
String hash = o.getAttributeValue("photo");
if (hash != null) {
boolean hasMatches = DatabaseUtils.doesAvatarHashExist(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress(), hash);
if (// we must reload
!hasMatches)
qAvatar.put(contact.getAddress().getAddress(), hash);
} else {
for (StandardExtensionElement element : o.getElements()) {
if (element.getElementName().equals("photo")) {
hash = element.getText();
if (hash != null) {
boolean hasMatches = DatabaseUtils.doesAvatarHashExist(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress(), hash);
if (// we must reload
!hasMatches)
qAvatar.put(contact.getAddress().getAddress(), hash);
}
break;
}
}
}
} else {
boolean hasAvatar = DatabaseUtils.hasAvatarContact(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress());
if (!hasAvatar) {
qAvatar.put(contact.getAddress().getAddress(), "");
}
}
}
return contact;
}
use of org.awesomeapp.messenger.model.Presence in project Zom-Android by zom.
the class XmppConnection method initPresenceProcessor.
private void initPresenceProcessor() {
mTimerPresence = new Timer();
mTimerPresence.schedule(new TimerTask() {
public void run() {
if (qPresence.size() > 0) {
try {
ContactList cList = getContactListManager().getDefaultContactList();
if (cList == null)
// not ready yet
return;
} catch (Exception e) {
// not ready yet
return;
}
Map<String, Contact> alUpdate = new HashMap<String, Contact>();
org.jivesoftware.smack.packet.Presence p = null;
Contact contact = null;
final int maxBatch = 20;
while (qPresence.peek() != null && alUpdate.size() < maxBatch) {
p = qPresence.poll();
contact = handlePresenceChanged(p);
if (contact != null) {
alUpdate.put(contact.getAddress().getBareAddress(), contact);
}
}
if (alUpdate.size() > 0) {
loadVCardsAsync();
// Log.d(TAG,"XMPP processed presence q=" + alUpdate.size());
Collection<Contact> contactsUpdate = alUpdate.values();
if (mContactListManager != null)
mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
}
}
}
}, 500, 500);
}
Aggregations