GeoLocationManager.java

  1. /**
  2.  *
  3.  * Copyright 2015-2017 Ishan Khanna, Fernando Ramirez 2019-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.geoloc;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.ConnectionCreationListener;
  21. import org.jivesoftware.smack.Manager;
  22. import org.jivesoftware.smack.SmackException.NoResponseException;
  23. import org.jivesoftware.smack.SmackException.NotConnectedException;
  24. import org.jivesoftware.smack.XMPPConnection;
  25. import org.jivesoftware.smack.XMPPConnectionRegistry;
  26. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  27. import org.jivesoftware.smack.packet.Message;

  28. import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
  29. import org.jivesoftware.smackx.geoloc.provider.GeoLocationProvider;
  30. import org.jivesoftware.smackx.pep.PepEventListener;
  31. import org.jivesoftware.smackx.pep.PepManager;
  32. import org.jivesoftware.smackx.pubsub.PayloadItem;
  33. import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
  34. import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProviderManager;

  35. import org.jxmpp.jid.Jid;

  36. /**
  37.  * Entry point for Smacks API for XEP-0080: User Location.
  38.  * <br>
  39.  * To publish a UserLocation, please use {@link #publishGeoLocation(GeoLocation)} method. This will publish the node.
  40.  * <br>
  41.  * To stop publishing a UserLocation, please use {@link #stopPublishingGeolocation()} method. This will send a disable publishing signal.
  42.  * <br>
  43.  * To add a {@link PepEventListener} in order to remain updated with other users GeoLocation, use {@link #addGeoLocationListener(PepEventListener)} method.
  44.  * <br>
  45.  * To link a GeoLocation with {@link Message}, use `message.addExtension(geoLocation)`.
  46.  * <br>
  47.  * An example for illustration is provided inside GeoLocationTest inside the test package.
  48.  * <br>
  49.  * @see <a href="https://xmpp.org/extensions/xep-0080.html">
  50.  *     XEP-0080: User Location</a>
  51.  */
  52. public final class GeoLocationManager extends Manager {

  53.     public static final String GEOLOCATION_NODE = GeoLocation.NAMESPACE;

  54.     private static final Map<XMPPConnection, GeoLocationManager> INSTANCES = new WeakHashMap<>();

  55.     private final PepManager pepManager;

  56.     static {
  57.         FormFieldChildElementProviderManager.addFormFieldChildElementProvider(
  58.                         GeoLocationProvider.GeoLocationFormFieldChildElementProvider.INSTANCE);

  59.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  60.             @Override
  61.             public void connectionCreated(XMPPConnection connection) {
  62.                 getInstanceFor(connection);
  63.             }
  64.         });
  65.     }

  66.     /**
  67.      * Retrieves a {@link GeoLocationManager} for the specified {@link XMPPConnection}, creating one if it doesn't
  68.      * already exist.
  69.      *
  70.      * @param connection The connection the manager is attached to.
  71.      * @return The new or existing manager.
  72.      */
  73.     public static synchronized GeoLocationManager getInstanceFor(XMPPConnection connection) {
  74.         GeoLocationManager geoLocationManager = INSTANCES.get(connection);
  75.         if (geoLocationManager == null) {
  76.             geoLocationManager = new GeoLocationManager(connection);
  77.             INSTANCES.put(connection, geoLocationManager);
  78.         }
  79.         return geoLocationManager;
  80.     }

  81.     private GeoLocationManager(XMPPConnection connection) {
  82.         super(connection);
  83.         pepManager = PepManager.getInstanceFor(connection);
  84.     }

  85.     public void sendGeoLocationToJid(GeoLocation geoLocation, Jid jid) throws InterruptedException,
  86.                     NotConnectedException {

  87.         final XMPPConnection connection = connection();

  88.         Message geoLocationMessage = connection.getStanzaFactory().buildMessageStanza()
  89.                 .to(jid)
  90.                 .addExtension(geoLocation)
  91.                 .build();

  92.         connection.sendStanza(geoLocationMessage);

  93.     }

  94.     /**
  95.      * Returns true if the message contains a GeoLocation extension.
  96.      *
  97.      * @param message the message to check if contains a GeoLocation extension or not
  98.      * @return a boolean indicating whether the message is a GeoLocation message
  99.      */
  100.     public static boolean isGeoLocationMessage(Message message) {
  101.         return GeoLocation.from(message) != null;
  102.     }

  103.     /**
  104.      * Publish the user's geographic location through the Personal Eventing Protocol (PEP).
  105.      *
  106.      * @param geoLocation the geographic location to publish.
  107.      * @throws InterruptedException if the calling thread was interrupted.
  108.      * @throws NotConnectedException if the XMPP connection is not connected.
  109.      * @throws XMPPErrorException if there was an XMPP error returned.
  110.      * @throws NoResponseException if there was no response from the remote entity.
  111.      * @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
  112.      */
  113.     public void publishGeoLocation(GeoLocation geoLocation)
  114.             throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException {
  115.         pepManager.publish(GEOLOCATION_NODE, new PayloadItem<GeoLocation>(geoLocation));
  116.     }

  117.     /**
  118.      * Send empty geolocation through the PubSub node.
  119.      *
  120.      * @throws InterruptedException if the calling thread was interrupted.
  121.      * @throws NotConnectedException if the XMPP connection is not connected.
  122.      * @throws XMPPErrorException if there was an XMPP error returned.
  123.      * @throws NoResponseException if there was no response from the remote entity.
  124.      * @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
  125.      */
  126.     public void stopPublishingGeolocation()
  127.             throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException {
  128.         pepManager.publish(GEOLOCATION_NODE, new PayloadItem<GeoLocation>(GeoLocation.EMPTY_GEO_LOCATION));
  129.     }

  130.     public boolean addGeoLocationListener(PepEventListener<GeoLocation> listener) {
  131.         return pepManager.addPepEventListener(GEOLOCATION_NODE, GeoLocation.class, listener);
  132.     }

  133.     public boolean removeGeoLocationListener(PepEventListener<GeoLocation> listener) {
  134.         return pepManager.removePepEventListener(listener);
  135.     }
  136. }