1 /* 2 * Copyright (C) 2007-2008 Esmertec AG. 3 * Copyright (C) 2007-2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.engine; 19 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.HashMap; 23 24 public class ContactList extends ImEntity { 25 protected Address mAddress; 26 protected String mName; 27 protected boolean mDefault; 28 protected ContactListManager mManager; 29 30 private HashMap<String, Contact> mContactsCache; 31 ContactList(Address address, String name, boolean isDefault, Collection<Contact> contacts, ContactListManager manager)32 public ContactList(Address address, String name, boolean isDefault, 33 Collection<Contact> contacts, ContactListManager manager) { 34 mAddress = address; 35 mDefault = isDefault; 36 mName = name; 37 mManager = manager; 38 39 mContactsCache = new HashMap<String, Contact>(); 40 if (contacts != null) { 41 for (Contact c : contacts) { 42 mContactsCache.put(manager.normalizeAddress(c.getAddress().getFullName()), c); 43 } 44 } 45 } 46 47 @Override getAddress()48 public Address getAddress() { 49 return mAddress; 50 } 51 getName()52 public String getName() { 53 return mName; 54 } 55 setName(String name)56 public void setName(String name) { 57 if (null == name) { 58 throw new NullPointerException(); 59 } 60 61 mManager.setListNameAsync(name, this); 62 } 63 setDefault(boolean isDefault)64 public void setDefault(boolean isDefault) { 65 this.mDefault = isDefault; 66 } 67 isDefault()68 public boolean isDefault() { 69 return mDefault; 70 } 71 72 /** 73 * Add a contact to the list. The contact is specified by its address 74 * string. 75 * 76 * @param address 77 * the address string specifies the contact. 78 * @throws IllegalArgumentException 79 * if the address is invalid. 80 * @throws NullPointerException 81 * if the address string is null 82 * @throws ImException 83 * if the contact is not allowed to be added 84 */ addContact(String address)85 public void addContact(String address) throws ImException { 86 address = mManager.normalizeAddress(address); 87 88 if (null == address) { 89 throw new NullPointerException(); 90 } 91 92 if (mManager.isBlocked(address)) { 93 throw new ImException(ImErrorInfo.CANT_ADD_BLOCKED_CONTACT, 94 "Contact has been blocked"); 95 } 96 97 if(containsContact(address)){ 98 throw new ImException(ImErrorInfo.CONTACT_EXISTS_IN_LIST, 99 "Contact already exists in the list"); 100 } 101 102 mManager.addContactToListAsync(address, this); 103 } 104 105 /** 106 * Remove a contact from the list. If the contact is not in the list, 107 * nothing will happen. Otherwise, the contact will be removed from 108 * the list on the server asynchronously. 109 * 110 * @param address 111 * the address of the contact to be removed from the list 112 * @throws NullPointerException 113 * If the address is null 114 */ removeContact(Address address)115 public void removeContact(Address address) throws ImException { 116 if(address == null) { 117 throw new NullPointerException(); 118 } 119 Contact c = getContact(address); 120 if(c != null) { 121 removeContact(c); 122 } 123 } 124 125 /** 126 * Remove a contact from the list. If the contact is not in the list, 127 * nothing will happen. Otherwise, the contact will be removed from 128 * the list on the server asynchronously. 129 * 130 * @param contact 131 * the contact to be removed from the list 132 * @throws NullPointerException 133 * If the contact is null 134 */ removeContact(Contact contact)135 public void removeContact(Contact contact) throws ImException { 136 if(contact == null) { 137 throw new NullPointerException(); 138 } 139 140 if(containsContact(contact)) { 141 mManager.removeContactFromListAsync(contact, this); 142 } 143 } 144 getContact(Address address)145 public synchronized Contact getContact(Address address) { 146 return mContactsCache.get(mManager.normalizeAddress(address.getFullName())); 147 } 148 getContact(String address)149 public synchronized Contact getContact(String address) { 150 return mContactsCache.get(mManager.normalizeAddress(address)); 151 } 152 getContactsCount()153 public synchronized int getContactsCount() { 154 return mContactsCache.size(); 155 } 156 getContacts()157 public synchronized Collection<Contact> getContacts() { 158 return new ArrayList<Contact>(mContactsCache.values()); 159 } 160 containsContact(String address)161 public synchronized boolean containsContact(String address) { 162 return mContactsCache.containsKey(mManager.normalizeAddress(address)); 163 } 164 containsContact(Address address)165 public synchronized boolean containsContact(Address address) { 166 return address == null ? false 167 : mContactsCache.containsKey(mManager.normalizeAddress(address.getFullName())); 168 } 169 containsContact(Contact c)170 public synchronized boolean containsContact(Contact c) { 171 return c == null ? false 172 : mContactsCache.containsKey(mManager.normalizeAddress(c.getAddress().getFullName())); 173 } 174 insertToCache(Contact contact)175 protected void insertToCache(Contact contact) { 176 mContactsCache.put(mManager.normalizeAddress(contact.getAddress().getFullName()), contact); 177 } 178 removeFromCache(Contact contact)179 protected void removeFromCache(Contact contact) { 180 mContactsCache.remove(mManager.normalizeAddress(contact.getAddress().getFullName())); 181 } 182 183 } 184