diff --git a/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java b/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java index affc8fd5c7200ae0cf01ef175d0744e5333e4b54..56bd513cf89244709616fc148b60172bb643b245 100644 --- a/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java +++ b/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java @@ -259,7 +259,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection public void onDisconnect() { try { if (ServerHolder.getSocket() != null) { - String str = "Disconnect##Client"+CLIENT_NAME; + String str = "Disconnect##Client##"+CLIENT_NAME; if (out == null) { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())), true); diff --git a/KvizServer/app/src/main/java/onlab/kvizserver/LobbyActivity.java b/KvizServer/app/src/main/java/onlab/kvizserver/LobbyActivity.java index bf3ec0885264c5dbb79d2299399754fdb815faa6..e5b0e9229e5b7878c8e2afbadd03dc95f68d29c7 100644 --- a/KvizServer/app/src/main/java/onlab/kvizserver/LobbyActivity.java +++ b/KvizServer/app/src/main/java/onlab/kvizserver/LobbyActivity.java @@ -46,6 +46,9 @@ public class LobbyActivity extends AppCompatActivity { private RecyclerView recyclerView; private ClientListAdapter adapter; + private RecyclerView observerView; + private ClientListAdapter observeradapter; + private String gameMode; private ArrayList<Thread> threads; @@ -143,6 +146,9 @@ public class LobbyActivity extends AppCompatActivity { for(ClientModel c : clients) if(c.getClientsocket() == client.getClientsocket()) req = false; + for(ClientModel o : observers) + if(o.getClientsocket() == client.getClientsocket()) + req = false; if(req || exit) SendConnectionLost(client); } @@ -316,7 +322,7 @@ public class LobbyActivity extends AppCompatActivity { if (params.length > 1) { if (params[1].equals("Observer")) { observers.add(new ClientModel(cname, clientSocket)); - // TODO: 2018. 04. 30. Add observer + updateConversationHandler.post(new updateUIThread(Operation.ADD_OBSERVER, "")); } if (params[1].equals("Client")) { clients.add(new ClientModel(cname, clientSocket)); @@ -347,6 +353,19 @@ public class LobbyActivity extends AppCompatActivity { break; } }; + for (ClientModel observer : observers) { + if (observer.getClientsocket() == clientSocket) { + int position = observers.indexOf(observer); + updateConversationHandler.post(new updateUIThread(Operation.REMOVE_OBSERVER, Integer.toString(position))); + observers.remove(observer); + clientSocket.close(); + allclients.remove(observer); + threads.remove(Thread.currentThread()); + Thread.currentThread().interrupt(); + + break; + } + }; break; case "Disconnect": if(params.length>1) @@ -357,7 +376,7 @@ public class LobbyActivity extends AppCompatActivity { if (observer.getClientsocket() == clientSocket) { int position = observers.indexOf(observer); observers.remove(observer); - // TODO: 2018. 04. 30. Remove Observer + updateConversationHandler.post(new updateUIThread(Operation.REMOVE_OBSERVER, Integer.toString(position))); break; } @@ -429,6 +448,12 @@ public class LobbyActivity extends AppCompatActivity { case REMOVE_CLIENT: adapter.removeItem(Integer.parseInt(msg)); break; + case ADD_OBSERVER: + observeradapter.addItem(); + break; + case REMOVE_OBSERVER: + observeradapter.removeItem((Integer.parseInt(msg))); + break; default: Log.d("updateUIThread", "Unknown operation"); break; @@ -442,6 +467,12 @@ public class LobbyActivity extends AppCompatActivity { recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); recyclerView.setAdapter(adapter); + + observerView = (RecyclerView) findViewById(R.id.ObserverRecyclerView); + observeradapter = new ClientListAdapter(observers); + observerView.setLayoutManager(new LinearLayoutManager(this)); + observerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); + observerView.setAdapter(observeradapter); } private void SendConnectionLost(ClientModel client) diff --git a/KvizServer/app/src/main/java/onlab/kvizserver/Operation.java b/KvizServer/app/src/main/java/onlab/kvizserver/Operation.java index 8165c2ad6f8c21b93920e6bafb819be5c9d3ef50..17b729ccfa520485bd47f4e161827793604ae6c5 100644 --- a/KvizServer/app/src/main/java/onlab/kvizserver/Operation.java +++ b/KvizServer/app/src/main/java/onlab/kvizserver/Operation.java @@ -1,5 +1,5 @@ package onlab.kvizserver; public enum Operation { - ADD_CLIENT, REMOVE_CLIENT; + ADD_CLIENT, REMOVE_CLIENT, ADD_OBSERVER, REMOVE_OBSERVER; } diff --git a/KvizServer/app/src/main/res/layout/activity_lobby.xml b/KvizServer/app/src/main/res/layout/activity_lobby.xml index 5682f19149e3b2e3fb46ff06ee950fc125740fbe..790ccef2b044c05f1aaa4e95ed34de95492c780b 100644 --- a/KvizServer/app/src/main/res/layout/activity_lobby.xml +++ b/KvizServer/app/src/main/res/layout/activity_lobby.xml @@ -9,7 +9,8 @@ <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" - android:orientation="vertical"> + android:orientation="vertical" + android:layout_marginLeft="5dp"> <LinearLayout android:layout_width="match_parent" @@ -41,15 +42,56 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - <android.support.v7.widget.RecyclerView - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" - android:id="@+id/MainRecyclerView" + <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" - app:layout_behavior="@string/appbar_scrolling_view_behavior" - /> + android:orientation="horizontal" + android:weightSum="2"> + <LinearLayout + android:layout_width="0dp" + android:layout_height="match_parent" + android:orientation="vertical" + android:layout_weight="1"> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Clients:" + android:textStyle="bold"/> + <android.support.v7.widget.RecyclerView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:id="@+id/MainRecyclerView" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior" + /> + + </LinearLayout> + <LinearLayout + android:layout_width="0dp" + android:layout_height="match_parent" + android:orientation="vertical" + android:layout_weight="1"> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Observers:" + android:textStyle="bold"/> + <android.support.v7.widget.RecyclerView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:id="@+id/ObserverRecyclerView" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior" + /> + </LinearLayout> + + + + + </LinearLayout> </android.support.design.widget.CoordinatorLayout> </LinearLayout>