fdo#61873 - add WiFi experimental feature alert.

fixes reconnect crash. Won't crash when server-end disconnect.

seperate Client construction and initialization so that Client()
will release its mutex lock and won't block service.run().
Otherwise onBackPressed() will be blocked in PairingActivity

Change-Id: I424a470aa02b0c74b28cb9f9ba79489aa0d4ab1b
This commit is contained in:
Siqi LIU (via Code Review) 2013-04-12 23:49:21 +00:00 committed by Michael Meeks
parent e5fb76a402
commit 38cc47f8b1
13 changed files with 360 additions and 101 deletions

View File

@ -57,6 +57,11 @@
android:icon="@drawable/icon_options"
android:label="@string/options" >
</activity>
<activity
android:name=".communication.ReconnectionActivity"
android:logo="@drawable/actionbar_icon_computer"
android:label="@string/reconnect" >
</activity>
</application>
</manifest>

View File

@ -41,6 +41,16 @@
android:text="@string/reconnect_description2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/countDownTV"
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:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>

View File

@ -3,8 +3,18 @@
android:id="@+id/addserver_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp"
android:orientation="vertical" >
<TextView
android:id="@+id/wifiAlertMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/wifiAlertMsg"
android:textStyle="italic"
android:typeface="serif" />
<TextView
android:id="@+id/addserver_namelabel"
android:layout_width="wrap_content"

View File

@ -7,6 +7,7 @@
<string name="presentation_ui_resizehandle">Handle to resize view.</string>
<string name="presentation_blank_screen">Blank Screen</string>
<string name="options">Options</string>
<string name="reconnect">Reconnect...</string>
<string name="actionbar_timeformat">h:mmaa</string>
<string name="actionbar_timerformat">mm:ss</string>
<string name="clock_timer_start">Start</string>
@ -15,6 +16,8 @@
<string name="clock_timer_reset">Reset</string>
<string name="clock_timer_resume">Resume</string>
<string name="options_autodecline">Decline Calls</string>
<string name="help">Help</string>
<string name="ConnectionFailedHelp">#1 Verify Impress is running \n#2 For Bluetooth user, enable \"Preference\"-\"LibreOffice Impress\"-\"General\"-\"Enable remote control\"\n#3 For WiFi user, tick \"Preferece\"-\"LibreOffice\"-\"Advanced\"-\"Enable Experimental Features\" \n </string>
<string name="options_description">Automatically decline all incoming calls.</string>
<string name="options_volumeswitching">Volume Switching</string>
<string name="options_volumeswitching_descripton">Change slides using volume buttons</string>
@ -49,6 +52,7 @@
<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>
<string name="reconnect_description2">Please try to reconnect</string>
<string name="wifiAlertMsg">This is still an experimental feature. You need to \"enable experimental features\" in \"Preference\"-\"LibreOffice\"-\"Advanced\" on your computer. \nThe use over Bluetooth is recommanded.</string>
</resources>

View File

