com.firebase.geofire.GeoFire Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geofire-java Show documentation
Show all versions of geofire-java Show documentation
GeoFire is an open-source library for Android/Java that allows you to store and query a set of keys based on their geographic location.
/*
* Firebase GeoFire Java Library
*
* Copyright © 2014 Firebase - All Rights Reserved
* https://www.firebase.com
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binaryform must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY FIREBASE AS IS AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL FIREBASE BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.firebase.geofire;
import com.firebase.geofire.core.GeoHash;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.GenericTypeIndicator;
import java.lang.Throwable;
import java.util.*;
import java.util.logging.Logger;
import static com.firebase.geofire.util.GeoUtils.capRadius;
/**
* A GeoFire instance is used to store geo location data in Firebase.
*/
public class GeoFire {
public static Logger LOGGER = Logger.getLogger("GeoFire");
/**
* A listener that can be used to be notified about a successful write or an error on writing.
*/
public interface CompletionListener {
/**
* Called once a location was successfully saved on the server or an error occurred. On success, the parameter
* error will be null; in case of an error, the error will be passed to this method.
*
* @param key The key whose location was saved
* @param error The error or null if no error occurred
*/
void onComplete(String key, DatabaseError error);
}
/**
* A small wrapper class to forward any events to the LocationEventListener.
*/
private static class LocationValueEventListener implements ValueEventListener {
private final LocationCallback callback;
LocationValueEventListener(LocationCallback callback) {
this.callback = callback;
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) {
this.callback.onLocationResult(dataSnapshot.getKey(), null);
} else {
GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
if (location != null) {
this.callback.onLocationResult(dataSnapshot.getKey(), location);
} else {
String message = "GeoFire data has invalid format: " + dataSnapshot.getValue();
this.callback.onCancelled(DatabaseError.fromException(new Throwable(message)));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
this.callback.onCancelled(databaseError);
}
}
public static GeoLocation getLocationValue(DataSnapshot dataSnapshot) {
try {
GenericTypeIndicator