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