use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.
the class UserInformationActivity method retrieveInfo.
/**
* This method connects to Firebase, retrieving the user information
* stored for a given userId.
*
* @param userId the ID for the user to display
* @return the user info corresponding with the ID if successful, null otherwise
*/
private void retrieveInfo(final String userId) {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference myRef = db.getReference(getString(R.string.activity_user_information_firebase, userId));
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserInformation information = dataSnapshot.getValue(UserInformation.class);
Log.d(TAG, "Retrieved " + userId + " from Firebase.");
if (information != null) {
info = information;
changeDisplayedInfo(info);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "Failed to retrieve " + userId + " from Firebase.");
}
});
}
use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.
the class AccountManager method updateUser.
/**
* Update a user account
*
* @param updatedUser User details
*/
public static void updateUser(final User updatedUser) {
AccountManager.user = updatedUser;
if (online) {
// Ensure that the user is the currently authenticated user
if (lastAuth.equals(updatedUser.getUsername())) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersReference = database.getReference("users/" + updatedUser.getUsername());
usersReference.setValue(updatedUser, null);
}
}
}
use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.
the class AccountManager method authenticate.
/**
* Determines whether the specified users username and password match those stored in the
* database.
*
* @param user User to be authenticated
* @param listener Callback on authentication result
*/
public static void authenticate(final User user, final BooleanResultListener listener) {
// retrieve a reference to the users node
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersReference = database.getReference("users/" + user.getUsername());
// attach a listener for data changes of the users reference. this will occur when
// the reference is populated
usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// always assume authentication fails until proven otherwise
boolean authResult = false;
// if the reference exists, convert it to a user instance
if (dataSnapshot.exists()) {
User userLoggingIn = dataSnapshot.getValue(User.class);
// and return a successful login attempt, otherwise, report a failed login
if (userLoggingIn.getPassword().equals(user.getPassword())) {
AccountManager.setUserLoginState(user.getUsername(), true);
lastAuth = user.getUsername();
online = true;
AccountManager.user = userLoggingIn;
authResult = true;
}
}
// call the listener if there is one with the result of authentication
if (listener != null)
listener.onResult(authResult);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.
the class MapsActivity method updateLocationData.
private static void updateLocationData(final UserLocation userLocation) {
final String username = userLocation.getUsername();
final FirebaseDatabase db = FirebaseDatabase.getInstance();
final DatabaseReference userLocationRef = db.getReference("user_locations");
userLocationRef.child(username).setValue(userLocation);
}
use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.
the class MapsActivity method saveToFirebase.
private static void saveToFirebase(String dbRef, long time, List<Location> locationList) {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference myRef = db.getReference(dbRef);
RecordedWorkout workout = new RecordedWorkout(locationList, time);
String key = myRef.push().getKey();
myRef.child(key).setValue(workout);
}
Aggregations