From 21e5de7d5630605735f4572369e37d7bab0a5522 Mon Sep 17 00:00:00 2001 From: Artur Dryomov Date: Sun, 8 Sep 2013 16:20:04 +0300 Subject: Fix servers search behaviour. * Start search and stop it during the lifecycle. Before this change searching was a constant process draining battery. * Enable and disable Bluetooth according to the service lifecycle. This should help to avoid race conditions and disable or enable Bluetooth better way. Change-Id: I02ef1bb67d9fb6fd56d0aff0a176cdb41284a49f --- .../communication/CommunicationService.java | 43 +++-- .../impressremote/fragment/ComputersFragment.java | 213 +++++++++++---------- .../impressremote/util/BluetoothOperator.java | 32 ++++ .../libreoffice/impressremote/util/Fragments.java | 2 +- 4 files changed, 171 insertions(+), 119 deletions(-) (limited to 'android/sdremote') diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index e6dd88a9c85b..f2ead2868a4e 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -43,7 +43,7 @@ public class CommunicationService extends Service implements Runnable, MessagesL private MessagesReceiver mMessagesReceiver; private CommandsTransmitter mCommandsTransmitter; - private boolean mBluetoothWasEnabled; + private BluetoothOperator.State mBluetoothState; private Timer mTimer; private SlideShow mSlideShow; @@ -61,7 +61,8 @@ public class CommunicationService extends Service implements Runnable, MessagesL mServersManager = new ServersManager(this); - mBluetoothWasEnabled = false; + saveBluetoothState(); + enableBluetooth(); mTimer = new Timer(this); mSlideShow = new SlideShow(mTimer); @@ -76,6 +77,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL } } + private void saveBluetoothState() { + mBluetoothState = BluetoothOperator.getState(); + } + + private void enableBluetooth() { + BluetoothOperator.enable(); + } + @Override public IBinder onBind(Intent intent) { return mBinder; @@ -164,28 +173,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent); } - public void startSearch() { + public void startServersSearch() { mState = State.SEARCHING; - if (BluetoothOperator.isAvailable()) { - mBluetoothWasEnabled = BluetoothOperator.getAdapter().isEnabled(); - - if (!BluetoothOperator.getAdapter().isEnabled()) { - BluetoothOperator.getAdapter().enable(); - } - } - mServersManager.startServersSearch(); } - public void stopSearch() { + public void stopServersSearch() { mServersManager.stopServersSearch(); - - if (BluetoothOperator.isAvailable()) { - if (!mBluetoothWasEnabled) { - BluetoothOperator.getAdapter().disable(); - } - } } public List getServers() { @@ -298,11 +293,21 @@ public class CommunicationService extends Service implements Runnable, MessagesL @Override public void onDestroy() { - stopSearch(); + stopServersSearch(); + + restoreBluetoothState(); mThread.interrupt(); mThread = null; } + + private void restoreBluetoothState() { + if (mBluetoothState.wasBluetoothEnabled()) { + return; + } + + BluetoothOperator.disable(); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java index b6d2b95eac0c..d24958891500 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java @@ -37,6 +37,7 @@ import android.widget.ViewAnimator; import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.view.MenuItem; import org.libreoffice.impressremote.adapter.ComputersAdapter; +import org.libreoffice.impressremote.util.Fragments; import org.libreoffice.impressremote.util.Intents; import org.libreoffice.impressremote.R; import org.libreoffice.impressremote.communication.CommunicationService; @@ -65,21 +66,16 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo private static Bundle buildArguments(Type aType) { Bundle aArguments = new Bundle(); - aArguments.putSerializable("TYPE", aType); + aArguments.putSerializable(Fragments.Arguments.TYPE, aType); return aArguments; } - @Override - public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) { - return aInflater.inflate(R.layout.fragment_computers_list, aContainer, false); - } - @Override public void onCreate(Bundle aSavedInstanceState) { super.onCreate(aSavedInstanceState); - mType = (Type) getArguments().getSerializable("TYPE"); + mType = (Type) getArguments().getSerializable(Fragments.Arguments.TYPE); setUpActionBar(); } @@ -88,6 +84,11 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo setHasOptionsMenu(true); } + @Override + public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) { + return aInflater.inflate(R.layout.fragment_computers_list, aContainer, false); + } + @Override public void onActivityCreated(Bundle aSavedInstanceState) { super.onActivityCreated(aSavedInstanceState); @@ -105,11 +106,22 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo CommunicationService.CBinder aServiceBinder = (CommunicationService.CBinder) aBinder; mCommunicationService = aServiceBinder.getService(); - mCommunicationService.startSearch(); - + startComputersSearch(); loadComputers(); } + private void startComputersSearch() { + if (!isServiceBound()) { + return; + } + + mCommunicationService.startServersSearch(); + } + + private boolean isServiceBound() { + return mCommunicationService != null; + } + private void loadComputers() { if (!isServiceBound()) { return; @@ -127,20 +139,46 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo } } - private boolean isServiceBound() { - return mCommunicationService != null; + private List getComputers() { + List aComputers = new ArrayList(); + + for (Server aComputer : mCommunicationService.getServers()) { + if (isComputerSupportsRequiredType(aComputer)) { + aComputers.add(aComputer); + } + } + + return aComputers; + } + + private boolean isComputerSupportsRequiredType(Server aComputer) { + switch (mType) { + case WIFI: + return aComputer.getProtocol() == Server.Protocol.TCP; + + case BLUETOOTH: + return aComputer.getProtocol() == Server.Protocol.BLUETOOTH; + + default: + return false; + } } private void hideComputersList() { + showView(getProgressBarLayout()); + } + + private void showView(View aView) { ViewAnimator aViewAnimator = getViewAnimator(); - int aProgressBarLayoutIndex = aViewAnimator.indexOfChild(getProgressBarLayout()); + int aViewIndex = aViewAnimator.indexOfChild(aView); + int aCurrentViewIndex = aViewAnimator.getDisplayedChild(); - if (aViewAnimator.getDisplayedChild() == aProgressBarLayoutIndex) { + if (aViewIndex == aCurrentViewIndex) { return; } - aViewAnimator.setDisplayedChild(aProgressBarLayoutIndex); + aViewAnimator.setDisplayedChild(aViewIndex); } private ViewAnimator getViewAnimator() { @@ -201,15 +239,12 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo setListAdapter(null); } - private ListView getComputesList() { - return (ListView) getView().findViewById(android.R.id.list); - } - private void showComputersList() { - ViewAnimator aViewAnimator = getViewAnimator(); - ListView aComputersList= getComputesList(); + showView(getComputersList()); + } - aViewAnimator.setDisplayedChild(aViewAnimator.indexOfChild(aComputersList)); + private ListView getComputersList() { + return (ListView) getView().findViewById(android.R.id.list); } private void setUpComputersAdapter() { @@ -233,57 +268,14 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo getComputersAdapter().add(getComputers()); } - private List getComputers() { - List aComputers = new ArrayList(); - - for (Server aServer : mCommunicationService.getServers()) { - if (isComputerSupportsRequiredType(aServer)) { - aComputers.add(aServer); - } - } - - return aComputers; - } - - private boolean isComputerSupportsRequiredType(Server aServer) { - switch (mType) { - case WIFI: - return aServer.getProtocol() == Server.Protocol.TCP; - - case BLUETOOTH: - return aServer.getProtocol() == Server.Protocol.BLUETOOTH; - - default: - return false; - } - } - - @Override - public void onDestroy() { - super.onDestroy(); - - unbindService(); - } - - private void unbindService() { - if (!isServiceBound()) { - return; - } - - getActivity().unbindService(this); - } - - @Override - public void onServiceDisconnected(ComponentName aComponentName) { - mCommunicationService = null; - } - @Override - public void onResume() { - super.onResume(); + public void onStart() { + super.onStart(); registerIntentsReceiver(); + setUpContextMenu(); + startComputersSearch(); loadComputers(); } @@ -322,37 +314,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo return LocalBroadcastManager.getInstance(aContext); } - @Override - public void onPause() { - super.onPause(); - - unregisterIntentsReceiver(); - } - - private void unregisterIntentsReceiver() { - try { - getBroadcastManager().unregisterReceiver(mIntentsReceiver); - } catch (IllegalArgumentException e) { - // Receiver not registered. - // Fixed in Honeycomb: Android’s issue #6191. - } - } - - @Override - public void onListItemClick(ListView aListView, View aView, int aPosition, long aId) { - Server aComputer = getComputersAdapter().getItem(aPosition); - - Intent aIntent = Intents.buildComputerConnectionIntent(getActivity(), aComputer); - startActivity(aIntent); - } - - @Override - public void onStart() { - super.onStart(); - - setUpContextMenu(); - } - private void setUpContextMenu() { registerForContextMenu(getListView()); } @@ -438,6 +399,60 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo Intent aIntent = Intents.buildServersListChangedIntent(); LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(aIntent); } + + @Override + public void onListItemClick(ListView aListView, View aView, int aPosition, long aId) { + Server aComputer = getComputersAdapter().getItem(aPosition); + + Intent aIntent = Intents.buildComputerConnectionIntent(getActivity(), aComputer); + startActivity(aIntent); + } + + @Override + public void onStop() { + super.onStop(); + + stopComputersSearch(); + + unregisterIntentsReceiver(); + } + + private void unregisterIntentsReceiver() { + try { + getBroadcastManager().unregisterReceiver(mIntentsReceiver); + } catch (IllegalArgumentException e) { + // Receiver not registered. + // Fixed in Honeycomb: Android’s issue #6191. + } + } + + private void stopComputersSearch() { + if (!isServiceBound()) { + return; + } + + mCommunicationService.stopServersSearch(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + unbindService(); + } + + private void unbindService() { + if (!isServiceBound()) { + return; + } + + getActivity().unbindService(this); + } + + @Override + public void onServiceDisconnected(ComponentName aComponentName) { + mCommunicationService = null; + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java index a531d860b349..4be74e172d31 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java @@ -11,6 +11,18 @@ 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() { } @@ -25,6 +37,26 @@ public final class BluetoothOperator { return BluetoothAdapter.getDefaultAdapter(); } + + public static State getState() { + return new State(getAdapter().isEnabled()); + } + + public static void enable() { + if (!isAvailable()) { + return; + } + + getAdapter().enable(); + } + + public static void disable() { + if (!isAvailable()) { + return; + } + + getAdapter().disable(); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/Fragments.java b/android/sdremote/src/org/libreoffice/impressremote/util/Fragments.java index 89752bf11620..3b180580052e 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/Fragments.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/Fragments.java @@ -17,8 +17,8 @@ public final class Fragments { } public static final String COMPUTER = "COMPUTER"; - public static final String MINUTES = "MINUTES"; + public static final String TYPE = "TYPE"; } } -- cgit