From f1fb03d08cbe2aaf99e4fbc18930d0c2e6c353e7 Mon Sep 17 00:00:00 2001 From: V Lau Date: Sat, 19 Jan 2019 13:36:38 -0800 Subject: [PATCH] Add a success callback for auto-join on reconnect. When auto-join on reconnection is success, invoke a callback. Conform to the proper copyright format. --- .../smackx/muc/AutoJoinSuccessCallback.java | 31 +++++++++++++++++++ .../smackx/muc/MultiUserChatManager.java | 21 +++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/muc/AutoJoinSuccessCallback.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/AutoJoinSuccessCallback.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/AutoJoinSuccessCallback.java new file mode 100644 index 000000000..87bc0b19f --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/AutoJoinSuccessCallback.java @@ -0,0 +1,31 @@ +/** + * + * Copyright 2019 Vincent Lau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.muc; + +import org.jxmpp.jid.parts.Resourcepart; + +public interface AutoJoinSuccessCallback { + + /** + * Invoked after the automatic rejoin rooms on reconnect success. + * + * @param muc the joined MultiUserChat. + * @param nickname nickname used by participant to join the room. + */ + void autoJoinSuccess(MultiUserChat muc, Resourcepart nickname); + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index 639e2f792..7d4691370 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -152,6 +152,8 @@ public final class MultiUserChatManager extends Manager { private AutoJoinFailedCallback autoJoinFailedCallback; + private AutoJoinSuccessCallback autoJoinSuccessCallback; + private final ServiceDiscoveryManager serviceDiscoveryManager; private MultiUserChatManager(XMPPConnection connection) { @@ -200,6 +202,7 @@ public final class MultiUserChatManager extends Manager { @Override public void run() { final AutoJoinFailedCallback failedCallback = autoJoinFailedCallback; + final AutoJoinSuccessCallback successCallback = autoJoinSuccessCallback; for (EntityBareJid mucJid : mucs) { MultiUserChat muc = getMultiUserChat(mucJid); @@ -221,6 +224,9 @@ public final class MultiUserChatManager extends Manager { } try { muc.join(nickname); + if (successCallback != null) { + successCallback.autoJoinSuccess(muc, nickname); + } } catch (NotAMucServiceException | NoResponseException | XMPPErrorException | NotConnectedException | InterruptedException e) { if (failedCallback != null) { @@ -481,6 +487,21 @@ public final class MultiUserChatManager extends Manager { } } + /** + * Set a callback invoked by this manager when automatic join on reconnect success. + * If successCallback is not null, automatic rejoin will also + * be enabled. + * + * @param successCallback the callback + */ + public void setAutoJoinSuccessCallback(AutoJoinSuccessCallback successCallback) { + autoJoinSuccessCallback = successCallback; + if (successCallback != null) { + setAutoJoinOnReconnect(true); + } + } + + void addJoinedRoom(EntityBareJid room) { joinedRooms.add(room); }