[Libreoffice-commits] core.git: 2 commits - android/sdremote

Artur Dryomov artur.dryomov at gmail.com
Thu Aug 1 09:47:32 PDT 2013


 android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java |   82 +++++-----
 android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java   |   25 +--
 android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java       |    4 
 android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java               |    6 
 4 files changed, 68 insertions(+), 49 deletions(-)

New commits:
commit 7e07a45500dcbb891a85f0bc9b7049cf4d50bba1
Author: Artur Dryomov <artur.dryomov at gmail.com>
Date:   Thu Aug 1 19:44:47 2013 +0300

    Change search timeouts from milliseconds to seconds.
    
    Makes things readable and should prevent potential typing bugs.
    
    Change-Id: Ifcb90568859e94896f5c2a313546fe1fdd6fa4bc

diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
index 4f1029b..2152cef 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
@@ -12,6 +12,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
@@ -26,7 +27,7 @@ import org.libreoffice.impressremote.util.BluetoothOperator;
 import org.libreoffice.impressremote.util.Intents;
 
 class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, Runnable {
-    private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 10;
+    private static final int SEARCH_DELAY_IN_SECONDS = 10;
 
     private final Context mContext;
 
@@ -102,7 +103,7 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder,
         }
 
         Handler aHandler = new Handler();
-        aHandler.postDelayed(this, SEARCH_DELAY_IN_MILLISECONDS);
+        aHandler.postDelayed(this, TimeUnit.SECONDS.toMillis(SEARCH_DELAY_IN_SECONDS));
     }
 
     @Override
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java
index 9f50fc1..f3280eb 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java
@@ -33,7 +33,7 @@ import org.libreoffice.impressremote.util.Intents;
 
 class TcpServersFinder implements ServersFinder, Runnable {
     private static final int SEARCH_DELAY_IN_SECONDS = 10;
-    private static final int BLOCKING_TIMEOUT_IN_MILLISECONDS = 1000 * 10;
+    private static final int BLOCKING_TIMEOUT_IN_SECONDS = 10;
 
     private static final int SEARCH_RESULT_BUFFER_SIZE = 1024;
 
@@ -74,7 +74,7 @@ class TcpServersFinder implements ServersFinder, Runnable {
     private void setUpSearchSocket() {
         try {
             mSearchSocket = new DatagramSocket();
-            mSearchSocket.setSoTimeout(BLOCKING_TIMEOUT_IN_MILLISECONDS);
+            mSearchSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(BLOCKING_TIMEOUT_IN_SECONDS));
         } catch (SocketException e) {
             throw new RuntimeException("Unable to open search socket.");
         }
commit 9864a2d5d4dbc9988ada970dfe713559e7db9326
Author: Artur Dryomov <artur.dryomov at gmail.com>
Date:   Thu Aug 1 19:32:19 2013 +0300

    Fix Bluetooth devices discovery.
    
    * Listen async Bluetooth status messages and start discovery properly.
    * Remember Bluetooth status before running the app and manage the state.
    
    Change-Id: I2be3fb6a503c5f6ace96732ebd0302935f1afb84

diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
index 35be694..4f1029b 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
@@ -24,10 +24,9 @@ import android.support.v4.content.LocalBroadcastManager;
 
 import org.libreoffice.impressremote.util.BluetoothOperator;
 import org.libreoffice.impressremote.util.Intents;
-import org.libreoffice.impressremote.communication.Server.Protocol;
 
 class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, Runnable {
-    private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 5;
+    private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 10;
 
     private final Context mContext;
 
@@ -49,55 +48,53 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder,
             return;
         }
 
-        setUpSearchResultsReceiver();
+        setUpBluetoothActionsReceiver();
+
+        if (!BluetoothOperator.getAdapter().isEnabled()) {
+            return;
+        }
 
         BluetoothOperator.getAdapter().startDiscovery();
     }
 
-    private void setUpSearchResultsReceiver() {
-        IntentFilter aSearchResultsFilter = new IntentFilter();
-        aSearchResultsFilter.addAction(BluetoothDevice.ACTION_FOUND);
-        aSearchResultsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
+    private void setUpBluetoothActionsReceiver() {
+        IntentFilter aBluetoothActionsFilter = new IntentFilter();
+        aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
+        aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
+        aBluetoothActionsFilter.addAction(BluetoothDevice.ACTION_FOUND);
 
-        mContext.registerReceiver(this, aSearchResultsFilter);
+        mContext.registerReceiver(this, aBluetoothActionsFilter);
     }
 
     @Override
     public void onReceive(Context aContext, Intent aIntent) {
-        if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
-            BluetoothDevice aBluetoothDevice = aIntent
-                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
-
-            addServer(buildServer(aBluetoothDevice));
-            callUpdatingServersList();
-
-            return;
+        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) {
+            switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
+                case BluetoothAdapter.STATE_ON:
+                    BluetoothOperator.getAdapter().startDiscovery();
+                    return;
+
+                default:
+                    return;
+            }
         }
 
         if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) {
             startDiscoveryDelayed();
+            return;
         }
-    }
-
-    private void addServer(Server aServer) {
-        mServers.put(aServer.getAddress(), aServer);
-    }
 
