Kaydet (Commit) d6dceb2b authored tarafından Andrzej J.R. Hunt's avatar Andrzej J.R. Hunt

Implemented automatic reconnection + reconnection activity.

Change-Id: I445fe2acb24ab6992aad4c75f6886f517bdcc0b0
üst 51f9e894
<?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
......@@ -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
......@@ -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();
......
......@@ -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.
}
}
......
......@@ -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);
......
......@@ -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 {
......
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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment