Move pairing operations from CommunicationService to a PairingProvider.

Clean up CommunicationService as well.

Change-Id: I0fcea89b2531192869f4e039dba7e06528f22def
This commit is contained in:
Artur Dryomov 2013-07-14 22:07:31 +03:00 committed by Michael Meeks
parent 1b085b8f73
commit d080b0efa1
3 changed files with 128 additions and 85 deletions

View File

@ -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);

View File

@ -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;
private SlideShow mSlideShow;
private Thread mThread;
@Override
public void onCreate() {
mState = State.DISCONNECTED;
mStateDesired = State.DISCONNECTED;
mServerDesired = null;
mBinder = new CBinder();
mServersManager = new ServersManager(this);
mThread = new Thread(this);
mThread.start();
}
if (BluetoothAdapter.getDefaultAdapter().getName() == null) {
return Build.MODEL;
public class CBinder extends Binder {
public CommunicationService getService() {
return CommunicationService.this;
}
}
return BluetoothAdapter.getDefaultAdapter().getName();
}
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());
}
String aPairingDeviceName = PairingProvider.getPairingDeviceName(this);
String aPairingPin = PairingProvider.getPairingPin(this, mServerDesired);
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);
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: */

View File

@ -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();
}
}