diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-03 11:32:22 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-03 11:34:46 +0200 |
commit | d6dceb2bb0bf30e5a9362705156afd5339863db8 (patch) | |
tree | ecaf1530455a6e45ce56ae9eb9b81d3eda818033 /android | |
parent | 51f9e894f46e718200a14bcd61e9e44c64bc5396 (diff) |
Implemented automatic reconnection + reconnection activity.
Change-Id: I445fe2acb24ab6992aad4c75f6886f517bdcc0b0
Diffstat (limited to 'android')
8 files changed, 109 insertions, 33 deletions
diff --git a/android/sdremote/res/drawable/warning_exclamation.png b/android/sdremote/res/drawable/warning_exclamation.png Binary files differnew file mode 100644 index 000000000000..76b2f2d46e27 --- /dev/null +++ b/android/sdremote/res/drawable/warning_exclamation.png 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" > + + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerHorizontal="true" + android:layout_centerVertical="true" + android:orientation="vertical" > + + <ImageView + android:id="@+id/imageView1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_horizontal" + android:src="@drawable/warning_exclamation" + android:layout_marginBottom="50dp" /> + + <TextView + android:id="@+id/textView1" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_horizontal" + android:layout_marginLeft="10dp" + android:layout_marginRight="10dp" + android:gravity="center_horizontal" + android:text="@string/reconnect_description1" + android:textAppearance="?android:attr/textAppearanceMedium" /> + + <TextView + android:id="@+id/textView2" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_horizontal" + android:layout_marginLeft="10dp" + android:layout_marginRight="10dp" + android:gravity="center_horizontal" + android:text="@string/reconnect_description2" + android:textAppearance="?android:attr/textAppearanceMedium" /> + + </LinearLayout> + +</RelativeLayout>
\ 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 @@ <string name="addserver_remember">Remember this server next time</string> <string name="addserver_add">Add</string> <string name="addserver_cancel">Cancel</string> - + <string name="reconnect_description1">Your connection has been dropped.</string> +<string name="reconnect_description2">Attempting to reconnect…</string> </resources>
\ 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<String> aList = new ArrayList<String>(); 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); + } + +} |