2019-05-01 09:59:10 +02:00
/ * *
*
2020-04-13 15:26:46 +02:00
* Copyright 2019 Aditya Borikar , 2020 Florian Schmaus .
2019-05-01 09:59:10 +02:00
*
* 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.usertune ;
import java.util.Map ;
import java.util.WeakHashMap ;
import org.jivesoftware.smack.Manager ;
import org.jivesoftware.smack.SmackException.NoResponseException ;
import org.jivesoftware.smack.SmackException.NotConnectedException ;
import org.jivesoftware.smack.SmackException.NotLoggedInException ;
import org.jivesoftware.smack.XMPPConnection ;
import org.jivesoftware.smack.XMPPException.XMPPErrorException ;
import org.jivesoftware.smack.packet.Message ;
2019-10-29 21:12:22 +01:00
2020-04-13 15:26:46 +02:00
import org.jivesoftware.smackx.pep.PepEventListener ;
2019-05-01 09:59:10 +02:00
import org.jivesoftware.smackx.pep.PepManager ;
import org.jivesoftware.smackx.pubsub.PayloadItem ;
import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException ;
import org.jivesoftware.smackx.usertune.element.UserTuneElement ;
/ * *
* Entry point for Smacks API for XEP - 0118 : User Tune .
* < br >
* To publish a UserTune , please use { @link # publishUserTune ( UserTuneElement ) } method . This will publish the node .
* < br >
* To stop publishing a UserTune , please use { @link # clearUserTune ( ) } method . This will send a disabling publish signal .
* < br >
2020-04-13 15:26:46 +02:00
* To add a UserTune listener in order to remain updated with other users UserTune , use { @link # addUserTuneListener ( PepEventListener ) } method .
2019-05-01 09:59:10 +02:00
* < br >
* To link a UserTuneElement with { @link Message } , use ' message . addExtension ( userTuneElement ) ' .
* < br >
* An example to illustrate is provided inside UserTuneElementTest inside the test package .
* < br >
* @see < a href = " https://xmpp.org/extensions/xep-0118.html " >
* XEP - 0118 : User Tune < / a >
* /
public final class UserTuneManager extends Manager {
public static final String USERTUNE_NODE = " http://jabber.org/protocol/tune " ;
private static final Map < XMPPConnection , UserTuneManager > INSTANCES = new WeakHashMap < > ( ) ;
private final PepManager pepManager ;
public static synchronized UserTuneManager getInstanceFor ( XMPPConnection connection ) throws NotLoggedInException {
UserTuneManager manager = INSTANCES . get ( connection ) ;
if ( manager = = null ) {
manager = new UserTuneManager ( connection ) ;
INSTANCES . put ( connection , manager ) ;
}
return manager ;
}
2020-04-13 15:26:46 +02:00
private UserTuneManager ( XMPPConnection connection ) {
2019-05-01 09:59:10 +02:00
super ( connection ) ;
pepManager = PepManager . getInstanceFor ( connection ) ;
}
public void clearUserTune ( ) throws NotLoggedInException , NotALeafNodeException , NoResponseException , NotConnectedException , XMPPErrorException , InterruptedException {
publishUserTune ( UserTuneElement . EMPTY_USER_TUNE ) ;
}
public void publishUserTune ( UserTuneElement userTuneElement ) throws NotLoggedInException , NotALeafNodeException , NoResponseException , NotConnectedException , XMPPErrorException , InterruptedException {
// TODO: To prevent a large number of updates when a user is skipping through tracks, an implementation SHOULD wait several seconds before publishing new tune information.
pepManager . publish ( USERTUNE_NODE , new PayloadItem < > ( userTuneElement ) ) ;
}
2020-04-13 15:26:46 +02:00
public boolean addUserTuneListener ( PepEventListener < UserTuneElement > listener ) {
return pepManager . addPepEventListener ( USERTUNE_NODE , UserTuneElement . class , listener ) ;
2019-05-01 09:59:10 +02:00
}
2020-04-13 15:26:46 +02:00
public boolean removeUserTuneListener ( PepEventListener < UserTuneElement > listener ) {
return pepManager . removePepEventListener ( listener ) ;
2019-05-01 09:59:10 +02:00
}
}