Skip to content

Commit

Permalink
Merge branch 'bugfix/prov_crash' into 'master'
Browse files Browse the repository at this point in the history
Bug fixes and improvements.

See merge request idf/esp-idf-provisioning-android!36
  • Loading branch information
shahpiyushv committed Jun 29, 2020
2 parents 7a05d54 + 694b2c5 commit 214e5ec
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 56 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
minSdkVersion 23
targetSdkVersion 29
versionCode 6
versionName "2.0.1 - ${getGitHash()}"
versionName "2.0.2 - ${getGitHash()}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/espressif/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AppConstants {

public static final String ESP_PREFERENCES = "Esp_Preferences";

public static final String DEVICE_TYPE_SOFTAP = "wifi";
public static final String DEVICE_TYPE_SOFTAP = "softap";
public static final String DEVICE_TYPE_BLE = "ble";
public static final String DEVICE_TYPE_BOTH = "both";
public static final String DEVICE_TYPE_DEFAULT = DEVICE_TYPE_BOTH;
Expand Down
109 changes: 85 additions & 24 deletions app/src/main/java/com/espressif/ui/activities/AddDeviceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,38 +356,68 @@ public void run() {
}

@Override
public void deviceDetected(ESPDevice device) {
public void deviceDetected(final ESPDevice device) {

Log.e(TAG, "Device detected");
espDevice = device;
if (ActivityCompat.checkSelfPermission(AddDeviceActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "Location Permission not granted.");
return;
}
final String deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);

String deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
runOnUiThread(new Runnable() {

if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE)) {
@Override
public void run() {

if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)) {
if (ActivityCompat.checkSelfPermission(AddDeviceActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "Location Permission not granted.");
return;
}

Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
finish();
} else {
device.connectToDevice();
}
} else if (deviceType.equals(AppConstants.DEVICE_TYPE_SOFTAP)) {
if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE)) {

if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_BLE)) {
if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)) {

Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
finish();
} else {
device.connectToDevice();
Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
finish();
} else {
device.connectToDevice();
}
} else if (deviceType.equals(AppConstants.DEVICE_TYPE_SOFTAP)) {

if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_BLE)) {

Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
finish();
} else {

if (espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (!wifiManager.isWifiEnabled()) {
alertForWiFi();
return;
}
}

device.connectToDevice();
}
} else {

if (espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (!wifiManager.isWifiEnabled()) {
alertForWiFi();
return;
}
}
device.connectToDevice();
}
}
} else {
device.connectToDevice();
}
});
}

@Override
Expand Down Expand Up @@ -421,6 +451,37 @@ private void goToProvisionActivity() {
startActivity(provisionIntent);
}

private void alertForWiFi() {

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
builder.setCancelable(false);
builder.setMessage(R.string.error_wifi_off);

// Set up the buttons
builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
espDevice = null;
hideLoading();
if (codeScanner != null) {
codeScanner.releaseResources();
codeScanner.startPreview();
if (ActivityCompat.checkSelfPermission(AddDeviceActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(AddDeviceActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
provisionManager.scanQRCode(codeScanner, qrCodeScanListener);
} else {
Log.e(TAG, "Permissions are not granted");
}
}
}
});

builder.show();
}

