src/com/beem/project/beem/service/Message.java
author dasilvj
Tue, 26 May 2009 19:56:38 +0200
changeset 212 bbc0b169cdf0
parent 92 e48817ca2398
child 224 d8e2cb1eb895
permissions -rw-r--r--
Issues #124, #67, #88

/**
 * 
 */
package com.beem.project.beem.service;

import org.jivesoftware.smack.packet.XMPPError;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 * This class represents a instant message.
 * 
 * @author darisk
 */
public class Message implements Parcelable {

    /**
     * Normal message type. Theese messages are like an email, with subject.
     */
    public static final int                         MSG_TYPE_NORMAL     = 100;

    /**
     * Chat message type.
     */
    public static final int                         MSG_TYPE_CHAT       = 200;

    /**
     * Group chat message type.
     */
    public static final int                         MSG_TYPE_GROUP_CHAT = 300;

    /**
     * Error message type.
     */
    public static final int                         MSG_TYPE_ERROR      = 400;

    private int                                     mType;
    private String                                  mBody;
    private String                                  mSubject;
    private String                                  mTo;
    private String                                  mThread;
    // TODO ajouter l'erreur

    /**
     * Parcelable.Creator needs by Android.
     */
    public static final Parcelable.Creator<Message> CREATOR             = new Parcelable.Creator<Message>() {

	                                                                    @Override
	                                                                    public Message createFromParcel(
	                                                                            Parcel source) {
		                                                                return new Message(source);
	                                                                    }

	                                                                    @Override
	                                                                    public Message[] newArray(int size) {
		                                                                return new Message[size];
	                                                                    }
	                                                                };

    public Message(org.jivesoftware.smack.packet.Message smackMsg) {
	this(smackMsg.getTo());
	switch (smackMsg.getType()) {
	case chat:
	    mType = MSG_TYPE_CHAT;
	    break;
	case groupchat:
	    mType = MSG_TYPE_GROUP_CHAT;
	    break;
	case normal:
	    mType = MSG_TYPE_NORMAL;
	    break;
	// TODO gerer les message de type error
	// this a little work around waiting for a better handling of error messages
	case error:
	    mType = MSG_TYPE_ERROR;
	    break;
	default:
	    Log.w("BEEM_MESSAGE", "type de message non gerer" + smackMsg.getType());
	    break;
	}
	if (mType == MSG_TYPE_ERROR) {
	    XMPPError er = smackMsg.getError();
	    String msg = er.getMessage();
	    if (msg != null)
		mBody = msg;
	    else
		mBody = er.getCondition();
	} else {
	    mBody = smackMsg.getBody();
	    mSubject = smackMsg.getSubject();
	    mThread = smackMsg.getThread();
	}
    }

    /**
     * Construct a message from a parcel.
     * 
     * @param in
     *            parcel to use for construction
     */
    private Message(final Parcel in) {
	mType = in.readInt();
	mTo = in.readString();
	mBody = in.readString();
	mSubject = in.readString();
	mThread = in.readString();
    }

    /**
     * Constructor a message of type chat.
     * 
     * @param to
     *            the destinataire of the message
     */
    public Message(final String to) {
	this(to, MSG_TYPE_CHAT);
    }

    /**
     * Constructor.
     * 
     * @param to
     *            the destinataire of the message
     * @param type
     *            the message type
     */
    public Message(final String to, final int type) {
	mTo = to;
	mType = type;
	mBody = "";
	mSubject = "";
	mThread = "";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int describeContents() {
	// TODO Auto-generated method stub
	return 0;
    }

    /**
     * Get the body of the message.
     * 
     * @return the Body of the message
     */
    public String getBody() {
	return mBody;
    }

    /**
     * Get the subject of the message.
     * 
     * @return the subject
     */
    public String getSubject() {
	return mSubject;
    }

    /**
     * Get the thread of the message.
     * 
     * @return the thread
     */
    public String getThread() {
	return mThread;
    }

    /**
     * Get the destinataire of the message.
     * 
     * @return the destinataire of the message
     */
    public String getTo() {
	return mTo;
    }

    /**
     * Get the type of the message.
     * 
     * @return the type of the message.
     */
    public int getType() {
	return mType;
    }

    /**
     * Set the body of the message.
     * 
     * @param body
     *            the body to set
     */
    public void setBody(String body) {
	mBody = body;
    }

    /**
     * Set the subject of the message.
     * 
     * @param subject
     *            the subject to set
     */
    public void setSubject(String subject) {
	mSubject = subject;
    }

    /**
     * Set the thread of the message.
     * 
     * @param thread
     *            the thread to set
     */
    public void setThread(String thread) {
	mThread = thread;
    }

    /**
     * Set the destinataire of the message.
     * 
     * @param to
     *            the destinataire to set
     */
    public void setTo(String to) {
	mTo = to;
    }

    /**
     * Set the type of the message.
     * 
     * @param type
     *            the type to set
     */
    public void setType(int type) {
	mType = type;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
	// TODO Auto-generated method stub
	dest.writeInt(mType);
	dest.writeString(mTo);
	dest.writeString(mBody);
	dest.writeString(mSubject);
	dest.writeString(mThread);
    }

}