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.Collection; 21 import java.util.Collections; 22 import java.util.List; 23 import java.util.Vector; 24 import java.util.concurrent.CopyOnWriteArrayList; 25 26 public class ChatGroup extends ImEntity{ 27 private ChatGroupManager mManager; 28 private Address mAddress; 29 private String mName; 30 private Vector<Contact> mMembers; 31 private CopyOnWriteArrayList<GroupMemberListener> mMemberListeners; 32 ChatGroup(Address address, String name, ChatGroupManager manager)33 public ChatGroup(Address address, String name, ChatGroupManager manager) { 34 this(address, name, null, manager); 35 } 36 ChatGroup(Address address, String name, Collection<Contact> members, ChatGroupManager manager)37 public ChatGroup(Address address, String name, Collection<Contact> members, 38 ChatGroupManager manager) { 39 mAddress = address; 40 mName = name; 41 mManager = manager; 42 mMembers = new Vector<Contact>(); 43 44 if(members != null) { 45 mMembers.addAll(members); 46 } 47 mMemberListeners = new CopyOnWriteArrayList<GroupMemberListener>(); 48 } 49 50 @Override getAddress()51 public Address getAddress() { 52 return mAddress; 53 } 54 55 /** 56 * Gets the name of the group. 57 * 58 * @return the name of the group. 59 */ getName()60 public String getName() { 61 return mName; 62 } 63 addMemberListener(GroupMemberListener listener)64 public void addMemberListener(GroupMemberListener listener) { 65 mMemberListeners.add(listener); 66 } 67 removeMemberListener(GroupMemberListener listener)68 public void removeMemberListener(GroupMemberListener listener) { 69 mMemberListeners.remove(listener); 70 } 71 72 /** 73 * Gets an unmodifiable collection of the members of the group. 74 * 75 * @return an unmodifiable collection of the members of the group. 76 */ getMembers()77 public List<Contact> getMembers(){ 78 return Collections.unmodifiableList(mMembers); 79 } 80 81 /** 82 * Adds a member to this group. 83 * TODO: more docs on async callbacks. 84 * 85 * @param contact the member to add. 86 */ addMemberAsync(Contact contact)87 public synchronized void addMemberAsync(Contact contact) { 88 mManager.addGroupMemberAsync(this, contact); 89 } 90 91 /** 92 * Removes a member from this group. 93 * TODO: more docs on async callbacks. 94 * 95 * @param contact the member to remove. 96 */ removeMemberAsync(Contact contact)97 public synchronized void removeMemberAsync(Contact contact) { 98 mManager.removeGroupMemberAsync(this, contact); 99 } 100 101 /** 102 * Notifies that a contact has joined into this group. 103 * 104 * @param contact the contact who has joined into the group. 105 */ notifyMemberJoined(Contact contact)106 void notifyMemberJoined(Contact contact) { 107 mMembers.add(contact); 108 for(GroupMemberListener listener : mMemberListeners) { 109 listener.onMemberJoined(this, contact); 110 } 111 } 112 113 /** 114 * Notifies that a contact has left this group. 115 * 116 * @param contact the contact who has left this group. 117 */ notifyMemberLeft(Contact contact)118 void notifyMemberLeft(Contact contact) { 119 if(mMembers.remove(contact)) { 120 for(GroupMemberListener listener : mMemberListeners) { 121 listener.onMemberLeft(this, contact); 122 } 123 } 124 } 125 126 /** 127 * Notifies that previous operation on this group has failed. 128 * 129 * @param error the error information. 130 */ notifyGroupMemberError(ImErrorInfo error)131 void notifyGroupMemberError(ImErrorInfo error) { 132 for(GroupMemberListener listener : mMemberListeners) { 133 listener.onError(this, error); 134 } 135 } 136 } 137