Skip to content

Commit

Permalink
fix: prevent crash if invalid host is entered (#2588)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcnause authored Dec 22, 2024
1 parent 6e117af commit 640da48
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions app/src/main/java/io/pslab/fragment/ESPFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -30,6 +29,8 @@
import okhttp3.Response;

public class ESPFragment extends DialogFragment {
private static final String TAG = ESPFragment.class.getSimpleName();

private String espIPAddress = "";
private ProgressBar espConnectProgressBar;
private Button espConnectBtn;
Expand All @@ -46,42 +47,34 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
EditText espIPEditText = rootView.findViewById(R.id.esp_ip_edit_text);
espConnectBtn = rootView.findViewById(R.id.esp_connect_btn);
espConnectProgressBar = rootView.findViewById(R.id.esp_connect_progressbar);
espConnectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
espIPAddress = espIPEditText.getText().toString();
espConnectBtn.onEditorAction(EditorInfo.IME_ACTION_DONE);
Activity activity;
if (espIPAddress.isEmpty() && ((activity = getActivity()) != null)) {
CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content),
getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT);

} else {
new ESPTask().execute();
}
espConnectBtn.setOnClickListener(v -> {
espIPAddress = espIPEditText.getText().toString();
espConnectBtn.onEditorAction(EditorInfo.IME_ACTION_DONE);
Activity activity;
if (espIPAddress.isEmpty() && ((activity = getActivity()) != null)) {
CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content),
getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT);
} else {
new ESPTask().execute();
}

});
espIPEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
espConnectBtn.performClick();
return true;
}
return false;
espIPEditText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
espConnectBtn.performClick();
return true;
}
return false;
});
return rootView;
}

@Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
getDialog().getWindow().setAttributes(params);
}

private class ESPTask extends AsyncTask<Void, Void, String> {
Expand All @@ -100,14 +93,15 @@ protected String doInBackground(Void... voids) {
Request request = new Request.Builder()
.url("http://" + espIPAddress)
.build();
Response response = client.newCall(request).execute();
if (response.code() == 200) {
ScienceLabCommon.setIsWifiConnected(true);
ScienceLabCommon.setEspBaseIP(espIPAddress);
try (Response response = client.newCall(request).execute()) {
if (response.code() == 200) {
ScienceLabCommon.setIsWifiConnected(true);
ScienceLabCommon.setEspBaseIP(espIPAddress);
}
result = response.body().string();
}
result = response.body().string();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException | IOException e) {
Log.e(TAG, "Unable to get data from " + espIPAddress, e);
}
return result;
}
Expand All @@ -121,7 +115,7 @@ protected void onPostExecute(String result) {
CustomSnackBar.showSnackBar(activity.findViewById(android.R.id.content),
getString(R.string.incorrect_IP_address_message), null, null, Snackbar.LENGTH_SHORT);
} else {
Log.v("Response", result);
Log.v(TAG, "Response: " + result);
}
}
}
Expand Down

0 comments on commit 640da48

Please sign in to comment.