Back to project page android_lucidchat.
The source code is released under:
Apache License
If you think the Android project android_lucidchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright 2013 Harri Smatt//from ww w .ja v a 2s .co m Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package fi.harism.lucidchat.api; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Vector; /** * Top-most implemention for chat handling. */ public class ChatHandler { private static final String HEADER_CHANNEL = "channel__"; private static final String HEADER_SERVER = "server_log"; private static final String HEADER_USER = "user__"; private ChatConnection mConnection = new ChatConnection( new ConnectionObserver()); private HashMap<String, Conversation> mConversationMap = new HashMap<String, Conversation>(); private Vector<Observer> mObservers = new Vector<Observer>(); /** * Default constructor. */ public ChatHandler() { mConversationMap.put(HEADER_SERVER, new Conversation( Conversation.TYPE_LOG, new Channel("Server", "Server log messages."))); } /** * Add new channel conversation for receiving messages and/or join messages. */ public void addConversation(Channel channel) { if (!hasConversation(channel)) { Conversation conversation = new Conversation( Conversation.TYPE_CHANNEL, channel); mConversationMap .put(HEADER_CHANNEL + channel.getId(), conversation); for (Observer observer : mObservers) { observer.onConversationAdded(conversation); } } } /** * Add new private user conversation. */ public void addConversation(User user) { if (!hasConversation(user)) { Conversation conversation = new Conversation( Conversation.TYPE_USER, user); conversation.addMember(user); mConversationMap.put(HEADER_USER + user.getId(), conversation); for (Observer observer : mObservers) { observer.onConversationAdded(conversation); } } } /** * Add observer for underlying chat connections events. */ public void addObserver(Observer observer) { if (!mObservers.contains(observer)) { mObservers.add(observer); } } /** * Connect to server. Once connected observer receives onServerConnect * event. */ public void connect(String name, String realName, boolean autojoin) { mConnection.connect(name, realName, autojoin); } /** * Disconnect from server. There will be onServerDisconnect event sent once * socket is actually closed. */ public void disconnect() { mConnection.sendDeleteUser(); } /** * Get conversation for given channel. */ public Conversation getConversation(Channel channel) { return mConversationMap.get(HEADER_CHANNEL + channel.getId()); } /** * Get conversation for given user. */ public Conversation getConversation(User user) { return mConversationMap.get(HEADER_USER + user.getId()); } /** * Get all conversations this chat handler observes. */ public List<Conversation> getConversations() { Vector<Conversation> conversations = new Vector<Conversation>(); for (Object key : mConversationMap.keySet()) { if (!key.equals(HEADER_SERVER)) { conversations.add(mConversationMap.get(key)); } } Collections.sort(conversations, new Comparator<Conversation>() { @Override public int compare(Conversation lhs, Conversation rhs) { return lhs.getTitle().compareToIgnoreCase(rhs.getTitle()); } }); return conversations; } /** * Return server log conversation. */ public Conversation getServerLog() { return mConversationMap.get(HEADER_SERVER); } /** * Check if there is conversation with given channel. */ public boolean hasConversation(Channel channel) { return mConversationMap.containsKey(HEADER_CHANNEL + channel.getId()); } /** * Check if there is conversation with given user. */ public boolean hasConversation(User user) { return mConversationMap.containsKey(HEADER_USER + user.getId()); } /** * Send join channel message to server. You will receive onConversationAdded * event once joining is complete. */ public void joinChannel(String channelId) { mConnection.sendJoinChannel(channelId); } /** * Send part channel message to server. You will receive * onConversationRemoved event once parting is complete. */ public void partChannel(String channelId) { mConnection.sendPartChannel(channelId); } /** * Reconnect to server with parameters received from previous * onServerConnect callback. */ public void reconnect(String userId, String userAuth, String sessionId) { mConnection.reconnect(userId, userAuth, sessionId); } /** * Remove conversation from handler. */ public void removeConversation(Conversation conversation) { if (conversation.getType() == Conversation.TYPE_USER) { User user = (User) conversation.getWith(); if (mConversationMap.remove(HEADER_USER + user.getId()) != null) { for (Observer observer : mObservers) { observer.onConversationRemoved(conversation); } } } else { partChannel(((Channel) conversation.getWith()).getId()); } } /** * Remove observer from handler. */ public void removeObserver(Observer observer) { mObservers.remove(observer); } /** * Send search message to server. You might receive multiple onSearchResults * callbacks once server responds. */ public void search(String searchText) { mConnection.sendSearch(searchText); } /** * Send message to conversation. You will receive message added notification * via conversation observer once server has handled the message sending. */ public boolean sendMessage(Conversation conversation, String message) { if (conversation.getType() == Conversation.TYPE_USER) { mConnection.sendMessageUser( ((User) conversation.getWith()).getId(), message); return true; } else if (conversation.getType() == Conversation.TYPE_CHANNEL) { mConnection.sendMessageChannel( ((Channel) conversation.getWith()).getId(), message); return true; } return false; } /** * Chat connection observer implementation. */ private class ConnectionObserver implements ChatConnection.Observer { @Override public void onChannelJoined(Channel channel, List<User> participants) { Conversation conversation = new Conversation( Conversation.TYPE_CHANNEL, channel); for (User user : participants) { conversation.addMember(user); } mConversationMap .put(HEADER_CHANNEL + channel.getId(), conversation); for (Observer observer : mObservers) { observer.onConversationAdded(conversation); } } @Override public void onChannelMessage(String channelId, Message message) { mConversationMap.get(HEADER_CHANNEL + channelId) .addMessage(message); } @Override public void onChannelParted(String channelId) { Conversation conversation = mConversationMap.remove(HEADER_CHANNEL + channelId); for (Observer observer : mObservers) { observer.onConversationRemoved(conversation); } } @Override public void onChannelUpdated(Channel channel) { mConversationMap.get(HEADER_CHANNEL + channel.getId()).setWith( channel); } @Override public void onConnect(String userId, String userAuth, String sessionId) { for (Observer observer : mObservers) { observer.onServerConnect(userId, userAuth, sessionId); } } @Override public void onDisconnect() { for (Observer observer : mObservers) { observer.onServerDisconnect(); } } @Override public void onInfoJoin(String channelId, String userId, String name) { if (mConversationMap.containsKey(HEADER_CHANNEL + channelId)) { for (Observer observer : mObservers) { observer.onInfoJoin( mConversationMap.get(HEADER_CHANNEL + channelId), userId, name); } } } @Override public void onInfoPart(String channelId, String userId, String name) { if (mConversationMap.containsKey(HEADER_CHANNEL + channelId)) { for (Observer observer : mObservers) { observer.onInfoPart( mConversationMap.get(HEADER_CHANNEL + channelId), userId, name); } } } @Override public void onSearchResults(List<Channel> channels, List<User> users) { for (Observer observer : mObservers) { observer.onSearchResults(channels, users); } } @Override public void onServerMessage(Message message) { mConversationMap.get(HEADER_SERVER).addMessage(message); } @Override public void onUserJoin(String channelId, User user) { if (mConversationMap.containsKey(HEADER_CHANNEL + channelId)) { mConversationMap.get(HEADER_CHANNEL + channelId) .addMember(user); } } @Override public void onUserMessage(User user, Message message) { if (mConversationMap.containsKey(HEADER_USER + user.getId())) { mConversationMap.get(HEADER_USER + user.getId()).addMessage( message); } else { Conversation conversation = new Conversation( Conversation.TYPE_USER, user); conversation.addMember(user); conversation.addMessage(message); mConversationMap.put(HEADER_USER + user.getId(), conversation); for (Observer observer : mObservers) { observer.onConversationAdded(conversation); } mConnection.sendDescribeUser(user.getId()); } } @Override public void onUserPart(String channelId, String userId) { if (mConversationMap.containsKey(HEADER_CHANNEL + channelId)) { mConversationMap.get(HEADER_CHANNEL + channelId).removeMember( userId); } } @Override public void onUserUpdated(User user) { for (Object key : mConversationMap.keySet()) { mConversationMap.get(key).updateMember(user); } } } /** * ChatHandler observer interface. */ public interface Observer { /** * Called once new conversation is added to handler. */ public void onConversationAdded(Conversation conversation); /** * Called once conversation has been removed from handler. */ public void onConversationRemoved(Conversation conversation); /** * Callback for channel join messages. */ public void onInfoJoin(Conversation conversation, String userId, String name); /** * Callback for channel part messages. */ public void onInfoPart(Conversation conversation, String userId, String name); /** * Called for results on search method. This method might be called * multiple times per search. */ public void onSearchResults(List<Channel> channels, List<User> users); /** * Called once underlying connection has been established. Client should * store given parameters for later use with reconnect. */ public void onServerConnect(String userId, String userAuth, String sessionId); /** * Called once underlying socket connection dies. */ public void onServerDisconnect(); } }