Improve ComputersFragment.
* Load saved computers better. * Handle removing and adding computers properly. Change-Id: I12027ad96f06cfeccbc249f453ccff588ccd79c6
This commit is contained in:
committed by
Michael Meeks
parent
359751db77
commit
15081f4582
@@ -81,8 +81,7 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void bindService() {
|
private void bindService() {
|
||||||
Intent aServiceIntent = new Intent(getActivity(), CommunicationService.class);
|
Intent aServiceIntent = Intents.buildCommunicationServiceIntent(getActivity());
|
||||||
|
|
||||||
getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE);
|
getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,91 +92,46 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
|
|||||||
mCommunicationService = aServiceBinder.getService();
|
mCommunicationService = aServiceBinder.getService();
|
||||||
|
|
||||||
mCommunicationService.startSearch();
|
mCommunicationService.startSearch();
|
||||||
|
|
||||||
|
loadComputers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void loadComputers() {
|
||||||
public void onDestroy() {
|
if (!isAdded()) {
|
||||||
super.onDestroy();
|
return;
|
||||||
|
|
||||||
unbindService();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unbindService() {
|
|
||||||
if (!isServiceBound()) {
|
if (!isServiceBound()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
getActivity().unbindService(this);
|
if (getComputers().isEmpty()) {
|
||||||
|
hideComputersList();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
showComputersList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isServiceBound() {
|
private boolean isServiceBound() {
|
||||||
return mCommunicationService != null;
|
return mCommunicationService != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void hideComputersList() {
|
||||||
public void onServiceDisconnected(ComponentName aComponentName) {
|
setListAdapter(null);
|
||||||
mCommunicationService = 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()) {
|
if (!isComputersAdapterExist()) {
|
||||||
setUpComputersAdapter();
|
setUpComputersAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
getComputersAdapter().clear();
|
getComputersAdapter().clear();
|
||||||
getComputersAdapter().add(getComputers());
|
getComputersAdapter().add(getComputers());
|
||||||
|
|
||||||
|
setListShown(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isComputersAdapterExist() {
|
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
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
@@ -264,9 +280,14 @@ public class ComputersFragment extends SherlockListFragment implements ServiceCo
|
|||||||
int aComputerPosition = getListItemPosition(aMenuItem);
|
int aComputerPosition = getListItemPosition(aMenuItem);
|
||||||
Server aComputer = getComputersAdapter().getItem(aComputerPosition);
|
Server aComputer = getComputersAdapter().getItem(aComputerPosition);
|
||||||
|
|
||||||
|
switch (aMenuItem.getItemId()) {
|
||||||
|
case R.id.menu_remove_computer:
|
||||||
removeComputer(aComputer);
|
removeComputer(aComputer);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return super.onContextItemSelected(aMenuItem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getListItemPosition(android.view.MenuItem 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 aServerAddress = aIntent.getStringExtra(Intents.Extras.SERVER_ADDRESS);
|
||||||
String aServerName = aIntent.getStringExtra(Intents.Extras.SERVER_NAME);
|
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);
|
mCommunicationService.addServer(aAddress, aName);
|
||||||
|
|
||||||
Intent aIntent = Intents.buildServersListChangedIntent();
|
Intent aIntent = Intents.buildServersListChangedIntent();
|
||||||
|
@@ -15,6 +15,7 @@ import org.libreoffice.impressremote.activity.ComputerConnectionActivity;
|
|||||||
import org.libreoffice.impressremote.activity.ComputerCreationActivity;
|
import org.libreoffice.impressremote.activity.ComputerCreationActivity;
|
||||||
import org.libreoffice.impressremote.activity.LicensesActivity;
|
import org.libreoffice.impressremote.activity.LicensesActivity;
|
||||||
import org.libreoffice.impressremote.activity.SlideShowActivity;
|
import org.libreoffice.impressremote.activity.SlideShowActivity;
|
||||||
|
import org.libreoffice.impressremote.communication.CommunicationService;
|
||||||
import org.libreoffice.impressremote.communication.Server;
|
import org.libreoffice.impressremote.communication.Server;
|
||||||
|
|
||||||
public final class Intents {
|
public final class Intents {
|
||||||
@@ -134,6 +135,10 @@ public final class Intents {
|
|||||||
public static Intent buildLicensesIntent(Context aContext) {
|
public static Intent buildLicensesIntent(Context aContext) {
|
||||||
return new Intent(aContext, LicensesActivity.class);
|
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: */
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||||
|
Reference in New Issue
Block a user