Move pairing operations from CommunicationService to a PairingProvider.
Clean up CommunicationService as well. Change-Id: I0fcea89b2531192869f4e039dba7e06528f22def
This commit is contained in:
parent
1b085b8f73
commit
d080b0efa1
@ -27,6 +27,7 @@ import android.content.DialogInterface.OnCancelListener;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
@ -229,7 +230,7 @@ public class SelectorActivity extends SherlockActivity {
|
|||||||
String aFormat = getResources().getString(
|
String aFormat = getResources().getString(
|
||||||
R.string.selector_dialog_connectionfailed);
|
R.string.selector_dialog_connectionfailed);
|
||||||
String aDialogText = MessageFormat.format(aFormat,
|
String aDialogText = MessageFormat.format(aFormat,
|
||||||
mCommunicationService.getPairingDeviceName());
|
Build.MODEL);
|
||||||
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(
|
AlertDialog.Builder builder = new AlertDialog.Builder(
|
||||||
SelectorActivity.this);
|
SelectorActivity.this);
|
||||||
|
@ -31,37 +31,48 @@ public class CommunicationService extends Service implements Runnable, MessagesL
|
|||||||
*/
|
*/
|
||||||
private final Object mConnectionVariableMutex = new Object();
|
private final Object mConnectionVariableMutex = new Object();
|
||||||
|
|
||||||
private State mState = State.DISCONNECTED;
|
private State mState;
|
||||||
private State mStateDesired = State.DISCONNECTED;
|
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;
|
||||||
|
|
||||||
/**
|
private MessagesReceiver mMessagesReceiver;
|
||||||
* Get the publicly visible device name -- generally the bluetooth name,
|
private CommandsTransmitter mCommandsTransmitter;
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BluetoothAdapter.getDefaultAdapter().getName() == null) {
|
private SlideShow mSlideShow;
|
||||||
return Build.MODEL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BluetoothAdapter.getDefaultAdapter().getName();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPairingDeviceName() {
|
public class CBinder extends Binder {
|
||||||
return getDeviceName();
|
public CommunicationService getService() {
|
||||||
|
return CommunicationService.this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return mBinder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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() {
|
private void closeConnection() {
|
||||||
mServerConnection.close();
|
mServerConnection.close();
|
||||||
|
|
||||||
@ -118,7 +124,7 @@ public class CommunicationService extends Service implements Runnable, MessagesL
|
|||||||
mMessagesReceiver = new MessagesReceiver(mServerConnection, this);
|
mMessagesReceiver = new MessagesReceiver(mServerConnection, this);
|
||||||
mCommandsTransmitter = new CommandsTransmitter(mServerConnection);
|
mCommandsTransmitter = new CommandsTransmitter(mServerConnection);
|
||||||
|
|
||||||
if (isPairingNecessary()) {
|
if (PairingProvider.isPairingNecessary(mServerDesired)) {
|
||||||
pair();
|
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() {
|
private void pair() {
|
||||||
mCommandsTransmitter.pair(getDeviceName(), loadPin());
|
String aPairingDeviceName = PairingProvider.getPairingDeviceName(this);
|
||||||
}
|
String aPairingPin = PairingProvider.getPairingPin(this, mServerDesired);
|
||||||
|
|
||||||
private String loadPin() {
|
mCommandsTransmitter.pair(aPairingDeviceName, aPairingPin);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectionFailed() {
|
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) {
|
public void connectTo(Server aServer) {
|
||||||
synchronized (mConnectionVariableMutex) {
|
synchronized (mConnectionVariableMutex) {
|
||||||
if (mState == State.SEARCHING) {
|
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() {
|
public CommandsTransmitter getTransmitter() {
|
||||||
return mCommandsTransmitter;
|
return mCommandsTransmitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Server> getServers() {
|
|
||||||
return mServersManager.getServers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public SlideShow getSlideShow() {
|
public SlideShow getSlideShow() {
|
||||||
return mSlideShow;
|
return mSlideShow;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Deprecated
|
||||||
* Manually add a new (network) server to the list of servers.
|
|
||||||
*/
|
|
||||||
public void addServer(String aAddress, String aName, boolean aRemember) {
|
public void addServer(String aAddress, String aName, boolean aRemember) {
|
||||||
mServersManager.addTcpServer(aAddress, aName);
|
mServersManager.addTcpServer(aAddress, aName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addServer(String aAddress, String aName) {
|
||||||
|
mServersManager.addTcpServer(aAddress, aName);
|
||||||
|
}
|
||||||
|
|
||||||
public void removeServer(Server aServer) {
|
public void removeServer(Server aServer) {
|
||||||
mServersManager.removeServer(aServer);
|
mServersManager.removeServer(aServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPinValidation() {
|
public void onPinValidation() {
|
||||||
Intent aIntent = Intents.buildPairingValidationIntent(loadPin());
|
String aPin = PairingProvider.getPairingPin(this, mServerDesired);
|
||||||
|
|
||||||
|
Intent aIntent = Intents.buildPairingValidationIntent(aPin);
|
||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,8 +240,6 @@ public class CommunicationService extends Service implements Runnable, MessagesL
|
|||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SlideShow mSlideShow;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSlideShowStart(int aSlidesCount, int aCurrentSlideIndex) {
|
public void onSlideShowStart(int aSlidesCount, int aCurrentSlideIndex) {
|
||||||
mSlideShow = new SlideShow();
|
mSlideShow = new SlideShow();
|
||||||
@ -317,6 +282,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL
|
|||||||
Intent aIntent = Intents.buildSlideNotesIntent(aSlideIndex);
|
Intent aIntent = Intents.buildSlideNotesIntent(aSlideIndex);
|
||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
stopSearch();
|
||||||
|
|
||||||
|
mThread.interrupt();
|
||||||
|
mThread = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user