001/**
002 *
003 * Copyright 2018 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.spoiler;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.Manager;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
025
026public final class SpoilerManager extends Manager {
027
028    public static final String NAMESPACE_0 = "urn:xmpp:spoiler:0";
029
030    private static final Map<XMPPConnection, SpoilerManager> INSTANCES = new WeakHashMap<>();
031
032    private final ServiceDiscoveryManager serviceDiscoveryManager;
033
034    /**
035     * Create a new SpoilerManager and add Spoiler to disco features.
036     *
037     * @param connection xmpp connection
038     */
039    private SpoilerManager(XMPPConnection connection) {
040        super(connection);
041        serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
042    }
043
044    /**
045     * Begin announcing support for Spoiler messages.
046     */
047    public void startAnnounceSupport() {
048        serviceDiscoveryManager.addFeature(NAMESPACE_0);
049    }
050
051    /**
052     * End announcing support for Spoiler messages.
053     */
054    public void stopAnnounceSupport() {
055        serviceDiscoveryManager.removeFeature(NAMESPACE_0);
056    }
057
058    /**
059     * Return the connections instance of the SpoilerManager.
060     *
061     * @param connection xmpp connection
062     * @return SpoilerManager
063     */
064    public static SpoilerManager getInstanceFor(XMPPConnection connection) {
065        SpoilerManager manager = INSTANCES.get(connection);
066        if (manager == null) {
067            manager = new SpoilerManager(connection);
068            INSTANCES.put(connection, manager);
069        }
070        return manager;
071    }
072}