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;
024
025import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
026
027public final class SpoilerManager extends Manager {
028
029    public static final String NAMESPACE_0 = "urn:xmpp:spoiler:0";
030
031    private static final Map<XMPPConnection, SpoilerManager> INSTANCES = new WeakHashMap<>();
032
033    private final ServiceDiscoveryManager serviceDiscoveryManager;
034
035    /**
036     * Create a new SpoilerManager and add Spoiler to disco features.
037     *
038     * @param connection xmpp connection
039     */
040    private SpoilerManager(XMPPConnection connection) {
041        super(connection);
042        serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
043    }
044
045    /**
046     * Begin announcing support for Spoiler messages.
047     */
048    public void startAnnounceSupport() {
049        serviceDiscoveryManager.addFeature(NAMESPACE_0);
050    }
051
052    /**
053     * End announcing support for Spoiler messages.
054     */
055    public void stopAnnounceSupport() {
056        serviceDiscoveryManager.removeFeature(NAMESPACE_0);
057    }
058
059    /**
060     * Return the connections instance of the SpoilerManager.
061     *
062     * @param connection xmpp connection
063     * @return SpoilerManager TODO javadoc me please
064     */
065    public static synchronized SpoilerManager getInstanceFor(XMPPConnection connection) {
066        SpoilerManager manager = INSTANCES.get(connection);
067        if (manager == null) {
068            manager = new SpoilerManager(connection);
069            INSTANCES.put(connection, manager);
070        }
071        return manager;
072    }
073}