diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-07-26 02:47:40 +0300 |
---|---|---|
committer | Artur Dryomov <artur.dryomov@gmail.com> | 2013-07-29 04:13:10 +0300 |
commit | ff5c113531ce9819927ecacbbb5853393eff501d (patch) | |
tree | 5a57188ed6395df0794028f52cb4600d3a97fbef /android | |
parent | d798d26bc4b7572ed10d6baf5aef7382269d7da5 (diff) |
Add auto-hiding tabs when Bluetooth is not available.
Introduce a helper class for Bluetooth as well.
Change-Id: I89b0b4c42ef56ce3f5c2be3a1ea9d443aec04fce
Diffstat (limited to 'android')
5 files changed, 116 insertions, 31 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java index 3a5bbb6beb88..3fa617339b6d 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/activity/ComputersActivity.java @@ -10,6 +10,7 @@ package org.libreoffice.impressremote.activity; import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; @@ -19,6 +20,9 @@ import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import org.libreoffice.impressremote.adapter.ComputersPagerAdapter; +import org.libreoffice.impressremote.fragment.ComputersFragment; +import org.libreoffice.impressremote.util.BluetoothOperator; +import org.libreoffice.impressremote.util.FragmentOperator; import org.libreoffice.impressremote.util.Intents; import org.libreoffice.impressremote.R; @@ -26,6 +30,20 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + setUpContent(); + } + + private void setUpContent() { + if (BluetoothOperator.isAvailable()) { + setUpComputersLists(); + } + else { + setUpComputersList(); + } + } + + private void setUpComputersLists() { setContentView(R.layout.activity_computers); setUpTabs(); @@ -35,12 +53,8 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio private void setUpTabs() { ActionBar aActionBar = getSupportActionBar(); - aActionBar.addTab(buildBluetoothServersTab()); - aActionBar.addTab(buildWiFiServersTab()); - } - - private ActionBar.Tab buildBluetoothServersTab() { - return buildActionBarTab(R.string.title_bluetooth); + aActionBar.addTab(buildActionBarTab(R.string.title_bluetooth)); + aActionBar.addTab(buildActionBarTab(R.string.title_wifi)); } private ActionBar.Tab buildActionBarTab(int aTitleResourceId) { @@ -69,13 +83,11 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio public void onTabReselected(ActionBar.Tab aTab, FragmentTransaction aTransaction) { } - private ActionBar.Tab buildWiFiServersTab() { - return buildActionBarTab(R.string.title_wifi); - } - private void setUpComputersPager() { - getComputersPager().setAdapter(buildComputersPagerAdapter()); - getComputersPager().setOnPageChangeListener(this); + ViewPager aComputersPager = getComputersPager(); + + aComputersPager.setAdapter(buildComputersPagerAdapter()); + aComputersPager.setOnPageChangeListener(this); } private PagerAdapter buildComputersPagerAdapter() { @@ -95,6 +107,12 @@ public class ComputersActivity extends SherlockFragmentActivity implements Actio public void onPageScrollStateChanged(int aPosition) { } + private void setUpComputersList() { + Fragment aComputersFragment = ComputersFragment.newInstance(ComputersFragment.Type.WIFI); + + FragmentOperator.setUpFragment(this, aComputersFragment); + } + @Override public boolean onCreateOptionsMenu(Menu aMenu) { getSupportMenuInflater().inflate(R.menu.menu_action_bar_computers, aMenu); diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java index 60cf3df9d289..a236ac59fa9a 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java @@ -22,6 +22,7 @@ import android.content.IntentFilter; import android.os.Handler; import android.support.v4.content.LocalBroadcastManager; +import org.libreoffice.impressremote.util.BluetoothOperator; import org.libreoffice.impressremote.util.Intents; import org.libreoffice.impressremote.communication.Server.Protocol; @@ -40,28 +41,23 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, @Override public void startSearch() { - if (!isBluetoothAvailable()) { + if (!BluetoothOperator.isAvailable()) { return; } - if (BluetoothAdapter.getDefaultAdapter().isDiscovering()) { + if (BluetoothOperator.getAdapter().isDiscovering()) { return; } setUpSearchResultsReceiver(); - BluetoothAdapter.getDefaultAdapter().startDiscovery(); - } - - private boolean isBluetoothAvailable() { - return BluetoothAdapter.getDefaultAdapter() != null; + BluetoothOperator.getAdapter().startDiscovery(); } private void setUpSearchResultsReceiver() { - IntentFilter aSearchResultsFilter = new IntentFilter( - BluetoothDevice.ACTION_FOUND); - aSearchResultsFilter - .addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + IntentFilter aSearchResultsFilter = new IntentFilter(); + aSearchResultsFilter.addAction(BluetoothDevice.ACTION_FOUND); + aSearchResultsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mContext.registerReceiver(this, aSearchResultsFilter); } @@ -73,15 +69,12 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); addServer(buildServer(aBluetoothDevice)); - callUpdatingServersList(); return; } - if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED - .equals(aIntent.getAction())) { - + if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) { startDiscoveryDelayed(); } } @@ -107,7 +100,7 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, // but check whether device is on in case the user manually // disabled bluetooth - if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { + if (!BluetoothOperator.getAdapter().isEnabled()) { return; } @@ -117,18 +110,18 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, @Override public void run() { - BluetoothAdapter.getDefaultAdapter().startDiscovery(); + BluetoothOperator.getAdapter().startDiscovery(); } @Override public void stopSearch() { - if (!isBluetoothAvailable()) { + if (!BluetoothOperator.isAvailable()) { return; } tearDownSearchResultsReceiver(); - BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); + BluetoothOperator.getAdapter().cancelDiscovery(); } private void tearDownSearchResultsReceiver() { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index bc11d927a207..4e94b6c9714a 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -32,6 +32,10 @@ public class Server implements Parcelable { return new Server(Protocol.TCP, aAddress, aName); } + public static Server newBluetoothInstance(String aAddress, String aName) { + return new Server(Protocol.BLUETOOTH, aAddress, aName); + } + public Protocol getProtocol() { return mProtocol; } diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java new file mode 100644 index 000000000000..5a1167e2286b --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java @@ -0,0 +1,26 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.libreoffice.impressremote.util; + +import android.bluetooth.BluetoothAdapter; + +public final class BluetoothOperator { + private BluetoothOperator() { + } + + public static boolean isAvailable() { + return BluetoothAdapter.getDefaultAdapter() != null; + } + + public static BluetoothAdapter getAdapter() { + return BluetoothAdapter.getDefaultAdapter(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/FragmentOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/FragmentOperator.java new file mode 100644 index 000000000000..3c6506307938 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/util/FragmentOperator.java @@ -0,0 +1,44 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.libreoffice.impressremote.util; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; + +public final class FragmentOperator { + private FragmentOperator() { + } + + public static void setUpFragment(FragmentActivity aActivity, Fragment aFragment) { + if (isFragmentSetUp(aActivity)) { + return; + } + + installFragment(aActivity, aFragment); + } + + private static boolean isFragmentSetUp(FragmentActivity aActivity) { + FragmentManager aFragmentManager = aActivity.getSupportFragmentManager(); + + return aFragmentManager.findFragmentById(android.R.id.content) != null; + } + + private static void installFragment(FragmentActivity aActivity, Fragment aFragment) { + FragmentManager aFragmentManager = aActivity.getSupportFragmentManager(); + FragmentTransaction aFragmentTransaction = aFragmentManager.beginTransaction(); + + aFragmentTransaction.add(android.R.id.content, aFragment); + + aFragmentTransaction.commit(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |