Kaydet (Commit) 21e5de7d authored tarafından Artur Dryomov's avatar Artur Dryomov

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
üst e759a986
...@@ -43,7 +43,7 @@ public class CommunicationService extends Service implements Runnable, MessagesL ...@@ -43,7 +43,7 @@ public class CommunicationService extends Service implements Runnable, MessagesL
private MessagesReceiver mMessagesReceiver; private MessagesReceiver mMessagesReceiver;
private CommandsTransmitter mCommandsTransmitter; private CommandsTransmitter mCommandsTransmitter;
private boolean mBluetoothWasEnabled; private BluetoothOperator.State mBluetoothState;
private Timer mTimer; private Timer mTimer;
private SlideShow mSlideShow; private SlideShow mSlideShow;
...@@ -61,7 +61,8 @@ public class CommunicationService extends Service implements Runnable, MessagesL ...@@ -61,7 +61,8 @@ public class CommunicationService extends Service implements Runnable, MessagesL
mServersManager = new ServersManager(this); mServersManager = new ServersManager(this);
mBluetoothWasEnabled = false; saveBluetoothState();
enableBluetooth();
mTimer = new Timer(this); mTimer = new Timer(this);
mSlideShow = new SlideShow(mTimer); mSlideShow = new SlideShow(mTimer);
...@@ -76,6 +77,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL ...@@ -76,6 +77,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL
} }
} }
private void saveBluetoothState() {
mBluetoothState = BluetoothOperator.getState();
}
private void enableBluetooth() {
BluetoothOperator.enable();
}
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return mBinder; return mBinder;
...@@ -164,28 +173,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL ...@@ -164,28 +173,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent); LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
} }
public void startSearch() { public void startServersSearch() {
mState = State.SEARCHING; mState = State.SEARCHING;
if (BluetoothOperator.isAvailable()) {
mBluetoothWasEnabled = BluetoothOperator.getAdapter().isEnabled();
if (!BluetoothOperator.getAdapter().isEnabled()) {
BluetoothOperator.getAdapter().enable();
}
}
mServersManager.startServersSearch(); mServersManager.startServersSearch();
} }
public void stopSearch() { public void stopServersSearch() {
mServersManager.stopServersSearch(); mServersManager.stopServersSearch();
if (BluetoothOperator.isAvailable()) {
if (!mBluetoothWasEnabled) {
BluetoothOperator.getAdapter().disable();
}
}
} }
public List<Server> getServers() { public List<Server> getServers() {
...@@ -298,11 +293,21 @@ public class CommunicationService extends Service implements Runnable, MessagesL ...@@ -298,11 +293,21 @@ public class CommunicationService extends Service implements Runnable, MessagesL
@Override @Override
public void onDestroy() { public void onDestroy() {
stopSearch(); stopServersSearch();
restoreBluetoothState();
mThread.interrupt(); mThread.interrupt();
mThread = null; mThread = null;
} }
private void restoreBluetoothState() {
if (mBluetoothState.wasBluetoothEnabled()) {
return;
}
BluetoothOperator.disable();
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -37,6 +37,7 @@ import android.widget.ViewAnimator; ...@@ -37,6 +37,7 @@ import android.widget.ViewAnimator;
import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import org.libreoffice.impressremote.adapter.ComputersAdapter; import org.libreoffice.impressremote.adapter.ComputersAdapter;
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.communication.CommunicationService; import org.libreoffice.impressremote.communication.CommunicationService;
...@@ -65,21 +66,16 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -65,21 +66,16 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
private static Bundle buildArguments(Type aType) { private static Bundle buildArguments(Type aType) {
Bundle aArguments = new Bundle(); Bundle aArguments = new Bundle();
aArguments.putSerializable("TYPE", aType); aArguments.putSerializable(Fragments.Arguments.TYPE, aType);
return aArguments; return aArguments;
} }
@Override
public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) {
return aInflater.inflate(R.layout.fragment_computers_list, aContainer, false);
}
@Override @Override
public void onCreate(Bundle aSavedInstanceState) { public void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState); super.onCreate(aSavedInstanceState);
mType = (Type) getArguments().getSerializable("TYPE"); mType = (Type) getArguments().getSerializable(Fragments.Arguments.TYPE);
setUpActionBar(); setUpActionBar();
} }
...@@ -88,6 +84,11 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -88,6 +84,11 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@Override
public View onCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) {
return aInflater.inflate(R.layout.fragment_computers_list, aContainer, false);
}
@Override @Override
public void onActivityCreated(Bundle aSavedInstanceState) { public void onActivityCreated(Bundle aSavedInstanceState) {
super.onActivityCreated(aSavedInstanceState); super.onActivityCreated(aSavedInstanceState);
...@@ -105,11 +106,22 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -105,11 +106,22 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
CommunicationService.CBinder aServiceBinder = (CommunicationService.CBinder) aBinder; CommunicationService.CBinder aServiceBinder = (CommunicationService.CBinder) aBinder;
mCommunicationService = aServiceBinder.getService(); mCommunicationService = aServiceBinder.getService();
mCommunicationService.startSearch(); startComputersSearch();
loadComputers(); loadComputers();
} }
private void startComputersSearch() {
if (!isServiceBound()) {
return;
}
mCommunicationService.startServersSearch();
}
private boolean isServiceBound() {
return mCommunicationService != null;
}
private void loadComputers() { private void loadComputers() {
if (!isServiceBound()) { if (!isServiceBound()) {
return; return;
...@@ -127,20 +139,46 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -127,20 +139,46 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
} }
} }
private boolean isServiceBound() { private List<Server> getComputers() {
return mCommunicationService != null; List<Server> aComputers = new ArrayList<Server>();
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() { private void hideComputersList() {
showView(getProgressBarLayout());
}
private void showView(View aView) {
ViewAnimator aViewAnimator = getViewAnimator(); 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; return;
} }
aViewAnimator.setDisplayedChild(aProgressBarLayoutIndex); aViewAnimator.setDisplayedChild(aViewIndex);
} }
private ViewAnimator getViewAnimator() { private ViewAnimator getViewAnimator() {
...@@ -201,15 +239,12 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -201,15 +239,12 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
setListAdapter(null); setListAdapter(null);
} }
private ListView getComputesList() {
return (ListView) getView().findViewById(android.R.id.list);
}
private void showComputersList() { private void showComputersList() {
ViewAnimator aViewAnimator = getViewAnimator(); showView(getComputersList());
ListView aComputersList= getComputesList(); }
aViewAnimator.setDisplayedChild(aViewAnimator.indexOfChild(aComputersList)); private ListView getComputersList() {
return (ListView) getView().findViewById(android.R.id.list);
} }
private void setUpComputersAdapter() { private void setUpComputersAdapter() {
...@@ -233,57 +268,14 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -233,57 +268,14 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
getComputersAdapter().add(getComputers()); getComputersAdapter().add(getComputers());
} }
private List<Server> getComputers() {
List<Server> aComputers = new ArrayList<Server>();
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 @Override
public void onResume() { public void onStart() {
super.onResume(); super.onStart();
registerIntentsReceiver(); registerIntentsReceiver();
setUpContextMenu();
startComputersSearch();
loadComputers(); loadComputers();
} }
...@@ -322,37 +314,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -322,37 +314,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
return LocalBroadcastManager.getInstance(aContext); 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() { private void setUpContextMenu() {
registerForContextMenu(getListView()); registerForContextMenu(getListView());
} }
...@@ -438,6 +399,60 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo ...@@ -438,6 +399,60 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
Intent aIntent = Intents.buildServersListChangedIntent(); Intent aIntent = Intents.buildServersListChangedIntent();
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(aIntent); 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: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -11,6 +11,18 @@ package org.libreoffice.impressremote.util; ...@@ -11,6 +11,18 @@ 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() {
} }
...@@ -25,6 +37,26 @@ public final class BluetoothOperator { ...@@ -25,6 +37,26 @@ public final class BluetoothOperator {
return BluetoothAdapter.getDefaultAdapter(); 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: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -17,8 +17,8 @@ public final class Fragments { ...@@ -17,8 +17,8 @@ public final class Fragments {
} }
public static final String COMPUTER = "COMPUTER"; public static final String COMPUTER = "COMPUTER";
public static final String MINUTES = "MINUTES"; public static final String MINUTES = "MINUTES";
public static final String TYPE = "TYPE";
} }
} }
......
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