Commit b8d0fb61 authored by KGreg's avatar KGreg
Browse files

Pause/Resume

parent 5fe59cec
......@@ -52,7 +52,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private ArrayList<Thread> threads;
private RecyclerView recyclerView;
private ServerListAdapter adapter;
private ServerListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -85,38 +85,14 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
@Override
protected void onPause() {
if (mNsdManager != null && discovering) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
for (ServerModel server : servers) {
if(ServerHolder.getSocket() != server.getSocket() || exit)
SendConnectionLost(server);
if(exit)
ServerHolder.setServerModel(null);
}
for (Thread thread : threads) {
thread.interrupt();
}
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
initRecyclerView();
if (mNsdManager != null && !discovering) {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
discovering = true;
}
super.onResume();
}
......@@ -126,6 +102,17 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
for (ServerModel server : servers) {
if(exit || ServerHolder.getSocket() != server.getSocket())
SendConnectionLost(server);
if(exit)
ServerHolder.setServerModel(null);
}
for (Thread thread : threads) {
thread.interrupt();
}
}
super.onDestroy();
......@@ -403,7 +390,8 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private void initRecyclerView() {
recyclerView = (RecyclerView) findViewById(R.id.MainRecyclerView);
adapter = new ServerListAdapter(this);
if(adapter == null)
adapter = new ServerListAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
......
......@@ -5,7 +5,7 @@ import java.net.Socket;
import onlab.kvizclient.model.ServerModel;
public class ServerHolder {
private static ServerModel serverModel;
private static ServerModel serverModel = null;
public static Socket getSocket() {
return serverModel.getSocket();
......
......@@ -115,16 +115,22 @@ public class LobbyActivity extends AppCompatActivity {
@Override
protected void onPause() {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
initRecyclerView();
}
@Override
protected void onDestroy() {
if (mNsdManager != null && registred) {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
for (ClientModel client : allclients) {
boolean req = true;
for(ClientModel c : clients)
......@@ -139,28 +145,6 @@ public class LobbyActivity extends AppCompatActivity {
for (Thread thread : threads) {
thread.interrupt();
}
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
initRecyclerView();
if (mNsdManager != null && !registred) {
registerService(9000);
registred = true;
}
}
@Override
protected void onDestroy() {
if (mNsdManager != null && registred) {
mNsdManager.unregisterService(mRegistrationListener);
registred = false;
}
try {
serverSocket.close();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment