Kaydet (Commit) 13cf8f25 authored tarafından Artur Dryomov's avatar Artur Dryomov

Change way of saving Bluetooth state one more time.

Read a comment at the ComputersActivity.

Change-Id: I4a933d262c28a08c1e2227a2eabec54ad2cfd16e
üst afa32202
......@@ -26,6 +26,7 @@ import org.libreoffice.impressremote.util.Fragments;
import org.libreoffice.impressremote.util.Intents;
import org.libreoffice.impressremote.R;
import org.libreoffice.impressremote.util.Preferences;
import org.libreoffice.impressremote.util.SavedStates;
public class ComputersActivity extends SherlockFragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private static final class TabsIndices {
......@@ -36,14 +37,44 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio
public static final int WIFI = 1;
}
private boolean mBluetoothWasEnabled;
@Override
protected void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
saveBluetoothState(aSavedInstanceState);
enableBluetooth();
setUpTitle();
setUpContent();
}
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
// but enabling or disabling Bluetooth is quite a long operation,
// so we have more chances to manage state right at the activity.
if (!BluetoothOperator.isAvailable()) {
return;
}
mBluetoothWasEnabled = wasBluetoothEnabled(aSavedInstanceState);
}
private boolean wasBluetoothEnabled(Bundle aSavedInstanceState) {
if (aSavedInstanceState == null) {
return BluetoothOperator.getAdapter().isEnabled();
}
return aSavedInstanceState.getBoolean(SavedStates.Keys.BLUETOOTH_ENABLED);
}
private void enableBluetooth() {
BluetoothOperator.enable();
}
private void setUpTitle() {
// Looks hacky but it seems to be the best way to set activity’s title
// different to application’s label. The other way is setting title
......@@ -234,6 +265,40 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio
aPreferences.setInt(Preferences.Keys.SELECTED_COMPUTERS_TAB_INDEX, aTabIndex);
}
@Override
protected void onSaveInstanceState(Bundle aSavedInstanceState) {
super.onSaveInstanceState(aSavedInstanceState);
rememberBluetoothState(aSavedInstanceState);
}
private void rememberBluetoothState(Bundle aSavedInstanceState) {
aSavedInstanceState.putBoolean(SavedStates.Keys.BLUETOOTH_ENABLED, mBluetoothWasEnabled);
}
@Override
protected void onDestroy() {
super.onDestroy();
restoreBluetoothState();
}
private void restoreBluetoothState() {
if (!BluetoothOperator.isAvailable()) {
return;
}
if (mBluetoothWasEnabled) {
return;
}
disableBluetooth();
}
private void disableBluetooth() {
BluetoothOperator.disable();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -36,8 +36,6 @@ public class CommunicationService extends Service implements Runnable, MessagesL
private ServersManager mServersManager;
private BluetoothOperator.State mBluetoothState;
private Timer mTimer;
private SlideShow mSlideShow;
......@@ -53,21 +51,10 @@ public class CommunicationService extends Service implements Runnable, MessagesL
mServersManager = new ServersManager(this);
saveBluetoothState();
enableBluetooth();
mTimer = new Timer(this);
mSlideShow = new SlideShow(mTimer);
}
private void saveBluetoothState() {
mBluetoothState = BluetoothOperator.getState();
}
private void enableBluetooth() {
BluetoothOperator.enable();
}
@Override
public IBinder onBind(Intent aIntent) {
return mBinder;
......@@ -237,24 +224,6 @@ public class CommunicationService extends Service implements Runnable, MessagesL
public void onDestroy() {
stopServersSearch();
disconnectServer();
restoreBluetoothState();
}
private void restoreBluetoothState() {
if (!BluetoothOperator.isStateValid(mBluetoothState)) {
return;
}
if (mBluetoothState.wasBluetoothEnabled()) {
return;
}
disableBluetooth();
}
private void disableBluetooth() {
BluetoothOperator.disable();
}
}
......
......@@ -11,18 +11,6 @@ package org.libreoffice.impressremote.util;
import android.bluetooth.BluetoothAdapter;
public final class BluetoothOperator {
public static final class State {
private final boolean mWasBluetoothEnabled;
private State(boolean aIsBluetoothEnabled) {
mWasBluetoothEnabled = aIsBluetoothEnabled;
}
public boolean wasBluetoothEnabled() {
return mWasBluetoothEnabled;
}
}
private BluetoothOperator() {
}
......@@ -38,18 +26,6 @@ public final class BluetoothOperator {
return BluetoothAdapter.getDefaultAdapter();
}
public static State getState() {
if (!isAvailable()) {
return null;
}
return new State(getAdapter().isEnabled());
}
public static boolean isStateValid(State aState) {
return aState != null;
}
public static void enable() {
if (!isAvailable()) {
return;
......
......@@ -16,6 +16,7 @@ public final class SavedStates {
private Keys() {
}
public static final String BLUETOOTH_ENABLED ="BLUETOOTH_ENABLED";
public static final String CURRENT_VIEW_ID = "CURRENT_VIEW_ID";
public static final String ERROR_MESSAGE = "ERROR_MESSAGE";
public static final String MODE = "MODE";
......
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