Android Open Source - CipherChat Chat Activity






From Project

Back to project page CipherChat.

License

The source code is released under:

MIT License

If you think the Android project CipherChat 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 com.ist.cipherchat.gui;
/* w w  w  .jav a2s .co m*/
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import javax.crypto.spec.SecretKeySpec;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ist.cipherchat.R;
import com.ist.cipherchat.networking.ChatActivityRunnable;
import com.ist.cipherchat.networking.ChatOutHandler;
import com.ist.cipherchat.networking.Globals;
import com.ist.cipherchat.networking.OutputSocketHandler;

public class ChatActivity extends Activity implements View.OnClickListener {

  static CyclicBarrier ChatBarrier;

  static ChatActivityRunnable chatActivityRunnable;
  Context androidContext;

  public static Origin origin;
  ChatOutHandler chatOut;

  public String thisUsername;

  /** Handles messages coming from ChatInRunnable thread. Texts come already formatted. */
  public static Handler handler;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {

        String text = (String) msg.obj;
        TextView messageReceived = (TextView) findViewById(R.id.receivedMsg);
        text = text + "\n";
        messageReceived.append(text);
      }
    };

    Log.d("register", "Activity has been set!");
    String thisUsername;
    String otherUsername;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    androidContext = this.getApplicationContext();

    TextView text = (TextView) findViewById(R.id.receivedMsg);
    text.setMovementMethod(new ScrollingMovementMethod());

    Button button = (Button) findViewById(R.id.sendToServerButton);
    button.setOnClickListener(this);

    Intent intent = getIntent();

    thisUsername = intent.getStringExtra(Contacts.EXTRA_THIS_USERNAME);
    otherUsername = intent.getStringExtra(Contacts.EXTRA_OTHER_USERNAME);

    SecretKeySpec sessionKey;
    ObjectOutputStream out;

    if (origin == Origin.CONTACTS) {

      CyclicBarrier barrier = new CyclicBarrier(2);

      chatActivityRunnable = new ChatActivityRunnable(barrier, thisUsername, otherUsername, this);

      Thread t = new Thread(chatActivityRunnable);
      t.start();

      try {
        barrier.await();
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (BrokenBarrierException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    chatOut = new ChatOutHandler();

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public void onClick(View v) {

    // Get message from GUI
    EditText messageEdit = (EditText) findViewById(R.id.message);
    String message = messageEdit.getText().toString();

    if(message.equals("")){
      return;
    }
    chatOut.sendMessage(message);
    
    messageEdit.setText("");

    Date dNow = new Date();
    SimpleDateFormat ft = new SimpleDateFormat("hh:mm");

    // Message is valid: format it to be pretty
    String formatted = String.format("[%s] %s: %s\n", ft.format(dNow), Globals.getThisUsername(), message);

    TextView messageReceived = (TextView) findViewById(R.id.receivedMsg);
    messageReceived.append(formatted);
  }
}




Java Source Code List

com.desperate.AdminConsole.java
com.desperate.ClientHandler.java
com.desperate.CryptoServer.java
com.desperate.UserDatabase.java
com.desperate.User.java
com.desperate.common.Message.java
com.desperate.common.NoncePacket.java
com.desperate.common.SessionKeyRequestInfo.java
com.desperate.common.TestCipherSerializable.java
com.desperate.common.Utilities.java
com.desperate.common.messages.ChatMessage.java
com.desperate.common.messages.IPMessage.java
com.desperate.common.messages.LoginMessage.java
com.desperate.common.messages.LogoutMessage.java
com.desperate.common.messages.RegisterMessage.java
com.desperate.common.messages.SessionKeyRequestMessage.java
com.desperate.common.messages.StartChatMessage.java
com.desperate.common.messages.UserListMessage.java
com.desperate.common.replies.CheckSessionMessage.java
com.desperate.common.replies.IPReplyMessage.java
com.desperate.common.replies.LoginReplyMessage.java
com.desperate.common.replies.LogoutReplyMessage.java
com.desperate.common.replies.NeedhamSchroederSuccessReply.java
com.desperate.common.replies.RegisterReplyMessage.java
com.desperate.common.replies.ReplyMessage.java
com.desperate.common.replies.SessionKeyReplyMessage.java
com.desperate.common.replies.StartChatReply.java
com.desperate.common.replies.UserListReplyMessage.java
com.desperate.debug.DebugClient.java
com.desperate.debug.DebugCryptoClient.java
com.desperate.debug.PlainServer.java
com.ist.cipherchat.gui.ChatActivity.java
com.ist.cipherchat.gui.ChooseServerActivity.java
com.ist.cipherchat.gui.Contacts.java
com.ist.cipherchat.gui.Origin.java
com.ist.cipherchat.networking.ChatActivityRunnable.java
com.ist.cipherchat.networking.ChatInRunnable.java
com.ist.cipherchat.networking.ChatOutHandler.java
com.ist.cipherchat.networking.Core.java
com.ist.cipherchat.networking.Globals.java
com.ist.cipherchat.networking.OutputSocketHandler.java
com.ist.cipherchat.networking.PhoneServerSocketHandler.java
com.ist.cipherchat.networking.ThreadComm.java