diff options
author | Christian Lohmaier <lohmaier+LibreOffice@googlemail.com> | 2013-12-15 16:38:17 +0100 |
---|---|---|
committer | Andrzej Hunt <andrzej.hunt@collabora.com> | 2014-01-16 15:43:24 +0000 |
commit | c0693031a7887a3146a049a5aabe36ed0ee14941 (patch) | |
tree | f9ff3f6de309b1f3c822fbd83a9846e8dbabf641 /android | |
parent | 17e7bc358faab29ddb524d478f6b5ca2fed1a5f8 (diff) |
get rid of some useless indirection/wrapper functions
there's no point in adding a layer of indirection if all that the called
function does is running one single command, and is only called in one
place. Getting rid of that indirection makes the code easier to read and
understand.
Change-Id: Ie2f3e03fe2870d1d4a84df738ebb3d0f34a2713c
Reviewed-on: https://gerrit.libreoffice.org/7089
Reviewed-by: Andrzej Hunt <andrzej.hunt@collabora.com>
Tested-by: Andrzej Hunt <andrzej.hunt@collabora.com>
Diffstat (limited to 'android')
4 files changed, 57 insertions, 215 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java index f75d197f8c8c..0c7bd685640b 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java @@ -36,10 +36,24 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta super.onCreate(aSavedInstanceState); saveBluetoothState(aSavedInstanceState); - enableBluetooth(); + BluetoothOperator.enable(); + + // Looks hacky but it seems to be the best way to set activity’s title + // different to application’s label. The other way is setting title + // to intents filter but it shows wrong label for recent apps screen then. + + ActionBar aActionBar = getSupportActionBar(); + + aActionBar.setTitle(R.string.title_computers); + aActionBar.setDisplayShowTitleEnabled(true); - setUpTitle(); - setUpContent(); + if (BluetoothOperator.isAvailable()) { + setUpComputersLists(); + } + else { + Fragment aComputersFragment = ComputersFragment.newInstance(ComputersFragment.Type.WIFI); + Fragments.Operator.add(this, aComputersFragment); + } } private void saveBluetoothState(Bundle aSavedInstanceState) { @@ -63,50 +77,22 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta return aSavedInstanceState.getBoolean(SavedStates.Keys.BLUETOOTH_ENABLED); } - private void enableBluetooth() { - BluetoothOperator.enable(); - } - - private void setUpTitle() { - // Looks hacky but it seems to be the best way to set activity’s title - // different to application’s label. The other way is setting title - // to intents filter but it shows wrong label for recent apps screen then. - - ActionBar aActionBar = getSupportActionBar(); - - aActionBar.setTitle(R.string.title_computers); - aActionBar.setDisplayShowTitleEnabled(true); - } - - private void setUpContent() { - if (areMultipleComputersTypesAvailable()) { - setUpComputersLists(); - } - else { - setUpComputersList(); - } - } - - private boolean areMultipleComputersTypesAvailable() { - return BluetoothOperator.isAvailable(); - } - private void setUpComputersLists() { setContentView(R.layout.activity_computers); - setUpTabs(); - setUpComputersPager(); - - setUpSavedTab(); - } - - private void setUpTabs() { ActionBar aActionBar = getSupportActionBar(); aActionBar.addTab(buildActionBarTab( R.string.title_bluetooth), ComputersPagerAdapter.PagesIndices.BLUETOOTH); aActionBar.addTab(buildActionBarTab( R.string.title_wifi), ComputersPagerAdapter.PagesIndices.WIFI); + + ViewPager aComputersPager = getComputersPager(); + + aComputersPager.setAdapter(new ComputersPagerAdapter(getSupportFragmentManager())); + aComputersPager.setOnPageChangeListener(this); + + getSupportActionBar().setSelectedNavigationItem(loadTabIndex()); } private ActionBar.Tab buildActionBarTab(int aTitleResourceId) { @@ -122,17 +108,13 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta public void onTabSelected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { getComputersPager().setCurrentItem(aTab.getPosition()); - refreshActionBarMenu(); + supportInvalidateOptionsMenu(); } private ViewPager getComputersPager() { return (ViewPager) findViewById(R.id.pager_computers); } - private void refreshActionBarMenu() { - supportInvalidateOptionsMenu(); - } - @Override public void onTabUnselected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { } @@ -141,17 +123,6 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta public void onTabReselected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { } - private void setUpComputersPager() { - ViewPager aComputersPager = getComputersPager(); - - aComputersPager.setAdapter(buildComputersPagerAdapter()); - aComputersPager.setOnPageChangeListener(this); - } - - private PagerAdapter buildComputersPagerAdapter() { - return new ComputersPagerAdapter(getSupportFragmentManager()); - } - @Override public void onPageSelected(int aPosition) { getSupportActionBar().setSelectedNavigationItem(aPosition); @@ -165,22 +136,12 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta public void onPageScrollStateChanged(int aPosition) { } - private void setUpSavedTab() { - getSupportActionBar().setSelectedNavigationItem(loadTabIndex()); - } - private int loadTabIndex() { Preferences aPreferences = Preferences.getApplicationStatesInstance(this); return aPreferences.getInt(Preferences.Keys.SELECTED_COMPUTERS_TAB_INDEX); } - private void setUpComputersList() { - Fragment aComputersFragment = ComputersFragment.newInstance(ComputersFragment.Type.WIFI); - - Fragments.Operator.add(this, aComputersFragment); - } - @Override public boolean onCreateOptionsMenu(Menu aMenu) { getMenuInflater().inflate(R.menu.menu_action_bar_computers, aMenu); @@ -190,7 +151,7 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta @Override public boolean onPrepareOptionsMenu(Menu aMenu) { - if (!areMultipleComputersTypesAvailable()) { + if (!BluetoothOperator.isAvailable()) { return super.onPrepareOptionsMenu(aMenu); } @@ -250,10 +211,6 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta protected void onStop() { super.onStop(); - saveTabIndex(); - } - - private void saveTabIndex() { Preferences aPreferences = Preferences.getApplicationStatesInstance(this); int aTabIndex = getSupportActionBar().getSelectedNavigationIndex(); @@ -264,10 +221,6 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta protected void onSaveInstanceState(Bundle aSavedInstanceState) { super.onSaveInstanceState(aSavedInstanceState); - rememberBluetoothState(aSavedInstanceState); - } - - private void rememberBluetoothState(Bundle aSavedInstanceState) { aSavedInstanceState.putBoolean(SavedStates.Keys.BLUETOOTH_ENABLED, mBluetoothWasEnabled); } @@ -275,10 +228,6 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta protected void onDestroy() { super.onDestroy(); - restoreBluetoothState(); - } - - private void restoreBluetoothState() { if (!BluetoothOperator.isAvailable()) { return; } @@ -287,10 +236,6 @@ public class ComputersActivity extends ActionBarActivity implements ActionBar.Ta return; } - disableBluetooth(); - } - - private void disableBluetooth() { BluetoothOperator.disable(); } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/activity/SettingsActivity.java b/android/sdremote/src/org/libreoffice/impressremote/activity/SettingsActivity.java index 762c49771d0c..b7964299be92 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/activity/SettingsActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/activity/SettingsActivity.java @@ -19,15 +19,6 @@ public class SettingsActivity extends PreferenceActivity { protected void onCreate(Bundle aSavedInstanceState) { super.onCreate(aSavedInstanceState); - setUpPreferences(); - } - - private void setUpPreferences() { - // This action is deprecated - // but we still need to target pre-Honeycomb devices. - - // TODO: try to use a fragment depending on platform version - addPreferencesFromResource(R.xml.preferences); } @@ -35,17 +26,13 @@ public class SettingsActivity extends PreferenceActivity { public boolean onOptionsItemSelected(MenuItem aMenuItem) { switch (aMenuItem.getItemId()) { case android.R.id.home: - navigateUp(); + finish(); return true; default: return super.onOptionsItemSelected(aMenuItem); } } - - private void navigateUp() { - finish(); - } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java index 29e9b9c1a051..ac6cdc34d1b5 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java @@ -89,8 +89,7 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) { BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - addServer(buildServer(aBluetoothDevice)); - callUpdatingServersList(); + addServer(aBluetoothDevice); } } @@ -111,17 +110,16 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, public void run() { BluetoothOperator.getAdapter().startDiscovery(); } - - private void addServer(Server aServer) { - mServers.put(aServer.getAddress(), aServer); - } - - private Server buildServer(BluetoothDevice aBluetoothDevice) { + private void addServer(BluetoothDevice aBluetoothDevice) { Server.Type aServerType = buildServerType(aBluetoothDevice); String aServerAddress = aBluetoothDevice.getAddress(); String aServerName = aBluetoothDevice.getName(); - return Server.newBluetoothInstance(aServerType, aServerAddress, aServerName); + Server aServer = Server.newBluetoothInstance(aServerType, aServerAddress, aServerName); + mServers.put(aServer.getAddress(), aServer); + + Intent bIntent = Intents.buildServersListChangedIntent(); + LocalBroadcastManager.getInstance(mContext).sendBroadcast(bIntent); } private Server.Type buildServerType(BluetoothDevice aBluetoothDevice) { @@ -139,29 +137,20 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, } } - private void callUpdatingServersList() { - Intent aIntent = Intents.buildServersListChangedIntent(); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); - } - @Override public void stopSearch() { if (!BluetoothOperator.isAvailable()) { return; } - tearDownBluetoothActionsReceiver(); - - BluetoothOperator.getAdapter().cancelDiscovery(); - } - - private void tearDownBluetoothActionsReceiver() { try { mContext.unregisterReceiver(this); } catch (IllegalArgumentException e) { // Receiver not registered. // Fixed in Honeycomb: Android’s issue #6191. } + + BluetoothOperator.getAdapter().cancelDiscovery(); } @Override diff --git a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java index bd80f6cfc65f..ff061e655b36 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/fragment/ComputersFragment.java @@ -76,10 +76,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onCreate(Bundle aSavedInstanceState) { super.onCreate(aSavedInstanceState); - setUpActionBar(); - } - - private void setUpActionBar() { setHasOptionsMenu(true); } @@ -92,21 +88,8 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onViewStateRestored(Bundle aSavedInstanceState) { super.onViewStateRestored(aSavedInstanceState); - if (!isSavedInstanceValid(aSavedInstanceState)) { - return; - } - - loadProgressMessage(aSavedInstanceState); - } - - private boolean isSavedInstanceValid(Bundle aSavedInstanceState) { - return aSavedInstanceState != null; - } - - private void loadProgressMessage(Bundle aSavedInstanceState) { - boolean aProgressMessageDisplayed = aSavedInstanceState.getBoolean(SavedStates.Keys.PROGRESS_MESSAGE); - - if (aProgressMessageDisplayed) { + if (aSavedInstanceState != null + && aSavedInstanceState.getBoolean(SavedStates.Keys.PROGRESS_MESSAGE)) { showProgressMessage(); showLearnMoreMessage(); } @@ -127,7 +110,7 @@ public class ComputersFragment extends ListFragment implements ServiceConnection } private void showLearnMoreMessage() { - TextView learnMoreView = getLearnMoreView(); + TextView learnMoreView = (TextView) getView().findViewById(R.id.text_learn_more); Animation aFadeInAnimation = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in); learnMoreView.setMovementMethod(LinkMovementMethod.getInstance()); @@ -136,10 +119,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection learnMoreView.setVisibility(View.VISIBLE); } - private TextView getLearnMoreView() { - return (TextView) getView().findViewById(R.id.text_learn_more); - } - private String getProgressMessage() { switch (getType()) { case WIFI: @@ -161,10 +140,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onActivityCreated(Bundle aSavedInstanceState) { super.onActivityCreated(aSavedInstanceState); - bindService(); - } - - private void bindService() { Intent aServiceIntent = Intents.buildCommunicationServiceIntent(getActivity()); getActivity().bindService(aServiceIntent, this, Context.BIND_AUTO_CREATE); } @@ -187,15 +162,22 @@ public class ComputersFragment extends ListFragment implements ServiceConnection return; } - if (getComputers().isEmpty()) { - hideComputersList(); + List<Server> computerList = getComputers(); + if (computerList.isEmpty()) { + setCurrentView(R.id.layout_progress); + setUpProgressMessage(); - tearDownComputersAdapter(); - } - else { - setUpComputersAdapter(); - fillComputersAdapter(); - showComputersList(); + + setListAdapter(null); + } else { + if (getComputersAdapter() == null) { + setListAdapter(new ComputersAdapter(getActivity())); + } + + getComputersAdapter().clear(); + getComputersAdapter().add(computerList); + + setCurrentView(android.R.id.list); } } @@ -224,10 +206,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection } } - private void hideComputersList() { - setCurrentView(R.id.layout_progress); - } - private void setCurrentView(int aViewId) { ViewAnimator aViewAnimator = (ViewAnimator) getView().findViewById(R.id.view_animator); View aView = getView().findViewById(aViewId); @@ -261,35 +239,10 @@ public class ComputersFragment extends ListFragment implements ServiceConnection return getProgressMessageView().getVisibility() == View.INVISIBLE; } - private void tearDownComputersAdapter() { - setListAdapter(null); - } - - private void setUpComputersAdapter() { - if (isComputersAdapterExist()) { - return; - } - - setListAdapter(new ComputersAdapter(getActivity())); - } - - private boolean isComputersAdapterExist() { - return getComputersAdapter() != null; - } - private ComputersAdapter getComputersAdapter() { return (ComputersAdapter) getListAdapter(); } - private void fillComputersAdapter() { - getComputersAdapter().clear(); - getComputersAdapter().add(getComputers()); - } - - private void showComputersList() { - setCurrentView(android.R.id.list); - } - @Override public void onServiceDisconnected(ComponentName aComponentName) { mCommunicationService = null; @@ -302,7 +255,7 @@ public class ComputersFragment extends ListFragment implements ServiceConnection registerIntentsReceiver(); setUpContextMenu(); - if (!isServiceBound()) { + if (mCommunicationService == null) { return; } @@ -312,7 +265,8 @@ public class ComputersFragment extends ListFragment implements ServiceConnection private void registerIntentsReceiver() { mIntentsReceiver = new IntentsReceiver(this); - IntentFilter aIntentFilter = buildIntentsReceiverFilter(); + IntentFilter aIntentFilter = new IntentFilter(); + aIntentFilter.addAction(Intents.Actions.SERVERS_LIST_CHANGED); getBroadcastManager().registerReceiver(mIntentsReceiver, aIntentFilter); } @@ -332,13 +286,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection } } - private IntentFilter buildIntentsReceiverFilter() { - IntentFilter aIntentFilter = new IntentFilter(); - aIntentFilter.addAction(Intents.Actions.SERVERS_LIST_CHANGED); - - return aIntentFilter; - } - private LocalBroadcastManager getBroadcastManager() { Context aContext = getActivity().getApplicationContext(); @@ -390,10 +337,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(aIntent); } - private boolean isServiceBound() { - return mCommunicationService != null; - } - @Override public boolean onOptionsItemSelected(MenuItem aMenuItem) { switch (aMenuItem.getItemId()) { @@ -451,20 +394,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onStop() { super.onStop(); - stopComputersSearch(); - - unregisterIntentsReceiver(); - } - - private void stopComputersSearch() { - if (!isServiceBound()) { - return; - } - - mCommunicationService.stopServersSearch(); - } - - private void unregisterIntentsReceiver() { try { getBroadcastManager().unregisterReceiver(mIntentsReceiver); } catch (IllegalArgumentException e) { @@ -477,10 +406,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onSaveInstanceState(Bundle aOutState) { super.onSaveInstanceState(aOutState); - saveProgressMessage(aOutState); - } - - private void saveProgressMessage(Bundle aOutState) { boolean aProgressMessageDisplayed = !TextUtils.isEmpty(getProgressMessageView().getText().toString()); aOutState.putBoolean(SavedStates.Keys.PROGRESS_MESSAGE, aProgressMessageDisplayed); @@ -490,10 +415,6 @@ public class ComputersFragment extends ListFragment implements ServiceConnection public void onDestroy() { super.onDestroy(); - unbindService(); - } - - private void unbindService() { getActivity().unbindService(this); } } |