diff --git a/briar-android/res/values/strings.xml b/briar-android/res/values/strings.xml index b15bdf91c6c211e8b0a9ce4cab67c8df019359bf..2c6debaf45db035fd5d62bd30e3e7c6ad7c406a4 100644 --- a/briar-android/res/values/strings.xml +++ b/briar-android/res/values/strings.xml @@ -31,7 +31,7 @@ <string name="bluetooth_not_available">Bluetooth is NOT AVAILABLE</string> <string name="bluetooth_disabled">Bluetooth is OFF</string> <string name="bluetooth_not_discoverable">Bluetooth is NOT DISCOVERABLE</string> - <string name="bluetooth_enabled">Bluetooth is discoverable</string> + <string name="bluetooth_discoverable">Bluetooth is discoverable</string> <string name="fact_to_face">For security reasons you must be face-to-face with someone to add them as a contact</string> <string name="continue_button">Continue</string> <string name="your_invitation_code">Your invitation code is</string> diff --git a/briar-android/src/net/sf/briar/android/invitation/AddContactActivity.java b/briar-android/src/net/sf/briar/android/invitation/AddContactActivity.java index dac361defa6a21d61e22e1a2601390c0cd122be7..33a43d4ade6668c2d36ae2605c71fc09b82f0ec1 100644 --- a/briar-android/src/net/sf/briar/android/invitation/AddContactActivity.java +++ b/briar-android/src/net/sf/briar/android/invitation/AddContactActivity.java @@ -1,5 +1,10 @@ package net.sf.briar.android.invitation; +import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED; +import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED; +import static android.bluetooth.BluetoothAdapter.EXTRA_STATE; +import static android.bluetooth.BluetoothAdapter.STATE_ON; +import static android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; @@ -25,7 +30,13 @@ import net.sf.briar.api.invitation.InvitationListener; import net.sf.briar.api.invitation.InvitationState; import net.sf.briar.api.invitation.InvitationTask; import net.sf.briar.api.invitation.InvitationTaskFactory; +import android.bluetooth.BluetoothAdapter; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; import android.os.Bundle; import com.google.inject.Inject; @@ -48,7 +59,7 @@ implements InvitationListener { private long taskHandle = -1; private AuthorId localAuthorId = null; private String networkName = null; - private boolean useBluetooth = false; + private boolean bluetoothEnabled = false; private int localInvitationCode = -1, remoteInvitationCode = -1; private int localConfirmationCode = -1, remoteConfirmationCode = -1; private boolean connectionFailed = false; @@ -70,8 +81,6 @@ implements InvitationListener { // Restore the activity's state byte[] b = state.getByteArray("net.sf.briar.LOCAL_AUTHOR_ID"); if(b != null) localAuthorId = new AuthorId(b); - networkName = state.getString("net.sf.briar.NETWORK_NAME"); - useBluetooth = state.getBoolean("net.sf.briar.USE_BLUETOOTH"); taskHandle = state.getLong("net.sf.briar.TASK_HANDLE", -1); task = referenceManager.getReference(taskHandle, InvitationTask.class); @@ -133,6 +142,25 @@ implements InvitationListener { } } + // Listen for Bluetooth and WiFi state changes + IntentFilter filter = new IntentFilter(); + filter.addAction(ACTION_STATE_CHANGED); + filter.addAction(ACTION_SCAN_MODE_CHANGED); + filter.addAction(NETWORK_STATE_CHANGED_ACTION); + BluetoothWifiStateReceiver receiver = new BluetoothWifiStateReceiver(); + registerReceiver(receiver, filter); + + // Get the current Bluetooth and WiFi state + BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); + if(bluetooth != null) bluetoothEnabled = bluetooth.isEnabled(); + view.bluetoothStateChanged(); + WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE); + if(wifi != null && wifi.isWifiEnabled()) { + WifiInfo info = wifi.getConnectionInfo(); + if(info.getNetworkId() != -1) networkName = info.getSSID(); + } + view.wifiStateChanged(); + // Bind to the service so we can wait for it to start bindService(new Intent(BriarService.class.getName()), serviceConnection, 0); @@ -150,8 +178,6 @@ implements InvitationListener { state.putByteArray("net.sf.briar.LOCAL_AUTHOR_ID", localAuthorId.getBytes()); } - state.putString("net.sf.briar.NETWORK_NAME", networkName); - state.putBoolean("net.sf.briar.USE_BLUETOOTH", useBluetooth); state.putInt("net.sf.briar.LOCAL_CODE", localInvitationCode); state.putInt("net.sf.briar.REMOTE_CODE", remoteInvitationCode); state.putBoolean("net.sf.briar.FAILED", connectionFailed); @@ -174,7 +200,7 @@ implements InvitationListener { } void reset(AddContactView view) { - // Don't reset localAuthorId, networkName or useBluetooth + // Don't reset localAuthorId, networkName or bluetoothEnabled task = null; taskHandle = -1; localInvitationCode = -1; @@ -230,20 +256,12 @@ implements InvitationListener { return localAuthorId; } - void setNetworkName(String networkName) { - this.networkName = networkName; - } - String getNetworkName() { return networkName; } - void setUseBluetooth(boolean useBluetooth) { - this.useBluetooth = useBluetooth; - } - - boolean getUseBluetooth() { - return useBluetooth; + boolean isBluetoothEnabled() { + return bluetoothEnabled; } int getLocalInvitationCode() { @@ -341,6 +359,30 @@ implements InvitationListener { }); } + private class BluetoothWifiStateReceiver extends BroadcastReceiver { + + public void onReceive(Context ctx, Intent intent) { + String action = intent.getAction(); + if(action.equals(ACTION_STATE_CHANGED)) { + int state = intent.getIntExtra(EXTRA_STATE, 0); + bluetoothEnabled = state == STATE_ON; + view.bluetoothStateChanged(); + } else if(action.equals(ACTION_SCAN_MODE_CHANGED)) { + view.bluetoothStateChanged(); + } else if(action.equals(NETWORK_STATE_CHANGED_ACTION)) { + WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE); + if(wifi == null || !wifi.isWifiEnabled()) { + networkName = null; + } else { + WifiInfo info = wifi.getConnectionInfo(); + if(info.getNetworkId() == -1) networkName = null; + else networkName = info.getSSID(); + } + view.wifiStateChanged(); + } + } + } + /** * Cleans up the reference to the invitation task when the task completes. * This class is static to prevent memory leaks. diff --git a/briar-android/src/net/sf/briar/android/invitation/AddContactView.java b/briar-android/src/net/sf/briar/android/invitation/AddContactView.java index 1f84c0e620fac069b85200e0a157874f972662f4..cd3602b30156d5b3007df0ed99f44c7fe7c2f432 100644 --- a/briar-android/src/net/sf/briar/android/invitation/AddContactView.java +++ b/briar-android/src/net/sf/briar/android/invitation/AddContactView.java @@ -22,4 +22,8 @@ abstract class AddContactView extends LinearLayout { } abstract void populate(); + + void wifiStateChanged() {} + + void bluetoothStateChanged() {} } diff --git a/briar-android/src/net/sf/briar/android/invitation/BluetoothStateListener.java b/briar-android/src/net/sf/briar/android/invitation/BluetoothStateListener.java deleted file mode 100644 index fa7846c7c4d79bac01fa724623fdd80b25e8632d..0000000000000000000000000000000000000000 --- a/briar-android/src/net/sf/briar/android/invitation/BluetoothStateListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.sf.briar.android.invitation; - -interface BluetoothStateListener { - - void bluetoothStateChanged(boolean enabled); -} diff --git a/briar-android/src/net/sf/briar/android/invitation/BluetoothWidget.java b/briar-android/src/net/sf/briar/android/invitation/BluetoothWidget.java index 93b86f88cb25bf3aaaceffc1869ddd064ab94470..073c5923af16a37c3a105835cb14aedd17bf5c67 100644 --- a/briar-android/src/net/sf/briar/android/invitation/BluetoothWidget.java +++ b/briar-android/src/net/sf/briar/android/invitation/BluetoothWidget.java @@ -17,14 +17,11 @@ import android.widget.TextView; public class BluetoothWidget extends LinearLayout implements OnClickListener { - private BluetoothStateListener listener = null; - public BluetoothWidget(Context ctx) { super(ctx); } - void init(BluetoothStateListener listener) { - this.listener = listener; + void init() { setOrientation(HORIZONTAL); setGravity(CENTER); populate(); @@ -39,7 +36,6 @@ public class BluetoothWidget extends LinearLayout implements OnClickListener { status.setPadding(10, 10, 10, 10); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if(adapter == null) { - listener.bluetoothStateChanged(false); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); @@ -47,19 +43,17 @@ public class BluetoothWidget extends LinearLayout implements OnClickListener { status.setText(R.string.bluetooth_not_available); addView(status); } else if(adapter.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE) { - listener.bluetoothStateChanged(true); ImageView ok = new ImageView(ctx); ok.setPadding(5, 5, 5, 5); ok.setImageResource(R.drawable.navigation_accept); addView(ok); - status.setText(R.string.bluetooth_enabled); + status.setText(R.string.bluetooth_discoverable); addView(status); ImageButton settings = new ImageButton(ctx); settings.setImageResource(R.drawable.action_settings); settings.setOnClickListener(this); addView(settings); } else if(adapter.isEnabled()) { - listener.bluetoothStateChanged(true); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); @@ -71,7 +65,6 @@ public class BluetoothWidget extends LinearLayout implements OnClickListener { settings.setOnClickListener(this); addView(settings); } else { - listener.bluetoothStateChanged(false); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); diff --git a/briar-android/src/net/sf/briar/android/invitation/ConnectionFailedView.java b/briar-android/src/net/sf/briar/android/invitation/ConnectionFailedView.java index 6367173dd492cf27779148e77f79d856e5f2f5e8..658153b87e228849b3bc5ac8fe17b0da5760213f 100644 --- a/briar-android/src/net/sf/briar/android/invitation/ConnectionFailedView.java +++ b/briar-android/src/net/sf/briar/android/invitation/ConnectionFailedView.java @@ -12,8 +12,10 @@ import android.widget.LinearLayout; import android.widget.TextView; public class ConnectionFailedView extends AddContactView -implements WifiStateListener, BluetoothStateListener, OnClickListener { +implements OnClickListener { + private WifiWidget wifi = null; + private BluetoothWidget bluetooth = null; private Button tryAgainButton = null; ConnectionFailedView(Context ctx) { @@ -44,12 +46,12 @@ implements WifiStateListener, BluetoothStateListener, OnClickListener { checkNetwork.setText(R.string.check_same_network); addView(checkNetwork); - WifiWidget wifi = new WifiWidget(ctx); - wifi.init(this); + wifi = new WifiWidget(ctx); + wifi.init(); addView(wifi); - BluetoothWidget bluetooth = new BluetoothWidget(ctx); - bluetooth.init(this); + bluetooth = new BluetoothWidget(ctx); + bluetooth.init(); addView(bluetooth); tryAgainButton = new Button(ctx); @@ -60,29 +62,21 @@ implements WifiStateListener, BluetoothStateListener, OnClickListener { addView(tryAgainButton); } - public void wifiStateChanged(final String networkName) { - container.runOnUiThread(new Runnable() { - public void run() { - container.setNetworkName(networkName); - enableOrDisableTryAgainButton(); - } - }); + void wifiStateChanged() { + if(wifi != null) wifi.populate(); + enableOrDisableTryAgainButton(); } - public void bluetoothStateChanged(final boolean enabled) { - container.runOnUiThread(new Runnable() { - public void run() { - container.setUseBluetooth(enabled); - enableOrDisableTryAgainButton(); - } - }); + void bluetoothStateChanged() { + if(bluetooth != null) bluetooth.populate(); + enableOrDisableTryAgainButton(); } private void enableOrDisableTryAgainButton() { if(tryAgainButton == null) return; // Activity not created yet - boolean useBluetooth = container.getUseBluetooth(); + boolean bluetoothEnabled = container.isBluetoothEnabled(); String networkName = container.getNetworkName(); - tryAgainButton.setEnabled(useBluetooth || networkName != null); + tryAgainButton.setEnabled(bluetoothEnabled || networkName != null); } public void onClick(View view) { diff --git a/briar-android/src/net/sf/briar/android/invitation/ConnectionView.java b/briar-android/src/net/sf/briar/android/invitation/ConnectionView.java index ba5e089d1163e3c87ea81d639416afe812359514..6b137c4a534b85e1c17614392245c6ac91f359bb 100644 --- a/briar-android/src/net/sf/briar/android/invitation/ConnectionView.java +++ b/briar-android/src/net/sf/briar/android/invitation/ConnectionView.java @@ -52,7 +52,7 @@ public class ConnectionView extends AddContactView { addView(innerLayout); } - if(container.getUseBluetooth()) { + if(container.isBluetoothEnabled()) { LinearLayout innerLayout = new LinearLayout(ctx); innerLayout.setOrientation(HORIZONTAL); innerLayout.setGravity(CENTER); diff --git a/briar-android/src/net/sf/briar/android/invitation/NetworkSetupView.java b/briar-android/src/net/sf/briar/android/invitation/NetworkSetupView.java index 90343a0225c00dc34cc7eead64faedf135653d8e..5c1487cd71168b985a51e21db15072efbc62339e 100644 --- a/briar-android/src/net/sf/briar/android/invitation/NetworkSetupView.java +++ b/briar-android/src/net/sf/briar/android/invitation/NetworkSetupView.java @@ -21,11 +21,12 @@ import android.widget.Spinner; import android.widget.TextView; public class NetworkSetupView extends AddContactView -implements WifiStateListener, BluetoothStateListener, OnItemSelectedListener, -OnClickListener { +implements OnItemSelectedListener, OnClickListener { private LocalAuthorSpinnerAdapter adapter = null; private Spinner spinner = null; + private WifiWidget wifi = null; + private BluetoothWidget bluetooth = null; private Button continueButton = null; NetworkSetupView(Context ctx) { @@ -55,12 +56,12 @@ OnClickListener { innerLayout.addView(spinner); addView(innerLayout); - WifiWidget wifi = new WifiWidget(ctx); - wifi.init(this); + wifi = new WifiWidget(ctx); + wifi.init(); addView(wifi); - BluetoothWidget bluetooth = new BluetoothWidget(ctx); - bluetooth.init(this); + bluetooth = new BluetoothWidget(ctx); + bluetooth.init(); addView(bluetooth); TextView faceToFace = new TextView(ctx); @@ -78,30 +79,22 @@ OnClickListener { addView(continueButton); } - public void wifiStateChanged(final String networkName) { - container.runOnUiThread(new Runnable() { - public void run() { - container.setNetworkName(networkName); - enableOrDisableContinueButton(); - } - }); + void wifiStateChanged() { + if(wifi != null) wifi.populate(); + enableOrDisableContinueButton(); } - public void bluetoothStateChanged(final boolean enabled) { - container.runOnUiThread(new Runnable() { - public void run() { - container.setUseBluetooth(enabled); - enableOrDisableContinueButton(); - } - }); + void bluetoothStateChanged() { + if(bluetooth != null) bluetooth.populate(); + enableOrDisableContinueButton(); } private void enableOrDisableContinueButton() { if(continueButton == null) return; // Activity not created yet AuthorId localAuthorId = container.getLocalAuthorId(); - boolean useBluetooth = container.getUseBluetooth(); + boolean bluetoothEnabled = container.isBluetoothEnabled(); String networkName = container.getNetworkName(); - boolean networkAvailable = useBluetooth || networkName != null; + boolean networkAvailable = bluetoothEnabled || networkName != null; continueButton.setEnabled(localAuthorId != null && networkAvailable); } diff --git a/briar-android/src/net/sf/briar/android/invitation/WifiStateListener.java b/briar-android/src/net/sf/briar/android/invitation/WifiStateListener.java deleted file mode 100644 index c668b0d2d37df6b224445777eed662509b2347f4..0000000000000000000000000000000000000000 --- a/briar-android/src/net/sf/briar/android/invitation/WifiStateListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.sf.briar.android.invitation; - -interface WifiStateListener { - - void wifiStateChanged(String networkName); -} diff --git a/briar-android/src/net/sf/briar/android/invitation/WifiWidget.java b/briar-android/src/net/sf/briar/android/invitation/WifiWidget.java index 90f34854a7de513a9498dbe14716162f2ff9244c..22d49df3990bff59a02aab1bb22b70199cb50c2c 100644 --- a/briar-android/src/net/sf/briar/android/invitation/WifiWidget.java +++ b/briar-android/src/net/sf/briar/android/invitation/WifiWidget.java @@ -18,14 +18,11 @@ import android.widget.TextView; public class WifiWidget extends LinearLayout implements OnClickListener { - private WifiStateListener listener = null; - public WifiWidget(Context ctx) { super(ctx); } - void init(WifiStateListener listener) { - this.listener = listener; + void init() { setOrientation(HORIZONTAL); setGravity(CENTER); populate(); @@ -40,7 +37,6 @@ public class WifiWidget extends LinearLayout implements OnClickListener { status.setLayoutParams(WRAP_WRAP_1); WifiManager wifi = (WifiManager) ctx.getSystemService(WIFI_SERVICE); if(wifi == null) { - wifiStateChanged(null); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); @@ -52,7 +48,6 @@ public class WifiWidget extends LinearLayout implements OnClickListener { String networkName = info.getSSID(); int networkId = info.getNetworkId(); if(networkName == null || networkId == -1) { - wifiStateChanged(null); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); @@ -64,7 +59,6 @@ public class WifiWidget extends LinearLayout implements OnClickListener { settings.setOnClickListener(this); addView(settings); } else { - wifiStateChanged(networkName); ImageView ok = new ImageView(ctx); ok.setPadding(5, 5, 5, 5); ok.setImageResource(R.drawable.navigation_accept); @@ -79,7 +73,6 @@ public class WifiWidget extends LinearLayout implements OnClickListener { addView(settings); } } else { - wifiStateChanged(null); ImageView warning = new ImageView(ctx); warning.setPadding(5, 5, 5, 5); warning.setImageResource(R.drawable.alerts_and_states_warning); @@ -93,10 +86,6 @@ public class WifiWidget extends LinearLayout implements OnClickListener { } } - private void wifiStateChanged(String networkName) { - if(listener != null) listener.wifiStateChanged(networkName); - } - public void onClick(View view) { getContext().startActivity(new Intent(ACTION_WIFI_SETTINGS)); }