001/**
002 *
003 * Copyright 2015-2016 Ishan Khanna
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.geoloc;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.Manager;
024import org.jivesoftware.smack.SmackException.NotConnectedException;
025import org.jivesoftware.smack.XMPPConnection;
026import org.jivesoftware.smack.XMPPConnectionRegistry;
027import org.jivesoftware.smack.packet.Message;
028
029import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
030
031import org.jxmpp.jid.Jid;
032
033public final class GeoLocationManager extends Manager {
034
035    private static final Map<XMPPConnection, GeoLocationManager> INSTANCES = new WeakHashMap<>();
036
037    static {
038        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
039            @Override
040            public void connectionCreated(XMPPConnection connection) {
041                getInstanceFor(connection);
042            }
043        });
044    }
045
046    public GeoLocationManager(XMPPConnection connection) {
047        super(connection);
048
049    }
050
051    /**
052     * Retrieves a {@link GeoLocationManager} for the specified {@link XMPPConnection}, creating one if it doesn't
053     * already exist.
054     * 
055     * @param connection The connection the manager is attached to.
056     * @return The new or existing manager.
057     */
058    public synchronized static GeoLocationManager getInstanceFor(XMPPConnection connection) {
059        GeoLocationManager geoLocationManager = INSTANCES.get(connection);
060        if (geoLocationManager == null) {
061            geoLocationManager = new GeoLocationManager(connection);
062            INSTANCES.put(connection, geoLocationManager);
063        }
064        return geoLocationManager;
065    }
066
067    public void sendGeoLocationToJid(GeoLocation geoLocation, Jid jid) throws InterruptedException,
068                    NotConnectedException {
069
070        final XMPPConnection connection = connection();
071
072        Message geoLocationMessage = new Message(jid);
073        geoLocationMessage.addExtension(geoLocation);
074
075        connection.sendStanza(geoLocationMessage);
076
077    }
078
079    /**
080     * Returns true if the message contains a GeoLocation extension.
081     * 
082     * @param message the message to check if contains a GeoLocation extension or not
083     * @return a boolean indicating whether the message is a GeoLocation message
084     */
085    public static boolean isGeoLocationMessage(Message message) {
086        return GeoLocation.from(message) != null;
087    }
088
089}