• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Vector;
21 import java.util.concurrent.CopyOnWriteArrayList;
22 
23 /**
24  * The ChatSessionManager keeps track of all current chat sessions and is also
25  * responsible to dispatch the new incoming message to the right session.
26  */
27 public abstract class ChatSessionManager {
28 
29     private CopyOnWriteArrayList<ChatSessionListener> mListeners;
30 
31     /** Map session to the participant communicate with. */
32     protected Vector<ChatSession> mSessions;
33 
ChatSessionManager()34     protected ChatSessionManager() {
35         mListeners = new CopyOnWriteArrayList<ChatSessionListener>();
36         mSessions = new Vector<ChatSession>();
37     }
38 
39     /**
40      * Registers a ChatSessionListener with the ChatSessionManager to receive
41      * events related to ChatSession.
42      *
43      * @param listener the listener
44      */
addChatSessionListener(ChatSessionListener listener)45     public synchronized void addChatSessionListener(ChatSessionListener listener) {
46         if ((listener != null) && !mListeners.contains(listener)) {
47             mListeners.add(listener);
48         }
49     }
50 
51     /**
52      * Removes a ChatSessionListener so that it will no longer be notified.
53      *
54      * @param listener the listener to remove.
55      */
removeChatSessionListener(ChatSessionListener listener)56     public synchronized void removeChatSessionListener(ChatSessionListener listener) {
57         mListeners.remove(listener);
58     }
59 
60     /**
61      * Creates a new ChatSession with specified participant.
62      *
63      * @param participant the participant.
64      * @return the created ChatSession.
65      */
createChatSession(ImEntity participant)66     public synchronized ChatSession createChatSession(ImEntity participant) {
67         for(ChatSession session : mSessions) {
68             if(session.getParticipant().equals(participant)) {
69                 return session;
70             }
71         }
72 
73         ChatSession session = new ChatSession(participant, this);
74         for (ChatSessionListener listener : mListeners) {
75             listener.onChatSessionCreated(session);
76         }
77 
78         mSessions.add(session);
79 
80         return session;
81     }
82 
83     /**
84      * Closes a ChatSession. This only removes the session from the list; the
85      * protocol implementation should override this if it has special work to
86      * do.
87      *
88      * @param session the ChatSession to close.
89      */
closeChatSession(ChatSession session)90     public void closeChatSession(ChatSession session) {
91         mSessions.remove(session);
92     }
93 
94     /**
95      * Sends a message to specified participant(s) asynchronously.
96      * TODO: more docs on async callbacks.
97      *
98      * @param message the message to send.
99      */
sendMessageAsync(ChatSession session, Message message)100     protected abstract void sendMessageAsync(ChatSession session, Message message);
101 }
102