From 2084b1d151c65e0f30199cf029fc9a9fab63c402 Mon Sep 17 00:00:00 2001 From: Alberto Geniola Date: Sun, 29 May 2022 11:57:40 +0200 Subject: [PATCH] Change API login default for HA integration Improve error handling on login --- .../albertogeniola/merossconf/Constants.java | 4 +-- .../merossconf/model/HttpClientManager.java | 2 +- .../ui/fragments/login/LoginFragment.java | 12 ++++++-- app/src/main/res/layout/fragment_login.xml | 5 ++-- .../merosslib/model/http/ErrorCodes.java | 30 +++++++++++++++++++ .../http/exceptions/HttpApiException.java | 4 +++ 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/albertogeniola/merossconf/Constants.java b/app/src/main/java/com/albertogeniola/merossconf/Constants.java index c8f1f6a..7494695 100644 --- a/app/src/main/java/com/albertogeniola/merossconf/Constants.java +++ b/app/src/main/java/com/albertogeniola/merossconf/Constants.java @@ -4,8 +4,8 @@ public class Constants { public static final String LOG_TAG = "Custom Meross Pairer"; public static final String MEROSS_CLOUD_EP = "https://iot.meross.com"; - public static final String HA_ADDON_DEFAULT_EMAIL = "meross@local"; - public static final String HA_ADDON_DEFAULT_PASSWORD = "Changeme!"; + public static final String HA_ADDON_DEFAULT_EMAIL = ""; + public static final String HA_ADDON_DEFAULT_PASSWORD = ""; public static final long PAIRING_VERIFY_TIMEOUT_MILLISECONDS = 30000; public static final long MDNS_DISCOVERY_TIMEOUT_MILLISEOONDS = 5000; diff --git a/app/src/main/java/com/albertogeniola/merossconf/model/HttpClientManager.java b/app/src/main/java/com/albertogeniola/merossconf/model/HttpClientManager.java index 743f9fc..b2f966c 100644 --- a/app/src/main/java/com/albertogeniola/merossconf/model/HttpClientManager.java +++ b/app/src/main/java/com/albertogeniola/merossconf/model/HttpClientManager.java @@ -136,6 +136,6 @@ private abstract static class HttpClientTask extends AsyncTask { public void onSuccess(T result); - public void onFailure(Exception result); + public void onFailure(Exception exception); } } diff --git a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java index 1dac7dd..df720b2 100644 --- a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java +++ b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java @@ -32,6 +32,8 @@ import com.albertogeniola.merossconf.model.HttpClientManager; import com.albertogeniola.merossconf.ui.MainActivityViewModel; import com.albertogeniola.merosslib.model.http.ApiCredentials; +import com.albertogeniola.merosslib.model.http.ErrorCodes; +import com.albertogeniola.merosslib.model.http.exceptions.HttpApiException; import com.google.android.material.button.MaterialButton; import com.google.android.material.progressindicator.CircularProgressIndicator; import com.google.android.material.progressindicator.CircularProgressIndicatorSpec; @@ -160,7 +162,7 @@ public void onClick(View view) { mRequiresWifiLocation = args.getBoolean(Args.REQUIRES_WIFI_LOCATION, false); mDiscoveryEnabled = args.getBoolean(Args.ENABLE_BROKER_DISCOVERY, false); - mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, "")); + mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, "http://homeassistant.local:2002")); mHttpUsernameEditText.setText(args.getString(Args.HTTP_BROKER_EMAIL, "")); mHttpPasswordEditText.setText(args.getString(Args.HTTP_BROKER_PASSWORD, "")); mDiscoveryButton.setEnabled(mDiscoveryEnabled); @@ -302,9 +304,13 @@ public void onSuccess(ApiCredentials creds) { } @Override - public void onFailure(Exception result) { + public void onFailure(Exception exception) { dialog.dismiss(); - Snackbar.make(mLoginButton, "An error while executing the request.", Snackbar.LENGTH_LONG).show(); + String errorMessage = "An error occurred while executing the request."; + if (exception instanceof HttpApiException) { + errorMessage = ((HttpApiException)exception).getErrorMessage(); + } + Snackbar.make(mLoginButton,errorMessage , Snackbar.LENGTH_LONG).show(); } }); } diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index a6d2b6b..bd3dab1 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -57,9 +57,8 @@ + android:singleLine="true" />