Java Code Examples for com.firebase.client.Firebase#updateChildren()
The following examples show how to use
com.firebase.client.Firebase#updateChildren() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageFragment.java From io16experiment-master with Apache License 2.0 | 4 votes |
/** * Handle the incoming message from the nearby device. Parse the message to retrieve the appropriate data. */ private void handleIncomingMessage(Message message) { LogUtils.LOGE("***> message", DeviceMessage.fromNearbyMessage(message).getMessageBody()); String[] messageParts = DeviceMessage.fromNearbyMessage(message).getMessageBody().split(Pattern.quote("|")); if (messageParts.length == 0) { LogUtils.LOGE("***> error", "error parsing message"); return; } mFirebaseUid = messageParts[0]; mDeviceNum = Integer.valueOf(messageParts[1]); mTotalDevices = Integer.valueOf(messageParts[2]); String deviceId = messageParts[3]; if (!PreferencesUtils.getBoolean(mActivity, R.string.key_is_connected, false)) { // Update Firebase and remove the old FirebaseUid String currentFirebaseUid = PreferencesUtils.getString(mActivity, R.string.key_firebase_uid, ""); LogUtils.LOGE("***> handleIncomingMessage", "remove firebase uid:" + currentFirebaseUid); Firebase userChallengeRef = new Firebase(Constants.FIREBASE_URL_USERS).child(currentFirebaseUid); userChallengeRef.removeValue(); } // Update the shared preferences PreferencesUtils.setString(mActivity, R.string.key_firebase_uid, mFirebaseUid); PreferencesUtils.setInt(mActivity, R.string.key_total_devices, mTotalDevices); PreferencesUtils.setInt(mActivity, R.string.key_device_number, mDeviceNum); PreferencesUtils.setBoolean(mActivity, R.string.key_is_connected, true); // Display the message displayMessage(); // Update the background color setInitialValues(); // Generate next message, based on Firebase getNextDeviceFirebase(); // Update connected status on Firebase Map<String, Object> device = new HashMap<>(); device.put(Constants.FIREBASE_PROPERTY_CONNECTED, true); Firebase deviceRef = new Firebase(Constants.FIREBASE_URL_DEVICES).child(mFirebaseUid).child(deviceId); deviceRef.updateChildren(device); deviceRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Device d = dataSnapshot.getValue(Device.class); LogUtils.LOGE("***> deviceRef", "here"); if (d == null) { LogUtils.LOGE("***> setupFirebase", "reset device"); MainActivity.resetDevice(); } } @Override public void onCancelled(FirebaseError firebaseError) { } }); if (mDeviceNum < mTotalDevices) { // Change the display text mIntroConnectDevice.setText(mActivity.getString(R.string.text_connect_device, mDeviceNum + 1)); // Change text/button action mDiscover.setOnClickListener(mConnectedClickListener); } else { mIntroConnectDevice.setVisibility(View.GONE); mDiscover.setVisibility(View.GONE); } unsubscribe(); }
Example 2
Source File: MessageFragment.java From io16experiment-master with Apache License 2.0 | 4 votes |
/** * Setup Firebase for the user and connected devices * * Update the existing user data with the number of devices selected on the previous screen * Add the devices based on the selection * Generate the message for the next device */ private void setupFirebase() { // Update Firebase to include the total number of devices if (!TextUtils.isEmpty(mFirebaseUid)) { Map<String, Object> profile = new HashMap<>(); profile.put(Constants.FIREBASE_PROPERTY_NUMBER_DEVICES, mTotalDevices); Firebase userRef = new Firebase(Constants.FIREBASE_URL_USERS).child(mFirebaseUid); userRef.updateChildren(profile); LogUtils.LOGE("***> setupFirebase", "update firebase uid:" + mFirebaseUid); // Add device ids to Firebase for (int i = 0; i < PreferencesUtils.getInt(mActivity, R.string.key_total_devices, 1); i++) { // If the challengeCode is empty, then push a new value to the database Firebase deviceRef = new Firebase(Constants.FIREBASE_URL_DEVICES).child(mFirebaseUid); final Firebase newDeviceRef = deviceRef.push(); mDeviceIds[i] = newDeviceRef.getKey(); final int currentDeviceNumber = i + 1; LogUtils.LOGE("***> new device #" + currentDeviceNumber, mDeviceIds[i]); Device device = new Device(mDeviceIds[i], i + 1, mDeviceNum == currentDeviceNumber); newDeviceRef.setValue(device); newDeviceRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Device d = dataSnapshot.getValue(Device.class); if (d != null) { LogUtils.LOGE("***> child updated", "id: " + d.getDeviceId()); LogUtils.LOGE("***> child updated", "number: " + d.getDeviceNumber()); LogUtils.LOGE("***> child updated", "connected: " + d.isConnected()); LogUtils.LOGE("***> child updated", "next: " + currentDeviceNumber); // Hide the button and show the message for the first device if (d.isConnected() && d.getDeviceNumber() > 1) { PreferencesUtils.setBoolean(mActivity, R.string.key_message_received, true); LogUtils.LOGE("***> child updated", "unpublished: " + currentDeviceNumber); unpublish(); mDiscover.setVisibility(View.GONE); mIntroConnectDevice.setText(mActivity.getString(R.string.text_message_received)); } } else { if (!PreferencesUtils.getBoolean(mActivity, R.string.key_device_reset_done, false)) { LogUtils.LOGE("***> setupFirebase", "reset device"); MainActivity.resetDevice(); } } } @Override public void onCancelled(FirebaseError firebaseError) { } }); // Create the message for the next device if (i == mDeviceNum) { LogUtils.LOGE("***> device", "message created"); String message = mActivity.getString(R.string.message_body, mFirebaseUid, mDeviceNum + 1, mTotalDevices, newDeviceRef.getKey()); mDeviceInfoMessage = DeviceMessage.newNearbyMessage( InstanceID.getInstance(mActivity.getApplicationContext()).getId(), message); } } } }