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.Collections; 21 import java.util.List; 22 import java.util.Vector; 23 import java.util.concurrent.CopyOnWriteArrayList; 24 25 import android.util.Log; 26 27 /** 28 * A ChatSession represents a conversation between two users. A ChatSession has 29 * a unique participant which is either another user or a group. 30 */ 31 public class ChatSession { 32 private ImEntity mParticipant; 33 private ChatSessionManager mManager; 34 private CopyOnWriteArrayList<MessageListener> mListeners; 35 private Vector<Message> mHistoryMessages; 36 37 /** 38 * Creates a new ChatSession with a particular participant. 39 * 40 * @param participant the participant with who the user communicates. 41 * @param connection the underlying network connection. 42 */ 43 ChatSession(ImEntity participant, ChatSessionManager manager) { 44 mParticipant = participant; 45 mManager = manager; 46 mListeners = new CopyOnWriteArrayList<MessageListener>(); 47 mHistoryMessages = new Vector<Message>(); 48 } 49 50 public ImEntity getParticipant() { 51 return mParticipant; 52 } 53 54 public void setParticipant(ImEntity participant) { 55 mParticipant = participant; 56 } 57 58 /** 59 * Adds a MessageListener so that it can be notified of any new message in 60 * this session. 61 * 62 * @param listener 63 */ 64 public synchronized void addMessageListener(MessageListener listener) { 65 if ((listener != null) && !mListeners.contains(listener)) { 66 mListeners.add(listener); 67 } 68 } 69 70 /** 71 * Removes a listener from this session. 72 * 73 * @param listener 74 */ 75 public synchronized void removeMessageListener(MessageListener listener) { 76 mListeners.remove(listener); 77 } 78 79 /** 80 * Sends a text message to other participant(s) in this session 81 * asynchronously. 82 * TODO: more docs on async callbacks. 83 * 84 * @param text the text to send. 85 */ 86 public void sendMessageAsync(String text) { 87 Message message = new Message(text); 88 sendMessageAsync(message); 89 } 90 91 /** 92 * Sends a message to other participant(s) in this session asynchronously. 93 * TODO: more docs on async callbacks. 94 * 95 * @param msg the message to send. 96 */ 97 public void sendMessageAsync(Message msg) { 98 msg.setTo(mParticipant.getAddress()); 99 100 mHistoryMessages.add(msg); 101 mManager.sendMessageAsync(this, msg); 102 } 103 104 /** 105 * Called by ChatSessionManager when received a message of the ChatSession. 106 * All the listeners registered in this session will be notified. 107 * 108 * @param msg the received message. 109 */ 110 public void onReceiveMessage(Message msg) { 111 mHistoryMessages.add(msg); 112 113 for (MessageListener listener : mListeners) { 114 listener.onIncomingMessage(this, msg); 115 } 116 } 117 118 /** 119 * Called by ChatSessionManager when an error occurs to send a message. 120 * @param message 121 * 122 * @param error the error information. 123 */ 124 public void onSendMessageError(Message message, ImErrorInfo error) { 125 for (MessageListener listener : mListeners) { 126 listener.onSendMessageError(this, message, error); 127 } 128 } 129 130 public void onSendMessageError(String msgId, ImErrorInfo error) { 131 for(Message msg : mHistoryMessages){ 132 if(msgId.equals(msg.getID())){ 133 onSendMessageError(msg, error); 134 return; 135 } 136 } 137 Log.i("ChatSession", "Message has been removed when we get delivery error:" 138 + error); 139 } 140 141 /** 142 * Returns a unmodifiable list of the history messages in this session. 143 * 144 * @return a unmodifiable list of the history messages in this session. 145 */ 146 public List<Message> getHistoryMessages() { 147 return Collections.unmodifiableList(mHistoryMessages); 148 } 149 } 150