2015-03-13 07:08:21 +01:00
|
|
|
/**
|
|
|
|
*
|
2020-04-13 12:14:32 +02:00
|
|
|
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez 2019-2020 Florian Schmaus
|
2015-03-13 07:08:21 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.geoloc;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
|
|
|
import org.jivesoftware.smack.Manager;
|
2018-04-04 15:02:23 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2015-03-13 07:08:21 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
2018-04-04 15:02:23 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2015-03-13 07:08:21 +01:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2020-04-13 15:26:46 +02:00
|
|
|
|
2015-03-13 07:08:21 +01:00
|
|
|
import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
|
2019-06-12 15:49:45 +02:00
|
|
|
import org.jivesoftware.smackx.geoloc.provider.GeoLocationProvider;
|
2020-04-13 15:26:46 +02:00
|
|
|
import org.jivesoftware.smackx.pep.PepEventListener;
|
2019-10-20 13:00:38 +02:00
|
|
|
import org.jivesoftware.smackx.pep.PepManager;
|
2018-04-04 15:02:23 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.PayloadItem;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
|
2019-06-12 15:49:45 +02:00
|
|
|
import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProviderManager;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-03-13 07:08:21 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
|
2019-10-20 13:00:38 +02:00
|
|
|
/**
|
|
|
|
* Entry point for Smacks API for XEP-0080: User Location.
|
|
|
|
* <br>
|
2020-04-13 15:26:46 +02:00
|
|
|
* To publish a UserLocation, please use {@link #publishGeoLocation(GeoLocation)} method. This will publish the node.
|
2019-10-20 13:00:38 +02:00
|
|
|
* <br>
|
|
|
|
* To stop publishing a UserLocation, please use {@link #stopPublishingGeolocation()} method. This will send a disble publishing signal.
|
|
|
|
* <br>
|
2020-04-13 15:26:46 +02:00
|
|
|
* To add a {@link PepEventListener} in order to remain updated with other users GeoLocation, use {@link #addGeoLocationListener(PepEventListener)} method.
|
2019-10-20 13:00:38 +02:00
|
|
|
* <br>
|
|
|
|
* To link a GeoLocation with {@link Message}, use `message.addExtension(geoLocation)`.
|
|
|
|
* <br>
|
|
|
|
* An example for illustration is provided inside GeoLocationTest inside the test package.
|
|
|
|
* <br>
|
|
|
|
* @see <a href="https://xmpp.org/extensions/xep-0080.html">
|
|
|
|
* XEP-0080: User Location</a>
|
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class GeoLocationManager extends Manager {
|
2015-03-13 07:08:21 +01:00
|
|
|
|
2020-04-13 15:26:46 +02:00
|
|
|
public static final String GEOLOCATION_NODE = GeoLocation.NAMESPACE;
|
2019-10-20 13:00:38 +02:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final Map<XMPPConnection, GeoLocationManager> INSTANCES = new WeakHashMap<>();
|
2015-03-13 07:08:21 +01:00
|
|
|
|
2019-10-20 13:00:38 +02:00
|
|
|
private final PepManager pepManager;
|
|
|
|
|
2015-03-13 07:08:21 +01:00
|
|
|
static {
|
2019-06-12 15:49:45 +02:00
|
|
|
FormFieldChildElementProviderManager.addFormFieldChildElementProvider(
|
|
|
|
GeoLocationProvider.GeoLocationFormFieldChildElementProvider.INSTANCE);
|
|
|
|
|
2015-03-13 07:08:21 +01:00
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-03-13 07:08:21 +01:00
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
|
|
|
getInstanceFor(connection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a {@link GeoLocationManager} for the specified {@link XMPPConnection}, creating one if it doesn't
|
|
|
|
* already exist.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-03-13 07:08:21 +01:00
|
|
|
* @param connection The connection the manager is attached to.
|
|
|
|
* @return The new or existing manager.
|
|
|
|
*/
|
2018-03-29 12:35:11 +02:00
|
|
|
public static synchronized GeoLocationManager getInstanceFor(XMPPConnection connection) {
|
2015-03-13 07:08:21 +01:00
|
|
|
GeoLocationManager geoLocationManager = INSTANCES.get(connection);
|
|
|
|
if (geoLocationManager == null) {
|
|
|
|
geoLocationManager = new GeoLocationManager(connection);
|
|
|
|
INSTANCES.put(connection, geoLocationManager);
|
|
|
|
}
|
|
|
|
return geoLocationManager;
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:00:38 +02:00
|
|
|
private GeoLocationManager(XMPPConnection connection) {
|
|
|
|
super(connection);
|
|
|
|
pepManager = PepManager.getInstanceFor(connection);
|
|
|
|
}
|
|
|
|
|
2015-03-13 07:08:21 +01:00
|
|
|
public void sendGeoLocationToJid(GeoLocation geoLocation, Jid jid) throws InterruptedException,
|
|
|
|
NotConnectedException {
|
|
|
|
|
|
|
|
final XMPPConnection connection = connection();
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
Message geoLocationMessage = connection.getStanzaFactory().buildMessageStanza()
|
|
|
|
.to(jid)
|
|
|
|
.addExtension(geoLocation)
|
|
|
|
.build();
|
2015-03-13 07:08:21 +01:00
|
|
|
|
|
|
|
connection.sendStanza(geoLocationMessage);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the message contains a GeoLocation extension.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-03-13 07:08:21 +01:00
|
|
|
* @param message the message to check if contains a GeoLocation extension or not
|
|
|
|
* @return a boolean indicating whether the message is a GeoLocation message
|
|
|
|
*/
|
|
|
|
public static boolean isGeoLocationMessage(Message message) {
|
|
|
|
return GeoLocation.from(message) != null;
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:02:23 +02:00
|
|
|
/**
|
2020-04-13 15:26:46 +02:00
|
|
|
* Publish the user's geographic location through the Personal Eventing Protocol (PEP).
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2020-04-13 15:26:46 +02:00
|
|
|
* @param geoLocation the geographic location to publish.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws XMPPErrorException if there was an XMPP error returned.
|
|
|
|
* @throws NoResponseException if there was no response from the remote entity.
|
|
|
|
* @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
|
2018-04-04 15:02:23 +02:00
|
|
|
*/
|
2020-04-13 15:26:46 +02:00
|
|
|
public void publishGeoLocation(GeoLocation geoLocation)
|
2018-04-04 15:02:23 +02:00
|
|
|
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException {
|
2020-04-13 15:26:46 +02:00
|
|
|
pepManager.publish(GEOLOCATION_NODE, new PayloadItem<GeoLocation>(geoLocation));
|
2018-04-04 15:02:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send empty geolocation through the PubSub node.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws XMPPErrorException if there was an XMPP error returned.
|
|
|
|
* @throws NoResponseException if there was no response from the remote entity.
|
|
|
|
* @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
|
2018-04-04 15:02:23 +02:00
|
|
|
*/
|
|
|
|
public void stopPublishingGeolocation()
|
|
|
|
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException {
|
2020-04-13 15:26:46 +02:00
|
|
|
pepManager.publish(GEOLOCATION_NODE, new PayloadItem<GeoLocation>(GeoLocation.EMPTY_GEO_LOCATION));
|
2018-04-04 15:02:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:26:46 +02:00
|
|
|
public boolean addGeoLocationListener(PepEventListener<GeoLocation> listener) {
|
|
|
|
return pepManager.addPepEventListener(GEOLOCATION_NODE, GeoLocation.class, listener);
|
2019-10-20 13:00:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:26:46 +02:00
|
|
|
public boolean removeGeoLocationListener(PepEventListener<GeoLocation> listener) {
|
|
|
|
return pepManager.removePepEventListener(listener);
|
2019-10-20 13:00:38 +02:00
|
|
|
}
|
2015-03-13 07:08:21 +01:00
|
|
|
}
|