2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.login;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-05-27 21:34:11 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
import android.content.Intent;
|
2019-04-14 18:12:04 +02:00
|
|
|
import android.os.Bundle;
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.appcompat.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-18 10:06:16 +02:00
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.R;
|
2019-05-27 21:34:11 +02:00
|
|
|
import org.mercury_im.messenger.persistence.model.AccountModel;
|
2019-05-19 02:00:33 +02:00
|
|
|
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
|
|
|
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
2019-05-27 21:34:11 +02:00
|
|
|
import org.mercury_im.messenger.service.XmppStartedService;
|
2019-05-18 10:06:16 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
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 {
|
|
|
|
|
2019-05-18 10:06:16 +02:00
|
|
|
@Inject
|
|
|
|
AccountRepository accountRepository;
|
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
// 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);
|
|
|
|
|
2019-05-18 10:06:16 +02:00
|
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
// 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);
|
2019-05-27 21:34:11 +02:00
|
|
|
displayCredentials(viewModel.getAccount());
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-27 21:34:11 +02:00
|
|
|
private void displayCredentials(LiveData<? extends AccountModel> account) {
|
|
|
|
account.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-14 18:12:04 +02:00
|
|
|
private void loginDetailsEntered() {
|
2019-05-18 10:06:16 +02:00
|
|
|
boolean loginIntact = true;
|
2019-04-14 18:12:04 +02:00
|
|
|
String jidInput = mJidView.getText().toString();
|
|
|
|
if (jidInput.isEmpty()) {
|
|
|
|
mJidView.setError(getResources().getString(R.string.error_field_required));
|
2019-05-18 10:06:16 +02:00
|
|
|
loginIntact = false;
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EntityBareJid jid = asValidJidOrNull(jidInput);
|
|
|
|
if (jid == null) {
|
|
|
|
mJidView.setError(getResources().getString(R.string.error_invalid_jid));
|
2019-05-18 10:06:16 +02:00
|
|
|
loginIntact = false;
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String password = mPasswordView.getText().toString();
|
|
|
|
if (!isPasswordValid(password)) {
|
|
|
|
mPasswordView.setError(getResources().getString(R.string.error_invalid_password));
|
2019-05-18 10:06:16 +02:00
|
|
|
loginIntact = false;
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-05-18 10:06:16 +02:00
|
|
|
if (loginIntact) {
|
2019-05-19 02:00:33 +02:00
|
|
|
RoomAccountModel accountModel = new RoomAccountModel();
|
2019-05-18 10:06:16 +02:00
|
|
|
accountModel.setEnabled(true);
|
|
|
|
accountModel.setJid(jid);
|
|
|
|
accountModel.setPassword(password);
|
|
|
|
long id = accountRepository.insertAccount(accountModel);
|
|
|
|
attemptLogin(jid, password, id);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void attemptLogin(EntityBareJid jid, String password, long accountId) {
|
2019-05-27 21:34:11 +02:00
|
|
|
Intent connectIntent = new Intent(getApplicationContext(), XmppStartedService.class);
|
|
|
|
connectIntent.setAction(XmppStartedService.ACTION_CONNECT);
|
|
|
|
connectIntent.putExtra(XmppStartedService.EXTRA_ACCOUNT_ID, accountId);
|
2019-05-18 10:06:16 +02:00
|
|
|
startService(connectIntent);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|