From 13cf8f2599a9888f2a6f1b9dd8938978b4a0274f Mon Sep 17 00:00:00 2001 From: Artur Dryomov Date: Sat, 21 Sep 2013 22:17:34 +0300 Subject: Change way of saving Bluetooth state one more time. Read a comment at the ComputersActivity. Change-Id: I4a933d262c28a08c1e2227a2eabec54ad2cfd16e --- .../impressremote/activity/ComputersActivity.java | 65 ++++++++++++++++++++++ .../communication/CommunicationService.java | 31 ----------- .../impressremote/util/BluetoothOperator.java | 24 -------- .../impressremote/util/SavedStates.java | 1 + 4 files changed, 66 insertions(+), 55 deletions(-) (limited to 'android/sdremote') diff --git a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java index 7b085e75ab3c..7ccd18ef7896 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java @@ -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: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 0db8b4166a52..a8aa85f48970 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -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(); } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java index 56b05aa5af43..95228388d501 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java @@ -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; diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/SavedStates.java b/android/sdremote/src/org/libreoffice/impressremote/util/SavedStates.java index 682632c307ed..7bc5dad6a9b0 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/SavedStates.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/SavedStates.java @@ -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"; -- cgit