@ -10,18 +10,25 @@ package org.libreoffice.impressremote;
import java.text.MessageFormat;
import org.libreoffice.impressremote.communication.CommunicationService;
import org.libreoffice.impressremote.communication.CommunicationService.State;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
public class PairingActivity extends SherlockActivity {
private ActivityChangeBroadcastProcessor mBroadcastProcessor;
private CommunicationService mCommunicationService;
/** Called when the activity is first created. */
@Override
@ -29,8 +36,8 @@ public class PairingActivity extends SherlockActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pairing);
mBroadcastProcessor = new ActivityChangeBroadcastProcessor(this);
bindService(new Intent(this, CommunicationService.class), mConnection,
Context.BIND_IMPORTANT);
IntentFilter aFilter = new IntentFilter();
@ -55,6 +62,22 @@ public class PairingActivity extends SherlockActivity {
getSupportActionBar().setTitle(aServerName);
}
@Override
public void onPause(){
super.onPause();
unbindService(mConnection);
}
@Override
public void onBackPressed() {
mCommunicationService.getClient().closeConnection();
Intent aIntent = new Intent(this, SelectorActivity.class);
aIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aIntent);
}
private BroadcastReceiver mListener = new BroadcastReceiver() {
@Override
@ -63,6 +86,32 @@ public class PairingActivity extends SherlockActivity {
}
};
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName aClassName,
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
}
@Override
public void onServiceDisconnected(ComponentName aClassName) {
mCommunicationService = null;
}
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -63,6 +63,8 @@ public class SelectorActivity extends SherlockActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selector);
if (mCommunicationService != null)
mCommunicationService.disconnect();
IntentFilter aFilter = new IntentFilter(
CommunicationService.MSG_SERVERLIST_CHANGED);
aFilter.addAction(CommunicationService.STATUS_CONNECTION_FAILED);
@ -223,15 +225,26 @@ public class SelectorActivity extends SherlockActivity {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
if (mCommunicationService != null) {
String aFormat = getResources().getString(
R.string.selector_dialog_connectionfailed);
String aDialogText = MessageFormat.format(aFormat,
mCommunicationService
.getPairingDeviceName());
mCommunicationService.getPairingDeviceName());
AlertDialog.Builder builder = new AlertDialog.Builder(
SelectorActivity.this);
builder.setMessage(aDialogText)
.setCancelable(false)
.setNeutralButton(R.string.help,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(
SelectorActivity.this);
builder.setMessage(
R.string.ConnectionFailedHelp)
.setCancelable(false)
.setPositiveButton(
R.string.selector_dialog_connectionfailed_ok,
@ -244,6 +257,19 @@ public class SelectorActivity extends SherlockActivity {
});
builder.show();
}
})
.setPositiveButton(
R.string.selector_dialog_connectionfailed_ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.dismiss();
}
});
builder.show();
}
}
}
mBroadcastProcessor.onReceive(aContext, aIntent);

View File

