Skip to content

Commit

Permalink
Segregated Meross cloud login from HA addon login
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Jan 22, 2022
1 parent 10ef65e commit 841fb5e
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

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@local";
public static final String HA_ADDON_DEFAULT_PASSWORD = "Changeme!";

}
22 changes: 15 additions & 7 deletions app/src/main/java/com/albertogeniola/merossconf/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -42,7 +41,7 @@ public class MainActivity extends AppCompatActivity {
private TextView wifiMerossWarning;
private TextView locationTextView;
private LinearLayout wifiLocationStatusLayout;

private boolean mActiveFragmentsRequireWifiLocationWarn = false;
private MenuItem mPairMenuItem, mDeviceMenuItem;

@Override
Expand Down Expand Up @@ -127,11 +126,20 @@ private void updateLocationStatus(boolean locationEnabled) {
updateStatusBarVisibility();
}

private void updateStatusBarVisibility() {
wifiLocationStatusLayout.setVisibility(
wifiTextView.getVisibility()==View.VISIBLE ||
locationTextView.getVisibility()==View.VISIBLE ||
wifiMerossWarning.getVisibility()==View.VISIBLE ? View.VISIBLE : View.GONE);
public void setWifiLocationWarnRequired(boolean enabled) {
mActiveFragmentsRequireWifiLocationWarn = enabled;
updateStatusBarVisibility();
}

public void updateStatusBarVisibility() {
if (mActiveFragmentsRequireWifiLocationWarn) {
wifiLocationStatusLayout.setVisibility(
wifiTextView.getVisibility() == View.VISIBLE ||
locationTextView.getVisibility() == View.VISIBLE ||
wifiMerossWarning.getVisibility() == View.VISIBLE ? View.VISIBLE : View.GONE);
} else {
wifiLocationStatusLayout.setVisibility(View.GONE);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -18,13 +19,13 @@
import androidx.navigation.fragment.NavHostFragment;

import com.albertogeniola.merossconf.AndroidPreferencesManager;
import com.albertogeniola.merossconf.Constants;
import com.albertogeniola.merossconf.MainActivity;
import com.albertogeniola.merossconf.R;
import com.albertogeniola.merossconf.model.HttpClientManager;
import com.albertogeniola.merossconf.ui.MainActivityViewModel;
import com.albertogeniola.merossconf.ui.fragments.login.LoginFragment;
import com.albertogeniola.merosslib.model.http.ApiCredentials;
import com.albertogeniola.merosslib.model.http.ErrorCodes;
import com.albertogeniola.merosslib.model.http.exceptions.HttpApiException;
import com.albertogeniola.merosslib.model.http.exceptions.HttpApiTokenException;

import org.eclipse.paho.client.mqttv3.util.Strings;

Expand All @@ -39,7 +40,8 @@ public View onCreateView(@NonNull LayoutInflater inflater,
final EditText httpTokenEditText = root.findViewById(R.id.httpTokenEditText);
final EditText mqttKeyEditText = root.findViewById(R.id.mqttKeyEditText);
final Button httpLogoutButton = root.findViewById(R.id.httpLogoutButton);
final Button httpLoginButton = root.findViewById(R.id.httpLoginButton);
final Button haBrokerLoginButton = root.findViewById(R.id.haBrokerLoginButton);
final Button merossCloudLoginButton = root.findViewById(R.id.merossCloudLoginButton);
final CardView loginCardView = root.findViewById(R.id.loginCard);
final Button manualSetupButton = root.findViewById(R.id.setManualButton);

Expand Down Expand Up @@ -79,15 +81,35 @@ public void onChanged(ApiCredentials apiCredentials) {
httpLogoutButton.setEnabled(apiCredentials != null);
httpInfoCard.setVisibility(apiCredentials == null ? View.GONE : View.VISIBLE);
loginCardView.setVisibility(apiCredentials == null ? View.VISIBLE : View.GONE);
httpLoginButton.setEnabled(apiCredentials == null);
haBrokerLoginButton.setEnabled(apiCredentials == null);
merossCloudLoginButton.setEnabled(apiCredentials == null);
manualSetupButton.setVisibility(apiCredentials == null ? View.VISIBLE : View.GONE);
}
});

httpLoginButton.setOnClickListener(new View.OnClickListener() {
haBrokerLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavHostFragment.findNavController(AccountFragment.this).navigate(R.id.login_fragment);
Bundle args = new Bundle();
args.putBoolean(LoginFragment.Args.ENABLE_BROKER_DISCOVERY, true);
args.putString(LoginFragment.Args.HTTP_BROKER_EMAIL, Constants.HA_ADDON_DEFAULT_EMAIL);
args.putString(LoginFragment.Args.HTTP_BROKER_PASSWORD, Constants.HA_ADDON_DEFAULT_PASSWORD);
args.putInt(LoginFragment.Args.INTRO_TEXT_RESOURCE_ID, R.string.login_intro_ha_broker);
args.putInt(LoginFragment.Args.INTRO_IMAGE_RESOURCE_ID, R.drawable.ha_logo);
args.putBoolean(LoginFragment.Args.REQUIRES_WIFI_LOCATION, true);
NavHostFragment.findNavController(AccountFragment.this).navigate(R.id.login_fragment, args);
}
});
merossCloudLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean(LoginFragment.Args.ENABLE_BROKER_DISCOVERY, false);
args.putString(LoginFragment.Args.HTTP_BROKER_URL, Constants.MEROSS_CLOUD_EP);
args.putInt(LoginFragment.Args.INTRO_TEXT_RESOURCE_ID, R.string.login_intro_text_meross);
args.putInt(LoginFragment.Args.INTRO_IMAGE_RESOURCE_ID, R.drawable.meross_logo);
args.putBoolean(LoginFragment.Args.REQUIRES_WIFI_LOCATION, false);
NavHostFragment.findNavController(AccountFragment.this).navigate(R.id.login_fragment, args);
}
});

