diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-07-14 22:07:31 +0300 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2013-07-25 18:01:55 +0100 |
commit | d080b0efa1af812a84f4a2f383d2401b226eea86 (patch) | |
tree | ba6633e63681e5d844db292f2ca66f4217fd0c5a /android | |
parent | 1b085b8f7337f78248c24701c2ab61772e3ceaf3 (diff) |
Move pairing operations from CommunicationService to a PairingProvider.
Clean up CommunicationService as well.
Change-Id: I0fcea89b2531192869f4e039dba7e06528f22def
Diffstat (limited to 'android')
3 files changed, 128 insertions, 85 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java index 7f58b85fb29b..57e0cb75f7f2 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java @@ -27,6 +27,7 @@ import android.content.DialogInterface.OnCancelListener; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; +import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; @@ -229,7 +230,7 @@ public class SelectorActivity extends SherlockActivity { String aFormat = getResources().getString( R.string.selector_dialog_connectionfailed); String aDialogText = MessageFormat.format(aFormat, - mCommunicationService.getPairingDeviceName()); + Build.MODEL); AlertDialog.Builder builder = new AlertDialog.Builder( SelectorActivity.this); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index c819456b19bc..9b2f47ae2a1a 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -31,37 +31,48 @@ public class CommunicationService extends Service implements Runnable, MessagesL */ private final Object mConnectionVariableMutex = new Object(); - private State mState = State.DISCONNECTED; - private State mStateDesired = State.DISCONNECTED; + private State mState; + private State mStateDesired; - private Server mServerDesired = null; + private Server mServerDesired; - private final IBinder mBinder = new CBinder(); + private IBinder mBinder; - private final ServersManager mServersManager = new ServersManager(this); + private ServersManager mServersManager; - private Thread mThread = null; + private ServerConnection mServerConnection; - /** - * Get the publicly visible device name -- generally the bluetooth name, - * however for bluetoothless devices the device model name is used. - * - * @return The device name. - */ - public static String getDeviceName() { - if (BluetoothAdapter.getDefaultAdapter() == null) { - return Build.MODEL; - } + private MessagesReceiver mMessagesReceiver; + private CommandsTransmitter mCommandsTransmitter; - if (BluetoothAdapter.getDefaultAdapter().getName() == null) { - return Build.MODEL; - } + private SlideShow mSlideShow; + + private Thread mThread; + + @Override + public void onCreate() { + mState = State.DISCONNECTED; + mStateDesired = State.DISCONNECTED; + + mServerDesired = null; - return BluetoothAdapter.getDefaultAdapter().getName(); + mBinder = new CBinder(); + + mServersManager = new ServersManager(this); + + mThread = new Thread(this); + mThread.start(); + } + + public class CBinder extends Binder { + public CommunicationService getService() { + return CommunicationService.this; + } } - public String getPairingDeviceName() { - return getDeviceName(); + @Override + public IBinder onBind(Intent intent) { + return mBinder; } @Override @@ -101,11 +112,6 @@ public class CommunicationService extends Service implements Runnable, MessagesL } } - private ServerConnection mServerConnection; - - private MessagesReceiver mMessagesReceiver; - private CommandsTransmitter mCommandsTransmitter; - private void closeConnection() { mServerConnection.close(); @@ -118,7 +124,7 @@ public class CommunicationService extends Service implements Runnable, MessagesL mMessagesReceiver = new MessagesReceiver(mServerConnection, this); mCommandsTransmitter = new CommandsTransmitter(mServerConnection); - if (isPairingNecessary()) { + if (PairingProvider.isPairingNecessary(mServerDesired)) { pair(); } @@ -138,29 +144,11 @@ public class CommunicationService extends Service implements Runnable, MessagesL } } - private boolean isPairingNecessary() { - return mServerDesired.getProtocol() == Server.Protocol.TCP; - } - private void pair() { - mCommandsTransmitter.pair(getDeviceName(), loadPin()); - } - - private String loadPin() { - if (Preferences.doContain(this, - Preferences.Locations.AUTHORIZED_REMOTES, - mServerDesired.getAddress())) { - return Preferences - .getString(this, Preferences.Locations.AUTHORIZED_REMOTES, - mServerDesired.getAddress()); - } - - String aPin = Protocol.Pin.generate(); - - Preferences.set(this, Preferences.Locations.AUTHORIZED_REMOTES, - mServerDesired.getAddress(), aPin); + String aPairingDeviceName = PairingProvider.getPairingDeviceName(this); + String aPairingPin = PairingProvider.getPairingPin(this, mServerDesired); - return aPin; + mCommandsTransmitter.pair(aPairingDeviceName, aPairingPin); } private void connectionFailed() { @@ -188,6 +176,10 @@ public class CommunicationService extends Service implements Runnable, MessagesL } } + public List<Server> getServers() { + return mServersManager.getServers(); + } + public void connectTo(Server aServer) { synchronized (mConnectionVariableMutex) { if (mState == State.SEARCHING) { @@ -213,57 +205,32 @@ public class CommunicationService extends Service implements Runnable, MessagesL } } - public class CBinder extends Binder { - public CommunicationService getService() { - return CommunicationService.this; - } - } - - @Override - public IBinder onBind(Intent intent) { - return mBinder; - } - - @Override - public void onCreate() { - mThread = new Thread(this); - mThread.start(); - } - - @Override - public void onDestroy() { - stopSearch(); - - mThread.interrupt(); - mThread = null; - } - public CommandsTransmitter getTransmitter() { return mCommandsTransmitter; } - public List<Server> getServers() { - return mServersManager.getServers(); - } - public SlideShow getSlideShow() { return mSlideShow; } - /** - * Manually add a new (network) server to the list of servers. - */ + @Deprecated public void addServer(String aAddress, String aName, boolean aRemember) { mServersManager.addTcpServer(aAddress, aName); } + public void addServer(String aAddress, String aName) { + mServersManager.addTcpServer(aAddress, aName); + } + public void removeServer(Server aServer) { mServersManager.removeServer(aServer); } @Override public void onPinValidation() { - Intent aIntent = Intents.buildPairingValidationIntent(loadPin()); + String aPin = PairingProvider.getPairingPin(this, mServerDesired); + + Intent aIntent = Intents.buildPairingValidationIntent(aPin); LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent); } @@ -273,8 +240,6 @@ public class CommunicationService extends Service implements Runnable, MessagesL LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent); } - private SlideShow mSlideShow; - @Override public void onSlideShowStart(int aSlidesCount, int aCurrentSlideIndex) { mSlideShow = new SlideShow(); @@ -317,6 +282,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL Intent aIntent = Intents.buildSlideNotesIntent(aSlideIndex); LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent); } + + @Override + public void onDestroy() { + stopSearch(); + + mThread.interrupt(); + mThread = null; + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/PairingProvider.java b/android/sdremote/src/org/libreoffice/impressremote/communication/PairingProvider.java new file mode 100644 index 000000000000..e6550fbea539 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/PairingProvider.java @@ -0,0 +1,69 @@ +package org.libreoffice.impressremote.communication; + +import android.bluetooth.BluetoothAdapter; +import android.content.Context; +import android.os.Build; + +import org.libreoffice.impressremote.Preferences; + +public final class PairingProvider { + private Context mContext; + + private PairingProvider(Context aContext) { + mContext = aContext; + } + + public static boolean isPairingNecessary(Server aServer) { + return aServer.getProtocol() == Server.Protocol.TCP; + } + + public static String getPairingPin(Context aContext, Server aServer) { + return new PairingProvider(aContext).getPairingPin(aServer); + } + + private String getPairingPin(Server aServer) { + if (isPinSaved(aServer)) { + return getSavedPin(aServer); + } + + String aPin = Protocol.Pin.generate(); + + savePin(aServer, aPin); + + return aPin; + } + + private boolean isPinSaved(Server aServer) { + return getSavedPin(aServer) != null; + } + + private String getSavedPin(Server aServer) { + String aLocation = Preferences.Locations.AUTHORIZED_REMOTES; + String aServerAddress = aServer.getAddress(); + + return Preferences.getString(mContext, aLocation, aServerAddress); + } + + private void savePin(Server aServer, String aPin) { + String aLocation = Preferences.Locations.AUTHORIZED_REMOTES; + String aServerAddress = aServer.getAddress(); + + Preferences.set(mContext, aLocation, aServerAddress, aPin); + } + + public static String getPairingDeviceName(Context aContext) { + return new PairingProvider(aContext).getPairingDeviceName(); + } + + public String getPairingDeviceName() { + if (BluetoothAdapter.getDefaultAdapter() == null) { + return Build.MODEL; + } + + if (BluetoothAdapter.getDefaultAdapter().getName() == null) { + return Build.MODEL; + } + + return BluetoothAdapter.getDefaultAdapter().getName(); + } +} |