📄 contactlistmanageradapter.java
字号:
mResolver.delete(uri, exclusion.getSelection(), exclusion.getSelectionArgs()); // remove all contact lists for this provider & account which have not been // added since login, yet still exist in db from a prior login exclusion = new Exclusion(Im.ContactList.NAME, mValidatedContactLists); builder = Im.ContactList.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, mProviderId); ContentUris.appendId(builder, mAccountId); uri = builder.build(); mResolver.delete(uri, exclusion.getSelection(), exclusion.getSelectionArgs()); } final class ContactListListenerAdapter extends RemoteListenerManager<IContactListListener> implements ContactListListener { private boolean mAllContactsLoaded; // class to hold contact changes made before mAllContactsLoaded private class StoredContactChange { int mType; ContactList mList; Contact mContact; StoredContactChange(int type, ContactList list, Contact contact) { mType = type; mList = list; mContact = contact; } } private Vector<StoredContactChange> mDelayedContactChanges = new Vector<StoredContactChange>(); public void onContactsPresenceUpdate(final Contact[] contacts) { // The client listens only to presence updates for now. Update // the avatars first to ensure it can get the new avatar when // presence updated. // TODO: Don't update avatar now since none of the server supports it // updateAvatarsContent(contacts); updatePresenceContent(contacts); notifyRemoteListeners(new ListenerInvocation<IContactListListener>() { public void invoke(IContactListListener remoteListener) throws RemoteException { remoteListener.onContactsPresenceUpdate(contacts); } }); } public void onContactChange(final int type, final ContactList list, final Contact contact) { ContactListAdapter removed = null; String notificationText = null; switch (type) { case LIST_LOADED: case LIST_CREATED: addContactListContent(list); break; case LIST_DELETED: removed = removeContactListFromDataBase(list.getName()); // handle case where a list is deleted before mAllContactsLoaded if (!mAllContactsLoaded) { // if a cached contact list is deleted before the actual contact list is // downloaded from the server, we will have to remove the list again once // once mAllContactsLoaded is true if (!mValidatedContactLists.contains(list.getName())) { mDelayedContactChanges.add(new StoredContactChange(type, list, contact)); } } break; case LIST_CONTACT_ADDED: long listId = getContactListAdapter(list.getAddress()).getDataBaseId(); String contactAddress = contact.getAddress().getFullName(); if(isTemporary(contactAddress)){ moveTemporaryContactToList(contactAddress, listId); } else { insertContactContent(contact, listId); } notificationText = mContext.getResources().getString( R.string.add_contact_success, contact.getName()); // handle case where a contact is added before mAllContactsLoaded if (!mAllContactsLoaded) { // if a contact is added to a cached contact list before the actual contact // list is downloaded from the server, we will have to add the contact to // the contact list once mAllContactsLoaded is true if (!mValidatedContactLists.contains(list.getName())) { mDelayedContactChanges.add(new StoredContactChange(type, list, contact)); } } break; case LIST_CONTACT_REMOVED: deleteContactFromDataBase(contact, list); // handle case where a contact is removed before mAllContactsLoaded if (!mAllContactsLoaded) { // if a contact is added to a cached contact list before the actual contact // list is downloaded from the server, we will have to add the contact to // the contact list once mAllContactsLoaded is true if (!mValidatedContactLists.contains(list.getName())) { mDelayedContactChanges.add(new StoredContactChange(type, list, contact)); } } // Clear ChatSession if any. String address = contact.getAddress().getFullName(); closeChatSession(address); notificationText = mContext.getResources().getString( R.string.delete_contact_success, contact.getName()); break; case LIST_RENAMED: updateListNameInDataBase(list); // handle case where a list is renamed before mAllContactsLoaded if (!mAllContactsLoaded) { // if a contact list name is updated before the actual contact list is // downloaded from the server, we will have to update the list name again // once mAllContactsLoaded is true if (!mValidatedContactLists.contains(list.getName())) { mDelayedContactChanges.add(new StoredContactChange(type, list, contact)); } } break; case CONTACT_BLOCKED: insertBlockedContactToDataBase(contact); address = contact.getAddress().getFullName(); updateContactType(address, Im.Contacts.TYPE_BLOCKED); closeChatSession(address); notificationText = mContext.getResources().getString( R.string.block_contact_success, contact.getName()); break; case CONTACT_UNBLOCKED: removeBlockedContactFromDataBase(contact); notificationText = mContext.getResources().getString( R.string.unblock_contact_success, contact.getName()); // handle case where a contact is unblocked before mAllContactsLoaded if (!mAllContactsLoaded) { // if a contact list name is updated before the actual contact list is // downloaded from the server, we will have to update the list name again // once mAllContactsLoaded is true if (!mValidatedBlockedContacts.contains(contact.getName())) { mDelayedContactChanges.add(new StoredContactChange(type, list, contact)); } } break; default: Log.e(TAG, "Unknown list update event!"); break; } final ContactListAdapter listAdapter; if (type == LIST_DELETED) { listAdapter = removed; } else { listAdapter = (list == null) ? null : getContactListAdapter(list.getAddress()); } notifyRemoteListeners(new ListenerInvocation<IContactListListener>() { public void invoke(IContactListListener remoteListener) throws RemoteException { remoteListener.onContactChange(type, listAdapter, contact); } }); if (mAllContactsLoaded && notificationText != null) { mContext.showToast(notificationText, Toast.LENGTH_SHORT); } } public void onContactError(final int errorType, final ImErrorInfo error, final String listName, final Contact contact) { notifyRemoteListeners(new ListenerInvocation<IContactListListener>() { public void invoke(IContactListListener remoteListener) throws RemoteException { remoteListener.onContactError(errorType, error, listName, contact); } }); } public void handleDelayedContactChanges() { for (StoredContactChange change : mDelayedContactChanges) { onContactChange(change.mType, change.mList, change.mContact); } } public void onAllContactListsLoaded() { mAllContactsLoaded = true; handleDelayedContactChanges(); removeObsoleteContactsAndLists(); notifyRemoteListeners(new ListenerInvocation<IContactListListener>() { public void invoke(IContactListListener remoteListener) throws RemoteException { remoteListener.onAllContactListsLoaded(); } }); } } final class SubscriptionRequestListenerAdapter extends RemoteListenerManager<ISubscriptionListener> implements SubscriptionRequestListener { public void onSubScriptionRequest(final Contact from) { String username = from.getAddress().getFullName(); String nickname = from.getName(); Uri uri = insertOrUpdateSubscription(username, nickname, Im.Contacts.SUBSCRIPTION_TYPE_FROM, Im.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING); mContext.getStatusBarNotifier().notifySubscriptionRequest(mProviderId, mAccountId, ContentUris.parseId(uri), username, nickname); notifyRemoteListeners(new ListenerInvocation<ISubscriptionListener>() { public void invoke(ISubscriptionListener remoteListener) throws RemoteException { remoteListener.onSubScriptionRequest(from); } }); } public void onSubscriptionApproved(final String contact) { insertOrUpdateSubscription(contact, null, Im.Contacts.SUBSCRIPTION_TYPE_NONE, Im.Contacts.SUBSCRIPTION_STATUS_NONE); notifyRemoteListeners(new ListenerInvocation<ISubscriptionListener>() { public void invoke(ISubscriptionListener remoteListener) throws RemoteException { remoteListener.onSubscriptionApproved(contact); } }); } public void onSubscriptionDeclined(final String contact) { insertOrUpdateSubscription(contact, null, Im.Contacts.SUBSCRIPTION_TYPE_NONE, Im.Contacts.SUBSCRIPTION_STATUS_NONE); notifyRemoteListeners(new ListenerInvocation<ISubscriptionListener>() { public void invoke(ISubscriptionListener remoteListener) throws RemoteException { remoteListener.onSubscriptionDeclined(contact); } }); } public void onApproveSubScriptionError(final String contact, final ImErrorInfo error) { String displayableAddress = getDisplayableAddress(contact); String msg = mContext.getString(R.string.approve_subscription_error, displayableAddress); mContext.showToast(msg, Toast.LENGTH_SHORT); } public void onDeclineSubScriptionError(final String contact, final ImErrorInfo error) { String displayableAddress = getDisplayableAddress(contact); String msg = mContext.getString(R.string.decline_subscription_error, displayableAddress); mContext.showToast(msg, Toast.LENGTH_SHORT); } } String getDisplayableAddress(String impsAddress) { if (impsAddress.startsWith("wv:")) { return impsAddress.substring(3); } return impsAddress; } void insertBlockedContactToDataBase(Contact contact) { // Remove the blocked contact if it already exists, to avoid duplicates and // handle the odd case where a blocked contact's nickname has changed removeBlockedContactFromDataBase(contact); Uri.Builder builder = Im.BlockedList.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, mProviderId); ContentUris.appendId(builder, mAccountId); Uri uri = builder.build(); String username = contact.getAddress().getFullName(); ContentValues values = new ContentValues(2); values.put(Im.BlockedList.USERNAME, username); values.put(Im.BlockedList.NICKNAME, contact.getName()); mResolver.insert(uri, values); mValidatedBlockedContacts.add(username); } void removeBlockedContactFromDataBase(Contact contact) { String address = contact.getAddress().getFullName(); Uri.Builder builder = Im.BlockedList.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, mProviderId); ContentUris.appendId(builder, mAccountId); Uri uri = builder.build(); mResolver.delete(uri, Im.BlockedList.USERNAME + "=?", new String[]{ address }); int type = isTemporary(address) ? Im.Contacts.TYPE_TEMPORARY : Im.Contacts.TYPE_NORMAL; updateContactType(address, type); } void moveTemporaryContactToList(String address, long listId) { synchronized (mTemporaryContacts) { mTemporaryContacts.remove(address); } ContentValues values = new ContentValues(2); values.put(Im.Contacts.TYPE, Im.Contacts.TYPE_NORMAL); values.put(Im.Contacts.CONTACTLIST, listId); String selection = Im.Contacts.USERNAME + "=? AND " + Im.Contacts.TYPE + "=" + Im.Contacts.TYPE_TEMPORARY; String[] selectionArgs = { address }; mResolver.update(mContactUrl, values, selection, selectionArgs); } void updateContactType(String address, int type) { ContentValues values = new ContentValues(1); values.put(Im.Contacts.TYPE, type); updateContact(address, values); } /** * Insert or update subscription request from user into the database. * * @param username * @param nickname * @param subscriptionType * @param subscriptionStatus */ Uri insertOrUpdateSubscription(String username, String nickname, int subscriptionType, int subscriptionStatus) { Cursor cursor = mResolver.query(mContactUrl, new String[]{ Im.Contacts._ID }, Im.Contacts.USERNAME + "=?", new String[]{username}, null); if (cursor == null) { Log.w(TAG, "query contact " + username + " failed"); return null; } Uri uri; if (cursor.moveToFirst()) { ContentValues values = new ContentValues(2); values.put(Im.Contacts.SUBSCRIPTION_TYPE, subscriptionType); values.put(Im.Contacts.SUBSCRIPTION_STATUS, subscriptionStatus); long contactId = cursor.getLong(0); uri = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, contactId); mResolver.update(uri, values, null, null); } else { ContentValues values = new ContentValues(6); values.put(Im.Contacts.USERNAME, username); values.put(Im.Contacts.NICKNAME, nickname); values.put(Im.Contacts.TYPE, Im.Contacts.TYPE_NORMAL); values.put(Im.Contacts.CONTACTLIST, FAKE_TEMPORARY_LIST_ID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -