android/sdremote/AndroidManifest.xml                                           
            |    1 
 android/sdremote/res/layout/testlayout.xml                                     
            |    5 
 android/sdremote/src/org/libreoffice/impressremote/TestClient.java             
            |   61 +++++++-
 android/sdremote/src/org/libreoffice/impressremote/communication/Client.java   
            |   16 +-
 
android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
 |   15 +-
 
android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java
        |   25 ++-
 android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java 
            |   73 ++++++++++
 
android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java
          |    1 
 8 files changed, 175 insertions(+), 22 deletions(-)

New commits:
commit 135f1e6a9c75412bab66d0071fe59aa07efd7e31
Author: Andrzej J. R. Hunt <andr...@ahunt.org>
Date:   Tue Jul 10 19:16:02 2012 +0100

    Preview image and slide number receiving code added to Android client.
    
    Change-Id: I40617d0523eaf7b9431694a0a4cd6d31ad92765e

diff --git a/android/sdremote/AndroidManifest.xml 
b/android/sdremote/AndroidManifest.xml
index e1c76d7..82cb1be 100644
--- a/android/sdremote/AndroidManifest.xml
+++ b/android/sdremote/AndroidManifest.xml
@@ -5,6 +5,7 @@
     android:versionName="1.0" >
 
     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
 
     <application
         android:icon="@drawable/ic_launcher"
diff --git a/android/sdremote/res/layout/testlayout.xml 
b/android/sdremote/res/layout/testlayout.xml
index 2e32325..8e7a4f1 100644
--- a/android/sdremote/res/layout/testlayout.xml
+++ b/android/sdremote/res/layout/testlayout.xml
@@ -25,4 +25,9 @@
         android:layout_gravity="center_horizontal"
         android:text="Previous" />
 
+    <ImageView
+        android:id="@+id/image_preview"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content" />
+
 </LinearLayout>
\ No newline at end of file
diff --git a/android/sdremote/src/org/libreoffice/impressremote/TestClient.java 
b/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
index b3bd3b5..e275efb 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/TestClient.java
@@ -1,18 +1,23 @@
 package org.libreoffice.impressremote;
 
 import org.libreoffice.impressremote.communication.CommunicationService;
-import org.libreoffice.impressremote.communication.Transmitter;
-
 import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
+import android.os.Message;
+import android.os.Messenger;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.TextView;
 
 public class TestClient extends Activity {
 
@@ -20,27 +25,45 @@ public class TestClient extends Activity {
 
        private CommunicationService mCommunicationService;
 
+       final Messenger mMessenger = new Messenger(new MessageHandler());
+
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.testlayout);
                setupUI();
+
+       }
+
+       @Override
+       protected void onResume() {
+               super.onResume();
                doBindService();
+       }
 
+       // FIXME: move all necessary code to CommunicationService.onUnbind
+
+       @Override
+       protected void onPause() {
+               super.onPause();
+               doUnbindService();
+               stopService(new Intent(this, CommunicationService.class));
        }
 
        private ServiceConnection mConnection = new ServiceConnection() {
+               @Override
                public void onServiceConnected(ComponentName aClassName,
                                IBinder aService) {
                        mCommunicationService = ((CommunicationService.CBinder) 
aService)
                                        .getService();
                        mCommunicationService.connectTo(
-                                       CommunicationService.Protocol.NETWORK, 
"128.232.128.144");
-
+                                       CommunicationService.Protocol.NETWORK, 
"10.0.2.2");
+                       mCommunicationService.setActivityMessenger(mMessenger);
                        enableButtons(true);
                }
 
