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
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 is supported by the server.
084     *
085     * @return true if Push Notifications is supported by the server.
086     * @throws NoResponseException
087     * @throws XMPPErrorException
088     * @throws NotConnectedException
089     * @throws InterruptedException
090     * @deprecated Use {@link #isSupported()} instead.
091     */
092    @Deprecated
093    // TODO: Remove in Smack 4.3
094    public boolean isSupportedByServer()
095            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
096        return ServiceDiscoveryManager.getInstanceFor(connection())
097                .serverSupportsFeature(PushNotificationsElements.NAMESPACE);
098    }
099
100    /**
101     * Returns true if Push Notifications are supported by this account.
102     *
103     * @return true if Push Notifications are supported by this account.
104     * @throws NoResponseException
105     * @throws XMPPErrorException
106     * @throws NotConnectedException
107     * @throws InterruptedException
108     * @since 4.2.2
109     */
110    public boolean isSupported()
111                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
112        return ServiceDiscoveryManager.getInstanceFor(connection()).accountSupportsFeatures(
113                        PushNotificationsElements.NAMESPACE);
114    }
115
116    /**
117     * Enable push notifications.
118     * 
119     * @param pushJid
120     * @param node
121     * @return true if it was successfully enabled, false if not
122     * @throws NoResponseException
123     * @throws XMPPErrorException
124     * @throws NotConnectedException
125     * @throws InterruptedException
126     */
127    public boolean enable(Jid pushJid, String node)
128            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
129        return enable(pushJid, node, null);
130    }
131
132    /**
133     * Enable push notifications.
134     * 
135     * @param pushJid
136     * @param node
137     * @param publishOptions
138     * @return true if it was successfully enabled, false if not
139     * @throws NoResponseException
140     * @throws XMPPErrorException
141     * @throws NotConnectedException
142     * @throws InterruptedException
143     */
144    public boolean enable(Jid pushJid, String node, HashMap<String, String> publishOptions)
145            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
146        EnablePushNotificationsIQ enablePushNotificationsIQ = new EnablePushNotificationsIQ(pushJid, node,
147                publishOptions);
148        return changePushNotificationsStatus(enablePushNotificationsIQ);
149    }
150
151    /**
152     * Disable all push notifications.
153     * 
154     * @param pushJid
155     * @return true if it was successfully disabled, false if not
156     * @throws NoResponseException
157     * @throws XMPPErrorException
158     * @throws NotConnectedException
159     * @throws InterruptedException
160     */
161    public boolean disableAll(Jid pushJid)
162            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
163        return disable(pushJid, null);
164    }
165
166    /**
167     * Disable push notifications of an specific node.
168     * 
169     * @param pushJid
170     * @param node
171     * @return true if it was successfully disabled, false if not
172     * @throws NoResponseException
173     * @throws XMPPErrorException
174     * @throws NotConnectedException
175     * @throws InterruptedException
176     */
177    public boolean disable(Jid pushJid, String node)
178            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
179        DisablePushNotificationsIQ disablePushNotificationsIQ = new DisablePushNotificationsIQ(pushJid, node);
180        return changePushNotificationsStatus(disablePushNotificationsIQ);
181    }
182
183    private boolean changePushNotificationsStatus(IQ iq)
184            throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException {
185        final XMPPConnection connection = connection();
186        IQ responseIQ = connection.createStanzaCollectorAndSend(iq).nextResultOrThrow();
187        return responseIQ.getType() != Type.error;
188    }
189
190}