Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Updated to use the latest firebase SDK version 3. Had trouble getting…
Browse files Browse the repository at this point in the history
… the tests to work and will need to revisit.
  • Loading branch information
fatherOfLegends committed May 21, 2016
1 parent 3b851b4 commit 47fceb0
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 115 deletions.
1 change: 1 addition & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"project_id": "geofire-java",
"firebase": "geofire-java",
"public": "site",
"ignore": [
Expand Down
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,9 @@
</build>
<dependencies>
<dependency>
<groupId>com.firebase</groupId>
<!-- Use the plain java artifact here so there are no Android specific API
calls -->
<artifactId>firebase-client-jvm</artifactId>
<version>[2.0.0,)</version>
<!-- Since there are two artifacts (jvm and android) we only want to make
sure it compiles agains the API and then let people include the
version they want -->
<groupId>com.google.firebase</groupId>
<artifactId>firebase-server-sdk</artifactId>
<version>[3.0.0,)</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
77 changes: 41 additions & 36 deletions src/main/java/com/firebase/geofire/GeoFire.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

package com.firebase.geofire;

import com.firebase.client.*;
import com.firebase.geofire.core.GeoHash;
import com.firebase.geofire.util.GeoUtils;
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 java.util.*;

Expand All @@ -46,10 +48,11 @@ public static 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 key The key whose location was saved
* @param error The error or null if no error occurred
*/
public void onComplete(String key, FirebaseError error);
public void onComplete(String key, DatabaseError error);
}

