Skip to content

Commit

Permalink
move wifi/bluetooth detection into RequestHandler
Browse files Browse the repository at this point in the history
this moves more functionalities closer together and needs to
introduces a new door reply state called DISABLED
  • Loading branch information
mwarning committed Mar 25, 2020
1 parent 4e07d6a commit 318ad4a
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 94 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/example/trigger/BluetoothDoorSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ public DoorState parseReply(DoorReply reply) {
} else {
return new DoorState(StateCode.UNKNOWN, msg);
}
default:
// should not happen
return new DoorState(StateCode.UNKNOWN, msg);
case DISABLED:
return new DoorState(StateCode.DISABLED, msg);
}

// keep compiler quiet :/
return null;
}

@Override
Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/com/example/trigger/BluetoothTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.example.trigger.Log;


public class BluetoothTools {
static final String TAG = "BluetoothTools";
private BluetoothAdapter bluetoothAdapter;
private static BluetoothAdapter adapter;

BluetoothTools(Context context) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
static void init(Context context) {
adapter = BluetoothAdapter.getDefaultAdapter();
}

public boolean isEnabled() {
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
public static boolean isEnabled() {
return adapter != null && adapter.isEnabled();
}

public static boolean isSupported() {
return (adapter != null);
}

public static BluetoothSocket createRfcommSocket(BluetoothDevice device) {
Expand Down
7 changes: 2 additions & 5 deletions app/src/main/java/com/example/trigger/DoorReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ public class DoorReply {
public enum ReplyCode {
LOCAL_ERROR, // could establish a connection for some reason
REMOTE_ERROR, // the door send some error
SUCCESS // the door send some message that has yet to be parsed
SUCCESS, // the door send some message that has yet to be parsed
DISABLED // Internet, WiFi or Bluetooth disabled or not supported
}

public DoorReply(ReplyCode code, String message) {
this.code = code;
this.message = message;
}

public static DoorReply internal_error() {
return new DoorReply(ReplyCode.LOCAL_ERROR, "Internal Error");
}
}
8 changes: 5 additions & 3 deletions app/src/main/java/com/example/trigger/HttpsDoorSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ public DoorState parseReply(DoorReply reply) {
} else {
return new DoorState(StateCode.UNKNOWN, msg);
}
default:
// should not happen
return new DoorState(StateCode.UNKNOWN, msg);
case DISABLED:
return new DoorState(StateCode.DISABLED, msg);
}

// keep compiler quiet :/
return null;
}

@Override
Expand Down
45 changes: 5 additions & 40 deletions app/src/main/java/com/example/trigger/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public class MainActivity extends AppCompatActivity implements OnTaskCompleted {
private ImageButton ringButton;
private ImageButton unlockButton;
private Spinner spinner;
private WifiTools wifi;
private BluetoothTools bluetooth;

private Animation pressed;

Expand Down Expand Up @@ -101,7 +99,7 @@ private int getPreferredSpinnerIndex(ArrayList<SpinnerItem> items, boolean match
int i;

// select by ssid
String ssid = wifi.getCurrentSSID();
String ssid = WifiTools.getCurrentSSID();
if (ssid.length() > 0 && match_ssid) {
i = 0;
for (SpinnerItem item : items) {
Expand Down Expand Up @@ -218,8 +216,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

Context context = this.getApplicationContext();
this.wifi = new WifiTools(context);
this.bluetooth = new BluetoothTools(context);
WifiTools.init(context);
BluetoothTools.init(context);
Settings.init(context);

Resources res = getResources();
Expand Down Expand Up @@ -263,38 +261,7 @@ protected void onPause() {
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Setup current = getSelectedSetup();

if (current == null) {
changeUI(StateCode.DISABLED);
} else if (current instanceof BluetoothDoorSetup || current instanceof NukiDoorSetup) {
// assume the current setup uses Bluetooth based!
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
if (state == BluetoothAdapter.STATE_ON) {
Log.d("MainActivity", "bluetooth turned on");
changeUI(StateCode.UNKNOWN);
updateSpinner(true); // auto select possible entry
callRequestHandler(Action.fetch_state);
}
if (state == BluetoothAdapter.STATE_OFF) {
Log.d("MainActivity", "bluetooth state unknown => " + state);
changeUI(StateCode.DISABLED);
}
}
} else {
// assume the current setup uses WiFi based!
Log.d("MainActivity", "wifi turned on");
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) && wifi.isConnected()) {
updateSpinner(true); // auto select possible entry
callRequestHandler(Action.fetch_state);
} else {
Log.d("MainActivity", "wifi state unknown => disabled");
changeUI(StateCode.DISABLED);
}
}
callRequestHandler(Action.fetch_state);
}
};

Expand Down Expand Up @@ -414,9 +381,7 @@ private void callRequestHandler(Action action) {
protected void onStart() {
super.onStart();

if (wifi.isConnected()) {
callRequestHandler(Action.fetch_state);
}
callRequestHandler(Action.fetch_state);
}

// Show/Hide menu items
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/example/trigger/MqttDoorSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public DoorState parseReply(DoorReply reply) {
} else {
return new DoorState(StateCode.UNKNOWN, msg);
}
default:
// should not happen
return new DoorState(StateCode.UNKNOWN, msg);
case DISABLED:
return new DoorState(StateCode.DISABLED, msg);
}

// keep compiler quiet :/
return null;
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/example/trigger/NukiDoorSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ public DoorState parseReply(DoorReply reply) {
} else {
return new DoorState(StateCode.UNKNOWN, msg);
}
default:
// should not happen
return new DoorState(StateCode.UNKNOWN, msg);
case DISABLED:
return new DoorState(StateCode.DISABLED, msg);
}

// keep compiler quiet :/
return null;
}

@Override
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/java/com/example/trigger/SshDoorSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,26 @@ public String getRegisterUrl() {
public DoorState parseReply(DoorReply reply) {
String msg = android.text.Html.fromHtml(reply.message).toString().trim();

if (reply.message.contains("UNLOCKED")) {
// door unlocked
return new DoorState(StateCode.OPEN, msg);
} else if (reply.message.contains("LOCKED")) {
// door locked
return new DoorState(StateCode.CLOSED, msg);
} else {
return new DoorState(StateCode.UNKNOWN, msg);
switch (reply.code) {
case LOCAL_ERROR:
case REMOTE_ERROR:
return new DoorState(StateCode.UNKNOWN, msg);
case SUCCESS:
if (reply.message.contains("UNLOCKED")) {
// door unlocked
return new DoorState(StateCode.OPEN, msg);
} else if (reply.message.contains("LOCKED")) {
// door locked
return new DoorState(StateCode.CLOSED, msg);
} else {
return new DoorState(StateCode.UNKNOWN, msg);
}
case DISABLED:
return new DoorState(StateCode.DISABLED, msg);
}

// keep compiler quiet :/
return null;
}

@Override
Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/example/trigger/WifiTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


public class WifiTools {
private WifiManager wifiManager;
private static WifiManager wifiManager;

WifiTools(Context context) {
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
static void init(Context context) {
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}

String getCurrentSSID() {
public static String getCurrentSSID() {
// From android 8.0 only available if GPS on?!
if (wifiManager != null) {
WifiInfo info = wifiManager.getConnectionInfo();
Expand All @@ -34,11 +34,11 @@ String getCurrentSSID() {
}
}

ArrayList<String> getScannedSSIDs() {
public static ArrayList<String> getScannedSSIDs() {
ArrayList<String> ssids;
List<ScanResult> results;

ssids = new ArrayList();
ssids = new ArrayList<>();
if (wifiManager != null) {
results = wifiManager.getScanResults();
if (results != null) {
Expand All @@ -51,11 +51,11 @@ ArrayList<String> getScannedSSIDs() {
return ssids;
}

ArrayList<String> getConfiguredSSIDs() {
public static ArrayList<String> getConfiguredSSIDs() {
List<WifiConfiguration> configs;
ArrayList<String> ssids;

ssids = new ArrayList();
ssids = new ArrayList<>();
if (wifiManager != null) {
configs = wifiManager.getConfiguredNetworks();
if (configs != null) {
Expand All @@ -68,7 +68,7 @@ ArrayList<String> getConfiguredSSIDs() {
return ssids;
}

WifiConfiguration findConfig(List<WifiConfiguration> configs, String ssid) {
public static WifiConfiguration findConfig(List<WifiConfiguration> configs, String ssid) {
for (WifiConfiguration config : configs) {
if (config.SSID.equals(ssid)) {
return config;
Expand Down Expand Up @@ -110,7 +110,7 @@ void connectBestOf(ArrayList<String> ssids) {
}
}
*/
boolean isConnected() {
public static boolean isConnected() {
if (wifiManager != null && wifiManager.isWifiEnabled()) {
// Wi-Fi adapter is ON
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ public void run() {
return;
}

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

if (bluetooth == null) {
this.listener.onTaskResult(setup.getId(), ReplyCode.LOCAL_ERROR, "Device does not support bluetooth");
if (adapter == null) {
this.listener.onTaskResult(setup.getId(), ReplyCode.DISABLED, "Device does not support Bluetooth");
return;
} else if (!bluetooth.isEnabled()) {
}

if (!adapter.isEnabled()) {
// request to enable
this.listener.onTaskResult(setup.getId(), ReplyCode.LOCAL_ERROR, "Bluetooth is disabled.");
this.listener.onTaskResult(setup.getId(), ReplyCode.DISABLED, "Bluetooth is disabled.");
return;
}

Expand Down Expand Up @@ -71,7 +73,7 @@ public void run() {
}

try {
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();

String address = "";
for (BluetoothDevice device : pairedDevices) {
Expand All @@ -86,7 +88,7 @@ public void run() {
return;
}

BluetoothDevice device = bluetooth.getRemoteDevice(address);
BluetoothDevice device = adapter.getRemoteDevice(address);

if (setup.service_uuid.isEmpty()) {
socket = BluetoothTools.createRfcommSocket(device);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.trigger.https;

import android.content.Context;

import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -15,6 +17,7 @@
import com.example.trigger.OnTaskCompleted;
import com.example.trigger.Utils;
import com.example.trigger.Log;
import com.example.trigger.WifiTools;


public class HttpsRequestHandler extends Thread {
Expand All @@ -34,6 +37,11 @@ public void run() {
return;
}

if (!WifiTools.isConnected()) {
this.listener.onTaskResult(setup.getId(), ReplyCode.DISABLED, "Wifi Disabled.");
return;
}

String command = "";

switch (action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.example.trigger.DoorReply.ReplyCode;
import com.example.trigger.OnTaskCompleted;
import com.example.trigger.Log;
import com.example.trigger.WifiTools;


public class MqttRequestHandler extends Thread implements MqttCallback {
Expand All @@ -36,6 +37,11 @@ public void run() {
return;
}

if (!WifiTools.isConnected()) {
this.listener.onTaskResult(setup.getId(), ReplyCode.DISABLED, "Wifi Disabled.");
return;
}

switch (action) {
case fetch_state:
if (setup.status_topic.isEmpty()) {
Expand Down
Loading

0 comments on commit 318ad4a

Please sign in to comment.