diff --git a/app/src/main/java/io/pslab/activity/MainActivity.java b/app/src/main/java/io/pslab/activity/MainActivity.java index a7a8017ae..a15250bea 100644 --- a/app/src/main/java/io/pslab/activity/MainActivity.java +++ b/app/src/main/java/io/pslab/activity/MainActivity.java @@ -1,5 +1,6 @@ package io.pslab.activity; +import static io.pslab.others.ScienceLabCommon.isWifiConnected; import static io.pslab.others.ScienceLabCommon.scienceLab; import android.app.PendingIntent; @@ -10,6 +11,7 @@ import android.content.IntentFilter; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; +import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.util.Log; @@ -55,6 +57,7 @@ import io.pslab.others.InitializationVariable; import io.pslab.others.ScienceLabCommon; import io.pslab.receivers.USBDetachReceiver; +import io.pslab.receivers.WifiDisconnectReceiver; public class MainActivity extends AppCompatActivity { @@ -136,6 +139,11 @@ protected void onCreate(Bundle savedInstanceState) { usbDetachReceiver = new USBDetachReceiver(this); registerReceiver(usbDetachReceiver, usbDetachFilter); + IntentFilter wifiDisconnectFilter = new IntentFilter(); + wifiDisconnectFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); + WifiDisconnectReceiver wifiDisconnectReceiver = new WifiDisconnectReceiver(this); + registerReceiver(wifiDisconnectReceiver, wifiDisconnectFilter); + setSupportActionBar(toolbar); mHandler = new Handler(); @@ -414,7 +422,7 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case R.id.menu_pslab_connected: + case R.id.menu_wifi_connected, R.id.menu_pslab_connected: CustomSnackBar.showSnackBar(findViewById(android.R.id.content), getString(R.string.device_connected_successfully), null, null, Snackbar.LENGTH_SHORT); break; @@ -496,8 +504,9 @@ private void attemptToGetUSBPermission() { @Override public boolean onPrepareOptionsMenu(Menu menu) { - menu.getItem(0).setVisible(PSLabisConnected); - menu.getItem(1).setVisible(!PSLabisConnected); + menu.getItem(0).setVisible(isWifiConnected); + menu.getItem(1).setVisible(PSLabisConnected); + menu.getItem(2).setVisible(!PSLabisConnected && !isWifiConnected); setPSLabVersionIDs(); return true; } diff --git a/app/src/main/java/io/pslab/communication/CommunicationHandler.java b/app/src/main/java/io/pslab/communication/CommunicationHandler.java index 14a1ad618..8b7f36c14 100644 --- a/app/src/main/java/io/pslab/communication/CommunicationHandler.java +++ b/app/src/main/java/io/pslab/communication/CommunicationHandler.java @@ -69,8 +69,7 @@ public void open(int baudRate) throws IOException { } if (mUsbDevice.getProductId() == PSLAB_PRODUCT_ID_V6 && mUsbDevice.getVendorId() == PSLAB_VENDOR_ID_V6) { PSLAB_VERSION = 6; - } - else { + } else { PSLAB_VERSION = 5; } connected = true; @@ -102,7 +101,7 @@ public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOExce } int numBytesRead = 0; int readNow; - Log.v(TAG, "TO read : " + bytesToBeRead); + Log.v(TAG, "To read : " + bytesToBeRead); int bytesToBeReadTemp = bytesToBeRead; while (numBytesRead < bytesToBeRead) { readNow = port.read(mReadBuffer, bytesToBeReadTemp, timeoutMillis); @@ -123,7 +122,7 @@ public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOExce public int readCdcAcm(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOException { int numBytesRead = 0; int readNow; - Log.v(TAG, "TO read : " + bytesToBeRead); + Log.v(TAG, "To read : " + bytesToBeRead); int bytesToBeReadTemp = bytesToBeRead; while (numBytesRead < bytesToBeRead) { readNow = mConnection.bulkTransfer(mUsbDevice.getInterface(1).getEndpoint(1), mReadBuffer, bytesToBeReadTemp, timeoutMillis); diff --git a/app/src/main/java/io/pslab/communication/HttpAsyncTask.java b/app/src/main/java/io/pslab/communication/HttpAsyncTask.java deleted file mode 100644 index 4aae41545..000000000 --- a/app/src/main/java/io/pslab/communication/HttpAsyncTask.java +++ /dev/null @@ -1,43 +0,0 @@ -package io.pslab.communication; - -import android.os.AsyncTask; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.IOException; - -import io.pslab.interfaces.HttpCallback; - -public class HttpAsyncTask extends AsyncTask { - - private HttpHandler mHttpHandler; - private HttpCallback mHttpCallback; - - public HttpAsyncTask(String baseIP, HttpCallback httpCallback) { - mHttpHandler = new HttpHandler(baseIP); - mHttpCallback = httpCallback; - } - - @Override - protected Void doInBackground(byte[]... data) { - int res = 0; - try { - if (data.length != 0) { - res = mHttpHandler.write(data[0]); - - } else { - res = mHttpHandler.read(); - } - } catch (IOException | JSONException e) { - mHttpCallback.error(e); - e.printStackTrace(); - } - if (res == 1) { - mHttpCallback.success(mHttpHandler.getReceivedData()); - } else { - mHttpCallback.error(new Exception()); - } - return null; - } -} diff --git a/app/src/main/java/io/pslab/communication/HttpHandler.java b/app/src/main/java/io/pslab/communication/HttpHandler.java deleted file mode 100644 index a2f7bed38..000000000 --- a/app/src/main/java/io/pslab/communication/HttpHandler.java +++ /dev/null @@ -1,87 +0,0 @@ -package io.pslab.communication; - -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.IOException; -import java.net.URL; - -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - -public class HttpHandler { - - private final String TAG = this.getClass().getSimpleName(); - private String baseIP; - private String sendDataEndPoint = "send"; - private String getDataEndPoint = "get"; - private String dataKeyString = "data"; - private OkHttpClient client; - private JSONObject receivedData; - - public HttpHandler(String baseIP) { - this.baseIP = baseIP; - this.client = new OkHttpClient(); - } - - /** - * Method to send data to ESP - * - * @param data data to be sent in byte array - * @return 1 if response code is "200" 0 otherwise; - */ - public int write(byte[] data) throws IOException, JSONException { - int result = 1; - URL baseURL = new URL("http://" + baseIP + "/" + sendDataEndPoint); - int written = 0; - JSONArray responseArray = new JSONArray(); - while (written < data.length) { - JSONObject jsonObject = new JSONObject(); - jsonObject.put(dataKeyString, data[written]); - RequestBody body = RequestBody.create(jsonObject.toString(), MediaType.get("application/json; charset=utf-8")); - Request request = new Request.Builder() - .url(baseURL) - .post(body) - .build(); - Response response = client.newCall(request).execute(); - responseArray.put(new JSONObject(response.body().string())); - if (response.code() != 200) { - Log.e(TAG, "Error writing byte:" + written); - return 0; - } - written++; - } - receivedData = new JSONObject(responseArray.toString()); - return result; - } - - /** - * Method to get data from ESP - * @return 1 if data was received 0 otherwise - */ - public int read() throws IOException, JSONException { - int result = 1; - URL baseURL = new URL("http://" + baseIP + "/" + getDataEndPoint); - Request request = new Request.Builder() - .url(baseURL) - .build(); - Response response = client.newCall(request).execute(); - if (response.code() != 200) { - Log.e(TAG, "Error reading data"); - return 0; - } else { - receivedData = new JSONObject(response.body().string()); - } - return result; - } - - public JSONObject getReceivedData() { - return receivedData; - } -} diff --git a/app/src/main/java/io/pslab/communication/PacketHandler.java b/app/src/main/java/io/pslab/communication/PacketHandler.java index 709b26659..ece99cf4a 100644 --- a/app/src/main/java/io/pslab/communication/PacketHandler.java +++ b/app/src/main/java/io/pslab/communication/PacketHandler.java @@ -2,9 +2,6 @@ import android.util.Log; -import org.json.JSONException; -import org.json.JSONObject; - import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -13,8 +10,10 @@ import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; -import io.pslab.interfaces.HttpCallback; import io.pslab.others.ScienceLabCommon; /** @@ -34,15 +33,19 @@ public class PacketHandler { private int timeout = 500, VERSION_STRING_LENGTH = 8, FW_VERSION_LENGTH = 3; public static int PSLAB_FW_VERSION = 0; ByteBuffer burstBuffer = ByteBuffer.allocate(2000); - private HttpAsyncTask httpAsyncTask; + private SocketClient socketClient; + ExecutorService executor = Executors.newSingleThreadExecutor(); public PacketHandler(int timeout, CommunicationHandler communicationHandler) { this.loadBurst = false; this.connected = false; this.timeout = timeout; this.mCommandsProto = new CommandsProto(); - this.mCommunicationHandler = communicationHandler; - connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected()); + socketClient = SocketClient.getInstance(); + if (communicationHandler != null) { + this.mCommunicationHandler = communicationHandler; + } + connected = (ScienceLabCommon.isWifiConnected() || mCommunicationHandler.isConnected()); } public boolean isConnected() { @@ -247,49 +250,58 @@ public byte[] sendBurst() { public int commonRead(int bytesToRead) throws IOException { final int[] bytesRead = {0}; - if (mCommunicationHandler.isConnected()) { + if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) { bytesRead[0] = mCommunicationHandler.read(buffer, bytesToRead, timeout); } else if (ScienceLabCommon.isWifiConnected()) { - httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { - @Override - public void success(JSONObject jsonObject) { - try { - //Server will send byte array - buffer = (byte[]) jsonObject.get("data"); - bytesRead[0] = buffer.length; - } catch (JSONException e) { - e.printStackTrace(); - } - } - - @Override - public void error(Exception e) { + Future future = executor.submit(() -> { + try { + socketClient.read(bytesToRead); + System.arraycopy(socketClient.getReceivedData(), 0, buffer, 0, bytesToRead); + bytesRead[0] = bytesToRead; + } catch (Exception e) { Log.e(TAG, "Error reading data over ESP"); } + return null; }); - httpAsyncTask.execute(new byte[]{}); + try { + future.get(); + } catch (Exception e) { + throw new IOException("Error reading data", e); + } } return bytesRead[0]; } public void commonWrite(byte[] data) throws IOException { - if (mCommunicationHandler.isConnected()) { + if (mCommunicationHandler != null && mCommunicationHandler.isConnected()) { mCommunicationHandler.write(data, timeout); } else if (ScienceLabCommon.isWifiConnected()) { - httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { - @Override - public void success(JSONObject jsonObject) { - Log.v(TAG, "write response:" + jsonObject.toString()); - } - - @Override - public void error(Exception e) { + Future future = executor.submit(() -> { + try { + socketClient.write(data); + } catch (Exception e) { Log.e(TAG, "Error writing data over ESP"); } + return null; }); - - httpAsyncTask.execute(data); + try { + future.get(); + } catch (Exception e) { + throw new IOException("Error writing data", e); + } } + } + public void close() { + try { + if (mCommunicationHandler != null) { + mCommunicationHandler.close(); + } else { + socketClient.closeConnection(); + } + executor.shutdown(); + } catch (Exception e) { + Log.e(TAG, "Error closing connection"); + } } -} +} \ No newline at end of file diff --git a/app/src/main/java/io/pslab/communication/ScienceLab.java b/app/src/main/java/io/pslab/communication/ScienceLab.java index 07e94466b..a6173bd20 100644 --- a/app/src/main/java/io/pslab/communication/ScienceLab.java +++ b/app/src/main/java/io/pslab/communication/ScienceLab.java @@ -36,6 +36,7 @@ import io.pslab.communication.peripherals.I2C; import io.pslab.fragment.HomeFragment; import io.pslab.others.InitializationVariable; +import io.pslab.others.ScienceLabCommon; /** * Created by viveksb007 on 28/3/17. @@ -79,15 +80,19 @@ public class ScienceLab { public ScienceLab(CommunicationHandler communicationHandler) { mCommandsProto = new CommandsProto(); mAnalogConstants = new AnalogConstants(); - mCommunicationHandler = communicationHandler; - if (isDeviceFound() && MainActivity.hasPermission) { - try { - mCommunicationHandler.open(1000000); - //Thread.sleep(200); - mPacketHandler = new PacketHandler(50, mCommunicationHandler); - } catch (IOException | NullPointerException e) { - e.printStackTrace(); + if (communicationHandler != null) { + mCommunicationHandler = communicationHandler; + if (isDeviceFound() && MainActivity.hasPermission) { + try { + mCommunicationHandler.open(1000000); + //Thread.sleep(200); + mPacketHandler = new PacketHandler(50, mCommunicationHandler); + } catch (IOException | NullPointerException e) { + e.printStackTrace(); + } } + } else { + mPacketHandler = new PacketHandler(50, null); } if (isConnected()) { initializeVariables(); @@ -228,11 +233,7 @@ public void run() { } public void close() { - try { - mCommunicationHandler.close(); - } catch (IOException e) { - e.printStackTrace(); - } + mPacketHandler.close(); } private void captureFullSpeedHrInitialize(String channel, int samples, double timeGap, List args) { @@ -736,6 +737,9 @@ public void setDataSplitting(int dataSplitting) { * @return true is device found; false otherwise */ public boolean isDeviceFound() { + if (mCommunicationHandler == null) { + return false; + } return mCommunicationHandler.isDeviceFound(); } @@ -745,7 +749,8 @@ public boolean isDeviceFound() { * @return true is device is connected; false otherwise */ public boolean isConnected() { - return mCommunicationHandler.isConnected(); + return ScienceLabCommon.isWifiConnected || + (mCommunicationHandler != null && mCommunicationHandler.isConnected()); } /* DIGITAL SECTION */ @@ -2133,7 +2138,7 @@ public void resetDevice() { * @throws IOException * @throws InterruptedException */ - public void enterBootloader() throws IOException, InterruptedException { + public void enterBootloader() throws Exception { mCommunicationHandler.close(); mCommunicationHandler.open(460800); mPacketHandler = new PacketHandler(50, mCommunicationHandler); diff --git a/app/src/main/java/io/pslab/communication/SocketClient.java b/app/src/main/java/io/pslab/communication/SocketClient.java new file mode 100644 index 000000000..03c8b32d5 --- /dev/null +++ b/app/src/main/java/io/pslab/communication/SocketClient.java @@ -0,0 +1,95 @@ +package io.pslab.communication; + +import android.util.Log; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class SocketClient { + + public static final String TAG = "SocketClient"; + private static SocketClient socketClient = null; + private Socket socket; + private OutputStream outputStream; + private InputStream inputStream; + private boolean isConnected = false; + + public static final int DEFAULT_READ_BUFFER_SIZE = 32 * 1024; + + private byte[] buffer = new byte[DEFAULT_READ_BUFFER_SIZE]; + + private byte[] receivedData; + + private SocketClient() { + } + + public void openConnection(String ip, int port) throws IOException { + socket = new Socket(ip, port); + outputStream = socket.getOutputStream(); + inputStream = socket.getInputStream(); + if (!socket.isConnected()) { + isConnected = false; + return; + } + isConnected = true; + socket.setTcpNoDelay(true); + socket.setKeepAlive(true); + } + + public static SocketClient getInstance() { + if (socketClient == null) { + socketClient = new SocketClient(); + } + return socketClient; + } + + public boolean isConnected() { + return isConnected; + } + + public synchronized void write(byte[] data) throws IOException { + if (isConnected && socketClient.isConnected && outputStream != null) { + outputStream.write(data); + } + } + + public synchronized int read(int bytesToBeRead) throws IOException { + int numBytesRead = 0; + int readNow; + Log.v(TAG, "To read : " + bytesToBeRead); + int bytesToBeReadTemp = bytesToBeRead; + receivedData = new byte[DEFAULT_READ_BUFFER_SIZE]; + while (numBytesRead < bytesToBeRead) { + readNow = inputStream.read(buffer, 0, bytesToBeReadTemp); + if (readNow <= 0) { + Log.e(TAG, "Read Error: " + bytesToBeReadTemp); + return numBytesRead; + } else { + System.arraycopy(buffer, 0, receivedData, numBytesRead, readNow); + numBytesRead += readNow; + bytesToBeReadTemp -= readNow; + } + } + Log.v("Bytes Read", "" + numBytesRead); + return numBytesRead; + } + + public byte[] getReceivedData() { + return receivedData; + } + + public void closeConnection() { + try { + if (isConnected) { + inputStream.close(); + outputStream.close(); + socket.close(); + isConnected = false; + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/io/pslab/fragment/ESPFragment.java b/app/src/main/java/io/pslab/fragment/ESPFragment.java index 07e0e3132..78bec4286 100644 --- a/app/src/main/java/io/pslab/fragment/ESPFragment.java +++ b/app/src/main/java/io/pslab/fragment/ESPFragment.java @@ -19,14 +19,11 @@ import com.google.android.material.snackbar.Snackbar; -import java.io.IOException; import io.pslab.R; +import io.pslab.communication.SocketClient; import io.pslab.others.CustomSnackBar; import io.pslab.others.ScienceLabCommon; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; public class ESPFragment extends DialogFragment { private static final String TAG = ESPFragment.class.getSimpleName(); @@ -77,7 +74,7 @@ public void onResume() { getDialog().getWindow().setAttributes(params); } - private class ESPTask extends AsyncTask { + private class ESPTask extends AsyncTask { @Override protected void onPreExecute() { @@ -86,36 +83,35 @@ protected void onPreExecute() { } @Override - protected String doInBackground(Void... voids) { - String result = ""; + protected Boolean doInBackground(Void... voids) { try { - OkHttpClient client = new OkHttpClient(); - Request request = new Request.Builder() - .url("http://" + espIPAddress) - .build(); - try (Response response = client.newCall(request).execute()) { - if (response.code() == 200) { - ScienceLabCommon.setIsWifiConnected(true); - ScienceLabCommon.setEspBaseIP(espIPAddress); - } - result = response.body().string(); + SocketClient socketClient = SocketClient.getInstance(); + socketClient.openConnection(espIPAddress, 80); + if (socketClient.isConnected()) { + ScienceLabCommon.setIsWifiConnected(true); + ScienceLabCommon.setEspBaseIP(espIPAddress); + return true; } - } catch (IllegalArgumentException | IOException e) { - Log.e(TAG, "Unable to get data from " + espIPAddress, e); + } catch (Exception e) { + e.printStackTrace(); } - return result; + return false; } @Override - protected void onPostExecute(String result) { + protected void onPostExecute(Boolean result) { espConnectProgressBar.setVisibility(View.GONE); espConnectBtn.setVisibility(View.VISIBLE); Activity activity; - if (result.isEmpty() && ((activity = getActivity()) != null)) { + if (!result && ((activity = getActivity()) != null)) { CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content), getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT); } else { - Log.v(TAG, "Response: " + result); + Log.v("ESPFragment", "ESP Connection Successful"); + ScienceLabCommon.getInstance().openDevice(null); + ScienceLabCommon.isWifiConnected = true; + getParentFragmentManager().beginTransaction().remove(ESPFragment.this).commitAllowingStateLoss(); + getParentFragmentManager().beginTransaction().replace(R.id.frame, HomeFragment.newInstance(true, false)).commitAllowingStateLoss(); } } } diff --git a/app/src/main/java/io/pslab/fragment/HomeFragment.java b/app/src/main/java/io/pslab/fragment/HomeFragment.java index 9574f1e58..be08913be 100644 --- a/app/src/main/java/io/pslab/fragment/HomeFragment.java +++ b/app/src/main/java/io/pslab/fragment/HomeFragment.java @@ -22,12 +22,17 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import com.google.android.material.snackbar.Snackbar; + import java.io.IOException; +import java.util.Objects; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; import io.pslab.R; +import io.pslab.activity.MainActivity; +import io.pslab.others.CustomSnackBar; import io.pslab.others.InitializationVariable; import io.pslab.others.ScienceLabCommon; @@ -97,7 +102,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c } imgViewDeviceStatus.setImageResource(R.drawable.icons8_usb_connected_100); tvDeviceStatus.setText(getString(R.string.device_connected_successfully)); - } else { + } + else if (!deviceFound && deviceConnected) { + tvConnectMsg.setVisibility(View.GONE); + try { + tvVersion.setText(scienceLab.getVersion()); + tvVersion.setVisibility(View.VISIBLE); + } catch (IOException e) { + e.printStackTrace(); + } + imgViewDeviceStatus.setImageResource(R.drawable.icons8_wifi_connected_100); + tvDeviceStatus.setText(getString(R.string.device_connected_successfully)); + requireActivity().invalidateOptionsMenu(); + CustomSnackBar.showSnackBar(requireActivity().findViewById(android.R.id.content), + getString(R.string.device_connected_successfully), null, null, Snackbar.LENGTH_SHORT); + } + else { imgViewDeviceStatus.setImageResource(R.drawable.icons_usb_disconnected_100); tvDeviceStatus.setText(getString(R.string.device_not_found)); } diff --git a/app/src/main/java/io/pslab/interfaces/HttpCallback.java b/app/src/main/java/io/pslab/interfaces/HttpCallback.java deleted file mode 100644 index 3bcdd62e8..000000000 --- a/app/src/main/java/io/pslab/interfaces/HttpCallback.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.pslab.interfaces; - -public interface HttpCallback { - void success(T t1); - void error(Exception e); -} diff --git a/app/src/main/java/io/pslab/receivers/WifiDisconnectReceiver.java b/app/src/main/java/io/pslab/receivers/WifiDisconnectReceiver.java new file mode 100644 index 000000000..9dc72a001 --- /dev/null +++ b/app/src/main/java/io/pslab/receivers/WifiDisconnectReceiver.java @@ -0,0 +1,65 @@ +package io.pslab.receivers; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.wifi.WifiManager; +import android.util.Log; + +import androidx.fragment.app.Fragment; + +import com.google.android.material.snackbar.Snackbar; + + +import io.pslab.R; +import io.pslab.activity.MainActivity; +import io.pslab.activity.PowerSourceActivity; +import io.pslab.communication.PacketHandler; +import io.pslab.fragment.HomeFragment; +import io.pslab.others.CustomSnackBar; +import io.pslab.others.ScienceLabCommon; + +public class WifiDisconnectReceiver extends BroadcastReceiver { + + private final String TAG = this.getClass().getSimpleName(); + private Context activityContext; + + public WifiDisconnectReceiver() { + } + + public WifiDisconnectReceiver(Context context) { + this.activityContext = context; + } + + @Override + public void onReceive(Context context, Intent intent) { + try { + if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { + if (ScienceLabCommon.isWifiConnected) { + ScienceLabCommon.scienceLab.close(); + // Clear saved values in Power Source Instrument + context.getSharedPreferences(PowerSourceActivity.POWER_PREFERENCES, Context.MODE_PRIVATE).edit().clear().apply(); + CustomSnackBar.showSnackBar(((Activity) context).findViewById(android.R.id.content), + "Wifi Device Disconnected", null, null, Snackbar.LENGTH_SHORT); + + PacketHandler.version = ""; + + if (activityContext != null) { + MainActivity mainActivity = (MainActivity) activityContext; + Fragment currentFragment = mainActivity.getSupportFragmentManager().findFragmentById(R.id.frame); + if (currentFragment instanceof HomeFragment) { + mainActivity.getSupportFragmentManager().beginTransaction().replace(R.id.frame, HomeFragment.newInstance(false, false)).commitAllowingStateLoss(); + } + ScienceLabCommon.isWifiConnected = false; + mainActivity.invalidateOptionsMenu(); + } + } else { + Log.v(TAG, "Board isn't connected."); + } + } + } catch (IllegalStateException ignored) { + /**/ + } + } +} diff --git a/app/src/main/res/drawable/ic_wifi_connected.xml b/app/src/main/res/drawable/ic_wifi_connected.xml new file mode 100644 index 000000000..68a3dedc2 --- /dev/null +++ b/app/src/main/res/drawable/ic_wifi_connected.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/icons8_wifi_connected_100.png b/app/src/main/res/drawable/icons8_wifi_connected_100.png new file mode 100644 index 000000000..e287453c3 Binary files /dev/null and b/app/src/main/res/drawable/icons8_wifi_connected_100.png differ diff --git a/app/src/main/res/layout/home_fragment.xml b/app/src/main/res/layout/home_fragment.xml index 1474c274f..c462b1234 100644 --- a/app/src/main/res/layout/home_fragment.xml +++ b/app/src/main/res/layout/home_fragment.xml @@ -39,9 +39,10 @@ +