Clean up after merging swap stuff.
This commit is contained in:
parent
0a373de7a4
commit
4f4310c0ca
@ -26,15 +26,10 @@
|
|||||||
android:layout_below="@+id/text_description"
|
android:layout_below="@+id/text_description"
|
||||||
android:layout_centerHorizontal="true" />
|
android:layout_centerHorizontal="true" />
|
||||||
|
|
||||||
<Button style="@style/SwapTheme.Wizard.OptionButton"
|
|
||||||
android:id="@+id/btn_bluetooth"
|
|
||||||
android:text="@string/swap_use_bluetooth"
|
|
||||||
android:layout_alignParentBottom="true" />
|
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
<Button style="@style/SwapTheme.Wizard.OptionButton"
|
<Button style="@style/SwapTheme.Wizard.OptionButton"
|
||||||
android:text="@string/swap_wifi_help"
|
android:text="@string/swap_wifi_help"
|
||||||
android:layout_above="@id/btn_bluetooth"
|
android:layout_alignParentBottom="true"
|
||||||
android:id="@+id/btn_learn_more_about_wifi"/>
|
android:id="@+id/btn_learn_more_about_wifi"/>
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
@ -585,6 +585,7 @@ public class SwapService extends Service {
|
|||||||
private void enableSwappingAsynchronous() {
|
private void enableSwappingAsynchronous() {
|
||||||
webServerType.startInBackground();
|
webServerType.startInBackground();
|
||||||
bonjourType.startInBackground();
|
bonjourType.startInBackground();
|
||||||
|
bluetoothType.startInBackground();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@ package org.fdroid.fdroid.localrepo.peers;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.fdroid.fdroid.localrepo.SwapService;
|
import org.fdroid.fdroid.localrepo.SwapService;
|
||||||
@ -33,7 +34,7 @@ public abstract class PeerFinder<T extends Peer> {
|
|||||||
Log.i(TAG, "Found peer " + peer.getName());
|
Log.i(TAG, "Found peer " + peer.getName());
|
||||||
Intent intent = new Intent(SwapService.ACTION_PEER_FOUND);
|
Intent intent = new Intent(SwapService.ACTION_PEER_FOUND);
|
||||||
intent.putExtra(SwapService.EXTRA_PEER, peer);
|
intent.putExtra(SwapService.EXTRA_PEER, peer);
|
||||||
context.sendBroadcast(intent);
|
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void removePeer(T peer) {
|
protected void removePeer(T peer) {
|
||||||
|
@ -9,6 +9,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.fdroid.fdroid.localrepo.SwapService;
|
import org.fdroid.fdroid.localrepo.SwapService;
|
||||||
|
import org.fdroid.fdroid.net.bluetooth.BluetoothServer;
|
||||||
|
|
||||||
public class BluetoothType extends SwapType {
|
public class BluetoothType extends SwapType {
|
||||||
|
|
||||||
@ -17,6 +18,8 @@ public class BluetoothType extends SwapType {
|
|||||||
@NonNull
|
@NonNull
|
||||||
private final BluetoothAdapter adapter;
|
private final BluetoothAdapter adapter;
|
||||||
|
|
||||||
|
private final BluetoothServer server;
|
||||||
|
|
||||||
public static SwapType create(@NonNull Context context) {
|
public static SwapType create(@NonNull Context context) {
|
||||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
if (adapter == null) {
|
if (adapter == null) {
|
||||||
@ -29,6 +32,7 @@ public class BluetoothType extends SwapType {
|
|||||||
private BluetoothType(@NonNull Context context, @NonNull BluetoothAdapter adapter) {
|
private BluetoothType(@NonNull Context context, @NonNull BluetoothAdapter adapter) {
|
||||||
super(context);
|
super(context);
|
||||||
this.adapter = adapter;
|
this.adapter = adapter;
|
||||||
|
this.server = new BluetoothServer(context, context.getFilesDir());
|
||||||
|
|
||||||
context.registerReceiver(new BroadcastReceiver() {
|
context.registerReceiver(new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
@ -50,6 +54,11 @@ public class BluetoothType extends SwapType {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
|
if (server.isAlive()) {
|
||||||
|
Log.d(TAG, "Attempting to start Bluetooth swap, but it appears to be running already.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sendBroadcast(SwapService.EXTRA_STARTING);
|
sendBroadcast(SwapService.EXTRA_STARTING);
|
||||||
|
|
||||||
if (!adapter.isEnabled()) {
|
if (!adapter.isEnabled()) {
|
||||||
@ -60,26 +69,22 @@ public class BluetoothType extends SwapType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (adapter.isEnabled()) {
|
if (adapter.isEnabled()) {
|
||||||
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
server.start();
|
||||||
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
|
setConnected(true);
|
||||||
|
} else {
|
||||||
// TODO: Hmm, don't like the idea of a background service being able to do this :(
|
Log.i(TAG, "Didn't start Bluetooth swapping server, because Bluetooth is disabled and couldn't be enabled.");
|
||||||
discoverableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
setConnected(false);
|
||||||
|
|
||||||
// Can't get notified if they cancel this, because we are not an activity and thus
|
|
||||||
// can't start an activity for a result :(
|
|
||||||
context.startActivity(discoverableIntent);
|
|
||||||
|
|
||||||
// Don't setConnected(true) yet, wait for the broadcast to come from the BluetoothAdapter
|
|
||||||
// saying its state has changed.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
Log.d(TAG, "Making bluetooth non-discoverable.");
|
if (server.isAlive()) {
|
||||||
adapter.cancelDiscovery();
|
server.close();
|
||||||
setConnected(false);
|
setConnected(false);
|
||||||
|
} else {
|
||||||
|
Log.i(TAG, "Attempting to stop Bluetooth swap, but it is not currently running.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,6 +11,7 @@ public class DownloaderFactory {
|
|||||||
public static Downloader create(String url, Context context) throws IOException {
|
public static Downloader create(String url, Context context) throws IOException {
|
||||||
Uri uri = Uri.parse(url);
|
Uri uri = Uri.parse(url);
|
||||||
if (isBluetoothAddress(uri)) {
|
if (isBluetoothAddress(uri)) {
|
||||||
|
// TODO: Don't pass null!!!
|
||||||
return new BluetoothDownloader(null, uri.getPath(), context);
|
return new BluetoothDownloader(null, uri.getPath(), context);
|
||||||
} else if (isOnionAddress(url)) {
|
} else if (isOnionAddress(url)) {
|
||||||
return new TorHttpDownloader(url, context);
|
return new TorHttpDownloader(url, context);
|
||||||
@ -22,6 +23,7 @@ public class DownloaderFactory {
|
|||||||
public static Downloader create(String url, File destFile) throws IOException {
|
public static Downloader create(String url, File destFile) throws IOException {
|
||||||
Uri uri = Uri.parse(url);
|
Uri uri = Uri.parse(url);
|
||||||
if (isBluetoothAddress(uri)) {
|
if (isBluetoothAddress(uri)) {
|
||||||
|
// TODO: Don't pass null!!!
|
||||||
return new BluetoothDownloader(null, uri.getPath(), destFile);
|
return new BluetoothDownloader(null, uri.getPath(), destFile);
|
||||||
} else if (isOnionAddress(url)) {
|
} else if (isOnionAddress(url)) {
|
||||||
return new TorHttpDownloader(url, destFile);
|
return new TorHttpDownloader(url, destFile);
|
||||||
|
@ -56,6 +56,8 @@ public class BluetoothServer extends Thread {
|
|||||||
connection.interrupt();
|
connection.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interrupt();
|
||||||
|
|
||||||
if (serverSocket != null) {
|
if (serverSocket != null) {
|
||||||
Utils.closeQuietly(serverSocket);
|
Utils.closeQuietly(serverSocket);
|
||||||
}
|
}
|
||||||
@ -86,6 +88,10 @@ public class BluetoothServer extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (isInterrupted()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BluetoothSocket clientSocket = serverSocket.accept();
|
BluetoothSocket clientSocket = serverSocket.accept();
|
||||||
if (clientSocket != null && !isInterrupted()) {
|
if (clientSocket != null && !isInterrupted()) {
|
||||||
|
@ -72,16 +72,6 @@ public class JoinWifiView extends RelativeLayout implements SwapWorkflowActivity
|
|||||||
},
|
},
|
||||||
new IntentFilter(WifiStateChangeService.BROADCAST)
|
new IntentFilter(WifiStateChangeService.BROADCAST)
|
||||||
);
|
);
|
||||||
|
|
||||||
Button bluetooth = (Button)findViewById(R.id.btn_bluetooth);
|
|
||||||
bluetooth.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
getActivity().connectWithBluetooth();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Listen for "Connecting..." state and reflect that in the view too.
|
// TODO: Listen for "Connecting..." state and reflect that in the view too.
|
||||||
|
@ -216,7 +216,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
|
|||||||
@Override
|
@Override
|
||||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
getManager().ensureBluetoothDiscoverable();
|
getActivity().connectWithBluetooth();
|
||||||
textBluetoothVisible.setText(R.string.swap_visible_bluetooth);
|
textBluetoothVisible.setText(R.string.swap_visible_bluetooth);
|
||||||
viewBluetoothId.setVisibility(View.VISIBLE);
|
viewBluetoothId.setVisibility(View.VISIBLE);
|
||||||
uiUpdatePeersInfo();
|
uiUpdatePeersInfo();
|
||||||
|
@ -421,7 +421,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
Log.d(TAG, "User enabled Bluetooth, will make sure we are discoverable.");
|
Log.d(TAG, "User enabled Bluetooth, will make sure we are discoverable.");
|
||||||
ensureBluetoothDiscoverable();
|
ensureBluetoothDiscoverableThenStart();
|
||||||
} else {
|
} else {
|
||||||
// Didn't enable bluetooth
|
// Didn't enable bluetooth
|
||||||
Log.d(TAG, "User chose not to enable Bluetooth, so doing nothing (i.e. sticking with wifi).");
|
Log.d(TAG, "User chose not to enable Bluetooth, so doing nothing (i.e. sticking with wifi).");
|
||||||
@ -458,7 +458,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
if (adapter != null)
|
if (adapter != null)
|
||||||
if (adapter.isEnabled()) {
|
if (adapter.isEnabled()) {
|
||||||
Log.d(TAG, "Bluetooth enabled, will pair with device.");
|
Log.d(TAG, "Bluetooth enabled, will pair with device.");
|
||||||
ensureBluetoothDiscoverable();
|
ensureBluetoothDiscoverableThenStart();
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Bluetooth disabled, asking user to enable it.");
|
Log.d(TAG, "Bluetooth disabled, asking user to enable it.");
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
@ -466,7 +466,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureBluetoothDiscoverable() {
|
private void ensureBluetoothDiscoverableThenStart() {
|
||||||
Log.d(TAG, "Ensuring Bluetooth is in discoverable mode.");
|
Log.d(TAG, "Ensuring Bluetooth is in discoverable mode.");
|
||||||
if (BluetoothAdapter.getDefaultAdapter().getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
if (BluetoothAdapter.getDefaultAdapter().getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||||
|
|
||||||
@ -475,13 +475,15 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
Log.d(TAG, "Not currently in discoverable mode, so prompting user to enable.");
|
Log.d(TAG, "Not currently in discoverable mode, so prompting user to enable.");
|
||||||
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||||
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
|
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); // TODO: What about when this expires? What if user manually disables discovery?
|
||||||
startActivityForResult(intent, REQUEST_BLUETOOTH_DISCOVERABLE);
|
startActivityForResult(intent, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(TAG, "Staring the Bluetooth Server whether we are discoverable or not, since paired devices can still connect.");
|
if (service == null) {
|
||||||
startBluetoothServer();
|
throw new IllegalStateException("Can't start Bluetooth swap because service is null for some strange reason.");
|
||||||
|
}
|
||||||
|
|
||||||
|
service.getBluetooth().startInBackground();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startBluetoothServer() {
|
private void startBluetoothServer() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user