📄 contactlistmanager.java
字号:
if (null == list) { throw new ImException(ImErrorInfo.CONTACT_LIST_NOT_FOUND, "Contact list doesn't exist"); } doDeleteContactListAsync(list); } public void blockContactAsync(Contact contact) throws ImException { blockContactAsync(contact.getAddress().getFullName()); } /** * Blocks a certain Contact. The contact will be removed from any * ContactList after be blocked. If the contact has already been blocked, * the method does nothing. * * @param address the address of the contact to block. * @throws ImException if an error occurs */ public void blockContactAsync(String address) throws ImException { checkState(); if(isBlocked(address)){ return; } doBlockContactAsync(address, true); } public void unblockContactAsync(Contact contact) throws ImException { unblockContactAsync(contact.getAddress().getFullName()); } /** * Unblock a certain contact. It will removes the contact from the blocked * list and allows the contact to send message or invitation to the client * again. If the contact is not blocked on the client, this method does * nothing. Whether the unblocked contact will be added to the ContactList * it belongs before blocked or not depends on the underlying protocol * implementation. * * @param address the address of the contact to unblock. * @throws ImException if the current state is illegal */ public void unblockContactAsync(String address) throws ImException { checkState(); if(!isBlocked(address)) { return; } doBlockContactAsync(address, false); } protected void addContactToListAsync(String address, ContactList list) throws ImException { checkState(); doAddContactToListAsync(address, list); } protected void removeContactFromListAsync(Contact contact, ContactList list) throws ImException { checkState(); doRemoveContactFromListAsync(contact, list); } /** * Gets a unmodifiable list of blocked contacts. * * @return a unmodifiable list of blocked contacts. * @throws ImException */ public List<Contact> getBlockedList() throws ImException { checkState(); return Collections.unmodifiableList(mBlockedList); } /** * Checks if a contact is blocked. * * @param contact the contact. * @return true if it's blocked, false otherwise. * @throws ImException if contacts has not been loaded. */ public boolean isBlocked(Contact contact) throws ImException { return isBlocked(contact.getAddress().getFullName()); } /** * Checks if a contact is blocked. * * @param address the address of the contact. * @return true if it's blocked, false otherwise. * @throws ImException if contacts has not been loaded. */ public synchronized boolean isBlocked(String address) throws ImException { if(mState < BLOCKED_LIST_LOADED) { throw new ImException(ImErrorInfo.ILLEGAL_CONTACT_LIST_MANAGER_STATE, "Blocked list hasn't been loaded"); } for(Contact c : mBlockedList) { if(c.getAddress().getFullName().equals(address)){ return true; } } return false; } /** * Check the state of the ContactListManager. Only the LIST_LOADED state * is permitted. * * @throws ImException if the current state is not LIST_LOADED */ protected void checkState() throws ImException { if (getConnection().getState() != ImConnection.LOGGED_IN) { throw new ImException(ImErrorInfo.CANT_CONNECT_TO_SERVER, "Can't connect to server"); } if (getState() != LISTS_LOADED) { throw new ImException(ImErrorInfo.ILLEGAL_CONTACT_LIST_MANAGER_STATE, "Illegal contact list manager state"); } } /** * Load the contact lists from the server. This method will normally called * after the user logged in to get the initial/saved contact lists from * server. After called once, this method should not be called again. */ public abstract void loadContactListsAsync(); public abstract void approveSubscriptionRequest(String contact); public abstract void declineSubscriptionRequest(String contact); protected abstract ImConnection getConnection(); /** * Block or unblock a contact. * * @param address * the address of the contact to block or unblock. * @param block * <code>true</code> to block the contact; <code>false</code> * to unblock the contact. */ protected abstract void doBlockContactAsync(String address, boolean block); protected abstract void doCreateContactListAsync(String name, Collection<Contact> contacts, boolean isDefault); protected abstract void doDeleteContactListAsync(ContactList list); /** * Notify that the presence of the contact has been updated * * @param contacts the contacts who have updated presence information */ protected void notifyContactsPresenceUpdated(Contact[] contacts) { for (ContactListListener listener : mContactListListeners) { listener.onContactsPresenceUpdate(contacts); } } /** * Notify that a contact list related error has been raised. * * @param type the type of the error * @param error the raised error * @param listName the list name, if any, associated with the error * @param contact the contact, if any, associated with the error */ protected void notifyContactError(int type, ImErrorInfo error, String listName, Contact contact) { for (ContactListListener listener : mContactListListeners) { listener.onContactError(type, error, listName, contact); } } /** * Notify that a contact list has been loaded * * @param list the loaded list */ protected void notifyContactListLoaded(ContactList list) { for (ContactListListener listener : mContactListListeners) { listener.onContactChange(ContactListListener.LIST_LOADED, list, null); } } /** * Notify that all contact lists has been loaded */ protected void notifyContactListsLoaded() { setState(LISTS_LOADED); for (ContactListListener listener : mContactListListeners) { listener.onAllContactListsLoaded(); } } /** * Notify that a contact has been added to or removed from a list. * * @param list the updated contact list * @param type the type of the update * @param contact the involved contact, null if no contact involved. */ protected void notifyContactListUpdated(ContactList list, int type, Contact contact) { synchronized (this) { if (type == ContactListListener.LIST_CONTACT_ADDED) { list.insertToCache(contact); } else if (type == ContactListListener.LIST_CONTACT_REMOVED) { list.removeFromCache(contact); } } for (ContactListListener listener : mContactListListeners) { listener.onContactChange(type, list, contact); } } /** * Notify that the name of the specified contact list has been updated. * * @param list * @param name the new name of the list */ protected void notifyContactListNameUpdated(ContactList list, String name) { list.mName = name; for (ContactListListener listener : mContactListListeners) { listener.onContactChange(ContactListListener.LIST_RENAMED, list, null); } } /** * Notify that a contact list has been created. * * @param list the created list */ protected void notifyContactListCreated(ContactList list) { synchronized (this) { if (list.isDefault()) { for (ContactList l : mContactLists) { l.setDefault(false); } mDefaultContactList = list; } mContactLists.add(list); } for (ContactListListener listener : mContactListListeners) { listener.onContactChange(ContactListListener.LIST_CREATED, list, null); } } /** * Notify that a contact list has been deleted * * @param list the deleted list */ protected void notifyContactListDeleted(ContactList list) { synchronized(this) { mContactLists.remove(list); if (list.isDefault() && mContactLists.size() > 0) { mContactLists.get(0).setDefault(true); } } for (ContactListListener listener : mContactListListeners) { listener.onContactChange(ContactListListener.LIST_DELETED, list, null); } } /** * Notify that a contact has been blocked/unblocked. * * @param contact the blocked/unblocked contact */ protected void notifyBlockContact(Contact contact, boolean blocked) { synchronized (this) { if (blocked) { mBlockedList.add(contact); } else { mBlockedList.remove(contact); } } for (ContactListListener listener : mContactListListeners) { listener.onContactChange(blocked ? ContactListListener.CONTACT_BLOCKED : ContactListListener.CONTACT_UNBLOCKED, null, contact); } } protected abstract void doAddContactToListAsync(String address, ContactList list) throws ImException; protected abstract void doRemoveContactFromListAsync(Contact contact, ContactList list); protected abstract void setListNameAsync(String name, ContactList list);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -