Search in sources :

Example 31 with DatabaseReference

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.");
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 32 with DatabaseReference

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);
        }
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 33 with DatabaseReference

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) {
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 34 with DatabaseReference

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);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 35 with DatabaseReference

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);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)388 DatabaseError (com.google.firebase.database.DatabaseError)219 DataSnapshot (com.google.firebase.database.DataSnapshot)218 ValueEventListener (com.google.firebase.database.ValueEventListener)172 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)100 Intent (android.content.Intent)66 HashMap (java.util.HashMap)59 View (android.view.View)58 ArrayList (java.util.ArrayList)36 TextView (android.widget.TextView)33 Test (org.junit.Test)29 NonNull (androidx.annotation.NonNull)27 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)24 ChildEventListener (com.google.firebase.database.ChildEventListener)23 FirebaseUser (com.google.firebase.auth.FirebaseUser)21 MutableData (com.google.firebase.database.MutableData)21 ImageView (android.widget.ImageView)20 Transaction (com.google.firebase.database.Transaction)20 ProgressDialog (android.app.ProgressDialog)19 Query (com.google.firebase.database.Query)19