Skip to content

Commit 47fceb0

Browse files
Updated to use the latest firebase SDK version 3. Had trouble getting the tests to work and will need to revisit.
1 parent 3b851b4 commit 47fceb0

File tree

13 files changed

+131
-115
lines changed

13 files changed

+131
-115
lines changed

firebase.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"project_id": "geofire-java",
23
"firebase": "geofire-java",
34
"public": "site",
45
"ignore": [

pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,9 @@
7878
</build>
7979
<dependencies>
8080
<dependency>
81-
<groupId>com.firebase</groupId>
82-
<!-- Use the plain java artifact here so there are no Android specific API
83-
calls -->
84-
<artifactId>firebase-client-jvm</artifactId>
85-
<version>[2.0.0,)</version>
86-
<!-- Since there are two artifacts (jvm and android) we only want to make
87-
sure it compiles agains the API and then let people include the
88-
version they want -->
81+
<groupId>com.google.firebase</groupId>
82+
<artifactId>firebase-server-sdk</artifactId>
83+
<version>[3.0.0,)</version>
8984
<scope>provided</scope>
9085
</dependency>
9186
<dependency>

src/main/java/com/firebase/geofire/GeoFire.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
package com.firebase.geofire;
3030

31-
import com.firebase.client.*;
3231
import com.firebase.geofire.core.GeoHash;
33-
import com.firebase.geofire.util.GeoUtils;
32+
import com.google.firebase.database.DataSnapshot;
33+
import com.google.firebase.database.DatabaseError;
34+
import com.google.firebase.database.DatabaseReference;
35+
import com.google.firebase.database.ValueEventListener;
3436

3537
import java.util.*;
3638

@@ -46,10 +48,11 @@ public static interface CompletionListener {
4648
/**
4749
* Called once a location was successfully saved on the server or an error occurred. On success, the parameter
4850
* error will be null; in case of an error, the error will be passed to this method.
49-
* @param key The key whose location was saved
51+
*
52+
* @param key The key whose location was saved
5053
* @param error The error or null if no error occurred
5154
*/
52-
public void onComplete(String key, FirebaseError error);
55+
public void onComplete(String key, DatabaseError error);
5356
}
5457

5558
/**
@@ -73,63 +76,63 @@ public void onDataChange(DataSnapshot dataSnapshot) {
7376
this.callback.onLocationResult(dataSnapshot.getKey(), location);
7477
} else {
7578
String message = "GeoFire data has invalid format: " + dataSnapshot.getValue();
76-
this.callback.onCancelled(new FirebaseError(FirebaseError.UNKNOWN_ERROR, message));
79+
this.callback.onCancelled(DatabaseError.fromStatus(message));
7780
}
7881
}
7982
}
8083

8184
@Override
82-
public void onCancelled(FirebaseError firebaseError) {
83-
this.callback.onCancelled(firebaseError);
85+
public void onCancelled(DatabaseError databaseError) {
86+
this.callback.onCancelled(databaseError);
8487
}
8588
}
8689

8790
static GeoLocation getLocationValue(DataSnapshot dataSnapshot) {
8891
try {
8992
Map data = dataSnapshot.getValue(Map.class);
9093
List<?> location = (List<?>) data.get("l");
91-
Number latitudeObj = (Number)location.get(0);
92-
Number longitudeObj = (Number)location.get(1);
94+
Number latitudeObj = (Number) location.get(0);
95+
Number longitudeObj = (Number) location.get(1);
9396
double latitude = latitudeObj.doubleValue();
9497
double longitude = longitudeObj.doubleValue();
9598
if (location.size() == 2 && GeoLocation.coordinatesValid(latitude, longitude)) {
9699
return new GeoLocation(latitude, longitude);
97100
} else {
98101
return null;
99102
}
100-
} catch (FirebaseException e) {
101-
return null;
102103
} catch (NullPointerException e) {
103104
return null;
104105
} catch (ClassCastException e) {
105106
return null;
106107
}
107108
}
108109

109-
private final Firebase firebase;
110+
private final DatabaseReference databaseReference;
110111

111112
/**
112113
* Creates a new GeoFire instance at the given Firebase reference.
113-
* @param firebase The Firebase reference this GeoFire instance uses
114+
*
115+
* @param databaseReference The Firebase reference this GeoFire instance uses
114116
*/
115-
public GeoFire(Firebase firebase) {
116-
this.firebase = firebase;
117+
public GeoFire(DatabaseReference databaseReference) {
118+
this.databaseReference = databaseReference;
117119
}
118120

119121
/**
120122
* @return The Firebase reference this GeoFire instance uses
121123
*/
122-
public Firebase getFirebase() {
123-
return this.firebase;
124+
public DatabaseReference getDatabaseReference() {
125+
return this.databaseReference;
124126
}
125127

126-
Firebase firebaseRefForKey(String key) {
127-
return this.firebase.child(key);
128+
DatabaseReference getDatabaseRefForKey(String key) {
129+
return this.databaseReference.child(key);
128130
}
129131

130132
/**
131133
* Sets the location for a given key.
132-
* @param key The key to save the location for
134+
*
135+
* @param key The key to save the location for
133136
* @param location The location of this key
134137
*/
135138
public void setLocation(String key, GeoLocation location) {
@@ -138,27 +141,26 @@ public void setLocation(String key, GeoLocation location) {
138141

139142
/**
140143
* Sets the location for a given key.
141-
* @param key The key to save the location for
142-
* @param location The location of this key
144+
*
145+
* @param key The key to save the location for
146+
* @param location The location of this key
143147
* @param completionListener A listener that is called once the location was successfully saved on the server or an
144148
* error occurred
145149
*/
146150
public void setLocation(final String key, final GeoLocation location, final CompletionListener completionListener) {
147151
if (key == null) {
148152
throw new NullPointerException();
149153
}
150-
Firebase keyRef = this.firebaseRefForKey(key);
154+
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
151155
GeoHash geoHash = new GeoHash(location);
152156
Map<String, Object> updates = new HashMap<String, Object>();
153157
updates.put("g", geoHash.getGeoHashString());
154158
updates.put("l", new double[]{location.latitude, location.longitude});
155159
if (completionListener != null) {
156-
keyRef.setValue(updates, geoHash.getGeoHashString(), new Firebase.CompletionListener() {
160+
keyRef.setValue(updates, geoHash.getGeoHashString(), new DatabaseReference.CompletionListener() {
157161
@Override
158-
public void onComplete(FirebaseError error, Firebase firebase) {
159-
if (completionListener != null) {
160-
completionListener.onComplete(key, error);
161-
}
162+
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
163+
completionListener.onComplete(key, databaseError);
162164
}
163165
});
164166
} else {
@@ -168,6 +170,7 @@ public void onComplete(FirebaseError error, Firebase firebase) {
168170

169171
/**
170172
* Removes the location for a key from this GeoFire.
173+
*
171174
* @param key The key to remove from this GeoFire
172175
*/
173176
public void removeLocation(String key) {
@@ -176,20 +179,21 @@ public void removeLocation(String key) {
176179

177180
/**
178181
* Removes the location for a key from this GeoFire.
179-
* @param key The key to remove from this GeoFire
182+
*
183+
* @param key The key to remove from this GeoFire
180184
* @param completionListener A completion listener that is called once the location is successfully removed
181185
* from the server or an error occurred
182186
*/
183187
public void removeLocation(final String key, final CompletionListener completionListener) {
184188
if (key == null) {
185189
throw new NullPointerException();
186190
}
187-
Firebase keyRef = this.firebaseRefForKey(key);
191+
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
188192
if (completionListener != null) {
189-
keyRef.setValue(null, new Firebase.CompletionListener() {
193+
keyRef.setValue(null, new DatabaseReference.CompletionListener() {
190194
@Override
191-
public void onComplete(FirebaseError error, Firebase firebase) {
192-
completionListener.onComplete(key, error);
195+
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
196+
completionListener.onComplete(key, databaseError);
193197
}
194198
});
195199
} else {
@@ -200,17 +204,18 @@ public void onComplete(FirebaseError error, Firebase firebase) {
200204
/**
201205
* Gets the current location for a key and calls the callback with the current value.
202206
*
203-
* @param key The key whose location to get
207+
* @param key The key whose location to get
204208
* @param callback The callback that is called once the location is retrieved
205209
*/
206210
public void getLocation(String key, LocationCallback callback) {
207-
Firebase keyFirebase = this.firebaseRefForKey(key);
211+
DatabaseReference keyRef = this.getDatabaseRefForKey(key);
208212
LocationValueEventListener valueListener = new LocationValueEventListener(callback);
209-
keyFirebase.addListenerForSingleValueEvent(valueListener);
213+
keyRef.addListenerForSingleValueEvent(valueListener);
210214
}
211215

212216
/**
213217
* Returns a new Query object centered at the given location and with the given radius.
218+
*
214219
* @param center The center of the query
215220
* @param radius The radius of the query, in kilometers
216221
* @return The new GeoQuery object

src/main/java/com/firebase/geofire/GeoQuery.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828

2929
package com.firebase.geofire;
3030

31-
import com.firebase.client.*;
3231
import com.firebase.geofire.core.GeoHash;
3332
import com.firebase.geofire.core.GeoHashQuery;
3433
import com.firebase.geofire.util.GeoUtils;
34+
import com.google.firebase.FirebaseApp;
35+
import com.google.firebase.database.*;
36+
import com.google.firebase.database.core.EventTarget;
37+
import com.google.firebase.database.core.Repo;
3538

3639
import java.util.*;
3740

@@ -80,7 +83,7 @@ public synchronized void onChildMoved(DataSnapshot dataSnapshot, String s) {
8083
}
8184

8285
@Override
83-
public synchronized void onCancelled(FirebaseError firebaseError) {
86+
public synchronized void onCancelled(DatabaseError databaseError) {
8487
// ignore, our API does not support onCancelled
8588
}
8689
};
@@ -112,8 +115,8 @@ private boolean locationIsInQuery(GeoLocation location) {
112115
}
113116

114117
private void postEvent(Runnable r) {
115-
EventTarget target = Firebase.getDefaultConfig().getEventTarget();
116-
target.postEvent(r);
118+
Repo repo = geoFire.getDatabaseReference().getRepo();
119+
repo.postEvent(r);
117120
}
118121

119122
private void updateLocationInfo(final String key, final GeoLocation location) {
@@ -209,13 +212,13 @@ public void onDataChange(DataSnapshot dataSnapshot) {
209212
}
210213

211214
@Override
212-
public void onCancelled(final FirebaseError firebaseError) {
215+
public void onCancelled(final DatabaseError databaseError) {
213216
synchronized (GeoQuery.this) {
214217
for (final GeoQueryEventListener listener : GeoQuery.this.eventListeners) {
215218
postEvent(new Runnable() {
216219
@Override
217220
public void run() {
218-
listener.onGeoQueryError(firebaseError);
221+
listener.onGeoQueryError(databaseError);
219222
}
220223
});
221224
}
@@ -238,8 +241,8 @@ private void setupQueries() {
238241
for (final GeoHashQuery query: newQueries) {
239242
if (!oldQueries.contains(query)) {
240243
outstandingQueries.add(query);
241-
Firebase firebase = this.geoFire.getFirebase();
242-
Query firebaseQuery = firebase.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
244+
DatabaseReference databaseReference = this.geoFire.getDatabaseReference();
245+
Query firebaseQuery = databaseReference.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
243246
firebaseQuery.addChildEventListener(this.childEventLister);
244247
addValueToReadyListener(firebaseQuery, query);
245248
firebaseQueries.put(query, firebaseQuery);
@@ -283,7 +286,7 @@ private void childRemoved(DataSnapshot dataSnapshot) {
283286
final String key = dataSnapshot.getKey();
284287
final LocationInfo info = this.locationInfos.get(key);
285288
if (info != null) {
286-
this.geoFire.firebaseRefForKey(key).addListenerForSingleValueEvent(new ValueEventListener() {
289+
this.geoFire.getDatabaseRefForKey(key).addListenerForSingleValueEvent(new ValueEventListener() {
287290
@Override
288291
public void onDataChange(DataSnapshot dataSnapshot) {
289292
synchronized(GeoQuery.this) {
@@ -307,7 +310,7 @@ public void run() {
307310
}
308311

309312
@Override
310-
public void onCancelled(FirebaseError firebaseError) {
313+
public void onCancelled(DatabaseError databaseError) {
311314
// tough luck
312315
}
313316
});

src/main/java/com/firebase/geofire/GeoQueryEventListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
package com.firebase.geofire;
3030

31-
import com.firebase.client.FirebaseError;
31+
import com.google.firebase.database.DatabaseError;
3232

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

8080
}

src/main/java/com/firebase/geofire/LocationCallback.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
package com.firebase.geofire;
3030

31-
import com.firebase.client.FirebaseError;
31+
import com.google.firebase.database.DatabaseError;
3232

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

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

5252
}

src/main/java/com/firebase/geofire/example/Example.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.firebase.geofire.example;
22

3-
import com.firebase.client.Firebase;
4-
import com.firebase.client.FirebaseError;
53
import com.firebase.geofire.GeoFire;
64
import com.firebase.geofire.GeoLocation;
75
import com.firebase.geofire.GeoQuery;
86
import com.firebase.geofire.GeoQueryEventListener;
7+
import com.google.firebase.database.DatabaseError;
8+
import com.google.firebase.database.DatabaseReference;
9+
import com.google.firebase.database.FirebaseDatabase;
910

1011
public class Example {
1112

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

3738
@Override
38-
public void onGeoQueryError(FirebaseError error) {
39+
public void onGeoQueryError(DatabaseError error) {
3940
System.err.println("There was an error querying locations: " + error.getMessage());
4041
}
4142
});

0 commit comments

Comments
 (0)