[Libreoffice-commits] .: android/sdremote

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Tue Aug 21 00:32:13 PDT 2012


 android/sdremote/.project                                                             |    2 
 android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java |    6 +
 android/sdremote/src/org/libreoffice/impressremote/communication/Server.java          |    5 +
 android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java    |   47 +++++++---
 4 files changed, 49 insertions(+), 11 deletions(-)

New commits:
commit 78d15f219ea5570b1b08c1122ac0d9c0e1736b46
Author: Andrzej J.R. Hunt <andrzej at ahunt.org>
Date:   Tue Aug 21 09:30:45 2012 +0200

    Emulator detection. Fix crash on bluetoothless devices.
    
    Change-Id: Ia4845940a83361cad1e824832dcad05019192705

diff --git a/android/sdremote/.project b/android/sdremote/.project
index 70ef552..1a4d0d2 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 d93ea5c..a686618 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 dfa75fe..08f973f 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 4734ff4..79839e7 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();
     }


More information about the Libreoffice-commits mailing list