Kaydet (Commit) ff5c1135 authored tarafından Artur Dryomov's avatar Artur Dryomov

Add auto-hiding tabs when Bluetooth is not available.

Introduce a helper class for Bluetooth as well.

Change-Id: I89b0b4c42ef56ce3f5c2be3a1ea9d443aec04fce
üst d798d26b
......@@ -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);
......
......@@ -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() {
......
......@@ -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;
}
......
/* -*- 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: */
/* -*- 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: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment