001/**
002 *
003 * Copyright © 2016 Fernando Ramirez
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.push_notifications;
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.WeakHashMap;
022
023import org.jivesoftware.smack.ConnectionCreationListener;
024import org.jivesoftware.smack.Manager;
025import org.jivesoftware.smack.SmackException.NoResponseException;
026import org.jivesoftware.smack.SmackException.NotConnectedException;
027import org.jivesoftware.smack.XMPPConnection;
028import org.jivesoftware.smack.XMPPConnectionRegistry;
029import org.jivesoftware.smack.XMPPException.XMPPErrorException;
030import org.jivesoftware.smack.packet.IQ;
031import org.jivesoftware.smack.packet.IQ.Type;
032
033import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
034import org.jivesoftware.smackx.push_notifications.element.DisablePushNotificationsIQ;
035import org.jivesoftware.smackx.push_notifications.element.EnablePushNotificationsIQ;
036import org.jivesoftware.smackx.push_notifications.element.PushNotificationsElements;
037
038import org.jxmpp.jid.Jid;
039
040/**
041 * Push Notifications manager class.
042 *
043 * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
044 *      Notifications</a>
045 * @author Fernando Ramirez
046 *
047 */
048public final class PushNotificationsManager extends Manager {
049
050    static {
051        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
052            @Override
053            public void connectionCreated(XMPPConnection connection) {
054                getInstanceFor(connection);
055            }
056        });
057    }
058
059    private static final Map<XMPPConnection, PushNotificationsManager> INSTANCES = new WeakHashMap<>();
060
061    /**
062     * Get the singleton instance of PushNotificationsManager.
063     *
064     * @param connection TODO javadoc me please
065     * @return the instance of PushNotificationsManager
066     */
067    public static synchronized PushNotificationsManager getInstanceFor(XMPPConnection connection) {
068        PushNotificationsManager pushNotificationsManager = INSTANCES.get(connection);
069
070        if (pushNotificationsManager == null) {
071            pushNotificationsManager = new PushNotificationsManager(connection);
072            INSTANCES.put(connection, pushNotificationsManager);
073        }
074
075        return pushNotificationsManager;
076    }
077
078    private PushNotificationsManager(XMPPConnection connection) {
079        super(connection);
080    }
081
082    /**
083     * Returns true if Push Notifications are supported by this account.
084     *
085     * @return true if Push Notifications are supported by this account.
086     * @throws NoResponseException if there was no response from the remote entity.
087     * @throws XMPPErrorException if there was an XMPP error returned.
088     * @throws NotConnectedException if the XMPP connection is not connected.
089     * @throws InterruptedException if the calling thread was interrupted.
090     * @since 4.2.2
091     */
092    public boolean isSupported()
093                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
094        return ServiceDiscoveryManager.getInstanceFor(connection()).accountSupportsFeatures(
095                        PushNotificationsElements.NAMESPACE);
096    }
097
098    /**
099     * Enable push notifications.
100     *
101     * @param pushJid TODO javadoc me please
102     * @param node TODO javadoc me please
103     * @return true if it was successfully enabled, false if not
104     * @throws NoResponseException if there was no response from the remote entity.
105     * @throws XMPPErrorException if there was an XMPP error returned.
106     * @throws NotConnectedException if the XMPP connection is not connected.
107     * @throws InterruptedException if the calling thread was interrupted.
108     */
109    public boolean enable(Jid pushJid, String node)
110            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
111        return enable(pushJid, node, null);
112    }
113
114    /**
115     * Enable push notifications.
116     *
117     * @param pushJid TODO javadoc me please
118     * @param node TODO javadoc me please
119     * @param publishOptions TODO javadoc me please
120     * @return true if it was successfully enabled, false if not
121     * @throws NoResponseException if there was no response from the remote entity.
122     * @throws XMPPErrorException if there was an XMPP error returned.
123     * @throws NotConnectedException if the XMPP connection is not connected.
124     * @throws InterruptedException if the calling thread was interrupted.
125     */
126    public boolean enable(Jid pushJid, String node, HashMap<String, String> publishOptions)
127            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
128        EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(pushJid, node,
129                publishOptions);
130        return changePushNotificationsStatus(enablePushNotificationsIQ);
131    }
132
133    /**
134     * Disable all push notifications.
135     *
136     * @param pushJid TODO javadoc me please
137     * @return true if it was successfully disabled, false if not
138     * @throws NoResponseException if there was no response from the remote entity.
139     * @throws XMPPErrorException if there was an XMPP error returned.
140     * @throws NotConnectedException if the XMPP connection is not connected.
141     * @throws InterruptedException if the calling thread was interrupted.
142     */
143    public boolean disableAll(Jid pushJid)
144            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
145        return disable(pushJid, null);
146    }
147
148    /**
149     * Disable push notifications of an specific node.
150     *
151     * @param pushJid TODO javadoc me please
152     * @param node TODO javadoc me please
153     * @return true if it was successfully disabled, false if not
154     * @throws NoResponseException if there was no response from the remote entity.
155     * @throws XMPPErrorException if there was an XMPP error returned.
156     * @throws NotConnectedException if the XMPP connection is not connected.
157     * @throws InterruptedException if the calling thread was interrupted.
158     */
159    public boolean disable(Jid pushJid, String node)
160            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
161        DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(pushJid, node);
162        return changePushNotificationsStatus(disablePushNotificationsIQ);
163    }
164
165    private boolean changePushNotificationsStatus(IQ iq)
166            throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException {
167        final XMPPConnection connection = connection();
168        IQ responseIQ = connection.createStanzaCollectorAndSend(iq).nextResultOrThrow();
169        return responseIQ.getType() != Type.error;
170    }
171
172}