Kaydet (Commit) 78d15f21 authored tarafından Andrzej J.R. Hunt's avatar Andrzej J.R. Hunt

Emulator detection. Fix crash on bluetoothless devices.

Change-Id: Ia4845940a83361cad1e824832dcad05019192705
üst 4cc09d0e
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ImpressRemote</name>
<name>SDRemote</name>
<comment></comment>
<projects>
</projects>
......
......@@ -28,6 +28,9 @@ public class BluetoothFinder {
}
public void startFinding() {
if (mAdapter == null) {
return; // No bluetooth adapter found (emulator, special devices)
}
System.out.println("BT:Discovery starting");
IntentFilter aFilter = new IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
......@@ -39,6 +42,9 @@ public class BluetoothFinder {
}
public void stopFinding() {
if (mAdapter == null) {
return; // No bluetooth adapter found (emulator, special devices)
}
mAdapter.cancelDiscovery();
try {
mContext.unregisterReceiver(mReceiver);
......
......@@ -10,6 +10,11 @@ public class Server {
private String mAddress;
private String mName;
private long mTimeDiscovered;
/**
* Signifies a Server that shouldn't be automatically removed from the list.
* Used e.g. for the emulator.
*/
protected boolean mNoTimeout = false;
protected Server(Protocol aProtocol, String aAddress, String aName,
long aTimeDiscovered) {
......
......@@ -9,6 +9,8 @@ import java.net.SocketException;
import java.util.Collection;
import java.util.HashMap;
import org.libreoffice.impressremote.communication.Server.Protocol;
import android.content.Context;
import android.content.Intent;
......@@ -69,15 +71,11 @@ public class ServerFinder {
mServerList.put(aServer.getAddress(), aServer);
System.out.println("Contains:<<" + aName + ">>");
Intent aIntent = new Intent(
CommunicationService.MSG_SERVERLIST_CHANGED);
mContext.sendBroadcast(aIntent);
notifyActivity();
} catch (java.net.SocketTimeoutException e) {
// Ignore -- we want to timeout to enable checking whether we
// should stop listening periodically
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
......@@ -92,6 +90,7 @@ public class ServerFinder {
mListenerThread = new Thread() {
@Override
public void run() {
checkAndAddEmulator();
long aTime = 0;
try {
mSocket = new DatagramSocket();
......@@ -106,13 +105,13 @@ public class ServerFinder {
PORT);
mSocket.send(aPacket);
aTime = System.currentTimeMillis();
// Remove stale servers
for (Server aServer : mServerList.values()) {
if (System.currentTimeMillis()
- aServer.getTimeDiscovered() > 60 * 1000) {
if (!aServer.mNoTimeout
&& System.currentTimeMillis()
- aServer.getTimeDiscovered() > 60 * 1000) {
mServerList.remove(aServer.getAddress());
Intent aIntent = new Intent(
CommunicationService.MSG_SERVERLIST_CHANGED);
mContext.sendBroadcast(aIntent);
notifyActivity();
}
}
......@@ -145,6 +144,34 @@ public class ServerFinder {
}
}
/**
* Check whether we are on an emulator and add it's host to the list of
* servers if so (although we do not know whether libo is running on
* the host).
*/
private void checkAndAddEmulator() {
try {
if (InetAddress.getByName("10.0.2.2").isReachable(100)) {
System.out.println("NulledNot");
Server aServer = new Server(Protocol.NETWORK, "10.0.2.2",
"Android Emulator Host", 0);
aServer.mNoTimeout = true;
mServerList.put(aServer.getAddress(), aServer);
notifyActivity();
}
} catch (IOException e) {
// Probably means we can't connect -- i.e. no emulator host
}
}
/**
* Notify the activity that the server list has changed.
*/
private void notifyActivity() {
Intent aIntent = new Intent(CommunicationService.MSG_SERVERLIST_CHANGED);
mContext.sendBroadcast(aIntent);
}
public Collection<Server> getServerList() {
return mServerList.values();
}
......
Markdown is supported
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