⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 impscontactlistmanager.java

📁 Android平台上即时通讯聊天工具源代码。 支持手机聊天。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * 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.imps;import java.util.ArrayList;import java.util.Collection;import java.util.Vector;import com.android.im.engine.Address;import com.android.im.engine.Contact;import com.android.im.engine.ContactList;import com.android.im.engine.ContactListListener;import com.android.im.engine.ContactListManager;import com.android.im.engine.ImConnection;import com.android.im.engine.ImErrorInfo;import com.android.im.engine.ImException;import com.android.im.engine.Presence;import com.android.im.engine.SubscriptionRequestListener;import com.android.im.imps.ImpsConstants.ImpsVersion;/** * An implementation of ContactListManager of Wireless Village IMPS protocol. */public class ImpsContactListManager extends ContactListManager        implements ServerTransactionListener {    private ImpsConnection mConnection;    private String mDefaultDomain;    ImpsTransactionManager mTransactionManager;    boolean mAllowAutoSubscribe = true;    ArrayList<Contact> mSubscriptionRequests;    /**     * Constructs the manager with specific connection.     *     * @param connection the connection related to the manager     */    ImpsContactListManager(ImpsConnection connection) {        mConnection = connection;        mDefaultDomain = connection.getConfig().getDefaultDomain();        mTransactionManager = connection.getTransactionManager();        mTransactionManager.setTransactionListener(ImpsTags.PresenceNotification_Request, this);        mTransactionManager.setTransactionListener(ImpsTags.PresenceAuth_Request, this);    }    @Override    public Contact createTemporaryContact(String address){        ImpsAddress impsAddr = new ImpsUserAddress(normalizeAddress(address));        return new Contact(impsAddr, impsAddr.getScreenName());    }    @Override    public String normalizeAddress(String address) {        String s = address.toLowerCase();        if (!s.startsWith(ImpsConstants.ADDRESS_PREFIX)) {            s = ImpsConstants.ADDRESS_PREFIX + s;        }        if (mDefaultDomain != null && s.indexOf('@') == -1) {            s = s + "@" + mDefaultDomain;        }        return s;    }    @Override    public synchronized void loadContactListsAsync() {        if (getState() != LISTS_NOT_LOADED) {            return;        }        setState(LISTS_LOADING);        // load blocked list first        Primitive request = new Primitive(ImpsTags.GetBlockedList_Request);        AsyncTransaction tx = new AsyncTransaction(mTransactionManager) {            @Override            public void onResponseError(ImpsErrorInfo error) {                // don't notify the 501 not implemented error                if (error.getCode() != ImpsConstants.STATUS_NOT_IMPLEMENTED) {                    notifyContactError(                            ContactListListener.ERROR_LOADING_BLOCK_LIST,                            error, null, null);                }                next();            }            @Override            public void onResponseOk(Primitive response) {                extractBlockedContacts(response);                next();            }            private void next() {                setState(BLOCKED_LIST_LOADED);                new LoadContactsTransaction().startGetContactLists();                //createDefaultAttributeListAsync();            }        };        tx.sendRequest(request);    }    Vector<ImpsContactListAddress> extractListAddresses(Primitive response){        Vector<ImpsContactListAddress> addresses = new Vector<ImpsContactListAddress>();        for (PrimitiveElement child : response.getContentElement()                .getChildren()) {            if (child.getTagName().equals(ImpsTags.ContactList)) {                // FIXME: ignore the PEP contact lists for now                // PEP: "Presence Enhanced Phonebook and Instant Messaging                // Application Category" specification from Nokia and SonyEricsson.                //  ~IM_subscriptions                //  ~pep1.0_privatelist                //  ~pep1.0_blocklist                //  ~pep1.0_friendlist                //  ~pep1.0_subscriptions-*                if (child.getContents().contains("/~pep1.0_")) {                    continue;                }                addresses.add(new ImpsContactListAddress(child.getContents()));            }        }        String defaultListAddress = response.getElementContents(ImpsTags.DefaultContactList);        if (null != defaultListAddress) {            addresses.add(new ImpsContactListAddress(defaultListAddress));        }        return addresses;    }//    void createDefaultAttributeListAsync() {//        Primitive request = new Primitive(ImpsTags.CreateAttributeList_Request);////        PrimitiveElement presenceList = request.addElement(ImpsTags.PresenceSubList);//        presenceList.setAttribute(ImpsTags.XMLNS, mConnection.getConfig().getPresenceNs());////        for (String tagName : ImpsClientCapability.getSupportedPresenceAttribs()) {//            presenceList.addChild(tagName);//        }////        request.addElement(ImpsTags.DefaultList, true);////        AsyncTransaction tx = new AsyncTransaction(mTransactionManager) {//            @Override//            public void onResponseError(ImpsErrorInfo error) {//                // don't notify the 501 not implemented error//                if(error.getCode() != ImpsConstants.STATUS_NOT_IMPLEMENTED) {//                    // TODO: not ERROR_RETRIEVING_PRESENCE exactly here...//                    notifyContactError(//                            ContactListListener.ERROR_RETRIEVING_PRESENCE,//                            error, null, null);//                }//            }////            @Override//            public void onResponseOk(Primitive response) {}//        };////        tx.sendRequest(request);//    }    @Override    public void approveSubscriptionRequest(String contact) {        handleSubscriptionRequest(contact, true);    }    @Override    public void declineSubscriptionRequest(String contact) {        handleSubscriptionRequest(contact, false);    }    private void handleSubscriptionRequest(final String contact, final boolean accept) {        Primitive request = new Primitive(ImpsTags.PresenceAuthUser);        request.addElement(ImpsTags.UserID, contact);        request.addElement(ImpsTags.Acceptance, accept);        AsyncTransaction tx = new AsyncTransaction(mTransactionManager){            @Override            public void onResponseError(ImpsErrorInfo error) {                SubscriptionRequestListener listener = getSubscriptionRequestListener();                if (listener != null) {                    if (accept) {                        listener.onApproveSubScriptionError(contact, error);                    } else {                        listener.onDeclineSubScriptionError(contact, error);                    }                }            }            @Override            public void onResponseOk(Primitive response) {                SubscriptionRequestListener listener = getSubscriptionRequestListener();                if (listener != null) {                    if (accept) {                        listener.onSubscriptionApproved(contact);                    } else {                        listener.onSubscriptionDeclined(contact);                    }                }            }        };        tx.sendRequest(request);    }    void subscribeToAllListAsync() {        AsyncCompletion completion = new AsyncCompletion(){            public void onComplete() {                // do nothing            }            public void onError(ImErrorInfo error) {                notifyContactError(ContactListListener.ERROR_RETRIEVING_PRESENCE,                        error, null, null);            }        };        subscribeToListsAsync(mContactLists,completion);    }    void subscribeToListAsync(final ContactList list, final AsyncCompletion completion) {        Vector<ContactList> lists = new Vector<ContactList>();        lists.add(list);        subscribeToListsAsync(lists, completion);    }    void subscribeToListsAsync(final Vector<ContactList> contactLists,            final AsyncCompletion completion) {        if (contactLists.isEmpty()) {            return;        }        Primitive request = buildSubscribeToListsRequest(contactLists);        AsyncTransaction tx = new AsyncTransaction(mTransactionManager) {            @Override            public void onResponseError(ImpsErrorInfo error) {                if (error.getCode() == ImpsConstants.STATUS_AUTO_SUBSCRIPTION_NOT_SUPPORTED) {                    mAllowAutoSubscribe = false;                    ArrayList<Contact> contacts = new ArrayList<Contact>();                    for (ContactList list : contactLists) {                        contacts.addAll(list.getContacts());                    }                    subscribeToContactsAsync(contacts, completion);                } else {                    completion.onError(error);                }            }            @Override            public void onResponseOk(Primitive response) {                completion.onComplete();            }        };        tx.sendRequest(request);    }    void subscribeToContactsAsync(ArrayList<Contact> contacts, AsyncCompletion completion) {        Primitive request = buildSubscribeToContactsRequest(contacts);        SimpleAsyncTransaction tx = new SimpleAsyncTransaction(mTransactionManager, completion);        tx.sendRequest(request);    }    void unsubscribeToListAsync(ContactList list, AsyncCompletion completion) {        Primitive request = new Primitive(ImpsTags.UnsubscribePresence_Request);        request.addElement(ImpsTags.ContactList, list.getAddress().getFullName());        SimpleAsyncTransaction tx = new SimpleAsyncTransaction(                mTransactionManager, completion);        tx.sendRequest(request);    }    void unsubscribeToContactAsync(Contact contact, AsyncCompletion completion) {        Primitive request = new Primitive(ImpsTags.UnsubscribePresence_Request);        request.addElement(ImpsTags.User).addPropertyChild(ImpsTags.UserID,                contact.getAddress().getFullName());        SimpleAsyncTransaction tx = new SimpleAsyncTransaction(                mTransactionManager, completion);        tx.sendRequest(request);    }    private Primitive buildSubscribeToContactsRequest(ArrayList<Contact> contacts) {        ArrayList<ImpsAddress> addresses = new ArrayList<ImpsAddress>();        for (Contact contact : contacts) {            addresses.add((ImpsAddress)contact.getAddress());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -