Android Open Source - android_lucidchat Container Center






From Project

Back to project page android_lucidchat.

License

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.

Java Source Code

/*
   Copyright 2013 Harri Smatt/*from   ww w. j  a v  a2s.  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;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
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.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import fi.harism.lucidchat.api.Conversation;
import fi.harism.lucidchat.api.Message;
import fi.harism.lucidchat.api.User;

/**
 * Main container UI class.
 */
public class ContainerCenter extends RelativeLayout implements
    Conversation.Observer {

  private ContainerMenu mContainerMenu;
  private Conversation mConversation;
  Date mDate = new Date();
  SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss",
      Locale.getDefault());
  private LayoutInflater mInflater;
  private Observer mObserver;

  /**
   * Default constructor.
   */
  public ContainerCenter(Context context) {
    super(context);
  }

  /**
   * Default constructor.
   */
  public ContainerCenter(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  /**
   * Default constructor.
   */
  public ContainerCenter(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  /**
   * Add message to chat container.
   */
  private void addMessage(Message message) {
    View v;
    String title;
    ViewGroup chatContainer = (ViewGroup) findViewById(R.id.container_chat);
    // Parse time string from millis.
    mDate.setTime(message.getTime());
    String time = mDateFormat.format(mDate);
    // Choose view based on message type.
    if (message.getType() == Message.TYPE_LOG) {
      v = mInflater.inflate(R.layout.item_chat_log, null);
      title = getContext().getString(R.string.item_title_server, time);
    } else {
      v = mInflater.inflate(R.layout.item_chat_conversation, null);
      title = getContext().getString(R.string.item_title_conversation,
          time, message.getSender());
    }
    // Set message title and content.
    ((TextView) v.findViewById(R.id.item_chat_title)).setText(title);
    ((TextView) v.findViewById(R.id.item_chat_message)).setText(message
        .getMessage());
    chatContainer.addView(v);
  }

  /**
   * Getter for currently active conversation.
   */
  public Conversation getConversation() {
    return mConversation;
  }

  /**
   * Get menu visibility.
   */
  public boolean isMenuVisible() {
    return getX() != 0;
  }

  @Override
  public void onConversationInfoChanged(Conversation conversation) {
    post(new Runnable() {
      @Override
      public void run() {
        // Fade in new title if needed.
        TextView title = (TextView) findViewById(R.id.textview_title);
        if (!title.getText().equals(mConversation.getTitle())) {
          setTextViewText(title, mConversation.getTitle());
        }
        // Fade in new caption if needed.
        TextView caption = (TextView) findViewById(R.id.textview_caption);
        if (!caption.getText().equals(mConversation.getCaption())) {
          setTextViewText(caption, mConversation.getCaption());
        }
      }
    });
  }

  @Override
  public void onConversationMemberAdded(Conversation conversation, User user) {
    // Don't care about this callback.
  }

  @Override
  public void onConversationMemberRemoved(Conversation conversation, User user) {
    // Don't care about this callback.
  }

  @Override
  public void onConversationMemberUpdated(Conversation conversation, User user) {
    // Don't care about this callback.
  }

  @Override
  public void onConversationMessageAdded(Conversation conversation,
      final Message message) {
    post(new Runnable() {
      @Override
      public void run() {
        addMessage(message);
      }
    });
  }

  @Override
  public void onFinishInflate() {
    super.onFinishInflate();

    mInflater = LayoutInflater.from(getContext());

    findViewById(R.id.button_menu).setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            setMenuVisible(!isMenuVisible());
          }
        });
    findViewById(R.id.button_send).setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            TextView tv = (TextView) findViewById(R.id.edittext_send);
            if (mObserver.onSendMessage(mConversation, tv.getText()
                .toString())) {
              tv.setText("");
            }
          }
        });
    ((EditText) findViewById(R.id.edittext_send))
        .setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView tv, int actionId,
              KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
              if (mObserver.onSendMessage(mConversation, tv
                  .getText().toString())) {
                tv.setText("");
              }
              return true;
            }
            return false;
          }
        });
  }

  /**
   * Set observer for center UI elements.
   */
  public void setCenterObserver(Observer observer) {
    mObserver = observer;
  }

  /**
   * Shows given conversation.
   */
  public void setConversation(Conversation conversation) {
    if (conversation == mConversation) {
      return;
    }

    if (mConversation != null) {
      mConversation.removeObserver(this);
    }

    conversation.addObserver(this);
    onConversationInfoChanged(conversation);
    mConversation = conversation;

    PropertyValuesHolder holderAlpha = PropertyValuesHolder.ofFloat(
        "alpha", getAlpha(), 0);
    ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(
        findViewById(R.id.container_chat), holderAlpha)
        .setDuration(500);

    alphaAnim.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        setVisibility(View.GONE);

        ViewGroup chatContainer = (ViewGroup) findViewById(R.id.container_chat);
        chatContainer.removeAllViews();
        for (Message message : mConversation.getMessages()) {
          addMessage(message);
        }

        setVisibility(View.VISIBLE);

        PropertyValuesHolder holderAlpha = PropertyValuesHolder
            .ofFloat("alpha", 0, 1);
        ObjectAnimator alphaAnim = ObjectAnimator
            .ofPropertyValuesHolder(chatContainer, holderAlpha)
            .setDuration(500);
        alphaAnim.start();
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }

      @Override
      public void onAnimationStart(Animator animation) {
      }
    });

    alphaAnim.start();
  }

  /**
   * Set menu container for being used whilst scrolling between menu
   * visibility.
   */
  public void setMenuContainer(ContainerMenu containerMenu) {
    mContainerMenu = containerMenu;
  }

  /**
   * Set menu visibility.
   */
  public void setMenuVisible(boolean visible) {
    setSendEnabled(!visible);

    if (!visible) {
      PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("x",
          getX(), 0);
      ObjectAnimator centerAnim = ObjectAnimator.ofPropertyValuesHolder(
          this, holderX).setDuration(500);
      centerAnim.addListener(new AnimatorListener() {
        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
          mContainerMenu.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationStart(Animator animation) {
        }
      });
      centerAnim.start();
    } else {
      mContainerMenu.setVisibility(View.VISIBLE);
      int newX = mContainerMenu.getWidth();
      PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("x",
          getX(), newX);
      ObjectAnimator centerAnim = ObjectAnimator.ofPropertyValuesHolder(
          this, holderX).setDuration(500);
      centerAnim.start();
    }
  }

  /**
   * Set send area enabled state.
   */
  public void setSendEnabled(boolean enabled) {
    findViewById(R.id.edittext_send).setEnabled(enabled);
    findViewById(R.id.edittext_send).setFocusable(enabled);
    findViewById(R.id.edittext_send).setFocusableInTouchMode(enabled);
  }

  /**
   * Fades in new text for given TextView.
   */
  private void setTextViewText(final TextView textView, final String text) {
    PropertyValuesHolder holderAlpha = PropertyValuesHolder.ofFloat(
        "alpha", getAlpha(), 0);
    ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(
        textView, holderAlpha).setDuration(500);

    alphaAnim.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        textView.setText(text);
        PropertyValuesHolder holderAlpha = PropertyValuesHolder
            .ofFloat("alpha", 0, 1);
        ObjectAnimator alphaAnim = ObjectAnimator
            .ofPropertyValuesHolder(textView, holderAlpha)
            .setDuration(500);
        alphaAnim.start();
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }

      @Override
      public void onAnimationStart(Animator animation) {
      }
    });

    alphaAnim.start();
  }

  /**
   * Center are UI element observer.
   */
  public interface Observer {

    /**
     * Sent once user has selected send from send area. Return true if
     * message was sent properly.
     */
    public boolean onSendMessage(Conversation conversation, String text);
  }

}




Java Source Code List

fi.harism.lucidchat.ContainerCenter.java
fi.harism.lucidchat.ContainerMenu.java
fi.harism.lucidchat.ConversationScrollView.java
fi.harism.lucidchat.DialogConnect.java
fi.harism.lucidchat.MainActivity.java
fi.harism.lucidchat.PreferencesHandler.java
fi.harism.lucidchat.SplashActivity.java
fi.harism.lucidchat.api.Channel.java
fi.harism.lucidchat.api.ChatConnection.java
fi.harism.lucidchat.api.ChatHandler.java
fi.harism.lucidchat.api.Conversation.java
fi.harism.lucidchat.api.Message.java
fi.harism.lucidchat.api.User.java
fi.harism.lucidchat.quickaction.ActionItem.java
fi.harism.lucidchat.quickaction.PopupWindows.java
fi.harism.lucidchat.quickaction.QuickAction.java
fi.harism.lucidchat.websocket.HybiParser.java
fi.harism.lucidchat.websocket.WebSocketClient.java