2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.login;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-04-22 04:54:02 +02:00
|
|
|
import android.arch.lifecycle.ViewModelProviders;
|
2019-04-14 18:12:04 +02:00
|
|
|
import android.os.Bundle;
|
2019-04-22 04:54:02 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2019-04-14 18:12:04 +02:00
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.widget.AutoCompleteTextView;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.R;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A login screen that offers login via email/password.
|
|
|
|
*/
|
|
|
|
public class LoginActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
|
|
|
|
|
|
|
|
// UI references.
|
|
|
|
private AutoCompleteTextView mJidView;
|
|
|
|
private EditText mPasswordView;
|
|
|
|
private View mProgressView;
|
|
|
|
private View mLoginFormView;
|
|
|
|
|
2019-05-04 00:27:02 +02:00
|
|
|
private LoginViewModel viewModel;
|
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_login);
|
|
|
|
|
|
|
|
// Set up the login form.
|
|
|
|
mJidView = findViewById(R.id.jid);
|
|
|
|
mPasswordView = findViewById(R.id.password);
|
|
|
|
|
2019-05-04 00:27:02 +02:00
|
|
|
viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
|
|
|
|
viewModel.getAccount().observe(this, accountModel -> {
|
|
|
|
if (accountModel == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accountModel.getJid() != null) {
|
|
|
|
mJidView.setText(accountModel.getJid().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accountModel.getPassword() != null) {
|
|
|
|
mPasswordView.setText(accountModel.getPassword());
|
|
|
|
}
|
|
|
|
});
|
2019-04-22 04:54:02 +02:00
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
mJidView.setOnEditorActionListener(this);
|
2019-04-22 04:54:02 +02:00
|
|
|
mPasswordView.setOnEditorActionListener(this);
|
2019-04-14 18:12:04 +02:00
|
|
|
|
|
|
|
Button mEmailSignInButton = findViewById(R.id.sign_in_button);
|
|
|
|
mEmailSignInButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
loginDetailsEntered();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mLoginFormView = findViewById(R.id.login_form);
|
|
|
|
mProgressView = findViewById(R.id.login_progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loginDetailsEntered() {
|
|
|
|
String jidInput = mJidView.getText().toString();
|
|
|
|
if (jidInput.isEmpty()) {
|
|
|
|
mJidView.setError(getResources().getString(R.string.error_field_required));
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityBareJid jid = asValidJidOrNull(jidInput);
|
|
|
|
if (jid == null) {
|
|
|
|
mJidView.setError(getResources().getString(R.string.error_invalid_jid));
|
|
|
|
}
|
|
|
|
|
|
|
|
String password = mPasswordView.getText().toString();
|
|
|
|
if (!isPasswordValid(password)) {
|
|
|
|
mPasswordView.setError(getResources().getString(R.string.error_invalid_password));
|
|
|
|
}
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
viewModel.login();
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to parse the input string into a {@link EntityBareJid} and return it.
|
|
|
|
* Return null on failure.
|
|
|
|
* @param input input string
|
|
|
|
* @return valid jid or null
|
|
|
|
*/
|
|
|
|
private EntityBareJid asValidJidOrNull(String input) {
|
|
|
|
return JidCreate.entityBareFromOrNull(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
|
return !password.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
|
switch (v.getId()) {
|
|
|
|
case R.id.jid:
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_NEXT) {
|
|
|
|
mPasswordView.requestFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R.id.password:
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL) {
|
|
|
|
loginDetailsEntered();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|