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 w w w. j av a 2 s .c o 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; import java.util.List; import java.util.Vector; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import fi.harism.lucidchat.api.Channel; import fi.harism.lucidchat.api.Conversation; import fi.harism.lucidchat.api.Message; import fi.harism.lucidchat.api.User; import fi.harism.lucidchat.quickaction.ActionItem; import fi.harism.lucidchat.quickaction.QuickAction; /** * Menu container UI handler class. */ public class ContainerMenu extends LinearLayout implements Conversation.Observer { static final int MENU_CONVERSATIONS = 2; static final int MENU_MEMBERS = 1; static final int MENU_NONE = 0; static final int MENU_SEARCH_RESULTS = 3; private Conversation mConversation; private List<Conversation> mConversations; private int mCurrentMenu = MENU_NONE; private LayoutInflater mInflater; private Observer mObserver; private Vector<Channel> mSearchChannels = new Vector<Channel>(); private Vector<User> mSearchUsers = new Vector<User>(); /** * Default constructor. */ public ContainerMenu(Context context) { super(context); } /** * Default constructor. */ public ContainerMenu(Context context, AttributeSet attrs) { super(context, attrs); } /** * Default constructor. */ public ContainerMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * Adds search results to result lists. */ public void addSearchResults(List<User> users, List<Channel> channels) { mSearchUsers.addAll(users); mSearchChannels.addAll(channels); showSearchResultList(); } /** * Returns current conversation. */ public Conversation getConversation() { return mConversation; } @Override public void onConversationInfoChanged(final Conversation conversation) { if (mCurrentMenu != MENU_CONVERSATIONS) { return; } post(new Runnable() { @Override public void run() { ViewGroup container = (ViewGroup) findViewById(R.id.container_menu_scroll); for (int i = 0; i < container.getChildCount(); ++i) { View v = container.getChildAt(i); if (v.getTag().equals(conversation)) { TextView title = (TextView) v .findViewById(R.id.item_menu_title); TextView caption = (TextView) v .findViewById(R.id.item_menu_caption); title.setText(conversation.getTitle()); caption.setText(conversation.getCaption()); v.setTag(conversation); } } } }); } @Override public void onConversationMemberAdded(final Conversation conversation, User user) { if (conversation == mConversation) { post(new Runnable() { @Override public void run() { if (mCurrentMenu == MENU_MEMBERS) { showMemberList(); } } }); } } @Override public void onConversationMemberRemoved(final Conversation conversation, User user) { if (conversation == mConversation) { post(new Runnable() { @Override public void run() { if (mCurrentMenu == MENU_MEMBERS) { showMemberList(); } } }); } } @Override public void onConversationMemberUpdated(Conversation conversation, final User user) { if (conversation == mConversation && mCurrentMenu == MENU_MEMBERS) { post(new Runnable() { @Override public void run() { updateUser( user, (ViewGroup) findViewById(R.id.container_menu_scroll)); } }); } } @Override public void onConversationMessageAdded(Conversation conversation, Message message) { } @Override public void onFinishInflate() { super.onFinishInflate(); mInflater = LayoutInflater.from(getContext()); findViewById(R.id.button_search).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mSearchUsers.clear(); mSearchChannels.clear(); showSearchResultList(); EditText tv = (EditText) findViewById(R.id.edittext_search); mObserver.onSearch(tv.getText().toString()); } }); ((EditText) findViewById(R.id.edittext_search)) .setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { mSearchUsers.clear(); mSearchChannels.clear(); showSearchResultList(); mObserver.onSearch(tv.getText().toString()); return true; } return false; } }); findViewById(R.id.checkbox_search_results).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (mCurrentMenu != MENU_SEARCH_RESULTS) { showSearchResultList(); } updateCheckBoxes(); } }); findViewById(R.id.checkbox_members).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (mCurrentMenu != MENU_MEMBERS) { showMemberList(); } updateCheckBoxes(); } }); findViewById(R.id.checkbox_conversations).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (mCurrentMenu != MENU_CONVERSATIONS) { showConversationList(); } updateCheckBoxes(); } }); } /** * Remove conversation from conversations list. */ public void removeConversation(Conversation conversation) { if (mConversations != null) { mConversations.remove(conversation); } if (mCurrentMenu == MENU_CONVERSATIONS) { ViewGroup containerConversations = (ViewGroup) findViewById(R.id.container_menu_scroll); for (int i = containerConversations.getChildCount() - 1; i >= 0; --i) { if (containerConversations.getChildAt(i).getTag() == conversation) { containerConversations.removeViewAt(i); } } } } /** * Set current conversation. */ public void setConversation(Conversation conversation) { if (mConversation != null) { mConversation.removeObserver(this); } conversation.addObserver(this); mConversation = conversation; if (mCurrentMenu == MENU_MEMBERS) { showMemberList(); } } /** * Set list of active conversations. */ public void setConversations(List<Conversation> conversations) { mConversations = conversations; if (mCurrentMenu == MENU_CONVERSATIONS) { showConversationList(); } } /** * Set observer for menu container. */ public void setMenuObserver(Observer observer) { mObserver = observer; } /** * Helper method for updating current conversations list. */ private void showConversationList() { mCurrentMenu = MENU_CONVERSATIONS; updateCheckBoxes(); ViewGroup container = (ViewGroup) findViewById(R.id.container_menu_scroll); container.removeAllViews(); if (mConversations == null) { return; } container.setVisibility(View.GONE); for (Conversation conversation : mConversations) { View v = mInflater.inflate(R.layout.item_menu, null); ((TextView) v.findViewById(R.id.item_menu_title)) .setText(conversation.getTitle()); ((TextView) v.findViewById(R.id.item_menu_caption)) .setText(conversation.getCaption()); if (conversation.getType() == Conversation.TYPE_CHANNEL) { ((ImageView) v.findViewById(R.id.item_menu_icon)) .setImageResource(R.drawable.ic_channel); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setColorFilter(0xFF808080); } if (conversation.getType() == Conversation.TYPE_USER) { ((ImageView) v.findViewById(R.id.item_menu_icon)) .setImageResource(R.drawable.ic_user); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setColorFilter(0xFF808080); } v.setTag(conversation); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { QuickAction qa = new QuickAction(getContext()); qa.addActionItem(new ActionItem(1, "View")); qa.addActionItem(new ActionItem(2, "Leave")); qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction source, int pos, int actionId) { switch (actionId) { case 1: mObserver.onShowConversation((Conversation) v .getTag()); break; case 2: mObserver.onPartConversation((Conversation) v .getTag()); break; } } }); qa.show(v); } }); container.addView(v); } container.setVisibility(View.VISIBLE); } /** * Helper method for updating current conversation user list. */ private void showMemberList() { mCurrentMenu = MENU_MEMBERS; updateCheckBoxes(); ViewGroup container = (ViewGroup) findViewById(R.id.container_menu_scroll); container.removeAllViews(); if (mConversation == null) { return; } container.setVisibility(View.GONE); for (User user : mConversation.getMembers()) { if (user.getConnected() == false) { continue; } View v = mInflater.inflate(R.layout.item_menu, null); ((TextView) v.findViewById(R.id.item_menu_title)).setText(user .getName()); ((TextView) v.findViewById(R.id.item_menu_caption)).setText(user .getRealName()); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setImageResource(R.drawable.ic_user); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setColorFilter(0xFF808080); v.setTag(user); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { QuickAction qa = new QuickAction(getContext()); qa.addActionItem(new ActionItem(1, "Chat")); qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction source, int pos, int actionId) { switch (actionId) { case 1: mObserver.onStartConversation((User) v.getTag()); break; } } }); qa.show(v); } }); container.addView(v); } container.setVisibility(View.VISIBLE); } /** * Helper method for showing search result list. */ private void showSearchResultList() { mCurrentMenu = MENU_SEARCH_RESULTS; updateCheckBoxes(); ViewGroup container = (ViewGroup) findViewById(R.id.container_menu_scroll); container.setVisibility(View.GONE); container.removeAllViews(); for (Channel channel : mSearchChannels) { View v = mInflater.inflate(R.layout.item_menu, null); ((TextView) v.findViewById(R.id.item_menu_title)).setText(channel .getName()); ((TextView) v.findViewById(R.id.item_menu_caption)).setText(channel .getTopic()); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setImageResource(R.drawable.ic_channel); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setColorFilter(0xFF808080); v.setTag(channel); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { QuickAction qa = new QuickAction(getContext()); qa.addActionItem(new ActionItem(1, "Join")); qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction source, int pos, int actionId) { if (actionId == 1) { mObserver.onJoinChannel((Channel) v.getTag()); } } }); qa.show(v); } }); container.addView(v); } for (User user : mSearchUsers) { View v = mInflater.inflate(R.layout.item_menu, null); ((TextView) v.findViewById(R.id.item_menu_title)).setText(user .getName()); ((TextView) v.findViewById(R.id.item_menu_caption)).setText(user .getRealName()); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setImageResource(R.drawable.ic_user); ((ImageView) v.findViewById(R.id.item_menu_icon)) .setColorFilter(0xFF808080); v.setTag(user); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { QuickAction qa = new QuickAction(getContext()); qa.addActionItem(new ActionItem(1, "Chat")); qa.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { @Override public void onItemClick(QuickAction source, int pos, int actionId) { switch (actionId) { case 1: mObserver.onStartConversation((User) v.getTag()); break; } } }); qa.show(v); } }); container.addView(v); } container.setVisibility(View.VISIBLE); } /** * Puts current menu into checked state. */ private void updateCheckBoxes() { ((CheckBox) findViewById(R.id.checkbox_search_results)) .setChecked(mCurrentMenu == MENU_SEARCH_RESULTS); ((CheckBox) findViewById(R.id.checkbox_conversations)) .setChecked(mCurrentMenu == MENU_CONVERSATIONS); ((CheckBox) findViewById(R.id.checkbox_members)) .setChecked(mCurrentMenu == MENU_MEMBERS); } /** * Helper method for updating user information. */ private void updateUser(User user, ViewGroup container) { for (int i = 0; i < container.getChildCount(); ++i) { View v = container.getChildAt(i); if (v.getTag().equals(user)) { TextView name = (TextView) v.findViewById(R.id.item_menu_title); TextView realName = (TextView) v .findViewById(R.id.item_menu_caption); name.setText(user.getName()); realName.setText(user.getRealName()); v.setTag(user); } } } /** * Menu event observer interface. */ public interface Observer { /** * Sent once user selects join channel. */ public void onJoinChannel(Channel channel); /** * Sent once user selects part conversation. */ public void onPartConversation(Conversation conversation); /** * Sent once user selects to search. */ public void onSearch(String searchText); /** * Sent once user selects to view conversation. */ public void onShowConversation(Conversation conversation); /** * Sent once user selects to start conversation. */ public void onStartConversation(User user); } }