Expand Down Expand Up @@ -143,4 +165,10 @@ public void onClick(DialogInterface dialog, int which) {
return root;
}

@Override
public void onResume() {
super.onResume();
((MainActivity)requireActivity()).setWifiLocationWarnRequired(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -14,10 +13,11 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
Expand All @@ -27,6 +27,7 @@

import com.albertogeniola.merossconf.AndroidPreferencesManager;
import com.albertogeniola.merossconf.AndroidUtils;
import com.albertogeniola.merossconf.MainActivity;
import com.albertogeniola.merossconf.R;
import com.albertogeniola.merossconf.model.HttpClientManager;
import com.albertogeniola.merossconf.ui.MainActivityViewModel;
Expand All @@ -50,21 +51,34 @@ public class LoginFragment extends Fragment {
private static final String TAG = "Login";

// Instance attributes
private boolean mDiscoveryInProgress;
private boolean mDiscoveryInProgress = false;
private boolean mDiscoveryEnabled = false;
private boolean mRequiresWifiLocation = false;
private NsdManager mNsdManager;
private TextInputLayout mHttpHostnameInputLayout;
private EditText mHttpHostnameEditText;
private EditText mHttpUsernameEditText;
private EditText mHttpPasswordEditText;
private MaterialButton mLoginButton;
private MaterialButton mDiscoveryButton;
private TextView httpLoginIntroText;
private ImageView loginLogo;

private Timer mTimer;
private Handler mUiHandler;

private CircularProgressIndicator mSearchProgress;
private IndeterminateDrawable<CircularProgressIndicatorSpec> mProgressIndicatorDrawable;

public static class Args {
public static final String HTTP_BROKER_URL = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.HTTP_BROKER_URL";
public static final String HTTP_BROKER_EMAIL = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.HTTP_BROKER_EMAIL";
public static final String HTTP_BROKER_PASSWORD = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.HTTP_BROKER_PASSWORD";
public static final String ENABLE_BROKER_DISCOVERY = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.ENABLE_BROKER_DISCOVERY";
public static final String INTRO_TEXT_RESOURCE_ID = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.INTRO_TEXT_RESOURCE_ID";
public static final String INTRO_IMAGE_RESOURCE_ID = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.INTRO_IMAGE_RESOURCE_ID";
public static final String REQUIRES_WIFI_LOCATION = "com.albertogeniola.merossconf.ui.fragments.account.AccountFragment.Args.REQUIRES_WIFI_LOCATION";
}

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -87,6 +101,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mHttpUsernameEditText = ((TextInputLayout)view.findViewById(R.id.httpUsernameEditText)).getEditText();
mLoginButton = view.findViewById(R.id.loginButton);
mDiscoveryButton = view.findViewById(R.id.discoveryButton);
httpLoginIntroText = view.findViewById(R.id.httpLoginIntroText);
loginLogo = view.findViewById(R.id.loginLogo);

// Configure HostEditText for progress showing
mSearchProgress = new CircularProgressIndicator(this.requireActivity(), null);
Expand Down Expand Up @@ -125,6 +141,22 @@ public void onClick(View view) {
showPasswordCheckBox.setChecked(false);
mHttpPasswordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());

// Configure the UI based on received args, if any.
Bundle args = getArguments();
if (args == null)
args = new Bundle();

mRequiresWifiLocation = args.getBoolean(Args.REQUIRES_WIFI_LOCATION, false);
mDiscoveryEnabled = args.getBoolean(Args.ENABLE_BROKER_DISCOVERY, false);

mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, ""));
mHttpUsernameEditText.setText(args.getString(Args.HTTP_BROKER_EMAIL, ""));
mHttpPasswordEditText.setText(args.getString(Args.HTTP_BROKER_PASSWORD, ""));
mDiscoveryButton.setEnabled(mDiscoveryEnabled);
mDiscoveryButton.setVisibility(mDiscoveryEnabled ? View.VISIBLE : View.INVISIBLE);
httpLoginIntroText.setText(args.getInt(Args.INTRO_TEXT_RESOURCE_ID, R.string.login_intro_text));
loginLogo.setImageResource(args.getInt(Args.INTRO_IMAGE_RESOURCE_ID, R.drawable.login_icon));

return view;
}