private void askForManualDeviceConnection() {

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
Expand Down Expand Up @@ -492,7 +553,7 @@ private void startProvisioningFlow() {

} else {

final String[] deviceTypes = {"BLE", "Wi-Fi"};
final String[] deviceTypes = {"BLE", "SoftAP"};
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
builder.setCancelable(true);
builder.setTitle(R.string.dialog_msg_device_selection);
Expand Down Expand Up @@ -550,7 +611,7 @@ private void goToWiFiProvisionLandingActivity(int securityType) {
wifiProvisioningIntent.putExtra(AppConstants.KEY_DEVICE_NAME, espDevice.getDeviceName());
wifiProvisioningIntent.putExtra(AppConstants.KEY_PROOF_OF_POSSESSION, espDevice.getProofOfPossession());
}
getApplicationContext().startActivity(wifiProvisioningIntent);
startActivity(wifiProvisioningIntent);
}

private String getWifiSsid() {
Expand Down
30 changes: 26 additions & 4 deletions app/src/main/java/com/espressif/ui/activities/EspMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -55,6 +56,7 @@ public class EspMainActivity extends AppCompatActivity {

private ESPProvisionManager provisionManager;
private CardView btnAddDevice;
private ImageView ivEsp;
private SharedPreferences sharedPreferences;
private String deviceType;

Expand All @@ -71,6 +73,27 @@ protected void onCreate(Bundle savedInstanceState) {
provisionManager = ESPProvisionManager.getInstance(getApplicationContext());
}

@Override
protected void onResume() {
super.onResume();

deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
if (deviceType.equals("wifi")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
editor.apply();
}

deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE)) {
ivEsp.setImageResource(R.drawable.ic_esp_ble);
} else if (deviceType.equals(AppConstants.DEVICE_TYPE_SOFTAP)) {
ivEsp.setImageResource(R.drawable.ic_esp_softap);
} else {
ivEsp.setImageResource(R.drawable.ic_esp);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

Expand Down Expand Up @@ -121,6 +144,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

private void initViews() {

ivEsp = findViewById(R.id.iv_esp);
btnAddDevice = findViewById(R.id.btn_provision_device);
btnAddDevice.findViewById(R.id.iv_arrow).setVisibility(View.GONE);
btnAddDevice.setOnClickListener(addDeviceBtnClickListener);
Expand Down Expand Up @@ -162,8 +186,6 @@ private void addDeviceClick() {

} else {

deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);

if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE) || deviceType.equals(AppConstants.DEVICE_TYPE_BOTH)) {

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
Expand All @@ -183,7 +205,7 @@ private void addDeviceClick() {

private void startProvisioningFlow() {

String deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
final boolean isSec1 = sharedPreferences.getBoolean(AppConstants.KEY_SECURITY_TYPE, true);
Log.d(TAG, "Device Types : " + deviceType);
Log.d(TAG, "isSec1 : " + isSec1);
Expand Down Expand Up @@ -212,7 +234,7 @@ private void startProvisioningFlow() {

} else {

final String[] deviceTypes = {"BLE", "Wi-Fi"};
final String[] deviceTypes = {"BLE", "SoftAP"};
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
builder.setCancelable(true);
builder.setTitle(R.string.dialog_msg_device_selection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void onEvent(DeviceConnectionEvent event) {

} else {

goToProvisionActivity();
goToWiFiConfigActivity();
}

} else {
Expand All @@ -158,7 +158,7 @@ public void onEvent(DeviceConnectionEvent event) {

} else {

goToProvisionActivity();
goToWiFiConfigActivity();
}
}
break;
Expand Down Expand Up @@ -259,16 +259,14 @@ private void goToWifiScanListActivity() {

finish();
Intent wifiListIntent = new Intent(getApplicationContext(), WiFiScanActivity.class);
wifiListIntent.putExtra(AppConstants.KEY_PROOF_OF_POSSESSION, "");
startActivity(wifiListIntent);
}

private void goToProvisionActivity() {
private void goToWiFiConfigActivity() {

finish();
Intent provisionIntent = new Intent(getApplicationContext(), ProvisionActivity.class);
provisionIntent.putExtra(AppConstants.KEY_PROOF_OF_POSSESSION, "");
startActivity(provisionIntent);
Intent wifiConfigIntent = new Intent(getApplicationContext(), WiFiConfigActivity.class);
startActivity(wifiConfigIntent);
}

private boolean hasPermissions() {
Expand Down
Binary file removed app/src/main/res/drawable/bluetooth_icon.png
Binary file not shown.
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_esp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector android:height="300dp" android:viewportHeight="730.5"
android:viewportWidth="730.5" android:width="300dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#e5e5e5" android:pathData="m287.121,485.314c0,19.2 -15.6,34.8 -34.8,34.8s-34.8,-15.6 -34.8,-34.8s15.6,-34.8 34.8,-34.8l0,0c19.2,0.1 34.8,15.6 34.8,34.8l0,0"/>
<path android:fillColor="#e5e5e5" android:pathData="m590.921,449.114c-22.3,-158.1 -147.7,-283.5 -305.9,-305.8c-18.4,9.8 -35.4,22 -50.5,36.4l0,33.6c157.8,0 286.2,128.4 286.2,286.2l33.7,0c14.4,-15 26.7,-32 36.5,-50.4"/>
<path android:fillColor="#e5e5e5" android:pathData="m617.021,343.514c0,-125 -101.3,-226.4 -226.4,-226.4c-7.8,0 -15.6,0.5 -23.3,1.3l-5.2,14.9c111.5,39.2 199.2,126.8 238.5,238.4l15.1,-5.3c0.8,-7.6 1.3,-15.2 1.3,-22.9"/>
<path android:fillColor="#e5e5e5" android:pathData="m393.221,612.414c-148.6,-0.1 -269.1,-120.5 -269.1,-269.2c0,-71.4 28.3,-139.8 78.8,-190.2l14.4,14.4c-97.1,97.1 -97.1,254.7 0,351.8s254.7,97.1 351.8,0l14.4,14.4c-50.4,50.6 -118.9,79 -190.3,78.8"/>
<path android:fillColor="#e5e5e5" android:pathData="m390.321,515.114c8.5,-86 -54.3,-162.6 -140.2,-171.2c-7.7,-0.8 -13.3,-7.6 -12.6,-15.3c0.6,-7.6 7.2,-13.3 14.8,-12.7c0.2,0 0.4,0 0.6,0.1c101,9.6 175,99.3 165.4,200.3c-1.1,11.3 -3.2,22.5 -6.3,33.4l40.8,11.5c12.1,-3.4 24,-7.9 35.4,-13.3c3,-15.9 4.5,-32.1 4.5,-48.3c0,-129.5 -95.7,-237 -220.1,-255.4c-14.9,-2.1 -30.1,-2.3 -41.2,0.5c-47.2,12 -75.7,59.9 -63.8,107.1c7.5,29.5 29.7,53.1 58.7,62.3c6.7,2.1 17,3 22.2,4l0.2,0c44.9,7.7 75.1,50.3 67.5,95.3c-1.9,11 -5.9,21.4 -12,30.8l28.3,18.1c13.8,3.7 27.9,6 42.2,7.1c8.4,-17.1 13.7,-35.4 15.6,-54.3"/>
<path android:fillAlpha="0" android:fillColor="#000000"
android:fillType="evenOdd" android:pathData="M576.5,482.4a47.3,76.6 0,1 0,94.6 0a47.3,76.6 0,1 0,-94.6 0z"/>
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_esp_ble.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector android:height="300dp" android:viewportHeight="730.5"
android:viewportWidth="730.5" android:width="300dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#e5e5e5" android:pathData="m233,497.9c0,19.2 -15.6,34.8 -34.8,34.8s-34.8,-15.6 -34.8,-34.8s15.6,-34.8 34.8,-34.8l0,0c19.2,0.1 34.8,15.6 34.8,34.8l0,0"/>
<path android:fillColor="#e5e5e5" android:pathData="m536.8,461.7c-22.3,-158.1 -147.7,-283.5 -305.9,-305.8c-18.4,9.8 -35.4,22 -50.5,36.4l0,33.6c157.8,0 286.2,128.4 286.2,286.2l33.7,0c14.4,-15 26.7,-32 36.5,-50.4"/>
<path android:fillColor="#e5e5e5" android:pathData="m562.9,356.1c0,-125 -101.3,-226.4 -226.4,-226.4c-7.8,0 -15.6,0.5 -23.3,1.3l-5.2,14.9c111.5,39.2 199.2,126.8 238.5,238.4l15.1,-5.3c0.8,-7.6 1.3,-15.2 1.3,-22.9"/>
<path android:fillColor="#e5e5e5" android:pathData="m339.1,625c-148.6,-0.1 -269.1,-120.5 -269.1,-269.2c0,-71.4 28.3,-139.8 78.8,-190.2l14.4,14.4c-97.1,97.1 -97.1,254.7 0,351.8s254.7,97.1 351.8,0l14.4,14.4c-50.4,50.6 -118.9,79 -190.3,78.8"/>
<path android:fillColor="#e5e5e5" android:pathData="m336.2,527.7c8.5,-86 -54.3,-162.6 -140.2,-171.2c-7.7,-0.8 -13.3,-7.6 -12.6,-15.3c0.6,-7.6 7.2,-13.3 14.8,-12.7c0.2,0 0.4,0 0.6,0.1c101,9.6 175,99.3 165.4,200.3c-1.1,11.3 -3.2,22.5 -6.3,33.4l40.8,11.5c12.1,-3.4 24,-7.9 35.4,-13.3c3,-15.9 4.5,-32.1 4.5,-48.3c0,-129.5 -95.7,-237 -220.1,-255.4c-14.9,-2.1 -30.1,-2.3 -41.2,0.5c-47.2,12 -75.7,59.9 -63.8,107.1c7.5,29.5 29.7,53.1 58.7,62.3c6.7,2.1 17,3 22.2,4l0.2,0c44.9,7.7 75.1,50.3 67.5,95.3c-1.9,11 -5.9,21.4 -12,30.8l28.3,18.1c13.8,3.7 27.9,6 42.2,7.1c8.4,-17.1 13.7,-35.4 15.6,-54.3"/>
<path android:fillColor="#ffffff" android:fillType="evenOdd" android:pathData="M576.5,482.4a47.3,76.6 0,1 0,94.6 0a47.3,76.6 0,1 0,-94.6 0z"/>
<path android:fillColor="#e5e5e5" android:fillType="evenOdd" android:pathData="m630.5,440.6l13.7,13.7l-13.6,13.6l-0.1,-27.3l0,0zM630.5,524.2l13.7,-13.7l-13.6,-13.6l-0.1,27.3l0,0zM615.9,482.4l-29.5,-29.6l8.6,-8.6l23.5,23.5l0,-56.4l42.8,42.8l-28.3,28.3l28.2,28.2l-42.8,42.8l0,-56.4l-23.4,23.5l-8.6,-8.6l29.5,-29.5l0,0zM623.8,564.9c36.1,0 60.9,-17.1 60.9,-82.6c0,-65.5 -24.9,-82.6 -60.9,-82.6c-36.1,0 -60.9,17.1 -60.9,82.6c0,65.5 24.9,82.6 60.9,82.6l0,0z"/>
<path android:fillColor="#e5e5e5" android:pathData="m715.4,412.1c0,6.9 -5.8,12.7 -12.7,12.7c-7.2,0 -12.7,-5.5 -12.7,-12.7c0,-7 5.7,-12.7 12.7,-12.7c7.1,0 12.7,5.5 12.7,12.7zM692.8,412c0,5.4 4.4,10.1 9.8,10.1c5.5,0 9.8,-4.5 9.8,-9.9c0,-5.5 -4.3,-10.1 -9.8,-10.1c-5.4,0 -9.8,4.5 -9.8,9.9zM705,419l-3.9,-6.1l-0.1,0l0,6.1l-2.4,0l0,-13.9l4.2,0c3.3,0 4.6,1.4 4.6,3.5c0,2.4 -1.4,3.8 -3.7,4c0.3,0.4 0.8,1.1 1.4,2l2.9,4.4l-3,0zM702.5,407l-1.5,0l0,4.3l1,0c2.2,0 3,-1.3 3,-2.4c-0.1,-1.4 -0.7,-1.9 -2.5,-1.9z"/>
</vector>
Loading

0 comments on commit 214e5ec

Please sign in to comment.