181 lines
6.1 KiB
Java
181 lines
6.1 KiB
Java
package org.mercury_im.messenger.ui.login;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
import androidx.lifecycle.ViewModel;
|
|
import androidx.annotation.NonNull;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
import org.jivesoftware.smack.XMPPException;
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
|
import org.mercury_im.messenger.core.MercuryConnection;
|
|
import org.mercury_im.messenger.handler.RoomPlainMessageHandler;
|
|
import org.mercury_im.messenger.handler.RoomRosterHandler;
|
|
import org.mercury_im.messenger.persistence.model.AccountModel;
|
|
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
|
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
|
import org.mercury_im.messenger.service.XmppConnectionService;
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
public class LoginViewModel extends ViewModel {
|
|
|
|
@Inject
|
|
AccountRepository accountRepository;
|
|
|
|
private XmppConnectionService connectionService;
|
|
|
|
private String jid;
|
|
private String password;
|
|
|
|
private MutableLiveData<JidError> jidError = new MutableLiveData<>();
|
|
private MutableLiveData<PasswordError> passwordError = new MutableLiveData<>();
|
|
|
|
private MutableLiveData<RoomAccountModel> account = new MutableLiveData<>();
|
|
|
|
public LoginViewModel() {
|
|
super();
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
init(new RoomAccountModel());
|
|
}
|
|
|
|
public enum JidError {
|
|
none,
|
|
emptyJid,
|
|
invalidJid,
|
|
unknownJid
|
|
}
|
|
|
|
public enum PasswordError {
|
|
none,
|
|
emptyPassword,
|
|
invalidPassword,
|
|
incorrectPassword
|
|
}
|
|
|
|
public void onJidInputChanged(String input) {
|
|
this.jid = input;
|
|
|
|
}
|
|
|
|
public void onPasswordInputChanged(String input) {
|
|
this.password = input;
|
|
}
|
|
|
|
public LiveData<JidError> getJidError() {
|
|
return jidError;
|
|
}
|
|
|
|
public LiveData<PasswordError> getPasswordError() {
|
|
return passwordError;
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
public void init(@NonNull RoomAccountModel account) {
|
|
this.account.setValue(account);
|
|
}
|
|
|
|
public MutableLiveData<RoomAccountModel> getAccount() {
|
|
return account;
|
|
}
|
|
|
|
public void login() {
|
|
RoomAccountModel account = getAccount().getValue();
|
|
if (account != null && account.getJid() != null && !TextUtils.isEmpty(account.getPassword())) {
|
|
accountRepository.insertAccount(account);
|
|
}
|
|
}
|
|
|
|
public void loginDetailsEntered() {
|
|
boolean loginIntact = true;
|
|
if (jid.isEmpty()) {
|
|
jidError.postValue(JidError.emptyJid);
|
|
loginIntact = false;
|
|
}
|
|
|
|
EntityBareJid bareJid = asValidJidOrNull(jid);
|
|
if (bareJid == null) {
|
|
jidError.postValue(JidError.invalidJid);
|
|
loginIntact = false;
|
|
}
|
|
|
|
if (!isPasswordValid(password)) {
|
|
passwordError.postValue(PasswordError.invalidPassword);
|
|
loginIntact = false;
|
|
}
|
|
|
|
if (loginIntact) {
|
|
RoomAccountModel accountModel = new RoomAccountModel();
|
|
accountModel.setEnabled(true);
|
|
accountModel.setJid(bareJid);
|
|
accountModel.setPassword(password);
|
|
long id = accountRepository.insertAccount(accountModel);
|
|
accountModel.setId(id);
|
|
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + id + " inserted.");
|
|
attemptLogin(accountModel);
|
|
}
|
|
}
|
|
|
|
private void attemptLogin(AccountModel accountModel) {
|
|
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
|
|
.setHost(accountModel.getJid().getDomain().toString())
|
|
.setXmppAddressAndPassword(accountModel.getJid(), accountModel.getPassword())
|
|
.setConnectTimeout(2 * 60 * 1000)
|
|
.build();
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
XMPPTCPConnection connection = new XMPPTCPConnection(configuration);
|
|
connection.setUseStreamManagementResumption(true);
|
|
connection.setUseStreamManagement(true);
|
|
MercuryConnection mercuryConnection = new MercuryConnection(connection, accountModel.getId());
|
|
connectionService.putConnection(accountModel.getId(), mercuryConnection);
|
|
mercuryConnection.setRosterHandler(new RoomRosterHandler(mercuryConnection));
|
|
mercuryConnection.setPlainMessageHandler(new RoomPlainMessageHandler(accountModel.getId()));
|
|
|
|
connection.connect().login();
|
|
Log.d(MercuryImApplication.TAG, "Logged in for " + accountModel.getJid().toString());
|
|
} 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();
|
|
}
|
|
|
|
public void setConnectionService(XmppConnectionService connectionService) {
|
|
this.connectionService = connectionService;
|
|
}
|
|
}
|