📄 impscontactlistmanager.java
字号:
notifyBlockContact(c, block); } }; tx.sendRequest(request); } void extractBlockedContacts(Primitive response) { mBlockedList.clear(); PrimitiveElement blockList = response.getElement(ImpsTags.BlockList); if(blockList == null) { return; } PrimitiveElement entityList = blockList.getChild(ImpsTags.EntityList); if(entityList == null) { return; } for (PrimitiveElement entity : entityList.getChildren()) { if (entity.getTagName().equals(ImpsTags.UserID)) { ImpsAddress userAddress = new ImpsUserAddress(entity.getContents()); notifyBlockContact(new Contact(userAddress, userAddress.getScreenName()), true); } } } /** * Generate NickName element for a specific contact. * * @param contact the contact which provides the info of the NickName elem * @return */ private PrimitiveElement buildNickNameElem(Contact contact) { PrimitiveElement nickName = new PrimitiveElement(ImpsTags.NickName); nickName.addChild(ImpsTags.Name, contact.getName()); nickName.addChild(((ImpsAddress)contact.getAddress()).toPrimitiveElement()); return nickName; } ContactList extractContactList(Primitive response, final ImpsAddress address) { String screenName = address.getScreenName(); boolean isDefault = false; PrimitiveElement propertyElem = response.getElement(ImpsTags.ContactListProperties); if (null != propertyElem) { for (PrimitiveElement elem : propertyElem.getChildren()) { if (elem.getTagName().equals(ImpsTags.Property)) { String name = elem.getChildContents(ImpsTags.Name); String value = elem.getChildContents(ImpsTags.Value); if (name.equals(ImpsConstants.DisplayName)) { screenName = value; } else if (name.equals(ImpsTags.Default)) { isDefault = ImpsUtils.isTrue(value); } } } } PrimitiveElement nickListElem = response.getElement(ImpsTags.NickList); if (null == nickListElem) { return new ContactList(address, screenName, isDefault, null, this); } Vector<Contact> contacts = new Vector<Contact>(); for (PrimitiveElement elem : nickListElem.getChildren()) { String id = null; String name = null; String tag = elem.getTagName(); if (tag.equals(ImpsTags.NickName)) { id = elem.getChild(ImpsTags.UserID).getContents(); name = elem.getChild(ImpsTags.Name).getContents(); } else if (tag.equals(ImpsTags.UserID)){ id = elem.getContents(); } if (id != null) { Address contactAddress = new ImpsUserAddress(id); Contact c = getContact(contactAddress); if (c == null) { if (name == null) { name = contactAddress.getScreenName(); } c = new Contact(contactAddress, name); } contacts.add(c); } } return new ContactList(address, screenName, isDefault, contacts, this); } private class LoadContactsTransaction extends AsyncTransaction { Vector<ImpsContactListAddress> mListAddresses; LoadContactsTransaction() { super(mTransactionManager); mListAddresses = new Vector<ImpsContactListAddress>(); } @Override public void onResponseError(ImpsErrorInfo error) { notifyContactError(ContactListListener.ERROR_LOADING_LIST, error, null, null); } @Override public void onResponseOk(Primitive response) { mContactLists.clear(); mListAddresses = extractListAddresses(response); if (!mListAddresses.isEmpty()) { fetchContacts(); } else { onContactListsLoaded(); } } void startGetContactLists() { Primitive getListRequest = new Primitive(ImpsTags.GetList_Request); sendRequest(getListRequest); } private void fetchContacts() { ImpsContactListAddress fisrtListAddress = mListAddresses.firstElement(); loadContactsOfListAsync(fisrtListAddress, new LoadListCompletion()); } private final class LoadListCompletion implements AsyncCompletion { private int listIndex; LoadListCompletion() { listIndex = 0; } public void onComplete() { processResult(null); } public void onError(ImErrorInfo error) { processResult(error); } private void processResult(ImErrorInfo error) { ImpsAddress addr = mListAddresses.get(listIndex); if (error == null) { notifyContactListLoaded(getContactList(addr)); } else { notifyContactError(ContactListListener.ERROR_LOADING_LIST, error, addr.getScreenName(), null); } listIndex++; if (listIndex < mListAddresses.size()) { loadContactsOfListAsync(mListAddresses.get(listIndex), this); } else { onContactListsLoaded(); } } } } void onContactListsLoaded() { notifyContactListsLoaded(); // notify the pending subscription requests received before contact lists has been loaded. SubscriptionRequestListener listener = getSubscriptionRequestListener(); if (mSubscriptionRequests != null && listener != null) { for (Contact c : mSubscriptionRequests) { listener.onSubScriptionRequest(c); } } ((ImpsChatSessionManager) mConnection.getChatSessionManager()).start(); } @Override protected void doAddContactToListAsync(String addressStr, ContactList list) throws ImException { ImpsUserAddress address = new ImpsUserAddress(addressStr); Contact contact; if (getContact(address) != null) { contact = getContact(address); } else { contact = new Contact(address, address.getScreenName()); } if (isBlocked(contact)) { throw new ImException(ImErrorInfo.CANT_ADD_BLOCKED_CONTACT, "Contact has been blocked"); } addContactToListAsync(contact, list); } private void addContactToListAsync(final Contact contact, final ContactList list) { final ArrayList<Contact> contacts = new ArrayList<Contact>(); contacts.add(contact); updateContactListAsync(list, contacts, null, null, new AsyncCompletion(){ public void onComplete() { notifyContactListUpdated(list, ContactListListener.LIST_CONTACT_ADDED, contact); AsyncCompletion subscribeCompletion = new AsyncCompletion(){ public void onComplete() {} public void onError(ImErrorInfo error) { notifyContactError( ContactListListener.ERROR_RETRIEVING_PRESENCE, error, list.getName(), contact); } }; if (mAllowAutoSubscribe) { // XXX Send subscription again after add contact to make sure we // can get the presence notification. Although the we set // AutoSubscribe True when subscribe presence after load contacts, // the server might not send presence notification. subscribeToListAsync(list, subscribeCompletion); } else { subscribeToContactsAsync(contacts, subscribeCompletion); } } public void onError(ImErrorInfo error) { notifyContactError(ContactListListener.ERROR_ADDING_CONTACT, error, list.getName(), contact); } }); } @Override protected void doRemoveContactFromListAsync(final Contact contact, final ContactList list) { ArrayList<Contact> contacts = new ArrayList<Contact>(); contacts.add(contact); updateContactListAsync(list, null, contacts, null, new AsyncCompletion(){ public void onComplete() { ImpsLog.log("removed contact"); notifyContactListUpdated(list, ContactListListener.LIST_CONTACT_REMOVED, contact); if (!mAllowAutoSubscribe) { unsubscribeToContactAsync(contact, new AsyncCompletion(){ public void onComplete() {} public void onError(ImErrorInfo error) { // don't bother to alert this error since the // contact has already been removed. ImpsLog.log("Warning: unsubscribing contact presence failed"); } }); } } public void onError(ImErrorInfo error) { ImpsLog.log("remove contact error:" + error); notifyContactError(ContactListListener.ERROR_REMOVING_CONTACT, error, list.getName(), contact); } }); } @Override protected void setListNameAsync(final String name, final ContactList list) { updateContactListAsync(list, null, null, name, new AsyncCompletion(){ public void onComplete() { notifyContactListNameUpdated(list, name); } public void onError(ImErrorInfo error) { notifyContactError(ContactListListener.ERROR_RENAMING_LIST, error, list.getName(), null); } }); } private void updateContactListAsync(final ContactList list, final ArrayList<Contact> contactsToAdd, final ArrayList<Contact> contactsToRemove, final String listName, AsyncCompletion completion) { Primitive request = buildListManageRequest(list, contactsToAdd, contactsToRemove, listName); SimpleAsyncTransaction tx = new SimpleAsyncTransaction(mTransactionManager, completion); tx.sendRequest(request); } String getPropertyValue(String propertyName, PrimitiveElement properties) { for (PrimitiveElement property : properties.getChildren(ImpsTags.Property)) { if (propertyName.equals(property.getChildContents(ImpsTags.Name))) { return property.getChildContents(ImpsTags.Value); } } return null; } void reset() { setState(LISTS_NOT_LOADED); } @Override protected ImConnection getConnection() { return mConnection; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -