diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-06-30 03:25:02 +0300 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2013-07-25 18:01:49 +0100 |
commit | 615a900393729e7ee7d286b88dbe45634c803e8a (patch) | |
tree | 2782dc9613c61abeb195fe395e72cfe3a9c1c4c1 /android/sdremote | |
parent | f6a170524c4b74045c302ca08b37e791573c3755 (diff) |
Change the existing code to use new classes.
* Remove old classes, their functionality was moved to more suitable
places.
* Update existing ones to use new schema.
Change-Id: Ic525fd4682051317dc717dedb6d08b97f11c0b09
Diffstat (limited to 'android/sdremote')
10 files changed, 165 insertions, 741 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java b/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java index 0b0624e533b2..45222c00e5df 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/PairingActivity.java @@ -70,8 +70,6 @@ public class PairingActivity extends SherlockActivity { @Override public void onBackPressed() { - mCommunicationService.getClient().closeConnection(); - Intent aIntent = new Intent(this, SelectorActivity.class); aIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(aIntent); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java deleted file mode 100644 index 8da48bb8dc63..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java +++ /dev/null @@ -1,123 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.communication; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.UUID; - -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothSocket; -import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; - -public class BluetoothClient extends Client { - // Standard UUID for the Serial Port Profile. - // https://www.bluetooth.org/en-us/specification/assigned-numbers-overview/service-discovery - private static final String STANDARD_SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; - - private final boolean mBluetoothWasEnabled; - - private BluetoothSocket mSocket; - - public BluetoothClient(Server aServer, CommunicationService aCommunicationService, Receiver aReceiver, boolean aBluetoothWasEnabled) { - super(aServer, aCommunicationService, aReceiver); - - mBluetoothWasEnabled = aBluetoothWasEnabled; - - if (!mBluetoothWasEnabled) { - BluetoothAdapter.getDefaultAdapter().enable(); - } - } - - @Override - protected void setUpServerConnection() { - mSocket = buildServerConnection(); - } - - private BluetoothSocket buildServerConnection() { - try { - BluetoothDevice aBluetoothServer = BluetoothAdapter - .getDefaultAdapter() - .getRemoteDevice(mServer.getAddress()); - - BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); - - BluetoothSocket aSocket = aBluetoothServer - .createRfcommSocketToServiceRecord( - UUID.fromString(STANDARD_SPP_UUID)); - - aSocket.connect(); - - return aSocket; - } catch (IOException e) { - throw new RuntimeException("Unable to connect to Bluetooth host."); - } - } - - protected InputStream buildMessagesStream() { - try { - return mSocket.getInputStream(); - } catch (IOException e) { - throw new RuntimeException("Unable to open messages stream."); - } - } - - protected OutputStream buildCommandsStream() { - try { - return mSocket.getOutputStream(); - } catch (IOException e) { - throw new RuntimeException("Unable to open commands stream."); - } - } - - @Override - public void closeConnection() { - try { - mSocket.close(); - } catch (IOException e) { - throw new RuntimeException("Unable to close Bluetooth socket."); - } - } - - protected void onDisconnect() { - if (!mBluetoothWasEnabled) { - BluetoothAdapter.getDefaultAdapter().disable(); - } - } - - @Override - public void validating() throws IOException { - String aMessage = mMessagesReader.readLine(); - - if (!aMessage.equals(Protocol.Messages.PAIRED)) { - return; - } - - while (mMessagesReader.readLine().length() != 0) { - // Get rid of extra lines - } - - callSuccessfulPairing(); - - startListening(); - } - - private void callSuccessfulPairing() { - Intent aSuccessfulPairingIntent = new Intent( - CommunicationService.MSG_PAIRING_SUCCESSFUL); - - LocalBroadcastManager.getInstance(mCommunicationService) - .sendBroadcast(aSuccessfulPairingIntent); - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java deleted file mode 100644 index 10fa6575ba87..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java +++ /dev/null @@ -1,168 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.communication; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.List; - -import android.content.Intent; -import android.text.TextUtils; - -public abstract class Client implements Runnable { - protected final BufferedReader mMessagesReader; - protected final OutputStream mCommandsStream; - - protected String mPin = ""; - protected String mName = ""; - - private static Client latestInstance = null; - - protected final Server mServer; - protected final CommunicationService mCommunicationService; - protected final Receiver mReceiver; - - protected Client(Server aServer, CommunicationService aCommunicationService, Receiver aReceiver) { - mServer = aServer; - mName = aServer.getName(); - mCommunicationService = aCommunicationService; - mReceiver = aReceiver; - latestInstance = this; - - setUpServerConnection(); - - mMessagesReader = buildMessagesReader(buildMessagesStream()); - mCommandsStream = buildCommandsStream(); - } - - protected abstract void setUpServerConnection(); - - private BufferedReader buildMessagesReader(InputStream aMessagesStream) { - try { - return new BufferedReader( - new InputStreamReader(aMessagesStream, Protocol.CHARSET)); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Unable to create messages reader."); - } - } - - protected abstract InputStream buildMessagesStream(); - - protected abstract OutputStream buildCommandsStream(); - - public static String getPin() { - if (latestInstance == null) { - return ""; - } - - return latestInstance.mName; - } - - public static String getName() { - if (latestInstance == null) { - return ""; - } - - return latestInstance.mName; - } - - protected void startListening() { - Thread aListeningThread = new Thread(this); - - aListeningThread.start(); - } - - @Override - public void run() { - listen(); - } - - private void listen() { - try { - while (true) { - List<String> aMessage = readMessage(); - - if (aMessage == null) { - return; - } - - mReceiver.parseCommand(aMessage); - } - } catch (IOException e) { - // TODO: stream couldn't be opened - e.printStackTrace(); - } finally { - onDisconnect(); - } - } - - private List<String> readMessage() throws IOException { - List<String> aMessage = new ArrayList<String>(); - - String aMessageParameter = mMessagesReader.readLine(); - - while ((aMessageParameter != null) && (!TextUtils - .isEmpty(aMessageParameter))) { - aMessage.add(aMessageParameter); - - aMessageParameter = mMessagesReader.readLine(); - } - - if (aMessageParameter == null) { - startReconnection(); - - return null; - } - - return aMessage; - } - - private void startReconnection() { - Intent aReconnectionIntent = new Intent( - mCommunicationService.getApplicationContext(), - ReconnectionActivity.class); - aReconnectionIntent.putExtra("server", mServer); - aReconnectionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - - mCommunicationService.getApplicationContext() - .startActivity(aReconnectionIntent); - } - - /** - * Called after the Client disconnects. Can be extended to allow for - * cleaning up bluetooth properties etc. - */ - protected void onDisconnect() { - } - - /** - * Send a valid command to the Server. - */ - public void sendCommand(String aCommand) { - try { - mCommandsStream.write(aCommand.getBytes(Protocol.CHARSET)); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 must be used for commands."); - } catch (IOException e) { - // I.e. connection closed. This will be dealt with by the listening - // loop. - } - } - - public abstract void closeConnection(); - - public abstract void validating() throws IOException; -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 273362c11d26..8678286461d9 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -8,7 +8,6 @@ */ package org.libreoffice.impressremote.communication; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -26,9 +25,8 @@ import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import org.libreoffice.impressremote.Preferences; -import org.libreoffice.impressremote.communication.Server.Protocol; -public class CommunicationService extends Service implements Runnable { +public class CommunicationService extends Service implements Runnable, MessagesListener { public static enum State { DISCONNECTED, SEARCHING, CONNECTING, CONNECTED } @@ -62,14 +60,9 @@ public class CommunicationService extends Service implements Runnable { private final IBinder mBinder = new CBinder(); - private Transmitter mTransmitter; - - private Client mClient; - - private final Receiver mReceiver = new Receiver(this); - private final ServersFinder mTcpServersFinder = new TcpServersFinder(this); - private final ServersFinder mBluetoothServersFinder = new BluetoothServersFinder(this); + private final ServersFinder mBluetoothServersFinder = new BluetoothServersFinder( + this); private Thread mThread = null; @@ -98,7 +91,7 @@ public class CommunicationService extends Service implements Runnable { } public String getPairingDeviceName() { - return Client.getName(); + return getDeviceName(); } @Override @@ -133,42 +126,76 @@ public class CommunicationService extends Service implements Runnable { } } + private ServerConnection mServerConnection; + + private MessagesReceiver mMessagesReceiver; + private CommandsTransmitter mCommandsTransmitter; + private void closeConnection() { - mClient.closeConnection(); - mClient = null; + mServerConnection.close(); mState = State.DISCONNECTED; } private void openConnection() { - try { - mClient = buildClient(); - mClient.validating(); + mServerConnection = buildServerConnection(); - mTransmitter = new Transmitter(mClient); + mMessagesReceiver = new MessagesReceiver(mServerConnection, this); + mCommandsTransmitter = new CommandsTransmitter(mServerConnection); - mState = State.CONNECTED; - } catch (IOException e) { - connectionFailed(); - } + pairWithServer(); + + mState = State.CONNECTED; } - private Client buildClient() { + private ServerConnection buildServerConnection() { switch (mServerDesired.getProtocol()) { - case NETWORK: - return new NetworkClient(mServerDesired, this, mReceiver); + case TCP: + return new TcpServerConnection(mServerDesired); case BLUETOOTH: - return new BluetoothClient(mServerDesired, this, mReceiver, - mBluetoothPreviouslyEnabled); + return new BluetoothServerConnection(mServerDesired); default: throw new RuntimeException("Unknown desired protocol."); } } + private void pairWithServer() { + if (mServerDesired.getProtocol() == Server.Protocol.BLUETOOTH) { + return; + } + + mCommandsTransmitter.pair(getDeviceName(), loadPin()); + + startPairingActivity(); + } + + private void startPairingActivity() { + Intent aPairingIntent = new Intent(MSG_PAIRING_STARTED); + aPairingIntent.putExtra("PIN", loadPin()); + + LocalBroadcastManager.getInstance(this).sendBroadcast(aPairingIntent); + } + + 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; + } + private void connectionFailed() { - mClient = null; mState = State.DISCONNECTED; Intent aIntent = new Intent( CommunicationService.STATUS_CONNECTION_FAILED); @@ -254,8 +281,8 @@ public class CommunicationService extends Service implements Runnable { mThread = null; } - public Transmitter getTransmitter() { - return mTransmitter; + public CommandsTransmitter getTransmitter() { + return mCommandsTransmitter; } public List<Server> getServers() { @@ -269,7 +296,7 @@ public class CommunicationService extends Service implements Runnable { } public SlideShow getSlideShow() { - return mReceiver.getSlideShow(); + return mSlideShow; } void loadServersFromPreferences() { @@ -282,7 +309,7 @@ public class CommunicationService extends Service implements Runnable { for (Entry<String, String> aServerEntry : aStoredMap.entrySet()) { mManualServers.put(aServerEntry.getKey(), new Server( - Protocol.NETWORK, aServerEntry.getKey(), + Server.Protocol.TCP, aServerEntry.getKey(), aServerEntry.getValue(), 0)); } } @@ -295,8 +322,9 @@ public class CommunicationService extends Service implements Runnable { if (aServer.equals(aAddress)) return; } - mManualServers.put(aAddress, new Server(Protocol.NETWORK, aAddress, - aName, 0)); + mManualServers + .put(aAddress, new Server(Server.Protocol.TCP, aAddress, + aName, 0)); if (aRemember) { Preferences @@ -312,8 +340,92 @@ public class CommunicationService extends Service implements Runnable { aServer.getAddress()); } - public Client getClient() { - return mClient; + @Override + public void onPinValidation() { + startPinValidation(); + } + + private void startPinValidation() { + Intent aPairingIntent = new Intent(STATUS_PAIRING_PINVALIDATION); + aPairingIntent.putExtra("PIN", loadPin()); + aPairingIntent.putExtra("SERVERNAME", mServerDesired.getName()); + + LocalBroadcastManager.getInstance(this).sendBroadcast(aPairingIntent); + } + + @Override + public void onSuccessfulPairing() { + callSuccessfulPairing(); + } + + private void callSuccessfulPairing() { + Intent aSuccessfulPairingIntent = new Intent(MSG_PAIRING_SUCCESSFUL); + + LocalBroadcastManager.getInstance(this).sendBroadcast( + aSuccessfulPairingIntent); + } + + private SlideShow mSlideShow; + + @Override + public void onSlideShowStart(int aSlidesCount, int aCurrentSlideIndex) { + mSlideShow = new SlideShow(); + mSlideShow.setSlidesCount(aSlidesCount); + mSlideShow.setCurrentSlideIndex(aCurrentSlideIndex); + + Intent aStatusConnectedSlideShowRunningIntent = new Intent( + STATUS_CONNECTED_SLIDESHOW_RUNNING); + Intent aSlideChangedIntent = new Intent(MSG_SLIDE_CHANGED); + aSlideChangedIntent.putExtra("slide_number", aCurrentSlideIndex); + + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aStatusConnectedSlideShowRunningIntent); + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aSlideChangedIntent); + } + + @Override + public void onSlideShowFinish() { + mSlideShow = new SlideShow(); + + Intent aStatusConnectedNoSlideShowIntent = new Intent( + STATUS_CONNECTED_NOSLIDESHOW); + + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aStatusConnectedNoSlideShowIntent); + } + + @Override + public void onSlideChanged(int aCurrentSlideIndex) { + mSlideShow.setCurrentSlideIndex(aCurrentSlideIndex); + + Intent aSlideChangedIntent = new Intent(MSG_SLIDE_CHANGED); + aSlideChangedIntent.putExtra("slide_number", aCurrentSlideIndex); + + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aSlideChangedIntent); + } + + @Override + public void onSlidePreview(int aSlideIndex, byte[] aPreview) { + mSlideShow.setSlidePreview(aSlideIndex, aPreview); + + Intent aSlidePreviewChangedIntent = new Intent(MSG_SLIDE_PREVIEW); + aSlidePreviewChangedIntent.putExtra("slide_number", aSlideIndex); + + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aSlidePreviewChangedIntent); + } + + @Override + public void onSlideNotes(int aSlideIndex, String aNotes) { + mSlideShow.setSlideNotes(aSlideIndex, aNotes); + + Intent aSlideNotesChangedIntent = new Intent(MSG_SLIDE_NOTES); + aSlideNotesChangedIntent.putExtra("slide_number", aSlideIndex); + + LocalBroadcastManager.getInstance(this) + .sendBroadcast(aSlideNotesChangedIntent); } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java deleted file mode 100644 index 438148034d0f..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java +++ /dev/null @@ -1,181 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.communication; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.Socket; -import java.net.UnknownHostException; -import java.util.Random; - -import android.content.Context; -import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; - -import org.libreoffice.impressremote.Preferences; - -public class NetworkClient extends Client { - private Socket mSocket; - - private final String mPin; - - public NetworkClient(Server aServer, CommunicationService aCommunicationService, Receiver aReceiver) { - super(aServer, aCommunicationService, aReceiver); - - mPin = loadPin(); - - startPairingActivity(); - startPairing(); - } - - private String loadPin() { - Context aContext = mCommunicationService.getApplicationContext(); - - if (Preferences - .doContain(aContext, Preferences.Locations.AUTHORIZED_REMOTES, - mServer.getName())) { - return Preferences - .getString(aContext, Preferences.Locations.AUTHORIZED_REMOTES, - mServer.getName()); - } - - String aPin = generatePin(); - - Preferences.set(aContext, Preferences.Locations.AUTHORIZED_REMOTES, - mServer.getName(), aPin); - - return aPin; - } - - private String generatePin() { - return String.format("%04d", generatePinNumber()); - } - - private int generatePinNumber() { - Random aRandomGenerator = new Random(); - - int aMaximumPin = (int) Math.pow(10, Protocol.PIN_NUMBERS_COUNT) - 1; - - return aRandomGenerator.nextInt(aMaximumPin); - } - - private void startPairingActivity() { - Intent aPairingIntent = new Intent( - CommunicationService.MSG_PAIRING_STARTED); - aPairingIntent.putExtra("PIN", mPin); - - LocalBroadcastManager.getInstance(mCommunicationService) - .sendBroadcast(aPairingIntent); - } - - private void startPairing() { - // TODO: get the proper name - String aPhoneName = CommunicationService.getDeviceName(); - - sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PAIR_WITH_SERVER, aPhoneName, mPin)); - } - - @Override - protected void setUpServerConnection() { - mSocket = buildServerConnection(); - } - - private Socket buildServerConnection() { - try { - return new Socket(mServer.getAddress(), - Protocol.Ports.CLIENT_CONNECTION); - } catch (UnknownHostException e) { - throw new RuntimeException("Unable to connect to unknown host."); - } catch (IOException e) { - e.printStackTrace(); - throw new RuntimeException("Unable to connect to host."); - } - } - - @Override - protected InputStream buildMessagesStream() { - try { - return mSocket.getInputStream(); - } catch (IOException e) { - throw new RuntimeException("Unable to open messages stream."); - } - } - - @Override - protected OutputStream buildCommandsStream() { - try { - return mSocket.getOutputStream(); - } catch (IOException e) { - throw new RuntimeException("Unable to open commands stream."); - } - } - - @Override - public void closeConnection() { - try { - mSocket.close(); - } catch (IOException e) { - throw new RuntimeException("Unable to close network socket."); - } - } - - @Override - public void validating() throws IOException { - String aMessage = mMessagesReader.readLine(); - - if (aMessage == null) { - throw new RuntimeException( - "End of stream reached before any data received."); - } - - while (!aMessage.equals(Protocol.Messages.PAIRED)) { - if (aMessage.equals(Protocol.Messages.VALIDATING)) { - startPinValidation(); - - while (mMessagesReader.readLine().length() != 0) { - // Read off empty lines - } - - aMessage = mMessagesReader.readLine(); - } else { - return; - } - } - - callSuccessfulPairing(); - - while (mMessagesReader.readLine().length() != 0) { - // Get rid of extra lines - } - - startListening(); - } - - private void startPinValidation() { - Intent aPairingIntent = new Intent( - CommunicationService.STATUS_PAIRING_PINVALIDATION); - aPairingIntent.putExtra("PIN", mPin); - aPairingIntent.putExtra("SERVERNAME", mServer.getName()); - - LocalBroadcastManager.getInstance(mCommunicationService) - .sendBroadcast(aPairingIntent); - } - - private void callSuccessfulPairing() { - Intent aSuccessfulPairingIntent = new Intent( - CommunicationService.MSG_PAIRING_SUCCESSFUL); - - LocalBroadcastManager.getInstance(mCommunicationService) - .sendBroadcast(aSuccessfulPairingIntent); - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Protocol.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Protocol.java index 6f40e5452f6c..c276ec99c5a6 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Protocol.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Protocol.java @@ -8,6 +8,8 @@ */ package org.libreoffice.impressremote.communication; +import java.util.Random; + import android.text.TextUtils; final class Protocol { @@ -16,7 +18,20 @@ final class Protocol { public static final String CHARSET = "UTF-8"; - public static final int PIN_NUMBERS_COUNT = 4; + public static final class Pin { + private Pin() { + } + + private static final int NUMBERS_COUNT = 4; + + public static String generate() { + Random aRandomGenerator = new Random(); + int aMaximumPin = (int) Math.pow(10, NUMBERS_COUNT) - 1; + int aPinNumber = aRandomGenerator.nextInt(aMaximumPin); + + return String.format("%04d", aPinNumber); + } + } public static final class Ports { private Ports() { @@ -31,7 +46,6 @@ final class Protocol { } public static final String SERVER_SEARCH = "239.0.0.1"; - public static final String SERVER_LOCAL_FOR_EMULATOR = "10.0.2.2"; } public static final class Messages { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java deleted file mode 100644 index 1ecb53e1cfb7..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java +++ /dev/null @@ -1,149 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.communication; - -import java.util.List; - -import android.content.Context; -import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; -import android.util.Base64; - -public class Receiver { - private final Context mContext; - - private SlideShow mSlideShow; - - public Receiver(Context aContext) { - this.mContext = aContext; - this.mSlideShow = new SlideShow(); - } - - public SlideShow getSlideShow() { - return mSlideShow; - } - - public boolean isSlideShowRunning() { - return mSlideShow.getSlidesCount() > 0; - } - - public void parseCommand(List<String> aInstruction) { - if (aInstruction.isEmpty()) { - return; - } - - String aCommand = aInstruction.get(0); - - if (aCommand.equals(Protocol.Messages.SLIDESHOW_STARTED)) { - startSlideShow(aInstruction); - return; - } - - if (aCommand.equals(Protocol.Messages.SLIDESHOW_FINISHED)) { - finishSlideShow(); - return; - } - - if (mSlideShow == null) { - return; - } - - if (aCommand.equals(Protocol.Messages.SLIDE_UPDATED)) { - setUpCurrentSlide(aInstruction); - return; - } - - if (aCommand.equals(Protocol.Messages.SLIDE_PREVIEW)) { - setUpSlidePreview(aInstruction); - return; - } - - if (aCommand.equals(Protocol.Messages.SLIDE_NOTES)) { - setUpSlideNotes(aInstruction); - } - } - - private void startSlideShow(List<String> aInstruction) { - int aSlideShowSlidesCount = Integer.parseInt(aInstruction.get(1)); - int aCurrentSlideIndex = Integer.parseInt(aInstruction.get(2)); - - mSlideShow.setSlidesCount(aSlideShowSlidesCount); - mSlideShow.setCurrentSlideIndex(aCurrentSlideIndex); - - Intent aStatusConnectedSlideShowRunningIntent = new Intent( - CommunicationService.STATUS_CONNECTED_SLIDESHOW_RUNNING); - Intent aSlideChangedIntent = new Intent( - CommunicationService.MSG_SLIDE_CHANGED); - aSlideChangedIntent.putExtra("slide_number", aCurrentSlideIndex); - - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aStatusConnectedSlideShowRunningIntent); - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aSlideChangedIntent); - } - - private void finishSlideShow() { - this.mSlideShow = new SlideShow(); - - Intent aStatusConnectedNoSlideShowIntent = new Intent( - CommunicationService.STATUS_CONNECTED_NOSLIDESHOW); - - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aStatusConnectedNoSlideShowIntent); - } - - private void setUpCurrentSlide(List<String> aInstruction) { - int aCurrentSlideIndex = Integer.parseInt(aInstruction.get(1)); - - mSlideShow.setCurrentSlideIndex(aCurrentSlideIndex); - - Intent aSlideChangedIntent = new Intent( - CommunicationService.MSG_SLIDE_CHANGED); - aSlideChangedIntent.putExtra("slide_number", aCurrentSlideIndex); - - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aSlideChangedIntent); - } - - private void setUpSlidePreview(List<String> aInstruction) { - int aSlideIndex = Integer.parseInt(aInstruction.get(1)); - String aImageAsString = aInstruction.get(2); - - byte[] aImage = Base64.decode(aImageAsString, Base64.DEFAULT); - mSlideShow.setSlidePreview(aSlideIndex, aImage); - - Intent aSlidePreviewChangedIntent = new Intent( - CommunicationService.MSG_SLIDE_PREVIEW); - aSlidePreviewChangedIntent.putExtra("slide_number", aSlideIndex); - - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aSlidePreviewChangedIntent); - } - - private void setUpSlideNotes(List<String> aInstruction) { - int aSlideIndex = Integer.parseInt(aInstruction.get(1)); - StringBuilder aNotesBuilder = new StringBuilder(); - for (int aNoteIndex = 2; aNoteIndex < aInstruction - .size(); aNoteIndex++) { - aNotesBuilder.append(aInstruction.get(aNoteIndex)); - } - String aNotes = aNotesBuilder.toString(); - - mSlideShow.setSlideNotes(aSlideIndex, aNotes); - - Intent aSlideNotesChangedIntent = new Intent( - CommunicationService.MSG_SLIDE_NOTES); - aSlideNotesChangedIntent.putExtra("slide_number", aSlideIndex); - - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(aSlideNotesChangedIntent); - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index 7edf63c50af2..c523f6184065 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -15,7 +15,7 @@ public class Server implements Parcelable { private static final int SPECIAL_PARCELABLE_OBJECTS_BITMASK = 0; public static enum Protocol { - NETWORK, BLUETOOTH + TCP, BLUETOOTH } private final Protocol mProtocol; diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java index 53a14c38d5c2..ea5e0b5faa19 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/TcpServersFinder.java @@ -126,7 +126,7 @@ public class TcpServersFinder implements ServersFinder, Runnable { String aFoundServerHostname = aSearchResultScanner.nextLine(); - Server aFoundServer = new Server(Server.Protocol.NETWORK, + Server aFoundServer = new Server(Server.Protocol.TCP, aSearchResultPacket.getAddress().getHostAddress(), aFoundServerHostname); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java deleted file mode 100644 index 520a0e97a27f..000000000000 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package org.libreoffice.impressremote.communication; - -import android.graphics.Color; - -/** - * Interface to send commands to the server. - */ -public class Transmitter { - private final Client mClient; - - public Transmitter(Client aClient) { - this.mClient = aClient; - } - - public void performNextTransition() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.TRANSITION_NEXT)); - } - - public void performPreviousTransition() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.TRANSITION_PREVIOUS)); - } - - public void setCurrentSlide(int slideIndex) { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.GOTO_SLIDE, - Integer.toString(slideIndex))); - } - - /** - * Blank the screen to the default colour (set server-side), which is - * generally black. This is slightly faster than using - * <code> setUpBlankScreen( colour ) </code>. - */ - public void setUpBlankScreen() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PRESENTATION_BLANK_SCREEN)); - } - - /** - * Set the screen to a specific colour. Only use if a non default colour is - * needed. - * - * @param aColor blank screen color - */ - public void setUpBlankScreen(Color aColor) { - // FIXME: check how to get colour in integer form. - - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PRESENTATION_BLANK_SCREEN, - aColor.toString())); - } - - public void resumePresentation() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PRESENTATION_RESUME)); - } - - public void startPresentation() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PRESENTATION_START)); - } - - public void stopPresentation() { - mClient.sendCommand(Protocol.Commands - .prepareCommand(Protocol.Commands.PRESENTATION_STOP)); - } -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |