Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: interface for ESP wifi module added #1911

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ dependencies {
implementation 'com.github.GoodieBag:ProtractorView:v1.2'
implementation 'com.github.Triggertrap:SeekArc:v1.1'

implementation "com.squareup.okhttp3:okhttp:$rootProject.okHttpVersion"

implementation "com.jakewharton:butterknife:$rootProject.butterKnifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.butterKnifeVersion"

Expand Down
43 changes: 37 additions & 6 deletions app/src/main/java/io/pslab/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -41,6 +42,7 @@
import io.pslab.communication.CommunicationHandler;
import io.pslab.fragment.AboutUsFragment;
import io.pslab.fragment.BluetoothScanFragment;
import io.pslab.fragment.ESPFragment;
import io.pslab.fragment.FAQFragment;
import io.pslab.fragment.HomeFragment;
import io.pslab.fragment.InstrumentsFragment;
Expand Down Expand Up @@ -406,22 +408,51 @@ private void attemptToConnectPSLab() {
initialisationDialog.dismiss();
Toast.makeText(this, getString(R.string.device_not_found), Toast.LENGTH_SHORT).show();
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setMessage(getResources().getString(R.string.bluetooth_scan_dialog))
.setMessage(getResources().getString(R.string.bluetooth_wifi_scan_dialog))
.setPositiveButton(getResources().getString(R.string.bluetooth_scan_text), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BluetoothScanFragment bluetoothScanFragment = new BluetoothScanFragment();
bluetoothScanFragment.show(getSupportFragmentManager(), "bluetooth");
bluetoothScanFragment.setCancelable(true);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.bluetooth_wifi_dialog_layout, null);
dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.cancel();
}
});

dialogView.findViewById(R.id.bluetooth_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BluetoothScanFragment bluetoothScanFragment = new BluetoothScanFragment();
bluetoothScanFragment.show(getSupportFragmentManager(), "bluetooth");
bluetoothScanFragment.setCancelable(true);
dialog.cancel();
}
});

dialogView.findViewById(R.id.wifi_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ESPFragment espFragment = new ESPFragment();
espFragment.show(getSupportFragmentManager(), "wifi");
espFragment.setCancelable(true);
dialog.cancel();
}
});

dialogBuilder.setView(dialogView)
.create()
.show();
}
})
.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create();
}).create();
dialog.show();
}
}
Expand Down
99 changes: 99 additions & 0 deletions app/src/main/java/io/pslab/fragment/ESPFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package io.pslab.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.IOException;

import io.pslab.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ESPFragment extends DialogFragment {
private String espIPAddress = "";
private ProgressBar espConnectProgressBar;
private Button espConnectBtn;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_esp, container, false);
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();
if (espIPAddress.length() == 0) {
Toast.makeText(getContext(), getResources().getString(R.string.incorrect_IP_address_message), Toast.LENGTH_SHORT).show();
} else {
new ESPTask().execute();
}
}
});
return rootView;
}

@Override
public void onResume() {
super.onResume();
ViewGroup.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);
}

private class ESPTask extends AsyncTask<Void, Void, String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformat the whole code to add proper indents and spaces. Should be quiet obvious by now.


@Override
protected void onPreExecute() {
espConnectBtn.setVisibility(View.GONE);
espConnectProgressBar.setVisibility(View.VISIBLE);
}

@Override
protected String doInBackground(Void... voids) {
String result = "";
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://" + espIPAddress)
.build();
Response response = client.newCall(request).execute();
result = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

@Override
protected void onPostExecute(String result) {
espConnectProgressBar.setVisibility(View.GONE);
espConnectBtn.setVisibility(View.VISIBLE);
if (result.length() == 0) {
Toast.makeText(getContext(), getResources().getString(R.string.incorrect_IP_address_message), Toast.LENGTH_SHORT).show();
} else {
Log.v("Response", result);
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_bluetooth.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M17.71,7.71L12,2h-1v7.59L6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 11,14.41L11,22h1l5.71,-5.71 -4.3,-4.29 4.3,-4.29zM13,5.83l1.88,1.88L13,9.59L13,5.83zM14.88,16.29L13,18.17v-3.76l1.88,1.88z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_wifi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M1,9l2,2c4.97,-4.97 13.03,-4.97 18,0l2,-2C16.93,2.93 7.08,2.93 1,9zM9,17l3,3 3,-3c-1.65,-1.66 -4.34,-1.66 -6,0zM5,13l2,2c2.76,-2.76 7.24,-2.76 10,0l2,-2C15.14,9.14 8.87,9.14 5,13z"/>
</vector>
74 changes: 74 additions & 0 deletions app/src/main/res/layout/bluetooth_wifi_dialog_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/dialog_space_width">

<io.pslab.items.SquareLinearLayout
android:id="@+id/bluetooth_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dialog_space_width"
android:layout_weight="1"
android:background="@drawable/btn_back_rounded"
android:orientation="vertical"
android:paddingTop="@dimen/btn_padding"
android:stateListAnimator="@animator/selector_animator">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:paddingLeft="@dimen/btn_title_side_padding"
android:paddingRight="@dimen/btn_title_side_padding"
android:text="@string/bluetooth"
android:textColor="@color/white"
android:textStyle="bold" />

<ImageView
android:layout_width="@dimen/bluetooth_wifi_icon_dimen"
android:layout_height="@dimen/bluetooth_wifi_icon_dimen"
android:layout_gravity="center"
android:layout_margin="@dimen/btn_margin"
android:src="@drawable/ic_bluetooth" />

</io.pslab.items.SquareLinearLayout>

<View
android:layout_width="@dimen/separator_width"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dialog_sep_margin_top"
android:layout_marginBottom="@dimen/dialog_sep_margin_top"
android:background="@color/grey_light" />

<io.pslab.items.SquareLinearLayout
android:id="@+id/wifi_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dialog_space_width"
android:layout_weight="1"
android:background="@drawable/btn_back_rounded"
android:orientation="vertical"
android:paddingTop="@dimen/btn_padding"
android:stateListAnimator="@animator/selector_animator">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:text="@string/wifi"
android:textColor="@color/white"
android:textStyle="bold" />

<ImageView
android:layout_width="@dimen/bluetooth_wifi_icon_dimen"
android:layout_height="@dimen/bluetooth_wifi_icon_dimen"
android:layout_gravity="center"
android:layout_margin="@dimen/btn_margin"
android:src="@drawable/ic_wifi" />
</io.pslab.items.SquareLinearLayout>

</LinearLayout>
31 changes: 31 additions & 0 deletions app/src/main/res/layout/fragment_esp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/esp_fragment_padding">

<EditText
android:id="@+id/esp_ip_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:hint="@string/enter_IP_hint" />

<Button
android:id="@+id/esp_connect_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/esp_ip_edit_text"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:text="@string/esp_connect_text"
android:textColor="@color/white" />

<ProgressBar
android:id="@+id/esp_connect_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/esp_ip_edit_text"
android:layout_centerHorizontal="true"
android:visibility="gone" />
</RelativeLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@
<dimen name="seekarc_padding">30dp</dimen>
<dimen name="seekarc_edit_text">36sp</dimen>

<dimen name="esp_fragment_padding">30dp</dimen>
<dimen name="bluetooth_wifi_icon_dimen">50dp</dimen>

<dimen name="create_config_margin1">10dp</dimen>
<dimen name="create_config_margin2">20dp</dimen>
<dimen name="create_config_margin3">22dp</dimen>
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
<string name="pslab_pinlayout_front">Pin Layout Front</string>
<string name="pslab_pinlayout_back">Pin Layout Back</string>
<string name="bluetooth_connect_menu">Bluetooth Connection</string>
<string name="bluetooth_scan_dialog">USB device not found. Do you want to scan for Bluetooth Connection?</string>

<string name="bluetooth_wifi_scan_dialog">USB device not found. Do you want to scan for Bluetooth or Wifi connection?</string>
<string name="bluetooth">Bluetooth</string>
<string name="wifi">WiFi</string>
<string name="bluetooth_scan_text">Scan</string>
<string name="bluetooth_stop_text">Stop</string>
<string name="scanned_devices_list_title">Scanned Devices</string>
<string name="bluetooth_not_supported">Your phone does not support bluetooth</string>
<string name="incorrect_IP_address_message">Please enter valid IP address</string>
<string name="enter_IP_hint">Enter IP address of ESP module</string>
<string name="esp_connect_text">Connect</string>

<string name="oscilloscope">Oscilloscope</string>
<string name="multimeter">Multimeter</string>
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ext {
carouselPickerVersion = 'v1.0'
speedView = '1.2.0'
realmAdapter = '2.1.1'
okHttpVersion = '4.0.1'

// Map related versions
osmVersion = '5.1'
Expand Down