Improve ComputersFragment.

* Load saved computers better.
* Handle removing and adding computers properly.

Change-Id: I12027ad96f06cfeccbc249f453ccff588ccd79c6
This commit is contained in:
Artur Dryomov
2013-07-21 00:06:49 +03:00
committed by Michael Meeks
parent 359751db77
commit 15081f4582
2 changed files with 97 additions and 70 deletions

View File

@@ -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,91 +92,46 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
mCommunicationService = aServiceBinder.getService();
mCommunicationService.startSearch();
loadComputers();
}
@Override
public void onDestroy() {
super.onDestroy();
private void loadComputers() {
if (!isAdded()) {
return;
}
unbindService();
}
private void unbindService() {
if (!isServiceBound()) {
return;
}
getActivity().unbindService(this);
if (getComputers().isEmpty()) {
hideComputersList();
}
else {
showComputersList();
}
}
private boolean isServiceBound() {
return mCommunicationService != null;
}
@Override
public void onServiceDisconnected(ComponentName aComponentName) {
mCommunicationService = null;
private void hideComputersList() {
setListAdapter(null);
setListShown(false);
}
@Override
public void onResume() {
super.onResume();
loadComputers();
registerIntentsReceiver();
}
private void registerIntentsReceiver() {
mIntentsReceiver = new IntentsReceiver(this);
IntentFilter aIntentFilter = buildIntentsReceiverFilter();
getBroadcastManager().registerReceiver(mIntentsReceiver, aIntentFilter);
}
private static final class IntentsReceiver extends BroadcastReceiver {
private final ComputersFragment mComputersFragment;
public IntentsReceiver(ComputersFragment aComputersFragment) {
mComputersFragment = aComputersFragment;
}
@Override
public void onReceive(Context aContext, Intent aIntent) {
if (Intents.Actions.SERVERS_LIST_CHANGED.equals(aIntent.getAction())) {
mComputersFragment.loadComputers();
}
}
}
private IntentFilter buildIntentsReceiverFilter() {
IntentFilter aIntentFilter = new IntentFilter();
aIntentFilter.addAction(Intents.Actions.SERVERS_LIST_CHANGED);
return aIntentFilter;
}
private LocalBroadcastManager getBroadcastManager() {
Context aContext = getActivity().getApplicationContext();
return LocalBroadcastManager.getInstance(aContext);
}
private void loadComputers() {
if (!isServiceBound()) {
return;
}
if (getComputers().isEmpty()) {
return;
}
private void showComputersList() {
if (!isComputersAdapterExist()) {
setUpComputersAdapter();
}
getComputersAdapter().clear();
getComputersAdapter().add(getComputers());
setListShown(true);
}
private boolean isComputersAdapterExist() {
@@ -217,6 +171,68 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
}
}
@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();
registerIntentsReceiver();
}
private void registerIntentsReceiver() {
mIntentsReceiver = new IntentsReceiver(this);
IntentFilter aIntentFilter = buildIntentsReceiverFilter();
getBroadcastManager().registerReceiver(mIntentsReceiver, aIntentFilter);
}
private static final class IntentsReceiver extends BroadcastReceiver {
private final ComputersFragment mComputersFragment;
public IntentsReceiver(ComputersFragment aComputersFragment) {
mComputersFragment = aComputersFragment;
}
@Override
public void onReceive(Context aContext, Intent aIntent) {
if (Intents.Actions.SERVERS_LIST_CHANGED.equals(aIntent.getAction())) {
mComputersFragment.loadComputers();
}
}
}
private IntentFilter buildIntentsReceiverFilter() {
IntentFilter aIntentFilter = new IntentFilter();
aIntentFilter.addAction(Intents.Actions.SERVERS_LIST_CHANGED);
return aIntentFilter;
}
private LocalBroadcastManager getBroadcastManager() {
Context aContext = getActivity().getApplicationContext();
return LocalBroadcastManager.getInstance(aContext);
}
@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();

View File

@@ -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: */