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