Expand Down Expand Up @@ -193,7 +225,10 @@ public void run() {
@Override
public void onResume() {
super.onResume();
startApiDiscovery();
((MainActivity)requireActivity()).setWifiLocationWarnRequired(mRequiresWifiLocation);

if (mDiscoveryEnabled)
startApiDiscovery();
}

@Override
Expand Down
Binary file added app/src/main/res/drawable/ha_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/meross_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 34 additions & 24 deletions app/src/main/res/layout/fragment_account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
android:layout_margin="10dp">

<LinearLayout
android:layout_margin="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10sp"
android:orientation="vertical">

<TextView
android:id="@+id/networkCardTitle"
android:layout_width="wrap_content"
Expand All @@ -29,65 +30,69 @@

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="API Endpoint"
android:layout_marginTop="4sp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
android:hint="API Endpoint">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/httpUrlEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:editable="false"
android:text="N/A"/>
android:text="N/A"
android:textIsSelectable="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User ID"
android:layout_marginTop="4sp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
android:hint="User ID">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/userIdEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:editable="false"
android:text="N/A"/>
android:text="N/A"
android:textIsSelectable="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField3"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HTTP Token"
android:layout_marginTop="4sp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
android:hint="HTTP Token">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/httpTokenEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:editable="false"
android:text="N/A"/>
android:text="N/A"
android:textIsSelectable="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField4"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MQTT Key"
android:layout_marginTop="4sp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
android:hint="MQTT Key">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/mqttKeyEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:text="N/A"/>
android:text="N/A" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
Expand All @@ -104,30 +109,34 @@
android:layout_height="wrap_content"
android:layout_margin="10dp"
tools:layout_editor_absoluteX="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10sp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please acquire an HTTP Token from the HTTP broker to start pairing devices. " />

<ImageView
android:layout_margin="5dp"
android:layout_gravity="center_horizontal"
android:id="@+id/imageView4"
android:layout_width="203dp"
android:layout_height="145dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:src="@drawable/login_icon" />

<com.google.android.material.button.MaterialButton
android:id="@+id/httpLoginButton"
android:id="@+id/haBrokerLoginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login to (HA) Meross Local Addon" />

android:text="Homeassistant Login" />
<com.google.android.material.button.MaterialButton
android:id="@+id/merossCloudLoginButton"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:text="Login to Official Meross Broker" />

<com.google.android.material.button.MaterialButton
android:id="@+id/setManualButton"
Expand All @@ -136,6 +145,7 @@
android:layout_height="wrap_content"

android:text="Manual User/Key setup" />

</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
Loading

0 comments on commit 841fb5e

Please sign in to comment.