📄 contactlistmanager.java
字号:
/* * Copyright (C) 2007-2008 Esmertec AG. * Copyright (C) 2007-2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.im.engine;import java.util.Collection;import java.util.Collections;import java.util.List;import java.util.Vector;import java.util.concurrent.CopyOnWriteArrayList;/** * ContactListManager manages the creating, removing and retrieving contact * lists. */public abstract class ContactListManager { /** * ContactListManager state that indicates the contact list(s) has not been loaded. */ public static final int LISTS_NOT_LOADED = 0; /** * ContactListManager state that indicates the contact list(s) is loading. */ public static final int LISTS_LOADING = 1; /** * ContactListManager state that indicates the blocked list has been loaded. */ public static final int BLOCKED_LIST_LOADED = 2; /** * ContactListManager state that indicates the contact list(s) has been loaded. */ public static final int LISTS_LOADED = 3; protected ContactList mDefaultContactList; protected Vector<ContactList> mContactLists; protected CopyOnWriteArrayList<ContactListListener> mContactListListeners; protected SubscriptionRequestListener mSubscriptionRequestListener; protected Vector<Contact> mBlockedList; private int mState; /** * Creates a new ContactListManager. * * @param conn The underlying protocol connection. */ protected ContactListManager() { mContactLists = new Vector<ContactList>(); mContactListListeners = new CopyOnWriteArrayList<ContactListListener>(); mBlockedList = new Vector<Contact>(); mState = LISTS_NOT_LOADED; } /** * Set the state of the ContactListManager * * @param state the new state */ protected synchronized void setState(int state) { if (state < LISTS_NOT_LOADED || state > LISTS_LOADED) { throw new IllegalArgumentException(); } mState = state; } /** * Get the state of the ContactListManager * * @return the current state of the manager */ public synchronized int getState() { return mState; } /** * Adds a listener to the manager so that it will be notified for contact * list changed. * * @param listener the listener to add. */ public synchronized void addContactListListener(ContactListListener listener) { if ((listener != null) && !mContactListListeners.contains(listener)) { mContactListListeners.add(listener); } } /** * Removes a listener from this manager. * * @param listener the listener to remove. */ public synchronized void removeContactListListener(ContactListListener listener) { mContactListListeners.remove(listener); } /** * Sets the SubscriptionRequestListener to the manager so that it will be notified * when a subscription request from another user is received. * * @param listener the ContactInvitationListener. */ public synchronized void setSubscriptionRequestListener( SubscriptionRequestListener listener) { mSubscriptionRequestListener = listener; } public synchronized SubscriptionRequestListener getSubscriptionRequestListener() { return mSubscriptionRequestListener; } /** * Gets a collection of the contact lists. * * @return a collection of the contact lists. */ public Collection<ContactList> getContactLists() { return Collections.unmodifiableCollection(mContactLists); } /** * Gets a contact by address. * * @param address the address of the Contact. * @return the Contact or null if the Contact doesn't exist in any list. */ public Contact getContact(Address address) { return getContact(address.getFullName()); } public Contact getContact(String address) { for (ContactList list : mContactLists) { Contact c = list.getContact(address); if( c != null) { return c; } } return null; } public abstract String normalizeAddress(String address); /** * Creates a temporary contact. It's usually used when we want to create * a chat with someone not in the list. * * @param address the address of the temporary contact. * @return the created temporary contact */ public abstract Contact createTemporaryContact(String address); /** * Tell whether the manager contains the specified contact * * @param contact the specified contact * @return true if the contact is contained in the lists of the manager, * otherwise, return false */ public boolean containsContact(Contact contact) { for (ContactList list : mContactLists) { if (list.containsContact(contact)) { return true; } } return false; } /** * Gets a contact list by name. * * @param name the name of the contact list. * @return the ContactList or null if the contact list doesn't exist. */ public ContactList getContactList(String name) { for (ContactList list : mContactLists) { if (list.getName() != null && list.getName().equals(name)) { return list; } } return null; } /** * Get the contact list by the address * * @param address the address of the contact list * @return the <code>ContactList</code> or null if the list doesn't exist */ public ContactList getContactList(Address address) { for (ContactList list : mContactLists) { if (list.getAddress().equals(address)) { return list; } } return null; } /** * Gets the default contact list. * * @return the default contact list. * @throws ImException */ public ContactList getDefaultContactList() throws ImException { checkState(); return mDefaultContactList; } /** * Create a contact list with the specified name asynchronously. * * @param name the specific name of the contact list * @throws ImException */ public void createContactListAsync(String name) throws ImException { createContactListAsync(name, null, false); } /** * Create a contact list with specified name and whether it is to be * created as the default list. * * @param name the specific name of the contact list * @param isDefault whether the contact list is to be created as the * default list * @throws ImException */ public void createContactListAsync(String name, boolean isDefault) throws ImException { createContactListAsync(name, null, isDefault); } /** * Create a contact list with specified name and contacts asynchronously. * * @param name the specific name of the contact list * @param contacts the initial contacts of the contact list * @throws ImException */ public void createContactListAsync(String name, Collection<Contact> contacts) throws ImException { createContactListAsync(name, contacts, false); } /** * Create a contact list with specified name and contacts asynchronously, * and whether it is to be created as the default contact list. * * @param name the name of the contact list * @param contacts the initial contacts of the list * @param isDefault whether the contact list is the default list * @throws ImException */ public synchronized void createContactListAsync(String name, Collection<Contact> contacts, boolean isDefault) throws ImException { checkState(); if (getContactList(name) != null) { throw new ImException(ImErrorInfo.CONTACT_LIST_EXISTS, "Contact list already exists"); } if (mContactLists.isEmpty()) { isDefault = true; } doCreateContactListAsync(name, contacts, isDefault); } /** * Delete a contact list of the specified name asynchronously * @param name the specific name of the contact list * @throws ImException */ public void deleteContactListAsync(String name) throws ImException { deleteContactListAsync(getContactList(name)); } /** * Delete a specified contact list asynchronously * @param list the contact list to be deleted * @throws ImException if any error raised */ public synchronized void deleteContactListAsync(ContactList list) throws ImException { checkState();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -