diff --git a/app/src/main/java/io/github/chronosx88/influence/EmptyLoginCredentialsException.java b/app/src/main/java/io/github/chronosx88/influence/EmptyLoginCredentialsException.java new file mode 100644 index 0000000..e7e978d --- /dev/null +++ b/app/src/main/java/io/github/chronosx88/influence/EmptyLoginCredentialsException.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2019 ChronosX88 + * + * This program 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, see . + */ + +package io.github.chronosx88.influence; + +public class EmptyLoginCredentialsException extends Exception { +} diff --git a/app/src/main/java/io/github/chronosx88/influence/LoginCredentials.java b/app/src/main/java/io/github/chronosx88/influence/LoginCredentials.java index 4f86166..9e89ec9 100644 --- a/app/src/main/java/io/github/chronosx88/influence/LoginCredentials.java +++ b/app/src/main/java/io/github/chronosx88/influence/LoginCredentials.java @@ -21,4 +21,8 @@ public class LoginCredentials { public String username = ""; public String password = ""; public String jabberHost = ""; + + public boolean isEmpty() { + return username.equals("") && password.equals("") && jabberHost.equals(""); + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java b/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java index c5eb369..cb16644 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java @@ -80,7 +80,10 @@ public class XMPPConnection implements ConnectionListener { networkHandler = new NetworkHandler(); } - public void connect() throws XMPPException, IOException, SmackException { + public void connect() throws XMPPException, IOException, SmackException, EmptyLoginCredentialsException { + if(credentials.isEmpty()) { + throw new EmptyLoginCredentialsException(); + } if(connection == null) { XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder() .setXmppDomain(credentials.jabberHost) diff --git a/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java b/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java index 03e20b8..11e5c22 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java @@ -74,6 +74,8 @@ public class XMPPConnectionService extends Service { if(connection != null) { connection.disconnect(); connection = null; + thread.interrupt(); + thread = null; } }); } @@ -87,10 +89,12 @@ public class XMPPConnectionService extends Service { } catch (IOException | SmackException e) { EventBus.getDefault().post(new AuthenticationStatusEvent(AuthenticationStatusEvent.NETWORK_ERROR)); e.printStackTrace(); + onServiceStop(); stopSelf(); - } catch (XMPPException e) { + } catch (XMPPException | EmptyLoginCredentialsException e) { EventBus.getDefault().post(new AuthenticationStatusEvent(AuthenticationStatusEvent.INCORRECT_LOGIN_OR_PASSWORD)); e.printStackTrace(); + onServiceStop(); stopSelf(); } } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java index d1898fb..da3a552 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java @@ -2,6 +2,7 @@ package io.github.chronosx88.influence.helpers; import android.app.Application; import android.content.Context; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; @@ -31,6 +32,7 @@ public class AppHelper extends MultiDexApplication { private static XMPPConnection xmppConnection; private static LoginCredentials currentLoginCredentials; private static Handler mainUIThreadHandler; + private static ServiceConnection serviceConnection; @Override public void onCreate() { @@ -103,4 +105,12 @@ public class AppHelper extends MultiDexApplication { public static Handler getMainUIThread() { return mainUIThreadHandler; } + + public static ServiceConnection getServiceConnection() { + return serviceConnection; + } + + public static void setServiceConnection(ServiceConnection serviceConnection) { + AppHelper.serviceConnection = serviceConnection; + } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt index 6b57551..894d636 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatPresenter.kt @@ -16,6 +16,7 @@ import io.github.chronosx88.influence.models.roomEntities.MessageEntity import java9.util.concurrent.CompletableFuture import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe +import org.greenrobot.eventbus.ThreadMode import org.jxmpp.jid.EntityBareJid import org.jxmpp.jid.impl.JidCreate import org.jxmpp.stringprep.XmppStringprepException @@ -77,7 +78,7 @@ class ChatPresenter(private val view: CoreContracts.IChatViewContract, private v // } - @Subscribe + @Subscribe(threadMode = ThreadMode.MAIN) public fun onNewMessage(event: NewMessageEvent) { if(event.chatID.equals(chatEntity!!.jid)) { val messageID = event.messageID diff --git a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java index 9c64f37..e7a2672 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java @@ -18,8 +18,12 @@ package io.github.chronosx88.influence.views; import android.app.ProgressDialog; +import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import android.text.TextUtils; import android.view.View; import android.widget.Button; @@ -62,7 +66,7 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL passwordInputLayout.setErrorEnabled(true); signInButton = findViewById(R.id.sign_in_button); - progressDialog = new ProgressDialog(LoginActivity.this); + progressDialog = new ProgressDialog(LoginActivity.this, R.style.AlertDialogTheme); progressDialog.setCancelable(false); progressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small); signInButton.setOnClickListener((v) -> { @@ -71,6 +75,7 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL doLogin(); } }); + EventBus.getDefault().register(this); } @Override @@ -126,7 +131,7 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL private void saveLoginCredentials() { AppHelper.getPreferences().edit() .putString("jid", jidEditText.getText().toString()) - .putString("pass", HashUtils.sha1(passwordEditText.getText().toString())) + .putString("pass", passwordEditText.getText().toString()) .putBoolean("logged_in", true) .apply(); } @@ -134,6 +139,20 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL private void doLogin() { loadingScreen(true); startService(new Intent(this, XMPPConnectionService.class)); + ServiceConnection connection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + XMPPConnectionService.XMPPServiceBinder binder = (XMPPConnectionService.XMPPServiceBinder) service; + AppHelper.setXmppConnection(binder.getConnection()); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + AppHelper.setXmppConnection(null); + } + }; + AppHelper.setServiceConnection(connection); + bindService(new Intent(this, XMPPConnectionService.class), connection, Context.BIND_AUTO_CREATE); } @Subscribe(threadMode = ThreadMode.MAIN) @@ -158,14 +177,8 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL } @Override - protected void onStart() { - super.onStart(); - EventBus.getDefault().register(this); - } - - @Override - protected void onStop() { + protected void onDestroy() { + super.onDestroy(); EventBus.getDefault().unregister(this); - super.onStop(); } }