From d6dceb2bb0bf30e5a9362705156afd5339863db8 Mon Sep 17 00:00:00 2001 From: "Andrzej J.R. Hunt" Date: Mon, 3 Sep 2012 11:32:22 +0200 Subject: Implemented automatic reconnection + reconnection activity. Change-Id: I445fe2acb24ab6992aad4c75f6886f517bdcc0b0 --- .../sdremote/res/drawable/warning_exclamation.png | Bin 0 -> 3333 bytes android/sdremote/res/layout/activity_reconnect.xml | 46 +++++++++++++++++++++ android/sdremote/res/values/strings.xml | 3 +- .../communication/BluetoothClient.java | 19 ++++----- .../impressremote/communication/Client.java | 34 +++++++++++---- .../communication/CommunicationService.java | 3 +- .../impressremote/communication/NetworkClient.java | 19 +++++---- .../communication/ReconnectionActivity.java | 18 ++++++++ 8 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 android/sdremote/res/drawable/warning_exclamation.png create mode 100644 android/sdremote/res/layout/activity_reconnect.xml create mode 100644 android/sdremote/src/org/libreoffice/impressremote/communication/ReconnectionActivity.java (limited to 'android') diff --git a/android/sdremote/res/drawable/warning_exclamation.png b/android/sdremote/res/drawable/warning_exclamation.png new file mode 100644 index 000000000000..76b2f2d46e27 Binary files /dev/null and b/android/sdremote/res/drawable/warning_exclamation.png differ diff --git a/android/sdremote/res/layout/activity_reconnect.xml b/android/sdremote/res/layout/activity_reconnect.xml new file mode 100644 index 000000000000..8baa76d1a73f --- /dev/null +++ b/android/sdremote/res/layout/activity_reconnect.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/sdremote/res/values/strings.xml b/android/sdremote/res/values/strings.xml index 61a340428ac2..811dac4185e0 100644 --- a/android/sdremote/res/values/strings.xml +++ b/android/sdremote/res/values/strings.xml @@ -39,5 +39,6 @@ Remember this server next time Add Cancel - + Your connection has been dropped. +Attempting to reconnect… \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java index 9880b8f8964e..4e9ff2a0f435 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java @@ -10,13 +10,11 @@ package org.libreoffice.impressremote.communication; import java.io.BufferedReader; import java.io.InputStreamReader; -import java.net.Socket; import java.util.UUID; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; -import android.content.Context; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; @@ -27,17 +25,13 @@ import android.support.v4.content.LocalBroadcastManager; */ public class BluetoothClient extends Client { - private static final int PORT = 5; - - private Socket mSocket; - - public BluetoothClient(String bluetoothAddress, Context aContext) { - super(aContext); + public BluetoothClient(Server aServer, + CommunicationService aCommunicationService) { + super(aServer, aCommunicationService); try { BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); - System.out.println("Attemtping to connect to:" + bluetoothAddress); - BluetoothDevice aDevice = aAdapter - .getRemoteDevice(bluetoothAddress); + BluetoothDevice aDevice = aAdapter.getRemoteDevice(aServer + .getAddress()); aAdapter.cancelDiscovery(); BluetoothSocket aSocket = aDevice .createRfcommSocketToServiceRecord(UUID @@ -69,7 +63,8 @@ public class BluetoothClient extends Client { } Intent aIntent = new Intent( CommunicationService.MSG_PAIRING_SUCCESSFUL); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); + LocalBroadcastManager.getInstance(mCommunicationService) + .sendBroadcast(aIntent); startListening(); // Pairing. // Random aRandom = new Random(); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java index 81c889333e29..964b62ce409d 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java @@ -15,7 +15,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; -import android.content.Context; +import android.content.Intent; /** * Generic Client for the remote control. To implement a Client for a specific @@ -40,10 +40,13 @@ public abstract class Client { private Receiver mReceiver; - protected Context mContext; + protected Server mServer; - public Client(Context aContext) { - mContext = aContext; + protected CommunicationService mCommunicationService; + + protected Client(Server aServer, CommunicationService aCommunicationService) { + mServer = aServer; + mCommunicationService = aCommunicationService; latestInstance = this; } @@ -62,25 +65,37 @@ public abstract class Client { t.start(); } - private void listen() { + private final void listen() { try { while (true) { ArrayList aList = new ArrayList(); String aTemp; // read until empty line - while ((aTemp = mReader.readLine()).length() != 0) { + while ((aTemp = mReader.readLine()) != null + && aTemp.length() != 0) { aList.add(aTemp); } + if (aTemp == null) { + mCommunicationService.connectTo(mServer); + Intent aIntent = new Intent( + mCommunicationService + .getApplicationContext(), + ReconnectionActivity.class); + aIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + mCommunicationService.getApplicationContext() + .startActivity(aIntent); + return; + } mReceiver.parseCommand(aList); } } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e1) { // TODO stream couldn't be opened. e1.printStackTrace(); + } finally { + latestInstance = null; } - latestInstance = null; } @@ -105,7 +120,8 @@ public abstract class Client { throw new Error("Specified network encoding [" + CHARSET + " not available."); } catch (IOException e) { - // TODO Notify that stream has closed. + // I.e. connection closed. This will be dealt with by the listening + // loop. } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 214103a0a993..1d07a29f621a 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -89,8 +89,7 @@ public class CommunicationService extends Service implements Runnable { mClient = new NetworkClient(mServerDesired, this); break; case BLUETOOTH: - mClient = new BluetoothClient( - mServerDesired.getAddress(), this); + mClient = new BluetoothClient(mServerDesired, this); break; } mTransmitter = new Transmitter(mClient); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java index dcb88a56f877..91b169d1a5dd 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/NetworkClient.java @@ -15,7 +15,6 @@ import java.net.Socket; import java.net.UnknownHostException; import java.util.Random; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -32,8 +31,9 @@ public class NetworkClient extends Client { private Socket mSocket; - public NetworkClient(Server aServer, Context aContext) { - super(aContext); + public NetworkClient(Server aServer, + CommunicationService aCommunicationService) { + super(aServer, aCommunicationService); try { mSocket = new Socket(aServer.getAddress(), PORT); mInputStream = mSocket.getInputStream(); @@ -46,7 +46,8 @@ public class NetworkClient extends Client { CommunicationService.MSG_PAIRING_STARTED); aIntent.putExtra("PIN", aPin); mPin = aPin; - LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); + LocalBroadcastManager.getInstance(mCommunicationService) + .sendBroadcast(aIntent); // Send out String aName = CommunicationService.getDeviceName(); // TODO: get the proper name sendCommand("LO_SERVER_CLIENT_PAIR\n" + aName + "\n" + aPin @@ -61,8 +62,8 @@ public class NetworkClient extends Client { } else { aIntent = new Intent( CommunicationService.MSG_PAIRING_SUCCESSFUL); - LocalBroadcastManager.getInstance(mContext).sendBroadcast( - aIntent); + LocalBroadcastManager.getInstance(mCommunicationService) + .sendBroadcast(aIntent); } while (mReader.readLine().length() != 0) { // Get rid of extra lines @@ -82,9 +83,9 @@ public class NetworkClient extends Client { private String setupPin(Server aServer) { // Get settings - SharedPreferences aPreferences = mContext.getSharedPreferences( - "sdremote_authorisedremotes", - android.content.Context.MODE_PRIVATE); + SharedPreferences aPreferences = mCommunicationService + .getSharedPreferences("sdremote_authorisedremotes", + android.content.Context.MODE_PRIVATE); if (aPreferences.contains(aServer.getName())) { return aPreferences.getString(aServer.getName(), ""); } else { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ReconnectionActivity.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ReconnectionActivity.java new file mode 100644 index 000000000000..ecc579ef468b --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ReconnectionActivity.java @@ -0,0 +1,18 @@ +package org.libreoffice.impressremote.communication; + +import org.libreoffice.impressremote.R; + +import android.os.Bundle; + +import com.actionbarsherlock.app.SherlockActivity; + +public class ReconnectionActivity extends SherlockActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + // TODO Auto-generated method stub + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_reconnect); + } + +} -- cgit