28
28
29
29
package com .firebase .geofire ;
30
30
31
- import com .firebase .client .*;
32
31
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 ;
34
36
35
37
import java .util .*;
36
38
@@ -46,10 +48,11 @@ public static interface CompletionListener {
46
48
/**
47
49
* Called once a location was successfully saved on the server or an error occurred. On success, the parameter
48
50
* 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
50
53
* @param error The error or null if no error occurred
51
54
*/
52
- public void onComplete (String key , FirebaseError error );
55
+ public void onComplete (String key , DatabaseError error );
53
56
}
54
57
55
58
/**
@@ -73,63 +76,63 @@ public void onDataChange(DataSnapshot dataSnapshot) {
73
76
this .callback .onLocationResult (dataSnapshot .getKey (), location );
74
77
} else {
75
78
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 ));
77
80
}
78
81
}
79
82
}
80
83
81
84
@ Override
82
- public void onCancelled (FirebaseError firebaseError ) {
83
- this .callback .onCancelled (firebaseError );
85
+ public void onCancelled (DatabaseError databaseError ) {
86
+ this .callback .onCancelled (databaseError );
84
87
}
85
88
}
86
89
87
90
static GeoLocation getLocationValue (DataSnapshot dataSnapshot ) {
88
91
try {
89
92
Map data = dataSnapshot .getValue (Map .class );
90
93
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 );
93
96
double latitude = latitudeObj .doubleValue ();
94
97
double longitude = longitudeObj .doubleValue ();
95
98
if (location .size () == 2 && GeoLocation .coordinatesValid (latitude , longitude )) {
96
99
return new GeoLocation (latitude , longitude );
97
100
} else {
98
101
return null ;
99
102
}
100
- } catch (FirebaseException e ) {
101
- return null ;
102
103
} catch (NullPointerException e ) {
103
104
return null ;
104
105
} catch (ClassCastException e ) {
105
106
return null ;
106
107
}
107
108
}
108
109
109
- private final Firebase firebase ;
110
+ private final DatabaseReference databaseReference ;
110
111
111
112
/**
112
113
* 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
114
116
*/
115
- public GeoFire (Firebase firebase ) {
116
- this .firebase = firebase ;
117
+ public GeoFire (DatabaseReference databaseReference ) {
118
+ this .databaseReference = databaseReference ;
117
119
}
118
120
119
121
/**
120
122
* @return The Firebase reference this GeoFire instance uses
121
123
*/
122
- public Firebase getFirebase () {
123
- return this .firebase ;
124
+ public DatabaseReference getDatabaseReference () {
125
+ return this .databaseReference ;
124
126
}
125
127
126
- Firebase firebaseRefForKey (String key ) {
127
- return this .firebase .child (key );
128
+ DatabaseReference getDatabaseRefForKey (String key ) {
129
+ return this .databaseReference .child (key );
128
130
}
129
131
130
132
/**
131
133
* 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
133
136
* @param location The location of this key
134
137
*/
135
138
public void setLocation (String key , GeoLocation location ) {
@@ -138,27 +141,26 @@ public void setLocation(String key, GeoLocation location) {
138
141
139
142
/**
140
143
* 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
143
147
* @param completionListener A listener that is called once the location was successfully saved on the server or an
144
148
* error occurred
145
149
*/
146
150
public void setLocation (final String key , final GeoLocation location , final CompletionListener completionListener ) {
147
151
if (key == null ) {
148
152
throw new NullPointerException ();
149
153
}
150
- Firebase keyRef = this .firebaseRefForKey (key );
154
+ DatabaseReference keyRef = this .getDatabaseRefForKey (key );
151
155
GeoHash geoHash = new GeoHash (location );
152
156
Map <String , Object > updates = new HashMap <String , Object >();
153
157
updates .put ("g" , geoHash .getGeoHashString ());
154
158
updates .put ("l" , new double []{location .latitude , location .longitude });
155
159
if (completionListener != null ) {
156
- keyRef .setValue (updates , geoHash .getGeoHashString (), new Firebase .CompletionListener () {
160
+ keyRef .setValue (updates , geoHash .getGeoHashString (), new DatabaseReference .CompletionListener () {
157
161
@ 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 );
162
164
}
163
165
});
164
166
} else {
@@ -168,6 +170,7 @@ public void onComplete(FirebaseError error, Firebase firebase) {
168
170
169
171
/**
170
172
* Removes the location for a key from this GeoFire.
173
+ *
171
174
* @param key The key to remove from this GeoFire
172
175
*/
173
176
public void removeLocation (String key ) {
@@ -176,20 +179,21 @@ public void removeLocation(String key) {
176
179
177
180
/**
178
181
* 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
180
184
* @param completionListener A completion listener that is called once the location is successfully removed
181
185
* from the server or an error occurred
182
186
*/
183
187
public void removeLocation (final String key , final CompletionListener completionListener ) {
184
188
if (key == null ) {
185
189
throw new NullPointerException ();
186
190
}
187
- Firebase keyRef = this .firebaseRefForKey (key );
191
+ DatabaseReference keyRef = this .getDatabaseRefForKey (key );
188
192
if (completionListener != null ) {
189
- keyRef .setValue (null , new Firebase .CompletionListener () {
193
+ keyRef .setValue (null , new DatabaseReference .CompletionListener () {
190
194
@ 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 );
193
197
}
194
198
});
195
199
} else {
@@ -200,17 +204,18 @@ public void onComplete(FirebaseError error, Firebase firebase) {
200
204
/**
201
205
* Gets the current location for a key and calls the callback with the current value.
202
206
*
203
- * @param key The key whose location to get
207
+ * @param key The key whose location to get
204
208
* @param callback The callback that is called once the location is retrieved
205
209
*/
206
210
public void getLocation (String key , LocationCallback callback ) {
207
- Firebase keyFirebase = this .firebaseRefForKey (key );
211
+ DatabaseReference keyRef = this .getDatabaseRefForKey (key );
208
212
LocationValueEventListener valueListener = new LocationValueEventListener (callback );
209
- keyFirebase .addListenerForSingleValueEvent (valueListener );
213
+ keyRef .addListenerForSingleValueEvent (valueListener );
210
214
}
211
215
212
216
/**
213
217
* Returns a new Query object centered at the given location and with the given radius.
218
+ *
214
219
* @param center The center of the query
215
220
* @param radius The radius of the query, in kilometers
216
221
* @return The new GeoQuery object
0 commit comments