diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-21 09:30:45 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-21 09:31:47 +0200 |
commit | 78d15f219ea5570b1b08c1122ac0d9c0e1736b46 (patch) | |
tree | 62192dd208c7236155c1c9a08a9d9a1bf9c1fe38 /android/sdremote | |
parent | 4cc09d0e0d8e0eaed36ffc9ae3baaa3fcfac30cb (diff) |
Emulator detection. Fix crash on bluetoothless devices.
Change-Id: Ia4845940a83361cad1e824832dcad05019192705
Diffstat (limited to 'android/sdremote')
4 files changed, 49 insertions, 11 deletions
diff --git a/android/sdremote/.project b/android/sdremote/.project index 70ef552c8876..1a4d0d228fc4 100644 --- a/android/sdremote/.project +++ b/android/sdremote/.project @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <projectDescription> - <name>ImpressRemote</name> + <name>SDRemote</name> <comment></comment> <projects> </projects> diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java index d93ea5ce0b1c..a6866186b8da 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java @@ -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); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index dfa75fe7577a..08f973f87056 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -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) { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java index 4734ff457e32..79839e779042 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java @@ -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(); } |