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

📄 contactlistmanageradapter.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.service;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Vector;import android.content.ContentResolver;import android.content.ContentUris;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.RemoteException;import android.provider.Im;import android.util.Log;import android.widget.Toast;import com.android.im.IContactList;import com.android.im.IContactListListener;import com.android.im.IContactListManager;import com.android.im.ISubscriptionListener;import com.android.im.R;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.ImErrorInfo;import com.android.im.engine.ImException;import com.android.im.engine.Presence;import com.android.im.engine.SubscriptionRequestListener;public class ContactListManagerAdapter extends IContactListManager.Stub {    static final String TAG = RemoteImService.TAG;    ImConnectionAdapter mConn;    ContentResolver     mResolver;    private ContactListManager          mAdaptee;    private ContactListListenerAdapter  mContactListListenerAdapter;    private SubscriptionRequestListenerAdapter mSubscriptionListenerAdapter;    HashMap<Address, ContactListAdapter> mContactLists;    HashMap<String, Contact> mTemporaryContacts;    HashSet<String> mValidatedContactLists;    HashSet<String> mValidatedContacts;    HashSet<String> mValidatedBlockedContacts;    private long mAccountId;    private long mProviderId;    private Uri mAvatarUrl;    private Uri mContactUrl;    static final long FAKE_TEMPORARY_LIST_ID = -1;    static final String[] CONTACT_LIST_ID_PROJECTION  = { Im.ContactList._ID };    RemoteImService mContext;    public ContactListManagerAdapter(ImConnectionAdapter conn) {        mAdaptee  = conn.getAdaptee().getContactListManager();        mConn     = conn;        mContext  = conn.getContext();        mResolver = mContext.getContentResolver();        mContactListListenerAdapter = new ContactListListenerAdapter();        mSubscriptionListenerAdapter = new SubscriptionRequestListenerAdapter();        mContactLists = new HashMap<Address, ContactListAdapter>();        mTemporaryContacts = new HashMap<String, Contact>();        mValidatedContacts = new HashSet<String>();        mValidatedContactLists = new HashSet<String>();        mValidatedBlockedContacts = new HashSet<String>();        mAdaptee.addContactListListener(mContactListListenerAdapter);        mAdaptee.setSubscriptionRequestListener(mSubscriptionListenerAdapter);        mAccountId  = mConn.getAccountId();        mProviderId = mConn.getProviderId();        Uri.Builder builder = Im.Avatars.CONTENT_URI_AVATARS_BY.buildUpon();        ContentUris.appendId(builder, mProviderId);        ContentUris.appendId(builder, mAccountId);        mAvatarUrl = builder.build();        builder = Im.Contacts.CONTENT_URI_CONTACTS_BY.buildUpon();        ContentUris.appendId(builder, mProviderId);        ContentUris.appendId(builder, mAccountId);        mContactUrl = builder.build();    }    public int createContactList(String name, List<Contact> contacts) {        try {            mAdaptee.createContactListAsync(name, contacts);        } catch (ImException e) {            return e.getImError().getCode();        }        return ImErrorInfo.NO_ERROR;    }    public int deleteContactList(String name) {        try {            mAdaptee.deleteContactListAsync(name);        } catch (ImException e) {            return e.getImError().getCode();        }        return ImErrorInfo.NO_ERROR;    }    public List getContactLists() {        synchronized (mContactLists) {            return new ArrayList<ContactListAdapter>(mContactLists.values());        }    }    public int removeContact(String address) {        if(isTemporary(address)) {            // For temporary contact, just close the session and delete him in            // database.            closeChatSession(address);            String selection = Im.Contacts.USERNAME + "=?";            String[] selectionArgs = { address };            mResolver.delete(mContactUrl, selection, selectionArgs);            synchronized (mTemporaryContacts) {                mTemporaryContacts.remove(address);            }        } else {            synchronized (mContactLists) {                Contact c = getContactByAddress(address);                for(ContactListAdapter list : mContactLists.values()) {                    int resCode = list.removeContact(c);                    if (ImErrorInfo.NO_ERROR != resCode) {                        return resCode;                    }                }            }        }        return ImErrorInfo.NO_ERROR;    }    public void approveSubscription(String address) {        mAdaptee.approveSubscriptionRequest(address);    }    public void declineSubscription(String address) {        mAdaptee.declineSubscriptionRequest(address);    }    public int blockContact(String address) {        try {            mAdaptee.blockContactAsync(address);        } catch (ImException e) {            return e.getImError().getCode();        }        return ImErrorInfo.NO_ERROR;    }    public int unBlockContact(String address) {        try {            mAdaptee.unblockContactAsync(address);        } catch (ImException e) {            Log.e(TAG, e.getMessage());            return e.getImError().getCode();        }        return ImErrorInfo.NO_ERROR;    }    public boolean isBlocked(String address) {        try {            return mAdaptee.isBlocked(address);        } catch (ImException e) {            Log.e(TAG, e.getMessage());            return false;        }    }    public void registerContactListListener(IContactListListener listener) {        mContactListListenerAdapter.addRemoteListener(listener);    }    public void unregisterContactListListener(IContactListListener listener) {        mContactListListenerAdapter.removeRemoteListener(listener);    }    public void registerSubscriptionListener(ISubscriptionListener listener) {        mSubscriptionListenerAdapter.addRemoteListener(listener);    }    public void unregisterSubscriptionListener(ISubscriptionListener listener) {        mSubscriptionListenerAdapter.removeRemoteListener(listener);    }    public IContactList getContactList(String name) {        return getContactListAdapter(name);    }    public void loadContactLists() {        if(mAdaptee.getState() == ContactListManager.LISTS_NOT_LOADED){            clearValidatedContactsAndLists();            mAdaptee.loadContactListsAsync();        }    }    public int getState() {        return mAdaptee.getState();    }    public Contact getContactByAddress(String address) {        Contact c = mAdaptee.getContact(address);        if(c == null) {            synchronized (mTemporaryContacts) {                return mTemporaryContacts.get(address);            }        } else {            return c;        }    }    public Contact createTemporaryContact(String address) {        Contact c = mAdaptee.createTemporaryContact(address);        insertTemporary(c);        return c;    }    public long queryOrInsertContact(Contact c) {        long result;        String username = c.getAddress().getFullName();        String selection = Im.Contacts.USERNAME + "=?";        String[] selectionArgs = { username };        String[] projection = {Im.Contacts._ID};        Cursor cursor = mResolver.query(mContactUrl, projection, selection,                selectionArgs, null);        if(cursor != null && cursor.moveToFirst()) {            result = cursor.getLong(0);        } else {            result = insertTemporary(c);        }        if(cursor != null) {            cursor.close();        }        return result;    }    private long insertTemporary(Contact c) {        synchronized (mTemporaryContacts) {            mTemporaryContacts.put(c.getAddress().getFullName(), c);        }        Uri uri = insertContactContent(c, FAKE_TEMPORARY_LIST_ID);        return ContentUris.parseId(uri);    }    /**     * Tells if a contact is a temporary one which is not in the list of     * contacts that we subscribe presence for. Usually created because of the     * user is having a chat session with this contact.     *     * @param address     *            the address of the contact.     * @return <code>true</code> if it's a temporary contact;     *         <code>false</code> otherwise.     */    public boolean isTemporary(String address) {        synchronized (mTemporaryContacts) {            return mTemporaryContacts.containsKey(address);        }    }    ContactListAdapter getContactListAdapter(String name) {        synchronized (mContactLists) {            for (ContactListAdapter list : mContactLists.values()) {                if (name.equals(list.getName())) {                    return list;                }            }            return null;        }    }    ContactListAdapter getContactListAdapter(Address address) {        synchronized (mContactLists) {            return mContactLists.get(address);        }    }    private class Exclusion {        private StringBuilder mSelection;        private List mSelectionArgs;        private String mExclusionColumn;        Exclusion(String exclusionColumn, Collection<String> items) {            mSelection = new StringBuilder();            mSelectionArgs = new ArrayList();            mExclusionColumn = exclusionColumn;            for (String s : items) {                add(s);            }        }        public void add(String exclusionItem) {            if (mSelection.length()==0) {                mSelection.append(mExclusionColumn + "!=?");            } else {                mSelection.append(" AND " + mExclusionColumn + "!=?");            }            mSelectionArgs.add(exclusionItem);        }        public String getSelection() {            return mSelection.toString();        }        public String[] getSelectionArgs() {            return (String []) mSelectionArgs.toArray(new String[0]);        }    }    private void removeObsoleteContactsAndLists() {        // remove all contacts for this provider & account which have not been        // added since login, yet still exist in db from a prior login        Exclusion exclusion = new Exclusion(Im.Contacts.USERNAME, mValidatedContacts);        mResolver.delete(mContactUrl, exclusion.getSelection(), exclusion.getSelectionArgs());        // remove all blocked contacts for this provider & account which have not been        // added since login, yet still exist in db from a prior login        exclusion = new Exclusion(Im.BlockedList.USERNAME, mValidatedBlockedContacts);        Uri.Builder builder = Im.BlockedList.CONTENT_URI.buildUpon();        ContentUris.appendId(builder, mProviderId);        ContentUris.appendId(builder, mAccountId);        Uri uri = builder.build();

⌨️ 快捷键说明

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