From 0c99ac4db1511ca0f07cd85aa8a61cda51ac88f7 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 25 May 2019 09:02:59 +0400 Subject: [PATCH] Fixed user presence system (now user availability/unavailability depends on the life cycle of the MainActivity --- .../chronosx88/influence/XMPPConnection.java | 15 +++++++++++++- .../influence/XMPPConnectionService.java | 7 ------- .../influence/contracts/CoreContracts.kt | 1 + .../influence/presenters/MainPresenter.kt | 20 +++++++++++++++++++ .../influence/views/MainActivity.java | 6 ++++++ 5 files changed, 41 insertions(+), 8 deletions(-) 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 bc7e945..0018d32 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java @@ -125,7 +125,6 @@ public class XMPPConnection implements ConnectionListener { if(mamManager.isSupported()) { MamManager.getInstanceFor(connection).enableMamForAllMessages(); } - connection.sendStanza(new Presence(Presence.Type.available)); } catch (InterruptedException e) { e.printStackTrace(); } @@ -224,4 +223,18 @@ public class XMPPConnection implements ConnectionListener { public Presence getUserPresence(BareJid jid) { return roster.getPresence(jid); } + + public void sendUserPresence(Presence presence) { + if(connection != null) { + if(isConnectionAlive()) { + try { + connection.sendStanza(presence); + } catch (SmackException.NotConnectedException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } } 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 025f059..a20b8a1 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java @@ -75,13 +75,6 @@ public class XMPPConnectionService extends Service { if(connection != null) { thread.interrupt(); thread = null; - try { - connection.getConnection().sendStanza(new Presence(Presence.Type.unavailable)); - } catch (SmackException.NotConnectedException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } connection.disconnect(); connection = null; } diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt index 6f0ac54..12c28ba 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/CoreContracts.kt @@ -66,6 +66,7 @@ interface CoreContracts { fun logoutFromAccount() fun onStart() fun onStop() + fun onDestroy() } interface IMainViewContract { diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt index 6276c6e..50ed7d5 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/MainPresenter.kt @@ -25,9 +25,13 @@ import io.github.chronosx88.influence.logic.MainLogic import io.github.chronosx88.influence.models.appEvents.AuthenticationStatusEvent import io.github.chronosx88.influence.models.appEvents.NewChatEvent import io.github.chronosx88.influence.views.LoginActivity +import java9.util.concurrent.CompletableFuture +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode +import org.jivesoftware.smack.packet.Presence class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract { private val logic: CoreContracts.IMainLogicContract = MainLogic() @@ -54,6 +58,13 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon AppHelper.getContext().startActivity(intent) view.finishActivity() } + + AuthenticationStatusEvent.CONNECT_AND_LOGIN_SUCCESSFUL -> { + GlobalScope.launch { + if(AppHelper.getXmppConnection().isConnectionAlive) + AppHelper.getXmppConnection().sendUserPresence(Presence(Presence.Type.available)) + } + } } } @@ -68,4 +79,13 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon override fun onStop() { EventBus.getDefault().unregister(this) } + + override fun onDestroy() { + GlobalScope.launch { + if(AppHelper.getXmppConnection() != null) { + if(AppHelper.getXmppConnection().isConnectionAlive) + AppHelper.getXmppConnection().sendUserPresence(Presence(Presence.Type.unavailable)) + } + } + } } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java index f220ff9..9dc93a5 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/MainActivity.java @@ -164,4 +164,10 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa public void finishActivity() { finish(); } + + @Override + protected void onDestroy() { + super.onDestroy(); + presenter.onDestroy(); + } }