-    private Server buildServer(BluetoothDevice aBluetoothDevice) {
-        String aServerAddress = aBluetoothDevice.getAddress();
-        String aServerName = aBluetoothDevice.getName();
-
-        return new Server(Protocol.BLUETOOTH, aServerAddress, aServerName);
-    }
+        if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
+            BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 
-    private void callUpdatingServersList() {
-        Intent aIntent = Intents.buildServersListChangedIntent();
-        LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
+            addServer(buildServer(aBluetoothDevice));
+            callUpdatingServersList();
+        }
     }
 
     private void startDiscoveryDelayed() {
         // Start discovery again after a small delay.
-        // but check whether device is on in case the user manually
+        // Check whether device is on in case the user manually
         // disabled bluetooth
 
         if (!BluetoothOperator.getAdapter().isEnabled()) {
@@ -113,18 +110,34 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder,
         BluetoothOperator.getAdapter().startDiscovery();
     }
 
+    private void addServer(Server aServer) {
+        mServers.put(aServer.getAddress(), aServer);
+    }
+
+    private Server buildServer(BluetoothDevice aBluetoothDevice) {
+        String aServerAddress = aBluetoothDevice.getAddress();
+        String aServerName = aBluetoothDevice.getName();
+
+        return Server.newBluetoothInstance(aServerAddress, aServerName);
+    }
+
+    private void callUpdatingServersList() {
+        Intent aIntent = Intents.buildServersListChangedIntent();
+        LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
+    }
+
     @Override
     public void stopSearch() {
         if (!BluetoothOperator.isAvailable()) {
             return;
         }
 
-        tearDownSearchResultsReceiver();
+        tearDownBluetoothActionsReceiver();
 
         BluetoothOperator.getAdapter().cancelDiscovery();
     }
 
-    private void tearDownSearchResultsReceiver() {
+    private void tearDownBluetoothActionsReceiver() {
         try {
             mContext.unregisterReceiver(this);
         } catch (IllegalArgumentException e) {
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
index 8ded38f..51ca64c 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
@@ -11,12 +11,12 @@ package org.libreoffice.impressremote.communication;
 import java.util.List;
 
 import android.app.Service;
-import android.bluetooth.BluetoothAdapter;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
 import android.support.v4.content.LocalBroadcastManager;
 
+import org.libreoffice.impressremote.util.BluetoothOperator;
 import org.libreoffice.impressremote.util.Intents;
 
 public class CommunicationService extends Service implements Runnable, MessagesListener, Timer.TimerListener {
@@ -37,14 +37,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL
     private IBinder mBinder;
 
     private ServersManager mServersManager;
-    private static boolean gWasBluetoothEnabled = false;
-    private static boolean gWasBluetoothEnabledFetched = false;
 
     private ServerConnection mServerConnection;
 
     private MessagesReceiver mMessagesReceiver;
     private CommandsTransmitter mCommandsTransmitter;
 
+    private boolean mBluetoothWasEnabled;
+
     private SlideShow mSlideShow;
 
     private Thread mThread;
@@ -60,6 +60,8 @@ public class CommunicationService extends Service implements Runnable, MessagesL
 
         mServersManager = new ServersManager(this);
 
+        mBluetoothWasEnabled = false;
+
         mSlideShow = new SlideShow(new Timer(this));
 
         mThread = new Thread(this);
@@ -163,13 +165,11 @@ public class CommunicationService extends Service implements Runnable, MessagesL
     public void startSearch() {
         mState = State.SEARCHING;
 
-        if (BluetoothAdapter.getDefaultAdapter() != null) {
-            if (!gWasBluetoothEnabledFetched) {
-                gWasBluetoothEnabled = BluetoothAdapter.getDefaultAdapter().isEnabled();
-                gWasBluetoothEnabledFetched = true;
+        if (BluetoothOperator.isAvailable()) {
+            mBluetoothWasEnabled = BluetoothOperator.getAdapter().isEnabled();
 
-                if (!gWasBluetoothEnabled)
-                    BluetoothAdapter.getDefaultAdapter().enable();
+            if (!BluetoothOperator.getAdapter().isEnabled()) {
+                BluetoothOperator.getAdapter().enable();
             }
         }
 
@@ -179,9 +179,10 @@ public class CommunicationService extends Service implements Runnable, MessagesL
     public void stopSearch() {
         mServersManager.stopServersSearch();
 
-        if (BluetoothAdapter.getDefaultAdapter() != null) {
-            if (!gWasBluetoothEnabled)
-                BluetoothAdapter.getDefaultAdapter().disable();
+        if (BluetoothOperator.isAvailable()) {
+            if (!mBluetoothWasEnabled) {
+                BluetoothOperator.getAdapter().disable();
+            }
         }
     }
 
diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java
index 5a1167e..a531d86 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java
@@ -15,10 +15,14 @@ public final class BluetoothOperator {
     }
 
     public static boolean isAvailable() {
-        return BluetoothAdapter.getDefaultAdapter() != null;
+        return getAdapter() != null;
     }
 
     public static BluetoothAdapter getAdapter() {
+        // TODO: should be acquired other way on Jelly Bean MR2
+        // Look at the BluetoothAdapter’s docs for details.
+        // It will require to use the latest version of SDK to get needed constant.
+
         return BluetoothAdapter.getDefaultAdapter();
     }
 }


More information about the Libreoffice-commits mailing list