diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-06-30 03:20:59 +0300 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2013-07-25 18:01:48 +0100 |
commit | f6a170524c4b74045c302ca08b37e791573c3755 (patch) | |
tree | 2edaf0761e98564ba68bb0a69881a0900ae46acd /android | |
parent | 51b6e108ba28a9b7946f1916d9377afaaf9a18b3 (diff) |
Add CommandsTransmitter and MessagesReceiver classes.
These classes consist of the code from Client, Receiver and Transmitter
classes. The main goal is to combine all actions at suitable plases.
Change-Id: Ic90f1c0a47a31bd32d57f409fe24a60f3b0686e1
Diffstat (limited to 'android')
3 files changed, 278 insertions, 0 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommandsTransmitter.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommandsTransmitter.java new file mode 100644 index 000000000000..1947021f17ff --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommandsTransmitter.java @@ -0,0 +1,87 @@ +/* -*- 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.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; + +public class CommandsTransmitter { + private final BufferedWriter mCommandsWriter; + + public CommandsTransmitter(ServerConnection aServerConnection) { + mCommandsWriter = buildCommandsWriter(aServerConnection); + } + + private BufferedWriter buildCommandsWriter(ServerConnection aServerConnection) { + try { + OutputStream aCommandsStream = aServerConnection.buildCommandsStream(); + + return new BufferedWriter( + new OutputStreamWriter(aCommandsStream, Protocol.CHARSET)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Unable to create commands writer."); + } + } + + public void pair(String aDeviceName, String aPin) { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.PAIR_WITH_SERVER, aDeviceName, + aPin)); + } + + private void writeCommand(String aCommand) { + try { + mCommandsWriter.write(aCommand); + mCommandsWriter.flush(); + } catch (IOException e) { + throw new RuntimeException("Unable to write command."); + } + } + + public void performNextTransition() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.TRANSITION_NEXT)); + } + + public void performPreviousTransition() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.TRANSITION_PREVIOUS)); + } + + public void setCurrentSlide(int slideIndex) { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.GOTO_SLIDE, + Integer.toString(slideIndex))); + } + + public void setUpBlankScreen() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.PRESENTATION_BLANK_SCREEN)); + } + + public void resumePresentation() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.PRESENTATION_RESUME)); + } + + public void startPresentation() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.PRESENTATION_START)); + } + + public void stopPresentation() { + writeCommand(Protocol.Commands + .prepareCommand(Protocol.Commands.PRESENTATION_STOP)); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesListener.java b/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesListener.java new file mode 100644 index 000000000000..7bc098b6f7cf --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesListener.java @@ -0,0 +1,17 @@ +package org.libreoffice.impressremote.communication; + +public interface MessagesListener { + public void onPinValidation(); + + public void onSuccessfulPairing(); + + public void onSlideShowStart(int aSlidesCount, int aCurrentSlideIndex); + + public void onSlideShowFinish(); + + public void onSlideChanged(int aCurrentSlideIndex); + + public void onSlidePreview(int aSlideIndex, byte[] aPreview); + + public void onSlideNotes(int aSlideIndex, String aNotes); +} diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesReceiver.java b/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesReceiver.java new file mode 100644 index 000000000000..b187e2494a4c --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/MessagesReceiver.java @@ -0,0 +1,174 @@ +/* -*- 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.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import android.text.TextUtils; +import android.util.Base64; + +public class MessagesReceiver implements Runnable { + private final BufferedReader mMessagesReader; + + private final MessagesListener mMessagesListener; + + private final Thread mMessagesListenerThread; + + public MessagesReceiver(ServerConnection aServerConnection, MessagesListener aMessagesListener) { + mMessagesReader = buildMessagesReader(aServerConnection); + + mMessagesListener = aMessagesListener; + + mMessagesListenerThread = new Thread(this); + mMessagesListenerThread.start(); + } + + private BufferedReader buildMessagesReader(ServerConnection aServerConnection) { + try { + InputStream aMessagesStream = aServerConnection.buildMessagesStream(); + + return new BufferedReader( + new InputStreamReader(aMessagesStream, Protocol.CHARSET)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Unable to create messages reader."); + } + } + + @Override + public void run() { + while (true) { + List<String> aMessage = readMessage(); + + if (aMessage == null) { + return; + } + + parseMessage(aMessage); + } + } + + private List<String> readMessage() { + List<String> aMessage = new ArrayList<String>(); + + String aMessageParameter = readMessageParameter(); + + while (true) { + if (aMessageParameter == null) { + break; + } + + if (TextUtils.isEmpty(aMessageParameter)) { + break; + } + + aMessage.add(aMessageParameter); + + aMessageParameter = readMessageParameter(); + } + + if (aMessageParameter == null) { + return null; + } + + return aMessage; + } + + private String readMessageParameter() { + try { + return mMessagesReader.readLine(); + } catch (IOException e) { + throw new RuntimeException("Unable to read message parameter."); + } + } + + private void parseMessage(List<String> aMessage) { + if (aMessage.isEmpty()) { + return; + } + + String aMessageType = aMessage.get(0); + + if (Protocol.Messages.VALIDATING.equals(aMessageType)) { + mMessagesListener.onPinValidation(); + return; + } + + if (Protocol.Messages.PAIRED.equals(aMessageType)) { + mMessagesListener.onSuccessfulPairing(); + return; + } + + if (Protocol.Messages.SLIDESHOW_STARTED.equals(aMessageType)) { + int aSlidesCount = parseSlidesCount(aMessage, 1); + int aCurrentSlideIndex = parseSlideIndex(aMessage, 2); + + mMessagesListener.onSlideShowStart(aSlidesCount, aCurrentSlideIndex); + return; + } + + if (Protocol.Messages.SLIDESHOW_FINISHED.equals(aMessageType)) { + mMessagesListener.onSlideShowFinish(); + return; + } + + if (Protocol.Messages.SLIDE_UPDATED.equals(aMessageType)) { + int aCurrentSlideIndex = parseSlideIndex(aMessage, 1); + + mMessagesListener.onSlideChanged(aCurrentSlideIndex); + return; + } + + if (Protocol.Messages.SLIDE_PREVIEW.equals(aMessageType)) { + int aSlideIndex = parseSlideIndex(aMessage, 1); + byte[] aSlidePreview = parseSlidePreview(aMessage, 2); + + mMessagesListener.onSlidePreview(aSlideIndex, aSlidePreview); + return; + } + + if (Protocol.Messages.SLIDE_NOTES.equals(aMessageType)) { + int aSlideIndex = parseSlideIndex(aMessage, 1); + String aSlideNotes = parseSlideNotes(aMessage, 2); + + mMessagesListener.onSlideNotes(aSlideIndex, aSlideNotes); + } + } + + private int parseSlidesCount(List<String> aMessage, int aMessageParameterIndex) { + return Integer.parseInt(aMessage.get(aMessageParameterIndex)); + } + + private int parseSlideIndex(List<String> aMessage, int aMessageParameterIndex) { + return Integer.parseInt(aMessage.get(aMessageParameterIndex)); + } + + private byte[] parseSlidePreview(List<String> aMessage, int aMessageParameterIndex) { + String aPreviewAsString = aMessage.get(aMessageParameterIndex); + + return Base64.decode(aPreviewAsString, Base64.DEFAULT); + } + + private String parseSlideNotes(List<String> aMessage, int aMessageParameterIndex) { + StringBuilder aNotesBuilder = new StringBuilder(); + + for (int aNoteIndex = aMessageParameterIndex; aNoteIndex < aMessage.size(); aNoteIndex++) { + aNotesBuilder.append(aMessage.get(aNoteIndex)); + } + + return aNotesBuilder.toString(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |