1e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project/*
2e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2007 Esmertec AG.
3e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
4e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
5e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
6e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * you may not use this file except in compliance with the License.
7e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * You may obtain a copy of the License at
8e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
9e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
10e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
11e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
12e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
13e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * See the License for the specific language governing permissions and
15e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * limitations under the License.
16e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project */
17e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
18e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectpackage com.android.im.engine;
19e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
20e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.Collections;
21e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.List;
22e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.Vector;
23e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.concurrent.CopyOnWriteArrayList;
24e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
25e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.util.Log;
26e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
27e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project/**
28e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * A ChatSession represents a conversation between two users. A ChatSession has
29e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * a unique participant which is either another user or a group.
30e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project */
31e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectpublic class ChatSession {
32e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private ImEntity mParticipant;
33e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private ChatSessionManager mManager;
34e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private CopyOnWriteArrayList<MessageListener> mListeners;
35e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private Vector<Message> mHistoryMessages;
36e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
37e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
38e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Creates a new ChatSession with a particular participant.
39e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
40e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param participant the participant with who the user communicates.
41e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param connection the underlying network connection.
42e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
43e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    ChatSession(ImEntity participant, ChatSessionManager manager) {
44e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mParticipant = participant;
45e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mManager = manager;
46e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mListeners = new CopyOnWriteArrayList<MessageListener>();
47e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mHistoryMessages = new Vector<Message>();
48e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
49e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
50e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public ImEntity getParticipant() {
51e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return mParticipant;
52e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
53e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
54e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void setParticipant(ImEntity participant) {
55e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mParticipant = participant;
56e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
57e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
58e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
59e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Adds a MessageListener so that it can be notified of any new message in
60e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * this session.
61e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
62e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param listener
63e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
64e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public synchronized void addMessageListener(MessageListener listener) {
65e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if ((listener != null) && !mListeners.contains(listener)) {
66e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mListeners.add(listener);
67e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
68e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
69e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
70e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
71e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Removes a listener from this session.
72e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
73e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param listener
74e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
75e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public synchronized void removeMessageListener(MessageListener listener) {
76e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mListeners.remove(listener);
77e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
78e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
79e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
80e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Sends a text message to other participant(s) in this session
81e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * asynchronously.
82e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * TODO: more docs on async callbacks.
83e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
84e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param text the text to send.
85e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
86e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void sendMessageAsync(String text) {
87e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        Message message = new Message(text);
88e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        sendMessageAsync(message);
89e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
90e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
91e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
92e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Sends a message to other participant(s) in this session asynchronously.
93e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * TODO: more docs on async callbacks.
94e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
95e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param msg the message to send.
96e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
97e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void sendMessageAsync(Message msg) {
98e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        msg.setTo(mParticipant.getAddress());
99e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
100e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mHistoryMessages.add(msg);
101e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mManager.sendMessageAsync(this, msg);
102e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
103e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
104e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
105e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Called by ChatSessionManager when received a message of the ChatSession.
106e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * All the listeners registered in this session will be notified.
107e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
108e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param msg the received message.
109e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
110e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void onReceiveMessage(Message msg) {
111e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mHistoryMessages.add(msg);
112e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
113e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        for (MessageListener listener : mListeners) {
114e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            listener.onIncomingMessage(this, msg);
115e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
116e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
117e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
118e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
119e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Called by ChatSessionManager when an error occurs to send a message.
120e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param message
121e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
122e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @param error the error information.
123e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
124e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void onSendMessageError(Message message, ImErrorInfo error) {
125e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        for (MessageListener listener : mListeners) {
126e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            listener.onSendMessageError(this, message, error);
127e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
128e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
129e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
130e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public void onSendMessageError(String msgId, ImErrorInfo error) {
131e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        for(Message msg : mHistoryMessages){
132e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            if(msgId.equals(msg.getID())){
133e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                onSendMessageError(msg, error);
134e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return;
135e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
136e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
137e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        Log.i("ChatSession", "Message has been removed when we get delivery error:"
138e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                + error);
139e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
140e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
141e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
142e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Returns a unmodifiable list of the history messages in this session.
143e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     *
144e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * @return a unmodifiable list of the history messages in this session.
145e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
146e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public List<Message> getHistoryMessages() {
147e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return Collections.unmodifiableList(mHistoryMessages);
148e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
149e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project}
150