Search in sources :

Example 36 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class MapsActivity method onCreate.

/**
 * This method sets up the fields and handles GPS permissions.
 * @param savedInstanceState not used
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    ((MapFragment) getFragmentManager().findFragmentById(R.id.record_map)).getMapAsync(this);
    timer = findViewById(R.id.record_chrono);
    recordButton = findViewById(R.id.record_button);
    pauseButton = findViewById(R.id.pause_button);
    userLocationPool = Executors.newScheduledThreadPool(1);
    userLocationPool.scheduleAtFixedRate(userLocRun, 0, 10, TimeUnit.SECONDS);
    if (checkForLocPermission()) {
        createLocationParameters();
        requestLocationUpdates();
    } else {
        requestLocPermissions();
    }
    Intent intent = getIntent();
    user = (User) intent.getSerializableExtra("user");
    boolean instantRecord = intent.getBooleanExtra("instantRecord", false);
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference myRef = db.getReference(getString(R.string.activity_user_information_firebase, user.getUsername()));
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            userInformation = dataSnapshot.getValue(UserInformation.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("MAPS", "DB call to UserInfo cancelled");
        }
    });
    if (instantRecord)
        toggleRecordStatus(null);
    Switch toggle = (Switch) findViewById(R.id.toggle_report);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            isPublishing = isChecked;
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) Switch(android.widget.Switch) DatabaseReference(com.google.firebase.database.DatabaseReference) MapFragment(com.google.android.gms.maps.MapFragment) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) CompoundButton(android.widget.CompoundButton)

Example 37 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class MapsActivity method retrieveUserLocs.

private void retrieveUserLocs() {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference myRef = db.getReference(getString(R.string.activity_maps_firebase_user_locs));
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> it = dataSnapshot.getChildren().iterator();
            friendLocationList.clear();
            while (it.hasNext()) {
                UserLocation userLocation = it.next().getValue(UserLocation.class);
                friendLocationList.add(userLocation);
            }
            populateMapMarkers();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("MAPSACTIVITY", "Failed to retrieve user locs from Firebase.");
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) Iterator(java.util.Iterator) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 38 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class MapsActivityUnitTests method initUserLoc.

@BeforeClass
public static void initUserLoc() {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference myRef = db.getReference("user_locations");
    UserLocation zLoc = new UserLocation(TEST_FRIEND, System.currentTimeMillis(), 0, 0.0, 0.0);
    myRef.child(TEST_FRIEND).setValue(zLoc);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference) BeforeClass(org.junit.BeforeClass)

Example 39 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class AccountManager method getUser.

/**
 * Retrieves the user account associated with the specified username.  User will be available
 * to the provided callback.
 *
 * @param username Username of the user to be loaded
 * @param listener Callback to handle response
 */
public static void getUser(String username, @NonNull final UserObjectListener listener) {
    Objects.requireNonNull(listener, "Null value for UserObjectListener is not valid.");
    Objects.requireNonNull(user, "Get user called in offline mode before logging in");
    if (!online) {
        if (username.equals(user.getUsername())) {
            listener.onUserPopulated(user);
        } else {
            listener.onUserPopulated(null);
        }
        return;
    }
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + username);
    // 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) {
            // otherwise return null
            if (dataSnapshot.exists()) {
                listener.onUserPopulated(dataSnapshot.getValue(User.class));
            } else {
                listener.onUserPopulated(null);
            }
        }

        @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 40 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TestingHelper method resetTestUserExercises.

/**
 * Test helper to reset the testuser's exercise list
 */
public static void resetTestUserExercises() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/testuser");
    usersReference.child("userExercises").setValue(null);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)382 DatabaseError (com.google.firebase.database.DatabaseError)216 DataSnapshot (com.google.firebase.database.DataSnapshot)215 ValueEventListener (com.google.firebase.database.ValueEventListener)169 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)102 Intent (android.content.Intent)61 HashMap (java.util.HashMap)58 View (android.view.View)56 ArrayList (java.util.ArrayList)36 TextView (android.widget.TextView)34 Test (org.junit.Test)29 NonNull (androidx.annotation.NonNull)24 ChildEventListener (com.google.firebase.database.ChildEventListener)23 ImageView (android.widget.ImageView)22 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)21 FirebaseUser (com.google.firebase.auth.FirebaseUser)21 MutableData (com.google.firebase.database.MutableData)21 Query (com.google.firebase.database.Query)20 Transaction (com.google.firebase.database.Transaction)20 ProgressDialog (android.app.ProgressDialog)19