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 */ 091 public boolean isSupportedByServer() 092 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 093 return ServiceDiscoveryManager.getInstanceFor(connection()) 094 .serverSupportsFeature(PushNotificationsElements.NAMESPACE); 095 } 096 097 /** 098 * Enable push notifications. 099 * 100 * @param pushJid 101 * @param node 102 * @return true if it was successfully enabled, false if not 103 * @throws NoResponseException 104 * @throws XMPPErrorException 105 * @throws NotConnectedException 106 * @throws InterruptedException 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 117 * @param node 118 * @param publishOptions 119 * @return true if it was successfully enabled, false if not 120 * @throws NoResponseException 121 * @throws XMPPErrorException 122 * @throws NotConnectedException 123 * @throws InterruptedException 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 136 * @return true if it was successfully disabled, false if not 137 * @throws NoResponseException 138 * @throws XMPPErrorException 139 * @throws NotConnectedException 140 * @throws InterruptedException 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 151 * @param node 152 * @return true if it was successfully disabled, false if not 153 * @throws NoResponseException 154 * @throws XMPPErrorException 155 * @throws NotConnectedException 156 * @throws InterruptedException 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.createStanzaCollectorAndSend(iq).nextResultOrThrow(); 168 return responseIQ.getType() != Type.error; 169 } 170 171}