+               @Override
                public void onServiceDisconnected(ComponentName aClassName) {
                        mCommunicationService = null;
                        enableButtons(false);
@@ -54,6 +77,7 @@ public class TestClient extends Activity {
        }
 
        void doUnbindService() {
+               mCommunicationService.setActivityMessenger(null);
                if (mIsBound) {
                        unbindService(mConnection);
                        mIsBound = false;
@@ -64,9 +88,15 @@ public class TestClient extends Activity {
 
        private Button mButtonPrevious;
 
+       private ImageView mImageView;
+
+       private TextView mSlideLabel;
+
        private void setupUI() {
                mButtonNext = (Button) findViewById(R.id.button_next);
                mButtonPrevious = (Button) findViewById(R.id.button_previous);
+               mImageView = (ImageView) findViewById(R.id.image_preview);
+               mSlideLabel = (TextView) findViewById(R.id.label_curSlide);
 
                enableButtons(false);
 
@@ -96,4 +126,27 @@ public class TestClient extends Activity {
                mButtonNext.setEnabled(aEnabled);
                mButtonPrevious.setEnabled(aEnabled);
        }
+
+       class MessageHandler extends Handler {
+               @Override
+               public void handleMessage(Message aMessage) {
+                       Bundle aData = aMessage.getData();
+                       switch (aMessage.what) {
+                       case CommunicationService.MSG_SLIDE_CHANGED:
+                               int newSlide = aData.getInt("slide_number");
+                               mSlideLabel.setText("Slide " + newSlide);
+                               // TODO: set slide;
+                               break;
+                       case CommunicationService.MSG_SLIDE_PREVIEW:
+                               int slideNumber = aData.getInt("slide_number");
+                               byte[] aPreviewImage = 
aData.getByteArray("preview_image");
+                               Bitmap aBitmap = 
BitmapFactory.decodeByteArray(aPreviewImage,
+                                               0, aPreviewImage.length);
+                               mImageView.setImageBitmap(aBitmap);
+                               // TODO: update
+                               break;
+
+                       }
+               }
+       }
 }
diff --git 
a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java
index 5596644..7014d78 100644
--- 
a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java
+++ 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java
@@ -1,10 +1,7 @@
 package org.libreoffice.impressremote.communication;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 
@@ -19,15 +16,22 @@ import org.json.JSONObject;
  * methods.
  *
  * @author Andrzej J.R. Hunt
- *
  */
 public abstract class Client {
 
-       private static final String CHARSET = "UTF-16";
+       private static final String CHARSET = "UTF-8";
 
        protected InputStream mInputStream;
        protected OutputStream mOutputStream;
 
+       public abstract void closeConnection();
+
+       private Receiver mReceiver;
+
+       public void setReceiver(Receiver aReceiver) {
+               aReceiver = mReceiver;
+       }
+
        private void listen() {
                ByteArrayBuffer aBuffer = new ByteArrayBuffer(10);
                byte aTemp;
@@ -65,7 +69,7 @@ public abstract class Client {
                        throw new Error("Specified network encoding [" + CHARSET
                                        + " not available.");
                }
-               parseCommand(aCommandString);
+               mReceiver.parseCommand(aCommandString);
 
        }
 
diff --git 
a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
 
b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
index 7a9db40..eec69fc 100644
--- 
a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
+++ 
b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
@@ -4,6 +4,7 @@ import android.app.Service;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
+import android.os.Messenger;
 
 public class CommunicationService extends Service {
 
@@ -22,10 +23,19 @@ public class CommunicationService extends Service {
                NETWORK, BLUETOOTH
        };
 
+       public static final int MSG_SLIDE_CHANGED = 1;
+       public static final int MSG_SLIDE_PREVIEW = 2;
+
        private Transmitter mTransmitter;
 
        private Client mClient;
 
+       private Receiver mReceiver = new Receiver();
+
+       public void setActivityMessenger(Messenger aActivityMessenger) {
+               mReceiver.setActivityMessenger(aActivityMessenger);
+       }
+
        @Override
        public IBinder onBind(Intent intent) {
                // TODO Auto-generated method stub
@@ -35,7 +45,6 @@ public class CommunicationService extends Service {
        @Override
        public void onCreate() {
                // TODO Create a notification (if configured).
-
        }
 
        @Override
@@ -45,7 +54,6 @@ public class CommunicationService extends Service {
 
        public Transmitter getTransmitter() {
                return mTransmitter;
-
        }
 
        public void connectTo(Protocol aProtocol, String address) {
@@ -53,6 +61,7 @@ public class CommunicationService extends Service {
                case NETWORK:
                        mClient = new NetworkClient(address);
                        mTransmitter = new Transmitter(mClient);
+                       mClient.setReceiver(mReceiver);
                        break;
 
                }
@@ -60,7 +69,7 @@ public class CommunicationService extends Service {
        }
 
        public void disconnect() {
-
+               mClient.closeConnection();
        }
 
 }
diff --git 
a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java
 
b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java
index 1da1941..b51711d 100644
--- 
a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java
+++ 
b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java
@@ -4,26 +4,25 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
-import android.content.Context;
-import android.content.ServiceConnection;
-
 /**
  * Standard Network client. Connects to a server using Sockets.
  *
  * @author Andrzej J.R. Hunt
- *
  */
 public class NetworkClient extends Client {
 
        private static final int PORT = 1599;
 
+       private Socket mSocket;
+
        public NetworkClient(String ipAddress) {
 
-               Socket aSocket;
+               System.out.println("Attempting to open port.");
                try {
-                       aSocket = new Socket(ipAddress, PORT);
-                       mInputStream = aSocket.getInputStream();
-                       mOutputStream = aSocket.getOutputStream();
+                       mSocket = new Socket(ipAddress, PORT);
+                       System.out.println("We seem to have opened.");
+                       mInputStream = mSocket.getInputStream();
+                       mOutputStream = mSocket.getOutputStream();
                } catch (UnknownHostException e) {
                        // TODO Tell the user we have a problem
                        e.printStackTrace();
@@ -34,4 +33,14 @@ public class NetworkClient extends Client {
 
        }
 
+       @Override
+       public void closeConnection() {
+               try {
+                       mSocket.close();
+               } catch (IOException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+       }
+
 }
diff --git 
a/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java
 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java
new file mode 100644
index 0000000..b6a3c97
--- /dev/null
+++ 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Receiver.java
@@ -0,0 +1,73 @@
+/* -*- Mode: C++; 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 org.json.JSONException;
+import org.json.JSONObject;
+
+import android.os.Bundle;
+import android.os.Message;
+import android.os.Messenger;
+import android.os.RemoteException;
+import android.util.Base64;
+
+public class Receiver {
+
+       private Messenger mActivityMessenger;
+
+       public void setActivityMessenger(Messenger aActivityMessenger) {
+               mActivityMessenger = aActivityMessenger;
+       }
+
+       public void parseCommand(String aJSONCommandString) {
+               if (mActivityMessenger == null) {
+                       return;
+               }
+               try {
+                       JSONObject aJSONCommand = new 
JSONObject(aJSONCommandString);
+                       String aInstruction = aJSONCommand.getString("command");
+                       if (aInstruction.equals("slide_updated")) {
+                               int aSlideNumber = 
aJSONCommand.getInt("slide_number");
+                               Message aMessage = Message.obtain(null,
+                                               
CommunicationService.MSG_SLIDE_CHANGED);
+                               Bundle aData = new Bundle();
+                               aData.putInt("slide_number", aSlideNumber);
+                               aMessage.setData(aData);
+                               try {
+                                       mActivityMessenger.send(aMessage);
+                               } catch (RemoteException e) {
+                                       // TODO Auto-generated catch block
+                                       e.printStackTrace();
+                               }
+                       } else if (aInstruction.equals("slide_preview")) {
+                               int aSlideNumber = 
aJSONCommand.getInt("slide_number");
+                               String aImageString = 
aJSONCommand.getString("image");
+                               byte[] aImage = Base64.decode(aImageString, 
Base64.DEFAULT);
+                               Message aMessage = Message.obtain(null,
+                                               
CommunicationService.MSG_SLIDE_PREVIEW);
+                               Bundle aData = new Bundle();
+                               aData.putInt("slide_number", aSlideNumber);
+                               aData.putByteArray("preview_image", aImage);
+                               aMessage.setData(aData);
+                               try {
+                                       mActivityMessenger.send(aMessage);
+                               } catch (RemoteException e) {
+                                       // TODO Auto-generated catch block
+                                       e.printStackTrace();
+                               }
+                       }
+
+               } catch (JSONException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+
+       }
+}
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file
diff --git 
a/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java
 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java
index 0e2da45..2861498 100644
--- 
a/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java
+++ 
b/android/sdremote/src/org/libreoffice/impressremote/communication/Transmitter.java
@@ -7,7 +7,6 @@ import org.json.JSONObject;
  * Interface to send commands to the server.
  *
  * @author Andrzej J.R. Hunt
- *
  */
 public class Transmitter {
 
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to