fdo#60486 Fix auto-enabling bluetooth and improve bluetooth handling.

We should only enable bluetooth with explicit approval of the user, see:
http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable%28%29

We now also display an appropriate message if bluetooth is disabled.

Change-Id: Ic3a07c9ad0806a60ac7c7e609a30add7af18916f
This commit is contained in:
Andrzej Hunt 2013-12-10 19:52:33 +00:00
parent a5ad6e750b
commit 3cc31f8978
6 changed files with 50 additions and 14 deletions

View File

@ -90,5 +90,6 @@
<string name="requirements_network_connection">The Android device and a computer connected to the same network.</string>
<string name="requirements_network_ports">If you have a firewall make sure ports 1598 and 1599 are opened.</string>
<string name="requirements_bluetooth_connection">A computer with enabled Bluetooth.</string>
<string name="message_bluetooth_disabled">Please enable bluetooth to connect to a bluetooth enabled computer.</string>
</resources>

View File

@ -31,6 +31,8 @@ import org.libreoffice.impressremote.util.SavedStates;
public class ComputersActivity extends SherlockFragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private boolean mBluetoothWasEnabled;
private final static int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
@ -42,6 +44,16 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio
setUpContent();
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
if (arg0 == REQUEST_ENABLE_BT) {
// Ideally we should do all detection based on listening to the bluetooth state
// as the user can still enable BT separately (see BluetoothServersFinder.java:onReceive)
}
}
private void saveBluetoothState(Bundle aSavedInstanceState) {
// In more ideal world this work should be done at the service.
// Unfortunately service cannot save or restore its state.
@ -64,7 +76,7 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio
}
private void enableBluetooth() {
BluetoothOperator.enable();
BluetoothOperator.enable(this, REQUEST_ENABLE_BT);
}
private void setUpTitle() {

View File

@ -72,9 +72,19 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder,
public void onReceive(Context aContext, Intent aIntent) {
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) {
switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
case BluetoothAdapter.STATE_ON:
case BluetoothAdapter.STATE_ON: {
BluetoothOperator.getAdapter().startDiscovery();
Intent aNewIntent = Intents.buildBluetoothStateChangedIntent();
LocalBroadcastManager.getInstance(mContext).sendBroadcast(aNewIntent);
return;
}
case BluetoothAdapter.STATE_OFF: {
mServers.clear();
Intent aNewIntent = Intents.buildBluetoothStateChangedIntent();
LocalBroadcastManager.getInstance(mContext).sendBroadcast(aNewIntent);
return;
}
default:
return;

View File

@ -13,6 +13,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@ -55,6 +56,8 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
WIFI, BLUETOOTH
}
boolean mBluetoothDisabled = false;
private CommunicationService mCommunicationService;
private BroadcastReceiver mIntentsReceiver;
@ -148,7 +151,10 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
return getString(R.string.message_search_wifi);
case BLUETOOTH:
return getString(R.string.message_search_bluetooth);
if (mBluetoothDisabled != true)
return getString(R.string.message_search_bluetooth);
else
return getString(R.string.message_bluetooth_disabled);
default:
return "";
@ -210,6 +216,8 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
}
}
mBluetoothDisabled = !BluetoothAdapter.getDefaultAdapter().isEnabled();
return aComputers;
}
@ -251,18 +259,10 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
return;
}
if (!isShowingProgressMessageRequired()) {
return;
}
showProgressMessage();
showLearnMoreMessage();
}
private boolean isShowingProgressMessageRequired() {
return getProgressMessageView().getVisibility() == View.INVISIBLE;
}
private void tearDownComputersAdapter() {
setListAdapter(null);
}
@ -330,6 +330,8 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
public void onReceive(Context aContext, Intent aIntent) {
if (Intents.Actions.SERVERS_LIST_CHANGED.equals(aIntent.getAction())) {
mComputersFragment.loadComputers();
} else if (Intents.Actions.BLUETOOTH_STATE_CHANGED.equals(aIntent.getAction())) {
mComputersFragment.loadComputers();
}
}
}
@ -337,7 +339,7 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
private IntentFilter buildIntentsReceiverFilter() {
IntentFilter aIntentFilter = new IntentFilter();
aIntentFilter.addAction(Intents.Actions.SERVERS_LIST_CHANGED);
aIntentFilter.addAction(Intents.Actions.BLUETOOTH_STATE_CHANGED);
return aIntentFilter;
}

View File

@ -8,7 +8,9 @@
*/
package org.libreoffice.impressremote.util;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
public final class BluetoothOperator {
private BluetoothOperator() {
@ -26,12 +28,16 @@ public final class BluetoothOperator {
return BluetoothAdapter.getDefaultAdapter();
}
public static void enable() {
public static void enable(Activity aActivity, int nRequestCode) {
if (!isAvailable()) {
return;
}
getAdapter().enable();
if (getAdapter() != null) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
aActivity.startActivityForResult(enableBtIntent, nRequestCode);
}
}
public static void disable() {

View File

@ -29,6 +29,7 @@ public final class Intents {
}
public static final String SERVERS_LIST_CHANGED = "SERVERS_LIST_CHANGED";
public static final String BLUETOOTH_STATE_CHANGED = "BLUETOOTH_STATE_CHANGED";
public static final String PAIRING_SUCCESSFUL = "PAIRING_SUCCESSFUL";
public static final String PAIRING_VALIDATION = "PAIRING_VALIDATION";
@ -74,6 +75,10 @@ public final class Intents {
return new Intent(Actions.SERVERS_LIST_CHANGED);
}
public static Intent buildBluetoothStateChangedIntent() {
return new Intent(Actions.BLUETOOTH_STATE_CHANGED);
}
public static Intent buildPairingSuccessfulIntent() {
return new Intent(Actions.PAIRING_SUCCESSFUL);
}