From 15081f45820a858f84c96189bbf53f8d3ea72f64 Mon Sep 17 00:00:00 2001 From: Artur Dryomov Date: Sun, 21 Jul 2013 00:06:49 +0300 Subject: Improve ComputersFragment. * Load saved computers better. * Handle removing and adding computers properly. Change-Id: I12027ad96f06cfeccbc249f453ccff588ccd79c6 --- .../impressremote/fragment/ComputersFragment.java | 154 ++++++++++++--------- .../libreoffice/impressremote/util/Intents.java | 5 + 2 files changed, 93 insertions(+), 66 deletions(-) (limited to 'android') diff --git a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java index 11d28964a418..18491d6cc73e 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java @@ -81,8 +81,7 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo } private void bindService() { - Intent aServiceIntent = new Intent(getActivity(), CommunicationService.class); - + Intent aServiceIntent = Intents.buildCommunicationServiceIntent(getActivity()); getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE); } @@ -93,6 +92,83 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo mCommunicationService = aServiceBinder.getService(); mCommunicationService.startSearch(); + + loadComputers(); + } + + private void loadComputers() { + if (!isAdded()) { + return; + } + + if (!isServiceBound()) { + return; + } + + if (getComputers().isEmpty()) { + hideComputersList(); + } + else { + showComputersList(); + } + } + + private boolean isServiceBound() { + return mCommunicationService != null; + } + + private void hideComputersList() { + setListAdapter(null); + + setListShown(false); + } + + private void showComputersList() { + if (!isComputersAdapterExist()) { + setUpComputersAdapter(); + } + + getComputersAdapter().clear(); + getComputersAdapter().add(getComputers()); + + setListShown(true); + } + + private boolean isComputersAdapterExist() { + return getComputersAdapter() != null; + } + + private ComputersAdapter getComputersAdapter() { + return (ComputersAdapter) getListAdapter(); + } + + private void setUpComputersAdapter() { + setListAdapter(new ComputersAdapter(getActivity())); + } + + 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 @@ -110,10 +186,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo getActivity().unbindService(this); } - private boolean isServiceBound() { - return mCommunicationService != null; - } - @Override public void onServiceDisconnected(ComponentName aComponentName) { mCommunicationService = null; @@ -123,8 +195,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo public void onResume() { super.onResume(); - loadComputers(); - registerIntentsReceiver(); } @@ -163,60 +233,6 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo return LocalBroadcastManager.getInstance(aContext); } - private void loadComputers() { - if (!isServiceBound()) { - return; - } - - if (getComputers().isEmpty()) { - return; - } - - if (!isComputersAdapterExist()) { - setUpComputersAdapter(); - } - - getComputersAdapter().clear(); - getComputersAdapter().add(getComputers()); - } - - private boolean isComputersAdapterExist() { - return getComputersAdapter() != null; - } - - private ComputersAdapter getComputersAdapter() { - return (ComputersAdapter) getListAdapter(); - } - - private void setUpComputersAdapter() { - setListAdapter(new ComputersAdapter(getActivity())); - } - - 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 onPause() { super.onPause(); @@ -264,9 +280,14 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo int aComputerPosition = getListItemPosition(aMenuItem); Server aComputer = getComputersAdapter().getItem(aComputerPosition); - removeComputer(aComputer); + switch (aMenuItem.getItemId()) { + case R.id.menu_remove_computer: + removeComputer(aComputer); + return true; - return true; + default: + return super.onContextItemSelected(aMenuItem); + } } private int getListItemPosition(android.view.MenuItem aMenuItem) { @@ -312,10 +333,11 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo String aServerAddress = aIntent.getStringExtra(Intents.Extras.SERVER_ADDRESS); String aServerName = aIntent.getStringExtra(Intents.Extras.SERVER_NAME); - addServer(aServerAddress, aServerName); + addComputer(aServerAddress, aServerName); + loadComputers(); } - private void addServer(String aAddress, String aName) { + private void addComputer(String aAddress, String aName) { mCommunicationService.addServer(aAddress, aName); Intent aIntent = Intents.buildServersListChangedIntent(); diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/Intents.java b/android/sdremote/src/org/libreoffice/impressremote/util/Intents.java index 04913457550a..ad7cfc1324c9 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/Intents.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/Intents.java @@ -15,6 +15,7 @@ import org.libreoffice.impressremote.activity.ComputerConnectionActivity; import org.libreoffice.impressremote.activity.ComputerCreationActivity; import org.libreoffice.impressremote.activity.LicensesActivity; import org.libreoffice.impressremote.activity.SlideShowActivity; +import org.libreoffice.impressremote.communication.CommunicationService; import org.libreoffice.impressremote.communication.Server; public final class Intents { @@ -134,6 +135,10 @@ public final class Intents { public static Intent buildLicensesIntent(Context aContext) { return new Intent(aContext, LicensesActivity.class); } + + public static Intent buildCommunicationServiceIntent(Context aContext) { + return new Intent(aContext, CommunicationService.class); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit