SLAM/mobile/src/main/java/de/vanitasvitae/slam/mvp/presenter/LoginPresenter.java

174 lines
6.3 KiB
Java
Raw Normal View History

2018-02-12 02:08:56 +01:00
/*
* Copyright 2018 Paul Schaub
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2018-02-10 17:34:01 +01:00
package de.vanitasvitae.slam.mvp.presenter;
2018-08-01 11:43:18 +02:00
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.packet.Message;
2018-02-10 17:34:01 +01:00
import org.jxmpp.jid.BareJid;
2018-08-01 11:43:18 +02:00
import org.jxmpp.jid.EntityBareJid;
2018-02-10 17:34:01 +01:00
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
2018-08-01 11:43:18 +02:00
import java.io.IOException;
import java.net.UnknownHostException;
2018-02-10 17:34:01 +01:00
import de.vanitasvitae.slam.mvp.contracts.LoginContract;
2018-08-01 11:43:18 +02:00
import de.vanitasvitae.slam.service.SlamXmppConnection;
import de.vanitasvitae.slam.service.SlamXmppService;
import de.vanitasvitae.slam.xmpp.Account;
2018-02-10 17:34:01 +01:00
public class LoginPresenter implements LoginContract.Presenter {
2018-08-01 11:43:18 +02:00
private static final String TAG = "LoginPresenter";
2018-02-10 17:34:01 +01:00
private final LoginContract.View view;
private BareJid jid;
private String password;
2018-08-01 11:43:18 +02:00
private SlamXmppService service;
2018-02-10 17:34:01 +01:00
public LoginPresenter(LoginContract.View view) {
this.view = view;
}
@Override
public void jidChanged(String jid) {
try {
this.jid = JidCreate.entityBareFrom(jid);
view.hideInvalidJidError();
2018-08-01 11:43:18 +02:00
if (password != null && !password.isEmpty()) {
view.enableLoginButton();
}
2018-02-10 17:34:01 +01:00
} catch (XmppStringprepException e) {
this.jid = null;
view.showInvalidJidError();
2018-08-01 11:43:18 +02:00
view.disableLoginButton();
2018-02-10 17:34:01 +01:00
}
}
@Override
public void passwordChanged(String password) {
2018-08-01 11:43:18 +02:00
this.password = password;
if (password == null || password.isEmpty()) {
view.disableLoginButton();
} else if (jid != null) {
view.enableLoginButton();
}
2018-02-10 17:34:01 +01:00
}
@Override
public void loginClicked() {
2018-08-01 11:43:18 +02:00
if (jid == null) {
view.showInvalidJidError();
return;
}
if (password == null) {
view.showInvalidPasswordError();
return;
}
if (service != null) {
view.disableLoginButton();
final Account account = new Account(jid);
2018-02-10 17:34:01 +01:00
2018-08-01 11:43:18 +02:00
Thread loginThread = new Thread(new Runnable() {
@Override
public void run() {
SlamXmppConnection connection = null;
try {
connection = new SlamXmppConnection(
jid.asEntityBareJidOrThrow(), password);
} catch (UnknownHostException e) {
e.printStackTrace();
((AppCompatActivity)view).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText((Context)view, "Unknown host", Toast.LENGTH_SHORT).show();
}
});
return;
}
service.addConnection(account, connection);
final SlamXmppConnection finalConnection = connection;
connection.login(new SlamXmppConnection.LoginCallback() {
@Override
public void success() {
((AppCompatActivity)view).runOnUiThread(new Runnable() {
public void run() {
view.navigateToMainActivity();
}
});
ChatManager.getInstanceFor(finalConnection.getConnection())
.addIncomingListener(new IncomingChatMessageListener() {
@Override
public void newIncomingMessage(final EntityBareJid from, final Message message, Chat chat) {
final AppCompatActivity activity = (AppCompatActivity) view;
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity.getApplicationContext(), from.toString() + ": " + message.getBody(), Toast.LENGTH_LONG).show();
}
});
}
});
}
@Override
public void failure(final Exception e) {
e.printStackTrace();
((AppCompatActivity)view).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText((Context)view, e.getMessage(), Toast.LENGTH_LONG).show();
view.enableLoginButton();
}
});
}
});
}
});
loginThread.start();
} else {
view.showServerNotFoundError();
}
}
@Override
public void bindService(SlamXmppService service) {
Log.d(TAG, "bindService()");
this.service = service;
}
@Override
public void unbindService() {
Log.d(TAG, "unbindService()");
this.service = null;
2018-02-10 17:34:01 +01:00
}
}