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