Java Code Examples for com.firebase.client.Firebase#setValue()

The following examples show how to use com.firebase.client.Firebase#setValue() . 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: BoardListActivity.java    From AndroidDrawing with MIT License 6 votes vote down vote up
private void createBoard() {
    // create a new board
    final Firebase newBoardRef = mBoardsRef.push();
    Map<String, Object> newBoardValues = new HashMap<>();
    newBoardValues.put("createdAt", ServerValue.TIMESTAMP);
    android.graphics.Point size = new android.graphics.Point();
    getWindowManager().getDefaultDisplay().getSize(size);
    newBoardValues.put("width", size.x);
    newBoardValues.put("height", size.y);
    newBoardRef.setValue(newBoardValues, new Firebase.CompletionListener() {
        @Override
        public void onComplete(FirebaseError firebaseError, Firebase ref) {
            if (firebaseError != null) {
                Log.e(TAG, firebaseError.toString());
                throw firebaseError.toException();
            } else {
                // once the board is created, start a DrawingActivity on it
                openBoard(newBoardRef.getKey());
            }
        }
    });
}
 
Example 2
Source File: MainActivity.java    From cloud-cup-android with Apache License 2.0 5 votes vote down vote up
public void join() {
    Intent intent = new Intent(this, BlankGameActivity.class);

    String codeValue = code.getText().toString();

    String playerName;
    String imageUrl;
    if(mGoogleApiClient.isConnected()) {
        // Get data of current signed-in user
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        playerName = currentPerson.getName().getGivenName();
        imageUrl = getUserImageUrl(currentPerson);

    } else {
        Random rand = new Random();
        playerName = "Anonymous " + rand.nextInt(10);
        imageUrl = "";
    }

    // Register player data in Firebase
    Firebase ref = firebase.child("room/" + codeValue + "/players");
    Firebase pushRef = ref.push();
    Map<String, Object> user = new HashMap<String, Object>();
    user.put("name", playerName);
    user.put("imageUrl", imageUrl);
    user.put("score", 0);
    pushRef.setValue(user);
    String key = pushRef.getKey();

    // Add intent data
    intent.putExtra("playerId", key);
    intent.putExtra("playerName", playerName);
    intent.putExtra("code", codeValue);

    // Start the intent
    startActivity(intent);
}
 
Example 3
Source File: DrawingView.java    From AndroidDrawing with MIT License 5 votes vote down vote up
private void onTouchEnd() {
    mPath.lineTo(mLastX * PIXEL_SIZE, mLastY * PIXEL_SIZE);
    mBuffer.drawPath(mPath, mPaint);
    mPath.reset();
    Firebase segmentRef = mFirebaseRef.push();
    final String segmentName = segmentRef.getKey();
    mOutstandingSegments.add(segmentName);

    // create a scaled version of the segment, so that it matches the size of the board
    Segment segment = new Segment(mCurrentSegment.getColor());
    for (Point point: mCurrentSegment.getPoints()) {
        segment.addPoint((int)Math.round(point.x / mScale), (int)Math.round(point.y / mScale));
    }

    // Save our segment into Firebase. This will let other clients see the data and add it to their own canvases.
    // Also make a note of the outstanding segment name so we don't do a duplicate draw in our onChildAdded callback.
    // We can remove the name from mOutstandingSegments once the completion listener is triggered, since we will have
    // received the child added event by then.
    segmentRef.setValue(segment, new Firebase.CompletionListener() {
        @Override
        public void onComplete(FirebaseError error, Firebase firebaseRef) {
            if (error != null) {
                Log.e("AndroidDrawing", error.toString());
                throw error.toException();
            }
            mOutstandingSegments.remove(segmentName);
        }
    });
}
 
Example 4
Source File: MessageFragment.java    From io16experiment-master with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 5
Source File: PerfTestFirebase.java    From android-database-performance with Apache License 2.0 4 votes vote down vote up
private void deleteAll(Firebase simpleEntityRef) throws InterruptedException {
    simpleEntityRef.setValue(null);
}