Commit 0f1c9ced authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- add basic app logic

parent cddc180e
package com.nclab.ncmultipeerchat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.nclab.ncmultipeerconnectivity.NCMCPeerID;
import com.nclab.ncmultipeerconnectivity.NCMCSession;
import java.util.List;
public class ChatActivity extends Activity {
private TextView txtChatHist;
......@@ -16,6 +30,36 @@ public class ChatActivity extends Activity {
private Button btnSendTo2;
private Button btnSendTo3;
private final IntentFilter m_intentFilter = new IntentFilter();
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE: {
Bundle bundle = intent.getExtras();
if (bundle.containsKey(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME) && bundle.containsKey(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_DATA)) {
String fromName = bundle.getString(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME);
byte[] data = bundle.getByteArray(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE_DATA);
if (data != null) {
String message = new String(data);
String chat = fromName + " : " + message;
String hist = txtChatHist.getText().toString();
String newHist = hist + "\n" + chat;
txtChatHist.setText(newHist);
}
}
break;
}
case MultiplayerController.BLE_BROADCAST_UPDATE_PLAYERLIST: {
setPlayerList();
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -27,13 +71,123 @@ public class ChatActivity extends Activity {
txtPlayer2 = (TextView)this.findViewById(R.id.txtSendToPlayer2);
txtPlayer3 = (TextView)this.findViewById(R.id.txtSendToPlayer3);
txtLocalPlayer = (TextView)this.findViewById(R.id.txtPlayerLocal);
btnSendTo1 = (Button)this.findViewById(R.id.btnSendTo1);
btnSendTo1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = getChatMsg();
if (msg.length() > 0) {
MultiplayerController.getInstance().sendDataToPeer(msg.getBytes(), txtPlayer1.getText().toString(), NCMCSession.NCMCSessionSendDataUnreliable);
}
}
});
btnSendTo2 = (Button)this.findViewById(R.id.btnSendTo2);
btnSendTo2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = getChatMsg();
if (msg.length() > 0) {
MultiplayerController.getInstance().sendDataToPeer(msg.getBytes(), txtPlayer2.getText().toString(), NCMCSession.NCMCSessionSendDataUnreliable);
}
}
});
btnSendTo3 = (Button)this.findViewById(R.id.btnSendTo3);
btnSendTo3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = getChatMsg();
if (msg.length() > 0) {
MultiplayerController.getInstance().sendDataToPeer(msg.getBytes(), txtPlayer3.getText().toString(), NCMCSession.NCMCSessionSendDataUnreliable);
}
}
});
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_RECEIVE_MESSAGE);
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_UPDATE_PLAYERLIST);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
backToSettingActivity();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onResume() {
super.onResume();
MultiplayerController.getInstance().setContext(this);
registerReceiver(m_broadcastReceiver, m_intentFilter);
setPlayerList();
}
@Override
protected void onPause() {
super.onPause();
MultiplayerController.getInstance().setContext(null);
unregisterReceiver(m_broadcastReceiver);
}
private void backToSettingActivity() {
MultiplayerController.getInstance().disconnect();
Intent intent = new Intent();
intent.setClass(ChatActivity.this, SettingActivity.class);
ChatActivity.this.startActivity(intent);
ChatActivity.this.finish();
}
public String getChatMsg() {
TextView user = (TextView)this.findViewById(R.id.txtChatMsg);
return user.getText().toString();
}
private void resetUI() {
txtPlayer1.setTextColor(Color.GRAY);
txtPlayer1.setText(getResources().getString(R.string.empty));
btnSendTo1.setEnabled(false);
txtPlayer2.setTextColor(Color.GRAY);
txtPlayer2.setText(getResources().getString(R.string.empty));
btnSendTo2.setEnabled(false);
txtPlayer3.setTextColor(Color.GRAY);
txtPlayer3.setText(getResources().getString(R.string.empty));
btnSendTo3.setEnabled(false);
txtLocalPlayer.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(MultiplayerController.getInstance().getLocalName()));
txtLocalPlayer.setTextColor(Color.WHITE);
}
private void setPlayerList(){
resetUI();
List<NCMCPeerID> playerData = MultiplayerController.getInstance().getCurrentSessionPlayerIDs();
int i = 0;
for (NCMCPeerID pid : playerData) {
if (!pid.getDisplayName().equalsIgnoreCase(MultiplayerController.getInstance().getLocalName())) {
if (i == 0) {
txtPlayer1.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer1.setTextColor(Color.WHITE);
btnSendTo1.setEnabled(true);
}
if (i == 1) {
txtPlayer2.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer2.setTextColor(Color.WHITE);
btnSendTo2.setEnabled(true);
}
if (i == 2) {
txtPlayer3.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer3.setTextColor(Color.WHITE);
btnSendTo3.setEnabled(true);
}
++i;
}
}
}
}
package com.nclab.ncmultipeerchat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.nclab.ncmultipeerconnectivity.NCMCPeerID;
import java.util.List;
public class LobbyActivity extends Activity {
private TextView lbMsg;
......@@ -16,13 +27,107 @@ public class LobbyActivity extends Activity {
private TextView txtPlayer3;
private TextView txtPlayer4;
private ImageButton btnStartGame;
private ImageButton btnBack;
private Handler mTickHandler;
private final IntentFilter m_intentFilter = new IntentFilter();
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case MultiplayerController.BLE_BROADCAST_START_FAILED: {
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(getResources().getString(R.string.startfailed)).setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
backToSettingActivity();
}
}).show();
break;
}
case MultiplayerController.BLE_BROADCAST_FOUND_PEER: {
Bundle bundle = intent.getExtras();
if (bundle.containsKey(MultiplayerController.BLE_BROADCAST_PEERID)) {
final NCMCPeerID peerID = (NCMCPeerID)bundle.getSerializable(MultiplayerController.BLE_BROADCAST_PEERID);
if (peerID != null) {
String name = MultiplayerController.getInstance().stringForMCPeerDisplayName(peerID.getDisplayName());
String msg = " \"" + name + "\" " + getResources().getString(R.string.periphfound);
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(msg).setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MultiplayerController.getInstance().invitePeer(peerID);
}
}).setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).show();
}
}
break;
}
case MultiplayerController.BLE_BROADCAST_INVITATION: {
Bundle bundle = intent.getExtras();
if (bundle.containsKey(MultiplayerController.BLE_BROADCAST_PEERID)) {
final NCMCPeerID peerID = (NCMCPeerID)bundle.getSerializable(MultiplayerController.BLE_BROADCAST_PEERID);
if (peerID != null) {
String name = MultiplayerController.getInstance().stringForMCPeerDisplayName(peerID.getDisplayName());
String msg = getResources().getString(R.string.centralfoundp1) + " \"" + name + "\" " + getResources().getString(R.string.centralfoundp2);
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(msg).setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MultiplayerController.getInstance().sendResponseToInvitation(true, peerID);
}
}).setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MultiplayerController.getInstance().sendResponseToInvitation(false, peerID);
}
}).show();
}
}
break;
}
case MultiplayerController.BLE_BROADCAST_SCAN_TIMEOUT: {
String msg;
if (MultiplayerController.getInstance().isHost())
{
msg = getResources().getString(R.string.scanfinish);
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(msg).setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (MultiplayerController.getInstance().isHost()) {
MultiplayerController.getInstance().startHost();
}
}
}).setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).show();
}
break;
}
case MultiplayerController.BLE_BROADCAST_GO_TO_CHATROOM: {
goToChat();
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lobby);
ImageButton btnBack = (ImageButton)this.findViewById(R.id.btnBackToSC);
btnBack = (ImageButton)this.findViewById(R.id.btnBackToSC);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
......@@ -30,12 +135,41 @@ public class LobbyActivity extends Activity {
}
});
mTickHandler = new Handler();
btnStartGame = (ImageButton)this.findViewById(R.id.btnStartGame);
if (MultiplayerController.getInstance().isHost()) {
btnStartGame.setEnabled(false);
btnStartGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MultiplayerController.getInstance().gotoChatRoom();
goToChat();
}
});
} else {
btnStartGame.setEnabled(false);
btnStartGame.setVisibility(View.INVISIBLE);
}
lbMsg = (TextView)this.findViewById(R.id.lbMsg);
txtPlayer1 = (TextView)this.findViewById(R.id.txtPlayer1);
txtPlayer2 = (TextView)this.findViewById(R.id.txtPlayer2);
txtPlayer3 = (TextView)this.findViewById(R.id.txtPlayer3);
txtPlayer4 = (TextView)this.findViewById(R.id.txtPlayer4);
if (MultiplayerController.getInstance().isHost()) {
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_FOUND_PEER);
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_SCAN_TIMEOUT);
} else {
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_INVITATION);
}
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_GO_TO_CHATROOM);
m_intentFilter.addAction(MultiplayerController.BLE_BROADCAST_START_FAILED);
}
@Override
......@@ -48,10 +182,125 @@ public class LobbyActivity extends Activity {
}
}
@Override
protected void onResume() {
super.onResume();
MultiplayerController.getInstance().setContext(this);
registerReceiver(m_broadcastReceiver, m_intentFilter);
if (MultiplayerController.getInstance().isHost()) {
MultiplayerController.getInstance().startHost();
} else {
MultiplayerController.getInstance().startClient();
}
mTickHandler.postDelayed(mTickRunnable, 100);
}
@Override
protected void onPause() {
super.onPause();
MultiplayerController.getInstance().setContext(null);
unregisterReceiver(m_broadcastReceiver);
if (MultiplayerController.getInstance().isHost()) {
MultiplayerController.getInstance().stopHost();
} else {
MultiplayerController.getInstance().stopClient();
}
mTickHandler.removeCallbacks(mTickRunnable);
}
private void backToSettingActivity() {
MultiplayerController.getInstance().disconnect();
Intent intent = new Intent();
intent.setClass(LobbyActivity.this, SettingActivity.class);
LobbyActivity.this.startActivity(intent);
LobbyActivity.this.finish();
}
private void goToChat() {
Intent intent = new Intent();
intent.setClass(LobbyActivity.this, ChatActivity.class);
LobbyActivity.this.startActivity(intent);
LobbyActivity.this.finish();
}
private Runnable mTickRunnable = new Runnable() {
@Override
public void run() {
// update UI
runOnUiThread(new Runnable() {
@Override
public void run() {
resetUI();
List<NCMCPeerID> playerData = MultiplayerController.getInstance().getCurrentSessionPlayerIDs();
for (int i=0; i<playerData.size(); ++i) {
NCMCPeerID pid = playerData.get(i);
if (i == 0) {
txtPlayer1.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer1.setTextColor(Color.WHITE);
}
if (i == 1) {
txtPlayer2.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer2.setTextColor(Color.WHITE);
}
if (i == 2) {
txtPlayer3.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer3.setTextColor(Color.WHITE);
}
if (i == 3) {
txtPlayer4.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(pid.getDisplayName()));
txtPlayer4.setTextColor(Color.WHITE);
}
}
if (playerData.size() >= 2) {
lbMsg.setText(getResources().getString(R.string.readytogame));
if (MultiplayerController.getInstance().isHost()) {
btnStartGame.setEnabled(true);
}
} else {
if (MultiplayerController.getInstance().isHost()) {
btnStartGame.setEnabled(false);
}
}
}
});
}
};
private void resetUI() {
if (MultiplayerController.getInstance().isHost()) {
lbMsg.setText(getResources().getString(R.string.waitingplayer));
txtPlayer1.setTextColor(Color.WHITE);
txtPlayer1.setText(MultiplayerController.getInstance().stringForMCPeerDisplayName(MultiplayerController.getInstance().getLocalName()));
txtPlayer2.setTextColor(Color.GRAY);
txtPlayer2.setText(getResources().getString(R.string.empty));
txtPlayer3.setTextColor(Color.GRAY);
txtPlayer3.setText(getResources().getString(R.string.empty));
txtPlayer4.setTextColor(Color.GRAY);
txtPlayer4.setText(getResources().getString(R.string.empty));
btnStartGame.setEnabled(false);
btnBack.setEnabled(true);
} else {
lbMsg.setText(getResources().getString(R.string.searchinggame));
txtPlayer1.setTextColor(Color.GRAY);
txtPlayer1.setText(getResources().getString(R.string.empty));
txtPlayer2.setTextColor(Color.GRAY);
txtPlayer2.setText(getResources().getString(R.string.empty));
txtPlayer3.setTextColor(Color.GRAY);
txtPlayer3.setText(getResources().getString(R.string.empty));
txtPlayer4.setTextColor(Color.GRAY);
txtPlayer4.setText(getResources().getString(R.string.empty));
btnStartGame.setEnabled(false);
btnStartGame.setVisibility(View.INVISIBLE);
btnBack.setEnabled(true);
}
}
}
......@@ -24,6 +24,7 @@ public class LoginActivity extends Activity {
public void onClick(View v) {
String user = getUserName();
if (!user.isEmpty() && user.length() >= 3 && user.length() <= 8) {
MultiplayerController.getInstance().setLocalName(user);
Intent intent = new Intent();
intent.setClass(LoginActivity.this, SettingActivity.class);
......@@ -42,6 +43,18 @@ public class LoginActivity extends Activity {
return user.getText().toString();
}
@Override
protected void onResume() {
super.onResume();
MultiplayerController.getInstance().setContext(this);
}
@Override
protected void onPause() {
super.onPause();
MultiplayerController.getInstance().setContext(null);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
......
package com.nclab.ncmultipeerchat;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.nclab.ncmultipeerconnectivity.NCMCCentralService;
import com.nclab.ncmultipeerconnectivity.NCMCCentralServiceCallback;
import com.nclab.ncmultipeerconnectivity.NCMCPeerID;
import com.nclab.ncmultipeerconnectivity.NCMCPeripheralService;
import com.nclab.ncmultipeerconnectivity.NCMCPeripheralServiceCallback;
import com.nclab.ncmultipeerconnectivity.NCMCSession;
import com.nclab.ncmultipeerconnectivity.NCMCSessionCallback;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
/**
* control the whole chat
*/
public class MultiplayerController {
private static final String TAG = "MultiplayerController";
public static final String TRANSFER_SERVICE_UUID = "ABE00C6E-58F1-44B2-BE41-20E66874B97D";
private static final char MSG_SERVER_CLIENT_GO_TO_CHAT = 0;
private static final char MSG_CHAT_MSG = 1;
public final static String BLE_BROADCAST_FOUND_PEER = "ncchat.nclab.com.BLE_BROADCAST_FOUND_PEER";
public final static String BLE_BROADCAST_INVITATION = "ncchat.nclab.com.BLE_BROADCAST_INVITATION";
public final static String BLE_BROADCAST_PEERID = "ncchat.nclab.com.BLE_BROADCAST_PEERID";
public final static String BLE_BROADCAST_GO_TO_CHATROOM = "ncchat.nclab.com.BLE_BROADCAST_GO_TO_CHATROOM";
public final static String BLE_BROADCAST_SCAN_TIMEOUT = "ncchat.nclab.com.BLE_BROADCAST_SCAN_TIMEOUT";
public final static String BLE_BROADCAST_START_FAILED = "ncchat.nclab.com.BLE_BROADCAST_START_FAILED";
public final static String BLE_BROADCAST_UPDATE_PLAYERLIST = "ncchat.nclab.com.BLE_BROADCAST_UPDATE_PLAYERLIST_";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_FROM_NAME";
public final static String BLE_BROADCAST_RECEIVE_MESSAGE_DATA = "ncchat.nclab.com.BLE_BROADCAST_RECEIVE_MESSAGE_DATA";
private NCMCSession currentSession;
private NCMCCentralService currentCentralService;
private NCMCPeripheralService currentPeripheralService;
private List<NCMCPeerID> currentSessionPlayerIDs;
private boolean isHost;
private String localName;
private Context context;
private NCMCSessionCallback mSessionCallback;
private NCMCCentralServiceCallback mCentralServiceCallback;
private NCMCPeripheralServiceCallback mPeripheralServiceCallback;
private static MultiplayerController ourInstance = new MultiplayerController();
......@@ -13,4 +60,353 @@ public class MultiplayerController {
private MultiplayerController() {
}