1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 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.HashMap; 22 import java.util.concurrent.CopyOnWriteArrayList; 23 24 /** 25 * ChatGroupManager manages the creating, removing and the member of ChatGroups. 26 */ 27 public abstract class ChatGroupManager { 28 protected HashMap<Address, ChatGroup> mGroups; 29 30 protected HashMap<String, Invitation> mInvitations; 31 32 protected CopyOnWriteArrayList<GroupListener> mGroupListeners; 33 34 protected InvitationListener mInvitationListener; 35 36 protected ChatGroupManager() { 37 mGroups = new HashMap<Address, ChatGroup>(); 38 mInvitations = new HashMap<String, Invitation>(); 39 mGroupListeners = new CopyOnWriteArrayList<GroupListener>(); 40 } 41 42 /** 43 * Adds a GroupListener to this manager so that it will be notified when a 44 * certain group changes. 45 * 46 * @param listener the listener to be notified. 47 */ 48 public void addGroupListener(GroupListener listener) { 49 mGroupListeners.add(listener); 50 } 51 52 /** 53 * Removes a GroupListener from this manager so that it won't be notified 54 * any more. 55 * 56 * @param listener the listener to remove. 57 */ 58 public void removeGroupListener(GroupListener listener) { 59 mGroupListeners.remove(listener); 60 } 61 62 /** 63 * Sets the InvitationListener to the manager so that it will be notified 64 * when an invitation from another users received. 65 * 66 * @param listener the InvitationListener. 67 */ 68 public synchronized void setInvitationListener(InvitationListener listener) { 69 mInvitationListener = listener; 70 } 71 72 /** 73 * Creates a new ChatGroup with specified name. This method returns 74 * immediately and the registered GroupListeners will be notified when the 75 * group is created or any error occurs. The newly created group is a 76 * temporary group and will be automatically deleted when all joined users 77 * have left. 78 * 79 * @param name the name of the ChatGroup to be created. 80 */ 81 public abstract void createChatGroupAsync(String name); 82 83 /** 84 * Deletes a certain ChatGroup. This method returns immediately and the 85 * registered GroupListeners will be notified when the group is deleted or 86 * any error occurs. Only the administrator of the ChatGroup can delete it. 87 * 88 * @param group the ChatGroup to be deleted. 89 */ 90 public abstract void deleteChatGroupAsync(ChatGroup group); 91 92 /** 93 * Adds a member to a certain ChatGroup. This method returns immediately and 94 * the GroupGroupListeners registered on the group will be notified when the 95 * member is added or any error occurs. Only the administrator of the 96 * ChatGroup can add member to it. 97 * 98 * @param group the ChatGroup to which the member will add. 99 * @param contact the member to add. 100 */ 101 protected abstract void addGroupMemberAsync(ChatGroup group, Contact contact); 102 103 /** 104 * Removes a member from certain ChatGroup. This method returns immediately 105 * and the GroupGroupListeners registered on the group will be notified when 106 * the member is added or any error occurs. Only the administrator of the 107 * ChatGroup can remove its members. 108 * 109 * @param group the ChatGroup whose member will be removed. 110 * @param contact the member to be removed. 111 */ 112 protected abstract void removeGroupMemberAsync(ChatGroup group, Contact contact); 113 114 /** 115 * Joins into a certain ChatGroup. This method returns immediately and the 116 * registered GroupListeners will be notified when the user joined into the 117 * group or any error occurs. 118 * 119 * @param address the address of the ChatGroup. 120 */ 121 public abstract void joinChatGroupAsync(Address address); 122 123 /** 124 * Leaves a certain ChatGroup.This method returns immediately and the 125 * registered GroupListeners will be notified when the the user left the 126 * group or any error occurs. 127 * 128 * @param group the ChatGroup. 129 */ 130 public abstract void leaveChatGroupAsync(ChatGroup group); 131 132 /** 133 * Invites a user to join a certain ChatGroup. If success, the invitee will 134 * receive an invitation with information of the group. Otherwise, the 135 * registered GroupListeners will be notified if any error occurs. 136 * 137 * @param group the ChatGroup. 138 * @param invitee the invitee. 139 */ 140 public abstract void inviteUserAsync(ChatGroup group, Contact invitee); 141 142 /** 143 * Accepts an invitation. The user will join the group automatically after 144 * accept the invitation. 145 * 146 * @param invitation the invitation to accept. 147 */ 148 public abstract void acceptInvitationAsync(Invitation invitation); 149 150 /** 151 * Accepts an invitation. The user can only accept or reject the same 152 * invitation only once. 153 * 154 * @param inviteId the id of the invitation to accept. 155 * @see #acceptInvitationAsync(Invitation) 156 */ 157 public void acceptInvitationAsync(String inviteId) { 158 Invitation invitation = mInvitations.remove(inviteId); 159 if (invitation != null) { 160 acceptInvitationAsync(invitation); 161 } 162 } 163 164 /** 165 * Rejects an invitation. 166 * 167 * @param inviteId the id of the invitation to reject. 168 * @see #rejectInvitationAsync(Invitation) 169 */ 170 public void rejectInvitationAsync(String inviteId) { 171 Invitation invitation = mInvitations.remove(inviteId); 172 if (invitation != null) { 173 rejectInvitationAsync(invitation); 174 } 175 } 176 177 /** 178 * Rejects an invitation. 179 * 180 * @param invitation the invitation to reject. 181 */ 182 public abstract void rejectInvitationAsync(Invitation invitation); 183 184 /** 185 * Gets a ChatGroup by address. 186 * 187 * @param address the address of the ChatGroup. 188 * @return a ChatGroup. 189 */ 190 public ChatGroup getChatGroup(Address address) { 191 return mGroups.get(address); 192 } 193 194 /** 195 * Notifies the GroupListeners that a ChatGroup has changed. 196 * 197 * @param groupAddress the address of group which has changed. 198 * @param joined a list of users that have joined the group. 199 * @param left a list of users that have left the group. 200 */ 201 protected void notifyGroupChanged(Address groupAddress, ArrayList<Contact> joined, 202 ArrayList<Contact> left) { 203 ChatGroup group = mGroups.get(groupAddress); 204 if (group == null) { 205 group = new ChatGroup(groupAddress, groupAddress.getScreenName(), this); 206 mGroups.put(groupAddress, group); 207 } 208 if (joined != null) { 209 for (Contact contact : joined) { 210 notifyMemberJoined(group, contact); 211 } 212 } 213 if (left != null) { 214 for (Contact contact : left) { 215 notifyMemberLeft(group, contact); 216 } 217 } 218 } 219 220 protected synchronized void notifyGroupCreated(ChatGroup group) { 221 mGroups.put(group.getAddress(), group); 222 for (GroupListener listener : mGroupListeners) { 223 listener.onGroupCreated(group); 224 } 225 } 226 227 protected synchronized void notifyGroupDeleted(ChatGroup group) { 228 mGroups.remove(group.getAddress()); 229 for (GroupListener listener : mGroupListeners) { 230 listener.onGroupDeleted(group); 231 } 232 } 233 234 protected synchronized void notifyJoinedGroup(ChatGroup group) { 235 mGroups.put(group.getAddress(), group); 236 for (GroupListener listener : mGroupListeners) { 237 listener.onJoinedGroup(group); 238 } 239 } 240 241 /** 242 * Notifies the GroupListeners that the user has left a certain group. 243 * 244 * @param groupAddress the address of the group. 245 */ 246 protected synchronized void notifyLeftGroup(ChatGroup group) { 247 mGroups.remove(group.getAddress()); 248 for (GroupListener listener : mGroupListeners) { 249 listener.onLeftGroup(group); 250 } 251 } 252 253 protected synchronized void notifyGroupError(int errorType, String groupName, ImErrorInfo error) { 254 for (GroupListener listener : mGroupListeners) { 255 listener.onGroupError(errorType, groupName, error); 256 } 257 } 258 259 /** 260 * Notifies the InvitationListener that another user invited the current 261 * logged user to join a group chat. 262 */ 263 protected synchronized void notifyGroupInvitation(Invitation invitation) { 264 mInvitations.put(invitation.getInviteID(), invitation); 265 if (mInvitationListener != null) { 266 mInvitationListener.onGroupInvitation(invitation); 267 } 268 } 269 270 /** 271 * Notifies that a contact has joined into this group. 272 * 273 * @param group the group into which the contact has joined. 274 * @param contact the contact who has joined into the group. 275 */ 276 protected void notifyMemberJoined(ChatGroup group, Contact contact) { 277 group.notifyMemberJoined(contact); 278 } 279 280 /** 281 * Notifies that a contact has left this group. 282 * 283 * @param group the group which the contact has left. 284 * @param contact the contact who has left this group. 285 */ 286 protected void notifyMemberLeft(ChatGroup group, Contact contact) { 287 group.notifyMemberLeft(contact); 288 } 289 290 /** 291 * Notifies that previous operation on this group has failed. 292 * 293 * @param error the error information. 294 */ 295 protected void notifyGroupMemberError(ChatGroup group, ImErrorInfo error) { 296 group.notifyGroupMemberError(error); 297 } 298 } 299