2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.login;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2019-06-03 01:16:49 +02:00
|
|
|
import android.util.Log;
|
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.Button;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-06-29 19:06:07 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
|
2019-06-06 13:49:13 +02:00
|
|
|
import com.google.android.material.textfield.TextInputEditText;
|
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
2019-07-02 00:46:40 +02:00
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
2019-04-14 18:12:04 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2019-06-20 17:20:23 +02:00
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
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-06-21 03:45:33 +02:00
|
|
|
import org.mercury_im.messenger.handler.RoomPlainMessageHandler;
|
2019-06-29 19:06:07 +02:00
|
|
|
import org.mercury_im.messenger.handler.RoomRosterHandler;
|
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.repository.AccountRepository;
|
2019-06-29 19:06:07 +02:00
|
|
|
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
2019-06-20 17:20:23 +02:00
|
|
|
import org.mercury_im.messenger.ui.BindingActivity;
|
2019-07-03 00:56:55 +02:00
|
|
|
import org.mercury_im.messenger.AndroidMercuryConnection;
|
2019-06-20 17:20:23 +02:00
|
|
|
import org.mercury_im.messenger.xmpp_core.MercuryConnection;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-05-18 10:06:16 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-06-03 01:16:49 +02:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
/**
|
|
|
|
* A login screen that offers login via email/password.
|
|
|
|
*/
|
2019-06-20 17:20:23 +02:00
|
|
|
public class LoginActivity extends BindingActivity implements TextView.OnEditorActionListener {
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-05-18 10:06:16 +02:00
|
|
|
@Inject
|
|
|
|
AccountRepository accountRepository;
|
|
|
|
|
2019-04-14 18:12:04 +02:00
|
|
|
// UI references.
|
2019-06-03 01:16:49 +02:00
|
|
|
@BindView(R.id.jid)
|
2019-06-06 13:49:13 +02:00
|
|
|
TextInputEditText mJidView;
|
2019-06-03 01:16:49 +02:00
|
|
|
|
|
|
|
@BindView(R.id.password)
|
2019-06-06 13:49:13 +02:00
|
|
|
TextInputEditText mPasswordView;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
@BindView(R.id.sign_in_button)
|
|
|
|
Button mSignInView;
|
|
|
|
|
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.
|
2019-06-03 01:16:49 +02:00
|
|
|
ButterKnife.bind(this);
|
2019-04-14 18:12:04 +02:00
|
|
|
|
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
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
mSignInView.setOnClickListener(new OnClickListener() {
|
2019-04-14 18:12:04 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
loginDetailsEntered();
|
|
|
|
}
|
|
|
|
});
|
2019-06-20 17:20:23 +02:00
|
|
|
mSignInView.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onServiceBound() {
|
|
|
|
super.onServiceBound();
|
|
|
|
mSignInView.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onServiceUnbound() {
|
|
|
|
super.onServiceUnbound();
|
|
|
|
mSignInView.setEnabled(false);
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
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-06-03 01:16:49 +02:00
|
|
|
// TODO: Move to ViewModel
|
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);
|
2019-06-20 17:20:23 +02:00
|
|
|
accountModel.setId(id);
|
2019-06-03 01:16:49 +02:00
|
|
|
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + id + " inserted.");
|
2019-06-20 17:20:23 +02:00
|
|
|
attemptLogin(accountModel);
|
2019-05-18 10:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
private void attemptLogin(AccountModel accountModel) {
|
2019-07-02 00:46:40 +02:00
|
|
|
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
|
|
|
|
.setHost(accountModel.getJid().getDomain().toString())
|
|
|
|
.setXmppAddressAndPassword(accountModel.getJid(), accountModel.getPassword())
|
|
|
|
.setConnectTimeout(2 * 60 * 1000)
|
|
|
|
.build();
|
2019-06-20 17:20:23 +02:00
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2019-07-02 00:46:40 +02:00
|
|
|
XMPPTCPConnection connection = new XMPPTCPConnection(configuration);
|
2019-06-30 13:37:36 +02:00
|
|
|
connection.setUseStreamManagementResumption(true);
|
|
|
|
connection.setUseStreamManagement(true);
|
2019-06-23 02:34:14 +02:00
|
|
|
MercuryConnection mercuryConnection = new AndroidMercuryConnection(connection, accountModel.getId());
|
2019-06-21 03:45:33 +02:00
|
|
|
connectionService.putConnection(accountModel.getId(), mercuryConnection);
|
2019-06-23 02:34:14 +02:00
|
|
|
mercuryConnection.setRosterHandler(new RoomRosterHandler(mercuryConnection));
|
2019-06-21 03:45:33 +02:00
|
|
|
mercuryConnection.setPlainMessageHandler(new RoomPlainMessageHandler(accountModel.getId()));
|
2019-06-20 17:20:23 +02:00
|
|
|
|
|
|
|
connection.connect().login();
|
2019-06-21 03:45:33 +02:00
|
|
|
Log.d(MercuryImApplication.TAG, "Logged in for " + accountModel.getJid().toString());
|
2019-06-20 17:20:23 +02:00
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (SmackException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
|
|
|
super.finish();
|
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;
|
|
|
|
}
|
|
|
|
}
|