Android Open Source - schat Message






From Project

Back to project page schat.

License

The source code is released under:

MIT License

If you think the Android project schat 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

package data;
/*  www  .  ja v a2 s. c o m*/
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This class represents a plain message(nothing is encrypted or authenticated). This message can contain any type of "Content".
 *
 * @param <C> the type of the Content, which will be stored in this class
 * @author Elias Frantar
 * @version 15.11.2013
 */
public class Message<C extends Content> implements Serializable {

    /* protected because we want to have these attributes in the secure message class */
    protected Date timestamp;
    protected String sender;
    protected String receiver;

    private C content; // this should >> not << be in any subclasses, so private

    /**
     * Creates a new message from the given information
     *
     * @param timestamp the time the message was created
     * @param sender    the sender of the message
     * @param receiver  the receiver of the message
     * @param content   the content of the message
     */
    public Message(Date timestamp, String sender, String receiver, C content) {
        this.timestamp = timestamp;
        this.sender = sender;
        this.receiver = receiver;
        this.content = content;
    }

    /**
     * Creates a new message without content. This constructor should only be called from the SecureMessage-class.
     *
     * @param timestamp the time the message is created
     * @param sender    the sender of the message
     * @param receiver  the receiver of the message
     */
    protected Message(Date timestamp, String sender, String receiver) { // this constructor should only be accessed from the secure message class, so protected
        this.timestamp = timestamp;
        this.sender = sender;
        this.receiver = receiver;
    }

    /**
     * Returns this message as a properly formatted and readable String.
     *
     * @return this message as a String
     */
    public String toString() {
        StringBuilder result = new StringBuilder("");

        result.append(requiredInfoToString());
        result.append(content); // automatically calls the toString()-method

        return result.toString();
    }

    /**
     * Returns the timestamp, sender and receiver of this message in a properly formatted and readable String.
     *
     * @return the timestamp, sender and receiver as a String
     */
    protected String requiredInfoToString() { // should be accessible for sub-classes
        StringBuilder result = new StringBuilder("");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");

        result.append(sdf.format(timestamp) + "\n");
        result.append("Sender: " + sender + "\n");
        result.append("Receiver: " + receiver + "\n");

        return result.toString();
    }

    public String getTimestampString() {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm dd.MM.yyyy");
        return sdf.format(timestamp);
    }

    /* once data is saved in an object of this class, it should not be modified anymore */
    public Date getTimestamp() {
        return timestamp;
    }

    public String getSender() {
        return sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public C getContent() {
        return content;
    }
}




Java Source Code List

.ServerMain.java
com.activities.Activity_Chat.java
com.activities.Activity_ContactList.java
com.activities.ContactView.java
com.data.AddContact.java
com.data.AndroidSQLManager.java
com.data.ApplicationUser.java
com.data.ChatAdapter.java
com.data.ChatArrayList.java
com.data.MySQLiteHelper.java
com.security.AndroidKeyPairManager.java
com.security.PRNGFixes.java
com.services.MessageService.java
crypto.CryptoConstants.java
crypto.Cryptography.java
crypto.Envelope.java
crypto.SecureMessage.java
crypto.TestCrypto.java
data.ChatMessage.java
data.Content.java
data.DatabaseManager.java
data.KeyPairManager.java
data.Message.java
data.SQLiteManager.java
data.User.java
data.contents.ChatContent.java
data.contents.LoginSuccess.java
data.contents.Login.java
data.contents.PublicKeyRequest.java
data.contents.PublicKeyResponse.java
data.contents.Registration.java
networking.SChatClientListener.java
networking.SChatClientWriter.java
networking.SChatClient.java
networking.SChatServerThread.java
networking.SChatServer.java