/**
Expand All @@ -73,63 +76,63 @@ public void onDataChange(DataSnapshot dataSnapshot) {
this.callback.onLocationResult(dataSnapshot.getKey(), location);
} else {
String message = "GeoFire data has invalid format: " + dataSnapshot.getValue();
this.callback.onCancelled(new FirebaseError(FirebaseError.UNKNOWN_ERROR, message));
this.callback.onCancelled(DatabaseError.fromStatus(message));
}
}
}

@Override
public void onCancelled(FirebaseError firebaseError) {
this.callback.onCancelled(firebaseError);
public void onCancelled(DatabaseError databaseError) {
this.callback.onCancelled(databaseError);
}
}

static GeoLocation getLocationValue(DataSnapshot dataSnapshot) {
try {
Map data = dataSnapshot.getValue(Map.class);
List<?> location = (List<?>) data.get("l");
Number latitudeObj = (Number)location.get(0);
Number longitudeObj = (Number)location.get(1);
Number latitudeObj = (Number) location.get(0);
Number longitudeObj = (Number) location.get(1);
double latitude = latitudeObj.doubleValue();
double longitude = longitudeObj.doubleValue();
if (location.size() == 2 && GeoLocation.coordinatesValid(latitude, longitude)) {
return new GeoLocation(latitude, longitude);
} else {
return null;
}
} catch (FirebaseException e) {
return null;
} catch (NullPointerException e) {
return null;
} catch (ClassCastException e) {
return null;
}
}

private final Firebase firebase;
private final DatabaseReference databaseReference;

/**
* Creates a new GeoFire instance at the given Firebase reference.
* @param firebase The Firebase reference this GeoFire instance uses
*
* @param databaseReference The Firebase reference this GeoFire instance uses
*/
public GeoFire(Firebase firebase) {
this.firebase = firebase;
public GeoFire(DatabaseReference databaseReference) {
this.databaseReference = databaseReference;
}

/**
* @return The Firebase reference this GeoFire instance uses
*/
public Firebase getFirebase() {
return this.firebase;
public DatabaseReference getDatabaseReference() {
return this.databaseReference;
}

Firebase firebaseRefForKey(String key) {
return this.firebase.child(key);
DatabaseReference getDatabaseRefForKey(String key) {
return this.databaseReference.child(key);
}

/**
* Sets the location for a given key.
* @param key The key to save the location for
*
* @param key The key to save the location for
* @param location The location of this key
*/
public void setLocation(String key, GeoLocation location) {
Expand All @@ -138,27 +141,26 @@ public void setLocation(String key, GeoLocation location) {

/**
* Sets the location for a given key.
* @param key The key to save the location for
* @param location The location of this key
*
* @param key The key to save the location for
* @param location The location of this key
* @param completionListener A listener that is called once the location was successfully saved on the server or an
* error occurred
*/
public void setLocation(final String key, final GeoLocation location, final CompletionListener completionListener) {
if (key == null) {
throw new NullPointerException();
}
Firebase keyRef = this.firebaseRefForKey(key);
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
GeoHash geoHash = new GeoHash(location);
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("g", geoHash.getGeoHashString());
updates.put("l", new double[]{location.latitude, location.longitude});
if (completionListener != null) {
keyRef.setValue(updates, geoHash.getGeoHashString(), new Firebase.CompletionListener() {
keyRef.setValue(updates, geoHash.getGeoHashString(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(FirebaseError error, Firebase firebase) {
if (completionListener != null) {
completionListener.onComplete(key, error);
}
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
completionListener.onComplete(key, databaseError);
}
});
} else {
Expand All @@ -168,6 +170,7 @@ public void onComplete(FirebaseError error, Firebase firebase) {

/**
* Removes the location for a key from this GeoFire.
*
* @param key The key to remove from this GeoFire
*/
public void removeLocation(String key) {
Expand All @@ -176,20 +179,21 @@ public void removeLocation(String key) {

/**
* Removes the location for a key from this GeoFire.
* @param key The key to remove from this GeoFire
*
* @param key The key to remove from this GeoFire
* @param completionListener A completion listener that is called once the location is successfully removed
* from the server or an error occurred
*/
public void removeLocation(final String key, final CompletionListener completionListener) {
if (key == null) {
throw new NullPointerException();
}
Firebase keyRef = this.firebaseRefForKey(key);
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
if (completionListener != null) {
keyRef.setValue(null, new Firebase.CompletionListener() {
keyRef.setValue(null, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(FirebaseError error, Firebase firebase) {
completionListener.onComplete(key, error);
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
completionListener.onComplete(key, databaseError);
}
});
} else {
Expand All @@ -200,17 +204,18 @@ public void onComplete(FirebaseError error, Firebase firebase) {
/**
* Gets the current location for a key and calls the callback with the current value.
*
* @param key The key whose location to get
* @param key The key whose location to get
* @param callback The callback that is called once the location is retrieved
*/
public void getLocation(String key, LocationCallback callback) {
Firebase keyFirebase = this.firebaseRefForKey(key);
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
LocationValueEventListener valueListener = new LocationValueEventListener(callback);
keyFirebase.addListenerForSingleValueEvent(valueListener);
keyRef.addListenerForSingleValueEvent(valueListener);
}

/**
* Returns a new Query object centered at the given location and with the given radius.
*
* @param center The center of the query
* @param radius The radius of the query, in kilometers
* @return The new GeoQuery object
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/firebase/geofire/GeoQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@

package com.firebase.geofire;

import com.firebase.client.*;
import com.firebase.geofire.core.GeoHash;
import com.firebase.geofire.core.GeoHashQuery;
import com.firebase.geofire.util.GeoUtils;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.*;
import com.google.firebase.database.core.EventTarget;
import com.google.firebase.database.core.Repo;

import java.util.*;

Expand Down Expand Up @@ -80,7 +83,7 @@ public synchronized void onChildMoved(DataSnapshot dataSnapshot, String s) {
}

@Override
public synchronized void onCancelled(FirebaseError firebaseError) {
public synchronized void onCancelled(DatabaseError databaseError) {
// ignore, our API does not support onCancelled
}
};
Expand Down Expand Up @@ -112,8 +115,8 @@ private boolean locationIsInQuery(GeoLocation location) {
}

private void postEvent(Runnable r) {
EventTarget target = Firebase.getDefaultConfig().getEventTarget();
target.postEvent(r);
Repo repo = geoFire.getDatabaseReference().getRepo();
repo.postEvent(r);
}

private void updateLocationInfo(final String key, final GeoLocation location) {
Expand Down Expand Up @@ -209,13 +212,13 @@ public void onDataChange(DataSnapshot dataSnapshot) {
}

@Override
public void onCancelled(final FirebaseError firebaseError) {
public void onCancelled(final DatabaseError databaseError) {
synchronized (GeoQuery.this) {
for (final GeoQueryEventListener listener : GeoQuery.this.eventListeners) {
postEvent(new Runnable() {
@Override
public void run() {
listener.onGeoQueryError(firebaseError);
listener.onGeoQueryError(databaseError);
}
});
}
Expand All @@ -238,8 +241,8 @@ private void setupQueries() {
for (final GeoHashQuery query: newQueries) {
if (!oldQueries.contains(query)) {
outstandingQueries.add(query);
Firebase firebase = this.geoFire.getFirebase();
Query firebaseQuery = firebase.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
DatabaseReference databaseReference = this.geoFire.getDatabaseReference();
Query firebaseQuery = databaseReference.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
firebaseQuery.addChildEventListener(this.childEventLister);
addValueToReadyListener(firebaseQuery, query);
firebaseQueries.put(query, firebaseQuery);
Expand Down Expand Up @@ -283,7 +286,7 @@ private void childRemoved(DataSnapshot dataSnapshot) {
final String key = dataSnapshot.getKey();
final LocationInfo info = this.locationInfos.get(key);
if (info != null) {
this.geoFire.firebaseRefForKey(key).addListenerForSingleValueEvent(new ValueEventListener() {
this.geoFire.getDatabaseRefForKey(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
synchronized(GeoQuery.this) {
Expand All @@ -307,7 +310,7 @@ public void run() {
}

@Override
public void onCancelled(FirebaseError firebaseError) {
public void onCancelled(DatabaseError databaseError) {
// tough luck
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/firebase/geofire/GeoQueryEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

package com.firebase.geofire;

import com.firebase.client.FirebaseError;
import com.google.firebase.database.DatabaseError;

/**
* GeoQuery notifies listeners with this interface about keys that entered, exited, or moved within the query.
Expand Down Expand Up @@ -75,6 +75,6 @@ public interface GeoQueryEventListener {
* Called in case an error occurred while retrieving locations for a query, e.g. violating security rules.
* @param error The error that occurred while retrieving the query
*/
public void onGeoQueryError(FirebaseError error);
public void onGeoQueryError(DatabaseError error);

}
6 changes: 3 additions & 3 deletions src/main/java/com/firebase/geofire/LocationCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

package com.firebase.geofire;

import com.firebase.client.FirebaseError;
import com.google.firebase.database.DatabaseError;

/**
* Classes implementing this interface can be used to receive the locations stored in GeoFire.
Expand All @@ -45,8 +45,8 @@ public interface LocationCallback {

/**
* Called if the callback could not be added due to failure on the server or security rules.
* @param firebaseError The error that occurred
* @param databaseError The error that occurred
*/
public void onCancelled(FirebaseError firebaseError);
public void onCancelled(DatabaseError databaseError);

}
9 changes: 5 additions & 4 deletions src/main/java/com/firebase/geofire/example/Example.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.firebase.geofire.example;

import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.firebase.geofire.GeoQuery;
import com.firebase.geofire.GeoQueryEventListener;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Example {

public static void main(String[] args) throws InterruptedException {
Firebase firebase = new Firebase("https://geofire-v3.firebaseio.com/geofire");
DatabaseReference firebase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://geofire-v3.firebaseio.com/geofire");
GeoFire geoFire = new GeoFire(firebase);
GeoQuery query = geoFire.queryAtLocation(new GeoLocation(37.7, -122.4), 10);
query.addGeoQueryEventListener(new GeoQueryEventListener() {
Expand All @@ -35,7 +36,7 @@ public void onGeoQueryReady() {
}

@Override
public void onGeoQueryError(FirebaseError error) {
public void onGeoQueryError(DatabaseError error) {
System.err.println("There was an error querying locations: " + error.getMessage());
}
});
Expand Down
Loading

0 comments on commit 47fceb0

Please sign in to comment.