Added deletion of stale detected servers, UI fixed.

Change-Id: I I97a833b45e0c95a217004ae4a36e72a314d68d9f
This commit is contained in:
Andrzej J.R. Hunt 2012-08-03 14:59:51 +02:00 committed by Michael Meeks
parent ad72b47df4
commit c56b0a2fe8
4 changed files with 48 additions and 22 deletions

View File

@ -9,19 +9,19 @@
android:layout_margin="10dip"
android:orientation="vertical" >
<TextView
android:id="@+id/selector_label_none"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selector_noservers"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/selector_container_bluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/selector_label_none"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selector_noservers"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/selector_label_bluetooth"
android:layout_width="wrap_content"

View File

@ -70,6 +70,9 @@ public class SelectorActivity extends Activity {
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mCommunicationService != null) {
mCommunicationService.stopFindingServers();
}
doUnbindService();
}
@ -89,6 +92,7 @@ public class SelectorActivity extends Activity {
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
mCommunicationService.startFindingServers();
}
@Override
@ -160,9 +164,8 @@ public class SelectorActivity extends Activity {
.setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE
: View.GONE);
mNoServerLabel.setVisibility((mBluetoothServers.size() == 0)
&& (mNetworkServers.size() == 0) ? View.VISIBLE
: View.GONE);
mNoServerLabel.setVisibility(((mBluetoothServers.size() == 0) && (mNetworkServers
.size() == 0)) ? View.VISIBLE : View.GONE);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -59,7 +59,6 @@ public class CommunicationService extends Service {
@Override
public void onCreate() {
// TODO Create a notification (if configured).
mFinder.startFinding();
}
@Override
@ -75,6 +74,14 @@ public class CommunicationService extends Service {
return mFinder.getServerList();
}
public void startFindingServers() {
mFinder.startFinding();
}
public void stopFindingServers() {
mFinder.stopFinding();
}
/**
* Connect to a specific server. This method cannot be called on the main
* activity thread.

View File

@ -21,6 +21,8 @@ public class ServerFinder {
private static final int PORT = 1598;
private static final String CHARSET = "UTF-8";
private static final long SEARCH_INTERVAL = 1000 * 20;
private DatagramSocket mSocket = null;
private Thread mListenerThread = null;
@ -40,9 +42,7 @@ public class ServerFinder {
try {
String aCommand = null;
String aName = null;
System.out.println("SF:listening for packet\n");
mSocket.receive(aPacket);
System.out.println("SF:received packet\n");
int i;
for (i = 0; i < aBuffer.length; i++) {
if (aPacket.getData()[i] == '\n') {
@ -95,18 +95,34 @@ public class ServerFinder {
mListenerThread = new Thread() {
@Override
public void run() {
long aTime = 0;
try {
mSocket = new DatagramSocket();
String aString = "LOREMOTE_SEARCH\n";
DatagramPacket aPacket = new DatagramPacket(
aString.getBytes(CHARSET),
aString.length(),
InetAddress.getByName("239.0.0.1"),
PORT);
mSocket.send(aPacket);
System.out.println("SF:sent packet\n");
mSocket.setSoTimeout(1000 * 10);
while (!mFinishRequested) {
if (System.currentTimeMillis() - aTime > SEARCH_INTERVAL) {
String aString = "LOREMOTE_SEARCH\n";
DatagramPacket aPacket = new DatagramPacket(
aString.getBytes(CHARSET),
aString.length(),
InetAddress.getByName("239.0.0.1"),
PORT);
mSocket.send(aPacket);
aTime = System.currentTimeMillis();
for (Server aServer : mServerList.values()) {
if (System.currentTimeMillis()
- aServer.getTimeDiscovered() > 60 * 1000) {
mServerList.remove(aServer.getAddress());
Intent aIntent = new Intent(
CommunicationService.MSG_SERVERLIST_CHANGED);
LocalBroadcastManager.getInstance(
mContext)
.sendBroadcast(aIntent);
}
}
}
listenForServer();
}
} catch (SocketException e) {
@ -135,6 +151,6 @@ public class ServerFinder {
}
public Server[] getServerList() {
return mServerList.entrySet().toArray(new Server[mServerList.size()]);
return mServerList.values().toArray(new Server[mServerList.size()]);
}
}