@ -10,6 +10,7 @@ package org.libreoffice.impressremote;
import org.libreoffice.impressremote.communication.CommunicationService;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@ -18,6 +19,7 @@ import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
@ -37,6 +39,7 @@ public class StartPresentationActivity extends SherlockActivity {
bindService(new Intent(this, CommunicationService.class), mConnection,
Context.BIND_IMPORTANT);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
IntentFilter aFilter = new IntentFilter(
CommunicationService.MSG_SLIDESHOW_STARTED);
@ -97,6 +100,18 @@ public class StartPresentationActivity extends SherlockActivity {
mBroadcastProcessor.onReceive(aContext, aIntent);
}
};
@Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -58,6 +58,27 @@ public class BluetoothClient extends Client {
mSocket.connect();
Log.i(Globals.TAG, "BluetoothClient: connected");
}
@Override
public void closeConnection() {
try {
if (mSocket != null)
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onDisconnect() {
if (!mBluetoothWasEnabled) {
mAdapter.disable();
}
}
@Override
public void validating() throws IOException {
// TODO Auto-generated method stub
mInputStream = mSocket.getInputStream();
mReader = new BufferedReader(new InputStreamReader(mInputStream,
@ -80,22 +101,6 @@ public class BluetoothClient extends Client {
}
@Override
public void closeConnection() {
try {
if (mSocket != null)
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onDisconnect() {
if (!mBluetoothWasEnabled) {
mAdapter.disable();
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -15,7 +15,11 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.libreoffice.impressremote.Globals;
import org.libreoffice.impressremote.communication.CommunicationService.State;
import android.content.Intent;
import android.util.Log;
/**
* Generic Client for the remote control. To implement a Client for a specific
@ -37,6 +41,7 @@ public abstract class Client {
public abstract void closeConnection();
public abstract void validating() throws IOException;
private Receiver mReceiver;
protected Server mServer;
@ -75,11 +80,11 @@ public abstract class Client {
aList.add(aTemp);
}
if (aTemp == null) {
mCommunicationService.connectTo(mServer);
Intent aIntent = new Intent(
mCommunicationService
.getApplicationContext(),
ReconnectionActivity.class);
aIntent.putExtra("server", mServer);
aIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mCommunicationService.getApplicationContext()
.startActivity(aIntent);

View File

@ -102,28 +102,34 @@ public class CommunicationService extends Service implements Runnable {
case NETWORK:
mClient = new NetworkClient(mServerDesired,
this, mReceiver);
mClient.validating();
break;
case BLUETOOTH:
mClient = new BluetoothClient(mServerDesired,
this, mReceiver,
mBluetoothPreviouslyEnabled);
mClient.validating();
break;
}
mTransmitter = new Transmitter(mClient);
mState = State.CONNECTED;
} catch (IOException e) {
Log.i(Globals.TAG, "CommunicationService.run: " + e);
connextionFailed();
}
}
}
Log.i(Globals.TAG, "CommunicationService.finished work");
}
}
}
private void connextionFailed() {
mClient = null;
mState = State.DISCONNECTED;
Intent aIntent = new Intent(
CommunicationService.STATUS_CONNECTION_FAILED);
LocalBroadcastManager.getInstance(this)
.sendBroadcast(aIntent);
}
}
}
}
}
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
}
private boolean mBluetoothPreviouslyEnabled;
@ -174,6 +180,7 @@ public class CommunicationService extends Service implements Runnable {
}
public void disconnect() {
Log.d(Globals.TAG, "Service Disconnected");
synchronized (mConnectionVariableMutex) {
mStateDesired = State.DISCONNECTED;
synchronized (this) {
@ -328,6 +335,10 @@ public class CommunicationService extends Service implements Runnable {
}
public Client getClient() {
return mClient;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -31,66 +31,31 @@ public class NetworkClient extends Client {
private static final int PORT = 1599;
private Socket mSocket;
private Intent mIntent;
private String mPin;
private Server mServer;
public NetworkClient(Server aServer,
CommunicationService aCommunicationService,
Receiver aReceiver) throws UnknownHostException,
IOException {
CommunicationService aCommunicationService, Receiver aReceiver)
throws UnknownHostException, IOException {
super(aServer, aCommunicationService, aReceiver);
mSocket = new Socket(aServer.getAddress(), PORT);
mServer = aServer;
mSocket = new Socket(mServer.getAddress(), PORT);
mInputStream = mSocket.getInputStream();
mReader = new BufferedReader(new InputStreamReader(mInputStream,
CHARSET));
mOutputStream = mSocket.getOutputStream();
// Pairing.
String aPin = setupPin(aServer);
Intent aIntent = new Intent(CommunicationService.MSG_PAIRING_STARTED);
aIntent.putExtra("PIN", aPin);
mPin = aPin;
mPin = setupPin(mServer);
mIntent = new Intent(CommunicationService.MSG_PAIRING_STARTED);
mIntent.putExtra("PIN", mPin);
LocalBroadcastManager.getInstance(mCommunicationService).sendBroadcast(
aIntent);
mIntent);
// Send out
String aName = CommunicationService.getDeviceName(); // TODO: get the proper name
sendCommand("LO_SERVER_CLIENT_PAIR\n" + aName + "\n" + aPin + "\n\n");
// Wait until we get the appropriate string back...
String aTemp = mReader.readLine();
if (aTemp == null) {
throw new IOException(
"End of stream reached before any data received.");
}
while (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) {
if (aTemp.equals("LO_SERVER_VALIDATING_PIN")) {
// Broadcast that we need a pin screen.
aIntent = new Intent(
CommunicationService.STATUS_PAIRING_PINVALIDATION);
aIntent.putExtra("PIN", aPin);
aIntent.putExtra("SERVERNAME", aServer.getName());
mPin = aPin;
LocalBroadcastManager.getInstance(mCommunicationService)
.sendBroadcast(aIntent);
while (mReader.readLine().length() != 0) {
// Read off empty lines
}
aTemp = mReader.readLine();
} else {
return;
}
}
aIntent = new Intent(CommunicationService.MSG_PAIRING_SUCCESSFUL);
LocalBroadcastManager.getInstance(mCommunicationService).sendBroadcast(
aIntent);
while (mReader.readLine().length() != 0) {
// Get rid of extra lines
Log.i(Globals.TAG, "NetworkClient: extra line");
}
Log.i(Globals.TAG, "NetworkClient: calling startListening");
startListening();
String aName = CommunicationService.getDeviceName(); // TODO: get the
// proper name
sendCommand("LO_SERVER_CLIENT_PAIR\n" + aName + "\n" + mPin + "\n\n");
}
private String setupPin(Server aServer) {
@ -132,6 +97,46 @@ public class NetworkClient extends Client {
}
}
@Override
public void validating() throws IOException {
// Wait until we get the appropriate string back...
String aTemp = mReader.readLine();
if (aTemp == null) {
throw new IOException(
"End of stream reached before any data received.");
}
while (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) {
if (aTemp.equals("LO_SERVER_VALIDATING_PIN")) {
// Broadcast that we need a pin screen.
mIntent = new Intent(
CommunicationService.STATUS_PAIRING_PINVALIDATION);
mIntent.putExtra("PIN", mPin);
mIntent.putExtra("SERVERNAME", mServer.getName());
LocalBroadcastManager.getInstance(mCommunicationService)
.sendBroadcast(mIntent);
while (mReader.readLine().length() != 0) {
// Read off empty lines
}
aTemp = mReader.readLine();
} else {
return;
}
}
mIntent = new Intent(CommunicationService.MSG_PAIRING_SUCCESSFUL);
LocalBroadcastManager.getInstance(mCommunicationService).sendBroadcast(
mIntent);
while (mReader.readLine().length() != 0) {
// Get rid of extra lines
Log.i(Globals.TAG, "NetworkClient: extra line");
}
Log.i(Globals.TAG, "NetworkClient: calling startListening");
startListening();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -8,18 +8,99 @@
*/
package org.libreoffice.impressremote.communication;
import org.libreoffice.impressremote.ActivityChangeBroadcastProcessor;
import org.libreoffice.impressremote.R;
import org.libreoffice.impressremote.SelectorActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
public class ReconnectionActivity extends SherlockActivity {
private ActivityChangeBroadcastProcessor mBroadcastProcessor;
private CommunicationService mCommunicationService;
// private TextView mCountDownTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reconnect);
// mCountDownTextView = (TextView) findViewById(R.id.countDownTV);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
bindService(new Intent(this, CommunicationService.class), mConnection,
Context.BIND_IMPORTANT);
final Server desiredServer = getIntent().getParcelableExtra("server");
IntentFilter aFilter = new IntentFilter();
mBroadcastProcessor = new ActivityChangeBroadcastProcessor(this);
mBroadcastProcessor.addToFilter(aFilter);
LocalBroadcastManager.getInstance(this).registerReceiver(mListener,
aFilter);
getSupportActionBar().setTitle(desiredServer.getName());
// TODO Connection to desired server
// Create a countdown clock for 10 seconds, then double the delay
// with every failure. Until it reaches 1min. Like Gmail retry
}
private BroadcastReceiver mListener = new BroadcastReceiver() {
@Override
public void onReceive(Context aContext, Intent aIntent) {
mBroadcastProcessor.onReceive(aContext, aIntent);
}
};
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName aClassName,
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
}
@Override
public void onServiceDisconnected(ComponentName aClassName) {
mCommunicationService = null;
}
};
@Override
public void onBackPressed() {
Intent aIntent = new Intent(this, SelectorActivity.class);
aIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aIntent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -8,7 +8,10 @@
*/
package org.libreoffice.impressremote.communication;
public class Server {
import android.os.Parcel;
import android.os.Parcelable;
public class Server implements Parcelable {
public enum Protocol {
NETWORK, BLUETOOTH
@ -56,6 +59,36 @@ public class Server {
public String toString() {
return getClass().getName() + '@' + Integer.toHexString(hashCode()) + ":{" + mAddress + "," + mName + "}";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mAddress);
out.writeString(mName);
out.writeString(mProtocol.name());
out.writeLong(mTimeDiscovered);
}
public static final Parcelable.Creator<Server> CREATOR = new Parcelable.Creator<Server>() {
public Server createFromParcel(Parcel in) {
return new Server(in);
}
public Server[] newArray(int size) {
return new Server[size];
}
};
private Server(Parcel in) {
mAddress = in.readString();
mName = in.readString();
mProtocol = Protocol.valueOf(in.readString());
mTimeDiscovered = in.readLong();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */